diff options
author | redmatrix <git@macgirvin.com> | 2016-06-23 20:05:57 -0700 |
---|---|---|
committer | redmatrix <git@macgirvin.com> | 2016-06-23 20:05:57 -0700 |
commit | fa02a091072e74a2468cfc34f9e38ce1c9ea5196 (patch) | |
tree | 7393dbb37eb8853b9cc661d44288b24e9b5af068 /Zotlabs/Lib | |
parent | 51e2ef39c221a6f8cd89f8bb9e85a8f374f1fd6c (diff) | |
download | volse-hubzilla-fa02a091072e74a2468cfc34f9e38ce1c9ea5196.tar.gz volse-hubzilla-fa02a091072e74a2468cfc34f9e38ce1c9ea5196.tar.bz2 volse-hubzilla-fa02a091072e74a2468cfc34f9e38ce1c9ea5196.zip |
SuperCurl to provide a re-usable curl options stack and just change options that are different from one call to the next
Diffstat (limited to 'Zotlabs/Lib')
-rw-r--r-- | Zotlabs/Lib/SuperCurl.php | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/Zotlabs/Lib/SuperCurl.php b/Zotlabs/Lib/SuperCurl.php new file mode 100644 index 000000000..40ca1addb --- /dev/null +++ b/Zotlabs/Lib/SuperCurl.php @@ -0,0 +1,112 @@ +<?php + +namespace Zotlabs\Lib; + +/** + * @brief wrapper for z_fetch_url() which can be instantiated with several built-in parameters and + * these can be modified and re-used. Useful for CalDAV and other processes which need to authenticate + * and set lots of CURL options (many of which stay the same from one call to the next). + */ + + + + +class SuperCurl { + + + private $auth; + private $url; + + private $curlopt = array(); + + private $headers = null; + public $filepos = 0; + public $filehandle = 0; + public $request_data = ''; + + private $request_method = 'GET'; + private $upload = false; + + + private function set_data($s) { + $this->request_data = $s; + $this->filepos = 0; + } + + public function curl_read($ch,$fh,$size) { + + if($this->filepos < 0) { + unset($fh); + return ''; + } + + $s = substr($this->request_data,$this->filepos,$size); + + if(strlen($s) < $size) + $this->filepos = (-1); + else + $this->filepos = $this->filepos + $size; + + return $s; + } + + + public function __construct($opts = array()) { + $this->set($opts); + } + + private function set($opts = array()) { + if($opts) { + foreach($opts as $k => $v) { + switch($k) { + case 'http_auth': + $this->auth = $v; + break; + case 'custom': + $this->request_method = $v; + break; + case 'url': + $this->url = $v; + break; + case 'data': + $this->set_data($v); + if($v) { + $this->upload = true; + } + else { + $this->upload = false; + } + break; + case 'headers': + $this->headers = $v; + break; + default: + $this->curlopts[$k] = $v; + break; + } + } + } + } + + function exec() { + $opts = $this->curlopts; + if($this->auth) + $opts['http_auth'] = $this->auth; + if($this->custom) + $opts['custom'] = $this->custom; + if($this->headers) + $opts['headers'] = $this->headers; + if($this->upload) { + $opts['upload'] = true; + $opts['infile'] = $this->filehandle; + $opts['infilesize'] = strlen($this->request_data); + $opts['readfunc'] = [ $this, 'curl_read' ] ; + } + + $recurse = 0; + return z_fetch_url($this->url,true,$recurse,(($opts) ? $opts : null)); + + } + + +} |