Beispiel #1
0
 /**
  * Get the Zip File from Server & return back the downloaded file location
  */
 public static function zipFile($url, $zipFile)
 {
     if (!extension_loaded('zip')) {
         self::log("Dependency Missing, Please install PHP Zip Extension");
         echo ser("PHP Zip Extension", "I can't find the Zip PHP Extension. Please Install It & Try again");
     }
     self::log("Started Downloading Zip File from {$url} to {$zipFile}");
     $userAgent = 'LobbyBot/0.1 (' . L_SERVER . ')';
     /**
      * Get The Zip From Server
      */
     $hooks = new \Requests_Hooks();
     if (self::$progress != null) {
         $progress = self::$progress;
         $hooks->register('curl.before_send', function ($ch) use($progress) {
             curl_setopt($ch, CURLOPT_NOPROGRESS, false);
             curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, $progress);
         });
     }
     try {
         \Requests::get($url, array("User-Agent" => $userAgent), array('filename' => $zipFile, 'hooks' => $hooks, 'timeout' => time()));
     } catch (\Requests_Exception $error) {
         self::log("HTTP Requests Error ({$url}) : {$error}");
         echo ser("Error", "HTTP Requests Error : " . $error);
         return false;
     }
     self::log("Downloaded Zip File from {$url} to {$zipFile}");
     return $zipFile;
 }
Beispiel #2
0
 /**
  * Register the necessary callbacks
  *
  * @since 1.6
  * @see curl_before_send
  * @see fsockopen_remote_socket
  * @see fsockopen_remote_host_path
  * @see fsockopen_header
  * @param Requests_Hooks $hooks Hook system
  */
 public function register(Requests_Hooks &$hooks)
 {
     $hooks->register('curl.before_send', array(&$this, 'curl_before_send'));
     $hooks->register('fsockopen.remote_socket', array(&$this, 'fsockopen_remote_socket'));
     $hooks->register('fsockopen.remote_host_path', array(&$this, 'fsockopen_remote_host_path'));
     if ($this->use_authentication) {
         $hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header'));
     }
 }
Beispiel #3
0
 /**
  * Collect data as it's received
  *
  * @since 1.6.1
  *
  * @param resource $handle cURL resource
  * @param string $data Body data
  * @return integer Length of provided data
  */
 protected function stream_body($handle, $data)
 {
     $this->hooks->dispatch('request.progress', array($data, $this->response_bytes, $this->response_byte_limit));
     $data_length = strlen($data);
     // Are we limiting the response size?
     if ($this->response_byte_limit) {
         if ($this->response_bytes === $this->response_byte_limit) {
             // Already at maximum, move on
             return $data_length;
         }
         if ($this->response_bytes + $data_length > $this->response_byte_limit) {
             // Limit the length
             $limited_length = $this->response_byte_limit - $this->response_bytes;
             $data = substr($data, 0, $limited_length);
         }
     }
     if ($this->stream_handle) {
         fwrite($this->stream_handle, $data);
     } else {
         $this->response_data .= $data;
     }
     $this->response_bytes += strlen($data);
     return $data_length;
 }
 /**
  * Dispatch a Requests hook to a native WordPress action.
  *
  * @param string $hook Hook name.
  * @param array $parameters Parameters to pass to callbacks.
  * @return boolean True if hooks were run, false if nothing was hooked.
  */
 public function dispatch($hook, $parameters = array())
 {
     $result = parent::dispatch($hook, $parameters);
     // Handle back-compat actions
     switch ($hook) {
         case 'curl.before_send':
             /** This action is documented in wp-includes/class-wp-http-curl.php */
             do_action_ref_array('http_api_curl', array($parameters[0], $this->request, $this->url));
             break;
     }
     /**
      * Transforms a native Request hook to a WordPress actions.
      *
      * This action maps Requests internal hook to a native WordPress action.
      *
      * @see https://github.com/rmccue/Requests/blob/master/docs/hooks.md
      *
      * @param array $parameters Parameters from Requests internal hook.
      * @param array $request Request data in WP_Http format.
      * @param string $url URL to request.
      */
     do_action_ref_array("requests-{$hook}", $parameters, $this->request, $this->url);
     return $result;
 }
Beispiel #5
0
 public function register(\Requests_Hooks &$hooks)
 {
     $hooks->register('requests.before_request', array(&$this, 'before_request'));
 }
Beispiel #6
0
 /**
  * Register the necessary callbacks
  *
  * @see curl_before_send
  * @see fsockopen_header
  * @param Requests_Hooks $hooks Hook system
  */
 public function register(Requests_Hooks &$hooks)
 {
     $hooks->register('curl.before_send', array(&$this, 'curl_before_send'));
     $hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header'));
 }
Beispiel #7
0
 public function testProgressCallback()
 {
     $mock = $this->getMockBuilder('stdClass')->setMethods(array('progress'))->getMock();
     $mock->expects($this->atLeastOnce())->method('progress');
     $hooks = new Requests_Hooks();
     $hooks->register('request.progress', array($mock, 'progress'));
     $options = array('hooks' => $hooks);
     $options = $this->getOptions($options);
     $response = Requests::get(httpbin('/get'), array(), $options);
 }
Beispiel #8
0
    /**
     * POST a file to VWflow's items-hmac endpoint.
     *
     * If the request is valid, a new item will be created.
     *
     * @param string $local_path Local path to the file that needs to be uploaded.
     * @param string $account_id ID of your VWflow account.
     * @param string $wprofile_id ID of your VWflow workflow profile.
     * @param string $auth_secret Shared secret attached to your workflow profile.
     * @param int $hmac_valid_seconds Number of seconds during which the HMAC will be considered as valid by VWflow (default = 30).
     * @param string $client_data Client specific data in JSON format (should be valid json, use json_encode() to generate it !!),
     *          which can be retrieved later as part of a notification or as the result of polling the job resource (optional).
     * @return JSON object containing the URL(s) required to retrieve the item's data through the VWflow items API, or containing an error
     */
    function createItemWithHmacAuth($local_path, $account_id, $wprofile_id, $auth_secret, $hmac_valid_seconds = 30, $client_data = "")
    {
        $a_item = array("src" => "@{$local_path}");
        $url = $this->uri . "items-hmac/{$account_id}/{$wprofile_id}/";
        $msg_data = uniqid(rand(), true);
        # generate unique id
        date_default_timezone_set('Europe/Brussels');
        $msg_timestamp = time() + $hmac_valid_seconds;
        # requests using this page will be valid during three hours
        $vwflow_hmac = md5($auth_secret . $msg_timestamp . $msg_data);
        $vwflow_info = <<<EOT
{"msg_data":"{$msg_data}","msg_timestamp":"{$msg_timestamp}","client_data":"{$client_data}"}
EOT;
        $headers = array('Content-Type' => 'multipart/form-data', 'x-vwflow-info' => $vwflow_info, "x-vwflow-hmac" => $vwflow_hmac);
        $hooks = new Requests_Hooks();
        $hooks->register('curl.before_send', function ($fp, $data = array()) use($a_item) {
            curl_setopt($fp, CURLOPT_POSTFIELDS, $a_item);
            curl_setopt($fp, CURLOPT_TIMEOUT, $this->upload_timeout);
        });
        $options = $this->request_options;
        $options['hooks'] = $hooks;
        $response = Requests::post($url, $headers, array(), $options);
        $this->_checkErrorResponse($response);
        return json_decode($response->body);
    }
Beispiel #9
0
 protected function _startDownloads()
 {
     $this->log('start downloads');
     $hooks = new \Requests_Hooks();
     $hooks->register('curl.before_send', array($this, 'setCurlCallbackFunction'));
     foreach ($this->_downloadList as $id => $url) {
         $this->log('download %s', $url);
         $targetFile = $this->_getTargetFilePath($id, $url);
         $options = array('filename' => $targetFile, 'hooks' => $hooks, 'timeout' => pow(2, 64));
         $this->_currentDownloadId = $id;
         \Requests::get($url, array(), $options);
         $this->log('finished downloading %s', $url);
     }
     $this->log('finished all downloading going to die now');
 }
Beispiel #10
0
 public function testAfterRequestCallback()
 {
     $mock = $this->getMockBuilder('stdClass')->setMethods(array('after_request'))->getMock();
     $mock->expects($this->atLeastOnce())->method('after_request')->with($this->isType('string'), $this->logicalAnd($this->isType('array'), $this->logicalNot($this->isEmpty())));
     $hooks = new Requests_Hooks();
     $hooks->register('curl.after_request', array($mock, 'after_request'));
     $hooks->register('fsockopen.after_request', array($mock, 'after_request'));
     $options = array('hooks' => $hooks);
     $options = $this->getOptions($options);
     $response = Requests::get(httpbin('/get'), array(), $options);
 }
 /**
  * @param \Requests_Hooks $hooks
  */
 public function register(\Requests_Hooks &$hooks)
 {
     $hooks->register('requests.before_request', [&$this, 'before_request']);
     $hooks->register('requests.after_request', [&$this, 'after_request']);
 }
Beispiel #12
0
 public function register(Requests_Hooks &$hooks)
 {
     $hooks->register('requests.before_request', array($this, 'add_headers'), 1000);
 }