PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/OA/XmlRpcClient.php

https://bitbucket.org/valmy/openx
PHP | 195 lines | 122 code | 35 blank | 38 comment | 19 complexity | 05c3e25e0ce44cac58338ef971ba812b MD5 | raw file
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v2.8 |
  5. | ========== |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id$
  25. */
  26. require_once MAX_PATH . '/lib/OA.php';
  27. require_once 'XML/RPC.php';
  28. /**
  29. * A class which extends PEAR::XML_RPC to improve client HTTPS support
  30. *
  31. * Note: Proxy support is currently disabled to keep things simple
  32. *
  33. * @package OpenX
  34. * @author Matteo Beccati <matteo.beccati@openx.org>
  35. */
  36. class OA_XML_RPC_Client extends XML_RPC_Client
  37. {
  38. var $hasCurl = false;
  39. var $hasOpenssl = false;
  40. var $verifyPeer;
  41. var $caFile;
  42. function OA_XML_RPC_Client($path, $server, $port = 0,
  43. $proxy = '', $proxy_port = 0,
  44. $proxy_user = '', $proxy_pass = '')
  45. {
  46. if ($aExtensions = OA::getAvailableSSLExtensions()) {
  47. $this->hasCurl = in_array('curl', $aExtensions);
  48. $this->hasOpenssl = in_array('openssl', $aExtensions);
  49. }
  50. $this->verifyPeer = false;
  51. // This CA file is reused in openXMarket plugin
  52. // to setup curl in Zend_Http_Client_Adapter_Curl in same way as here
  53. $this->caFile = MAX_PATH . '/etc/curl-ca-bundle.crt';
  54. parent::XML_RPC_Client($path, $server, $port);
  55. }
  56. function canUseSSL()
  57. {
  58. return $this->hasCurl || $this->hasOpenssl;
  59. }
  60. function _sendHttpGenerate(&$msg, $username = '', $password = '')
  61. {
  62. // Pre-emptive BC hacks for fools calling sendPayloadHTTP10() directly
  63. if ($username != $this->username) {
  64. $this->setCredentials($username, $password);
  65. }
  66. // Only create the payload if it was not created previously
  67. if (empty($msg->payload)) {
  68. $msg->createPayload();
  69. }
  70. $this->createHeaders($msg);
  71. $this->headers = str_replace(': PEAR XML_RPC', ': Openads XML_RPC', $this->headers);
  72. }
  73. function _sendHttpOpenSsl($msg, $server, $port, $timeout = 0,
  74. $username = '', $password = '')
  75. {
  76. if (!empty($timeout)) {
  77. // Set timeout
  78. $old_timeout = ini_get('default_socket_timeout');
  79. ini_set('default_socket_timeout', $timeout);
  80. }
  81. $this->_sendHttpGenerate($msg, $username, $password);
  82. $context = stream_context_create(array(
  83. 'http' => array(
  84. 'method' => 'POST',
  85. 'header' => preg_replace('/^.*?\r\n/', '', $this->headers),
  86. 'content' => $msg->payload
  87. ),
  88. 'ssl' => array(
  89. 'verify_peer' => $this->verifyPeer,
  90. 'cafile' => $this->caFile
  91. )
  92. ));
  93. $protocol = $this->protocol == 'ssl://' ? 'https://' : 'http://';
  94. $fp = @fopen("{$protocol}{$this->server}:{$port}{$this->path}", 'rb', false, $context);
  95. if (!empty($timeout)) {
  96. // Restore timeout
  97. ini_set('default_socket_timeout', $old_timeout);
  98. }
  99. if (!$fp) {
  100. $this->raiseError('Connection to RPC server '
  101. . $server . ':' . $port
  102. . ' failed. ' . $this->errstr,
  103. XML_RPC_ERROR_CONNECTION_FAILED);
  104. return 0;
  105. }
  106. $resp = $msg->parseResponseFile($fp);
  107. $meta = stream_get_meta_data($fp);
  108. if ($meta['timed_out']) {
  109. fclose($fp);
  110. $this->errstr = 'RPC server did not send response before timeout.';
  111. $this->raiseError($this->errstr, XML_RPC_ERROR_CONNECTION_FAILED);
  112. return 0;
  113. }
  114. fclose($fp);
  115. return $resp;
  116. }
  117. function _sendHttpCurl($msg, $server, $port, $timeout = 0,
  118. $username = '', $password = '')
  119. {
  120. $this->_sendHttpGenerate($msg, $username, $password);
  121. $protocol = $this->protocol == 'ssl://' ? 'https://' : 'http://';
  122. $ch = curl_init("{$protocol}{$this->server}:{$port}{$this->path}");
  123. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  124. curl_setopt($ch, CURLOPT_HEADER, true);
  125. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->headers."\r\n\r\n".$msg->payload);
  126. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verifyPeer);
  127. curl_setopt($ch, CURLOPT_CAINFO, $this->caFile);
  128. if (!empty($timeout)) {
  129. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  130. }
  131. $buffer = @curl_exec($ch);
  132. $status = curl_errno($ch);
  133. @curl_close($ch);
  134. if ($status != CURLE_OK) {
  135. if ($status == CURLE_OPERATION_TIMEOUTED) {
  136. $this->errstr = 'RPC server did not send response before timeout.';
  137. $this->raiseError($this->errstr, XML_RPC_ERROR_CONNECTION_FAILED);
  138. } else {
  139. $this->raiseError('Connection to RPC server '
  140. . $server . ':' . $port
  141. . ' failed. ' . $this->errstr,
  142. XML_RPC_ERROR_CONNECTION_FAILED);
  143. }
  144. return 0;
  145. }
  146. $resp = $msg->parseResponse($buffer);
  147. return $resp;
  148. }
  149. function sendPayloadHTTP10($msg, $server, $port, $timeout = 0,
  150. $username = '', $password = '')
  151. {
  152. if ($this->hasCurl || $this->hasOpenssl) {
  153. $args = func_get_args();
  154. $method = $this->hasCurl ? '_sendHttpCurl' : '_sendHttpOpenSsl';
  155. return call_user_func_array(array(&$this, $method), $args);
  156. }
  157. return parent::sendPayloadHTTP10($msg, $server, $port, $timeout, $username, $password);
  158. }
  159. }
  160. ?>