function dispatch_url($url) { $moduleRoot = ROOT . EnvConf::$moduleRoot . "/" . App::getModule(); $classFile = "{$moduleRoot}/controllers/"; $subDir = App::getSubDir(); if (!empty($subDir)) { $classFile .= $subDir . '/'; } $className = ucfirst(App::getController()) . 'Controller'; $func = ucfirst(App::getAction()) . 'Action'; KC_LOG_DEBUG("hit controller: {$className}, action: {$func}."); $classFile .= strtolower(substr($className, 0, -10)) . '.php'; if (!is_readable($classFile)) { KC_LOG_DEBUG("not_found invalid controller which is unavailable [ file: {$classFile} ]"); throw new Exception("not_found invalid controller which is unavailable."); } require_once $classFile; if (!class_exists($className)) { KC_LOG_DEBUG("not_found invalid controller which is unavailable [ file: {$classFile}, class: {$className} ]"); throw new Exception("not_found invalid controller which is unavailable."); } $obj = new $className(); $obj->request = $this->app->request; $obj->response = $this->app->response; $obj->encoding = $this->app->encoding; $obj->debug = $this->app->debug; try { $this->_callmethod($obj, '_before'); if (!$this->_callmethod($obj, $func, array())) { throw new Exception("not_found call method failed [ method name: {$func} ]"); } } catch (Exception $ex) { $this->_callmethod($obj, '_after'); throw $ex; } $this->_callmethod($obj, '_after'); }
function send() { $data = $this->_formatResponse(); $this->sendHeaders(); //获取缓冲数据 $ob = ini_get('output_buffering'); if ($ob && strtolower($ob) !== 'off') { $str = ob_get_clean(); //忽略前后空白 $data = trim($str) . $data; } if ($data) { if (EnvConf::$debug) { KC_LOG_DEBUG("[RETURNED DATA]\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" . $data . "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n"); } //统计模块 if (true) { $success = true; $code = 0; $msg = "succ"; $module = App::getModule(); $interface = App::getController() . '_' . App::getAction(); $dataArr = json_decode($data, true); if (isset($dataArr['error_code']) && $dataArr['error_code'] == 0) { $msg = $dataArr['error_msg']; } else { $success = false; $code = isset($dataArr['error_code']) ? $dataArr['error_code'] : -1; $msg = (isset($dataArr['error_msg']) ? $dataArr['error_msg'] : "failed") . " ,requestid:" . $this->app->requestId; $msg .= ', ip:' . App::getClientIp(); } $do_not_report_code = array('100801'); if (!in_array($code, $do_not_report_code)) { Statis_Client::report($module, $interface, $success, $code, $msg); } } //support for return format -- jsonp if ('json' === $this->app->request->of) { $cb = $this->app->request->get('callback', ''); $cb = trim($cb); if (!empty($cb)) { $data = $cb . "({$data})"; } } echo $data; } }
/** * * 连接到数据库 * @param string $identifier 连接到哪个数据库,分表依据 * @param string $tag 直接指明连接到哪个数据库 */ public function fetchMysqlHandler($identifier, $tag = NULL) { if ($this->_isConnected) { $this->_mysqli->close(); $this->_isConnected = false; $this->_mysqli = mysqli_init(); } if (!is_null($tag)) { if (!isset(AppConf::$dbConf['server'][Env::getIDC()][intval($tag)])) { KC_LOG_WARNING(__FUNCTION__ . " failed, invalid tag received [ tag: {$tag} ]"); return false; } $arrMysqlServer = AppConf::$dbConf['server'][Env::getIDC()][intval($tag)]; } else { $tag = $this->getDbNo($identifier); $arrMysqlServer = AppConf::$dbConf['server'][Env::getIDC()][intval($tag)]; } $totalNum = count($arrMysqlServer); $index = mt_rand(0, $totalNum - 1); for ($i = 0; $i < $totalNum; $i++) { $mysqlServer = $arrMysqlServer[$index]; if (!isset($mysqlServer['host']) || !isset($mysqlServer['username']) || !isset($mysqlServer['password']) || !isset($mysqlServer['database']) || !isset($mysqlServer['port'])) { KC_LOG_WARNING(__FUNCTION__ . " failed, config must have host/username/password/database/port fields [mysqlServer: " . json_encode($mysqlServer) . "]"); return false; } if (false === $this->_mysqli->real_connect($mysqlServer['host'], $mysqlServer['username'], $mysqlServer['password'], $mysqlServer['database'], $mysqlServer['port'], NULL, 0)) { $index = ++$index % $totalNum; continue; } KC_LOG_DEBUG("fetch mysql conntion host [" . $mysqlServer['host'] . "] port [" . $mysqlServer['port'] . "]"); $this->_mysqli->set_charset(AppConf::$dbConf['charset']); $this->_isConnected = true; break; } if (false === $this->_isConnected) { KC_LOG_WARNING("fetch mysql conntion [identifier: {$identifier}, tag: {$tag}] failed."); return false; } return true; }
private function _errorHandler() { restore_error_handler(); $error = func_get_args(); if (!($error[0] & error_reporting())) { KC_LOG_DEBUG('caught info, errno:%d,errmsg:%s,file:%s,line:%d', $error[0], $error[1], $error[2], $error[3]); set_error_handler(array($this, 'errorHandler')); return false; } elseif ($error[0] === E_USER_NOTICE) { KC_LOG_TRACE('caught trace, errno:%d,errmsg:%s,file:%s,line:%d', $error[0], $error[1], $error[2], $error[3]); set_error_handler(array($this, 'errorHandler')); return false; } elseif ($error[0] === E_STRICT) { set_error_handler(array($this, 'errorHandler')); return false; } else { KC_LOG_FATAL('caught error, errno:%d,errmsg:%s,file:%s,line:%d', $error[0], $error[1], $error[2], $error[3]); $this->endStatus = 'error'; return true; } }
public static function createShortUrl($url, $retry = 3) { if (empty($url)) { return $url; } while ($retry > 0) { #$dwz = "http://dwz.cn/create.php"; $dwz = "http://s.youzu.com/gen.php"; $data = array('url' => $url); $res = self::httpRequest($dwz, 'POST', $data); KC_LOG_DEBUG("create short url ,source url : {$url}, res: {$res}"); $result = json_decode($res, true); $shortUrl = $url; if (isset($result['tinyurl'])) { $shortUrl = $result['tinyurl']; break; } $retry--; } return $shortUrl; }
public static function noticeApi() { $notice_url = isset($_COOKIE[AppConf::$cookiePrefix . '_notice_url']) ? $_COOKIE[AppConf::$cookiePrefix . '_notice_url'] : 0; $api_url = isset($_COOKIE[AppConf::$cookiePrefix . '_api_url']) ? $_COOKIE[AppConf::$cookiePrefix . '_api_url'] : 0; $token = isset($_COOKIE[AppConf::$cookiePrefix . '_token']) ? $_COOKIE[AppConf::$cookiePrefix . '_token'] : 0; if ($api_url != 0) { $params = array("notice_url" => Uc_Com_Utils::sDecode($notice_url), "api_url" => Uc_Com_Utils::sDecode($api_url), "token" => $token, "uid" => Uc_Com_Utils::encodeUserId($uid)); $res = Utils::httpRequest(AppConf::$noticeApp, 'POST', $params); setcookie(AppConf::$cookiePrefix . '_notice_url', "", time() - 3600); setcookie(AppConf::$cookiePrefix . '_api_url', "", time() - 3600); setcookie(AppConf::$cookiePrefix . '_token', "", time() - 3600); KC_LOG_DEBUG("notice succ [ url:" . AppConf::$noticeApp . ", params:" . json_encode($params) . " , res: {$res} ]."); } }
/** * 设置key的信息 * * @param string $key 要设置的key * @param string $value 要设置的value * @param string $timeout 超时时间 * @throws Exception */ public function setEx($key, $value, $timeout = null) { $retry = 0; $ret = false; do { $this->getConnection($retry !== 0); try { if (!is_null($timeout)) { $ret = $this->_redisObj->setex($key, $timeout, $value); } else { $ret = $this->_redisObj->setex($key, $value); } } catch (Exception $ex) { KC_LOG_WARNING('try to set key value to redis returned false [ retry: %s ].', $retry); sleep(1); $ret = false; continue; } break; } while ($retry++ < AppConf::$redisRetry); if (false === $ret) { throw new Exception('query redis to save all failed.'); } KC_LOG_DEBUG('function %s sycc [ key: %s, value: %s, timeout: %s ].', __FUNCTION__, $key, $value, strval($timeout)); }