public static function debugAndLayout() { if (!self::hasDebug()) { return; } Core::$debug = true; View::$layout = true; }
public function __construct($vars = null) { // This part only needs to be run once if (null === Session::$instance) { // Load config Session::$config = Core::config('session'); if (!isset(Session::$config['check_string']) || !Session::$config['check_string']) { Session::$config['check_string'] = '&$@de23#$%@.d3l-3=!#1'; } if (!isset(Session::$config['name']) || !preg_match('#^(?=.*[a-z])[a-z0-9_]++$#iD', Session::$config['name'])) { // Name the session, this will also be the name of the cookie Session::$config['name'] = 'PHPSESSINID'; } if (IS_DEBUG) { $time = microtime(1); $is_debug = (bool) Core::debug()->profiler()->is_open(); if ($is_debug) { Core::debug()->profiler()->start('Core', 'Session StartTime'); } } if (isset(Session::$config['driver']) && class_exists('Session_Driver_' . Session::$config['driver'], true)) { $driver_name = 'Session_Driver_' . Session::$config['driver']; if (isset(Session::$config['driver_config'])) { Session::$driver = new $driver_name(Session::$config['driver_config']); } else { Session::$driver = new $driver_name(); } } else { Session::$driver = new Session_Driver_Default(); } if (!isset(Session::$config['type']) || Session::$config['type'] != 'url') { Session::$config['type'] = 'cookie'; } if (IS_DEBUG) { if ($is_debug) { Core::debug()->profiler()->stop(); } # 输出Session启动使用时间 Core::debug()->info(microtime(1) - $time, 'Session start use time'); } if ($vars) { $this->set($vars); } if (!isset($_SESSION['_flash_session_'])) { $_SESSION['_flash_session_'] = array(); } Session::$flash =& $_SESSION['_flash_session_']; # 清理Flash Session $this->expire_flash(); $_SESSION['SID'] = Session::$driver->session_id(); # 确保关闭前执行保存 Core::register_shutdown_function(array('Session', 'write_close')); Session::$instance = $this; # 加载用户数据 Session::load_member_data(); } }
/** * 删除指定key的缓存,若$key===true则表示删除全部 * * @param string $key */ public function delete($key) { if (IS_DEBUG) { Core::debug()->info($key, 'apc cache delete key'); } if ($key === true) { return $this->delete_all(); } return apc_delete($key); }
/** * 获取数据 * * @param $query SQL OR Query_Builder * @return \OOP\ORM\Result */ public function find($query = null) { if (\is_array($query)) { $query = \http_build_query($query, '', '&'); } $url = $this->api_url . (\strpos($this->api_url, '?') !== false ? '?' : '&') . $query; try { $data = (string) $this->driver()->get($url); } catch (\Exception $e) { \Core::debug()->error('ORM获取数据失败,URL:' . $url); $data = '[]'; } $this->last_query = $url; $data = @\json_decode($data, true); return $this->create_group_data($data, true); }
/** * 存数据 * * @param string/array $key 支持多存 * @param $data Value 多存时此项可空 * @param $lifetime 有效期,默认3600,即1小时,0表示最大值30天(2592000) * @return boolean */ public function set($key, $value = null, $lifetime = 3600) { \Core::debug()->info($key, 'apc cache set key'); if (\is_array($key)) { $return = true; foreach ($key as $k => &$v) { static::_format_data($v); $s = \apc_store($k, $v, $lifetime); if (false === $s) { $return = false; } } return $return; } else { static::_format_data($value); return \apc_store($key, $value, $lifetime); } }
/** * 导入指定类库 * 支持多个,当一次导入多个时,从数组最后一个开始导入 * * 导入的格式必须是类似 com.a.b 的形式,否则会抛出异常,例如: `com.myqee.test` * * Bootstrap::import_library('com.myqee.test'); * Bootstrap::import_library(array('com.myqee.test','com.myqee.cms')); * * @param string|array $library_name 指定类库 支持多个 * @return num 返回新加载的类库总数 */ public static function import_library($library_name) { if (!$library_name) { return false; } $library_name = (array) $library_name; # 反向排序,从最后一个开始导入 $library_name = array_reverse($library_name); $config_files = array(); $load_num = 0; foreach ($library_name as $lib) { $set = self::_add_include_path_lib($lib); if (true === $set[2]) { # 已加载过 continue; } $config_file = $set[1] . 'config' . EXT; if (is_file($config_file)) { $config_files[] = $config_file; } if (self::$core_config['runtime_config']) { $config_file = $set[1] . 'config' . self::$core_config['runtime_config'] . '.runtime' . EXT; if (is_file($config_file)) { $config_files[] = $config_file; } } $load_num++; if (IS_DEBUG && class_exists('Core', false) && class_exists('Debug', false)) { Core::debug()->info('import a new library: ' . Core::debug_path($lib)); } } if ($config_files) { __include_config_file(self::$config, $config_files); } return $load_num; }
/** * 删除指定key的缓存,若$key===true则表示删除全部 * * @param string $key */ public function delete($key) { if (IS_DEBUG) { Core::debug()->info($key, 'database storage delete key'); } if (is_array($key)) { $new_keys = array(); foreach ($key as $k) { $k = $this->prefix . $k; $new_keys[] = md5($k); } $this->_handler->in('key', $new_keys); } elseif (true !== $key) { $key = $this->prefix . $key; $this->_handler->where('key', $key); } try { $this->_handler->delete($this->tablename); return true; } catch (Exception $e) { Core::debug()->warn($e->getMessage()); return false; } }
/** * 支持多线程获取网页 * * @see http://cn.php.net/manual/en/function.curl-multi-exec.php#88453 * @param Array/string $urls * @param Int $timeout * @return Array */ protected function request_urls($urls, $timeout = 10) { # 去重 $urls = array_unique($urls); if (!$urls) { return array(); } # 监听列表 $listener_list = array(); # 返回值 $result = array(); # 总列队数 $list_num = 0; # 记录页面跳转数据 $redirect_list = array(); # 排队列表 $multi_list = array(); foreach ($urls as $url) { if ($this->multi_exec_num > 0 && $list_num >= $this->multi_exec_num) { # 加入排队列表 $multi_list[] = $url; } else { # 列队数控制 $listener_list[] = array($url, $this->_create($url, $timeout)); $list_num++; } $result[$url] = null; $this->http_data[$url] = null; } # 已完成数 $done_num = 0; while ($listener_list) { list($done_url, $f) = array_shift($listener_list); $time = microtime(1); $str = ''; while (!feof($f)) { $str .= fgets($f); } fclose($f); $time = microtime(1) - $time; list($header, $body) = explode("\r\n\r\n", $str, 2); $header_arr = explode("\r\n", $header); $first_line = array_shift($header_arr); if (preg_match('#^HTTP/1.1 ([0-9]+) #', $first_line, $m)) { $code = $m[1]; } else { $code = 0; } if (strpos($header, 'Transfer-Encoding: chunked')) { $body = explode("\r\n", $body); $body = array_slice($body, 1, -1); $body = implode('', $body); } if (preg_match('#Location(?:[ ]*):([^\\r]+)\\r\\n#Uis', $header, $m)) { if (count($redirect_list[$done_url]) >= 10) { # 防止跳转次数太大 $body = $header = ''; $code = 0; } else { # 302 跳转 $new_url = trim($m[1]); $redirect_list[$done_url][] = $new_url; // 插入列队 if (preg_match('#Set-Cookie(?:[ ]*):([^\\r+])\\r\\n#is', $header, $m2)) { // 把cookie传递过去 $old_cookie = $this->cookies; $this->cookies = $m2[1]; } array_unshift($listener_list, array($done_url, $this->_create($new_url, $timeout))); if (isset($old_cookie)) { $this->cookies = $old_cookie; } continue; } } $rs = array('code' => $code, 'data' => $body, 'header' => $header_arr, 'time' => $time); $this->http_data[$done_url] = $rs; if ($rs['code'] != 200) { Core::debug()->error('URL:' . $done_url . ' ERROR,TIME:' . $this->http_data[$done_url]['time'] . ',CODE:' . $this->http_data[$done_url]['code']); $result[$done_url] = false; } else { Core::debug()->info('URL:' . $done_url . ' OK.TIME:' . $this->http_data[$done_url]['time']); $result[$done_url] = $rs['data']; } $done_num++; if ($multi_list) { # 获取列队中的一条URL $current_url = array_shift($multi_list); # 更新监听列队信息 $listener_list[] = array($current_url, $this->_create($current_url, $timeout)); # 更新列队数 $list_num++; } if ($done_num >= $list_num) { break; } } return $result; }
/** * 通过CURL执行 * * @param array $hosts * @param string $url * @param array $param_arr * @return array */ protected static function exec_by_curl($hosts, $url, array $param_arr = null) { $mh = curl_multi_init(); # 监听列表 $listener_list = array(); $vars = http_build_query($param_arr); # 创建列队 foreach ($hosts as $h) { # 排除重复HOST if (isset($listener_list[$h])) { continue; } list($host, $port) = explode(':', $h, 2); if (!$port) { # 默认端口 $port = $_SERVER["SERVER_PORT"]; } # 一个mictime $mictime = microtime(1); # 生成一个随机字符串 $rstr = Text::random(); # 生成一个HASH $hash = self::get_hash($vars, $rstr, $mictime); # 创建一个curl对象 $current = HttpCall::_create_curl($host, $port, $url, 10, $hash, $vars, $mictime, $rstr); # 列队数控制 curl_multi_add_handle($mh, $current); $listener_list[$h] = $current; } unset($current); $running = null; $result = array(); # 已完成数 $done_num = 0; # 待处理数 $list_num = count($listener_list); do { while (($execrun = curl_multi_exec($mh, $running)) == CURLM_CALL_MULTI_PERFORM) { } if ($execrun != CURLM_OK) { break; } while (true == ($done = curl_multi_info_read($mh))) { foreach ($listener_list as $done_host => $listener) { if ($listener === $done['handle']) { # 获取内容 $result[$done_host] = curl_multi_getcontent($done['handle']); $code = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE); if ($code != 200) { Core::debug()->error('system exec:' . $done_host . ' ERROR,CODE:' . $code); // $result[$done_host] = false; } else { # 返回内容 Core::debug()->info('system exec:' . $done_host . ' OK.'); } curl_close($done['handle']); curl_multi_remove_handle($mh, $done['handle']); unset($listener_list[$done_host], $listener); $done_num++; $time = microtime(1); break; } } } if ($done_num >= $list_num) { break; } if (!$running) { break; } } while (true); # 关闭列队 curl_multi_close($mh); return $result; }
/** * 关闭所有链接 */ public static function close_all_connect() { foreach (Storage_Driver_Swift::$connections as $host => $obj) { try { fclose($obj); } catch (Exception $e) { Core::debug()->error('close swift storage connect error:' . $e->getMessage()); } Storage_Driver_Swift::$connections[$host] = null; } # 重置全部数据 Storage_Driver_Swift::$connections = array(); Storage_Driver_Swift::$last_used = array(); Storage_Driver_Swift::$requests_num = array(); if (IS_DEBUG) { Core::debug()->info('close all swift storage server.'); } }
/** * 清除配置缓存 * * @return boolean */ public function clear_cache($type = '') { if (!$this->is_use_cache) { // 非普通文件写入,不缓存,无需清理 return true; } $tmp_file = array(); $projects = array_keys((array) Core::config('core.projects')); if ($projects) { foreach ($projects as $project) { # 所有项目的配置文件 $tmp_file[] = $this->get_config_cache_file($project, $type); } } if (!$tmp_file) { return true; } $rs = File::unlink($tmp_file); if (IS_DEBUG) { Core::debug()->log('clear extends config cache ' . ($rs ? 'success' : 'fail') . '.'); } return $rs; }
/** * 查询 * * $use_connection_type 默认不传为自动判断,可传true/false,若传字符串(只支持a-z0-9的字符串),则可以切换到另外一个连接,比如传other,则可以连接到$this->_connection_other_id所对应的ID的连接 * * @param string $sql 查询语句 * @param string $as_object 是否返回对象 * @param boolean $use_connection_type 是否使用主数据库,不设置则自动判断 * @return Database_Driver_Postgre_Result */ public function query($sql, $as_object = null, $use_connection_type = null) { $sql = trim($sql); if (preg_match('#^([a-z]+)(:? |\\n|\\r)#i', $sql, $m)) { $type = strtoupper($m[1]); } else { $type = null; } $typeArr = array('SELECT', 'SHOW', 'EXPLAIN', 'DESCRIBE', 'INSERT', 'REPLACE', 'UPDATE', 'DELETE'); if (!in_array($type, $typeArr)) { $type = 'MASTER'; } $slaverType = array('SELECT', 'SHOW', 'EXPLAIN'); if ($type != 'MASTER' && in_array($type, $slaverType)) { if (true === $use_connection_type) { $use_connection_type = 'master'; } else { if (is_string($use_connection_type)) { if (!preg_match('#^[a-z0-9_]+$#i', $use_connection_type)) { $use_connection_type = 'master'; } } else { $use_connection_type = 'slaver'; } } } else { $use_connection_type = 'master'; } # 设置连接类型 $this->_set_connection_type($use_connection_type); # 连接数据库 $connection = $this->connection(); # 记录调试 if (IS_DEBUG) { Core::debug()->info($sql, 'Postgre'); static $is_sql_debug = null; if (null === $is_sql_debug) { $is_sql_debug = (bool) Core::debug()->profiler('sql')->is_open(); } if ($is_sql_debug) { $host = $this->_get_hostname_by_connection_hash($this->connection_id()); $benchmark = Core::debug()->profiler('sql')->start('Database', 'postgre://' . ($host['username'] ? $host['username'] . '@' : '') . $host['hostname'] . ($host['port'] && $host['port'] != '3306' ? ':' . $host['port'] : '') . $host['database']); } } // Execute the query if (($result = pg_query($connection, $sql)) === false) { if (isset($benchmark)) { // This benchmark is worthless $benchmark->delete(); } if (IS_DEBUG) { $err = 'Error:' . pg_last_error($connection) . '. PostgreSQL:' . $sql; } else { $err = pg_last_error($connection); } throw new Exception($err); } if (isset($benchmark)) { Core::debug()->profiler('sql')->stop(); } // Set the last query $this->last_query = $sql; if ($type === 'INSERT' || $type === 'REPLACE') { // Return a list of insert id and rows created return array($this->_insert_id($connection), pg_affected_rows($connection)); } elseif ($type === 'UPDATE' || $type === 'DELETE') { // Return the number of rows affected return pg_affected_rows($connection); } else { // Return an iterator of results return new Database_Driver_Postgre_Result($result, $sql, $as_object, $this->config); } }
/** * 查询 * * $use_connection_type 默认不传为自动判断,可传true/false,若传字符串(只支持a-z0-9的字符串),则可以切换到另外一个连接,比如传other,则可以连接到$this->_connection_other_id所对应的ID的连接 * * @param string $sql 查询语句 * @param string $as_object 是否返回对象 * @param boolean $use_connection_type 是否使用主数据库,不设置则自动判断 * @return \Database_Driver_MySQL_Result */ public function query($sql, $as_object = null, $use_connection_type = null) { $sql = \trim($sql); if (\preg_match('#^([a-z]+)(:? |\\n|\\r)#i', $sql, $m)) { $type = \strtoupper($m[1]); } $typeArr = array('SELECT', 'SHOW', 'EXPLAIN', 'DESCRIBE', 'INSERT', 'REPLACE', 'UPDATE', 'DELETE'); if (!\in_array($type, $typeArr)) { $type = 'MASTER'; } $slaverType = array('SELECT', 'SHOW', 'EXPLAIN'); if ($type != 'MASTER' && \in_array($type, $slaverType)) { if (true === $use_connection_type) { $use_connection_type = 'master'; } else { if (\is_string($use_connection_type)) { if (!\preg_match('#^[a-z0-9_]+$#i', $use_connection_type)) { $use_connection_type = 'master'; } } else { $use_connection_type = 'slaver'; } } } else { $use_connection_type = 'master'; } # 设置连接类型 $this->_set_connection_type($use_connection_type); # 连接数据库 $connection = $this->connection(); # 记录调试 if (\IS_DEBUG) { \Core::debug()->info($sql, 'MySQL'); static $is_sql_debug = null; if (null === $is_sql_debug) { $is_sql_debug = (bool) \Core::debug()->profiler('sql')->is_open(); } if ($is_sql_debug) { $host = $this->_get_hostname_by_connection_hash($this->connection_id()); $benchmark = \Core::debug()->profiler('sql')->start('Database', 'mysqli://' . ($host['username'] ? $host['username'] . '@' : '') . $host['hostname'] . ($host['port'] && $host['port'] != '3306' ? ':' . $host['port'] : '')); } } static $is_no_cache = null; if (null === $is_no_cache) { $is_no_cache = (bool) \Core::debug()->profiler('nocached')->is_open(); } //显示无缓存数据 if ($is_no_cache && \strtoupper(\substr($sql, 0, 6)) == 'SELECT') { $sql = 'SELECT SQL_NO_CACHE' . \substr($sql, 6); } // Execute the query if (($result = \mysql_query($sql, $connection)) === false) { if (isset($benchmark)) { // This benchmark is worthless $benchmark->delete(); } if (\IS_DEBUG) { $err = 'Error:' . \mysql_error($connection) . '. SQL:' . $sql; } else { $err = \mysql_error($connection); } throw new \Exception($err, \mysql_errno($connection)); } if (isset($benchmark)) { # 在线查看SQL情况 if ($is_sql_debug) { $data = array(); $data[0]['db'] = $host['hostname'] . '/' . $this->config['connection']['database'] . '/'; $data[0]['select_type'] = ''; $data[0]['table'] = ''; $data[0]['key'] = ''; $data[0]['key_len'] = ''; $data[0]['Extra'] = ''; $data[0]['query'] = ''; $data[0]['type'] = ''; $data[0]['id'] = ''; $data[0]['row'] = \count($result); $data[0]['ref'] = ''; $data[0]['all rows'] = ''; $data[0]['possible_keys'] = ''; if (\strtoupper(\substr($sql, 0, 6)) == 'SELECT') { $re = \mysql_query('EXPLAIN ' . $sql, $connection); $i = 0; while (true == ($row = \mysql_fetch_array($re, \MYSQL_NUM))) { $data[$i]['select_type'] = (string) $row[1]; $data[$i]['table'] = (string) $row[2]; $data[$i]['key'] = (string) $row[5]; $data[$i]['key_len'] = (string) $row[6]; $data[$i]['Extra'] = (string) $row[9]; if ($i == 0) { $data[$i]['query'] = ''; } $data[$i]['type'] = (string) $row[3]; $data[$i]['id'] = (string) $row[0]; $data[$i]['ref'] = (string) $row[7]; $data[$i]['all rows'] = (string) $row[8]; $data[$i]['possible_keys'] = (string) $row[4]; $i++; } } $data[0]['query'] = $sql; } else { $data = null; } \Core::debug()->profiler('sql')->stop($data); } // Set the last query $this->last_query = $sql; if ($type === 'INSERT' || $type === 'REPLACE') { // Return a list of insert id and rows created return array(\mysql_insert_id($connection), \mysql_affected_rows($connection)); } elseif ($type === 'UPDATE' || $type === 'DELETE') { // Return the number of rows affected return \mysql_affected_rows($connection); } else { // Return an iterator of results return new \Database_Driver_MySQL_Result($result, $sql, $as_object, $this->config); } }
/** * 获取数据 * * @param $query 请求的参数 * @return OOP_ORM_Result */ public function find($query = null) { if (!$this->api_url) { throw new Exception(__('orm api url is not declared.')); } $url = $this->parse_api_fullurl($query); try { if ($this->method == 'POST') { $rs = (string) $this->driver()->post($url, $this->parse_api_post_data($query)); } else { if ($this->method == 'PUT') { $rs = (string) $this->driver()->put($url, $this->parse_api_post_data($query)); } else { if ($this->method == 'DELETE') { $rs = (string) $this->driver()->delete($url); } else { if ($this->method != 'GET') { $this->driver()->method($this->method); } $rs = (string) $this->driver()->get($url); } } } } catch (Exception $e) { Core::debug()->warn('ORM获取数据失败,URL:' . $url); $rs = '[]'; } $this->last_query = $url; // 处理解析数据 $this->parse_result_data($rs); // 重置数据 $this->reset(); return $this->create_group_data($rs, true); }
* * @category Core * @package LeQG * @author Damien Senger <*****@*****.**> * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License 3.0 * @link http://leqg.info */ require_once 'includes.php'; $link = Configuration::read('db.link'); $query = $link->prepare('SELECT * FROM `TABLE 30` LIMIT 0, 50'); $query->execute(); $contacts = $query->fetchAll(PDO::FETCH_ASSOC); foreach ($contacts as $contact) { $person = People::create(); $person = new People($person); Core::debug($contact, false); // Traitement du nom $person->update('nom', $contact['NOM']); $person->update('prenoms', $contact['PRENOM']); // On paramètre le sexe $genre = trim($contact['GENRE']); if ($genre == 'Madame') { $person->update('sexe', 'F'); } else { $person->update('sexe', 'H'); } $adresse = array('pays' => 'France', 'ville' => '', 'zip' => '', 'street' => '', 'building' => ''); $decomposition_rue = explode(' ', $contact['ADRESSE 3']); $numero = $decomposition_rue[0]; $first = substr($numero, 0, 1); if (is_numeric($first)) {
/** * 执行构造语法执行 * * @param $builder * @return Database_Driver_PDO_Result */ public function execute($statement, array $input_parameters = array(), $as_object = null, $connection_type = null) { $statement = trim($statement); $type = $this->_get_query_type($statement, $connection_type); # 设置连接类型 $this->_set_connection_type($connection_type); # 连接数据库 $connection = $this->connection(); # 记录调试 if (IS_DEBUG) { Core::debug()->info($statement, 'PDO Statement'); static $is_sql_debug = null; if (null === $is_sql_debug) { $is_sql_debug = (bool) Core::debug()->profiler('sql')->is_open(); } if ($is_sql_debug) { $host = $this->_get_hostname_by_connection_hash($this->connection_id()); $benchmark = Core::debug()->profiler('sql')->start('Database', 'pbo ' . ($host['username'] ? $host['username'] . '@' : '') . $host['hostname']); } } try { $sth = $connection->prepare($statement); if (false === $sth) { throw new Exception('pdo prepare sql error.'); } try { if (false === $sth->execute($input_parameters)) { throw new Exception('pdo execute error.'); } } catch (Exception $e) { if (IS_DEBUG) { throw $e; } throw new Exception($e->getMessage(), (int) $e->getCode()); } } catch (Exception $e) { $sth = false; } # 关闭调试记录 if (IS_DEBUG && isset($benchmark)) { Core::debug()->profiler('sql')->stop(); } if (false === $sth) { if (isset($e)) { if (IS_DEBUG) { throw $e; } else { throw new Exception($e->getMessage(), $e->getCode()); } } else { throw new Exception('Error: pdo prepare sql error.'); } } $this->last_query = $sth->queryString; if ($type === 'INSERT' || $type === 'REPLACE') { return array($connection->lastInsertId(), $sth->rowCount()); } elseif ($type === 'UPDATE' || $type === 'DELETE') { return $sth->rowCount(); } else { return new Database_Driver_PDO_Result($sth, $this->last_query, $as_object, $this->config); } }
/** * 支持多线程获取网页 * * @see http://cn.php.net/manual/en/function.curl-multi-exec.php#88453 * @param Array/string $urls * @param Int $timeout * @return Array */ protected function request_urls($urls, $timeout = 10) { # 去重 $urls = \array_unique($urls); if (!$urls) { return array(); } $mh = \curl_multi_init(); # 监听列表 $listener_list = array(); # 返回值 $result = array(); # 总列队数 $list_num = 0; # 排队列表 $multi_list = array(); foreach ($urls as $url) { # 创建一个curl对象 $current = $this->_create($url, $timeout); if ($this->multi_exec_num > 0 && $list_num >= $this->multi_exec_num) { # 加入排队列表 $multi_list[] = $url; } else { # 列队数控制 \curl_multi_add_handle($mh, $current); $listener_list[$url] = $current; $list_num++; } $result[$url] = null; $this->http_data[$url] = null; } unset($current); $running = null; # 已完成数 $done_num = 0; do { while (($execrun = \curl_multi_exec($mh, $running)) == \CURLM_CALL_MULTI_PERFORM) { } if ($execrun != \CURLM_OK) { break; } while (true == ($done = \curl_multi_info_read($mh))) { foreach ($listener_list as $done_url => $listener) { if ($listener === $done['handle']) { # 获取内容 $this->http_data[$done_url] = $this->get_data(\curl_multi_getcontent($done['handle']), $done['handle']); if ($this->http_data[$done_url]['code'] != 200) { \Core::debug()->error('URL:' . $done_url . ' ERROR,TIME:' . $this->http_data[$done_url]['time'] . ',CODE:' . $this->http_data[$done_url]['code']); $result[$done_url] = false; } else { # 返回内容 $result[$done_url] = $this->http_data[$done_url]['data']; \Core::debug()->info('URL:' . $done_url . ' OK.TIME:' . $this->http_data[$done_url]['time']); } \curl_close($done['handle']); curl_multi_remove_handle($mh, $done['handle']); # 把监听列表里移除 unset($listener_list[$done_url], $listener); $done_num++; # 如果还有排队列表,则继续加入 if ($multi_list) { # 获取列队中的一条URL $current_url = \array_shift($multi_list); # 创建CURL对象 $current = $this->_create($current_url, $timeout); # 加入到列队 \curl_multi_add_handle($mh, $current); # 更新监听列队信息 $listener_list[$current_url] = $current; unset($current); # 更新列队数 $list_num++; } break; } } } if ($done_num >= $list_num) { break; } } while (true); # 关闭列队 \curl_multi_close($mh); return $result; }
/** * 调用HttpServer执行 * * @param string $storage * @param string $uri * @param mixed $arg1 * @param mixed $arg2 * @return boolean mixed */ protected static function call_http_host($storage, $uri, $arg1 = null, $arg2 = null) { $param_arr = func_get_args(); array_shift($param_arr); // 把 $storage 移除 $sync_mode = File::sync_mode(); if ($sync_mode == 'rsync') { // rsync 模式,调用主服执行 $action = 'master_exec'; } else { // 全部同步执行 $action = 'sync_exec'; } $rs = call_user_func_array(array(HttpCall::factory($storage), $action), $param_arr); if (IS_DEBUG) { Core::debug()->log($rs); } if (is_array($rs)) { $i = 0; foreach ($rs as $item) { $i++; if ($item !== 'success') { if (IS_DEBUG) { Core::debug()->error($i . '/' . count($rs), 'call_http_host rs status'); } return false; } } } else { if ($rs === 'success') { return true; } else { Core::debug()->error('call_http_host error.'); return false; } } return $rs; }
public static function failure_addserver($host, $port, $udp, $info, $code) { Core::debug()->error('memcache server failover:' . ' host: ' . $host . ' port: ' . $port . ' udp: ' . $udp . ' info: ' . $info . ' code: ' . $code); }
/** * 切换到另外一个项目 * * 切换其它项目后,相关的config,include_path等都将加载为设定项目的,但是已经加载的class等是不可能销毁的,所以需谨慎使用 * * @param string $project * @return boolean * @throws Exception 失败则抛出异常(比如不存在指定的项目) */ public static function change_project($project) { if (Core::$project === $project) { return true; } if (!isset(Core::$core_config['projects'][$project])) { Core::show_500(__('not found the project: :project.', array(':project' => $project))); } if (!Core::$core_config['projects'][$project]['isuse']) { Core::show_500(__('the project: :project is not open.', array(':project' => $project))); } # 记录所有项目设置,当切换回项目时,使用此设置还原 static $all_projects_setting = array(); if (Core::$project) { // 记录上一个项目设置 $all_projects_setting[Core::$project] = array('config' => Core::$config, 'include_path' => Core::$include_path, 'file_list' => Core::$file_list, 'project_dir' => Core::$project_dir, 'base_url' => Core::$base_url); } # 原来的项目 $old_project = Core::$project; if (isset($all_projects_setting[$project])) { # 设为当前项目 Core::$project = $project; # 还原配置 Core::$config = $all_projects_setting[$project]['config']; Core::$include_path = $all_projects_setting[$project]['include_path']; Core::$file_list = $all_projects_setting[$project]['file_list']; Core::$project_dir = $all_projects_setting[$project]['project_dir']; Core::$base_url = $all_projects_setting[$project]['base_url']; # 清除缓存数据 unset($all_projects_setting[$project]); } else { $p_config = Core::$core_config['projects'][$project]; if (!isset($p_config['dir']) || !$p_config['dir']) { Core::show_500(__('the project ":project" dir is not defined.', array(':project' => $project))); } # 设置include path $project_dir = DIR_PROJECT . $project . DS; if (!is_dir($project_dir)) { Core::show_500(__('not found the project: :project.', array(':project' => $project))); } # 项目路径 $project_dir = realpath(DIR_PROJECT . $p_config['dir']); if (!$project_dir || !is_dir($project_dir)) { Core::show_500(__('the project dir :dir is not exist.', array(':dir' => $p_config['dir']))); } # 设为当前项目 Core::$project = $project; $project_dir .= DS; Core::$project_dir = $project_dir; # 处理base_url if (isset($p_config['url']) && $p_config['url']) { $url = rtrim(current((array) $p_config['url']), '/'); } else { $url = '/'; } if (IS_ADMIN_MODE) { if (isset($p_config['url_admin']) && $p_config['url_admin']) { $current_url = current((array) $p_config['url_admin']); if (false === strpos($current_url[0], '://')) { $url .= trim($current_url, '/'); $url = trim($url, '/') . '/'; } } } if (IS_REST_MODE) { if (isset($p_config['url_rest']) && $p_config['url_rest']) { $current_url = current((array) $p_config['url_rest']); if (false === strpos($current_url[0], '://')) { $url .= trim($current_url, '/'); $url = trim($url, '/') . '/'; } } } Core::$base_url = $url; # 重置$include_path Core::$include_path['project'] = array(Core::$project => $project_dir); # 重新加载类库配置 Core::reload_all_libraries(); } # 记录debug信息 if (IS_DEBUG) { Core::debug()->info($project, 'Change to new Project'); } # 调用Event Core::event_trigger('system.change_project'); return true; }
/** * 查询 * * $use_connection_type 默认不传为自动判断,可传true/false,若传字符串(只支持a-z0-9的字符串),则可以切换到另外一个连接,比如传other,则可以连接到$this->_connection_other_id所对应的ID的连接 * * @param string $sql 查询语句 * @param string $as_object 是否返回对象 * @return Database_Driver_SQLite_Result */ public function query($sql, $as_object = null, $use_master = null) { $sql = trim($sql); if (preg_match('#^([a-z]+)(?: |\\n|\\r)#i', $sql, $m)) { $type = strtoupper($m[1]); } # 连接数据库 $connection = $this->connection(); # 记录调试 if (IS_DEBUG) { Core::debug()->info($sql, 'SQLite'); static $is_sql_debug = null; if (null === $is_sql_debug) { $is_sql_debug = (bool) Core::debug()->profiler('sql')->is_open(); } if ($is_sql_debug) { $db = $this->_get_hostname_by_connection_hash($this->connection_id()); $benchmark = Core::debug()->profiler('sql')->start('Database', 'sqlite://' . $db); } } static $is_no_cache = null; if (null === $is_no_cache) { $is_no_cache = (bool) Core::debug()->profiler('nocached')->is_open(); } //显示无缓存数据 if ($is_no_cache && strtoupper(substr($sql, 0, 6)) == 'SELECT') { $sql = 'SELECT SQL_NO_CACHE' . substr($sql, 6); } // Execute the query if (($result = sqlite_query($sql, $connection)) === false) { if (isset($benchmark)) { // This benchmark is worthless $benchmark->delete(); } if (IS_DEBUG) { $err = 'Error:' . sqlite_error_string($connection) . '. SQL:' . $sql; } else { $err = sqlite_error_string($connection); } throw new Exception($err, sqlite_last_error($connection)); } if (isset($benchmark)) { # 在线查看SQL情况 if ($is_sql_debug) { $data = array(); $data[0]['db'] = $db; $data[0]['select_type'] = ''; $data[0]['table'] = ''; $data[0]['key'] = ''; $data[0]['key_len'] = ''; $data[0]['Extra'] = ''; $data[0]['query'] = ''; $data[0]['type'] = ''; $data[0]['id'] = ''; $data[0]['row'] = count($result); $data[0]['ref'] = ''; $data[0]['all rows'] = ''; $data[0]['possible_keys'] = ''; if (strtoupper(substr($sql, 0, 6)) == 'SELECT') { $re = sqlite_query('EXPLAIN ' . $sql, $connection); $i = 0; while (true == ($row = sqlite_fetch_array($re, SQLITE_NUM))) { $data[$i]['select_type'] = (string) $row[1]; $data[$i]['table'] = (string) $row[2]; $data[$i]['key'] = (string) $row[5]; $data[$i]['key_len'] = (string) $row[6]; $data[$i]['Extra'] = (string) $row[9]; if ($i == 0) { $data[$i]['query'] = ''; } $data[$i]['type'] = (string) $row[3]; $data[$i]['id'] = (string) $row[0]; $data[$i]['ref'] = (string) $row[7]; $data[$i]['all rows'] = (string) $row[8]; $data[$i]['possible_keys'] = (string) $row[4]; $i++; } } $data[0]['query'] = $sql; } else { $data = null; } Core::debug()->profiler('sql')->stop($data); } // Set the last query $this->last_query = $sql; if ($type === 'INSERT' || $type === 'REPLACE') { // Return a list of insert id and rows created return array(sqlite_last_insert_rowid($connection), sqlite_changes($connection)); } elseif ($type === 'UPDATE' || $type === 'DELETE') { // Return the number of rows affected return sqlite_changes($connection); } else { // Return an iterator of results return new Database_Driver_SQLite_Result($result, $sql, $as_object, $this->config); } }
/** * 获取$accept_language * * @return array */ protected static function accept_language() { if (null !== self::$accept_language) { return self::$accept_language; } if (self::$core_initialized && ($local_cookie_name = Core::config('local_lang_cookie_name')) && isset($_COOKIE[$local_cookie_name]) && preg_match('#[a-z0-9\\-_]+#i', $_COOKIE[$local_cookie_name])) { # 读取COOKIE中的语言包设置 $accept_language = (string) $_COOKIE[$local_cookie_name]; } elseif (self::$core_initialized && ($lang_config = Core::config('lang')) && $lang_config !== 'auto') { # 系统设置的语言包 $accept_language = explode(',', $lang_config); } else { # 客户端语言包 $language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : null; $accept_language = array(); # 匹配语言设置 if ($language && false === strpos($language, ';')) { # zh-cn if (preg_match('#^([a-z]+(?:\\-[a-z]+)?),#i', $language, $m)) { $accept_language = array(rtrim(strtolower($language), ',')); } else { $accept_language = array(strtolower($language)); } } elseif ($language) { $language = strtolower(trim(str_replace(',', ';', preg_replace('#(,)?q=[0-9\\.]+(,)?#', '', $language)), ';')); $accept_language = explode(';', $language); $accept_language = array_values(array_slice($accept_language, 0, 4)); //只取前4个语言设置 } if (self::$core_initialized && ($default_lang = Core::config('default_lang')) && !in_array($default_lang, $accept_language)) { $accept_language[] = $default_lang; } /* $accept_language 整理之前 Array ( [0] => ko-kr [1] => en-us [2] => zh-cn ) $accept_language 整理之后 Array ( [0] => ko-kr [1] => ko [2] => en-us [3] => en [4] => zh-cn [5] => zh ) */ $renew_accept_language = array(); foreach ($accept_language as $item) { $sub_lang = explode('-', $item); $renew_accept_language[] = $item; if (count($sub_lang) > 1) { $renew_accept_language[] = $sub_lang[0]; } } $accept_language = array_unique($renew_accept_language); } self::$accept_language = $accept_language; if (IS_DEBUG) { Core::debug()->info(self::$accept_language, 'language'); } return self::$accept_language; }
<title>GCM Demo</title> </head> <body> <?php require_once "../objects/Core.php"; require_once "../objects/models/DeviceRegistration.php"; require_once "../objects/controllers/GCMPush.php"; require_once "../objects/Settings.php"; $core = new Core(); global $core; $core->debugger(Settings::$debug); $db = new DatabaseConn(Settings::$db_server, Settings::$db_user, Settings::$db_pass, Settings::$db_name); $db_sets = array("prod" => $db); $db_key = "prod"; $core->connect_db($db_sets[$db_key], $db_key, true); $core->debug("Database Connected"); $gcm = new GCMPush(); $gcm->setAPIKey(Settings::$gcm_key); if ($_GET["action"] == "delete") { DeviceRegistration::deleteAll(); } $count = sizeof($gcm->getAllIDs()); ?> <h1>There are <?php echo $count; ?> IDs registered in the database</h1> <?php if ($count > 0) { ?> <form method="get">
/** * 执行查询 * * 目前支持插入、修改、保存(类似mysql的replace)查询 * * $use_connection_type 默认不传为自动判断,可传true/false,若传字符串(只支持a-z0-9的字符串),则可以切换到另外一个连接,比如传other,则可以连接到$this->_connection_other_id所对应的ID的连接 * * @param array $options * @param string $as_object 是否返回对象 * @param boolean $use_master 是否使用主数据库,不设置则自动判断 * @return Database_Driver_Mongo_Result */ public function query($options, $as_object = null, $use_connection_type = null) { if (\IS_DEBUG) { \Core::debug()->log($options); } if (\is_string($options)) { # 设置连接类型 $this->_set_connection_type($use_connection_type); // 必需数组 if (!\is_array($as_object)) { $as_object = array(); } return $this->connection()->execute($options, $as_object); } $type = \strtoupper($options['type']); $typeArr = array('SELECT', 'SHOW', 'EXPLAIN', 'DESCRIBE', 'INSERT', 'BATCHINSERT', 'REPLACE', 'SAVE', 'UPDATE', 'REMOVE'); $slaverType = array('SELECT', 'SHOW', 'EXPLAIN'); if (\in_array($type, $slaverType)) { if (true === $use_connection_type) { $use_connection_type = 'master'; } else { if (\is_string($use_connection_type)) { if (!\preg_match('#^[a-z0-9_]+$#i', $use_connection_type)) { $use_connection_type = 'master'; } } else { $use_connection_type = 'slaver'; } } } else { $use_connection_type = 'master'; } # 设置连接类型 $this->_set_connection_type($use_connection_type); # 连接数据库 $connection = $this->connection(); if (!$options['table']) { throw new \Exception('查询条件中缺少Collection'); } $tablename = $this->config['table_prefix'] . $options['table']; if (\IS_DEBUG) { static $is_sql_debug = null; if (null === $is_sql_debug) { $is_sql_debug = (bool) \Core::debug()->profiler('sql')->is_open(); } if ($is_sql_debug) { $host = $this->_get_hostname_by_connection_hash($this->connection_id()); $benchmark = \Core::debug()->profiler('sql')->start('Database', 'mongodb://' . ($host['username'] ? $host['username'] . '@' : '') . $host['hostname'] . ($host['port'] && $host['port'] != '27017' ? ':' . $host['port'] : '')); } } try { switch ($type) { case 'SELECT': if ($options['distinct']) { # 查询唯一值 $result = $connection->command(array('distinct' => $tablename, 'key' => $options['distinct'], 'query' => $options['where'])); $last_query = 'db.' . $tablename . '.distinct(' . $options['distinct'] . ', ' . \json_encode($options['where']) . ')'; if ($result && $result['ok'] == 1) { $rs = new \Database_Driver_Mongo_Result(new \ArrayIterator($result['values']), $options, $as_object, $this->config); } else { throw new \Exception($result['errmsg']); } } elseif ($options['group']) { # group by $option = array(); if ($options['group']['cond']) { $option['condition'] = $options['group']['cond']; } if ($options['group']['finalize']) { $option['finalize'] = $options['group']['finalize']; } $result = $connection->selectCollection($tablename)->group($options['group']['key'], $options['group']['initial'], $options['group']['reduce'], $option); $last_query = 'db.' . $tablename . '.group({"key":' . \json_encode($options['group']['key']) . ', "initial":' . \json_encode($options['group']['initial']) . ', "reduce":' . (string) $options['group']['reduce'] . ', ' . (isset($options['group']['finalize']) && $options['group']['finalize'] ? '"finalize":' . (string) $options['group']['finalize'] : '"cond":' . \json_encode($options['group']['cond'])) . '})'; if ($result && $result['ok'] == 1) { $rs = new \Database_Driver_Mongo_Result(new \ArrayIterator($result['retval']), $options, $as_object, $this->config); } else { throw new \Exception($result['errmsg']); } } else { $last_query = 'db.' . $tablename . '.find('; $last_query .= $options['where'] ? \json_encode($options['where']) : '{}'; $last_query .= $options['select'] ? ',' . \json_encode($options['select']) : ''; $last_query .= ')'; $result = $connection->selectCollection($tablename)->find($options['where'], (array) $options['select']); if ($options['total_count']) { $result = $result->count(); # 仅统计count $rs = new \Database_Driver_Mongo_Result(new \ArrayIterator(array(array('total_row_count' => $result))), $options, $as_object, $this->config); } else { if ($options['sort']) { $last_query .= '.sort(' . \json_encode($options['sort']) . ')'; $result = $result->sort($options['sort']); } if ($options['skip']) { $last_query .= '.skip(' . \json_encode($options['skip']) . ')'; $result = $result->skip($options['skip']); } if ($options['limit']) { $last_query .= '.limit(' . \json_encode($options['limit']) . ')'; $result = $result->limit($options['limit']); } $rs = new \Database_Driver_Mongo_Result($result, $options, $as_object, $this->config); } } break; case 'UPDATE': $result = $connection->selectCollection($tablename)->update($options['where'], $options['data'], $options['options']); $rs = $result['n']; $last_query = 'db.' . $tablename . '.update(' . \json_encode($options['where']) . ',' . \json_encode($options['data']) . ')'; break; case 'SAVE': case 'INSERT': case 'BATCHINSERT': $fun = \strtolower($type); $result = $connection->selectCollection($tablename)->{$fun}($options['data'], $options['options']); if ($type == 'BATCHINSERT') { # 批量插入 $rs = array('', \count($options['data'])); } elseif (isset($result['data']['_id']) && $result['data']['_id'] instanceof \MongoId) { $rs = array((string) $result['data']['_id'], 1); } else { $rs = array('', 0); } if ($type == 'BATCHINSERT') { $last_query = ''; foreach ($options['data'] as $d) { $last_query .= 'db.' . $tablename . '.insert(' . \json_encode($d) . ');' . "\n"; } $last_query = \trim($last_query); } else { $last_query = 'db.' . $tablename . '.' . $fun . '(' . \json_encode($options['data']) . ')'; } break; case 'REMOVE': $result = $connection->selectCollection($tablename)->remove($options['where']); $rs = $result['n']; $last_query = 'db.' . $tablename . '.remove(' . \json_encode($options['where']) . ')'; break; default: throw new \Exception('不支持的操作类型'); } } catch (\Exception $e) { if (\IS_DEBUG && isset($benchmark)) { \Core::debug()->profiler('sql')->stop(); } throw $e; } $this->last_query = $last_query; # 记录调试 if (\IS_DEBUG) { \Core::debug()->info($last_query, 'MongoDB'); if (isset($benchmark)) { if ($is_sql_debug) { $data = array(); $data[0]['db'] = $host['hostname'] . '/' . $this->config['connection']['database'] . '/'; $data[0]['cursor'] = ''; $data[0]['nscanned'] = ''; $data[0]['nscannedObjects'] = ''; $data[0]['n'] = ''; $data[0]['millis'] = ''; $data[0]['row'] = \count($result); $data[0]['query'] = ''; $data[0]['nYields'] = ''; $data[0]['nChunkSkips'] = ''; $data[0]['isMultiKey'] = ''; $data[0]['indexOnly'] = ''; $data[0]['indexBounds'] = ''; if ($type == 'SELECT' && !$options['group'] && \is_object($result) && \method_exists($result, 'explain')) { $re = $result->explain(); foreach ($re as $k => $v) { $data[0][$k] = $v; } } $data[0]['query'] = $last_query; } else { $data = null; } \Core::debug()->profiler('sql')->stop($data); } } return $rs; }
/** * 反解数据 * * @param mixed $data * @param array $config */ public static function de_format_data($data, $config) { $config = \array_reverse($config); foreach ($config as $v) { try { if (\is_array($v)) { $fun = 'OOP_ORM_Parse::_de_format_action_' . \array_shift($v); \array_unshift($v, $data); $data = \call_user_func_array($fun, $v); } else { $fun = '_de_format_action_' . $v; $data = static::$fun($data); } } catch (\Exception $e) { \Core::debug()->error('ORM数据解析失败,方法:' . $fun . '。'); } } return $data; }
/** * 处理解析LESS文件 * * @throws Exception */ protected function prease_css($out_file, $type, &$content) { # 通过recess处理less文件 $tmpfile = DIR_TEMP . 'tmpless_' . md5($this->file) . $type; if (!function_exists('exec')) { throw new Exception(__('The system does not allow you to execute exec function, you can not call the node processing less file')); } try { file_put_contents($tmpfile, $content); } catch (Exception $e) { if (strpos($e->getMessage(), 'Permission denied') !== false) { Core::show_500(__('Permission denied : :file', array(':file' => Core::debug_path($tmpfile)))); } throw $e; } list($node_file, $node_modules_path) = $this->get_node_set(); static $assets_path = null; if (null === $assets_path) { $include_path = Core::include_path(); $include_path = array_reverse($include_path); # assets目录,经过 escapeshellarg 处理,用于sass,less处理时增加包含目录参数 $assets_path = array(); $tmp_dir = dirname($this->file); if ($tmp_dir && $tmp_dir != '.') { $tmp_dir .= '/'; } else { $tmp_dir = ''; } # 循环include path foreach ($include_path as $path) { $dir = $path . "assets/" . $tmp_dir; if (is_dir($dir)) { $assets_path[] = escapeshellarg($dir); } } } if ($type == 'less') { if ($assets_path) { $path_str = ' --includePath=' . implode(' --includePath=', $assets_path); } $cmd = 'cd ' . escapeshellcmd($node_modules_path) . ' && ' . escapeshellcmd($node_file) . ' ' . escapeshellarg('./node_modules/recess/bin/recess') . ' --compile' . $path_str . ' ' . escapeshellarg($tmpfile); } else { if ($assets_path) { $path_str = ' --load-path=' . implode(' --load-path=', $assets_path); } $cmd = 'sass -t expanded' . $path_str . ' ' . escapeshellarg($tmpfile); } if (IS_DEBUG) { Core::debug()->info($cmd, 'exec'); } # 执行 exec($cmd, $output, $r); # 删除临时文件 @unlink($tmpfile); if (0 === $r) { if ($output) { # 更新content $content = implode("\r\n", $output); } } else { if (127 === $r) { throw new Exception(__('Systems perform less processing failed, please check the implementation of the recess command')); } else { if (1 === $r) { throw new Exception(__('Systems perform less processing failed, please check the file. cmd: %c, output: %s', array('%c' => $cmd, '%s' => implode("\n", $output)))); } else { $err = array(1 => __('Please check no recess module installed or not set node_modules path')); throw new Exception(__('Systems perform less handling failed,RsCode:%s', array('%s' => $r . '.' . (isset($err[$r]) ? $err[$r] : '')))); } } } }
/** * 匹配路由 * * @param string $uri 请求的URI * @param string $route_name 使用路由的名称 * @return boolean|Ambigous <boolean, mixed, multitype:unknown > */ protected static function _matches($uri, $route_name) { if (!$route_name || !isset(Route::$regex[Core::$project][$route_name])) { return false; } if (IS_DEBUG) { Core::debug()->group('路由匹配'); Core::debug()->info(array('URI:' => $uri, 'Route:' => Route::$regex[Core::$project][$route_name]), '匹配'); } if (!preg_match(Route::$regex[Core::$project][$route_name], $uri, $matches)) { if (IS_DEBUG) { Core::debug()->info('↑未匹配到当前路由'); Core::debug()->groupEnd(); } return false; } $params = array(); foreach ($matches as $key => $value) { if (is_int($key)) { // 跳过 continue; } $params[$key] = $value; } $route_config = Core::config('route.' . $route_name); if ($route_config) { if (isset($route_config['default']) && is_array($route_config['default'])) { foreach ($route_config['default'] as $key => $value) { if (!isset($params[$key]) || $params[$key] === '') { $params[$key] = $value; } } } // 处理回调过滤 if (isset($route_config['filters']) && is_array($route_config['filters'])) { foreach ($route_config['filters'] as $callback) { $return = call_user_func($callback, $params); if (false === $return) { $params = false; } elseif (is_array($return)) { $params = $return; } } } } if (IS_DEBUG) { Core::debug()->info($params, '获取参数'); Core::debug()->groupEnd(); } return $params; }
/** * 关闭所有memcache链接 */ public static function close_all_connect() { foreach (static::$memcaches as $config_name => $memcache) { try { $memcache->close(); } catch (\Exception $e) { \Core::debug()->error('close memcache connect error:' . $e); } static::$memcaches[$config_name] = null; } # 重置全部数据 static::$memcaches = array(); static::$memcaches_num = array(); if (\IS_DEBUG) { \Core::debug()->info('close all memcache server.'); } }
/** * 关闭所有链接 */ public static function close_all_connect() { foreach (Cache_Driver_Redis::$redis as $config_name => $obj) { try { $obj->close(); } catch (Exception $e) { Core::debug()->error('close cache redis connect error:' . $e); } Cache_Driver_Redis::$redis[$config_name] = null; } # 重置全部数据 Cache_Driver_Redis::$redis = array(); Cache_Driver_Redis::$redis_num = array(); if (IS_DEBUG) { Core::debug()->info('close all cache redis server.'); } }
/** * 保存数据 * * @param ORM_Admin_MemberGroup_Data $group */ protected function save(ORM_Admin_MemberGroup_Data $group) { if (isset($_POST['group_name']) && $this->check_auth_for_info($group)) { if (empty($_POST['group_name'])) { $this->message('权限组名称不能空', 0); } if (strlen($_POST['group_desc']) > 1000) { $this->message('权限组说明太长了,限定1000个字符', 0); } $group->group_name = $_POST['group_name']; $group->group_desc = $_POST['group_desc']; $group->sort = (int) $_POST['sort']; # 群设置 if (isset($_POST['setting']['menu_config']) && !$this->session()->member()->perm()->is_own('administrator.edit_menu_config')) { # 若不具备菜单管理权限,则清除此配置 unset($_POST['setting']['menu_config']); } $data = (array) $group->setting; if (is_array($_POST['setting'])) { foreach ($_POST['setting'] as $k => $v) { $data[$k] = $v; } } $group->setting = $data; } # 处理权限 if (isset($_POST['perm_setting']) && is_array($_POST['perm_setting']) && $this->check_auth_for_perm($group)) { try { $perm_setting = Controller_Administrator_Index::check_perm_data($_POST['perm_setting']); } catch (Exception $e) { $this->message($e->getMessage(), $e->getCode()); } # 设置数据 $group->perm_setting = $perm_setting; } try { if ($group->id) { $s = $group->update(); } else { # 指定项目 $group->project = Core::$project; $s = $group->insert(); } if ($s) { $this->message('保存成功', 1); } else { $this->message('未保存任何数据'); } } catch (Exception $e) { Core::debug()->error($e->getMessage()); $this->message('保存失败,请重试', -1); } }