public static function create($dir, $mode = 0755) { if (is_dir($dir) || @mkdir($dir, $mode)) { return true; } if (!dir::create(dirname($dir), $mode)) { return false; } return @mkdir($dir, $mode); }
/** * 创建目录 * @staticvar array $dirArr * @param type $dir * @return boolean */ private function createDir($dir) { static $dirArr = array(); if (isset($dirArr[$dir])) { return true; } if (!dir::create($dir)) { return false; } $dirArr[$dir] = true; return true; }
public function run() { if (C("SESSION_SAVE_PATH")) { session_save_path(C("SESSION_SAVE_PATH")); } if (!session_save_path()) { dir::create(ROOT_PATH . '/session'); session_save_path(ROOT_PATH . '/session'); } session_name(C("SESSION_NAME")); session_set_save_handler(array(&$this, "open"), array(&$this, "close"), array(&$this, "read"), array(&$this, "write"), array(&$this, "destroy"), array(&$this, "gc")); }
/** * 设置缓存数据 * @param type $args * @return type */ public function set($name, $data, $time = null, $path = null) { $cacheFile = $this->getCacheFile($name, $path); if ($this->isValid($name, $time, $path)) { //缓存是否有效 return true; } $data = "<?php \r\n if(!defined('PATH_HD'))exit;\r\n" . $this->createCatchData($name, $data) . "?>"; dir::create(dirname($cacheFile)); //创建缓存目录 if (file_put_contents($cacheFile, $data)) { return true; } }
/** * 模板显示 * @param string $tpl_file 模板文件 * @param number $cacheTime 缓存时间 * @param string $contentType 文件类型 * @param string $charset 字符集 * @param boolean $show 是否显示 */ public function display($tplFile = "", $cacheTime = null, $contentType = "text/html", $charset = "", $show = true) { $this->endFix = '.' . trim(C("TPL_FIX"), '.'); $this->tplFile = $this->getTemplateFile($tplFile); $this->set_cache_file(); //设置缓存文件 $this->set_cache_time($cacheTime); //设置缓存时间 $this->compileFile = CACHE_COMPILE_PATH . '/' . md5_d($this->tplFile) . '.php'; if (C("debug")) { tplCompile(array(basename($this->tplFile), $this->compileFile)); //记录模板编译文件 } $content = false; //静态缓存数据内容 if ($this->cacheTime > 0) { //缓存控制ssp dir::create(CACHE_TPL_PATH); //缓存目录 if ($this->cacheStat || $this->is_cache($this->cacheTime)) { $content = file_get_contents($this->cacheFile); } } if ($content === false) { //不使用缓存 if ($this->checkCompile($tplFile)) { //编译文件失效 $this->compile(); } $_CONFIG = C(); $_LANGUAGE = L(); if (!empty(self::$vars)) { //加载全局变量 extract(self::$vars); } ob_start(); include $this->compileFile; $content = ob_get_clean(); if ($this->cacheTime > 0) { file_put_contents($this->cacheFile, $content); } } if ($show) { $charset = strtoupper(C("CHARSET")) == 'UTF8' ? "UTF-8" : strtoupper(C("CHARSET")); header("Content-type:" . $contentType . ';charset=' . $charset); echo $content; } else { return $content; } }
public function run() { $this->content = file_get_contents($this->tplFile); //获得模板内容 $this->replaceAssignVar(); $this->loadParseTags(); //加载标签库 及解析标签 $this->compile(); //解析全局内容 $this->replaceConst(); //将所有常量替换 如把__APP__进行替换 $this->content = '<?php if(!defined("PATH_HD"))exit;?>' . "\r\n" . $this->content; if (!is_dir(CACHE_COMPILE_PATH)) { dir::create(CACHE_COMPILE_PATH); } file_put_contents($this->compileFile, $this->content); }
public static function write($message, $type = 3, $destination = NULL, $extra_headers = NULL) { if (!C("LOG_START")) { return; } dir::create(PATH_LOG); if (is_null($destination)) { $destination = PATH_LOG . '/' . date("ymd") . md5(C("LOG_KEY")) . ".log"; } if ($type == 3) { if (is_file($destination) && filesize($destination) > C("LOG_SIZE")) { rename($destination, dirname($destination) . "/" . time() . ".log"); } } $now = date("y-m-d h:i:s"); $message = $message . "\t[时间]" . $now . "\r\n"; error_log($message, $type, $destination, $extra_headers = null); }
/** * 取得联动分类或城市的缓存数据值 * @param string $type city or linkage * @return 换成数据数组 */ private function _getLinkageCache($type = 'linkage') { $field = array('laid', 'title'); if ($type == 'city') { $field = array('id', 'name'); } $cache = array(); $path = PATH_ROOT . '/caches/linkage'; $file = $path . '/' . $type . '.php'; if (file_exists($file)) { $cache = (include $file); } else { dir::create($path); $db = M($type); $linkages = $db->field(implode(',', $field))->findall(); foreach ($linkages as $value) { $cache[$value[$field[0]]] = $value[$field[1]]; } file_put_contents($file, '<?php if(!defined("PATH_HD")){exit;}return ' . var_export($cache, TRUE) . ';'); } return $cache; }
public function add() { if (IS_POST) { $data = $_POST; $data['has_adminlist'] = isset($_POST['has_adminlist']) ? 1 : 0; //有后台 $data['has_outurl'] = isset($_POST['has_outurl']) ? 1 : 0; //前台访问 $data['config'] = isset($_POST['config']) ? 1 : 0; //有配置文件 //-------------------检查必要信息 $this->db->validate = array(array('name', 'nonull', '插件标识不能为空!', 2, 3), array('name', 'regexp:/^[a-zA-Z][a-zA-Z0-9_]+$/i', '插件标识只支持英文、数字', 2, 3), array('name', 'addonUniqueCheck', '该插件已经存在!', 2, 3), array('title', 'nonull', '插件名称不能为空!', 2, 3), array('version', 'nonull', '插件版本不能为空!', 2, 3), array('author', 'nonull', '插件作者不能为空!', 2, 3), array('description', 'nonull', '插件描述不能为空!', 2, 3)); if (!$this->db->validate($data)) { $this->error($this->db->error); } $data['name'] = ucfirst($data['name']); $addonDir = APP_ADDON_PATH . $data['name'] . '/'; //插件目录 //验证安装目录 if (!is_writable(APP_ADDON_PATH)) { $this->error(APP_ADDON_PATH . ' 不可写'); } is_dir($addonDir) or dir::create($addonDir); //创建插件目录 //-------------------创建配置文件 if ($data['config']) { copy(HDPHP_PATH . 'Lib/Tpl/configAddon.php', $addonDir . 'config.php'); } //-----------------控制器 if ($data['has_adminlist'] || $data['has_outurl']) { //创建控制器目录 is_dir($addonDir . 'Controller') or Dir::create($addonDir . 'Controller'); } if ($data['has_adminlist']) { $controller = <<<str <?php /** * {$data['name']} 插件 * @author 后盾向军 <*****@*****.**> */ class AdminController extends AddonController { public function index() { \$this->display(); } } str; file_put_contents($addonDir . 'Controller/AdminController.class.php', $controller); } //创建前台访问控制器 if ($data['has_outurl']) { $controller = <<<str <?php /** * {$data['name']} 插件 * @author 后盾向军 <*****@*****.**> */ class IndexController extends AddonController { public function index() { \$this->display(); } } str; file_put_contents($addonDir . 'Controller/IndexController.class.php', $controller); } $addonData = <<<str <?php /** * {$data['name']} 插件 * @author 后盾向军 <*****@*****.**> */ class {$data['name']}Addon extends Addon { //插件信息 public \$info = array( 'name' => '{$data['name']}', 'title' => '{$data['title']}', 'description' => '{$data['description']}', 'status' => 1, 'author' => '{$data['author']}', 'version' => '{$data['version']}', 'has_adminlist' => {$data['has_adminlist']}, ); //安装 public function install() { return true; } //卸载 public function uninstall() { return true; } str; if (isset($data['hooks'])) { foreach ($data['hooks'] as $hook) { $addonData .= "\n //实现的{$hook}钩子方法\n public function {$hook}(\$param){\n }\n"; } } $addonData .= '}'; file_put_contents($addonDir . $data['name'] . 'Addon.class.php', $addonData); //创建View视图文件 if ($data['has_adminlist']) { Dir::create($addonDir . 'View/Admin'); copy(MODULE_PATH . 'Data/View/addonAdmin.php', $addonDir . 'View/Admin/index.php'); } if ($data['has_outurl']) { Dir::create($addonDir . 'View/Index'); copy(MODULE_PATH . 'Data/View/addonIndex.php', $addonDir . 'View/Index/index.php'); } $this->display('success.php'); } else { $this->assign('hooks', K('Hooks')->all()); $this->display(); } }
/** * 创建项目运行目录 * @access private * @return void */ public static function mkDirs() { if (is_dir(APP_COMMON_PATH)) { return; } //目录 $dirs = array(APP_PATH, TEMP_PATH, APP_COMMON_PATH, APP_CONFIG_PATH, APP_ADDON_PATH, APP_MODEL_PATH, APP_CONTROLLER_PATH, APP_LANGUAGE_PATH, APP_HOOK_PATH, APP_TAG_PATH, APP_LIB_PATH, APP_COMPILE_PATH, APP_CACHE_PATH, APP_TABLE_PATH, APP_LOG_PATH, MODULE_CONTROLLER_PATH, MODULE_CONFIG_PATH, MODULE_LANGUAGE_PATH, MODULE_MODEL_PATH, MODULE_HOOK_PATH, MODULE_TAG_PATH, MODULE_LIB_PATH, MODULE_VIEW_PATH, CONTROLLER_VIEW_PATH, MODULE_PUBLIC_PATH, STATIC_PATH); foreach ($dirs as $d) { if (!dir_create($d, 0755)) { header("Content-type:text/html;charset=utf-8"); exit("目录{$d}创建失败,请检查权限"); } } //复制视图 is_file(CONTROLLER_VIEW_PATH . "index.html") or copy(HDPHP_PATH . 'Lib/Tpl/view.html', CONTROLLER_VIEW_PATH . "index.html"); //复制公共模板文件 is_file(MODULE_PUBLIC_PATH . "success.html") or copy(HDPHP_PATH . 'Lib/Tpl/success.html', MODULE_PUBLIC_PATH . "success.html"); is_file(MODULE_PUBLIC_PATH . "error.html") or copy(HDPHP_PATH . 'Lib/Tpl/error.html', MODULE_PUBLIC_PATH . "error.html"); //复制模块配置文件 is_file(MODULE_CONFIG_PATH . "config.php") or copy(HDPHP_PATH . 'Lib/Tpl/configModule.php', MODULE_CONFIG_PATH . "config.php"); is_file(APP_CONFIG_PATH . "config.php") or copy(HDPHP_PATH . 'Lib/Tpl/configApp.php', APP_CONFIG_PATH . "config.php"); //创建测试控制器 is_file(MODULE_CONTROLLER_PATH . 'IndexController.class.php') or file_put_contents(MODULE_CONTROLLER_PATH . 'IndexController.class.php', file_get_contents(HDPHP_PATH . 'Lib/Tpl/controller.php')); //创建安全文件 self::safeFile(); //批量创建模块 if (defined('MODULE_LIST')) { $module = explode(',', MODULE_LIST); if (!empty($module)) { foreach ($module as $m) { dir::create(APP_PATH . $m); dir::copy(MODULE_PATH, APP_PATH . $m); } } } }
/** * * 图片裁切处理 * @param $img 操作的图片文件路径 * @param $outfile 输出文件路径 * @param path 缩略图存放路径 * @param $t_type 裁切图片的方式 * @param $t_w 缩略图宽度 * @param $t_h 缩略图高度 * return $string 处理后的文件名 */ public function thumb($img, $outfile = '', $path = '', $t_w = '', $t_h = '', $t_type = '') { if (!$this->check($img)) { return false; } //基础配置 $t_type = $t_type ? $t_type : $this->thumb_type; $t_w = $t_w ? $t_w : $this->thumb_width; $t_h = $t_h ? $t_h : $this->thumb_height; $path = $path ? $path : C("THUMB_PATH"); //获得图像信息 $img_info = getimagesize($img); $img_w = $img_info[0]; $img_h = $img_info[1]; $img_type = image_type_to_extension($img_info[2]); //获得相关尺寸 $thumb_size = $this->thumb_size($img_w, $img_h, $t_w, $t_h, $t_type); //原始图像资源 $func = "imagecreatefrom" . substr($img_type, 1); $res_img = $func($img); //缩略图的资源 if ($img_type == '.gif') { $res_thumb = imagecreate($thumb_size[0], $thumb_size[1]); $color = imagecolorallocate($res_thumb, 255, 0, 0); } else { $res_thumb = imagecreatetruecolor($thumb_size[0], $thumb_size[1]); imagealphablending($res_thumb, false); //关闭混色 imagesavealpha($res_thumb, true); //储存透明通道 } //绘制缩略图X if (function_exists("imagecopyresampled")) { imagecopyresampled($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0], $thumb_size[1], $thumb_size[2], $thumb_size[3]); } else { imagecopyresized($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0], $thumb_size[1], $thumb_size[2], $thumb_size[3]); } //处理透明色 if ($img_type == '.gif') { imagecolortransparent($res_thumb, $color); } //配置输出文件名 $imgInfo = pathinfo($img); $outfile = $outfile ? $outfile : $this->thumb_prefix . $imgInfo['filename'] . $this->thumb_endfix . "." . $imgInfo['extension']; $upload_dir = $path ? $path : dirname($img); dir::create($upload_dir); $outfile = $upload_dir . '/' . $outfile; $func = "image" . substr($img_type, 1); $func($res_thumb, $outfile); if (isset($res_img)) { imagedestroy($res_img); } if (isset($res_thumb)) { imagedestroy($res_thumb); } return $outfile; }
/** * 验证目录 * @param string $path 目录 */ public function checkDir($path) { return dir::create($path) && is_writeable($path) ? true : false; }
/** * 设置SESSION存储路径 * @param type $path */ static function set_save_path($path) { self::start(); if (!is_dir($path)) { dir::create($path); } session_save_path($path); }
public function getSaveFile($savepath, $filename) { //返回实际目录 $dir = ZOTOP_PATH_ROOT . DS . $savepath; //目录检测 if (!is_dir($dir) && !dir::create($dir, 0777)) { $this->error = 8; //目录不存在且无法自动创建 return false; } @chmod($dir, 0777); if (!is_writeable($dir) && $dir != '/') { $this->error = 9; //不可写 return false; } $savefile = $dir . $filename; return $savefile; }
/** * 写入文件 * * @param string $file * @param string $content * @param boolean $overwrite * @return boolean */ public static function write($file, $content = '', $overwrite = TRUE) { $file = path::decode($file); //当目录不存在的情况下先创建目录 if (!dir::exists(dirname($file))) { dir::create(dirname($file)); } if (!file::exists($file) || $overwrite) { return @file_put_contents($file, $content); } return false; }
/** * 创建项目缓存目录结构 */ private static function createDir() { if (!is_dir(CACHE_APP_PATH)) { dir::create(CACHE_APP_PATH); } }
public function dbBackup() { //有POST先将要备份的表存到文件 //然后go(__METH__.'/table/1/page/1') if ($_SERVER['REQUEST_METHOD'] == 'POST') { $created = time(); $filename = PATH_ROOT . '/caches/db/' . $created; dir::create($filename); $tables = $_POST['table']; file_put_contents($filename . '/backup_tables.php', '<?php return ' . var_export($tables, true) . ';'); $limit = '/limit/500'; $filesize = '/filesize/2048000'; if (isset($_POST['limit'])) { $limit = '/limit/' . $_POST['limit']; } if (isset($_POST['filesize'])) { $filesize = '/filesize/' . $_POST['filesize']; } go(__METH__ . '/table/1/page/1/file/1/folder/' . $created . $limit . $filesize); } if (isset($_GET['table'])) { $table_index = $_GET['table'] - 1; $db_backup = new DB_backup($_GET['filesize']); if ($db_backup->backup($table_index)) { $this->success('数据备份成功!', 'dbTool'); } } }
/** * 上传文件 * * @param string $name FILE字段名称 * @param string $path 上传的路径 * @param string $ext 扩展名 * @param boolean $rename 是否重新命名 * @return array */ public static function upload($name, $path, $ext, $rename = true) { if (!dir::exists(dirname($path))) { dir::create(dirname($path)); } $ext = explode(',', $ext); $files = $_FILES[$name]; $attachments = array(); //转换数组 if (is_array($files['name'])) { foreach ($files as $key => $var) { foreach ($var as $id => $val) { $attachments[$id][$key] = $val; } } } else { $attachments[] = $files; } //上传 $return = array(); foreach ($attachments as $k => $file) { if (in_array(self::ext($file['name']), $ext)) { $tmp = $path; if ($rename) { $tmp .= DS . rand::string(10) . self::ext($file['name']); } else { $tmp .= DS . $file['name']; } @move_uploaded_file($file['name'], $tmp); $return[] = $tmp; @unlink($file['tmp_name']); } else { $return[] = false; } } return $return; }
public function add() { if (IS_POST) { $data = $_POST; $data['has_adminlist'] = isset($_POST['has_adminlist']) ? 1 : 0; //有后台 $data['has_outurl'] = isset($_POST['has_outurl']) ? 1 : 0; //前台访问 $data['config'] = isset($_POST['config']) ? 1 : 0; //有配置文件 $data['viewTag'] = isset($_POST['viewTag']) ? 1 : 0; //有前台标签文件 //字段验证 $this->db->validate = array(array('name', 'nonull', '插件标识不能为空!', 2, 3), array('name', 'regexp:/^[a-zA-Z]+$/i', '插件标识必须为英文字母', 2, 3), array('name', 'addonUniqueCheck', '该插件已经存在!', 2, 3), array('title', 'nonull', '插件名称不能为空!', 2, 3), array('version', 'nonull', '插件版本不能为空!', 2, 3), array('author', 'nonull', '插件作者不能为空!', 2, 3), array('description', 'nonull', '插件描述不能为空!', 2, 3)); //验证插件数据合法性 if (!$this->db->validate($data)) { $this->error($this->db->error); } //插件名首字母大小 $data['name'] = ucfirst($data['name']); //验证安装目录 if (!is_writable(APP_ADDON_PATH)) { $this->error(APP_ADDON_PATH . ' 不可写'); } //-------------------插件目录---------------------- $addonDir = APP_ADDON_PATH . $data['name'] . '/'; if (!dir::create($addonDir)) { $this->error('插件目录创建失败'); } //-------------------配置文件---------------------- if ($data['config']) { copy(MODULE_PATH . 'Data/Addon/configAddon.php', $addonDir . 'config.php'); } //-------------------标签目录------------------ if ($data['viewTag']) { if (!dir::create($addonDir . 'Tag')) { $this->error('标签Tag目录创建失败'); } $viewTagPhp = <<<tag <?php //标签类文件命名规范:Addon插件名Tag class Addon{$data['name']}Tag { //声明标签 public \$tag = array( 'addon_{$data['name']}_test' => array('block' => 1, 'level' => 4), ); //示例标签 //a) 标签命名规范:_addon_插件名_标签 //b) 插件安装后才可以使用标签 //c) 模板使用<addon_{$data['name']}_test> </addon_{$data['name']}_test>调用 public function _addon_{$data['name']}_test(\$attr, \$content) { return '这是标签测试结果'; } } tag; file_put_contents($addonDir . 'Tag/Addon' . $data['name'] . 'Tag.class.php', $viewTagPhp); } //--------------------控制器目录---------------------- if ($data['has_adminlist'] || $data['has_outurl']) { if (!Dir::create($addonDir . 'Controller')) { $this->error('控制器目录创建失败'); } } //--------------------后台控制器---------------------- if ($data['has_adminlist']) { $controller = <<<str <?php /** * {$data['name']} 插件 * @author 后盾向军 <*****@*****.**> */ class AdminController extends AddonController { public function index() { \$this->display(); } } str; file_put_contents($addonDir . 'Controller/AdminController.class.php', $controller); } //--------------------前台控制器---------------------- if ($data['has_outurl']) { $controller = <<<str <?php /** * {$data['name']} 插件 * @author 后盾向军 <*****@*****.**> */ class IndexController extends AddonController { public function index() { \$this->display(); } } str; file_put_contents($addonDir . 'Controller/IndexController.class.php', $controller); } //--------------------插件控制器---------------------- $addonData = <<<str <?php /** * {$data['name']} 插件 * @author 后盾向军 <*****@*****.**> */ class {$data['name']}Addon extends Addon { //插件信息 public \$info = array( 'name' => '{$data['name']}', 'title' => '{$data['title']}', 'description' => '{$data['description']}', 'status' => 1, 'author' => '{$data['author']}', 'version' => '{$data['version']}', 'has_adminlist' => {$data['has_adminlist']}, ); //安装 public function install() { return true; } //卸载 public function uninstall() { return true; } str; if (isset($data['hooks'])) { foreach ($data['hooks'] as $hook) { $addonData .= "\n //实现的{$hook}钩子方法\n public function {$hook}(\$param){\n }\n"; } } $addonData .= '}'; file_put_contents($addonDir . $data['name'] . 'Addon.class.php', $addonData); //创建View视图文件 if ($data['has_adminlist']) { Dir::create($addonDir . 'View/Admin'); copy(MODULE_PATH . 'Data/Addon/addonAdmin.php', $addonDir . 'View/Admin/index.php'); } if ($data['has_outurl']) { Dir::create($addonDir . 'View/Index'); copy(MODULE_PATH . 'Data/Addon/addonIndex.html', $addonDir . 'View/Index/index.html'); } $this->success('安装成功,请刷新后台'); } else { $this->assign('hooks', M('hooks')->all()); $this->display(); } }