PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/manager/application/libraries/__Curl.php

https://bitbucket.org/jerwinse/iagh-cms
PHP | 375 lines | 260 code | 66 blank | 49 comment | 25 complexity | b67695492dc8fc059f871dbd470875d3 MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter Curl Class
  4. *
  5. * Work with remote servers via cURL much easier than using the native PHP bindings.
  6. *
  7. * @package CodeIgniter
  8. * @subpackage Libraries
  9. * @category Libraries
  10. * @author Philip Sturgeon
  11. * @license http://philsturgeon.co.uk/code/dbad-license
  12. * @link http://philsturgeon.co.uk/code/codeigniter-curl
  13. */
  14. class Curl {
  15. protected $_ci; // CodeIgniter instance
  16. protected $response = ''; // Contains the cURL response for debug
  17. protected $session; // Contains the cURL handler for a session
  18. protected $url; // URL of the session
  19. protected $options = array(); // Populates curl_setopt_array
  20. protected $headers = array(); // Populates extra HTTP headers
  21. public $error_code; // Error code returned as an int
  22. public $error_string; // Error message returned as a string
  23. public $info; // Returned after request (elapsed time, etc)
  24. function __construct($url = '')
  25. {
  26. $this->_ci = & get_instance();
  27. log_message('debug', 'cURL Class Initialized');
  28. if ( ! $this->is_enabled())
  29. {
  30. log_message('error', 'cURL Class - PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.');
  31. }
  32. $url AND $this->create($url);
  33. }
  34. public function __call($method, $arguments)
  35. {
  36. if (in_array($method, array('simple_get', 'simple_post', 'simple_put', 'simple_delete')))
  37. {
  38. // Take off the "simple_" and past get/post/put/delete to _simple_call
  39. $verb = str_replace('simple_', '', $method);
  40. array_unshift($arguments, $verb);
  41. return call_user_func_array(array($this, '_simple_call'), $arguments);
  42. }
  43. }
  44. /* =================================================================================
  45. * SIMPLE METHODS
  46. * Using these methods you can make a quick and easy cURL call with one line.
  47. * ================================================================================= */
  48. public function _simple_call($method, $url, $params = array(), $options = array())
  49. {
  50. // Get acts differently, as it doesnt accept parameters in the same way
  51. if ($method === 'get')
  52. {
  53. // If a URL is provided, create new session
  54. $this->create($url.($params ? '?'.http_build_query($params, NULL, '&') : ''));
  55. }
  56. else
  57. {
  58. // If a URL is provided, create new session
  59. $this->create($url);
  60. $this->{$method}($params);
  61. }
  62. // Add in the specific options provided
  63. $this->options($options);
  64. return $this->execute();
  65. }
  66. public function simple_ftp_get($url, $file_path, $username = '', $password = '')
  67. {
  68. // If there is no ftp:// or any protocol entered, add ftp://
  69. if ( ! preg_match('!^(ftp|sftp)://! i', $url))
  70. {
  71. $url = 'ftp://' . $url;
  72. }
  73. // Use an FTP login
  74. if ($username != '')
  75. {
  76. $auth_string = $username;
  77. if ($password != '')
  78. {
  79. $auth_string .= ':' . $password;
  80. }
  81. // Add the user auth string after the protocol
  82. $url = str_replace('://', '://' . $auth_string . '@', $url);
  83. }
  84. // Add the filepath
  85. $url .= $file_path;
  86. $this->option(CURLOPT_BINARYTRANSFER, TRUE);
  87. $this->option(CURLOPT_VERBOSE, TRUE);
  88. return $this->execute();
  89. }
  90. /* =================================================================================
  91. * ADVANCED METHODS
  92. * Use these methods to build up more complex queries
  93. * ================================================================================= */
  94. public function post($params = array(), $options = array())
  95. {
  96. // If its an array (instead of a query string) then format it correctly
  97. if (is_array($params))
  98. {
  99. $params = http_build_query($params, NULL, '&');
  100. }
  101. // Add in the specific options provided
  102. $this->options($options);
  103. $this->http_method('post');
  104. $this->option(CURLOPT_POST, TRUE);
  105. $this->option(CURLOPT_POSTFIELDS, $params);
  106. }
  107. public function put($params = array(), $options = array())
  108. {
  109. // If its an array (instead of a query string) then format it correctly
  110. if (is_array($params))
  111. {
  112. $params = http_build_query($params, NULL, '&');
  113. }
  114. // Add in the specific options provided
  115. $this->options($options);
  116. $this->http_method('put');
  117. $this->option(CURLOPT_POSTFIELDS, $params);
  118. // Override method, I think this overrides $_POST with PUT data but... we'll see eh?
  119. $this->option(CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
  120. }
  121. public function delete($params, $options = array())
  122. {
  123. // If its an array (instead of a query string) then format it correctly
  124. if (is_array($params))
  125. {
  126. $params = http_build_query($params, NULL, '&');
  127. }
  128. // Add in the specific options provided
  129. $this->options($options);
  130. $this->http_method('delete');
  131. $this->option(CURLOPT_POSTFIELDS, $params);
  132. }
  133. public function set_cookies($params = array())
  134. {
  135. if (is_array($params))
  136. {
  137. $params = http_build_query($params, NULL, '&');
  138. }
  139. $this->option(CURLOPT_COOKIE, $params);
  140. return $this;
  141. }
  142. public function http_header($header, $content = NULL)
  143. {
  144. $this->headers[] = $content ? $header . ': ' . $content : $header;
  145. }
  146. public function http_method($method)
  147. {
  148. $this->options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
  149. return $this;
  150. }
  151. public function http_login($username = '', $password = '', $type = 'any')
  152. {
  153. $this->option(CURLOPT_HTTPAUTH, constant('CURLAUTH_' . strtoupper($type)));
  154. $this->option(CURLOPT_USERPWD, $username . ':' . $password);
  155. return $this;
  156. }
  157. public function proxy($url = '', $port = 80)
  158. {
  159. $this->option(CURLOPT_HTTPPROXYTUNNEL, TRUE);
  160. $this->option(CURLOPT_PROXY, $url . ':' . $port);
  161. return $this;
  162. }
  163. public function proxy_login($username = '', $password = '')
  164. {
  165. $this->option(CURLOPT_PROXYUSERPWD, $username . ':' . $password);
  166. return $this;
  167. }
  168. public function ssl($verify_peer = TRUE, $verify_host = 2, $path_to_cert = NULL)
  169. {
  170. if ($verify_peer)
  171. {
  172. $this->option(CURLOPT_SSL_VERIFYPEER, TRUE);
  173. $this->option(CURLOPT_SSL_VERIFYHOST, $verify_host);
  174. $this->option(CURLOPT_CAINFO, $path_to_cert);
  175. }
  176. else
  177. {
  178. $this->option(CURLOPT_SSL_VERIFYPEER, FALSE);
  179. }
  180. return $this;
  181. }
  182. public function options($options = array())
  183. {
  184. // Merge options in with the rest - done as array_merge() does not overwrite numeric keys
  185. foreach ($options as $option_code => $option_value)
  186. {
  187. $this->option($option_code, $option_value);
  188. }
  189. // Set all options provided
  190. curl_setopt_array($this->session, $this->options);
  191. return $this;
  192. }
  193. public function option($code, $value)
  194. {
  195. if (is_string($code) && !is_numeric($code))
  196. {
  197. $code = constant('CURLOPT_' . strtoupper($code));
  198. }
  199. $this->options[$code] = $value;
  200. return $this;
  201. }
  202. // Start a session from a URL
  203. public function create($url)
  204. {
  205. // If no a protocol in URL, assume its a CI link
  206. if ( ! preg_match('!^\w+://! i', $url))
  207. {
  208. $this->_ci->load->helper('url');
  209. $url = site_url($url);
  210. }
  211. $this->url = $url;
  212. $this->session = curl_init($this->url);
  213. return $this;
  214. }
  215. // End a session and return the results
  216. public function execute()
  217. {
  218. // Set two default options, and merge any extra ones in
  219. if ( ! isset($this->options[CURLOPT_TIMEOUT]))
  220. {
  221. $this->options[CURLOPT_TIMEOUT] = 30;
  222. }
  223. if ( ! isset($this->options[CURLOPT_RETURNTRANSFER]))
  224. {
  225. $this->options[CURLOPT_RETURNTRANSFER] = TRUE;
  226. }
  227. if ( ! isset($this->options[CURLOPT_FAILONERROR]))
  228. {
  229. $this->options[CURLOPT_FAILONERROR] = TRUE;
  230. }
  231. // Only set follow location if not running securely
  232. if ( ! ini_get('safe_mode') && ! ini_get('open_basedir'))
  233. {
  234. // Ok, follow location is not set already so lets set it to true
  235. if ( ! isset($this->options[CURLOPT_FOLLOWLOCATION]))
  236. {
  237. $this->options[CURLOPT_FOLLOWLOCATION] = TRUE;
  238. }
  239. }
  240. if ( ! empty($this->headers))
  241. {
  242. $this->option(CURLOPT_HTTPHEADER, $this->headers);
  243. }
  244. $this->options();
  245. // Execute the request & and hide all output
  246. $this->response = curl_exec($this->session);
  247. $this->info = curl_getinfo($this->session);
  248. // Request failed
  249. if ($this->response === FALSE)
  250. {
  251. $errno = curl_errno($this->session);
  252. $error = curl_error($this->session);
  253. curl_close($this->session);
  254. $this->set_defaults();
  255. $this->error_code = $errno;
  256. $this->error_string = $error;
  257. return FALSE;
  258. }
  259. // Request successful
  260. else
  261. {
  262. curl_close($this->session);
  263. $this->last_response = $this->response;
  264. $this->set_defaults();
  265. return $this->last_response;
  266. }
  267. }
  268. public function is_enabled()
  269. {
  270. return function_exists('curl_init');
  271. }
  272. public function debug()
  273. {
  274. echo "=============================================<br/>\n";
  275. echo "<h2>CURL Test</h2>\n";
  276. echo "=============================================<br/>\n";
  277. echo "<h3>Response</h3>\n";
  278. echo "<code>" . nl2br(htmlentities($this->last_response)) . "</code><br/>\n\n";
  279. if ($this->error_string)
  280. {
  281. echo "=============================================<br/>\n";
  282. echo "<h3>Errors</h3>";
  283. echo "<strong>Code:</strong> " . $this->error_code . "<br/>\n";
  284. echo "<strong>Message:</strong> " . $this->error_string . "<br/>\n";
  285. }
  286. echo "=============================================<br/>\n";
  287. echo "<h3>Info</h3>";
  288. echo "<pre>";
  289. print_r($this->info);
  290. echo "</pre>";
  291. }
  292. public function debug_request()
  293. {
  294. return array(
  295. 'url' => $this->url
  296. );
  297. }
  298. public function set_defaults()
  299. {
  300. $this->response = '';
  301. $this->headers = array();
  302. $this->options = array();
  303. $this->error_code = NULL;
  304. $this->error_string = '';
  305. $this->session = NULL;
  306. }
  307. }
  308. /* End of file Curl.php */
  309. /* Location: ./application/libraries/Curl.php */