AdodbZip1.1有更新,更新了下載地址。
SmartyZip,一個Smarty的裝載類
<?php教程
// 設定參數
SmartyZip::$zip_url = 'http://www.smarty.net教程/distributions/Smarty-2.6.26.zip'; //[設定項]Smarty的Zip檔案下載地址
SmartyZip::$zip_file = sys_get_temp_dir () . preg_replace ( '/^.*/(Smarty-.*.zip)$/i', 'smarty/$1', SmartyZip::$zip_url ); //[設定項]Smarty的Zip檔案快取位置
SmartyZip::$entry_dir = preg_replace ( '/^.*/(Smarty-.*).zip$/i', '$1/libs', SmartyZip::$zip_file );
SmartyZip::$extract_dir = sys_get_temp_dir () . 'smarty/' . SmartyZip::$entry_dir; //[設定項]Smarty程式檔案快取位置
SmartyZip::$template_dir = dirname ( realpath ( __FILE__ ) ); //[設定項]Smarty模板檔案 所在位置
SmartyZip::$compile_dir = sys_get_temp_dir () . 'smarty/template_c/' . md5 ( SmartyZip::$template_dir ); //[設定項]Smarty編譯檔案快取位置
// 註冊協議
if (! in_array ( 'SmartyZip', stream_get_wrappers () )) {
stream_wrapper_register ( 'SmartyZip', 'SmartyZip' );
}
// 定義常量
if (! defined ( 'SMARTY_DIR' )) {
define ( 'SMARTY_DIR', 'SmartyZip://' );
}
// 包含程式
require_once (SMARTY_DIR . 'Smarty.class.php');
// $smarty = SmartyZip::init(new Smarty); // [選擇項]引用即定義$smarty
// return SmartyZip::init(new Smarty); // [選擇項]引用即返回$smarty,注意只可引用一次。
/**
* SmartyZip類定義
*/
class SmartyZip {
/**
* Smarty變數
*/
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變數
*/
private $handle;
public $context;
/**
* Smarty函數組
*/
/**
* 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 ( '請建立目錄 ' . self::$compile_dir );
}
}
$smarty->compile_dir = self::$compile_dir;
return $smarty;
}
/**
* Stream函數組
*/
/**
* __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) {
// 驗證檔案地址
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;
// 驗證程式檔案
if (! file_exists ( $tmp_file ) || file_exists ( $zip_file ) && filectime ( $tmp_file ) < filectime ( $zip_file )) {
// 下載檔案
if (! file_exists ( $zip_file )) {
// 目錄處理
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 ( '請建立目錄 ' . $zip_dir );
}
}
// 下載檔案
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'] : 80, $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 ( '請下載檔案 <a href="' . self::$zip_url . '">' . self::$zip_url . '</a > 儲存為 ' . self::$zip_file );
}
}
}
// 建立目錄
$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 ( '請建立目錄 ' . $tmp_dir );
}
}
// 開啟壓縮檔
$zip = zip_open ( $zip_file );
if (! is_resource ( $zip )) {
return false;
}
// 尋找解壓檔案
do {
$entry = zip_read ( $zip );
if (! is_resource ( $entry )) {
return false;
}
if (zip_entry_name ( $entry ) == $entry_file) {
break;
}
} while ( true );
// 轉存壓縮檔
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 );
}
// 開啟檔案
$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 $arg1
* @param int $arg2
* @return bool
*/
public function stream_set_option($option, $arg1, $arg2) {
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' => 0, 'size' => 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0 );
return array_merge ( array_values ( $arr ), $arr );
}
return false;
}
}
?>
使用Smarty很簡單:
1. 包含SmartyZip檔案。
2. 使用SmartyZip::init(new Smarty)自動為smarty對象設定預設路徑。
範例代碼: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 ?>