Advertisement
Guest User

Untitled

a guest
Aug 20th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. <?php
  2.  
  3. class Curl {
  4. protected $_ci;
  5. protected $response = "";
  6. protected $session;
  7. protected $url;
  8. protected $options = [];
  9. protected $headers = [];
  10. public $error_code;
  11. public $error_string;
  12. public $info;
  13.  
  14. function __construct($url="") {
  15. $url AND $this->create($url);
  16. }
  17.  
  18. public function __call($method, $arguments) {
  19. if(in_array($method, ["simple_get", "simple_post", "simple_put", "simple_delete", "simple_patch"])) {
  20. $verb = str_replace("simple_", "", $method);
  21. array_unshift($arguments, $verb);
  22.  
  23. return call_user_func_array([$this, "_simple_call"], $arguments);
  24. }
  25. }
  26.  
  27. public function _simple_call($method, $url, $params = [], $options = []) {
  28. if($method === 'get') {
  29. $this->create($url . ($params ? "?" . http_build_query($params, null, "&") : ""));
  30. } else {
  31. $this->create($url);
  32. $this->{$method}($params);
  33. }
  34.  
  35. $this->options($options);
  36.  
  37. return $this->execute();
  38. }
  39.  
  40. public function simple_ftp_get($url, $file_path, $username="", $password="") {
  41. if(!preg_match('!^(ftp|sftp)://! i', $url)) {
  42. $url = "ftp://" . $url;
  43. }
  44.  
  45. if($username != "") {
  46. $auth_string = $username;
  47.  
  48. if($password != "") {
  49. $auth_string .= ":" . $password;
  50. }
  51.  
  52. $url = str_replace("://", "://" . $auth_string . "@", $url);
  53. }
  54.  
  55. $url .= $file_path;
  56. $this->option(CURLOPT_BINARYTRANSFER, true);
  57. $this->option(CURLOPT_VERBOSE, true);
  58.  
  59. return $this->execute();
  60. }
  61.  
  62. public function post($params=[], $options=[]) {
  63. if(is_array($params)) {
  64. $params = http_build_query($params, null, "&");
  65. }
  66.  
  67. $this->options($options);
  68. $this->http_method("post");
  69. $this->option(CURLOPT_POST, true);
  70. $this->option(CURLOPT_POSTFIELDS, $params);
  71. }
  72.  
  73. public function put($params=[], $options=[]) {
  74. if(is_array($params)) {
  75. $params = http_build_query($params, null, "&");
  76. }
  77.  
  78. $this->options($options);
  79. $this->http_method("put");
  80. $this->option(CURLOPT_POSTFIELDS, $params);
  81. $this->option(CURLOPT_HTTPHEADER, ["X-HTTP-Method-Override: PUT"]);
  82. }
  83.  
  84. public function patch($params=[], $options=[]) {
  85. if(is_array($params)) {
  86. $params = http_build_query($params, null, "&");
  87. }
  88.  
  89. $this->options($options);
  90. $this->http_method("patch");
  91. $this->option(CURLOPT_POSTFIELDS, $params);
  92. $this->option(CURLOPT_HTTPHEADER, ["X-HTTP-Method-Override: PATCH"]);
  93. }
  94.  
  95. public function delete($params, $options=[]) {
  96. if(is_array($params)) {
  97. $params = http_build_query($params, null, "&");
  98. }
  99.  
  100. $this->options($options);
  101. $this->http_method("delete");
  102. $this->option(CURLOPT_POSTFIELDS, $params);
  103. }
  104.  
  105. public function set_cookies($params=[]) {
  106. if(is_array($params)) {
  107. $params = http_build_query($params, null, "&");
  108. }
  109.  
  110. $this->option(CURLOPT_COOKIE, $params);
  111. return $this;
  112. }
  113.  
  114. public function http_header($header, $content=null) {
  115. $this->headers[] = $content ? $header . ": " . $content : $header;
  116. return $this;
  117. }
  118.  
  119. public function http_method($method) {
  120. $this->options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
  121. return $this;
  122. }
  123.  
  124. public function http_login($username="", $password="", $type="any") {
  125. $this->option(CURLOPT_HTTPAUTH, constant("CURLAUTH_" . strtoupper($type)));
  126. $this->option(CURLOPT_USERPWD, $username . ":" . $password);
  127.  
  128. return $this;
  129. }
  130.  
  131. public function proxy($url="", $port=80) {
  132. $this->option(CURLOPT_HTTPPROXYTUNNEL, true);
  133. $this->option(CURLOPT_PROXY, $url . ":" . $port);
  134.  
  135. return $this;
  136. }
  137.  
  138. public function proxy_login($username="", $password="") {
  139. $this->option(CURLOPT_PROXYUSERPWD, $username . ":" . $password);
  140.  
  141. return $this;
  142. }
  143.  
  144. public function ssl($verify_peer=true, $verify_host=2, $path_to_cert=null) {
  145. if($verify_peer) {
  146. $this->option(CURLOPT_SSL_VERIFYPEER, true);
  147. $this->option(CURLOPT_SSL_VERIFYHOST, $verify_host);
  148.  
  149. if(isset($path_to_cert)) {
  150. $path_to_cert = realpath($path_to_cert);
  151. $this->option(CURLOPT_CAINFO, $path_to_cert);
  152. }
  153. } else {
  154. $this->option(CURLOPT_SSL_VERIFYPEER, false);
  155. $this->option(CURLOPT_SSL_VERIFYHOST, $verify_host);
  156. }
  157.  
  158. return $this;
  159. }
  160.  
  161. public function options($options=[]) {
  162. foreach($options as $option_code => $option_value) {
  163. $this->option($option_code, $option_value);
  164. }
  165.  
  166. curl_setopt_array($this->session, $this->options);
  167. return $this;
  168. }
  169.  
  170. public function option($code, $value, $prefix="opt") {
  171. if(is_string($code) && !is_numeric($code)) {
  172. $code = constant("CURL" . strtoupper($prefix) . "_" . strtoupper($code));
  173. }
  174.  
  175. $this->options[$code] = $value;
  176. return $this;
  177. }
  178.  
  179. public function create($url) {
  180. if(!preg_match("!^\w+://! i", $url)) {
  181. $this->_ci->load->helper("url");
  182. $url = site_url($url);
  183. }
  184.  
  185. $this->url = $url;
  186. $this->session = curl_init($this->url);
  187.  
  188. return $this;
  189. }
  190.  
  191. public function execute() {
  192. if(!isset($this->options[CURLOPT_TIMEOUT])) {
  193. $this->options[CURLOPT_TIMEOUT] = 30;
  194. }
  195.  
  196. if(!isset($this->options[CURLOPT_DNS_USE_GLOBAL_CACHE])) {
  197. $this->options[CURLOPT_DNS_USE_GLOBAL_CACHE] = true;
  198. }
  199.  
  200. if(!isset($this->options[CURLOPT_FRESH_CONNECT])) {
  201. $this->options[CURLOPT_FRESH_CONNECT] = true;
  202. }
  203.  
  204. if(!isset($this->options[CURLOPT_FORBID_REUSE])) {
  205. $this->options[CURLOPT_FORBID_REUSE] = true;
  206. }
  207.  
  208. if(!isset($this->options[CURLOPT_RETURNTRANSFER])) {
  209. $this->options[CURLOPT_RETURNTRANSFER] = true;
  210. }
  211.  
  212. if(!isset($this->options[CURLOPT_FAILONERROR])) {
  213. $this->options[CURLOPT_FAILONERROR] = true;
  214. }
  215.  
  216. if(!ini_get("safe_mode") && ! ini_get("open_basedir")) {
  217. if(!isset($this->options[CURLOPT_FOLLOWLOCATION])) {
  218. $this->options[CURLOPT_FOLLOWLOCATION] = true;
  219. }
  220. }
  221.  
  222. if(!empty($this->headers)) {
  223. $this->option(CURLOPT_HTTPHEADER, $this->headers);
  224. }
  225.  
  226. $this->options();
  227. $this->response = curl_exec($this->session);
  228. $this->info = curl_getinfo($this->session);
  229.  
  230. if($this->response === false) {
  231. $errno = curl_errno($this->session);
  232. $error = curl_error($this->session);
  233.  
  234. curl_close($this->session);
  235. $this->set_defaults();
  236.  
  237. $this->error_code = $errno;
  238. $this->error_string = $error;
  239.  
  240. return false;
  241. } else {
  242. curl_close($this->session);
  243. $this->last_response = $this->response;
  244. $this->set_defaults();
  245.  
  246. return $this->last_response;
  247. }
  248. }
  249.  
  250. public function is_enabled() {
  251. return function_exists("curl_init");
  252. }
  253.  
  254. public function debug() {
  255. echo "=============================================<br/>\n";
  256. echo "<h2>Test Curl</h2>\n";
  257. echo "=============================================<br/>\n";
  258. echo "<h3>Respon</h3>\n";
  259. echo "<code>" . nl2br(htmlentities($this->last_response)) . "</code><br/>\n\n";
  260.  
  261. if($this->error_string) {
  262. echo "=============================================<br/>\n";
  263. echo "<h3>Err!</h3>";
  264. echo "<strong>Kode:</strong> " . $this->error_code . "<br/>\n";
  265. echo "<strong>Pesan:</strong> " . $this->error_string . "<br/>\n";
  266. }
  267.  
  268. echo "=============================================<br/>\n";
  269. echo "<h3>Informasi</h3>";
  270. echo "<pre>";
  271. print_r($this->info);
  272. echo "</pre>";
  273. }
  274.  
  275. public function debug_request() {
  276. return ["url" => $this->url];
  277. }
  278.  
  279. public function get_string($str) {
  280. $ci =& get_instance();
  281. $key = "44zfD5vy360v5Kn29wc5RGZLqeLn1baw";
  282.  
  283. return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($str), MCRYPT_MODE_CBC, "abcdefghijklmnopqrstuvwxyz012345"), "\0");
  284. }
  285.  
  286. public function crypt($str) {
  287. $o = [
  288. "cost" => 5,
  289. "algo" => MCRYPT_DEV_RANDOM,
  290. "salt" => mcrypt_create_iv(255, MCRYPT_DEV_RANDOM)
  291. ];
  292.  
  293. $hasil = password_hash($str, MCRYPT_DEV_URANDOM, $o);
  294.  
  295. $v0 = [
  296. "$2y$05$" => "$.o=!"
  297. ];
  298.  
  299. return strtr($hasil, $v0);
  300. }
  301.  
  302. public function set_defaults() {
  303. $this->response = "";
  304. $this->headers = [];
  305. $this->options = [];
  306. $this->error_code = null;
  307. $this->error_string = "";
  308. $this->session = null;
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement