Exemple #1
0
 /**
  * 生成带token的路径
  *
  * @param $path
  * @return string
  */
 public function getTokenUrl($path)
 {
     $parts = parse_url($path);
     $params = array();
     if (!empty($parts['query'])) {
         parse_str($parts['query'], $params);
     }
     $params['_'] = $this->getToken($this->request->getRequestUrl());
     $parts['query'] = http_build_query($params);
     return Typecho_Common::buildUrl($parts);
 }
Exemple #2
0
 /**
  * 发送请求
  *
  * @access public
  * @param string $url 请求地址
  * @return string
  * @throws Typecho_Http_Client_Exception
  * @internal param string $rfc 请求协议
  */
 public function send($url)
 {
     $params = parse_url($url);
     if (!empty($params['host'])) {
         $this->host = $params['host'];
     } else {
         throw new Typecho_Http_Client_Exception('Unknown Host', 500);
     }
     if (!empty($params['path'])) {
         $this->path = $params['path'];
     }
     $query = empty($params['query']) ? '' : $params['query'];
     if (!empty($this->query)) {
         $query = empty($query) ? $this->query : '&' . $this->query;
     }
     if (!empty($query)) {
         $this->path .= '?' . $query;
         $params['query'] = $query;
     }
     $this->scheme = $params['scheme'];
     $this->port = 'https' == $params['scheme'] ? 443 : 80;
     $url = Typecho_Common::buildUrl($params);
     if (!empty($params['port'])) {
         $this->port = $params['port'];
     }
     /** 整理cookie */
     if (!empty($this->cookies)) {
         $this->setHeader('Cookie', str_replace('&', '; ', http_build_query($this->cookies)));
     }
     $response = $this->httpSend($url);
     if (!$response) {
         return;
     }
     str_replace("\r", '', $response);
     $rows = explode("\n", $response);
     $foundStatus = false;
     $foundInfo = false;
     $lines = array();
     foreach ($rows as $key => $line) {
         if (!$foundStatus) {
             if (0 === strpos($line, "HTTP/")) {
                 if ('' == trim($rows[$key + 1])) {
                     continue;
                 } else {
                     $status = explode(' ', str_replace('  ', ' ', $line));
                     $this->responseStatus = intval($status[1]);
                     $foundStatus = true;
                 }
             }
         } else {
             if (!$foundInfo) {
                 if ('' != trim($line)) {
                     $status = explode(':', $line);
                     $name = strtolower(array_shift($status));
                     $data = implode(':', $status);
                     $this->responseHeader[trim($name)] = trim($data);
                 } else {
                     $foundInfo = true;
                 }
             } else {
                 $lines[] = $line;
             }
         }
     }
     $this->responseBody = implode("\n", $lines);
     return $this->responseBody;
 }
Exemple #3
0
 /**
  * 发送pingback实现
  *
  * @access public
  * @return void
  */
 public function sendPingHandle()
 {
     /** 验证权限 */
     $this->user->pass('contributor');
     /** 忽略超时 */
     ignore_user_abort(true);
     /** 获取post */
     $post = $this->widget('Widget_Archive', "type=post", "cid={$this->request->cid}");
     if ($post->have() && preg_match_all("|<a[^>]*href=[\"'](.*?)[\"'][^>]*>(.*?)</a>|", $post->text, $matches)) {
         $links = array_unique($matches[1]);
         $permalinkPart = parse_url($post->permalink);
         /** 发送pingback */
         foreach ($links as $url) {
             $urlPart = parse_url($url);
             if (isset($urlPart['scheme'])) {
                 if ('http' != $urlPart['scheme'] || 'https' != $urlPart['scheme']) {
                     continue;
                 }
             } else {
                 $urlPart['scheme'] = 'http';
                 $url = Typecho_Common::buildUrl($urlPart);
             }
             if ($permalinkPart['host'] == $urlPart['host'] && $permalinkPart['path'] == $urlPart['path']) {
                 continue;
             }
             $spider = Typecho_Http_Client::get();
             if ($spider) {
                 $spider->setTimeout(10)->send($url);
                 if (!($xmlrpcUrl = $spider->getResponseHeader('x-pingback'))) {
                     if (preg_match("/<link[^>]*rel=[\"']pingback[\"'][^>]*href=[\"']([^\"']+)[\"'][^>]*>/i", $spider->getResponseBody(), $out)) {
                         $xmlrpcUrl = $out[1];
                     }
                 }
                 if (!empty($xmlrpcUrl)) {
                     try {
                         $xmlrpc = new IXR_Client($xmlrpcUrl);
                         $xmlrpc->pingback->ping($post->permalink, $url);
                         unset($xmlrpc);
                     } catch (Exception $e) {
                         continue;
                     }
                 }
             }
             unset($spider);
         }
     }
     /** 发送trackback */
     if ($post->have() && !empty($this->request->trackback)) {
         $links = $this->request->trackback;
         foreach ($links as $url) {
             $client = Typecho_Http_Client::get();
             if ($client) {
                 try {
                     $client->setTimeout(5)->setData(array('blog_name' => $this->options->title . ' &raquo ' . $post->title, 'url' => $post->permalink, 'excerpt' => $post->excerpt))->send($url);
                     unset($client);
                 } catch (Typecho_Http_Client_Exception $e) {
                     continue;
                 }
             }
         }
     }
 }
Exemple #4
0
 /**
  * 返回来路
  *
  * @access public
  * @param string $anchor 附加地址
  * @param string $default 默认来路
  * @return void
  */
 public function goBack($suffix = NULL, $default = NULL)
 {
     //获取来源
     $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
     //判断来源
     if (!empty($referer)) {
         // ~ fix Issue 38
         if (!empty($suffix)) {
             $parts = parse_url($referer);
             $myParts = parse_url($suffix);
             if (isset($myParts['fragment'])) {
                 $parts['fragment'] = $myParts['fragment'];
             }
             if (isset($myParts['query'])) {
                 $args = array();
                 if (isset($parts['query'])) {
                     parse_str($parts['query'], $args);
                 }
                 parse_str($myParts['query'], $currentArgs);
                 $args = array_merge($args, $currentArgs);
                 $parts['query'] = http_build_query($args);
             }
             $referer = Typecho_Common::buildUrl($parts);
         }
         $this->redirect($referer, false);
     } else {
         if (!empty($default)) {
             $this->redirect($default);
         }
     }
 }
Exemple #5
0
 /**
  * 根据当前uri构造指定参数的uri
  *
  * @access public
  * @param mixed $parameter 指定的参数
  * @return string
  */
 public function makeUriByRequest($parameter = NULL)
 {
     /** 初始化地址 */
     $requestUri = $this->getRequestUrl();
     $parts = parse_url($requestUri);
     /** 初始化参数 */
     if (is_string($parameter)) {
         parse_str($parameter, $args);
     } else {
         if (is_array($parameter)) {
             $args = $parameter;
         } else {
             return $requestUri;
         }
     }
     /** 构造query */
     if (isset($parts['query'])) {
         parse_str($parts['query'], $currentArgs);
         $args = array_merge($currentArgs, $args);
     }
     $parts['query'] = http_build_query($args);
     /** 返回地址 */
     return Typecho_Common::buildUrl($parts);
 }
Exemple #6
0
 /**
  * 客户端构造函数
  *
  * @access public
  * @param string $server 服务端地址
  * @param bool|string $path 路径名称
  * @param integer $port 端口名称
  * @param string $useragent 客户端
  * @param null $prefix
  */
 public function __construct($server, $path = false, $port = 80, $useragent = self::DEFAULT_USERAGENT, $prefix = NULL)
 {
     if (!$path) {
         $this->url = $server;
         // Assume we have been given a Url instead
         $bits = parse_url($server);
         $this->server = $bits['host'];
         $this->port = isset($bits['port']) ? $bits['port'] : 80;
         $this->path = isset($bits['path']) ? $bits['path'] : '/';
         // Make absolutely sure we have a path
         if (isset($bits['query'])) {
             $this->path .= '?' . $bits['query'];
         }
     } else {
         /** Typecho_Common */
         require_once 'Typecho/Common.php';
         $this->url = Typecho_Common::buildUrl(array('scheme' => 'http', 'host' => $server, 'path' => $path, 'port' => $port));
         $this->server = $server;
         $this->path = $path;
         $this->port = $port;
     }
     $this->prefix = $prefix;
     $this->useragent = $useragent;
 }
Exemple #7
0
 /**
  * 根据当前uri构造指定参数的uri
  *
  * @access public
  * @param mixed $parameter 指定的参数
  * @return string
  */
 public function makeUriByRequest($parameter = NULL)
 {
     /** 初始化地址 */
     list($scheme) = explode('/', $_SERVER["SERVER_PROTOCOL"]);
     $requestUri = strtolower($scheme) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
     $parts = parse_url($requestUri);
     /** 初始化参数 */
     if (is_string($parameter)) {
         parse_str($parameter, $args);
     } else {
         if (is_array($parameter)) {
             $args = $parameter;
         } else {
             return $requestUri;
         }
     }
     /** 构造query */
     if (isset($parts['query'])) {
         parse_str($parts['query'], $currentArgs);
         $args = array_merge($currentArgs, $args);
     }
     $parts['query'] = http_build_query($args);
     /** Typecho_Common */
     require_once 'Typecho/Common.php';
     /** 返回地址 */
     return Typecho_Common::buildUrl($parts);
 }