PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/HTTP.php

https://github.com/yetanotherx/mw-peachy
PHP | 306 lines | 138 code | 63 blank | 105 comment | 30 complexity | 1fac2a52393af43a28b9fb054a50c90a MD5 | raw file
  1. <?php
  2. /*
  3. This file is part of Peachy MediaWiki Bot API
  4. Peachy is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /**
  16. * @file
  17. * HTTP object
  18. * Stores all cURL functions
  19. */
  20. /**
  21. * HTTP Class, stores cURL functions
  22. */
  23. class HTTP {
  24. /**
  25. * Curl object
  26. *
  27. * @var cURL
  28. * @access private
  29. */
  30. private $curl_instance;
  31. /**
  32. * Hash to use for cookies
  33. *
  34. * @var string
  35. * @access private
  36. */
  37. private $cookie_hash;
  38. /**
  39. * Whether or not to enable GET:, POST:, and DLOAD: messages being sent to the terminal.
  40. *
  41. * @var bool
  42. * @access private
  43. */
  44. private $echo;
  45. /**
  46. * Useragent
  47. *
  48. * @var mixed
  49. * @access private
  50. */
  51. private $user_agent;
  52. /**
  53. * Temporary file where cookies are stored
  54. *
  55. * @var mixed
  56. * @access private
  57. */
  58. private $cookie_jar;
  59. /**
  60. * Construction method for the HTTP class
  61. *
  62. * @access public
  63. * @param bool $echo Whether or not to enable GET:, POST:, and DLOAD: messages being sent to the terminal. Default false;
  64. * @return void
  65. */
  66. function __construct( $echo = false ) {
  67. global $pgUA;
  68. if( !function_exists( 'curl_init' ) ) {
  69. throw new DependencyError( "cURL", "http://us2.php.net/manual/en/curl.requirements.php" );
  70. }
  71. $this->echo = $echo;
  72. $this->curl_instance = curl_init();
  73. $this->cookie_hash = md5( time() . '-' . rand( 0, 999 ) );
  74. $this->cookie_jar = sys_get_temp_dir() . 'peachy.cookies.'.$this->cookie_hash.'.dat';
  75. $this->user_agent = 'Peachy MediaWiki Bot API Version ' . PEACHYVERSION;
  76. Hooks::runHook( 'HTTPNewCURLInstance', array( &$this, &$echo ) );
  77. $this->setCookieJar( $this->cookie_jar );
  78. curl_setopt($this->curl_instance,CURLOPT_MAXCONNECTS,100);
  79. curl_setopt($this->curl_instance,CURLOPT_CLOSEPOLICY,CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
  80. curl_setopt($this->curl_instance,CURLOPT_MAXREDIRS,10);
  81. curl_setopt($this->curl_instance,CURLOPT_HTTPHEADER, array('Expect:'));
  82. curl_setopt($this->curl_instance,CURLOPT_ENCODING, 'gzip');
  83. curl_setopt($this->curl_instance,CURLOPT_RETURNTRANSFER,1);
  84. curl_setopt($this->curl_instance,CURLOPT_TIMEOUT,30);
  85. curl_setopt($this->curl_instance,CURLOPT_CONNECTTIMEOUT,10);
  86. $this->setUserAgent( $pgUA );
  87. }
  88. function setCookieJar( $cookie_file ) {
  89. $this->cookie_jar = $cookie_file;
  90. Hooks::runHook( 'HTTPSetCookieJar', array( &$cookie_file ) );
  91. curl_setopt($this->curl_instance,CURLOPT_COOKIEJAR, $cookie_file);
  92. curl_setopt($this->curl_instance,CURLOPT_COOKIEFILE, $cookie_file);
  93. }
  94. function setUserAgent( $user_agent = null ) {
  95. $this->user_agent = $user_agent;
  96. Hooks::runHook( 'HTTPSetUserAgent', array( &$user_agent ) );
  97. curl_setopt($this->curl_instance,CURLOPT_USERAGENT, $user_agent);
  98. }
  99. /**
  100. * Get an url with HTTP GET
  101. *
  102. * @access public
  103. * @param string $url URL to get
  104. * @param array $data Array of data to pass. Gets transformed into the URL inside the function. Default null.
  105. * @return bool|string Result
  106. */
  107. function get( $url, $data = null ) {
  108. global $argv, $pgProxy;
  109. if( count( $pgProxy ) ) {
  110. curl_setopt($this->curl_instance,CURLOPT_PROXY, $pgProxy['addr']);
  111. if( isset( $pgProxy['type'] ) ) {
  112. curl_setopt($this->curl_instance,CURLOPT_PROXYTYPE, $pgProxy['type']);
  113. }
  114. if( isset( $pgProxy['userpass'] ) ) {
  115. curl_setopt($this->curl_instance,CURLOPT_PROXYUSERPWD, $pgProxy['userpass']);
  116. }
  117. if( isset( $pgProxy['port'] ) ) {
  118. curl_setopt($this->curl_instance,CURLOPT_PROXYPORT, $pgProxy['port']);
  119. }
  120. }
  121. curl_setopt($this->curl_instance,CURLOPT_FOLLOWLOCATION,1);
  122. curl_setopt($this->curl_instance,CURLOPT_HTTPGET,1);
  123. /*if( !is_null( $this->use_cookie ) ) {
  124. curl_setopt($this->curl_instance,CURLOPT_COOKIE, $this->use_cookie);
  125. }*/
  126. if( !is_null( $data ) && is_array( $data ) ) {
  127. $url .= '?' . http_build_query( $data );
  128. }
  129. curl_setopt($this->curl_instance,CURLOPT_URL,$url);
  130. if( (!is_null( $argv ) && in_array( 'peachyecho', $argv )) || $this->echo ) {
  131. pecho( "GET: $url\n", PECHO_NORMAL );
  132. }
  133. Hooks::runHook( 'HTTPGet', array( &$this, &$url, &$data ) );
  134. $data = curl_exec( $this->curl_instance );
  135. if( curl_errno( $this->curl_instance ) != 0 ) {
  136. throw new CURLError( curl_errno( $this->curl_instance ), curl_error( $this->curl_instance ) );
  137. return false;
  138. }
  139. return $data;
  140. }
  141. /**
  142. * Returns the HTTP code of the last request
  143. *
  144. * @access public
  145. * @return int HTTP code
  146. */
  147. function get_HTTP_code() {
  148. $ci = curl_getinfo( $this->curl_instance );
  149. return $ci['http_code'];
  150. }
  151. /**
  152. * Sends data via HTTP POST
  153. *
  154. * @access public
  155. * @param string $url URL to send
  156. * @param array $data Array of data to pass.
  157. * @return bool|string Result
  158. */
  159. function post( $url, $data ) {
  160. global $argv, $pgProxy;
  161. if( count( $pgProxy ) ) {
  162. curl_setopt($this->curl_instance,CURLOPT_PROXY, $pgProxy['addr']);
  163. if( isset( $pgProxy['type'] ) ) {
  164. curl_setopt($this->curl_instance,CURLOPT_PROXYTYPE, $pgProxy['type']);
  165. }
  166. if( isset( $pgProxy['userpass'] ) ) {
  167. curl_setopt($this->curl_instance,CURLOPT_PROXYUSERPWD, $pgProxy['userpass']);
  168. }
  169. if( isset( $pgProxy['port'] ) ) {
  170. curl_setopt($this->curl_instance,CURLOPT_PROXYPORT, $pgProxy['port']);
  171. }
  172. }
  173. curl_setopt($this->curl_instance,CURLOPT_FOLLOWLOCATION,0);
  174. curl_setopt($this->curl_instance,CURLOPT_POST,1);
  175. curl_setopt($this->curl_instance,CURLOPT_POSTFIELDS, $data);
  176. /*if( !is_null( $this->use_cookie ) ) {
  177. curl_setopt($this->curl_instance,CURLOPT_COOKIE, $this->use_cookie);
  178. }*/
  179. curl_setopt($this->curl_instance,CURLOPT_URL,$url);
  180. if( (!is_null( $argv ) && in_array( 'peachyecho', $argv )) || $this->echo ) {
  181. pecho( "POST: $url\n", PECHO_NORMAL );
  182. }
  183. Hooks::runHook( 'HTTPPost', array( &$this, &$url, &$data ) );
  184. $data = curl_exec( $this->curl_instance );
  185. if( curl_errno( $this->curl_instance ) != 0 ) {
  186. throw new CURLError( curl_errno( $this->curl_instance ), curl_error( $this->curl_instance ) );
  187. return false;
  188. }
  189. return $data;
  190. }
  191. /**
  192. * Downloads an URL to the local disk
  193. *
  194. * @access public
  195. * @param string $url URL to get
  196. * @param array $local Local filename to download to
  197. * @return bool
  198. */
  199. function download( $url, $local ) {
  200. global $argv, $pgProxy;
  201. $out = fopen($local, 'wb');
  202. if( count( $pgProxy ) ) {
  203. curl_setopt($this->curl_instance,CURLOPT_PROXY, $pgProxy['addr']);
  204. if( isset( $pgProxy['type'] ) ) {
  205. curl_setopt($this->curl_instance,CURLOPT_PROXYTYPE, $pgProxy['type']);
  206. }
  207. if( isset( $pgProxy['userpass'] ) ) {
  208. curl_setopt($this->curl_instance,CURLOPT_PROXYUSERPWD, $pgProxy['userpass']);
  209. }
  210. if( isset( $pgProxy['port'] ) ) {
  211. curl_setopt($this->curl_instance,CURLOPT_PROXYPORT, $pgProxy['port']);
  212. }
  213. }
  214. //curl_setopt($this->curl_instance, CURLOPT_FILE, $out);
  215. curl_setopt($this->curl_instance, CURLOPT_URL, $url);
  216. curl_setopt($this->curl_instance, CURLOPT_HEADER, 0);
  217. if( (!is_null( $argv ) && in_array( 'peachyecho', $argv )) || $this->echo ) {
  218. pecho( "DLOAD: $url\n", PECHO_NORMAL );
  219. }
  220. Hooks::runHook( 'HTTPDownload', array( &$this, &$url, &$local ) );
  221. $ret = curl_exec( $this->curl_instance );
  222. if( curl_errno( $this->curl_instance ) != 0 ) {
  223. throw new CURLError( curl_errno( $this->curl_instance ), curl_error( $this->curl_instance ) );
  224. return false;
  225. }
  226. fwrite( $out, $ret );
  227. fclose($out);
  228. }
  229. /**
  230. * Destructor, deletes cookies and closes cURL class
  231. *
  232. * @access public
  233. * @return void
  234. */
  235. function __destruct () {
  236. Hooks::runHook( 'HTTPClose', array( &$this ) );
  237. curl_close($this->curl_instance);
  238. //@unlink($this->cookie_jar);
  239. }
  240. }