Exemple #1
0
 /**
  * 控制器参数设置的魔术方法
  * @param [type] $name  [description]
  * @param [type] $value [description]
  */
 public function __set($name, $value)
 {
     if (!isset($this->paramRule[$name])) {
         Tool_Log::error("unsupported controller param - {$name}");
         return;
     }
     do {
         $opt = $this->paramRule[$name];
         if (null === $value) {
             if (isset($opt['default'])) {
                 $value = $opt['default'];
                 break;
             }
             $msg = empty($opt['nulmsg']) ? "缺少参数:{$name}" : $opt['nulmsg'];
             $e = new Exception_Api($msg, CODE_PARAM_ERROR);
             $e->name = $name;
             throw $e;
         }
         if (!empty($opt['rule']) && !Comm_Checker::run($value, $opt['rule'])) {
             if (isset($opt['default'])) {
                 $value = $opt['default'];
                 break;
             }
             $msg = empty($opt['errmsg']) ? "参数错误:{$name}" : $opt['errmsg'];
             $e = new Exception_Api($msg, CODE_PARAM_ERROR);
             $e->name = $name;
             throw $e;
         }
     } while (false);
     $this->paramData[$name] = $value;
 }
Exemple #2
0
 /**
  * Memcached函数调用,方法列表及参数参见memcached扩展
  * @param  [type] $func [description]
  * @param  [type] $args [description]
  * @return [type]       [description]
  */
 public function __call($func, $args)
 {
     $res = call_user_func_array(array($this->mc, $func), $args);
     if (false === $res) {
         $info = array('func' => $func, 'args' => $args, 'code' => $this->mc->getResultCode(), 'msg' => $this->mc->getResultMessage());
         Tool_Log::error('mc __call failed - ' . Tool_Json::encode($info));
     }
     return $res;
 }
Exemple #3
0
 /**
  * 全局异常处理
  * 异常有两个来源
  * 1. 通过set_exception_handler设置的全局未捕获异常
  * 2. 通过Core_App::dispatch()捕获的异常
  * @param  [type] $e [description]
  * @return [type]    [description]
  */
 public function handleException($e)
 {
     $controller = Core_App::getInstance()->controllerObj;
     $isBaseExp = $e instanceof Exception_Base;
     $info = array('exp' => get_class($e), 'code' => $e->getCode(), 'msg' => $e->getMessage(), 'expParams' => $isBaseExp ? $e->getParams() : null, 'line' => sprintf('%s:%d', $e->getFile(), $e->getLine()), 'uri' => Comm_Context::getServer('REQUEST_URI'), 'elapse' => sprintf('%.3f', microtime(true) - SYSTEM_START_TIME), 'reqParams' => empty($controller) ? null : $controller->getParams());
     Tool_Log::error('request exception, ' . Tool_Json::encode($info));
     if ($isBaseExp) {
         // 异常自身处理过程
         $e->handle();
     } else {
         // 默认异常处理过程
         $a = new Exception_Base();
         $a->__real__ = $e;
         $a->handle();
     }
 }
Exemple #4
0
 public function delete()
 {
     $options = $this->_parseOptions();
     $data = array();
     $sql = "DELETE FROM {$options['table']} ";
     if (!empty($options['where'])) {
         $sql .= " WHERE {$options['where'][0]}";
         array_shift($options['where']);
         $data = array_merge($data, $options['where']);
     } else {
         // 没有删除条件,不做任何操作
         Tool_Log::error('model no delete condition!');
         return false;
     }
     empty($options['order']) || ($sql .= " ORDER BY {$options['order']}");
     empty($options['limit']) || ($sql .= " LIMIT {$options['limit']}");
     $res = $this->db->exec($sql, $data);
     $this->delCache($options['cache']);
     return $res;
 }
Exemple #5
0
 private function _logmsg($func, $args, $code, $msg)
 {
     $info = array('func' => $func, 'args' => $args, 'code' => $code, 'msg' => $msg);
     Tool_Log::error('redis failed - ' . Tool_Json::encode($info));
 }
Exemple #6
0
 private function _mainLoop()
 {
     Tool_Log::info("daemon:{$this->name} main start");
     Tool_Proc::setTitle("daemon:{$this->name} main");
     $pid = posix_getpid();
     while (true) {
         // 检查pid文件
         if (!file_exists($this->pidFile) || $pid != file_get_contents($this->pidFile)) {
             break;
         }
         // 检查子进程状态
         foreach ($this->children as $index => $child) {
             $msg = fgets($child['socket']);
             if (feof($child['socket'])) {
                 $this->_spawnChild($index);
             }
         }
         usleep(self::CHECK_INTERVAL * 1000 * 1000);
     }
     Tool_Log::info("daemon:{$this->name} main stop");
     exit;
 }
Exemple #7
0
 private function _logmsg($func, $args)
 {
     $info = array('func' => $func, 'args' => $args, 'code' => $this->mc->getResultCode(), 'msg' => $this->mc->getResultMessage());
     Tool_Log::error('memcached failed - ' . Tool_Json::encode($info));
 }
Exemple #8
0
 private function _doStop()
 {
     if (true !== $this->controllerObj->getLogRequest()) {
         return;
     }
     $info = array('elapse' => sprintf('%.3f', microtime(true) - SYSTEM_START_TIME), 'uri' => Comm_Context::getServer('REQUEST_URI'), 'params' => $this->controllerObj->getParams());
     Tool_Log::info('request ok, ' . Tool_Json::encode($info));
 }
 /**
  * 取验证码图片
  * @param $rcode: 随机数
  *        $length: 验证码长度 4-英文波浪4个 5-英文波浪5个加随机线条
  */
 public function getCaptchaImg($length, $rcode, $action = "")
 {
     $width = $this->width;
     $height = $this->height;
     $scale = 2;
     $captchaLevel = self::CAPTCHA_COMMON;
     if (strlen($action)) {
         $captchaLevel = Tool_Limit::getCaptchaLevel($action);
         if ($captchaLevel == self::CAPTCHA_ADVANCE) {
             $length = 5;
         }
     }
     //创建图像
     $im = imagecreatetruecolor($width * $scale, $height * $scale);
     //前景和背景色
     $forecolor = imagecolorallocate($im, $this->forecolor[0], $this->forecolor[1], $this->forecolor[2]);
     $backcolor = imagecolorallocate($im, $this->backcolor[0], $this->backcolor[1], $this->backcolor[2]);
     imagefilledrectangle($im, 0, 0, $width * $scale, $height * $scale, $backcolor);
     //画干扰折线
     if ($length == 5) {
         $this->drawBrokenLine($im, $width, $height, $scale, $forecolor);
     }
     //写验证码
     $text = $this->getCaptchaText($length);
     $this->writeEnhanceText($im, $text, $forecolor, $width, $height, $scale);
     //$this->waveImage($im, $width, $height, $scale);
     //压缩图
     $imResampled = imagecreatetruecolor($width, $height);
     imagecopyresampled($imResampled, $im, 0, 0, 0, 0, $width, $height, $width * $scale, $height * $scale);
     imagedestroy($im);
     $im = $imResampled;
     //缓存验证码
     $this->memcache->set($this->prefix . $rcode, strtolower($text), 600);
     $this->memcache->set($this->prefixLevel . $rcode, $captchaLevel, 600);
     Tool_Log::addDebugLog('captcha', strtolower($text));
     //输出图片
     header("Content-type: image/gif");
     imagegif($im, null, 80);
     imagedestroy($im);
     return true;
 }
Exemple #10
0
 private function _connect($name)
 {
     $tmp = explode(':', $name, 2);
     $host = empty($tmp[0]) ? '127.0.0.1' : $tmp[0];
     $port = empty($tmp[1]) ? 6379 : $tmp[1];
     $redis = new Redis();
     $res = $redis->connect($host, $port, self::REDIS_TIMEOUT);
     // 如果扩展支持,使用PHP自动序列化
     if (defined('Redis::OPT_SERIALIZER')) {
         $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
     }
     if (!$res) {
         Tool_Log::error("redis connect failed - {$host}:{$port}");
         return false;
     }
     $this->servers[$name] = $redis;
     return true;
 }