Exemple #1
0
 /**
  * Method call overload
  *
  * @param string $method Method name
  * @param array $args Method args
  * @return Zend_Rest_Client_Result|Zend_Rest_Client
  */
 public function __call($method, $args)
 {
     $methods = array('post', 'get', 'delete', 'put');
     if (in_array(strtolower($method), $methods)) {
         if (!isset($args[0])) {
             $args[0] = $this->_uri->getPath();
         }
         $this->_data['rest'] = 1;
         $response = $this->{'rest' . $method}($args[0], $this->_data);
         $sxml = new Zend_Rest_Client_Result($response->getBody());
         return $sxml;
     } else {
         if (sizeof($args) == 1) {
             // More than one arg means it's definitely a Zend_WebService_Rest_Server
             $this->_data[$method] = $args[0];
             $this->_data['arg1'] = $args[0];
         } else {
             $this->_data['method'] = $method;
             if (sizeof($args) > 0) {
                 foreach ($args as $key => $arg) {
                     $key = 'arg' . $key;
                     $this->_data[$key] = $arg;
                 }
             }
         }
         return $this;
     }
 }
Exemple #2
0
 /**
  * Method call overload
  *
  * Allows calling REST actions as object methods; however, you must
  * follow-up by chaining the request with a request to an HTTP request
  * method (post, get, delete, put):
  * <code>
  * $response = $rest->sayHello('Foo', 'Manchu')->get();
  * </code>
  *
  * Or use them together, but in sequential calls:
  * <code>
  * $rest->sayHello('Foo', 'Manchu');
  * $response = $rest->get();
  * </code>
  *
  * @param string $method Method name
  * @param array $args Method args
  * @return Zend_Rest_Client_Result|Zend_Rest_Client Zend_Rest_Client if using
  * a remote method, Zend_Rest_Client_Result if using an HTTP request method
  */
 public function __call($method, $args)
 {
     $methods = array('post', 'get', 'delete', 'put');
     if (in_array(strtolower($method), $methods)) {
         if (!isset($args[0])) {
             $args[0] = $this->_uri->getPath();
         }
         $this->_data['rest'] = 1;
         $data = array_slice($args, 1) + $this->_data;
         $response = $this->{'rest' . $method}($args[0], $data);
         $this->_data = array();
         //Initializes for next Rest method.
         return new Zend_Rest_Client_Result($response->getBody());
     } else {
         // More than one arg means it's definitely a Zend_Rest_Server
         if (sizeof($args) == 1) {
             // Uses first called function name as method name
             if (!isset($this->_data['method'])) {
                 $this->_data['method'] = $method;
                 $this->_data['arg1'] = $args[0];
             }
             $this->_data[$method] = $args[0];
         } else {
             $this->_data['method'] = $method;
             if (sizeof($args) > 0) {
                 foreach ($args as $key => $arg) {
                     $key = 'arg' . $key;
                     $this->_data[$key] = $arg;
                 }
             }
         }
         return $this;
     }
 }
    /**
     * Send request to the remote server with streaming support.
     *
     * @param string        $method
     * @param Zend_Uri_Http $uri
     * @param string        $http_ver
     * @param array         $headers
     * @param string        $body
     * @return string Request as string
     */
    public function write($method, $uri, $http_ver = '1.1', $headers = array(),
        $body = '')
    {
        // Make sure we're properly connected
        if (! $this->socket) {
            require_once 'Zend/Http/Client/Adapter/Exception.php';
            throw new Zend_Http_Client_Adapter_Exception(
                'Trying to write but we are not connected');
        }

        $host = $uri->getHost();
        $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
        if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
            require_once 'Zend/Http/Client/Adapter/Exception.php';
            throw new Zend_Http_Client_Adapter_Exception(
                'Trying to write but we are connected to the wrong host');
        }

        // Save request method for later
        $this->method = $method;

        // Build request headers
        $path = $uri->getPath();
        if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
        $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
        foreach ($headers as $k => $v) {
            if (is_string($k)) $v = ucfirst($k) . ": $v";
            $request .= "$v\r\n";
        }

        // Send the headers over
        $request .= "\r\n";
        if (! @fwrite($this->socket, $request)) {
            require_once 'Zend/Http/Client/Adapter/Exception.php';
            throw new Zend_Http_Client_Adapter_Exception(
                'Error writing request to server');
        }


        //read from $body, write to socket
        $chunk = $body->read(self::CHUNK_SIZE);
        while ($chunk !== FALSE) {
            if (! @fwrite($this->socket, $chunk)) {
                require_once 'Zend/Http/Client/Adapter/Exception.php';
                throw new Zend_Http_Client_Adapter_Exception(
                    'Error writing request to server');
            }
            $chunk = $body->read(self::CHUNK_SIZE);
        }
        $body->closeFileHandle();
        return 'Large upload, request is not cached.';
    }
 /**
  * Check if this request needs token
  * @return bool
  */
 protected function needsToken()
 {
     if (!empty($this->is_request)) {
         return false;
     }
     if (empty($this->requestPath)) {
         return true;
     }
     $GLOBALS['log']->debug("URLs: now: " . $this->url->getUri() . " req: {$this->requestPath}");
     if ($this->requestPath[0] == '/') {
         return $this->url->getPath() != $this->requestPath;
     }
     return $this->url->getUri() != $this->requestPath;
 }
 public function isMatch(Zend_Uri_Http $url)
 {
     if (preg_match($this->getRegularExpression(), $url->getPath(), $params)) {
         // Remove all integer indexed values.
         foreach ($params as $index => $value) {
             if (is_int($index)) {
                 unset($params[$index]);
             }
         }
         /**
          *	Zend_Uri_Http is lacking an getter for the query string as an array
          */
         $queryParams = array();
         if ($url->getQuery() !== false) {
             parse_str($url->getQuery(), $queryParams);
         }
         return array_merge($this->defaults, $queryParams, $params);
     }
     return false;
 }
 /**
  * Send the HTTP request and return an HTTP response object
  *
  * @param string $method
  * @return Zend_Http_Response
  * @throws Zend_Http_Client_Exception
  */
 public function request($method = null)
 {
     if (!$this->uri instanceof Zend_Uri_Http) {
         /** @see Zend_Http_Client_Exception */
         require_once 'Zend/Http/Client/Exception.php';
         throw new Zend_Http_Client_Exception('No valid URI has been passed to the client');
     }
     if ($method) {
         $this->setMethod($method);
     }
     $this->redirectCounter = 0;
     $response = null;
     // Make sure the adapter is loaded
     if ($this->adapter == null) {
         $this->setAdapter($this->config['adapter']);
     }
     // Send the first request. If redirected, continue.
     do {
         // Clone the URI and add the additional GET parameters to it
         $uri = clone $this->uri;
         if (!empty($this->paramsGet)) {
             $query = $uri->getQuery();
             if (!empty($query)) {
                 $query .= '&';
             }
             $query .= http_build_query($this->paramsGet, null, '&');
             if ($this->config['rfc3986_strict']) {
                 $query = str_replace('+', '%20', $query);
             }
             // @see ZF-11671 to unmask for some services to foo=val1&foo=val2
             if ($this->getUnmaskStatus()) {
                 if ($this->_queryBracketsEscaped) {
                     $query = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query);
                 } else {
                     $query = preg_replace('/\\[(?:[0-9]|[1-9][0-9]+)\\]=/', '=', $query);
                 }
             }
             $uri->setQuery($query);
         }
         $body = $this->_prepareBody();
         $headers = $this->_prepareHeaders();
         // check that adapter supports streaming before using it
         if (is_resource($body) && !$this->adapter instanceof Zend_Http_Client_Adapter_Stream) {
             /** @see Zend_Http_Client_Exception */
             require_once 'Zend/Http/Client/Exception.php';
             throw new Zend_Http_Client_Exception('Adapter does not support streaming');
         }
         // Open the connection, send the request and read the response
         $this->adapter->connect($uri->getHost(), $uri->getPort(), $uri->getScheme() == 'https' ? true : false);
         if ($this->config['output_stream']) {
             if ($this->adapter instanceof Zend_Http_Client_Adapter_Stream) {
                 $stream = $this->_openTempStream();
                 $this->adapter->setOutputStream($stream);
             } else {
                 /** @see Zend_Http_Client_Exception */
                 require_once 'Zend/Http/Client/Exception.php';
                 throw new Zend_Http_Client_Exception('Adapter does not support streaming');
             }
         }
         $this->last_request = $this->adapter->write($this->method, $uri, $this->config['httpversion'], $headers, $body);
         $response = $this->adapter->read();
         if (!$response) {
             /** @see Zend_Http_Client_Exception */
             require_once 'Zend/Http/Client/Exception.php';
             throw new Zend_Http_Client_Exception('Unable to read response, or response is empty');
         }
         if ($this->config['output_stream']) {
             $streamMetaData = stream_get_meta_data($stream);
             if ($streamMetaData['seekable']) {
                 rewind($stream);
             }
             // cleanup the adapter
             $this->adapter->setOutputStream(null);
             $response = Zend_Http_Response_Stream::fromStream($response, $stream);
             $response->setStreamName($this->_stream_name);
             if (!is_string($this->config['output_stream'])) {
                 // we used temp name, will need to clean up
                 $response->setCleanup(true);
             }
         } else {
             $response = Zend_Http_Response::fromString($response);
         }
         if ($this->config['storeresponse']) {
             $this->last_response = $response;
         }
         // Load cookies into cookie jar
         if (isset($this->cookiejar)) {
             $this->cookiejar->addCookiesFromResponse($response, $uri, $this->config['encodecookies']);
         }
         // If we got redirected, look for the Location header
         if ($response->isRedirect() && ($location = $response->getHeader('location'))) {
             // Avoid problems with buggy servers that add whitespace at the
             // end of some headers (See ZF-11283)
             $location = trim($location);
             // Check whether we send the exact same request again, or drop the parameters
             // and send a GET request
             if ($response->getStatus() == 303 || !$this->config['strictredirects'] && ($response->getStatus() == 302 || $response->getStatus() == 301)) {
                 $this->resetParameters();
                 $this->setMethod(self::GET);
             }
             // If we got a well formed absolute URI
             if (($scheme = substr($location, 0, 6)) && ($scheme == 'http:/' || $scheme == 'https:')) {
                 $this->setHeaders('host', null);
                 $this->setUri($location);
             } else {
                 // Split into path and query and set the query
                 if (strpos($location, '?') !== false) {
                     list($location, $query) = explode('?', $location, 2);
                 } else {
                     $query = '';
                 }
                 $this->uri->setQuery($query);
                 // Else, if we got just an absolute path, set it
                 if (strpos($location, '/') === 0) {
                     $this->uri->setPath($location);
                     // Else, assume we have a relative path
                 } else {
                     // Get the current path directory, removing any trailing slashes
                     $path = $this->uri->getPath();
                     $path = rtrim(substr($path, 0, strrpos($path, '/')), "/");
                     $this->uri->setPath($path . '/' . $location);
                 }
             }
             ++$this->redirectCounter;
         } else {
             // If we didn't get any location, stop redirecting
             break;
         }
     } while ($this->redirectCounter < $this->config['maxredirects']);
     return $response;
 }
Exemple #7
0
 /**
  * Send the HTTP request and return an HTTP response object
  *
  * @param string $method
  * @return Zend_Http_Response
  * @throws Zend_Http_Client_Exception
  */
 public function request($method = null)
 {
     if (!$this->uri instanceof Zend_Uri_Http) {
         /** @see Zend_Http_Client_Exception */
         require_once 'Zend/Http/Client/Exception.php';
         throw new Zend_Http_Client_Exception('No valid URI has been passed to the client');
     }
     if ($method) {
         $this->setMethod($method);
     }
     $this->redirectCounter = 0;
     $response = null;
     // Make sure the adapter is loaded
     if ($this->adapter == null) {
         $this->setAdapter($this->config['adapter']);
     }
     // Send the first request. If redirected, continue.
     do {
         // Clone the URI and add the additional GET parameters to it
         $uri = clone $this->uri;
         if (!empty($this->paramsGet)) {
             $query = $uri->getQuery();
             if (!empty($query)) {
                 $query .= '&';
             }
             $query .= http_build_query($this->paramsGet, null, '&');
             $uri->setQuery($query);
         }
         $body = $this->_prepareBody();
         $headers = $this->_prepareHeaders();
         // Open the connection, send the request and read the response
         $this->adapter->connect($uri->getHost(), $uri->getPort(), $uri->getScheme() == 'https' ? true : false);
         $this->last_request = $this->adapter->write($this->method, $uri, $this->config['httpversion'], $headers, $body);
         $response = $this->adapter->read();
         if (!$response) {
             /** @see Zend_Http_Client_Exception */
             require_once 'Zend/Http/Client/Exception.php';
             throw new Zend_Http_Client_Exception('Unable to read response, or response is empty');
         }
         $response = Zend_Http_Response::fromString($response);
         if ($this->config['storeresponse']) {
             $this->last_response = $response;
         }
         // Load cookies into cookie jar
         if (isset($this->cookiejar)) {
             $this->cookiejar->addCookiesFromResponse($response, $uri);
         }
         // If we got redirected, look for the Location header
         if ($response->isRedirect() && ($location = $response->getHeader('location'))) {
             // Check whether we send the exact same request again, or drop the parameters
             // and send a GET request
             if ($response->getStatus() == 303 || !$this->config['strictredirects'] && ($response->getStatus() == 302 || $response->getStatus() == 301)) {
                 $this->resetParameters();
                 $this->setMethod(self::GET);
             }
             // If we got a well formed absolute URI
             if (Zend_Uri_Http::check($location)) {
                 $this->setHeaders('host', null);
                 $this->setUri($location);
             } else {
                 // Split into path and query and set the query
                 if (strpos($location, '?') !== false) {
                     list($location, $query) = explode('?', $location, 2);
                 } else {
                     $query = '';
                 }
                 $this->uri->setQuery($query);
                 // Else, if we got just an absolute path, set it
                 if (strpos($location, '/') === 0) {
                     $this->uri->setPath($location);
                     // Else, assume we have a relative path
                 } else {
                     // Get the current path directory, removing any trailing slashes
                     $path = $this->uri->getPath();
                     $path = rtrim(substr($path, 0, strrpos($path, '/')), "/");
                     $this->uri->setPath($path . '/' . $location);
                 }
             }
             ++$this->redirectCounter;
         } else {
             // If we didn't get any location, stop redirecting
             break;
         }
     } while ($this->redirectCounter < $this->config['maxredirects']);
     return $response;
 }
Exemple #8
0
 /**
  * Send request to the remote server
  *
  * @param string        $method
  * @param Zend_Uri_Http $uri
  * @param string        $http_ver
  * @param array         $headers
  * @param string        $body
  * @return string Request as string
  */
 public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
 {
     $host = $uri->getHost();
     $host = strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host;
     // Build request headers
     $path = $uri->getPath();
     if ($uri->getQuery()) {
         $path .= '?' . $uri->getQuery();
     }
     $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
     foreach ($headers as $k => $v) {
         if (is_string($k)) {
             $v = ucfirst($k) . ": {$v}";
         }
         $request .= "{$v}\r\n";
     }
     // Add the request body
     $request .= "\r\n" . $body;
     // Do nothing - just return the request as string
     return $request;
 }
Exemple #9
0
 /**
  * Send request to the remote server
  *
  * @param string        $method
  * @param Zend_Uri_Http $uri
  * @param string        $http_ver
  * @param array         $headers
  * @param string        $body
  * @return string Request as string
  */
 public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
 {
     // Make sure we're properly connected
     if (!$this->socket) {
         require_once 'Zend/Http/Client/Adapter/Exception.php';
         throw new Zend_Http_Client_Adapter_Exception('Trying to write but we are not connected');
     }
     $host = $uri->getHost();
     $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
     if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
         require_once 'Zend/Http/Client/Adapter/Exception.php';
         throw new Zend_Http_Client_Adapter_Exception('Trying to write but we are connected to the wrong host');
     }
     // Save request method for later
     $this->method = $method;
     // Build request headers
     $path = $uri->getPath();
     if ($uri->getQuery()) {
         $path .= '?' . $uri->getQuery();
     }
     $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
     foreach ($headers as $k => $v) {
         if (is_string($k)) {
             $v = ucfirst($k) . ": {$v}";
         }
         $request .= "{$v}\r\n";
     }
     // Add the request body
     $request .= "\r\n" . $body;
     // Send the request
     if (!@fwrite($this->socket, $request)) {
         require_once 'Zend/Http/Client/Adapter/Exception.php';
         throw new Zend_Http_Client_Adapter_Exception('Error writing request to server');
     }
     return $request;
 }
 /**
  * Send request to the remote server
  *
  * @param string $method
  * @param Zend_Uri_Http $uri
  * @param float $http_ver
  * @param array  $headers
  * @param string $body
  * @return string Request as string
  */
 public function write($method, $uri, $http_ver = 1.1, $headers = array(), $body = '')
 {
     // Make sure we're properly connected
     if (!$this->socket) {
         throw Zend::exception('Zend_Http_Client_Adapter_Exception', "Trying to write but we are not connected");
     }
     $host = $uri->getHost();
     $host = strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host;
     if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
         throw Zend::exception('Zend_Http_Client_Adapter_Exception', "Trying to write but we are connected to the wrong host");
     }
     // Build request headers
     $path = $uri->getPath();
     if ($uri->getQuery()) {
         $path .= '?' . $uri->getQuery();
     }
     $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
     foreach ($headers as $k => $v) {
         if (is_string($k)) {
             $v = ucfirst($k) . ": {$v}";
         }
         $request .= "{$v}\r\n";
     }
     // Add the request body
     $request .= "\r\n" . $body;
     // Send the request
     if (!fwrite($this->socket, $request)) {
         throw Zend::exception('Zend_Http_Client_Adapter_Exception', "Error writing request to server");
     }
     return $request;
 }
 /**
  * Delete a cookie according to it's name and domain. If no name is specified,
  * all cookies from this domain will be cleared out.
  *
  * @param string|Zend_Uri_Http $domain
  * @param string $cookie_name 
  * @return boolean true if cookie was deleted.
  */
 public function deleteCookies($domain, $cookie_name = null)
 {
     $ret = false;
     $path = '/';
     if ($domain instanceof Zend_Uri_Http) {
         $path = dirname($domain->getPath());
         $domain = $domain->getHost();
     } elseif (is_string($domain) && Zend_Uri_Http::check($domain)) {
         $domain = Zend_Uri_Http::factory($domain);
         $path = dirname($domain->getPath());
         $domain = $domain->getHost();
     }
     // If we have a cookie's name, delete only this one
     if (isset($cookie_name) && isset($this->cookies[$domain][$path][$cookie_name])) {
         unset($this->cookies[$domain][$path][$cookie_name]);
         $ret = true;
         // If we only got a URI, clear all cookies matching this URI.
     } else {
         $cookies = $this->_matchPath($this->_matchDomain($domain), $path);
         foreach ($cookies as $cookie) {
             if (isset($this->cookies[$cookie->getDomain()][$cookie->getPath])) {
                 unset($this->cookies[$cookie->getDomain()][$cookie->getPath]);
                 $ret = true;
                 if (count($this->cookies[$cookie->getDomain()]) == 0) {
                     unset($this->cookies[$cookie->getDomain()]);
                 }
             }
         }
     }
     return $ret;
 }
Exemple #12
0
 /**
  * Send the HTTP request and return an HTTP response object
  *
  * @param string $method
  * @return Zend_Http_Response
  */
 public function request($method = null)
 {
     if (!$this->uri instanceof Zend_Uri_Http) {
         throw new Zend_Http_Client_Exception("No valid URI has been passed to the client");
     }
     if ($method) {
         $this->setMethod($method);
     }
     $this->redirectCounter = 0;
     $response = null;
     // Send the first request. If redirected, continue.
     do {
         // Clone the URI and add the additional GET parameters to it
         $uri = clone $this->uri;
         $uri_params = array();
         parse_str($uri->getQuery(), $uri_params);
         $uri->setQuery(array_merge($uri_params, $this->paramsGet));
         $body = $this->prepare_body();
         $headers = $this->prepare_headers();
         $request = implode("\r\n", $headers) . "\r\n" . $body;
         $this->last_request = $request;
         // Open the connection, send the request and read the response
         $this->adapter->connect($uri->getHost(), $uri->getPort(), $uri->getScheme() == 'https' ? true : false);
         $this->adapter->write($this->method, $uri, $this->config['httpversion'], $headers, $body);
         $response = Zend_Http_Response::factory($this->adapter->read());
         // Load cookies into cookie jar
         if (isset($this->Cookiejar)) {
             $this->Cookiejar->addCookiesFromResponse($response, $uri);
         }
         // If we got redirected, look for the Location header
         if ($response->isRedirect() && ($location = $response->getHeader('location'))) {
             // Check whether we send the exact same request again, or drop the parameters
             // and send a GET request
             if ($response->getStatus() == 303 || !$this->config['strictredirects'] && ($response->getStatus() == 302 || $response->getStatus() == 301)) {
                 $this->resetParameters();
                 $this->setMethod(self::GET);
             }
             // If we got a well formed absolute URI
             if (Zend_Uri_Http::check($location)) {
                 $this->setHeaders('host', null);
                 $this->setUri($location);
             } else {
                 // Split into path and query and set the query
                 list($location, $query) = explode('?', $location, 2);
                 $this->uri->setQueryString($query);
                 // Else, if we got just an absolute path, set it
                 if (strpos($location, '/') === 0) {
                     $this->uri->setPath($location);
                     // Else, assume we have a relative path
                 } else {
                     // Get the current path directory, removing any trailing slashes
                     $path = rtrim(dirname($this->uri->getPath()), "/");
                     $this->uri->setPath($path . '/' . $location);
                 }
             }
             $this->redirectCounter++;
         } else {
             // If we didn't get any location, stop redirecting
             break;
         }
     } while ($this->redirectCounter < $this->config['maxredirects']);
     return $response;
 }