Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
 
3
/***************************************************************************
4
 *   Copyright (C) 2009-2011 by Geo Varghese(www.seopanel.in)  	   		   *
5
 *   sendtogeo@gmail.com   												   *
6
 *                                                                         *
7
 *   This program is free software; you can redistribute it and/or modify  *
8
 *   it under the terms of the GNU General Public License as published by  *
9
 *   the Free Software Foundation; either version 2 of the License, or     *
10
 *   (at your option) any later version.                                   *
11
 *                                                                         *
12
 *   This program is distributed in the hope that it will be useful,       *
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 *   GNU General Public License for more details.                          *
16
 *                                                                         *
17
 *   You should have received a copy of the GNU General Public License     *
18
 *   along with this program; if not, write to the                         *
19
 *   Free Software Foundation, Inc.,                                       *
20
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21
 ***************************************************************************/
22
 
23
# class defines all site auditor controller functions
24
class SiteAuditorController extends Controller{
25
 
26
    var $cron = false;                    // to identify whether it is executed through cron
27
    var $seArr = array('google', 'bing'); // the array contains search engines
28
 
29
	function showAuditorProjects($info="") {
30
		$info['userid'] = intval($info['userid']);
31
	    $userId = isLoggedIn();
32
		if(isAdmin()){
33
			$sql = "select ap.*,w.name,u.username from websites w,users u,auditorprojects ap where ap.website_id=w.id and u.id=w.user_id";
34
			$sql .= empty($info['userid']) ? "" : " and w.user_id=".$info['userid'];
35
			$sql .= " order by ap.score DESC,ap.id";
36
			$this->set('isAdmin', 1);
37
 
38
			$userCtrler = New UserController();
39
			$userList = $userCtrler->__getAllUsers();
40
			$this->set('userList', $userList);
41
 
42
		}else{
43
			$sql = "select w.name,ap.* from websites w, auditorprojects ap where ap.website_id=w.id and user_id=$userId order by ap.id";
44
		}
45
		$this->set('userId', empty($info['userid']) ? 0 : $info['userid']);
46
 
47
		# pagination setup
48
		$this->db->query($sql, true);
49
		$this->paging->setDivClass('pagingdiv');
50
		$this->paging->loadPaging($this->db->noRows, SP_PAGINGNO);
51
		$pagingDiv = $this->paging->printPages('siteauditor.php?userid='.$info['userid']);
52
		$this->set('pagingDiv', $pagingDiv);
53
		$sql .= " limit ".$this->paging->start .",". $this->paging->per_page;
54
 
55
		$projectList = $this->db->select($sql);
56
		foreach ($projectList as $i => $projectInfo) {
57
		    $projectList[$i]['total_links'] = $this->getCountcrawledLinks($projectInfo['id']);
58
		    $projectList[$i]['crawled_links'] = $this->getCountcrawledLinks($projectInfo['id'], true);
59
		    $projectList[$i]['last_updated'] = $this->getProjectLastUpdate($projectInfo['id']);
60
		}
61
		$this->set('pageNo', $info['pageno']);
62
		$this->set('list', $projectList);
63
		$this->render('siteauditor/list');
64
	}
65
 
66
	// func to create new project
67
	function newProject($info=''){
68
 
69
		$userId = isLoggedIn();
70
		$websiteController = New WebsiteController();
71
		$websiteList = $websiteController->__getAllWebsites($userId, true);
72
		$this->set('websiteList', $websiteList);
73
		$websiteId = empty($info['website_id']) ? $websiteList[0]['id'] : intval($info['website_id']);
74
		$this->set('websiteId', $websiteId);
75
 
76
		if (!isset($info['website_id'])) {
77
		    $info['max_links'] = SA_MAX_NO_PAGES;
78
		    $info['cron'] = 1;
79
		    $this->set('post', $info);
80
		}
81
 
82
		$this->render('siteauditor/new');
83
	}
84
 
85
	// func to create project
86
	function createProject($listInfo){
87
 
88
		$userId = isLoggedIn();
89
		$this->set('post', $listInfo);
90
		$listInfo['website_id'] = intval($listInfo['website_id']);
91
		$listInfo['max_links'] = intval($listInfo['max_links']);
92
 
93
		$errMsg['website_id'] = formatErrorMsg($this->validate->checkBlank($listInfo['website_id']));
94
		$errMsg['max_links'] = formatErrorMsg($this->validate->checkNumber($listInfo['max_links']));
95
		if(!$this->validate->flagErr){
96
		    $errorFlag = 0;
97
		    if ($listInfo['max_links'] > SA_MAX_NO_PAGES) {
98
		        $errorFlag = 1;
99
		        $errMsg['max_links'] = formatErrorMsg($this->spTextSA["Number of pages is greater than"]. " ". SA_MAX_NO_PAGES);
100
		    }
101
 
102
		    if ($listInfo['max_links'] <= 0) {
103
		        $errorFlag = 1;
104
		        $errMsg['max_links'] = formatErrorMsg($this->spTextSA["Number of pages should be greater than"]. " 0");
105
		    }
106
 
107
		    $webCtrler = New WebsiteController();
108
		    $websiteInfo = $webCtrler->__getWebsiteInfo($listInfo['website_id']);
109
 
110
		    // commented to exclude site auditor with query arguments also by with out adding fulll url
111
		    /*$excludeInfo = $this->checkExcludeLinks($listInfo['exclude_links'], $websiteInfo['url']);
112
		    if (!empty($excludeInfo['err_msg'])) {
113
		        $errorFlag = 1;
114
		        $errMsg['exclude_links'] = formatErrorMsg($excludeInfo['err_msg']);
115
		    }
116
		    $listInfo['exclude_links'] = $excludeInfo['exclude_links'];*/
117
 
118
		    // check for max links allowed for the account type
119
		    $maxValidInfo = $this->validateMaxLinkCount($websiteInfo['user_id'], $listInfo['max_links']);
120
		    if ($maxValidInfo['error']) {
121
		        $errorFlag = 1;
122
		        $errMsg['max_links'] = $maxValidInfo['msg'];
123
		    }
124
 
125
		    if (!$errorFlag) {
126
    			if (!$this->isProjectExists($listInfo['website_id'])) {
127
    				$sql = "insert into auditorprojects(website_id,max_links,exclude_links,check_pr,check_backlinks,check_indexed,store_links_in_page,check_brocken,cron)
128
							values({$listInfo['website_id']},{$listInfo['max_links']},'".addslashes($listInfo['exclude_links'])."',
129
							".intval($listInfo['check_pr']).",".intval($listInfo['check_backlinks']).",".intval($listInfo['check_indexed']).",
130
							".intval($listInfo['store_links_in_page']).",".intval($listInfo['check_brocken']).",".intval($listInfo['cron']).")";
131
    				$this->db->query($sql);
132
    				$this->showAuditorProjects();
133
    				exit;
134
    			}else{
135
    				$errMsg['website_id'] = formatErrorMsg($this->spTextSA['projectalreadyexist']);
136
    			}
137
		    }
138
		}
139
		$this->set('errMsg', $errMsg);
140
		$this->newProject($listInfo);
141
	}
142
 
143
	// check entered excluded list
144
	function checkExcludeLinks($excludeLinks, $websiteUrl, $exclude=true) {
145
	    $linkList = explode(',', $excludeLinks);
146
	    $newList = array();
147
	    $errMsg = "";
148
	    $checkUrl = formatUrl($websiteUrl);
149
	    foreach ($linkList as $link) {
150
	        $link = str_replace(' ', '+', trim($link));
151
	        $linkUrl = formatUrl($link);
152
	        if (!empty($linkUrl)) {
153
	            $newList[] = $link;
154
	            if (empty($errMsg)) {
155
	                if (!preg_match("/^".preg_quote($checkUrl, '/')."/", $linkUrl)) {
156
	                    $linkLabel = $exclude ? $_SESSION['text']['label']["Exclude"] : $_SESSION['text']['label']["Include"];
157
	                    $errMsg = "$linkLabel link <b>'$link'</b> {$this->spTextSA['should start with']} <b>'$websiteUrl'</b>";
158
	                }
159
	            }
160
	        }
161
	    }
162
	    $excludeLinks = implode(',', $newList);
163
	    $retInfo['exclude_links'] = $excludeLinks;
164
	    $retInfo['err_msg'] = $errMsg;
165
	    return $retInfo;
166
	}
167
 
168
	// function check project already exists
169
	function isProjectExists($websiteId, $projectId=false){
170
		$sql = "select id from auditorprojects where website_id=$websiteId";
171
		$sql .= $projectId ? " and id!=$projectId" : "";
172
		$listInfo = $this->db->select($sql, true);
173
		return empty($listInfo['id']) ? false :  $listInfo['id'];
174
	}
175
 
176
	// func to get project info
177
	function __getProjectInfo($projectId) {
178
		$sql = "select p.*,w.url,w.name from auditorprojects p,websites w where  p.website_id=w.id and p.id=$projectId";
179
		$info = $this->db->select($sql, true);
180
		$info['url'] = @Spider::removeTrailingSlash($info['url']);
181
		return $info;
182
	}
183
 
184
	// func to edit project
185
	function editProject($projectId, $listInfo=''){
186
	    $userId = isLoggedIn();
187
	    $projectId = intval($projectId);
188
		if(!empty($projectId)){
189
			if(empty($listInfo)){
190
				$listInfo = $this->__getProjectInfo($projectId);
191
				$listInfo['oldName'] = $listInfo['keyword'];
192
			}
193
			$this->set('post', $listInfo);
194
 
195
			$websiteController = New WebsiteController();
196
    		$websiteList = $websiteController->__getAllWebsites($userId, true);
197
    		$this->set('websiteList', $websiteList);
198
    		$websiteId = empty($listInfo['website_id']) ? $websiteList[0]['id'] : intval($listInfo['website_id']);
199
    		$this->set('websiteId', $websiteId);
200
 
201
    		$langController = New LanguageController();
202
    		$this->set('langList', $langController->__getAllLanguages());
203
			$this->render('siteauditor/edit');
204
			exit;
205
		}
206
	}
207
 
208
	// func to update project
209
	function updateProject($listInfo){
210
		$listInfo['id'] = intval($listInfo['id']);
211
		$userId = isLoggedIn();
212
		$listInfo['website_id'] = intval($listInfo['website_id']);
213
		$listInfo['max_links'] = intval($listInfo['max_links']);
214
		$this->set('post', $listInfo);
215
		$errMsg['website_id'] = formatErrorMsg($this->validate->checkBlank($listInfo['website_id']));
216
		$errMsg['max_links'] = formatErrorMsg($this->validate->checkNumber($listInfo['max_links']));
217
		if(!$this->validate->flagErr){
218
		    $errorFlag = 0;
219
		    if ($listInfo['max_links'] > SA_MAX_NO_PAGES) {
220
		        $errorFlag = 1;
221
		        $errMsg['max_links'] = formatErrorMsg($this->spTextSA["Number of pages is greater than"]. " ". SA_MAX_NO_PAGES);
222
		    }
223
 
224
		    if ($listInfo['max_links'] <= 0) {
225
		        $errorFlag = 1;
226
		        $errMsg['max_links'] = formatErrorMsg($this->spTextSA["Number of pages should be greater than"]. " 0");
227
		    }
228
 
229
		    $webCtrler = New WebsiteController();
230
		    $websiteInfo = $webCtrler->__getWebsiteInfo($listInfo['website_id']);
231
 
232
		    // commented to exclude site auditor with query arguments also by with out adding fulll url
233
		    /*$excludeInfo = $this->checkExcludeLinks($listInfo['exclude_links'], $websiteInfo['url']);
234
		    if (!empty($excludeInfo['err_msg'])) {
235
		        $errorFlag = 1;
236
		        $errMsg['exclude_links'] = formatErrorMsg($excludeInfo['err_msg']);
237
		    }
238
		    $listInfo['exclude_links'] = $excludeInfo['exclude_links'];*/
239
 
240
		    // check for max links allowed for the account type
241
		    $maxValidInfo = $this->validateMaxLinkCount($websiteInfo['user_id'], $listInfo['max_links']);
242
		    if ($maxValidInfo['error']) {
243
		        $errorFlag = 1;
244
		        $errMsg['max_links'] = $maxValidInfo['msg'];
245
		    }
246
 
247
		    // if error occured
248
		    if (!$errorFlag) {
249
    			if (!$this->isProjectExists($listInfo['website_id'], $listInfo['id'])) {
250
    				$sql = "Update auditorprojects set
251
    						website_id={$listInfo['website_id']},
252
    						max_links={$listInfo['max_links']},
253
    						check_pr=".intval($listInfo['check_pr']).",
254
    						check_backlinks=".intval($listInfo['check_backlinks']).",
255
    						check_indexed=".intval($listInfo['check_indexed']).",
256
    						store_links_in_page=".intval($listInfo['store_links_in_page']).",
257
    						check_brocken='".intval($listInfo['check_brocken'])."',
258
    						cron='".intval($listInfo['cron'])."',
259
    						exclude_links='".addslashes($listInfo['exclude_links'])."'
260
    						where id=".$listInfo['id'];
261
    				$this->db->query($sql);
262
    				$this->showAuditorProjects();
263
    				exit;
264
    			}else{
265
    				$errMsg['website_id'] = formatErrorMsg($this->spTextSA['projectalreadyexist']);
266
    			}
267
		    }
268
		}
269
		$this->set('errMsg', $errMsg);
270
		$this->editProject($listInfo['id'], $listInfo);
271
	}
272
 
273
	// func to change status
274
	function __changeStatus($projectId, $status){
275
		$projectId = intval($projectId);
276
		$sql = "update auditorprojects set status=$status where id=$projectId";
277
		$this->db->query($sql);
278
	}
279
 
280
	// func to delete project
281
	function __deleteProject($projectId){
282
	    // delete teh project
283
		$projectId = intval($projectId);
284
		$sql = "delete from auditorprojects where id=$projectId";
285
		$this->db->query($sql);
286
 
287
		// delete all pages found in reports
288
		$sql = "select id from auditorreports where project_id=$projectId";
289
		$repList = $this->db->select($sql);
290
		foreach ($repList as $repInfo) {
291
		    $this->__deleteReportPage($repInfo['id']);
292
		}
293
	}
294
 
295
	// function to get number of links of a project
296
	function getCountcrawledLinks($projectId, $statusCheck=false, $statusVal=1, $conditions='') {
297
		$sql = "select count(*) count from auditorreports r where r.project_id=$projectId";
298
		$sql .= $statusCheck ? " and crawled=$statusVal" : "";
299
		$sql .= $conditions;
300
		$info = $this->db->select($sql, true);
301
		return $info['count'] ? $info['count'] : 0;
302
	}
303
 
304
    // function to get all projects of user
305
	function getAllProjects($where='') {
306
		$sql = "select ap.*,w.url,w.name from auditorprojects ap,websites w where w.id=ap.website_id and w.status=1 and ap.status=1 $where";
307
		$projectList = $this->db->select($sql);
308
		return $projectList;
309
	}
310
 
311
	// function to get number of links of a project
312
	function getProjectLastUpdate($projectId) {
313
		$sql = "select max(updated) updated from auditorreports r where r.project_id=$projectId";
314
		$info = $this->db->select($sql, true);
315
		return empty($info['updated']) ? "Not Started" : $info['updated'];
316
	}
317
 
318
	// function to show interface to run a project
319
	function showRunProject($projectId) {
320
	    $projectId = intval($projectId);
321
	    $projectInfo = $this->__getProjectInfo($projectId);
322
	    $projectInfo['total_links'] = $this->getCountcrawledLinks($projectInfo['id']);
323
	    $projectInfo['crawled_links'] = $this->getCountcrawledLinks($projectInfo['id'], true);
324
		$projectInfo['last_updated'] = $this->getProjectLastUpdate($projectInfo['id']);
325
		$projectInfo['crawling_url'] =  $this->getProjectRandomUrl($projectId);
326
	    $this->set('projectInfo', $projectInfo);
327
	    $this->render('siteauditor/showrunproject');
328
	}
329
 
330
	// fucntion to load reports page after teh actions
331
	function loadReportsPage($info='') {
332
	    print "<script>scriptDoLoadPost('siteauditor.php', 'search_form', 'subcontent', '&sec=showreport&pageno={$info['pageno']}&order_col={$info['order_col']}&order_val={$info['order_val']}')</script>";
333
	}
334
 
335
	// function to check page score
336
	function checkPageScore($info='') {
337
	    if (!empty($info['report_id'])) {
338
	        $reportId = intval($info['report_id']);
339
	        $auditorComp = $this->createComponent('AuditorComponent');
340
	        $reportInfo = $auditorComp->getReportInfo(" and id=$reportId");
341
	        $projectInfo = $this->__getProjectInfo($reportInfo['project_id']);
342
	        $auditorComp->runReport($reportInfo['page_url'], $projectInfo, $this->getCountcrawledLinks($projectInfo['id']));
343
	        $this->loadReportsPage($info);
344
	    }
345
	}
346
 
347
	// func to delete page of report
348
	function __deleteReportPage($reportId){
349
	    if (!empty($reportId)) {
350
	        $reportId = intval($reportId);
351
 
352
	        // delete report page
353
	        $sql = "delete from auditorreports where id=$reportId";
354
	        $this->db->query($sql);
355
 
356
	        // delete all links under this page
357
	        $sql = "delete from auditorpagelinks where report_id=$reportId";
358
	        $this->db->query($sql);
359
	    }
360
	}
361
 
362
	// function to recheck report pages of project
363
	function recheckReportPages($projectId) {
364
	    $projectId = intval($projectId);
365
	    if (!empty($projectId)) {
366
	        $sql = "update auditorreports set crawled=0 where project_id=$projectId";
367
	        $this->db->query($sql);
368
 
369
 
370
    	    // delete all pages found in reports
371
    		$sql = "select id from auditorreports where project_id=$projectId";
372
    		$repList = $this->db->select($sql);
373
    		foreach ($repList as $repInfo) {
374
    		    // delete all links under this page
375
	            $sql = "delete from auditorpagelinks where report_id=".$repInfo['id'];
376
	            $this->db->query($sql);
377
    		}
378
 
379
 
380
	    }
381
	}
382
 
383
	// function to run project, save blog links to database
384
	function runProject($projectId) {
385
	    $projectId = intval($projectId);
386
        $projectInfo = $this->__getProjectInfo($projectId);
387
	    $completed = 0;
388
	    $errorMsg = '';
389
	    if ($reportUrl = $this->getProjectRandomUrl($projectId)) {
390
	        $auditorComp = $this->createComponent('AuditorComponent');
391
	        $crawledUrl = $auditorComp->runReport($reportUrl, $projectInfo, $this->getCountcrawledLinks($projectId));
392
	        $this->set('crawledUrl', $crawledUrl);
393
	        if (!$crawlUrl = $this->getProjectRandomUrl($projectId)) {
394
	            $completed = 1;
395
	        } else {
396
	            if (!$this->cron) updateJsLocation('crawling_url', $crawledUrl);
397
	        }
398
	    } else {
399
	        $completed = 1;
400
	    }
401
 
402
	    // if execution not through cron
403
	    if (!$this->cron) {
404
    	    updateJsLocation('last_updated', date('Y-m-d H:i:s'));
405
    	    updateJsLocation('total_links', $this->getCountcrawledLinks($projectId));
406
    	    updateJsLocation('crawled_pages', $this->getCountcrawledLinks($projectId, true));
407
    	    $this->set('completed', $completed);
408
    	    $this->set('projectId', $projectId);
409
    	    $this->set('errorMsg', $errorMsg);
410
    	    $this->set('projectInfo', $projectInfo);
411
    	    $this->render('siteauditor/runproject');
412
	    } else {
413
	        return $crawledUrl;
414
	    }
415
	}
416
 
417
	// function to get random url of a project
418
	function getProjectRandomUrl($projectId) {
419
	    $sql = "SELECT page_url FROM auditorreports where project_id=$projectId and crawled=0 ORDER BY RAND() LIMIT 1";
420
		$listInfo = $this->db->select($sql, true);
421
		if (empty($listInfo['page_url'])) {
422
		    $totalLinks = $this->getCountcrawledLinks($projectId);
423
		    if ($totalLinks == 0) {
424
		        $auditorComp = $this->createComponent('AuditorComponent');
425
		        $projectInfo = $this->__getProjectInfo($projectId);
426
		        $reportInfo['page_url'] = Spider::formatUrl($projectInfo['url']);
427
		        $reportInfo['project_id'] = $projectId;
428
		        $auditorComp->saveReportInfo($reportInfo);
429
		        return $reportInfo['page_url'];
430
		    } else {
431
		        return false;
432
		    }
433
		} else {
434
		    return $listInfo['page_url'];
435
		}
436
	}
437
 
438
	// function to view the reports
439
	function viewReports($info='') {
440
 
441
	    $userId = isLoggedIn();
442
	    $where = isAdmin() ? "" : " and w.user_id=$userId";
443
	    $pList = $this->getAllProjects($where);
444
	    $projectList = array();
445
	    foreach($pList as $pInfo) {
446
		    $pInfo['total_links'] = $this->getCountcrawledLinks($pInfo['id']);
447
		    if ($pInfo['total_links'] > 0) {
448
	            $projectList[] = $pInfo;
449
		    }
450
	    }
451
 
452
	    if (empty($info['project_id'])) {
453
            $projectId = $projectList[0]['id'];
454
	    } else {
455
	        $projectId = intval($info['project_id']);
456
	    }
457
	    $this->set('projectId', $projectId);
458
	    $this->set('projectList', $projectList);
459
 
460
 
461
		$reportTypes = array(
462
			'rp_links' => $this->spTextSA["Link Reports"],
463
			'rp_summary' => $this->spTextSA["Report Summary"],
464
			'page_title' => $this->spTextSA["Duplicate Title"],
465
			'page_description' => $this->spTextSA["Duplicate Description"],
466
			'page_keywords' => $this->spTextSA["Duplicate Keywords"],
467
		);
468
		$this->set('reportTypes', $reportTypes);
469
		$this->set('repType', empty($info['report_type']) ? "rp_links" : $info['report_type']);
470
 
471
	    $this->render('siteauditor/viewreports');
472
	}
473
 
474
	//function to show the reports by using view reportss filters
475
	function showProjectReport($data='') {
476
	    $data['project_id'] = intval($data['project_id']);
477
	    $projectInfo = $this->__getProjectInfo($data['project_id']);
478
	    $projectInfo['last_updated'] = $this->getProjectLastUpdate($data['project_id']);
479
	    $this->set('projectId', $data['project_id']);
480
		$this->set('projectInfo', $projectInfo);
481
 
482
		$exportVersion = false;
483
		switch($data['doc_type']){
484
 
485
			case "export":
486
				$exportVersion = true;
487
				break;
488
 
489
			case "pdf":
490
				$this->set('pdfVersion', true);
491
				break;
492
 
493
			case "print":
494
				$this->set('printVersion', true);
495
				break;
496
		}
497
 
498
		switch($data['report_type']) {
499
 
500
			case "rp_summary":
501
				$this->showReportSummary($data, $exportVersion, $projectInfo);
502
				break;
503
 
504
			case "page_title":
505
			case "page_description":
506
			case "page_keywords":
507
				$this->showDuplicateMetaInfo($data, $exportVersion, $projectInfo);
508
				break;
509
 
510
			case "rp_links":
511
			default:
512
				$this->showLinksReport($data, $exportVersion, $projectInfo);
513
				break;
514
 
515
		}
516
	}
517
 
518
	// function to show links reports of a auditor project
519
    function showLinksReport($data, $exportVersion, $projectInfo) {
520
 
521
		$projectId = intval($data['project_id']);
522
		$sql = "select * from auditorreports where project_id=$projectId";
523
		$filter = "";
524
 
525
		// check for page rank
526
		if(isset($data['pagerank']) && ($data['pagerank'] != -1)) {
527
			$prMax = intval($data['pagerank']) + 0.5;
528
			$prMin = intval($data['pagerank']) - 0.5;
529
			$sql .= " and pagerank<$prMax and pagerank>=$prMin";
530
			$filter .= "&pagerank=".$data['pagerank'];
531
		}
532
 
533
		// check for page url
534
		if(!empty($data['page_url'])) {
535
			$pageLink = urldecode($data['page_url']);
536
			$filter .= "&page_url=".urlencode($pageLink);
537
			$sql .= " and page_url like '%".addslashes($pageLink)."%'";
538
		}
539
 
540
        // check for page url
541
		if(isset($data['crawled']) && ($data['crawled'] != -1) ) {
542
		    $data['crawled'] = intval($data['crawled']);
543
			$filter .= "&crawled=".$data['crawled'];
544
			$sql .= " and crawled=".$data['crawled'];
545
		}
546
 
547
		// to find order col
548
        if (!empty($data['order_col'])) {
549
		    $orderCol = $data['order_col'];
550
		    $orderVal = $data['order_val'];
551
		} else {
552
		    $orderCol = 'score';
553
		    $orderVal = 'DESC';
554
		}
555
		$filter .= "&order_col=$orderCol&order_val=$orderVal";
556
		$this->set('orderCol', $orderCol);
557
		$this->set('orderVal', $orderVal);
558
 
559
		$pgScriptPath = "siteauditor.php?sec=showreport&report_type=rp_links&project_id=$projectId".$filter;
560
		$this->set('filter', $filter);
561
 
562
		// pagination setup
563
		$this->db->query($sql, true);
564
		$this->paging->setDivClass('pagingdiv');
565
		$this->paging->loadPaging($this->db->noRows, SP_PAGINGNO);
566
		$pagingDiv = $this->paging->printPages($pgScriptPath, '', 'scriptDoLoad', 'subcontent', 'layout=ajax');
567
		$this->set('pagingDiv', $pagingDiv);
568
		$sql .= " order by ".addslashes($orderCol)." ".addslashes($orderVal);
569
 
570
		$sql .= in_array($data['doc_type'], array('pdf', 'print', 'export')) ? "" : " limit ".$this->paging->start .",". $this->paging->per_page;
571
 
572
		$reportList = $this->db->select($sql);
573
		$spTextHome = $this->getLanguageTexts('home', $_SESSION['lang_code']);
574
		$headArr =  array(
575
        	'page_url' => $this->spTextSA["Page Link"],
576
        	'pagerank' => $_SESSION['text']['common']['MOZ Rank'],
577
        	'page_authority' => $_SESSION['text']['common']['Page Authority'],
578
        	'score' => $_SESSION['text']['label']["Score"],
579
        	'brocken' => $_SESSION['text']['label']["Brocken"],
580
            'external_links' => $this->spTextSA["External Links"],
581
        	'total_links' => $this->spTextSA["Total Links"],
582
            'google_backlinks' => "Google {$spTextHome['Backlinks']}",
583
            'bing_backlinks' => "Bing {$spTextHome['Backlinks']}",
584
            'google_indexed' => "Google {$spTextHome['Indexed']}",
585
            'bing_indexed' => "Bing {$spTextHome['Indexed']}",
586
		    'crawled' => $this->spTextSA['Crawled'],
587
		    'brocken' => $_SESSION['text']['label']['Brocken'],
588
		    'page_title' => $_SESSION['text']['label']['Title'],
589
		    'page_description' => $_SESSION['text']['label']['Description'],
590
		    'page_keywords' => $_SESSION['text']['label']['Keywords'],
591
		    'comments' => $_SESSION['text']['label']['Comments'],
592
        );
593
 
594
		if ($exportVersion) {
595
			$spText = $_SESSION['text'];
596
			$exportContent = createExportContent(array('', $this->spTextSA["Link Reports"], ''));
597
			$exportContent .= createExportContent(array());
598
			$exportContent .= createExportContent(array());
599
			$exportContent .= createExportContent(array($this->spTextSA['Project Url'], $projectInfo['url']));
600
			$exportContent .= createExportContent(array($_SESSION['text']['label']['Updated'], $projectInfo['last_updated']));
601
			$exportContent .= createExportContent(array($_SESSION['text']['label']['Total Results'], $this->db->noRows));
602
			$exportContent .= createExportContent(array());
603
			$exportContent .= createExportContent(array($spText['common']['No'],$headArr['page_url'],$headArr['pagerank'],$headArr['page_authority'],$headArr['google_backlinks'],$headArr['bing_backlinks'],$headArr['google_indexed'],$headArr['bing_indexed'],$headArr['external_links'],$headArr['total_links'],$headArr['score'],$headArr['brocken'],$headArr['crawled'],$headArr['page_title'],$headArr['page_description'],$headArr['page_keywords'],$headArr['comments']));
604
			$auditorComp = $this->createComponent('AuditorComponent');
605
			foreach($reportList as $i => $listInfo) {
606
			    if ($listInfo['crawled']) {
607
			        $auditorComp->countReportPageScore($listInfo);
608
			        $comments = strip_tags(implode("\n", $auditorComp->commentInfo));
609
			    } else {
610
			        $comments = "";
611
			    }
612
			    $listInfo['crawled'] = $listInfo['crawled'] ? $spText['common']['Yes'] : $spText['common']['No'];
613
			    $listInfo['brocken'] = $listInfo['brocken'] ? $spText['common']['Yes'] : $spText['common']['No'];
614
				$exportContent .= createExportContent(array($i+1, $listInfo['page_url'],$listInfo['pagerank'],$listInfo['page_authority'],$listInfo['google_backlinks'],$listInfo['bing_backlinks'],$listInfo['google_indexed'],$listInfo['bing_indexed'],$listInfo['external_links'],$listInfo['total_links'],$listInfo['score'],$listInfo['brocken'],$listInfo['crawled'],$listInfo['page_title'],$listInfo['page_description'],$listInfo['page_keywords'],$comments));
615
			}
616
			exportToCsv('siteauditor_report', $exportContent);
617
		} else {
618
			$this->set('totalResults', $this->db->noRows);
619
			$this->set('list', $reportList);
620
			$this->set('pageNo', $_GET['pageno']);
621
			$this->set('data', $data);
622
    	    $this->set('headArr', $headArr);
623
 
624
    	    // if pdf export
625
    	    if ($data['doc_type'] == "pdf") {
626
    	    	exportToPdf($this->getViewContent('siteauditor/reportlinks'), "site_auditor_report_links.pdf");
627
    	    } else {
628
				$this->render('siteauditor/reportlinks');
629
    	    }
630
		}
631
 
632
	}
633
 
634
	# function show the details of a page
635
	function viewPageDetails($info='') {
636
	    $reportId = intval($info['report_id']);
637
	    if (!empty($reportId)) {
638
	        $auditorComp = $this->createComponent('AuditorComponent');
639
	        $reportInfo = $auditorComp->getReportInfo(" and id=$reportId");
640
	        $reportInfo['score'] = array_sum($auditorComp->countReportPageScore($reportInfo));
641
	        $reportInfo['comments'] = $comments = implode("<br>", $auditorComp->commentInfo);
642
	        $this->set('spTextHome', $this->getLanguageTexts('home', $_SESSION['lang_code']));
643
	        $this->set('reportInfo', $reportInfo);
644
	        $this->set('linkList', $auditorComp->getAllLinksPage($reportId));
645
	        $this->set('post', $info);
646
	        $this->render('siteauditor/pagedetails');
647
	    }
648
	}
649
 
650
	// function to show reports summary of a project
651
    function showReportSummary($data, $exportVersion, $projectInfo) {
652
 
653
	    $projectInfo['total_links'] = $this->getCountcrawledLinks($projectInfo['id']);
654
	    $projectInfo['crawled_links'] = $this->getCountcrawledLinks($projectInfo['id'], true);
655
	    $mainLink = SP_WEBPATH."/siteauditor.php?project_id=".$projectInfo['id']."&sec=showreport&report_type=rp_summary";
656
 
657
	    // check for page url
658
        $statusCheck = false;
659
        $statusVal = 0;
660
		if(isset($data['crawled']) && ($data['crawled'] != -1) ) {
661
		    $statusCheck = true;
662
		    $statusVal = intval($data['crawled']);
663
		    $mainLink .= "&crawled=$statusVal";
664
		}
665
 
666
		// check for brocken
667
		$conditions = " and brocken=1";
668
	    $projectInfo['brocken'] = $this->getCountcrawledLinks($projectInfo['id'], $statusCheck, $statusVal, $conditions);
669
 
670
		// check for pagerank
671
		for ($i=0; $i<=10; $i++) {
672
		    $prMax = $i + 0.5;
673
		    $prMin = $i - 0.5;
674
		    $conditions = " and pagerank<$prMax and pagerank>=$prMin";
675
		    $projectInfo['PR'.$i] = $this->getCountcrawledLinks($projectInfo['id'], $statusCheck, $statusVal, $conditions);
676
		}
677
 
678
		// check for backlinks
679
	    foreach ($this->seArr as $se) {
680
		    $conditions = " and $se"."_backlinks>0";
681
		    $projectInfo[$se."_backlinks"] = $this->getCountcrawledLinks($projectInfo['id'], $statusCheck, $statusVal, $conditions);
682
	    }
683
 
684
        // check for indexed
685
	    foreach ($this->seArr as $se) {
686
		    $conditions = " and $se"."_indexed>0";
687
		    $projectInfo[$se."_indexed"] = $this->getCountcrawledLinks($projectInfo['id'], $statusCheck, $statusVal, $conditions);
688
	    }
689
 
690
	    // duplicate titles,descriptions and keywords
691
	    $metaArr = array('page_title' => $this->spTextSA["Duplicate Title"], 'page_description' => $this->spTextSA['Duplicate Description'], 'page_keywords' => $this->spTextSA['Duplicate Keywords']);
692
	    foreach ($metaArr as $meta => $val) {
693
	        $auditorComp = $this->createComponent('AuditorComponent');
694
	        $projectInfo["duplicate_".$meta] = $auditorComp->getDuplicateMetaInfoCount($projectInfo['id'], $meta, $statusCheck, $statusVal);
695
	    }
696
	    $spTextHome = $this->getLanguageTexts('home', $_SESSION['lang_code']);
697
	    $this->set('spTextHome', $spTextHome);
698
 
699
	    if ($exportVersion) {
700
			$spText = $_SESSION['text'];
701
			$exportContent = createExportContent(array('', $this->spTextSA['Project Summary'], ''));
702
			$exportContent .= createExportContent(array());
703
			$exportContent .= createExportContent(array());
704
			$exportContent .= createExportContent(array($this->spTextSA['Project Url'], $projectInfo['url']));
705
			$exportContent .= createExportContent(array($_SESSION['text']['label']['Updated'], $projectInfo['last_updated']));
706
			$exportContent .= createExportContent(array($_SESSION['text']['label']['Score'], $projectInfo['score']));
707
			$exportContent .= createExportContent(array($this->spTextSA['Maximum Pages'], $projectInfo['max_links']));
708
			$exportContent .= createExportContent(array($this->spTextSA['Pages Found'], $projectInfo['total_links']));
709
			$exportContent .= createExportContent(array($this->spTextSA['Crawled Pages'], $projectInfo['crawled_links']));
710
 
711
			foreach ($this->seArr as $se) {
712
		        $exportContent .= createExportContent(array(ucfirst($se). " {$spTextHome['Backlinks']}", $projectInfo[$se."_backlinks"]));
713
			}
714
 
715
			foreach ($this->seArr as $se) {
716
		        $exportContent .= createExportContent(array(ucfirst($se). " {$spTextHome['Indexed']}", $projectInfo[$se."_indexed"]));
717
			}
718
 
719
			foreach ($metaArr as $meta => $val) {
720
			    $exportContent .= createExportContent(array($val, $projectInfo["duplicate_".$meta]));
721
	        }
722
 
723
    	    for ($i=0; $i<=10; $i++) {
724
    	        $exportContent .= createExportContent(array("PR$i", $projectInfo["PR$i"]));
725
    		}
726
    		$exportContent .= createExportContent(array($_SESSION['text']['label']['Brocken'], $projectInfo['brocken']));
727
			exportToCsv('siteauditor_report_summary', $exportContent);
728
		} else {
729
    		$this->set('projectInfo', $projectInfo);
730
    		$this->set('metaArr', $metaArr);
731
    		$this->set('seArr', $this->seArr);
732
    		$this->set('mainLink', $mainLink);
733
 
734
            // if pdf export
735
            if ($data['doc_type'] == "pdf") {
736
            	exportToPdf($this->getViewContent('siteauditor/projectreportsummary'), "site_auditor_report_summary.pdf");
737
            } else {
738
            	$this->render('siteauditor/projectreportsummary');
739
            }
740
		}
741
    }
742
 
743
    // function to show reports summary of a project
744
    function showDuplicateMetaInfo($data, $exportVersion, $projectInfo) {
745
        $repType = addslashes($data['report_type']);
746
        $projectId = $projectInfo['id'];
747
        $sql = "select $repType,count(*) as count from auditorreports where project_id=$projectId and $repType!=''";
748
        $filter = "";
749
 
750
        // check for page url
751
		if(isset($data['crawled']) && ($data['crawled'] != -1) ) {
752
		    $data['crawled'] = intval($data['crawled']);
753
			$filter .= "&crawled=".$data['crawled'];
754
			$sql .= " and crawled=".$data['crawled'];
755
		}
756
 
757
	    // to find order col
758
        if (!empty($data['order_col'])) {
759
		    $orderCol = $data['order_col'];
760
		    $orderVal = $data['order_val'];
761
		} else {
762
		    $orderCol = 'count';
763
		    $orderVal = 'DESC';
764
		}
765
		$filter .= "&order_col=$orderCol&order_val=$orderVal";
766
		$pgScriptPath = SP_WEBPATH."/siteauditor.php?sec=showreport&report_type=$repType&project_id=".$projectId.$filter;
767
 
768
		// pagination setup
769
		$sql .= " group by $repType having count>1";
770
		$this->db->query($sql, true);
771
		$this->paging->setDivClass('pagingdiv');
772
		$this->paging->loadPaging($this->db->noRows, SP_PAGINGNO);
773
		$pagingDiv = $this->paging->printPages($pgScriptPath, '', 'scriptDoLoad', 'subcontent', 'layout=ajax');
774
		$this->set('pagingDiv', $pagingDiv);
775
		$sql .= " order by ".addslashes($orderCol)." ".addslashes($orderVal);
776
		$sql .= in_array($data['doc_type'], array('pdf', 'print', 'export')) ? "" : " limit ".$this->paging->start .",". $this->paging->per_page;
777
 
778
	    $totalResults = $this->db->noRows;
779
	    $headArr =  array(
780
        	'page_title' => $this->spTextSA["Duplicate Title"],
781
        	'page_description' => $this->spTextSA["Duplicate Description"],
782
        	'page_keywords' => $this->spTextSA["Duplicate Keywords"],
783
        	'page_urls' => $this->spTextSA["Page Links"],
784
        	'count' => $_SESSION['text']['label']["Count"],
785
        );
786
 
787
        $list = $this->db->select($sql);
788
        $dupInfo[$repType] = $list;
789
        $auditorComp = $this->createComponent('AuditorComponent');
790
        foreach($dupInfo[$repType] as $i => $listInfo) {
791
            $dupInfo[$repType][$i]['page_urls'] = $auditorComp->getAllreportPages(" and project_id=$projectId and $repType='".addslashes($listInfo[$repType])."'", 'id,page_url');
792
        }
793
 
794
	    if ($exportVersion) {
795
			$spText = $_SESSION['text'];
796
			$exportContent = createExportContent(array('', $headArr[$repType]." ".$_SESSION['text']['common']['Reports'], ''));
797
			$exportContent .= createExportContent(array());
798
			$exportContent .= createExportContent(array());
799
			$exportContent .= createExportContent(array($this->spTextSA['Project Url'], $projectInfo['url']));
800
			$exportContent .= createExportContent(array($_SESSION['text']['label']['Updated'], $projectInfo['last_updated']));
801
			$exportContent .= createExportContent(array($_SESSION['text']['label']['Total Results'], $totalResults));
802
			$exportContent .= createExportContent(array());
803
			$exportContent .= createExportContent(array($spText['common']['No'], $headArr[$repType], $headArr["page_urls"], $headArr["count"]));
804
			foreach($dupInfo[$repType] as $i => $listInfo) {
805
			    $pageUrls = "";
806
			    foreach($listInfo['page_urls'] as $urlInfo) $pageUrls .= $urlInfo['page_url'] . "\n";
807
				$exportContent .= createExportContent(array($i+1, $listInfo[$repType], $pageUrls, $listInfo['count']));
808
			}
809
			exportToCsv('siteauditor_duplicate_'.$repType, $exportContent);
810
		} else {
811
    		$this->set('orderCol', $orderCol);
812
    		$this->set('orderVal', $orderVal);
813
    		$this->set('filter', $filter);
814
			$this->set('pageNo', $_GET['pageno']);
815
    	    $this->set('totalResults', $totalResults);
816
    	    $this->set('list', $dupInfo[$repType]);
817
    	    $this->set('repType', $repType);
818
    	    $this->set('headArr', $headArr);
819
 
820
    	    // if pdf export
821
    	    if ($data['doc_type'] == "pdf") {
822
    	    	exportToPdf($this->getViewContent('siteauditor/showduplicatemetainfo'), "site_auditor_report_duplicate_meta.pdf");
823
    	    } else {
824
    	    	$this->render('siteauditor/showduplicatemetainfo');
825
    	    }
826
		}
827
    }
828
 
829
    // function to show cron command
830
    function showCronCommand(){
831
		$this->render('siteauditor/croncommand');
832
	}
833
 
834
    // function toexecute cron job
835
    function executeCron() {
836
        $sql = "select id from auditorprojects where cron=1 and status=1";
837
		$prList = $this->db->select($sql);
838
		$urlFound = false;
839
		foreach ($prList as $info) {
840
	        if ($this->getProjectRandomUrl($info['id']) ) {
841
	            $urlFound = true;
842
	            $reportUrl = $this->runProject($info['id']);
843
	            break;
844
	        }
845
		}
846
 
847
		if ($urlFound) {
848
            echo "\n==='$reportUrl' crawled successfully!===";
849
		} else {
850
		    echo "\n===No projects found to execute!===";
851
		}
852
    }
853
 
854
    // function to show import links to a project
855
    function showImportProjectLinks($info='') {
856
        $userId = isLoggedIn();
857
        $where = isAdmin() ? "" : " and w.user_id=$userId";
858
	    $projectList = $this->getAllProjects($where);
859
	    $this->set('projectList', $projectList);
860
 
861
	    if(empty($projectList)) {
862
	        showErrorMsg($this->spTextSA['No active projects found'].'!');
863
        }
864
 
865
	    $projectId = empty($info['project_id']) ? 0 : $info['project_id'];
866
	    $this->set('projectId', $projectId);
867
 
868
        $this->render('siteauditor/importlinks');
869
    }
870
 
871
    // function to import links
872
    function importLinks($listInfo) {
873
        $userId = isLoggedIn();
874
        $listInfo['project_id'] = intval($listInfo['project_id']);
875
		$this->set('post', $listInfo);
876
		$errMsg['links'] = formatErrorMsg($this->validate->checkBlank($listInfo['links']));
877
 
878
		if (!$this->validate->flagErr) {
879
 
880
    		$totalLinks = $this->getCountcrawledLinks($listInfo['project_id']);
881
    		$projectInfo = $this->__getProjectInfo($listInfo['project_id']);
882
    		// if total links greater than max links of a project
883
    		if ($totalLinks >= $projectInfo['max_links']) {
884
		        $errMsg['links'] = formatErrorMsg($this->spTextSA['totallinksgreaterallowed']." - {$projectInfo['max_links']}");
885
    		} else {
886
                // check whether links are pages of website
887
        		$linkInfo = $this->checkExcludeLinks($listInfo['links'], $projectInfo['url'], false);
888
        		if (!empty($linkInfo['err_msg'])) {
889
        		    $errMsg['links'] = formatErrorMsg($linkInfo['err_msg']);
890
        		} else {
891
        		    $auditorComp = $this->createComponent('AuditorComponent');
892
        		    $links = explode(",", $listInfo['links']);
893
        			$error = false;
894
        			$linkList = array();
895
        			foreach ($links as $i => $link) {
896
        				$link = Spider::formatUrl(trim($link));
897
        				if (empty($link)) continue;
898
        				if ($auditorComp->isExcludeLink($link, $projectInfo['exclude_links'])) continue;
899
 
900
        				// check whether url exists or not
901
        				if ($auditorComp->getReportInfo(" and project_id={$projectInfo['id']} and page_url='".addslashes($link)."'")) {
902
        					$errMsg['links'] = formatErrorMsg($this->spTextSA['Page Link']." '<b>$link</b>' ". $_SESSION['text']['label']['already exist']);
903
        					$error = true;
904
        					break;
905
        				} else {
906
        				    $totalLinks++;
907
        				    // if total links greater than max links of a project
908
        				    if ($totalLinks > $projectInfo['max_links']) {
909
        				        $error = true;
910
        				        $errMsg['links'] = formatErrorMsg($this->spTextSA['totallinksgreaterallowed']." - {$projectInfo['max_links']}");
911
        				        break;
912
        				    }
913
        				}
914
        				$linkList[$link] = 1;
915
        			}
916
 
917
        			// to save the page if no error occurs
918
        			if (!$error) {
919
        			    foreach ($linkList as $link => $val) {
920
            		        $reportInfo['page_url'] = $link;
921
            		        $reportInfo['project_id'] = $projectInfo['id'];
922
            		        $auditorComp->saveReportInfo($reportInfo);
923
        			    }
924
        			    $this->showAuditorProjects();
925
            		    exit;
926
        			}
927
        		}
928
    		}
929
 
930
		}
931
		$this->set('errMsg', $errMsg);
932
		$this->showImportProjectLinks();
933
    }
934
 
935
    // Function to check / validate the user type site auditor project maximum list
936
    function validateMaxLinkCount($userId, $count) {
937
    	$userCtrler = new UserController();
938
    	$validation = array('error' => false);
939
 
940
    	// if admin user id return true
941
    	if ($userCtrler->isAdminUserId($userId)) {
942
    		return $validation;
943
    	}
944
 
945
    	$userTypeCtrlr = new UserTypeController();
946
    	$userTypeDetails = $userTypeCtrlr->getUserTypeSpecByUser($userId);
947
 
948
    	// if limit is set and not -1
949
    	if (isset($userTypeDetails['site_auditor_max_page_limit']) && $userTypeDetails['site_auditor_max_page_limit'] >= 0) {
950
 
951
    		// check whether count greater than limit
952
    		if ($count > $userTypeDetails['site_auditor_max_page_limit']) {
953
    			$spTextSubs = $userTypeCtrlr->getLanguageTexts('subscription', $_SESSION['lang_code']);
954
    			$validation['error'] = true;
955
    			$validation['msg'] = formatErrorMsg(str_replace("[limit]", $userTypeDetails['site_auditor_max_page_limit'], $spTextSubs['total_count_greater_account_limit']));
956
    		}
957
 
958
    	}
959
 
960
    	return $validation;
961
    }
962
 
963
}
964
?>