public function __construct($logName, $options = array())
 {
     $logName = trim($logName);
     if (empty($logName)) {
         $logName = 'zc';
     }
     if (stripos($logName, '.log') === false) {
         $logName .= '.log';
     }
     $logName = str_replace(array('/', '\\'), '-', $logName);
     $logDir = !empty($options['logDir']) ? $options['logDir'] : Zc::C(ZcConfigConst::LogDir);
     $logDir = rtrim($logDir, '/') . '/';
     $today = date('Y-m-d', time());
     $yesterday = date('Y-m-d', time() - 24 * 3600);
     $todayLogDir = $logDir . $today . '/';
     $yesterdayLogDir = $logDir . $yesterday . '/';
     $this->logFile = $todayLogDir . $logName;
     $this->mkdir($todayLogDir);
     // 加这个标识,纯粹就是为了让logstash能够得到multiline
     if (!file_exists($this->logFile)) {
         $yesterdayLogFile = $yesterdayLogDir . $logName;
         error_log(date("[c]") . " -- End {$yesterday} Log --\r\n", 3, $yesterdayLogFile);
         error_log(date("[c]") . " -- Start {$today} Log --\r\n", 3, $this->logFile);
     }
 }
 public function __construct($hostname = '', $username = '', $password = '', $database = '')
 {
     if (empty($hostname)) {
         $hostname = Zc::C(ZcConfigConst::DbHostname);
         $username = Zc::C(ZcConfigConst::DbUsername);
         $password = Zc::C(ZcConfigConst::DbPassword);
         $database = Zc::C(ZcConfigConst::DbDatabase);
     }
     $this->errorDisplay = Zc::C(ZcConfigConst::MonitorExitOnDbError) == true ? true : false;
     if (!($this->connection = mysql_connect($hostname, $username, $password, true))) {
         if ($this->errorDisplay) {
             exit('Error: Could not make a database connection using ' . $username . '@' . $hostname);
         }
         return false;
     }
     if (!mysql_select_db($database, $this->connection)) {
         if ($this->errorDisplay) {
             exit('Error: Could not connect to database ' . $database);
         }
         return false;
     }
     mysql_query("SET NAMES 'utf8'", $this->connection);
     mysql_query("SET CHARACTER SET utf8", $this->connection);
     mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection);
     mysql_query("SET SQL_MODE = ''", $this->connection);
 }
 /**
  * 全局唯一语言对象
  * @return ZcLanguage
  */
 static function getLanguageObject()
 {
     if (empty(self::$objectPool['language'])) {
         self::$objectPool['language'] = new ZcLanguage(Zc::C(ZcConfigConst::LanguageCurrent), Zc::C(ZcConfigConst::LanguageDefault));
     }
     return self::$objectPool['language'];
 }
Exemple #4
0
 public function __construct()
 {
     $urlHandlerConfig = ZcFactory::getConfig()->get(ZcConfigConst::UrlHandler);
     if (!empty($urlHandlerConfig['file'])) {
         require_once Zc::C(ZcConfigConst::DirFsApp) . $urlHandlerConfig['file'];
     }
     $this->urlHandler = new $urlHandlerConfig['class']();
 }
Exemple #5
0
 /**
  +----------------------------------------------------------
  * 渲染模板输出 供render方法内部调用
  +----------------------------------------------------------
  * @access public
  +----------------------------------------------------------
  * @param string $templateFile  模板文件
  * @param mixed $var  模板变量
  +----------------------------------------------------------
  * @return string
  +----------------------------------------------------------
 */
 protected function renderFile($templateFile = '', $renderData = '')
 {
     ob_start();
     ob_implicit_flush(0);
     if (!empty($renderData)) {
         extract($renderData, EXTR_OVERWRITE);
     }
     $widgetFile = Zc::C(ZcConfigConst::DirFsViewsWidget) . $templateFile . '.php';
     if (file_exists($widgetFile)) {
         include $widgetFile;
     }
     $content = ob_get_clean();
     return $content;
 }
 public function __construct($timestamp, $options = '')
 {
     $this->log = Zc::getLog('cache/file_cache.log');
     $this->options = array('temp' => G_CACHING_CACHEFS_DIRECTORY, 'expire' => 3600, 'use_subdir' => true, 'subdir_level' => 2, 'cache_data_check' => false, 'cache_data_compress' => false);
     if (!empty($options)) {
         $this->options = array_merge($this->options, $options);
     }
     if (substr($this->options['temp'], -1) != '/') {
         $this->options['temp'] .= '/';
     }
     $this->timestamp = $timestamp;
     $this->options['temp'] .= $this->timestamp . '/';
     $this->init();
     $this->conntected = is_dir($this->options['temp']) && is_writeable($this->options['temp']);
 }
 public function __construct($timestamp, $options = '')
 {
     $this->log = Zc::getLog('cache/apc_cache.log');
     if (!function_exists('apc_cache_info') || apc_cache_info() === false) {
         $this->log->info("apc module cound not found or disable");
         $this->conntected = false;
     } else {
         $this->conntected = true;
     }
     $this->options = array('expire' => 3600);
     if (!empty($options)) {
         $this->options = array_merge($this->options, $options);
     }
     $this->timestamp = $timestamp;
 }
 function __construct($logName, $options = array())
 {
     $logName = trim($logName);
     if (empty($logName)) {
         $logName = 'zc';
     }
     if (stripos($logName, '.log') === false) {
         $logName .= '.log';
     }
     if ($logName[0] != '/' && $logName[1] != ':') {
         $logDir = !empty($options['logDir']) ? $options['logDir'] : Zc::C(ZcConfigConst::LogDir);
         $logName = rtrim($logDir, '/') . '/' . $logName;
     }
     $logFile = $logName . '.' . date('Y-m-d', time());
     $this->logFile = $logFile;
     $logDir = dirname($logFile);
     $this->mkdir($logDir);
 }
 /**
  * 
  * 记录Log信息及其日志信息
  * 
  * @param string $message
  */
 public function log($message)
 {
     parent::log($message);
     if (!self::$isEnable) {
         return;
     }
     if (self::$isSupportedRedis && !self::$redis) {
         self::$redis = new Redis();
         try {
             self::$isSupportedRedis = self::$redis->connect(Zc::C(ZcConfigConst::LogHandlerLogstashredisHost), Zc::C(ZcConfigConst::LogHandlerLogstashredisPort), 1);
         } catch (Exception $ex) {
             self::$isSupportedRedis = false;
             parent::log(print_r($ex, true));
         }
     }
     if (self::$isSupportedRedis) {
         self::$redis->rPush($this->redisKey, '[' . $this->logName . '] ' . $message);
     }
 }
 /**
  * 寻找Layout的方法
  */
 private function getLayoutFiles($view)
 {
     if (empty($view)) {
         $view = $this->route;
     }
     $dirViewsLayout = Zc::C(ZcConfigConst::DirFsViewsLayout);
     $layoutFiles = array();
     $layoutFiles[] = $dirViewsLayout . 'default.php';
     $path = '';
     $parts = explode('/', $view);
     array_pop($parts);
     foreach ($parts as $part) {
         $path .= $part . '/';
         $layoutFiles[] = $dirViewsLayout . $path . 'default.php';
     }
     $layoutFiles[] = $dirViewsLayout . $view . '.php';
     $layoutFiles = array_reverse($layoutFiles);
     //想知道是按照什么顺序来寻找Layout的,把下面这行代码注释掉就行了
     //dump($layoutFiles);exit;
     return $layoutFiles;
 }
Exemple #11
0
 public function __construct($route, $args = array())
 {
     $dirController = Zc::C(ZcConfigConst::DirFsLibsController);
     $this->route = $route;
     $path = '';
     $parts = explode('/', $route);
     foreach ($parts as $part) {
         $tryDir = $path . $part . '/';
         if (is_dir($dirController . $tryDir)) {
             $path = $tryDir;
             array_shift($parts);
             continue;
         }
         $tryClass = str_replace(' ', '', ucwords(strtolower(str_replace(array('-', '_'), ' ', $part)))) . 'Controller';
         $tryFile = $path . 'class.' . $tryClass . '.php';
         if (is_file($dirController . $tryFile)) {
             $this->file = $dirController . $tryFile;
             $this->class = $tryClass;
             array_shift($parts);
             break;
         }
     }
     if ($args) {
         $this->args = $args;
     }
     $method = array_shift($parts);
     if ($method) {
         //$method = preg_replace ( '/(?:^|_)(.?)/e', "strtoupper('$1')", $method );
         $method = preg_replace_callback('/(?:^|_)(.?)/', function ($matches) {
             return strtolower($matches[1]);
         }, $method);
         $method[0] = strtolower($method[0]);
         $this->method = $method;
     } else {
         $this->method = 'index';
         $this->route = $this->route . (substr($this->route, -1) == '/' ? 'index' : '/index');
     }
 }
Exemple #12
0
 /**
  * Constructor: Single parameter allows setting of global encoding for use by
  * the current object. If PHP 5.4 is detected, additional ENT_SUBSTITUTE flag
  * is set for htmlspecialchars() calls.
  *
  * @param string $encoding
  * @throws Exception\InvalidArgumentException
  */
 public function __construct($encoding = null)
 {
     if ($encoding !== null) {
         $encoding = (string) $encoding;
         if ($encoding === '') {
             throw new Exception(get_class($this) . ' constructor parameter does not allow a blank value');
         }
         $encoding = strtolower($encoding);
         if (!in_array($encoding, $this->supportedEncodings)) {
             throw new Exception('Value of \'' . $encoding . '\' passed to ' . get_class($this) . ' constructor parameter is invalid. Provide an encoding supported by htmlspecialchars()');
         }
         $this->encoding = $encoding;
     } else {
         $this->encoding = Zc::C(ZcConfigConst::DefaultOutputEncoding);
     }
     if (defined('ENT_SUBSTITUTE')) {
         $this->htmlSpecialCharsFlags |= ENT_SUBSTITUTE;
     }
     // set matcher callbacks
     $this->htmlAttrMatcher = array($this, 'htmlAttrMatcher');
     $this->jsMatcher = array($this, 'jsMatcher');
     $this->cssMatcher = array($this, 'cssMatcher');
 }
Exemple #13
0
 public static function startSessionWithParams($sessionName = '', $sessionDomain = '', $sessionId = '', $sessionType = 'file', $options = array())
 {
     // 		Zc::dump($sessionName);
     // 		Zc::dump($sessionDomain);
     // 		Zc::dump($sessionId);
     // 		Zc::dump($sessionType);
     // 		Zc::dump($options);
     self::$log = Zc::getLog('session/manager.log');
     if (!empty($sessionName)) {
         session_name($sessionName);
     }
     session_set_cookie_params(0, '/', !empty($sessionDomain) ? $sessionDomain : '');
     if (!empty($sessionId)) {
         session_id($sessionId);
     }
     if ($sessionType === 'memcached' || $sessionType == 'db') {
         $handlerClassName = 'Zc' . ucfirst($sessionType) . 'SessionHandler';
         self::$sessionHander = new $handlerClassName($options);
         session_set_save_handler(array(self::$sessionHander, 'open'), array(self::$sessionHander, 'close'), array(self::$sessionHander, 'read'), array(self::$sessionHander, 'write'), array(self::$sessionHander, 'destroy'), array(self::$sessionHander, 'gc'));
         register_shutdown_function('session_write_close');
     } elseif ($sessionType === 'file') {
         if (!empty($options['session_save_path'])) {
             session_save_path($options['session_save_path']);
         }
     } else {
         throw new Exception("do not support session type {$sessionType}");
     }
     $ret = session_start();
     if (!$ret) {
         self::$log->monitor("{$sessionType} session start failed");
     }
     self::$isStart = $ret;
     if (!isset($_SESSION['securityToken'])) {
         $_SESSION['securityToken'] = md5(uniqid(rand(), true));
     }
 }
 public function __construct()
 {
     $this->lifeTime = $this->getLiftTime();
     $this->sessionLog = Zc::getLog('session/handler.log', ZcLog::INFO, false);
 }
Exemple #15
0
 public function __construct($config = '')
 {
     if (empty($config)) {
         $config = Zc::C(ZcConfigConst::DbConfig);
     }
     $this->config = $config;
     // 初始化DB缓存
     if (isset($this->config['db_cache'])) {
         $cc = $this->config['db_cache'];
         $this->cache = Zc::getCache($cc['biz_name'], $cc['cache_type'], $cc['timestamp'], $cc['options']);
     }
     // 重新计算分布式DB的配置和权重
     foreach ($this->config['connections'] as $groupName => &$groupConfig) {
         $this->groupMapping[$groupName]['master'] = array('role' => 'master', 'db_id' => $groupConfig['master']['db_id'], 'read_weight' => isset($groupConfig['master']['read_weight']) ? $groupConfig['master']['read_weight'] : false);
         $this->dbIdMapping[$groupConfig['master']['db_id']] =& $groupConfig['master'];
         $this->dbIdMapping[$groupConfig['master']['db_id']]['role'] = 'master';
         if (!empty($groupConfig['slaves'])) {
             foreach ($groupConfig['slaves'] as &$slaveConfig) {
                 $this->dbIdMapping[$slaveConfig['db_id']] =& $slaveConfig;
                 $this->dbIdMapping[$slaveConfig['db_id']]['role'] = 'slave';
                 $this->groupMapping[$groupName][] = array('role' => 'slave', 'db_id' => $slaveConfig['db_id'], 'read_weight' => isset($slaveConfig['read_weight']) ? $slaveConfig['read_weight'] : false);
             }
         }
     }
     //重新计算下每台机器要分配的读权重
     foreach ($this->groupMapping as $groupName => &$group) {
         $countWeight = 0;
         $setCount = 0;
         foreach ($group as &$host) {
             if ($host['read_weight'] !== false) {
                 $setCount++;
             }
             $countWeight += $host['read_weight'];
         }
         if ($setCount === 0) {
             $setCount = count($group);
             $countWeight = 100;
         }
         $unsetWeight = (int) ($countWeight / $setCount);
         $currValve = 0;
         foreach ($group as &$host) {
             if ($host['read_weight'] === false) {
                 $host['read_weight'] = $unsetWeight;
             }
             $currValve += $host['read_weight'];
             $host['read_valve'] = $currValve;
         }
         $this->groupMaxValve[$groupName] = $currValve;
     }
 }
Exemple #16
0
 public static function surlParam($string)
 {
     /* @var $escaper ZcEscaper */
     $escaper = Zc::singleton('ZcEscaper');
     return $escaper->escapeUrl($string);
 }
 public function close()
 {
     Zc::G(' memcache close start');
     $ret = $this->memcache->close();
     if (!$ret) {
         $this->log->error("memcache close failed!");
     }
     Zc::G(' memcache close end');
     return $ret;
 }
Exemple #18
0
 public function __construct()
 {
     $this->db = Zc::getDb();
 }
 /**
  * 渲染分页链接
  * 
  * @return string
  */
 public function renderLinks()
 {
     $displayStr = sprintf('<div class="pagination zc-pagination %s"><ul>', $this->themeCssClass);
     // 渲染“上一页”按钮
     if ($this->currPageNo == 1) {
         $displayStr .= '<li class="active zc-prev"><span class="zc-current">' . $this->prevText . '</span></li>';
     } else {
         $pageUrl = $this->buildPageUrl($this->currPageNo - 1);
         $displayStr .= sprintf('<li class="zc-prev"><a href="%s" title="%s">%s</a></li>', Zc::shm($pageUrl), $this->prevATitle, $this->prevText);
     }
     // 渲染中间的分页按钮
     foreach ($this->layout as $page) {
         if (is_int($page)) {
             if ($page == $this->currPageNo) {
                 $displayStr .= '<li class="active"><span class="zc-current">' . $page . '</span></li>';
             } else {
                 $pageUrl = $this->buildPageUrl($page);
                 $displayStr .= sprintf('<li><a href="%s" title="%s">%s</a></li>', Zc::shm($pageUrl), Zc::shm(sprintf($this->pageATitle, $page)), $page);
             }
         } else {
             $displayStr .= '<li class="disabled"><span class="zc-ellipse">…</span></li>';
         }
     }
     // 渲染“下一页”按钮
     if ($this->currPageNo == $this->totalPageNum) {
         $displayStr .= '<li class="active zc-next"><span class="zc-current">' . $this->nextText . '</span></li>';
     } else {
         $pageUrl = $this->buildPageUrl($this->currPageNo + 1);
         $displayStr .= sprintf('<li class="zc-next"><a href="%s" title="%s">%s</a></li>', Zc::shm($pageUrl), $this->nextATitle, $this->nextText);
     }
     $displayStr .= "</ul></div>";
     return $displayStr;
 }
 public function __construct($timestamp, $options = '')
 {
     $this->log = Zc::getLog('cache/debug_cache.log');
     $this->timestamp = $timestamp;
 }
 public function write($key, $val)
 {
     Zc::G('session start write to memcache');
     if (empty($val)) {
         $this->sessionLog->info("{$key} want to write, but {$val} is empty");
         return;
     } else {
         if ($this->sessionLog->isEnableLogLevel(ZcLog::DEBUG)) {
             $this->sessionLog->debug("start write [{$key}] [{$val}]");
         }
     }
     if ($this->masterConntected) {
         $retMaster = $this->masterMemcache->set($key, $val, 0, $this->lifeTime);
         if (!$retMaster) {
             $this->sessionLog->error("{$key} {$val} write to master failed. try slave");
         }
     }
     if ($this->slaveConntected) {
         $retSlave = $this->slaveMemcache->set($key, $val, 0, $this->lifeTime);
         if (!$retSlave) {
             $this->sessionLog->error("{$key} {$val} write to salve failed.");
         }
     }
     if ($this->sessionLog->isEnableLogLevel(ZcLog::DEBUG)) {
         $this->sessionLog->debug("write [{$key}] -> [{$val}], master write ret [" . $retMaster . '], slave write ret [' . $retSlave . ']');
     }
     if (!$retMaster && !$retSlave) {
         $this->sessionLog->error("write to master and slave are all failed. [{$key}] [{$val}]");
     }
     Zc::G('session end write to memcache');
 }