-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.YabsHandler.php
More file actions
71 lines (58 loc) · 1.83 KB
/
class.YabsHandler.php
File metadata and controls
71 lines (58 loc) · 1.83 KB
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
<?php
class YabsHandler
{
private static $posts = array();
static public function get_posts($username, $tags = null, $page = 0, $limit = 100)
{
$key = $username.'-'.$tags.'-'.$page.'-'.$limit;
if(isset(self::$posts[$key]))
return self::$posts[$key];
$p = incached::load($key);
if($p['cache'])
self::$posts[$key] = $p['cache'];
if(empty(self::$posts[$key]))
{
$request = "https://www.yabs.io/a/view.php?user=$username&tags=$tags&page=$page&limit=$limit&r=json";
self::$posts[$key] = @file_get_contents($request);
if(!self::$posts[$key])
self::$posts[$key] = $p['org'];
else
{
self::$posts[$key] = json_decode(self::$posts[$key], true);
incached::save($key, self::$posts[$key]);
}
}
return (array)self::$posts[$key];
}
}
class incached
{
static private function name($uid)
{
return 'incached_'.$uid;
}
static function load($uid, $minutes = 60)
{
$rv = array('cache'=>null, 'org'=>null);
$filename = self::name($uid);
if(!file_exists($filename))
return $rv;
//echo time().'-'.filemtime($filename).'='.time() - filemtime($filename);
$rv['org'] = unserialize(file_get_contents($filename));
if(time() - filemtime($filename) < ($minutes * 60))
$rv['cache'] = $rv['org'];
return $rv;
}
static function save($uid, $data)
{
$filename = self::name($uid);
file_put_contents($filename, serialize($data));
}
static function load_direct($uid)
{
$filename = self::name($uid);
if(!file_exists($filename))
return null;
return unserialize(file_get_contents($filename));
}
}