-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.reddit.php
More file actions
197 lines (163 loc) · 4.45 KB
/
class.reddit.php
File metadata and controls
197 lines (163 loc) · 4.45 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
class reddit {
private $subreddit = array();
private $username = false;
private $password = false;
private $img = false;
public $fileType = array();
public $expiration = false;
public $mininumUps = false;
public $author = false;
public $minimumWidth = false;
public $minimumHeight = false;
public $maximumWidth = false;
public $maximumHeight = false;
public $minumumFilesize = false;
public $maximumFilesize = false;
public $saveDir = false;
public $tempDir = false;
public $data = array();
private $prefix = 'https://www.reddit.com/r/';
private $suffix = '/new.json?sort=new';
public function __construct ($subreddit = false, $username = false, $password = false)
{
date_default_timezone_set('Atlantic/Reykjavik');
$this->tempDir = sys_get_temp_dir();
$this->setSubreddit($subreddit);
$this->username = $username;
$this->password = $password;
}
public function setSubreddit ($subreddit)
{
if (is_array($subreddit)) {
foreach ($subreddit as $sr) {
$this->subreddit[] = $sr;
}
} else if ($subreddit) {
$this->subreddit[] = $subreddit;
}
}
public function scanSubreddits ()
{
foreach ($this->subreddit as $sr) {
if (!in_array($sr, $this->data)) {
$data = json_decode($this->_download($this->prefix.$sr.$this->suffix));
if (isset($data->data)) {
foreach ($data->data->children as $row) {
$this->data[$sr][] = $row->data;
}
}
}
}
}
public function validate ($row)
{
// Correct extension
if ($this->fileType) {
$img_name_parts = explode('.', $row->url);
$img_name_extension = end($img_name_parts);
if (!in_array($img_name_extension, $this->fileType)) {
return false;
}
}
// Minimum ups requirement met
if ($this->mininumUps) {
if ($row->ups < $this->mininumUps) {
return false;
}
}
// Only this author
if ($this->author) {
if ($row->author != $this->author) {
return false;
}
}
// File does not exist already
$img = $this->_download($row->url);
$img_md5 = md5($img);
$save_path = $this->saveDir .'/'. $img_md5 .'.'. $img_name_extension;
$temp_path = $this->tempDir .'/'. $img_md5 .'.'. $img_name_extension;
if (file_exists($save_path)) {
return false;
} else {
$this->img = $img_md5 .'.'. $img_name_extension;
file_put_contents($temp_path, $img);
if (!$this->_validateSize($temp_path)) {
unlink($temp_path);
return false;
}
}
return true;
}
private function _validateSize ($file)
{
if ($this->minimumWidth || $this->minimumHeight || $this->maximumWidth || $this->maximumHeight) {
list($width, $height) = getimagesize($file);
if ($this->minimumWidth && $this->minimumWidth > $width) {
return false;
}
if ($this->minimumHeight && $this->minimumHeight > $height) {
return false;
}
if ($this->maximumWidth && $this->maximumWidth < $width) {
return false;
}
if ($this->maximumHeight && $this->maximumHeight < $height) {
return false;
}
}
if ($this->minimumFilesize || $this->maximumFilesize) {
$size = filesize($file);
if ($this->minimumFilesize && $this->minimumFilesize > $size) {
return false;
}
if ($this->maximumFilesize && $this->maximumFilesize < $size) {
return false;
}
}
return true;
}
public function saveWallpaper ($keep)
{
if ($this->img) {
if ($keep) {
rename($this->tempDir .'/'. $this->img, $this->saveDir .'/'. $this->img);
} else if (file_exists($this->tempDir .'/'. $this->img)) {
unlink($this->tempDir .'/'. $this->img);
}
$this->img = false;
} else {
return false;
}
return true;
}
public function deleteExpiredWallpaper ()
{
if ($this->expiration) {
foreach (scandir($this->saveDir) as $file) {
if (strtotime('-'. $this->expiration) > filemtime($this->saveDir .'/'. $file)) {
unlink($this->saveDir .'/'. $file);
}
}
} else {
return false;
}
return true;
}
private function _download ($url)
{
// Fix spaces
$url = str_replace(' ', '%20', $url);
if (function_exists('curl_version')) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$return = curl_exec($ch);
curl_close($ch);
return $return;
} else {
return file_get_contents($url);
}
}
}