예제 #1
0
 /**
  * Makes a request to the URL by using the preferred method
  * @param  string $uri URL to call
  * @param  array $options An array of parameters for both `curl` and `fopen`
  * @param  callable $callback Either a function or callback in array notation
  * @return string The response of the request
  */
 public static function get($uri = '', $options = [], $callback = null)
 {
     if (!self::isCurlAvailable() && !self::isFopenAvailable()) {
         throw new \RuntimeException('Could not start an HTTP request. `allow_url_open` is disabled and `cURL` is not available');
     }
     // disable time limit if possible to help with slow downloads
     if (!Utils::isFunctionDisabled('set_time_limit') && !ini_get('safe_mode')) {
         set_time_limit(0);
     }
     $options = array_replace_recursive(self::$defaults, $options);
     $method = 'get' . ucfirst(strtolower(self::$method));
     self::$callback = $callback;
     return static::$method($uri, $options, $callback);
 }
예제 #2
0
파일: Response.php 프로젝트: dweelie/grav
 /**
  * Makes a request to the URL by using the preferred method
  *
  * @param  string   $uri      URL to call
  * @param  array    $options  An array of parameters for both `curl` and `fopen`
  * @param  callable $callback Either a function or callback in array notation
  *
  * @return string The response of the request
  */
 public static function get($uri = '', $options = [], $callback = null)
 {
     if (!self::isCurlAvailable() && !self::isFopenAvailable()) {
         throw new \RuntimeException('Could not start an HTTP request. `allow_url_open` is disabled and `cURL` is not available');
     }
     // check if this function is available, if so use it to stop any timeouts
     try {
         if (!Utils::isFunctionDisabled('set_time_limit') && !ini_get('safe_mode') && function_exists('set_time_limit')) {
             set_time_limit(0);
         }
     } catch (\Exception $e) {
     }
     $options = array_replace_recursive(self::$defaults, $options);
     $method = 'get' . ucfirst(strtolower(self::$method));
     self::$callback = $callback;
     return static::$method($uri, $options, $callback);
 }
예제 #3
0
 /**
  * Makes a request to the URL by using the preferred method
  *
  * @param  string   $uri      URL to call
  * @param  array    $options  An array of parameters for both `curl` and `fopen`
  * @param  callable $callback Either a function or callback in array notation
  *
  * @return string The response of the request
  */
 public static function get($uri = '', $options = [], $callback = null)
 {
     if (!self::isCurlAvailable() && !self::isFopenAvailable()) {
         throw new \RuntimeException('Could not start an HTTP request. `allow_url_open` is disabled and `cURL` is not available');
     }
     // check if this function is available, if so use it to stop any timeouts
     try {
         if (!Utils::isFunctionDisabled('set_time_limit') && !ini_get('safe_mode') && function_exists('set_time_limit')) {
             set_time_limit(0);
         }
     } catch (\Exception $e) {
     }
     $config = Grav::instance()['config'];
     $overrides = [];
     // SSL Verify Peer and Proxy Setting
     $settings = ['method' => $config->get('system.gpm.method', self::$method), 'verify_peer' => $config->get('system.gpm.verify_peer', true), 'proxy_url' => $config->get('system.gpm.proxy_url', $config->get('system.proxy_url', false))];
     if (!$settings['verify_peer']) {
         $overrides = array_replace_recursive([], $overrides, ['curl' => [CURLOPT_SSL_VERIFYPEER => $settings['verify_peer']], 'fopen' => ['ssl' => ['verify_peer' => $settings['verify_peer'], 'verify_peer_name' => $settings['verify_peer']]]]);
     }
     // Proxy Setting
     if ($settings['proxy_url']) {
         $proxy = parse_url($settings['proxy_url']);
         $fopen_proxy = ($proxy['scheme'] ?: 'http') . '://' . $proxy['host'] . (isset($proxy['port']) ? ':' . $proxy['port'] : '');
         $overrides = array_replace_recursive([], $overrides, ['curl' => [CURLOPT_PROXY => $proxy['host'], CURLOPT_PROXYTYPE => 'HTTP'], 'fopen' => ['proxy' => $fopen_proxy, 'request_fulluri' => true]]);
         if (isset($proxy['port'])) {
             $overrides['curl'][CURLOPT_PROXYPORT] = $proxy['port'];
         }
         if (isset($proxy['user']) && isset($proxy['pass'])) {
             $fopen_auth = $auth = base64_encode($proxy['user'] . ':' . $proxy['pass']);
             $overrides['curl'][CURLOPT_PROXYUSERPWD] = $proxy['user'] . ':' . $proxy['pass'];
             $overrides['fopen']['header'] = "Proxy-Authorization: Basic {$fopen_auth}";
         }
     }
     $options = array_replace_recursive(self::$defaults, $options, $overrides);
     $method = 'get' . ucfirst(strtolower($settings['method']));
     self::$callback = $callback;
     return static::$method($uri, $options, $callback);
 }
예제 #4
0
파일: Utils.php 프로젝트: khanduras/grav
 /**
  * Provides the ability to download a file to the browser
  *
  * @param      $file            the full path to the file to be downloaded
  * @param bool $force_download  as opposed to letting browser choose if to download or render
  */
 public static function download($file, $force_download = true)
 {
     if (file_exists($file)) {
         // fire download event
         self::getGrav()->fireEvent('onBeforeDownload', new Event(['file' => $file]));
         $file_parts = pathinfo($file);
         $filesize = filesize($file);
         // check if this function is available, if so use it to stop any timeouts
         try {
             if (!Utils::isFunctionDisabled('set_time_limit') && !ini_get('safe_mode') && function_exists('set_time_limit')) {
                 set_time_limit(0);
             }
         } catch (\Exception $e) {
         }
         ignore_user_abort(false);
         if ($force_download) {
             header('Content-Description: File Transfer');
             header('Content-Type: application/octet-stream');
             header('Content-Disposition: attachment; filename=' . $file_parts['basename']);
             header('Content-Transfer-Encoding: binary');
             header('Expires: 0');
             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
             header('Pragma: public');
         } else {
             header("Content-Type: " . Utils::getMimeType($file_parts['extension']));
         }
         header('Content-Length: ' . $filesize);
         // 8kb chunks for now
         $chunk = 8 * 1024;
         $fh = fopen($file, "rb");
         if ($fh === false) {
             return;
         }
         // Repeat reading until EOF
         while (!feof($fh)) {
             echo fread($fh, $chunk);
             ob_flush();
             // flush output
             flush();
         }
         exit;
     }
 }
예제 #5
0
파일: UtilsTest.php 프로젝트: getgrav/grav
 public function testIsFunctionDisabled()
 {
     $disabledFunctions = explode(',', ini_get('disable_functions'));
     if ($disabledFunctions[0]) {
         $this->assertEquals(Utils::isFunctionDisabled($disabledFunctions[0]), true);
     }
 }