forked from serphacker/serposcope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.php
266 lines (227 loc) · 7.81 KB
/
cron.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
<?php
/**
* Serposcope - An open source rank checker for SEO
* http://serphacker.com/serposcope/
*
* @link http://serphacker.com/serposcope Serposcope
* @author SERP Hacker <[email protected]>
* @license http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode CC-BY-NC-SA
*
* Redistributions of files must retain the above notice.
*/
// get max execution time before everything because we try to override it later
// and default value would be incorrect even if it's still used (suhoshin patch)
$defaultMaxExecutionTime = ini_get('max_execution_time');
include('inc/config.php');
include('inc/define.php');
include('inc/common.php');
include("lib/deathbycaptcha/deathbycaptcha.php");
if(php_sapi_name() != "cli"){
// don't abort the script on client disconnect
ignore_user_abort(true);
header('Content-type: text/plain');
}
$logs="";
$runid=null;
function my_ob_logs($str){
global $logs;
global $runid;
global $db;
$logs .= $str;
if(!$db->query("UPDATE `".SQL_PREFIX."run` SET logs = '".addslashes($logs)."' WHERE idRun = ".$runid)){
die("Critical sql error");
}
return $str;
}
// we will store the output in the database
ob_start('my_ob_logs',2);
// if another job is running, abort
$res=$db->query("SELECT * FROM `".SQL_PREFIX."run` WHERE ISNULL(dateStop)");
if($res && ($run=mysql_fetch_assoc($res))){
e('Cron',"Fatal, another job is running : PID = ".$run['pid'].", dateStart = ".$run['dateStart']);
e('Cron',"Abort the current running jobs via the \"Logs\" page");
die();
}
$res=$db->query("INSERT INTO `".SQL_PREFIX."run`(dateStart,pid) VALUES (now(),".getmypid().")");
if(!$res || ( $runid=mysql_insert_id())==0 ){
e('Cron',"Fatal, can't insert run in the database");
die();
}
l('Cron','Starting cron via "'.php_sapi_name().'"');
if(ini_get('safe_mode')){
e('Cron','safe_mode should be off, may trigger errors');
}
// we did override max_execution_time before, but it doesn't always work
// force the user to do it in php.ini
if($defaultMaxExecutionTime > 0){
e('Cron','max_execution_time is '.$defaultMaxExecutionTime.' it should be 0. All positions may not be checked. Edit your php.ini');
}
$urls = explode("\n", $options['general']['proxies_list_url']);
$prx_list = array();
foreach ($urls as $url) {
$url = trim($url);
if(empty($url)){
continue;
}
l('Cron',"Fetching proxies list from URL $url");
$added=0;
$prx_list_new = load_proxies_url($url);
foreach ($prx_list_new as $prx_new) {
if(!in_array($prx_new, $prx_list)){
++$added;
$prx_list[] = $prx_new;
}
}
l('Cron',"$added new proxies added");
}
shuffle($prx_list);
$urlproxies = array();
foreach ($prx_list as $prx_new) {
$proproxy = explode(":",$prx_new);
$urlproxies[] = array(
'type' => 'http',
'ip' => $proproxy[0],
'port' => $proproxy[1],
'user' => null,
'password' => null
);
}
$dbproxies = load_proxies();
// load the proxies
$proxies = new Proxies( $dbproxies + $urlproxies);
$argIdGroup=-1;
if(isset($_GET['idGroup'])){
$argIdGroup=intval($_GET['idGroup']);
}
if(isset($argv[1])){
$argIdGroup=intval($argv[1]);
}
$query = "SELECT * FROM `".SQL_PREFIX."group`"; //" WHERE idGroup = 10";
if($argIdGroup > 0){
$query .= " WHERE idGroup = ".$argIdGroup;
}else{
// shuffle
$query .= " ORDER BY rand()";
}
$currentUnit=0;
$totalUnit=0;
$allGroups = array();
$resGroup=$db->query($query);
while( $resGroup && ($row = @mysql_fetch_assoc($resGroup)) ){
if(!isset($modules[$row['module']])){
e('Cron',"Can't find ".$row['module']." for group ".$row['name']." (uninstalled ?)");
continue;
}
$keywords = array();
$qKW = "SELECT idKeyword,name FROM `".SQL_PREFIX."keyword` WHERE idGroup = ".intval($row['idGroup']);
// shuffle keywords
$qKW .= " ORDER BY rand()";
$result=$db->query($qKW);
while($kw= mysql_fetch_assoc($result)){
$keywords[$kw['idKeyword']] = $kw['name'];
}
if(empty($keywords)){
e('Cron',"No keywords for group ".$row['name']." ");
continue;
}
$row['keywords'] = $keywords;
$sites = array();
$qSite = "SELECT idTarget,name FROM `".SQL_PREFIX."target` WHERE idGroup = ".intval($row['idGroup']);
$result=$db->query($qSite);
while($site= mysql_fetch_assoc($result)){
$sites[$site['idTarget']] = $site['name'];
}
if(empty($sites)){
e('Cron',"No site for group ".$row['name']." ");
continue;
}
$group = array();
$group['id'] = $row['idGroup'];
$group['name'] = $row['name'];
$group['module'] = $row['module'];
$group['options'] = empty($row['options']) ? array() : json_decode($row['options'],true);
$group['keywords'] = $keywords;
$group['sites'] = $sites;
$allGroups[] = $group;
$totalUnit += $modules[$row['module']]->getTotalProgressBarUnit($group);
}
d('Cron','display total unit '.$totalUnit);
$dbc = null;
$captchaBrokenCurrentRun=0;
if(!empty($options['general']['dbc_user']) && !empty($options['general']['dbc_pass'])){
$balance = 0;
try {
$dbc = new DeathByCaptcha_SocketClient($options['general']['dbc_user'], $options['general']['dbc_pass']);
$balance = $dbc->get_balance();
}catch(Exception $ex){
$balance=0;
}
if($balance == 0){
$dbc = null;
e('Cron','DeathByCaptcha balance is to low');
}else{
l('Cron','DeathByCaptcha balance = '.$balance);
}
}else{
l('Cron','DeathByCaptcha not configured, skipping');
}
l('Cron','Clearing the cache (force='.strval(CACHE_RUN_CLEAR).')...');
clear_cache(CACHE_RUN_CLEAR);
l('Cron','Cache clear');
if(!file_exists(COOKIE_DIR)){
@mkdir(COOKIE_DIR);
}else{
clear_cookies();
}
foreach ($allGroups as $group) {
// if everything looks OK, let's go for a run
l('Cron','Checking group['.$group['id'].'] '.$group['name'].' with module '.$group['module']);
// save current total unit
$date = "NOW()";
//$date = "DATE_SUB(NOW(),INTERVAL ".$interval." DAY)";
$qRun = "INSERT INTO `".SQL_PREFIX."check`(idGroup,idRun,date) VALUES(".intval($group['id']).",".intval($runid).",".$date.")";
$db->query($qRun);
$id= mysql_insert_id();
$res = $modules[$group['module']]->check($group);
$currentUnit += $modules[$group['module']]->getTotalProgressBarUnit($group);
// update the progress bar
if($res === null || !is_array($res)){
e('Cron','Module returned fatal error for group['.$group['id'].'] '.$group['name']);
continue;
}else if(isset($res['__have_error'])){
e('Cron','Module have non fatal error for group['.$group['id'].'] '.$group['name'].'. All positions may have not been checked');
}else{
l('Cron','Checking done for group['.$group['id'].'] '.$group['name']);
}
unset($res['__have_error']);
$qRank = "INSERT INTO `".SQL_PREFIX."rank` VALUES ";
foreach ($res as $idKW => $sites) {
foreach ($sites as $idSite => $rank) {
$qRank .= " (".
intval($id).",".
intval($idSite).",".
intval($idKW).",".
intval($rank[0]).",".
"'".addslashes($rank[1])."'),";
}
}
$qRank = substr($qRank, 0, strlen($qRank)-1);
// e('DEBUG', $qRank);
$db->query($qRank);
}
l('Cron','All groups done');
if($dbc != null){
l('Cron','Broke a total of '.$captchaBrokenCurrentRun.' captchas');
}
l('Cron','Clearing the cache (force='.strval(CACHE_RUN_CLEAR).')...');
clear_cache(CACHE_RUN_CLEAR);
l('Cron','Cache clear');
ob_end_flush();
$db->query(
"UPDATE `".SQL_PREFIX."run` SET ".
"dateStop = NOW(), ".
"logs = '".addslashes($logs)."', ".
"haveError = ".($haveError ? 1 : 0)." ".
"WHERE idRun = ".$runid
);
?>