File indexing completed on 2025-03-02 05:29:26

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Gdata
0018  * @subpackage App
0019  * @version    $Id$
0020  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0021  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0022  */
0023 
0024 /**
0025  * @see Zend_Http_Client_Adapter_Socket
0026  */
0027 // require_once 'Zend/Http/Client/Adapter/Socket.php';
0028 
0029 /**
0030  * Overrides the traditional socket-based adapter class for Zend_Http_Client to
0031  * enable logging of requests.  All requests are logged to a location specified
0032  * in the config as $config['logfile'].  Requests and responses are logged after
0033  * they are sent and received/processed, thus an error could prevent logging.
0034  *
0035  * @category   Zend
0036  * @package    Zend_Gdata
0037  * @subpackage App
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0040  */
0041 class Zend_Gdata_App_LoggingHttpClientAdapterSocket extends Zend_Http_Client_Adapter_Socket
0042 {
0043 
0044     /**
0045      * The file handle for writing logs
0046      *
0047      * @var resource|null
0048      */
0049     protected $log_handle = null;
0050 
0051     /**
0052      * Log the given message to the log file.  The log file is configured
0053      * as the config param 'logfile'.  This method opens the file for
0054      * writing if necessary.
0055      *
0056      * @param string $message The message to log
0057      */
0058     protected function log($message)
0059     {
0060         if ($this->log_handle == null) {
0061             $this->log_handle = fopen($this->config['logfile'], 'a');
0062         }
0063         fwrite($this->log_handle, $message);
0064     }
0065 
0066     /**
0067      * Connect to the remote server
0068      *
0069      * @param string  $host
0070      * @param int     $port
0071      * @param boolean $secure
0072      * @param int     $timeout
0073      */
0074     public function connect($host, $port = 80, $secure = false)
0075     {
0076         $this->log("Connecting to: ${host}:${port}");
0077         return parent::connect($host, $port, $secure);
0078     }
0079 
0080     /**
0081      * Send request to the remote server
0082      *
0083      * @param string        $method
0084      * @param Zend_Uri_Http $uri
0085      * @param string        $http_ver
0086      * @param array         $headers
0087      * @param string        $body
0088      * @return string Request as string
0089      */
0090     public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
0091     {
0092         $request = parent::write($method, $uri, $http_ver, $headers, $body);
0093         $this->log("\n\n" . $request);
0094         return $request;
0095     }
0096 
0097     /**
0098      * Read response from server
0099      *
0100      * @return string
0101      */
0102     public function read()
0103     {
0104         $response = parent::read();
0105         $this->log("${response}\n\n");
0106         return $response;
0107     }
0108 
0109     /**
0110      * Close the connection to the server
0111      *
0112      */
0113     public function close()
0114     {
0115         $this->log("Closing socket\n\n");
0116         parent::close();
0117     }
0118 
0119 }