-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreddit.php
262 lines (227 loc) · 7.55 KB
/
reddit.php
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* Reddit object types
*/
define('REDDIT_COMMENT', 1);
define('REDDIT_ACCOUNT', 2);
define('REDDIT_LINK', 3);
define('REDDIT_MESSAGE', 4);
define('REDDIT_SUBREDDIT', 5);
/**
* Reddit vote directions
*/
define('REDDIT_VOTE_UP', 1);
define('REDDIT_VOTE_RESCIND', 0);
define('REDDIT_VOTE_DOWN', -1);
class RedditBot {
private $_userName;
private $_password = false;
private $_hash;
private $_cookie;
private $_runCallback;
/**
* The data field for this bot
* @var string
*/
public $data;
/**
* Unix timestamp of the last time this bot was updated
* @var int
*/
public $lastUpdated;
/**
* @param mixed $ident The row ID or user name of the bot to load
* @param string $password The password to use for the bot in case one was not provided in the database
*/
public function __construct($ident, $password = false) {
$loaded = false;
// Attempt to load a previous session
if (is_int($ident)) {
$loaded = $this->_loadSessionById($ident);
} else {
$loaded = $this->_loadSessionByUserName($ident);
}
if ($loaded) {
$hasSession = strlen($this->_hash) > 0 && strlen($this->_cookie) > 0;
if (!$hasSession) {
$this->_password = false === $this->_password ? $password : $this->_password;
if (false !== $this->_password) {
$this->_login();
}
}
}
}
/**
* Logs the user in and saves the hash/cookie for later sessions
*/
private function _login() {
$retVal = false;
$obj = $this->_doAction('api/login/' . $this->_userName, array('user'=>$this->_userName, 'passwd'=>$this->_password, 'api_type'=>'json'));
if (is_string($obj)) {
$obj = json_decode($obj);
if (is_object($obj) && count($obj->json->errors) == 0) {
$obj = $obj->json;
$this->_hash = $obj->data->modhash;
$this->_cookie = $obj->data->cookie;
$this->_persist();
}
}
}
/**
* Submits a vote on a reddit object
* @param int $direction What kind of vote to submit (upvote, downvote, or rescind)
* @param string $id The ID of the object to post to
* @param string $type The type of reddit object to post to
* @return boolean Returns whether the vote was posted successfully
*/
public function Vote($direction, $id, $type) {
$retVal = false;
$obj = $this->_doAction('api/vote/', array('id'=>'t' . $type . '_' . $id, 'dir'=>$direction, 'uh'=>$this->_hash), $this->_cookie);
echo $obj;
if ($obj == '{}') {
$retVal = true;
}
return $retVal;
}
/**
* Posts a comment on a reddit object
* @param string $text The text body of the comment
* @param string $id The ID of the object to post to
* @param string $type The type of reddit object to post to
* @return boolean Returns whether the comment was posted successfully
*/
public function Comment($text, $id, $type) {
$retVal = false;
$obj = $this->_doAction('api/comment/', array('thing_id'=>'t' . $type . '_' . $id, 'text'=>$text, 'uh'=>$this->_hash), $this->_cookie);
if (is_string($obj)) {
$retVal = strpos($obj, 'contentHTML') !== false;
}
return $retVal;
}
/**
* Reports on a reddit object (e.g. comment, post, link)
* @param string $id The thing to report, e.g. t3_203xa
* @return boolean Returns whether the thing was reported successfully
*/
public function Report($id) {
$retVal = false;
$obj = $this->_doAction('api/report/', array('id'=>$id, 'uh'=>$this->_hash), $this->_cookie);
if ($obj == '{}') {
$retVal = true;
}
return $retVal;
}
/**
* Updated GetPageListing to be more generic, sends your cookie using cURL so you get your results, not the general result
* @param string $page The page to get. Ex: 'r/anime' returns the data object for the anime subreddit; '/message/inbox' returns the data object from your inbox
* @return object Returns the data object retrieved
*/
public function GetListing($page) {
$retVal = false;
$file = $this->_doAction($page,null,$this->_cookie);
if (strlen($file) > 0) {
$obj = json_decode($file);
if (is_object($obj) && is_array($obj->data->children)) {
$retVal = $obj->data->children;
}
}
return $retVal;
}
/**
* Checks to see if this bot has been logged in and has a valid hash and cookie
*/
public function HasSession() {
return is_object($this->_userObj) && is_string($this->_hash) && is_string($this->_cookie);
}
/**
* Runs the callback set for this bot
*/
public function Run() {
$retVal = false;
if (is_callable($this->_runCallback)) {
$retVal = call_user_func($this->_runCallback, $this);
}
return $retVal;
}
/**
* Saves this bot to the database
*/
public function Save() {
$this->_persist();
}
/**
* Loads a reddit bot's data by row ID
*/
private function _loadSessionById($id) {
$retVal = false;
$result = Db::Query('SELECT bot_name, bot_password, bot_hash, bot_cookie, bot_data, bot_updated, bot_callback FROM bot_users WHERE bot_id=:id LIMIT 1', array(':id'=>$id));
while ($row = Db::Fetch($result)) {
$this->_userName = $row->bot_name;
$this->_password = $row->bot_password;
$this->_hash = $row->bot_hash;
$this->_cookie = $row->bot_cookie;
$this->data = $row->bot_data;
$this->lastUpdated = $row->bot_updated;
$this->_runCallback = $row->bot_callback;
$retVal = true;
}
return $retVal;
}
/**
* Loads a reddit bot's data by username
*/
private function _loadSessionByUserName($userName) {
$retVal = false;
$result = Db::Query('SELECT bot_name, bot_password, bot_hash, bot_cookie, bot_data, bot_updated, bot_callback FROM bot_users WHERE bot_name=:name LIMIT 1', array(':name'=>$userName));
while ($row = Db::Fetch($result)) {
$this->_userName = $row->bot_name;
$this->_password = $row->bot_password;
$this->_userObj = new stdClass();
$this->_hash = $row->bot_hash;
$this->_cookie = $row->bot_cookie;
$this->data = $row->bot_data;
$this->lastUpdated = $row->bot_updated;
$this->_runCallback = $row->bot_callback;
$retVal = true;
}
return $retVal;
}
/**
* Saves the bot data back to the database. If the bot has not been created, it will create it.
*/
private function _persist() {
$retVal = false;
$result = Db::Query('SELECT bot_id FROM bot_users WHERE bot_name=:user', array(':user'=>$this->_userName));
if ($result->count > 0) {
$params = array(':hash'=>$this->_hash, ':cookie'=>$this->_cookie, ':name'=>$this->_userName, ':data'=>$this->data);
$retVal = Db::Query('UPDATE bot_users SET bot_updated=UNIX_TIMESTAMP(NOW()), bot_hash=:hash, bot_cookie=:cookie, bot_data=:data WHERE bot_name=:name', $params) > 0;
} else {
$retVal = Db::Query('INSERT INTO bot_users (bot_created, bot_name, bot_password, bot_cookie, bot_hash, bot_enabled) VALUES (UNIX_TIMESTAMP(NOW()), :user, :password, :cookie, :hash, 0)', array(':password'=>$this->_password, ':hash'=>$this->_hash, ':cookie'=>$this->_cookie, ':user'=>$this->_userName)) > 0;
}
}
/**
* Wrapper to perform an HTTP POST action to reddit. Requires the PHP cURL extension
*/
private function _doAction($url, $data = null, $cookie = false) {
$retVal = false;
$c = curl_init('http://www.reddit.com/'.$url.'.json');
if ($c) {
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
if (is_array($data)) {
curl_setopt($c, CURLOPT_POST, true);
$post = '';
foreach ($data as $key=>$value) {
$post .= $key . '=' . urlencode($value) . '&';
}
$post = substr($post, 0, strlen($post) - 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $post);
curl_setopt($c, CURLINFO_HEADER_OUT, true);
}
if (false !== $cookie) {
curl_setopt($c, CURLOPT_COOKIE, 'reddit_session=' . $cookie);
}
$retVal = curl_exec($c);
}
return $retVal;
}
}