PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/automatic-youtube-video-posts/class/curl.php

https://gitlab.com/endomorphosis/jeffersonsmithmayor
PHP | 417 lines | 233 code | 54 blank | 130 comment | 13 complexity | c23937d881a6843cf91e57a68465829c MD5 | raw file
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////////////////////////
  3. //// File:
  4. //// curl.php
  5. //// Actions:
  6. //// 1)
  7. //// Account:
  8. //// Added on July 17th 2010
  9. //// Version:
  10. //// 1.1
  11. ////
  12. //// Written by Matthew Praetzel. Copyright (c) 2010 Matthew Praetzel.
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. /****************************************Commence Script*******************************************/
  15. if(!class_exists('tern_curl')) {
  16. class tern_curl {
  17. var $options = array(
  18. //authentication
  19. //'HTTPAUTH' => CURLAUTH_ANY,
  20. //'NETRC' => false,
  21. //'UNRESTRICTED_AUTH' => false,
  22. //'USERPWD' => '',
  23. //buffer
  24. //'BUFFERSIZE' => 128,
  25. //cache
  26. //'DNS_USE_GLOBAL_CACHE'=> true,
  27. //'FORBID_REUSE' => false,
  28. //'FRESH_CONNECT' => false,
  29. //'TIMECONDITION' => CURL_TIMECOND_IFMODSINCE,
  30. //'TIMEVALUE' => (int),
  31. //callbacks
  32. //'HEADERFUNCTION' => (func),
  33. //'PASSWDFUNCTION' => (func),
  34. //'PROGRESSFUNCTION' => (func),
  35. //'READFUNCTION' => (func),
  36. //'WRITEFUNCTION' => (func),
  37. //connection
  38. 'MAXCONNECTS' => 3,
  39. //'CLOSEPOLICY' => CURLCLOSEPOLICY_OLDEST,
  40. //'INTERFACE' => '',
  41. //'PROTOCOLS' => CURLPROTO_ALL,
  42. //debugging
  43. //'NOPROGRESS' => true,
  44. //FTP
  45. //'FTP_USE_EPRT' => false,
  46. //'FTP_USE_EPSV' => false,
  47. //'FTPAPPEND' => false,
  48. //'FTPLISTONLY' => false,
  49. //'FTPPORT' => '',
  50. //'FTPSSLAUTH' => CURLFTPAUTH_DEFAULT,
  51. //'INFILESIZE' => 128,
  52. //'KRB4LEVEL' => NULL,
  53. //'POSTQUOTE' => array(),
  54. //'QUOTE' => array(),
  55. //'TRANSFERTEXT' => true,
  56. //processing
  57. //'NOSIGNAL' => true,
  58. //porting
  59. //'PORT' => (int),
  60. //proxies
  61. //'HTTPPROXYTUNNEL' => false,
  62. //'PROXY' => '',
  63. //'PROXYAUTH' => CURLAUTH_BASIC,
  64. //'PROXYPORT' => (int),
  65. //'PROXYTYPE' => CURLPROXY_HTTP,
  66. //'PROXYUSERPWD' => '',
  67. //redirects
  68. //'AUTOREFERER' => true,
  69. //'FOLLOWLOCATION' => false,
  70. 'MAXREDIRS' => 3,
  71. //'REDIR_PROTOCOLS' => CURLPROTO_ALL,
  72. //return
  73. //'BINARYTRANSFER' => true,
  74. //'CRLF' => false,
  75. //'FAILONERROR' => false,
  76. //'FILE' => STDOUT,
  77. //'FILETIME' => false,
  78. 'HEADER' => true,
  79. //'HEADER_OUT' => false,
  80. //'MUTE' => false,
  81. //'NOBODY' => false,
  82. 'RETURNTRANSFER' => false,
  83. //'STDERR' => (resource),
  84. //'VERBOSE' => false,
  85. //'WRITEHEADER' => (resource),
  86. //request
  87. //'CUSTOMREQUEST' => '',
  88. //'ENCODING' => '',
  89. //'HTTPGET' => (bool),
  90. //'HTTPHEADER' => array(),
  91. //'INFILE' => (resource),
  92. //'POST' => (bool),
  93. //'POSTFIELDS' => '',
  94. //'PUT' => (bool),
  95. //'RANGE' => '',
  96. //'REFERER' => '',
  97. //'UPLOAD' => (bool),
  98. 'USERAGENT' => 'Ternstyle/3.0',
  99. //sessions
  100. //'COOKIE' => '',
  101. //'COOKIEFILE' => '',
  102. //'COOKIEJAR' => '',
  103. //'COOKIESESSION' => false,
  104. //ssl
  105. //'CAINFO' => '',
  106. //'CAPATH' => '',
  107. //'EGDSOCKET' => '',
  108. //'RANDOM_FILE' => '',
  109. //'SSL_CIPHER_LIST' => '',
  110. //'SSL_VERIFYHOST' => true,
  111. //'SSL_VERIFYPEER' => 2,
  112. //'SSLCERT' => '',
  113. //'SSLCERTPASSWD' => '',
  114. //'SSLCERTTYPE' => '',
  115. //'SSLENGINE' => '',
  116. //'SSLENGINE_DEFAULT' => '',
  117. //'SSLKEY' => '',
  118. //'SSLKEYPASSWD' => '',
  119. //'SSLKEYTYPE' => 'PEM',
  120. //'SSLVERSION' => 2,
  121. //timeout
  122. 'CONNECTTIMEOUT' => 5,
  123. //'CONNECTTIMEOUT_MS' => 5000,
  124. //'DNS_CACHE_TIMEOUT' => 120,
  125. 'TIMEOUT' => 5,
  126. //'TIMEOUT_MS' => 5000,
  127. //transfer
  128. //'LOW_SPEED_LIMIT' => (int),
  129. //'LOW_SPEED_TIME' => (int),
  130. //'RESUME_FROM' => (int),
  131. //versioning
  132. 'HTTP_VERSION' => CURL_HTTP_VERSION_1_0
  133. );
  134. var $statii = array(
  135. 100 => 'Continue',
  136. 101 => 'Switching Protocols',
  137. 102 => 'Processing',
  138. 200 => 'OK',
  139. 201 => 'Created',
  140. 202 => 'Accepted',
  141. 203 => 'Non-Authoritative Information',
  142. 204 => 'No Content',
  143. 205 => 'Reset Content',
  144. 206 => 'Partial Content',
  145. 207 => 'Multi-Status',
  146. 226 => 'IM Used',
  147. 300 => 'Multiple Choices',
  148. 301 => 'Moved Permanently',
  149. 302 => 'Found',
  150. 303 => 'See Other',
  151. 304 => 'Not Modified',
  152. 305 => 'Use Proxy',
  153. 306 => 'Reserved',
  154. 307 => 'Temporary Redirect',
  155. 400 => 'Bad Request',
  156. 401 => 'Unauthorized',
  157. 402 => 'Payment Required',
  158. 403 => 'Forbidden',
  159. 404 => 'Not Found',
  160. 405 => 'Method Not Allowed',
  161. 406 => 'Not Acceptable',
  162. 407 => 'Proxy Authentication Required',
  163. 408 => 'Request Timeout',
  164. 409 => 'Conflict',
  165. 410 => 'Gone',
  166. 411 => 'Length Required',
  167. 412 => 'Precondition Failed',
  168. 413 => 'Request Entity Too Large',
  169. 414 => 'Request-URI Too Long',
  170. 415 => 'Unsupported Media Type',
  171. 416 => 'Requested Range Not Satisfiable',
  172. 417 => 'Expectation Failed',
  173. 422 => 'Unprocessable Entity',
  174. 423 => 'Locked',
  175. 424 => 'Failed Dependency',
  176. 426 => 'Upgrade Required',
  177. 500 => 'Internal Server Error',
  178. 501 => 'Not Implemented',
  179. 502 => 'Bad Gateway',
  180. 503 => 'Service Unavailable',
  181. 504 => 'Gateway Timeout',
  182. 505 => 'HTTP Version Not Supported',
  183. 506 => 'Variant Also Negotiates',
  184. 507 => 'Insufficient Storage',
  185. 510 => 'Not Extended'
  186. );
  187. function __contruct() {
  188. if(!function_exists('curl_init')) {
  189. return false;
  190. }
  191. }
  192. function __destruct() {
  193. unset($this->options,$this->statii,$this->o,$this->response,$this->a,$this->c);
  194. }
  195. function get($a) {
  196. $a = array_merge($a,array('type'=>'GET'));
  197. return $this->request($a);
  198. }
  199. function post($a) {
  200. $a = array_merge($a,array('type'=>'POST'));
  201. return $this->request($a);
  202. }
  203. function request($a) {
  204. $a['options'] = $a['options'] ? array_merge($this->options,$a['options']) : $this->options;
  205. $this->a = array_merge(array(
  206. 'url' => '',
  207. 'type' => 'GET',
  208. 'data' => array(),
  209. 'headers' => array(),
  210. 'options' => array()
  211. ),$a);
  212. $this->c = curl_init();
  213. $this->set_headers();
  214. $this->set_cookies();
  215. $this->set_opts();
  216. $this->o = new tern_curl_response;
  217. $this->response = curl_exec($this->c);
  218. $this->status();
  219. $this->parse();
  220. return $this->o;
  221. }
  222. function set_headers() {
  223. $h = array();
  224. foreach((array)$this->a['headers'] as $k => $v) {
  225. $h[] = $k.': '.$v;
  226. }
  227. if(!empty($h)) {
  228. curl_setopt($this->c,CURLOPT_HTTPHEADER,$h);
  229. }
  230. }
  231. function set_cookies() {
  232. if(!empty($this->a['cookies'])) {
  233. curl_setopt($this->c,CURLOPT_COOKIE,$this->compile_cookies());
  234. }
  235. }
  236. function set_opts() {
  237. curl_setopt($this->c,CURLOPT_URL,$this->a['url']);
  238. curl_setopt($this->c,CURLOPT_REFERER,$this->a['url']);
  239. foreach($this->a['options'] as $k => $v) {
  240. eval('curl_setopt($this->c,CURLOPT_'.$k.',$v);');
  241. }
  242. switch ($this->a['type']) {
  243. case 'GET' :
  244. curl_setopt($this->c,CURLOPT_HTTPGET,true);
  245. break;
  246. case 'POST' :
  247. curl_setopt($this->c,CURLOPT_POST,true);
  248. if(!empty($this->a['data'])) {
  249. curl_setopt($this->c,CURLOPT_POSTFIELDS,$this->a['data']);
  250. }
  251. break;
  252. case 'PUT' :
  253. curl_setopt($this->c,CURLOPT_PUT,true);
  254. break;
  255. case 'HEAD' :
  256. curl_setopt($this->c,CURLOPT_CUSTOMREQUEST,'HEAD');
  257. break;
  258. default :
  259. curl_setopt($this->c,CURLOPT_CUSTOMREQUEST,$this->a['type']);
  260. break;
  261. }
  262. }
  263. function compile_cookies() {
  264. $s = '';
  265. foreach((array)$this->a['cookies'] as $k => $v) {
  266. $s .= empty($s) ? $k.'='.$v : '; '.$k.'='.$v;
  267. }
  268. return $s;
  269. }
  270. function status() {
  271. $c = curl_getinfo($this->c,CURLINFO_HTTP_CODE);
  272. $this->o->set_code($c);
  273. $this->o->set_status($this->statii[$c]);
  274. }
  275. function parse() {
  276. $this->hsize = curl_getinfo($this->c,CURLINFO_HEADER_SIZE);
  277. $this->parse_head();
  278. $this->parse_body();
  279. }
  280. function parse_head() {
  281. $h = explode("\n",preg_replace("/\n[ \t]/",' ',str_replace("\r\n","\n",trim(substr($this->response,0,$this->hsize)))));
  282. foreach($h as $v) {
  283. if(empty($v)) {
  284. continue;
  285. }
  286. if(strpos($v,':') !== false) {
  287. $v = explode(':',$v,2);
  288. if(strtolower($v[0]) == 'set-cookie') {
  289. $this->parse_cookie($v[1]);
  290. }
  291. else {
  292. $this->o->set_header($v[0],trim($v[1]));
  293. }
  294. }
  295. }
  296. }
  297. function parse_cookie($c) {
  298. $c = explode(';',$c);
  299. $b = 0;
  300. $a = array();
  301. foreach($c as $v) {
  302. $v = explode('=',$v);
  303. $w = trim(urldecode($v[1]));
  304. $m = trim($v[0]);
  305. if(!$b) {
  306. $n = $m;
  307. $a['value'] = $w;
  308. }
  309. else {
  310. $a[$m] = $w;
  311. }
  312. $b++;
  313. }
  314. $this->o->set_cookie($n,$a);
  315. /*
  316. if($this->a['set_cookies']) {
  317. $e = $a['expires'] ? $a['expires'] : 0;
  318. $p = $a['path'] ? $a['path'] : '/';
  319. $d = $a['domain'] ? $a['domain'] : '';
  320. $s = $a['secure'] ? true : false;
  321. setcookie($n,$a['value'],$e,$p,'staging.ternstyle.us',$s);
  322. }
  323. */
  324. }
  325. function parse_body() {
  326. $this->o->set_body(substr($this->response,$this->hsize));
  327. }
  328. }
  329. class tern_curl_response {
  330. var $code = 0;
  331. var $status = '';
  332. var $headers = array();
  333. var $cookies = array();
  334. var $body = '';
  335. function set_code($c) {
  336. $this->code = $c;
  337. }
  338. function set_status($s) {
  339. $this->status = $s;
  340. }
  341. function set_header($k,$v) {
  342. $this->headers[$k] = $v;
  343. }
  344. function set_body($b) {
  345. $this->body = $b;
  346. }
  347. function set_cookie($k, $v) {
  348. $this->cookies[$k] = $v;
  349. }
  350. function get_cookie($n) {
  351. return $this->cookies[$n]['value'];
  352. }
  353. function get_cookies() {
  354. $a = array();
  355. foreach($this->cookies as $k => $v) {
  356. $a[$k] = $v['value'];
  357. }
  358. return $a;
  359. }
  360. }
  361. }
  362. /****************************************Terminate Script******************************************/
  363. ?>