Author: Tianfeng.Han
Example #1
0
 function get($params = null, $cache_id = '')
 {
     $url = $this->config['url'];
     if ($params) {
         if (Tool::endchar($url) == '&') {
             $url .= http_build_query($params);
         } else {
             $url .= '?' . http_build_query($params);
         }
     }
     if (!empty($this->config['cache'])) {
         if (empty($cache_id)) {
             $cache_id = md5($url);
         }
         $cache_key = self::CACHE_KEY_PREFIX . $cache_id;
         $result = \Swoole::$php->cache->get($cache_key);
         if ($result) {
             return $result;
         }
     }
     $result = $this->_curl->get($url);
     if ($result and $this->_curl->info['http_code'] == 200) {
         if (!empty($this->config['json'])) {
             $result = json_decode($result, true);
         } elseif (!empty($this->config['serialize'])) {
             $result = unserialize($result);
         }
         if (!empty($this->config['cache'])) {
             \Swoole::$php->cache->set($cache_key, $result, $this->config['lifetime']);
         }
     }
     $this->info = $this->_curl->info;
     return $result;
 }
Example #2
0
 protected function getDB($type = self::DB_SLAVE)
 {
     if ($this->forceMaster) {
         goto master;
     }
     //只读的语句
     if ($type == self::DB_SLAVE) {
         if (empty($this->slaveDB)) {
             //连接到从库
             $config = $this->config;
             //从从库中随机选取一个
             $server = Swoole\Tool::getServer($config['slaves']);
             unset($config['slaves'], $config['use_proxy']);
             $config['host'] = $server['host'];
             $config['port'] = $server['port'];
             $this->slaveDB = $this->connect($config);
         }
         return $this->slaveDB;
     } else {
         master:
         if (empty($this->masterDB)) {
             //连接到主库
             $config = $this->config;
             unset($config['slaves'], $config['use_proxy']);
             $this->masterDB = $this->connect($config);
         }
         return $this->masterDB;
     }
 }
Example #3
0
 function scan($prefix)
 {
     $dir = $this->baseDir . '/' . str_replace(self::$separator, '/', trim($prefix, self::$separator));
     if (!is_dir($dir)) {
         return false;
     }
     return Tool::scandir($dir);
 }
Example #4
0
 function category()
 {
     if (empty($_GET['cid'])) {
         return "Access Deny";
     }
     //Error::dbd();
     $this->common();
     $tplname = strtolower($this->app) . '_list.html';
     $cate_id = (int) $_GET['cid'];
     $cate = getCategory($cate_id);
     if (empty($cate)) {
         Swoole_js::js_back('不存在的分类!', '/index.php');
         exit;
     }
     if (!empty($cate['uptime']) and Swoole\Tool::httpExpire($cate['uptime']) === false) {
         exit;
     }
     if ($cate['fid'] == 0) {
         $this->swoole->tpl->assign("fid", $cate_id);
         $this->swoole->tpl->assign("ccate", $cate);
         if ($cate['tplname']) {
             $tplname = $cate['tplname'];
         }
         $gets['fid'] = $cate_id;
     } else {
         if ($cate['tplname']) {
             $tplname = $cate['tplname'];
         }
         $this->swoole->tpl->assign("cate", $cate);
         $ccate = $this->swoole->db->query("select * from st_catelog where id={$cate['fid']} limit 1")->fetch();
         $this->swoole->tpl->assign("ccate", $ccate);
         $gets['cid'] = $cate_id;
     }
     $pager = null;
     $gets['order'] = 'addtime desc';
     $gets['page'] = empty($_GET['page']) ? 1 : (int) $_GET['page'];
     $gets['pagesize'] = empty($this->_model->pagesize) ? $this->swoole->config['cms']['pagesize'] : $this->_model->pagesize;
     $gets['select'] = "id,title,addtime";
     $list = $this->_model->gets($gets, $pager);
     if ($this->swoole->config['cms']['html_static']) {
         $pager->page_tpl = WEBROOT . "/{$this->app}/list_{$cate_id}_%s.html";
     }
     $pager = array('total' => $pager->total, 'render' => $pager->render());
     $this->swoole->tpl->assign('pager', $pager);
     $this->swoole->tpl->assign("list", $list);
     $this->swoole->tpl->assign('cid', $cate_id);
     $this->swoole->tpl->display($tplname);
 }
Example #5
0
 function call($param, $post = null)
 {
     foreach ($param as &$m) {
         if (is_array($m) or is_object($m)) {
             $m = serialize($m);
         }
     }
     $url = $this->server_url . \Swoole\Tool::combine_query($param);
     if ($post === null) {
         $res = $this->http->get($url);
     } else {
         $res = $this->http->post($url, $post);
     }
     if ($this->debug) {
         echo $url, BL, $res;
     }
     return json_decode($res);
 }
Example #6
0
 function comment()
 {
     \Swoole::$php->session->start();
     if (!$_SESSION['isLogin']) {
         return 'nologin';
     }
     $uid = $_SESSION['user_id'];
     $post['aid'] = (int) $_POST['aid'];
     $post['app'] = $_POST['app'];
     $post['content'] = $_POST['content'];
     $post['uid'] = $uid;
     $post['uname'] = $_SESSION['user']['nickname'];
     if ($post['app'] === 'mblog') {
         $m = createModel('MicroBlog');
         $entity = $m->get($post['aid']);
         $entity->reply_count++;
         $entity->save();
         if ($entity->uid != $uid) {
             App\Api::sendmail($entity->uid, $uid, "【系统】{$post['uname']}评论了你的微博", $post['content']);
         }
     } elseif ($post['app'] === 'blog') {
         $m = createModel('UserLogs');
         $entity = $m->get($post['aid']);
         $entity->reply_count++;
         $entity->save();
         if ($entity->uid != $uid) {
             App\Api::sendmail($entity->uid, $uid, "【系统】{$post['uname']}评论了你的日志.({$entity['title']})", $post['content']);
         }
     }
     createModel('UserComment')->put($post);
     $return = array('id' => $_SESSION['user']['id'], 'addtime' => Swoole\Tool::howLongAgo(date('Y-m-d H:i:s')), 'nickname' => $_SESSION['user']['nickname']);
     if (empty($_SESSION['user']['avatar'])) {
         $return['avatar'] = Swoole::$php->config['user']['default_avatar'];
     } else {
         $return['avatar'] = $_SESSION['user']['avatar'];
     }
     return $return;
 }
Example #7
0
 /**
  * 从配置中取出一个服务器配置
  * @return array
  * @throws \Exception
  */
 function getServer()
 {
     if (empty($this->servers)) {
         throw new \Exception("servers config empty.");
     }
     return Tool::getServer($this->servers);
 }