-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflwdwn.php
314 lines (238 loc) · 11 KB
/
flwdwn.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
<?php
// for error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);
//memory limit set to necessary
ini_set('memory_limit', '-1');
//twitter library n namespace
require '/home/bme2kggy0iwu/public_html/twiproj/twitter/twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
//controller writes to cron.txt file cronjob to perform
require_once("controller.php");
//dompdf to create pdf file
include '/home/bme2kggy0iwu/public_html/twiproj/library/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
//uncomment this values while running through cron job
$path = $argv[0];
$format = $argv[1];
$int_cursor = $argv[2];
$screen_name = $argv[3];
$email = $argv[4];
//static values
// $path = '/home/bme2kggy0iwu/public_html/twiproj/flwdwn.php';
// $format='csv';
// $int_cursor =-1;
// $screen_name='khuntimd';
// $email = 'niraj.visana@gmail.com';
shell_exec("echo '$argv[0] $argv[1] $argv[2] $argv[3] $argv[4]' >> /home/bme2kggy0iwu/public_html/twiproj/argv.txt");
//shell_exec("sudo touch /var/www/html/rtcamp/tmp");
define("ID", "FollowerName");
define("VALUE", "FollowerScreenName");
define("FILE_NAME", $screen_name);
define("ROOT", "Follower");
$cursor = $int_cursor;
if (!isset($_SESSION['access_token']))
{
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback'=>OAUTH_CALLBACK));
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
}
else
{
$accesstoken = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $accesstoken['oauth_token'], $accesstoken['oauth_token_secret']);
}
if ($cursor != 0)
{
// $flwdwn=$connection->get('followers/list',["screen_name"=>$_SESSION['flwdwn'],"count"=>200,"cursor"=>$cursor]);
$td_t = array();
for ($i = 1; $cursor != 0; $i++) {
$flwdwn = $connection->get('followers/list', ["screen_name"=>$screen_name, "count"=>200, "cursor"=>$cursor]);
if (!isset($flwdwn->users))
{
shell_exec("echo 'inside first if' >> /home/bme2kggy0iwu/public_html/twiproj/argv.txt");
$reading = fopen('/home/bme2kggy0iwu/public_html/twiproj/cron.txt', 'r');
$writing = fopen('/home/bme2kggy0iwu/public_html/twiproj/cron.tmp', 'w');
$replaced = false;
while (!feof($reading)) {
$line = fgets($reading);
if (stristr($line, "*/15 * * * * /usr/local/bin/php /home/bme2kggy0iwu/public_html/twiproj/flwdwn.php $format $int_cursor $screen_name $email")) {
$line = "*/15 * * * * /usr/local/bin/php /home/bme2kggy0iwu/public_html/twiproj/flwdwn.php $format $cursor $screen_name $email \n";
$replaced = true;
}
fputs($writing, $line);
}
fclose($reading);
fclose($writing);
// might as well not overwrite the file if we didn't replace anything
if ($replaced)
{
rename(__DIR__.'/cron.tmp', __DIR__.'/cron.txt');
} else {
unlink(__DIR__.'/cron.tmp');
}
shell_exec("chmod 777 ".__DIR__.'/cron.txt');
//$cmd = "bash ".__DIR__."/cron.sh";
//shell_exec($cmd);
echo shell_exec('sh '.__DIR__.'/cron.sh');
break;
}
foreach ($flwdwn->users as $f) {
$tmp = new stdClass;
$tmp->id_str = $f->name;
$tmp->text = $f->screen_name;
array_push($td_t, [$tmp]);
}
$cursor = $flwdwn->next_cursor;
}
if ($format == "pdf")
{
// $file_name = "followers details -".$screen_name.".pdf";
$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
$html .= '<html xmlns="http://www.w3.org/1999/xhtml">';
$html .= '<head>';
$html .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
$html .= '<title>Twitter Followers</title>';
$html .= '<style>* { font-family: "DejaVu Sans","ariblk", "monospace","Times-Roman"; } @page { margin: 0em; } </style>';
$html .= '</head>';
$html .= '<body bgcolor="#ffffff">';
$html .= '<table id="Table_01" width="100%" border="1" cellpadding="0" cellspacing="0" bgcolor="#ffffff">';
$html .= '<tr>';
$html .= '<td colspan="4" style="text-align:center; padding:10px; font-weight:bold; font-size:16px;">followers list<br/></td>';
$html .= '</tr>';
$html .= '<tr>';
$html .= '<td style="padding:5px; text-align:left; font-weight:bold;">No</td><td style="padding:5px; text-align:left; font-weight:bold;">User Name</td><td style="padding:5px; text-align:left; font-weight:bold;">Screen Name</td>';
$html .= '</tr>';
$i = 0;
foreach ($td_t as $rows) {
foreach ($rows as $row) {
$i = $i + 1;
$html .= '<tr>';
$html .= '<td style="padding:5px; text-align:left; width:18%;">'.trim($i).'</td>';
$html .= '<td style="padding:5px; text-align:left; width:18%;">'.trim($row->id_str).'</td>';
$html .= '<td style="padding:5px; text-align:left; width:18%;">'.trim($row->text).'</td>';
$html .= '</tr>';
}
}
$html .= '</table>';
$html .= '</body>';
$html .= '</html>';
// $dompdf = new DOMPDF();
// $dompdf->load_html($html);
// $dompdf->set_paper('a4','portrait');
// $dompdf->render();
// $dompdf->stream($file_name);
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
//$dompdf->stream('testpdf',array('Attachment'=>0));
$output = $dompdf->output();
$file_to_save = __DIR__."/".$screen_name.'.'.$format;
file_put_contents($file_to_save, $output);
} else
{
$file = fopen(__DIR__."/".FILE_NAME.'.'.$format, 'a');
if ($int_cursor == -1)
{
fputcsv($file, array(ID, VALUE));
}
foreach ($td_t as $rows)
{
foreach ($rows as $row) {
fputcsv($file, array($row->id_str, $row->text));
}
}
fclose($file);
}
}
if ($cursor == 0)
{
require '/home/bme2kggy0iwu/public_html/twiproj/PHPMailer-master/src/Exception.php';
require '/home/bme2kggy0iwu/public_html/twiproj/PHPMailer-master/src/PHPMailer.php';
require '/home/bme2kggy0iwu/public_html/twiproj/PHPMailer-master/src/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsMail();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->IsHTML(true);
$mail->Username = "mail";
$mail->Password = "Password";
$mail->SetFrom('twiproj@visana.xyz', 'Twitter-data');
$mail->Subject = "Followers Data";
$mail->AltBody = "";
$mail->AddAddress($email);
$mail->MsgHTML("Your requested follower data is in file attached below");
if ($format == "pdf")
{
$mail->AddAttachment($file_to_save);
} else
{
$mail->AddAttachment(__DIR__."/".FILE_NAME.'.'.$format);
}
// if(!$mail->Send()) {
// echo "Mailer Error: " . $mail->ErrorInfo;
// shell_exec("echo 'failed to send mail' >> /home/bme2kggy0iwu/public_html/twiproj/argv.txt");
// // $output = exec('crontab -r');
// } else {
// shell_exec("echo 'sent mail' >> /home/bme2kggy0iwu/public_html/twiproj/argv.txt");
// echo "Message has been sent";
// // $output = exec('crontab -r');
// }
// $DELETE = stristr($line,"*/15 * * * * php $path $format $int_cursor $screen_name $email ");
// $data = file("__DIR__.'/cron.txt'");
// $out = array();
// foreach($data as $line) {
// if(trim($line) != $DELETE) {
// $out[] = $line;
// }
// }
// $fp = fopen("__DIR__.'/cron.txt'", "w+");
// flock($fp, LOCK_EX);
// foreach($out as $line) {
// fwrite($fp, $line);
// }
// flock($fp, LOCK_UN);
// fclose($fp);
if ($mail->Send()) {
$reading = fopen('/home/bme2kggy0iwu/public_html/twiproj/cron.txt', 'r');
$writing = fopen('/home/bme2kggy0iwu/public_html/twiproj/cron.tmp', 'w');
$str = "*/15 * * * * /usr/local/bin/php /home/bme2kggy0iwu/public_html/twiproj/flwdwn.php ".$format." -1 ".$screen_name." ".$email." \n";
$replaced = false;
while (!feof($reading)) {
$line = fgets($reading);
if (stristr($line, "*/15 * * * * /usr/local/bin/php /home/bme2kggy0iwu/public_html/twiproj/flwdwn.php $format $int_cursor $screen_name $email")) {
shell_exec("echo 'inside if' >> /home/bme2kggy0iwu/public_html/twiproj/argv.txt");
$line = "";
$replaced = true;
}
shell_exec("echo 'line '.$line >> /home/bme2kggy0iwu/public_html/twiproj/argv.txt ");
shell_exec("echo ' replaced '.$replaced >> /home/bme2kggy0iwu/public_html/twiproj/argv.txt ");
shell_exec("echo $str >> /home/bme2kggy0iwu/public_html/twiproj/argv.txt ");
fputs($writing, $line);
}
fclose($reading);
fclose($writing);
// might as well not overwrite the file if we didn't replace anything
if ($replaced)
{
rename(__DIR__.'/cron.tmp', __DIR__.'/cron.txt');
} else
{
unlink(__DIR__.'/cron.tmp');
}
//shell_exec("chmod 777 ".__DIR__.'/cron.txt');
echo shell_exec('sh '.__DIR__.'/cron.sh');
unlink(__DIR__."/".FILE_NAME.'.'.$format);
}
else {
echo "Error while sending mail : ".$mail->ErrorInfo;
}
}