aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/SuperCurl.php
blob: fd47c399cf867b1328cdc739e3b9684431e68546 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?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 $cookies = 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 'magicauth':
						$this->magicauth = $v;
						\Zotlabs\Daemon\Master::Summon([ 'CurlAuth', $v ]);
						sleep(2);
						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,$c['channel_address'] . '@' . \App::get_hostname());
		}	
		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)); 
		
	}
		
	
}