-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWeb.php
316 lines (267 loc) · 9.7 KB
/
Web.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* Author: Kyle Thielk (www.kylethielk.com)
* License:
* Copyright (c) 2013 Kyle Thielk
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
require_once(dirname(__FILE__) . '/request/QueueAdd.php');
require_once(dirname(__FILE__) . '/request/QueueFetch.php');
require_once(dirname(__FILE__) . '/request/UnFollowUsersFetch.php');
require_once(dirname(__FILE__) . '/request/UnFollow.php');
require_once(dirname(__FILE__) . '/request/Filter.php');
require_once(dirname(__FILE__) . '/request/Follow.php');
require_once(dirname(__FILE__) . '/WebResponse.php');
require_once(dirname(__FILE__) . '/driver/Filter.php');
require_once(dirname(__FILE__) . '/driver/Follow.php');
require_once(dirname(__FILE__) . '/driver/Queue.php');
require_once(dirname(__FILE__) . '/driver/UnFollow.php');
/**
* Executes AJAX requests from html file.
* Class FTF_Web
*/
class FTF_Web
{
const ACTION_RUN = 'run';
const ACTION_FOLLOW = 'follow';
const ACTION_UNFOLLOW = 'unfollow';
const ACTION_ADD_QUEUE = 'addqueue';
const ACTION_FETCH_QUEUE = 'fetchqueue';
const ACTION_FETCH_USERS_TO_UNFOLLOW = 'fetchunfollowusers';
const ACTION_SWITCH_USER = 'switchuser';
/**
* @var FTF_Driver_Base
*/
static public $currentDriver = null;
/**
* Execute the ajax request.
* @param array $data $_POST data.
*/
public static function executeRequest($data)
{
if (!isset($data) || !isset($data['action']))
{
return;
} else if (!FTF_Web::validateConfig())
{
FTF_Web::writeErrorResponse("Config.php has not been setup or configured properly.");
} else if (!FTF_Web::validateAuthenticated())
{
FTF_Web::writeErrorResponse("No user currently authorized to run application. Something went very wrong.");
}
$action = $data['action'];
if ($action == FTF_Web::ACTION_RUN)
{
FTF_Web::filterFollowers($data);
} else if ($action == FTF_Web::ACTION_FOLLOW)
{
FTF_Web::followUser($data);
} else if ($action == FTF_Web::ACTION_UNFOLLOW)
{
FTF_Web::unFollowUser($data);
} else if ($action == FTF_Web::ACTION_ADD_QUEUE)
{
FTF_Web::addToQueue($data);
} else if ($action == FTF_Web::ACTION_FETCH_QUEUE)
{
FTF_Web::fetchQueue($data);
} else if ($action == FTF_Web::ACTION_FETCH_USERS_TO_UNFOLLOW)
{
FTF_Web::fetchUnFollowUsers();
} else if ($action == FTF_Web::ACTION_SWITCH_USER)
{
FTF_Web::switchUser();
}
}
public static function switchUser()
{
FTF_UserData::getUserData()->clearCurrentUser();
FTF_Web::writeValidResponse("", "");
}
/**
* Process request to fetch queue.
*/
public static function fetchUnFollowUsers()
{
$request = new FTF_Request_UnFollowUsersFetch();
if (!isset($request) || !$request->validate())
{
FTF_Web::writeErrorResponse('Queue Request invalid or not supplied.' . print_r($request, true));
}
$queue = new FTF_Driver_UnFollow(FTF_Config::$apiKeys);
FTF_Web::writeValidResponse($queue->generateHtmlForUsers(), "");
}
/**
* Process request to fetch queue.
* @param array $data $_POST
*/
public static function fetchQueue($data)
{
$request = new FTF_Request_QueueFetch($data);
if (!isset($request) || !$request->validate())
{
FTF_Web::writeErrorResponse('Queue Request invalid or not supplied.' . print_r($request, true));
}
$queue = new FTF_Driver_Queue($request);
FTF_Web::writeValidResponse($queue->generateHtmlForQueue(), "");
}
/**
* Process request to add users to queue.
* @param array $data $_POST
*/
public static function addToQueue($data)
{
$request = new FTF_Request_QueueAdd($data);
if (!isset($request) || !$request->validate())
{
FTF_Web::writeErrorResponse('Queue Request invalid or not supplied.' . print_r($request, true));
}
$queue = new FTF_Driver_Queue($request);
$queue->pushUserIdsToQueue();
FTF_Web::writeValidResponse("", "");
}
/**
* Executes the web request to unfollow an user.
* @param array $data $_POST data.
*/
private static function unFollowUser($data)
{
$request = new FTF_Request_UnFollow($data);
if (!isset($request) || !$request->validate())
{
FTF_Web::writeErrorResponse('UnFollow Request invalid or not supplied.' . print_r($request, true));
}
$follow = new FTF_Driver_UnFollow(FTF_Config::$apiKeys, $request);
FTF_Web::$currentDriver = $follow;
$follow->unFollowUser();
}
/**
* Executes the web request to follow an user.
* @param array $data $_POST data.
*/
private static function followUser($data)
{
$request = new FTF_Request_Follow($data);
if (!isset($request) || !$request->validate())
{
FTF_Web::writeErrorResponse('Follow Request invalid or not supplied.' . print_r($request, true));
}
$follow = new FTF_Driver_Follow(FTF_Config::$apiKeys, $request);
FTF_Web::$currentDriver = $follow;
$follow->followUser();
}
/**
* Start the process and find filtered users to follow.
* @param array $data $_POST data.
*/
private static function filterFollowers($data)
{
$settings = new FTF_Request_Filter($data);
if (!isset($settings) || !$settings->validate())
{
FTF_Web::writeErrorResponse('Settings invalid or not supplied.' . print_r($settings, true));
} else
{
$findToFollow = new FTF_Driver_Filter(FTF_Config::$apiKeys, $settings);
FTF_Web::$currentDriver = $findToFollow;
set_error_handler(array('FTF_Web', 'errorHandler'));
$findToFollow->buildFriendIds();
$findToFollow->buildFollowerIds();
$findToFollow->buildFilteredFollowers();
FTF_Web::writeValidResponse($findToFollow->generateHtml(), $findToFollow->generateLog());
}
}
/**
* Write response to browser.
* @param $html String The html to write.
* @param $log String a string for the log.
*/
public static function writeValidResponse($html, $log)
{
$response = new FTF_WebResponse();
$response->hasError = false;
$response->errorMessage = '';
$response->html = $html;
$response->log = $log;
echo json_encode($response);
exit(3);
}
/**
* Write response to browser and mark it as having an error.
* @param $errorMessage String The error message to write.
* @param $log String Optional.
* @param $continue boolean Set to true if the error should not be considered fatal by the UI and progress
* can resume. Optional.
*
*/
public static function writeErrorResponse($errorMessage, $log = '', $continue = false)
{
$response = new FTF_WebResponse();
$response->hasError = true;
$response->errorMessage = $errorMessage;
$response->log = $log;
$response->continue = $continue;
echo json_encode($response);
exit(3);
}
/**
* Our custom PHP error handler.
* @param $errno
* @param $errstr
* @param $errfile
* @param $errline
* @return bool
*/
public static function errorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno))
{
// This error code is not included in error_reporting
return false;
}
FTF_Web::writeErrorResponse('Unknown error happened. Error Message [' . $errstr . '] at ' . $errfile . ' (' . $errline . ')', FTF_Web::$currentDriver->generateLog());
/* Don't execute PHP internal error handler */
return true;
}
/**
* Validates our config has been setup.
* @return bool True if valid, false otherwise.
*/
public static function validateConfig()
{
if (!class_exists('FTF_Config'))
{
return false;
} else if (empty(FTF_Config::$twitterUsername) || FTF_Config::$twitterUsername == 'CHANGE_THIS')
{
return false;
} else if (FTF_Config::$apiKeys['consumer_key'] == 'CHANGE_THIS'
|| FTF_Config::$apiKeys['consumer_secret'] == 'CHANGE_THIS'
)
{
return false;
}
return true;
}
public static function validateAuthenticated()
{
return FTF_TwitterUser::validateUser(FTF_UserData::getUserData()->currentUser());
}
}
?>