Пример #1
0
 /**
  * NV_Http::request()
  *
  * @param mixed $url
  * @param mixed $args
  * @return
  */
 private function request($url, $args)
 {
     $defaults = array('method' => 'GET', 'timeout' => 10, 'redirection' => 5, 'requested' => 0, 'httpversion' => 1.0, 'user-agent' => 'NUKEVIET CMS ' . $this->site_config['version'] . '. Developed by VINADES. Url: http://nukeviet.vn. Code: ' . md5($this->site_config['sitekey']), 'referer' => null, 'reject_unsafe_urls' => false, 'blocking' => true, 'headers' => array(), 'cookies' => array(), 'body' => null, 'compress' => false, 'decompress' => true, 'sslverify' => true, 'sslcertificates' => $this->root_dir . '/includes/certificates/ca-bundle.crt', 'stream' => false, 'filename' => null, 'limit_response_size' => null);
     // Get full args
     $args = $this->build_args($args, $defaults);
     // Get url info
     $infoURL = @parse_url($url);
     // Check valid url
     if (empty($url) or empty($infoURL['scheme'])) {
         $this->set_error(1);
         return false;
     }
     // Set SSL
     $args['ssl'] = $infoURL['scheme'] == 'https' or $infoURL['scheme'] == 'ssl';
     /**
      * Block url
      * By basic version, all url will be enabled and no blocking by check function
      */
     //if( $this->is_blocking( $url ) )
     //{
     //	$this->set_error(2);
     //	return false;
     //}
     // Determine if this request is to OUR install of NukeViet
     $homeURL = parse_url($this->site_config['my_domain']);
     $args['local'] = $homeURL['host'] == $infoURL['host'] || 'localhost' == $infoURL['host'];
     unset($homeURL);
     // If Stream but no file, default is a file in temp dir with base $url name
     if ($args['stream'] and empty($args['filename'])) {
         $args['filename'] = $this->tmp_dir . '/' . basename($url);
     }
     // Check if streaming a file
     if ($args['stream']) {
         $args['blocking'] = true;
         if (!@is_writable(dirname($args['filename']))) {
             $this->set_error(3);
             return false;
         }
     }
     // Default header is an empty array
     if (is_null($args['headers'])) {
         $args['headers'] = array();
     }
     if (!is_array($args['headers'])) {
         $processedHeaders = NV_Http::processHeaders($args['headers'], $url);
         $args['headers'] = $processedHeaders['headers'];
     }
     // Get User Agent
     if (isset($args['headers']['User-Agent'])) {
         $args['user-agent'] = $args['headers']['User-Agent'];
         unset($args['headers']['User-Agent']);
     }
     if (isset($args['headers']['user-agent'])) {
         $args['user-agent'] = $args['headers']['user-agent'];
         unset($args['headers']['user-agent']);
     }
     // Get Referer
     if (isset($args['headers']['Referer'])) {
         $args['referer'] = $args['headers']['Referer'];
         unset($args['headers']['Referer']);
     } elseif (isset($args['headers']['referer'])) {
         $args['referer'] = $args['headers']['referer'];
         unset($args['headers']['referer']);
     }
     if ($args['httpversion'] == '1.1' and !isset($args['headers']['connection'])) {
         $args['headers']['connection'] = 'close';
     }
     NV_Http::buildCookieHeader($args);
     NV_Http::mbstring_binary_safe_encoding();
     if (!isset($args['headers']['Accept-Encoding'])) {
         if ($encoding = NV_http_encoding::accept_encoding($url, $args)) {
             $args['headers']['Accept-Encoding'] = $encoding;
         }
     }
     if (!is_null($args['body']) and '' != $args['body'] or $args['method'] == 'POST' or $args['method'] == 'PUT') {
         if (is_array($args['body']) or is_object($args['body'])) {
             $args['body'] = http_build_query($args['body'], null, '&');
             if (!isset($args['headers']['Content-Type'])) {
                 $args['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . $this->site_config['site_charset'];
             }
         }
         if ($args['body'] === '') {
             $args['body'] = null;
         }
         if (!isset($args['headers']['Content-Length']) and !isset($args['headers']['content-length'])) {
             $args['headers']['Content-Length'] = strlen($args['body']);
         }
     }
     $response = $this->_dispatch_request($url, $args);
     NV_Http::reset_mbstring_encoding();
     if ($this->is_error($response)) {
         return $response;
     }
     // Append cookies that were used in this request to the response
     if (!empty($args['cookies'])) {
         $cookies_set = array();
         foreach ($response['cookies'] as $key => $value) {
             if (is_object($value)) {
                 $cookies_set[$key] = $value->name;
             } else {
                 $cookies_set[$key] = $value['name'];
             }
         }
         foreach ($args['cookies'] as $cookie) {
             if (!in_array($cookie->name, $cookies_set) and $cookie->test($url)) {
                 $response['cookies'][] = $cookie;
             }
         }
     }
     return $response;
 }