diff options
Diffstat (limited to 'Zotlabs/Lib')
-rw-r--r-- | Zotlabs/Lib/AbConfig.php | 32 | ||||
-rw-r--r-- | Zotlabs/Lib/Cache.php | 46 | ||||
-rw-r--r-- | Zotlabs/Lib/SuperCurl.php | 112 |
3 files changed, 174 insertions, 16 deletions
diff --git a/Zotlabs/Lib/AbConfig.php b/Zotlabs/Lib/AbConfig.php index f2d6522b9..138d0dfea 100644 --- a/Zotlabs/Lib/AbConfig.php +++ b/Zotlabs/Lib/AbConfig.php @@ -5,18 +5,18 @@ namespace Zotlabs\Lib; class AbConfig { - static public function Load($chash,$xhash) { - $r = q("select * from abconfig where chan = '%s' and xchan = '%s'", - dbesc($chash), + static public function Load($chan,$xhash) { + $r = q("select * from abconfig where chan = %d and xchan = '%s'", + intval($chan), dbesc($xhash) ); return $r; } - static public function Get($chash,$xhash,$family,$key) { - $r = q("select * from abconfig where chan = '%s' and xchan = '%s' and cat = '%s' and k = '%s' limit 1", - dbesc($chash), + static public function Get($chan,$xhash,$family,$key) { + $r = q("select * from abconfig where chan = %d and xchan = '%s' and cat = '%s' and k = '%s' limit 1", + intval($chan), dbesc($xhash), dbesc($family), dbesc($key) @@ -28,14 +28,14 @@ class AbConfig { } - static public function Set($chash,$xhash,$family,$key,$value) { + static public function Set($chan,$xhash,$family,$key,$value) { $dbvalue = ((is_array($value)) ? serialize($value) : $value); $dbvalue = ((is_bool($dbvalue)) ? intval($dbvalue) : $dbvalue); - if(self::Get($chash,$xhash,$family,$key) === false) { - $r = q("insert into abconfig ( chan, xchan, cat, k, v ) values ( '%s', '%s', '%s', '%s', '%s' ) ", - dbesc($chash), + if(self::Get($chan,$xhash,$family,$key) === false) { + $r = q("insert into abconfig ( chan, xchan, cat, k, v ) values ( %d, '%s', '%s', '%s', '%s' ) ", + intval($chan), dbesc($xhash), dbesc($family), dbesc($key), @@ -43,9 +43,9 @@ class AbConfig { ); } else { - $r = q("update abconfig set v = '%s' where chan = '%s' and xchan = '%s' and cat = '%s' and k = '%s' ", + $r = q("update abconfig set v = '%s' where chan = %d and xchan = '%s' and cat = '%s' and k = '%s' ", dbesc($dbvalue), - dbesc($chash), + dbesc($chan), dbesc($xhash), dbesc($family), dbesc($key) @@ -58,10 +58,10 @@ class AbConfig { } - static public function Delete($chash,$xhash,$family,$key) { + static public function Delete($chan,$xhash,$family,$key) { - $r = q("delete from abconfig where chan = '%s' and xchan = '%s' and cat = '%s' and k = '%s' ", - dbesc($chash), + $r = q("delete from abconfig where chan = %d and xchan = '%s' and cat = '%s' and k = '%s' ", + intval($chan), dbesc($xhash), dbesc($family), dbesc($key) @@ -70,4 +70,4 @@ class AbConfig { return $r; } -}
\ No newline at end of file +} diff --git a/Zotlabs/Lib/Cache.php b/Zotlabs/Lib/Cache.php new file mode 100644 index 000000000..35c8f56ad --- /dev/null +++ b/Zotlabs/Lib/Cache.php @@ -0,0 +1,46 @@ +<?php /** @file */ + +namespace Zotlabs\Lib; + + /** + * cache api + */ + +class Cache { + public static function get($key) { + $r = q("SELECT v FROM cache WHERE k = '%s' limit 1", + dbesc($key) + ); + + if ($r) + return $r[0]['v']; + return null; + } + + public static function set($key,$value) { + + $r = q("SELECT * FROM cache WHERE k = '%s' limit 1", + dbesc($key) + ); + if($r) { + q("UPDATE cache SET v = '%s', updated = '%s' WHERE k = '%s'", + dbesc($value), + dbesc(datetime_convert()), + dbesc($key)); + } + else { + q("INSERT INTO cache ( k, v, updated) VALUES ('%s','%s','%s')", + dbesc($key), + dbesc($value), + dbesc(datetime_convert())); + } + } + + + public static function clear() { + q("DELETE FROM cache WHERE updated < '%s'", + dbesc(datetime_convert('UTC','UTC',"now - 30 days"))); + } + +} + 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)); + + } + + +} |