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 'magicauth': // currently experimental $this->magicauth = $v; \Zotlabs\Daemon\Master::Summon([ 'CurlAuth', $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; $url = $this->url; if($this->auth) $opts['http_auth'] = $this->auth; if($this->magicauth) { $opts['cookiejar'] = 'store/[data]/cookie_' . $this->magicauth; $opts['cookiefile'] = 'store/[data]/cookie_' . $this->magicauth; $opts['cookie'] = 'PHPSESSID=' . trim(file_get_contents('store/[data]/cookien_' . $this->magicauth)); $c = channelx_by_n($this->magicauth); if($c) $url = zid($this->url,channel_reddress($c)); } 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)); } }