Smartyzip, a Smarty loading class

Source: Internet
Author: User
Tags class definition create directory lstat mkdir smarty template

AdodbZip1.1 has updated and updated the download address.


Smartyzip, a Smarty loading class
<?php Tutorial

Setting parameters
Smartyzip:: $zip _url = ' http://www.smarty.net tutorial/distributions/smarty-2.6.26.zip '; [Set item]smarty zip file download Address
Smartyzip:: $zip _file = Sys_get_temp_dir (). Preg_replace ('/^.*/(smarty-.*.zip) $/i ', ' smarty/$1 ', Smartyzip:: $zip _url); [Set item]smarty ZIP file cache location
Smartyzip:: $entry _dir = preg_replace ('/^.*/(smarty-.*). zip$/i ', ' $1/libs ', Smartyzip:: $zip _file);
Smartyzip:: $extract _dir = Sys_get_temp_dir (). ' smarty/'. Smartyzip:: $entry _dir; [Set Item]smarty program file cache location
Smartyzip:: $template _dir = dirname (Realpath (__file__)); [Set Item]smarty template file location
Smartyzip:: $compile _dir = Sys_get_temp_dir (). ' smarty/template_c/'. MD5 (smartyzip:: $template _dir); [Set item]smarty compile file cache location


Registration Agreement
if (! In_array (' Smartyzip ', Stream_get_wrappers ())) {
Stream_wrapper_register (' Smartyzip ', ' smartyzip ');
}
Defining constants
if (! defined (' Smarty_dir ')) {
Define (' Smarty_dir ', ' smartyzip://');
}
Include program
Require_once (Smarty_dir. ' Smarty.class.php ');
$smarty = Smartyzip::init (new smarty); [Select item] reference is defined $smarty
Return Smartyzip::init (new Smarty); The Select item reference returns $SMARTY, noting that it can only be referenced once.


/**
* Smartyzip class definition
*/
Class Smartyzip {

/**
* Smarty Variable
*/
public static $zip _url;
public static $zip _file;
public static $entry _dir;
public static $extract _dir;
public static $template _dir;
public static $compile _dir;

/**
* Stream Variable
*/
Private $handle;
Public $context;

/**
* Smarty Function Group
*/

/**
* Init
* @param Smarty & $smarty
* @return Smarty
*/
public static function init (& $smarty) {
$smarty->template_dir = self:: $template _dir;
if (! Is_dir (self:: $compile _dir)) {
if (MkDir (self:: $compile _dir, 0777, true) = = False) {
Header (' Content-type:text/html;charset=utf-8 ');
Die (' Please create directory '. Self:: $compile _dir);
}
}
$smarty->compile_dir = self:: $compile _dir;
return $smarty;
}

/**
* Stream Function Group
*/

/**
* __construct
*/
Public Function __construct () {
}

/**
* Stream_cast
* @param int $cast _as
* @return Resource
*/
Public Function Stream_cast ($cast _as) {
return false;
}

/**
* Stream_close
*/
Public Function Stream_close () {
Fclose ($this->handle);
}

/**
* stream_eof
* @return BOOL
*/
Public Function stream_eof () {
Return feof ($this->handle);
}

/**
* Stream_flush
* @return BOOL
*/
Public Function Stream_flush () {
Return Fflush ($this->handle);
}

/**
* Stream_lock
* @param mode $options
* @return BOOL
*/
Public Function Stream_lock ($options) {
Return Flock ($this->handle, $options);
}

/**
* Stream_open
* @param string $path
* @param string $mode
* @param int $options
* @param string & $opend _path
* @return BOOL
*/
Public Function Stream_open ($path, $mode, $options, & $opend _path) {
Verify file Address
if (! Preg_match ('/^.*?:/ /(. *) $/', $path, $matches)) {
return false;
}
$tmp _file = self:: $extract _dir. Directory_separator. $matches [1];
$entry _file = self:: $entry _dir. '/' . Str_replace (' \ ', '/', $matches [1]);
$zip _file = self:: $zip _file;
Validator Files
if (! file_exists ($tmp _file) | | file_exists ($zip _file) && filectime ($tmp _file) < Filectime ($zip _file )) {
Download files
if (! file_exists ($zip _file)) {
Directory processing
if (! Is_dir (DirName (self:: $zip _file))) {
if (mkdir (DirName (self:: $zip _file), 0777, true) = = False) {
Header (' Content-type:text/html;charset=utf-8 ');
Die (' Please create directory '. $zip _dir);
}
}
Download files
if (! file_exists (self:: $zip _file)) {
$break = true;
do {
$url _arr = Parse_url (self:: $zip _url);
$fp = Fsockopen ($url _arr [' Host '], isset ($url _arr [' Port '])? (int) $url _arr [' Port ']: $errno, $ERRSTR, 10);
if ($fp = = False) {
Break
}
$out = "Get". $url _arr [' Path ']. "Http/1.0rnhost:". $url _arr [' Host ']. "Rnconnection:closernrn";
Fputs ($fp, $out);
if (feof ($fp)) {
Break
}
$buffer = fgets ($fp, 1024);
if (! Preg_match ('/^http/1.d 200/i ', $buffer)) {
Break
}
$content _length = false;
$content _start = false;
while (! feof ($fp)) {
$buffer = fgets ($fp, 1024);
if ($buffer = = = "RN") {
$content _start = true;
Break
}
if (Preg_match ('/^content-length:s* (d+)/I ', $buffer, $matches)) {
$content _length = (int) $matches [1];
}
}
if ($content _length = = False | | $content _start = = False) {
Break
}
$content = Stream_get_contents ($FP);
if ($content = = False) {
Break
}
$result = File_put_contents (self:: $zip _file, $content);
Unset ($content);
if ($result = = False) {
Break
}
Fclose ($FP);
while ($break = false);
if ($break) {
Header (' Content-type:text/html;charset=utf-8 ');
Die (' Please download the file <a href= '. Self:: $zip _url. ' > '. Self:: $zip _url. ' </a > Save as '. Self:: $zip _file);
}
}
}
Create a table of contents
$tmp _dir = dirname ($tmp _file);
if (! Is_dir ($tmp _dir)) {
if (mkdir ($tmp _dir, 0777, true) = = False) {
Header (' Content-type:text/html;charset=utf-8 ');
Die (' Please create directory '. $tmp _dir);
}
}
Open compressed file
$zip = Zip_open ($zip _file);
if (! Is_resource ($zip)) {
return false;
}
Looking for uncompressed files
do {
$entry = Zip_read ($zip);
if (! Is_resource ($entry)) {
return false;
}
if (Zip_entry_name ($entry) = = $entry _file) {
Break
}
} while (true);
Dump compressed Files
Zip_entry_open ($zip, $entry);
File_put_contents ($tmp _file, Zip_entry_read ($entry, Zip_entry_filesize ($entry));
Zip_entry_close ($entry);
Zip_close ($zip);
}
Open File
$this->handle = fopen ($tmp _file, $mode);
if (! Is_resource ($this->handle)) {
return false;
}
return true;
}

/**
* Stream_read
* @param int $count
* @return String
*/
Public Function Stream_read ($count) {
Return Fread ($this->handle, $count);
}

/**
* Stream_seek
* @param int $offset
* @param int $whence =seek_set
* @return BOOL
*/
Public Function Stream_seek ($offset, $whence = Seek_set) {
Return fseek ($this->handle, $offset, $whence);
}

/**
* Stream_set_option
* @param int $option
* @param int $arg 1
* @param int $arg 2
* @return BOOL
*/
Public Function Stream_set_option ($option, $arg 1, $arg 2) {
return false;
}

/**
* Stream_stat
* @return Array
*/
Public Function Stream_stat () {
Return Fstat ($this->handle);
}

/**
* Stream_tell
* @return int
*/
Public Function Stream_tell () {
Return Ftell ($this->handle);
}

/**
* Stream_write
* @param string $data
* @return int
*/
Public Function Stream_write ($data) {
Return fwrite ($this->handle, $data);
}

/**
* Url_stat
* @param string $path
* @param int $flag
* @return Array
*/
Public Function Url_stat ($path, $flag) {
if (! Preg_match ('/^.*?:/ /(. *) $/', $path, $matches)) {
return false;
}
$tmp _file = self:: $extract _dir. Directory_separator. $matches [1];
if (file_exists ($tmp _file)) {
if ($flag & Stream_url_stat_link) {
Return Lstat ($tmp _file);
} else {
Return stat ($tmp _file);
}
}
if ($flag & Stream_url_stat_quiet) {
$file _stat = Lstat (__file__);
$mode = $file _stat [' mode '];
Smarty Plugins Adjust
if (Strpos ($matches [1], ' plugins '. Directory_separator. ' Compiler. ') !== false) {
if (Strpos ($matches [1], ' plugins '. Directory_separator. ' compiler.assign.php ') = = False) {
$mode = 0;
}
} elseif (Strpos ($matches [1], ' plugins '. Directory_separator. ' Block. ') !== false) {
if (Strpos ($matches [1], ' plugins '. Directory_separator. ' block.textformat.php ') = = False) {
$mode = 0;
}
}
$arr = Array (' dev ' => 0, ' ino ' => 0, ' mode ' => $mode, ' Nlink ' => 0, ' uid ' => 0, ' gid ' => 0, ' Rdev ' =&gt ; 0, ' size ' => 0, ' atime ' => 0, ' Mtime ' => 0, ' CTime ' => 0, ' blksize ' => 0, ' blocks ' => 0);
Return Array_merge (Array_values ($arr), $arr);
}
return false;
}
}
?>

Using Smarty is simple:

1. Contains smartyzip files.
2. Use Smartyzip::init (new Smarty) to automatically set the default path for the Smarty object.

Sample code: View Sourceprint?1 <?php

2 include_once ' smartyzip.php ';

3 $smarty = smartyzip::init (new smarty);

4 $smarty->assign (' name ', ' Smartyzip ');

5 $smarty->display (' Test.<span class=t_tag onclick=tagshow (event) href= "tag.php?name=html" >html</span > ');

6?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.