public function __construct($config = array()) { if (!$this->test()) { zotop::error(zotop::t('The memcache extension is not available')); } $host = $config['host']; $host = empty($host) ? zotop::config('system.cache.memcache.host') : $host; $host = empty($host) ? '127.0.0.1' : $host; $post = $config['post']; $port = empty($port) ? zotop::config('system.cache.memcache.port') : $port; $port = empty($port) ? '11211' : $port; $timeout = isset($config['timeout']) ? (bool) $config['timeout'] : false; $persistent = isset($config['persistent']) ? (bool) $config['persistent'] : false; unset($config); //是否持久链接 $connect = $persistent ? 'pconnect' : 'connect'; $this->memcache =& new Memcache(); if ($timeout === false) { $this->connected = @$this->memcache->{$connect}($host, $port); } else { $this->connected = @$this->memcache->{$connect}($host, $port, $timeout); } if (!$this->connected) { zotop::error(zotop::t('无法连接memcache服务器 “{$host}:{$port}”,请检查参数配置是否正确', array('host' => $host, 'port' => $port))); } }
public static function isPostBack() { if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ((empty($_SERVER['HTTP_REFERER']) || preg_replace("/https?:\\/\\/([^\\:\\/]+).*/i", "\\1", $_SERVER['HTTP_REFERER']) == preg_replace("/([^\\:]+).*/", "\\1", $_SERVER['HTTP_HOST'])) && $_POST['_FORMHASH'] == form::hash()) { return true; } else { zotop::error('invalid submit!'); } } return false; }
/** * 生成数据库唯一实例 * * @param $config * @return unknown_type */ public function &factory($config = '') { if (is_string($config)) { $config = $this->parseDNS($config); } if (empty($config['driver'])) { zotop::error(-1, 'there is some error in database config'); } $driver = 'Zotop_DataBase_' . ucfirst(strtolower($config['driver'])); if (!zotop::autoload($driver)) { zotop::error(-1, 'the database driver (' . $driver . ') does not support'); } $db = new $driver($config); return $db; }
/** * 类初始化 * * @param string|array config 配置 * @return object */ public function __construct($config = array()) { //支持json格式的缓存配置 if (is_string($config)) { $config = json_decode($config, true); } if (is_array($config)) { $config += array('driver' => zotop::config('system.cache.driver'), 'expire' => (int) zotop::config('system.cache.expire')); } if (empty($config['driver'])) { $config['driver'] = 'file'; } //缓存驱动程序 $driver = 'cache_' . strtolower($config['driver']); //加载驱动程序 if (!zotop::autoload($driver)) { zotop::error(zotop::t('未能找到缓存驱动 "{$driver}"', $config)); } $this->driver = new $driver($config); return $this->driver; }
/** * 执行一个sql语句 query,相当于包装后的mysql_query * * @param $sql * @param $silent * @return unknown_type */ public function query($sql, $silent = false) { if (!is_resource($this->connect)) { $this->connect(); } if ($sql = $this->parseSql($sql)) { //echo $this->sql; if ($this->query) { $this->free(); //释放前次的查询结果 } $this->query = @mysql_query($sql, $this->connect); //查询数据 if ($this->query === false) { if ($silent) { return false; } zotop::error(-3, '查询语句错误', zotop::t('<h2>SQL: {$sql}</h2>{$error}', array('sql' => $sql, 'error' => @mysql_error()))); } $this->numRows = mysql_num_rows($this->query); return $this->query; } return false; }
/** * 生成数据库唯一实例 * * @param $config * @return object */ public function &instance($config = array()) { static $instances = array(); //实例唯一的编号 $id = serialize($config); if (!isset($instances[$id])) { if (is_string($config)) { $config = $this->parseDNS($config); } if (empty($config['driver'])) { zotop::error(zotop::t('错误的数据库配置文件', $config)); } //数据库驱动程序 $driver = 'database_' . strtolower($config['driver']); //加载驱动程序 if (!zotop::autoload($driver)) { zotop::error(zotop::t('未能找到数据库驱动 "{$driver}"', $config)); } //取得驱动实例 $instance = new $driver($config); $instances[$id] =& $instance; } return $instances[$id]; }
/** * 读取具体的某条数据 * * 空条件:$model->read();前面必须定义过主键值: $model->id = 1; * 默认条件:$model->read(1) 相当于 $model->read(array('id','=',1)) * 自定义条件:$model->read(array('id','=',1)) * * @param mix $value 键值 * * @return array */ public function read($value = '') { if (is_array($value)) { $this->db()->where($value); } else { $key = $this->getPrimaryKey(); if (empty($value)) { $value = $this->{$key}; } $this->db()->where($key, '=', $value); } $this->data = $this->db()->select('*')->from($this->getTableName())->getRow(); if ($this->data === null) { zotop::error(zotop::t('未能找到 <b>{$key}</b> 等于 <b>{$value}</b> 的数据<br>' . reset($this->db->sql()), array('key' => $key, 'value' => $value))); } return $this->data; }
/** * 数据查询,该方法必须被重载,实现数据库的查询,无返回 * * @return null */ public function execute() { zotop::error(zotop::t('函数必须被重载')); }
/** * 用于实例化一个模型{module}.{model},如 zotop.user,实例化系统模块的user模型 * * * @param $name 模型名称空间 * @return object(model) */ public static function model($name = '') { static $models = array(); if (empty($name)) { return new model(); } if (isset($models[$name])) { return $models[$name]; } list($module, $model) = explode('.', $name); $modelName = $model . '_model'; if (!class_exists($modelName)) { $modelPath = zotop::module($module, 'path') . DS . 'models' . DS . $model . '.php'; if (zotop::load($modelPath) == false) { zotop::error(zotop::t('<h2>请检查相应的模型文件是否存在</h2>文件地址:{$modelPath}', array('modelPath' => $modelPath))); } } if (class_exists($modelName)) { $m = new $modelName(); $m->moduleName = $module; $models[$name] = $m; return $m; } zotop::error(zotop::t('<h2>请检查相应的模型文件中是否存在模型类 {$modelName}</h2>文件地址:{$modelPath}', array('modelPath' => $modelPath, 'modelName' => $modelName))); }
/** * 文件重命名 * * @param string $file 文件路径 * @param string $newname 新文件名称,含文件扩展名 * * @return bool * @since 0.1 */ public static function rename($file, $newname) { $file = path::decode($file); $path = dirname($file); $newfile = $path . DS . file::safename($newname); if ($file == $newfile) { zotop::error(zotop::t('目标文件名称和原文件名称相同')); return false; } elseif (file::exists($newfile)) { zotop::error(zotop::t('目标文件已经存在')); return false; } elseif (rename($file, $newfile)) { return true; } return false; }
/** * 应用程序执行 * * * @return null */ public static function execute() { if (zotop::module(application::module()) === null || (int) zotop::module(application::module(), 'status') < 0) { msg::error(array('title' => '404 error', 'content' => zotop::t('<h2>未能找到模块,模块可能尚未安装或者已经被禁用?</h2>'), 'detail' => zotop::t('模块名称:{$module}', array('module' => application::$module)))); } define('ZOTOP_MODULE', application::module()); define('ZOTOP_MODULE_PATH', zotop::module(application::module(), 'path')); define('ZOTOP_MODULE_URL', zotop::module(application::module(), 'url')); $controllerPath = ZOTOP_MODULE_PATH . DS . ZOTOP_APPLICATION . DS . application::controller() . '.php'; if (zotop::load($controllerPath)) { } elseif (zotop::load(ZOTOP_MODULE_PATH . DS . ZOTOP_GROUP . DS . 'default.php')) { $controllerPath = ZOTOP_MODULE_PATH . DS . ZOTOP_GROUP . DS . 'default.php'; application::$arguments = array_merge(array(application::$controller), array(application::$action), application::$arguments); application::$controller = 'default'; application::$action = ''; } else { zotop::error(array('title' => '404 error', 'content' => zotop::t('<h2>未能找到控制器,请检查控制器文件是否存在?</h2>'), 'detail' => zotop::t('文件名称:{$file}', array('file' => $controllerPath)))); } define('ZOTOP_CONTROLLER', application::controller()); $class = application::module() . '_controller_' . application::controller(); if (class_exists($class, false)) { //实例化控制器 $controller = new $class(); if (!method_exists($controller, 'action' . ucfirst(application::action()))) { if (strlen(application::action()) > 0) { application::$arguments = array_merge(array(application::$action), application::$arguments); } application::$action = $controller->action; } define('ZOTOP_ACTION', application::action()); if (method_exists($controller, 'action' . ucfirst(application::action()))) { zotop::run("system.execute.before"); call_user_func_array(array($controller, 'action' . ucfirst(application::action())), application::arguments()); zotop::run("system.execute.after"); } else { call_user_func_array(array($controller, '__empty'), array(application::action(), application::arguments())); } } else { zotop::error(array('title' => '404 error', 'content' => zotop::t('<h2>未能找到控制器类,请检查控制器文件中是否存在控制器类?</h2>'), 'detail' => zotop::t('类名称:{$className}', array('className' => $class)))); } }
public function __empty($action = '', $arguments = '') { zotop::error(array('title' => '404 error', 'content' => zotop::t('<h2>未能找到相应的动作,请检查控制器中动作是否存在?</h2>'), 'detail' => zotop::t('动作名称:{$action}', array('action' => $action)))); }
/** * 用于实例化一个模型{module}.{model},如 zotop.user,实例化系统模块的user模型 * * * @param $name 模型名称空间 * @return object(model) */ public static function model($name = '') { static $models = array(); if (empty($name)) { return new model(); } if (isset($models[$name])) { return $models[$name]; } list($module, $model) = explode('.', $name); $modelName = $module . '_model_' . $model; if (!class_exists($modelName)) { $modelPath = zotop::modules($module, 'path') . DS . 'models' . DS . $model . '.php'; if (zotop::load($modelPath) == false) { zotop::error(array('content' => zotop::t('请检查相应的模型文件是否存在'), 'detail' => zotop::t('文件地址:{$modelPath}', array('modelPath' => $modelPath)))); } } if (class_exists($modelName)) { $m = new $modelName(); $models[$name] = $m; return $m; } zotop::error(array('content' => zotop::t('请检查相应的模型文件中是否存在模型类 :{$modelName}', array('modelPath' => $modelPath, 'modelName' => $modelName)), 'detail' => zotop::t('文件地址:{$modelPath}', array('modelPath' => $modelPath, 'modelName' => $modelName)))); }
public static function instance($classname, $method = '', $args = array()) { static $instances = array(); $id = empty($args) ? strtolower($classname . $method) : strtolower($classname . $method . rand::guid($args)); if (!isset($instances[$id])) { if (class_exists($classname)) { $instance = new $classname(); if (method_exists($instance, $method)) { $instances[$id] = call_user_func_array(array(&$instance, $method), $args); } else { $instances[$id] = $instance; } } else { zotop::error($classname . ' not found!'); } } return $instances[$id]; }
/** * 返回当前的控制器的真实路径 * * @return string */ public static function getControllerPath() { $controller = application::getController(); $module = application::getModule(); $path = zotop::module($module, 'path'); if (empty($path)) { zotop::error(array('title' => '系统错误', 'content' => zotop::t('<h2>未能找到相应模块,请检查模块是否未安装或者已被禁用?</h2>模块名称:{$module}', array('module' => $module)))); } $path = $path . DS . router::application() . DS . $controller . '.php'; return $path; }
public function onEdit($tablename, $fieldname) { if (form::isPostBack()) { $field = array(); $field['name'] = request::post('name'); $field['length'] = request::post('len'); $field['type'] = request::post('type'); $field['collation'] = request::post('collation'); $field['null'] = request::post('null'); $field['default'] = request::post('default'); $field['attribute'] = request::post('attribute'); $field['extra'] = request::post('extra'); $field['comment'] = request::post('comment'); $field['position'] = request::post('position'); $fieldname = request::post('fieldname'); $result = zotop::db()->table($tablename)->field($fieldname)->rename($field['name']); $result = zotop::db()->table($tablename)->modify($field); if ($result) { msg::success('修改成功', '<h2>字段修改成功</h2>', form::referer()); } } $tables = zotop::db()->tables(true); $table = $tables[$tablename]; $fields = array(); if (isset($table)) { $fields = zotop::db()->table($tablename)->fields(true); } $field = $fields[$fieldname]; if (!isset($field)) { zotop::error(-10, '字段不存在,请勿修改浏览器参数'); } $positions = array(); $positions[-1] = '位于表头'; if ($fields) { foreach ($fields as $key => $val) { $positions[$key] = '位于 ' . $key . ' 之后'; } } $positions[0] = ' '; $header['title'] = '<a href="' . zotop::url('zotop/database') . '">数据库管理</a> <i>></i> <a href="' . zotop::url('system/database/fields/', array('table' => $tablename)) . '">数据表 [ ' . $tablename . ' ] </a> <i>></i> 字段修改'; page::header($header); page::top(); page::navbar($this->navbar($tablename), 'edit'); form::header(); form::field(array('type' => 'hidden', 'name' => 'fieldname', 'label' => '字段名称', 'value' => $field['name'], 'valid' => '{required:true}')); form::field(array('type' => 'text', 'name' => 'name', 'label' => '字段名称', 'value' => $field['name'], 'valid' => '{required:true}', 'description' => '请输入字段的名称,3到32位,请勿使用特殊字符')); form::field(array('type' => 'text', 'name' => 'type', 'label' => '字段类型', 'value' => $field['type'], 'valid' => '{required:true}')); form::field(array('type' => 'text', 'name' => 'len', 'label' => '长度/值', 'value' => $field['length'], 'valid' => '{number:true,min:1}', 'description' => '请输入字段的长度,如果字段无须定义长度,请保持空值')); form::field(array('type' => 'hidden', 'name' => 'collation', 'label' => '整理', 'value' => $field['collation'], 'valid' => '', 'description' => '默认使用 <b>utf8_general_ci</b>: Unicode (多语言), 不区分大小写')); form::field(array('type' => 'select', 'options' => array('' => ' ', 'UNSIGNED' => 'UNSIGNED', 'UNSIGNED ZEROFILL' => 'UNSIGNED ZEROFILL', 'ON UPDATE CURRENT_TIMESTAMP' => 'ON UPDATE CURRENT_TIMESTAMP'), 'name' => 'attribute', 'label' => '属性', 'value' => $field['attribute'], 'valid' => '')); form::field(array('type' => 'select', 'options' => array('' => 'NULL', 'NOT NULL' => 'NOT NULL'), 'name' => 'null', 'label' => 'null', 'value' => $field['null'], 'valid' => '')); form::field(array('type' => 'text', 'name' => 'default', 'label' => '默认值', 'value' => $field['default'], 'valid' => '', 'description' => '如果需要可以为字段设置一个默认值')); form::field(array('type' => 'select', 'options' => array('' => '', 'AUTO_INCREMENT' => 'AUTO_INCREMENT'), 'name' => 'extra', 'label' => '额外', 'value' => $field['extra'], 'valid' => '', 'description' => '设置为自动增加:<b>AUTO_INCREMENT</b>时,该字段必须为数字类型')); form::field(array('type' => 'text', 'name' => 'comment', 'label' => '注释', 'value' => $field['comment'], 'valid' => '')); form::field(array('type' => 'select', 'name' => 'position', 'options' => $positions, 'label' => zotop::t('字段位置'), 'value' => $position, 'description' => '')); form::buttons(array('type' => 'submit'), array('type' => 'reset')); form::footer(); page::bottom(); page::footer(); }
/** * 执行一个sql语句 ,等价于 mysql_query * * @param $sql * @param $silent * @return unknown_type */ public function query($sql, $silent = false) { $this->reset(); if (!is_resource($this->connect)) { $this->connect(); } if ($sql = $this->parseSql($sql)) { //zotop::dump($this->sql); if ($this->query) { $this->free(); //释放前次的查询结果 } $this->query = @mysql_query($sql, $this->connect); //查询数据 if ($this->query === false) { if ($silent) { return false; } zotop::error(array('content' => @mysql_error(), 'detail' => zotop::t('SQL : {$sql}', array('sql' => $sql)))); } database::Q(true); $this->numRows = @mysql_num_rows($this->query); return $this->query; } return false; }
public function create($overwirte = false) { //如果已经存在,是否覆写 if ($tablename = $this->name()) { if ($this->exist()) { zotop::error(-1, '数据表已经存在', zotop::t('您视图创建的数据表<b>{$tablename}</b>已经存在', array('{$tablename}' => $tablename))); if ($overwirte) { $this->drop(); } else { zotop::error(-1, '数据表已经存在', zotop::t('您视图创建的数据表<b>{$tablename}</b>已经存在', array('{$tablename}' => $tablename))); } return false; } elseif (false !== $this->db->execute('CREATE TABLE `' . $tablename . '`( id VARCHAR( 32 ) NOT NULL)ENGINE = MYISAM ;')) { return true; } } return false; }
public function create() { $database = $this->config('database'); $charset = $this->config('charset'); if ($create = $this->query("CREATE DATABASE IF NOT EXISTS " . $database . " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;\r\n\t\t")) { if (!$create) { zotop::error(-20, 'Could not connect to database server (' . $this->config('hostname') . ')'); } } return true; }