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 seo plugins controller functions
24
class SeoPluginsController extends Controller{
25
 
26
	var $layout = 'ajax';
27
	var $info = array();
28
	var $pluginText = "";
29
	var $pluginCtrler = FALSE;
30
	var $pluginPath;
31
	var $pluginId;
32
	var $pluginViewPath;
33
	var $pluginWebPath;
34
	var $pluginImagePath;
35
	var $pluginCssPath;
36
	var $pluginJsPath;
37
	var $pluginScriptUrl;
38
 
39
	# function to manage seo plugins
40
	function manageSeoPlugins($info, $method='get', $cronJob = false) {
41
 
42
		// check for plugin access level for user, if not admin
43
		if (!isAdmin() && !$cronJob) {
44
			$userTypeCtrler = new UserTypeController();
45
			$userSessInfo = Session::readSession('userInfo');
46
			$pluginAccessList = $userTypeCtrler->getPluginAccessSettings($userSessInfo['userTypeId']);
47
			if (isset($pluginAccessList[$info['pid']]) && empty($pluginAccessList[$info['pid']]['value'])) {
48
				showErrorMsg($_SESSION['text']['label']['Access denied']);
49
			}
50
		}
51
 
52
		$pluginInfo = $this->__getSeoPluginInfo($info['pid']);
53
		$pluginDirName = $pluginInfo['name'];
54
		$pluginPath = SP_PLUGINPATH."/".$pluginDirName;
55
 
56
		if(file_exists($pluginPath."/".SP_PLUGINCONF)){
57
			include_once($pluginPath."/".SP_PLUGINCONF);
58
		}
59
 
60
		include_once($pluginPath."/".$pluginDirName.".ctrl.php");
61
		$pluginControler = New $pluginDirName();
62
 
63
		// set plugin specific variabled
64
		$pluginControlerpluginDirName = $pluginDirName;
65
		$pluginControler->pluginPath = $pluginPath;
66
		$pluginControler->pluginId = $info['pid'];
67
		$pluginControler->pluginViewPath = $this->getPluginViewPath($pluginControler->pluginPath);
68
		$pluginControler->pluginWebPath = SP_WEBPATH . "/" . SP_PLUGINDIR . "/" . $pluginDirName;
69
		$pluginControler->pluginImagePath = $pluginControler->pluginWebPath . "/images";
70
		$pluginControler->pluginCssPath = $pluginControler->pluginWebPath . "/css";
71
		$pluginControler->pluginJsPath = $pluginControler->pluginWebPath . "/js";
72
		$pluginControler->pluginScriptUrl = SP_WEBPATH . "/seo-plugins.php?pid=" . $pluginControler->pluginId;
73
 
74
		// if not_set_global_vars is not assigned
75
		if (empty($info['not_set_global_vars']) || !$info['not_set_global_vars']) {
76
			define('PLUGIN_PATH', $pluginControler->pluginPath);
77
			define('PLUGIN_ID', $info['pid']);
78
			define('PLUGIN_VIEWPATH', $pluginControler->pluginViewPath);
79
			define('PLUGIN_WEBPATH', $pluginControler->pluginWebPath);
80
			define('PLUGIN_IMGPATH', $pluginControler->pluginImagePath);
81
			define('PLUGIN_CSSPATH', $pluginControler->pluginCssPath);
82
			define('PLUGIN_JSPATH', $pluginControler->pluginJsPath);
83
			define("PLUGIN_SCRIPT_URL", $pluginControler->pluginScriptUrl);
84
		}
85
 
86
		// if no action specified just initialize plugin
87
		if ($info['action'] == 'get_plugin_object') {
88
			$pluginControler->initPlugin($data);
89
			return $pluginControler;
90
		} else {
91
			$this->pluginCtrler = $pluginControler;
92
			$action = empty($info['action']) ? "index" : $info['action'];
93
			$data = $_REQUEST;
94
			$pluginControler->initPlugin($data);
95
			$pluginControler->$action($data);
96
		}
97
	}
98
 
99
	# function to get plugin view path, chekc for theme exists or not
100
	function getPluginViewPath($pluginPath) {
101
		$pluginViewPath = $pluginPath."/views";
102
 
103
 
104
		# if theme compatible design for plugin
105
		if (file_exists($pluginPath."/themes")) {
106
			$themeInfo = $this->dbHelper->getRow("themes", "status=1 order by id");
107
 
108
			// if activated theme folder is existing
109
			if (file_exists($pluginPath."/themes/". $themeInfo['folder'])) {
110
				$pluginViewPath = $pluginPath."/themes/". $themeInfo['folder'] ."/views";
111
			} else {
112
				$pluginViewPath = $pluginPath."/themes/classic/views";
113
			}
114
 
115
		}
116
 
117
		return $pluginViewPath;
118
 
119
	}
120
 
121
 
122
	# function to init plugin before do action
123
	function initPlugin($data) {
124
		return;
125
	}
126
 
127
	# func to load plugin css files
128
	function loadAllPluginCss($pluginPathDir = "", $pluginCssWebPath = "") {
129
		$styleCont = "";
130
		$pluginPathDir = !empty($pluginPathDir) ? $pluginPathDir : PLUGIN_PATH."/css";
131
		$pluginCssWebPath = !empty($pluginCssWebPath) ? $pluginCssWebPath : PLUGIN_CSSPATH;
132
		if(file_exists($pluginPathDir)){
133
			if ($handle = opendir($pluginPathDir)) {
134
				while (false !== ($file = readdir($handle))) {
135
					if ( ($file != ".") && ($file != "..") &&  preg_match('/\.css$/i', $file) ) {
136
						$styleCont .= 'loadJsCssFile("'.$pluginCssWebPath."/".$file.'", "css");';
137
					}
138
				}
139
			}
140
		}
141
 
142
		if (!empty($styleCont)) {
143
			$styleCont = "<script>$styleCont</script>";
144
		}
145
 
146
		return $styleCont;
147
	}
148
 
149
	# func to load plugin js files
150
	function loadAllPluginJs($pluginPathDir = "", $pluginJsWebPath = "") {
151
		$styleCont = "";
152
		$pluginPathDir = !empty($pluginPathDir) ? $pluginPathDir : PLUGIN_PATH."/js";
153
		$pluginJsWebPath = !empty($pluginJsWebPath) ? $pluginJsWebPath : PLUGIN_JSPATH;
154
		if(file_exists($pluginPathDir)){
155
			if ($handle = opendir($pluginPathDir)) {
156
				while (false !== ($file = readdir($handle))) {
157
					if ( ($file != ".") && ($file != "..") &&  preg_match('/\.js$/i', $file) ) {
158
						$styleCont .= 'loadJsCssFile("'.$pluginJsWebPath."/".$file.'", "js");';
159
					}
160
				}
161
			}
162
		}
163
 
164
		if (!empty($styleCont)) {
165
			$styleCont = "<script>$styleCont</script>";
166
		}
167
 
168
		return $styleCont;
169
	}
170
 
171
	# index function
172
	function showSeoPlugins($info=''){
173
		$this->layout = "default";
174
 
175
		$menuList = array();
176
		$pluginList = $this->__getAllSeoPlugins("status=1 and installed=1 ");
177
 
178
		// if not admin, check plugin access set for user,
179
		if (!isAdmin()) {
180
			$userTypeCtrler = new UserTypeController();
181
			$userSessInfo = Session::readSession('userInfo');
182
			$pluginAccessList = $userTypeCtrler->getPluginAccessSettings($userSessInfo['userTypeId']);
183
 
184
			// loop through plugin list
185
			foreach ($pluginList as $pluginInfo) {
186
 
187
				// if access is set for plugin
188
				if (isset($pluginAccessList[$pluginInfo['id']]['value'])) {
189
 
190
					// access is on
191
					if (!empty($pluginAccessList[$pluginInfo['id']]['value'])) {
192
						$menuList[] = $pluginInfo;
193
					}
194
 
195
				} else {
196
					$menuList[] = $pluginInfo;
197
				}
198
 
199
			}
200
 
201
		} else {
202
			$menuList = $pluginList;
203
		}
204
 
205
		if(count($menuList) <= 0){
206
		    $msg = $_SESSION['text']['label']['noactiveplugins'];
207
		    $msgButton = '<a class="actionbut" href="'.SP_PLUGINSITE.'" target="_blank">'.$this->spTextPlugin['Download Seo Panel Plugins'].' &gt;&gt;</a>';
208
			$this->set('msg', $msg);
209
			$this->set('msgButton', $msgButton);
210
			$this->render('common/notfound');
211
			exit;
212
		}
213
 
214
		# to get sub menus under a plugin main menu
215
		foreach($menuList as $i => $menuInfo){
216
			@Session::setSession('plugin_id', $menuInfo['id']);
217
			$pluginDirName = $menuInfo['name'];
218
 
219
			// for older versions with out themes specific files
220
			$pluginDirName = $menuInfo['name'];
221
			$menuFile = SP_PLUGINPATH."/".$pluginDirName."/views/".SP_PLUGINMENUFILE;
222
 
223
			// check for menu file exists or not
224
			if(file_exists($menuFile)){
225
				$menuList[$i]['menu'] = @View::fetchFile($menuFile);
226
			}else{
227
 
228
				// create plugin object and access the menu file - theme specific files
229
				$pluginObj = $this->createPluginObject($pluginDirName, array('not_set_global_vars' => true));
230
				$menuFile = $pluginObj->pluginViewPath . "/". SP_PLUGINMENUFILE;
231
 
232
				if(file_exists($menuFile)){
233
					$menuList[$i]['menu'] = @View::fetchFile($menuFile);
234
				}else{
235
					$menuList[$i]['menu'] = "<ul id='subui'>
236
											<li><a href='javascript:void(0);' onclick=\"".pluginMenu('action=index')."\">{$menuInfo['name']}</a></li>
237
										</ul>";
238
				}
239
			}
240
 
241
			// load plugin js and css files
242
			$pluginPathDir = SP_PLUGINPATH."/".$pluginDirName;
243
			$pluginWebPath = SP_WEBPATH . "/plugins/" . $pluginDirName;
244
			$pluginCssJsCont = $this->loadAllPluginCss($pluginPathDir . "/css", $pluginWebPath . "/css");
245
			$pluginCssJsCont .= $this->loadAllPluginJs($pluginPathDir . "/js", $pluginWebPath . "/js");
246
			$menuList[$i]['menu'] .= $pluginCssJsCont;
247
		}
248
 
249
		$this->set('menuList', $menuList);
250
		$menuSelected = empty($info['menu_selected']) ? $menuList[0]['id'] : $info['menu_selected'];
251
		$this->set('menuSelected', $menuSelected);
252
 
253
		$this->render('seoplugins/showseoplugins');
254
	}
255
 
256
	# func to get all seo tools
257
	function __getAllSeoPlugins($whereCond = ""){
258
		$whereCond = !empty($whereCond) ? $whereCond : "1=1";
259
		$sql = "select * from seoplugins where $whereCond order by priority,id";
260
		$seoPluginList = $this->db->select($sql);
261
		return $seoPluginList;
262
	}
263
 
264
	# func to list seo tools
265
	function listSeoPlugins($msg='', $error=false){
266
 
267
		if(empty($msg)) $this->__updateAllSeoPlugins();
268
		$userId = isLoggedIn();
269
		$this->set('msg', $msg);
270
		$this->set('error', $error);
271
 
272
		$sql = "select * from seoplugins order by id";
273
 
274
		# pagination setup
275
		$this->db->query($sql, true);
276
		$this->paging->setDivClass('pagingdiv');
277
		$this->paging->loadPaging($this->db->noRows, SP_PAGINGNO);
278
		$pagingDiv = $this->paging->printPages('seo-plugins-manager.php?');
279
		$this->set('pagingDiv', $pagingDiv);
280
		$sql .= " limit ".$this->paging->start .",". $this->paging->per_page;
281
 
282
		$seoPluginList = $this->db->select($sql);
283
		$this->set('pageNo', $_GET['pageno']);
284
		$this->set('list', $seoPluginList);
285
		$this->render('seoplugins/listseoplugins');
286
	}
287
 
288
	#function to change status of seo plugins
289
	function changeStatus($seoPluginId, $status){
290
		$status = intval($status);
291
		$seoPluginId = intval($seoPluginId);
292
		$sql = "update seoplugins set status=$status where id=$seoPluginId";
293
		$this->db->query($sql);
294
	}
295
 
296
	#function to change installed status of seo plugins
297
	function __changeInstallStatus($seoPluginId, $status){
298
		$status = intval($status);
299
		$seoPluginId = intval($seoPluginId);
300
		$sql = "update seoplugins set installed=$status where id=$seoPluginId";
301
		$this->db->query($sql);
302
	}
303
 
304
	# func to get seo plugin info
305
	function __getSeoPluginInfo($val, $col='id') {
306
		$val = ($col == 'id') ? intval($val) : addslashes($val);
307
		$sql = "select * from seoplugins where $col='$val'";
308
		$seoPluginInfo = $this->db->select($sql, true);
309
		return $seoPluginInfo;
310
	}
311
 
312
	# func to edit seo plugin
313
	function editSeoPlugin($info, $error=false){
314
 
315
		if($error){
316
			$this->set('post', $info);
317
		}else{
318
			$info['pid'] = intval($info['pid']);
319
			$this->set('post', $this->__getSeoPluginInfo($info['pid']));
320
		}
321
 
322
		$this->render('seoplugins/editseoplugin');
323
	}
324
 
325
	# func to list seo plugin info
326
	function listPluginInfo($pluginId){
327
	    $pluginId = intval($pluginId);
328
		$this->set('pluginInfo', $this->__getSeoPluginInfo($pluginId));
329
		$this->set('pageNo', $_GET['pageno']);
330
		$this->render('seoplugins/listplugininfo');
331
	}
332
 
333
	function updateSeoPlugin($listInfo){
334
 
335
		$listInfo['id'] = intval($listInfo['id']);
336
		$this->set('post', $listInfo);
337
		$errMsg['plugin_name'] = formatErrorMsg($this->validate->checkBlank($listInfo['plugin_name']));
338
		$errMsg['priority'] = formatErrorMsg($this->validate->checkNumber($listInfo['priority']));
339
		if(!$this->validate->flagErr){
340
			$sql = "update seoplugins set
341
						label='".addslashes($listInfo['plugin_name'])."',
342
						priority='".intval($listInfo['priority'])."'
343
						where id={$listInfo['id']}";
344
			$this->db->query($sql);
345
			$this->listSeoPlugins();
346
		}else{
347
			$this->set('errMsg', $errMsg);
348
			$listInfo['label'] = $listInfo['plugin_name'];
349
			$this->editSeoPlugin($listInfo, true);
350
		}
351
	}
352
 
353
	function updatePluginInfo($pluginId, $pluginInfo){
354
 
355
		$pluginId = intval($pluginId);
356
		$sql = "update seoplugins set
357
					label='".addslashes($pluginInfo['label'])."',
358
					author='".addslashes($pluginInfo['author'])."',
359
					description='".addslashes($pluginInfo['description'])."',
360
					version='{$pluginInfo['version']}',
361
					website='{$pluginInfo['website']}'
362
					where id=$pluginId";
363
		$this->db->query($sql);
364
	}
365
 
366
	# func to upgrade seo plugin
367
	function upgradeSeoPlugin($pluginId){
368
		$pluginInfo = $this->__getSeoPluginInfo($pluginId);
369
 
370
		if(file_exists(SP_PLUGINPATH."/".$pluginInfo['name'])){
371
			$pluginDBFile = SP_PLUGINPATH."/".$pluginInfo['name']."/".SP_PLUGINUPGRADEFILE;
372
			if(file_exists($pluginDBFile)){
373
				$this->db->debugMode = false;
374
				$this->db->importDatabaseFile($pluginDBFile, false);
375
			}
376
 
377
			# parse plugin info
378
			$pluginInfo = $this->parsePluginInfoFile($pluginInfo['name']);
379
			$this->updatePluginInfo($pluginId, $pluginInfo);
380
 
381
			$this->__changeInstallStatus($pluginId, 1);
382
			$this->listSeoPlugins("Plugin <b>{$pluginInfo['label']}</b> upgraded successfully!");
383
		}else{
384
			$this->__changeInstallStatus($pluginId, 0);
385
			$this->listSeoPlugins("Plugin <b>{$pluginInfo['label']}</b> upgrade failed!", true);
386
		}
387
	}
388
 
389
	# func to re install the seo plugin
390
	function reInstallSeoPlugin($pluginId){
391
		$pluginInfo = $this->__getSeoPluginInfo($pluginId);
392
 
393
		if(file_exists(SP_PLUGINPATH."/".$pluginInfo['name'])){
394
			$pluginDBFile = SP_PLUGINPATH."/".$pluginInfo['name']."/".SP_PLUGINDBFILE;
395
			if(file_exists($pluginDBFile)){
396
				$this->db->debugMode = false;
397
				$this->db->importDatabaseFile($pluginDBFile, false);
398
			}
399
 
400
			# parse plugin info
401
			$pluginInfo = $this->parsePluginInfoFile($pluginInfo['name']);
402
			$this->updatePluginInfo($pluginId, $pluginInfo);
403
 
404
			$this->__changeInstallStatus($pluginId, 1);
405
			$this->listSeoPlugins("Plugin <b>{$pluginInfo['label']}</b> re-installed successfully!");
406
		}else{
407
			$this->__changeInstallStatus($pluginId, 0);
408
			$this->listSeoPlugins("Plugin <b>{$pluginInfo['label']}</b> re-installation failed!", true);
409
		}
410
	}
411
 
412
	# to check whether the directory is plugin
413
	function isPluginDirectory($file){
414
		if ( ($file != ".") && ($file != "..") && ($file != ".svn") &&  is_dir(SP_PLUGINPATH."/".$file) ) {
415
			if(!preg_match('/^\./', $file)){
416
				return true;
417
			}
418
		}
419
		return false;
420
	}
421
 
422
	# func to update seo plugins in db
423
	function __updateAllSeoPlugins(){
424
		$sql = "update seoplugins set installed=0";
425
		$this->db->query($sql);
426
 
427
		if ($handle = opendir(SP_PLUGINPATH)) {
428
			while (false !== ($file = readdir($handle))) {
429
				if ( $this->isPluginDirectory($file) ) {
430
					$pluginName = $file;
431
					$seoPluginInfo = $this->__getSeoPluginInfo($pluginName, 'name');
432
					if(empty($seoPluginInfo['id'])){
433
 
434
						# parse plugin info
435
						$pluginInfo = $this->parsePluginInfoFile($file);
436
 
437
						$sql = "insert into seoplugins(label,name,author,description,version,website,status,installed)
438
								values('".addslashes($pluginInfo['label'])."','$pluginName','".addslashes($pluginInfo['author'])."','".addslashes($pluginInfo['description'])."','{$pluginInfo['version']}','{$pluginInfo['website']}',0,1)";
439
						$this->db->query($sql);
440
 
441
						$pluginDBFile = SP_PLUGINPATH."/".$file."/".SP_PLUGINDBFILE;
442
						if(file_exists($pluginDBFile)){
443
 
444
							$this->db->debugMode = false;
445
							$this->db->importDatabaseFile($pluginDBFile, false);
446
						}
447
 
448
					}else{
449
						$this->__changeInstallStatus($seoPluginInfo['id'], 1);
450
					}
451
				}
452
			}
453
			closedir($handle);
454
		}
455
	}
456
 
457
	# func to parse plugin info file
458
	function parsePluginInfoFile($file) {
459
		$pluginInfo = array();
460
		$pluginInfoFile = SP_PLUGINPATH."/".$file."/".SP_PLUGININFOFILE;
461
		if(file_exists($pluginInfoFile)){
462
			$xml = new XMLParser;
463
    		$pInfo = $xml->parse($pluginInfoFile);
464
    		if(!empty($pInfo[0]['child'])){
465
    			foreach($pInfo[0]['child'] as $info){
466
    				$infoCol = strtolower($info['name']);
467
    				$pluginInfo[$infoCol] = $info['content'];
468
    			}
469
    		}
470
		}
471
 
472
		$pluginInfo['label'] = empty($pluginInfo['label']) ? $file : $pluginInfo['label'];
473
		$pluginInfo['version'] = empty($pluginInfo['version']) ? '1.0.0' : $pluginInfo['version'];
474
		$pluginInfo['author'] = empty($pluginInfo['author']) ? 'Seo Panel': $pluginInfo['author'];
475
		$pluginInfo['website'] = empty($pluginInfo['website']) ? SP_PLUGINSITE : $pluginInfo['website'];
476
		return $pluginInfo;
477
	}
478
 
479
	# function to create helpers for main controlller
480
	function createHelper($helperName) {
481
		$pluginPath = !empty($this->pluginPath)	? $this->pluginPath : PLUGIN_PATH;
482
		include_once($pluginPath . "/".strtolower($helperName).".ctrl.php");
483
		$helperObj = New $helperName();
484
		$helperObj->pluginPath = $this->pluginPath;
485
		$helperObj->pluginId = $this->pluginId;
486
		$helperObj->pluginViewPath = $this->pluginViewPath;
487
		$helperObj->pluginWebPath = $this->pluginWebPath;
488
		$helperObj->pluginImagePath = $this->pluginImagePath;
489
		$helperObj->pluginCssPath = $this->pluginCssPath;
490
		$helperObj->pluginJsPath = $this->pluginJsPath;
491
		$helperObj->pluginScriptUrl = $this->pluginScriptUrl;
492
		$helperObj->data = $this->data;
493
		$helperObj->pluginText = $this->pluginText;
494
		return $helperObj;
495
	}
496
 
497
	# func to get plugin language texts
498
	function getPluginLanguageTexts($category, $langCode='en', $table='') {
499
		$langTexts = array();
500
 
501
		$sql = "select label,content from $table where category='$category' and lang_code='$langCode' and content!='' order by label";
502
		$textList = $this->db->select($sql);
503
		foreach ($textList as $listInfo) {
504
			$langTexts[$listInfo['label']] = stripslashes($listInfo['content']);
505
		}
506
 
507
		# if langauge is not english
508
		if ($langCode != 'en') {
509
			$defaultTexts = $this->getPluginLanguageTexts($category, 'en', $table);
510
			foreach ($defaultTexts as $label => $content) {
511
				if (empty($langTexts[$label])) {
512
					$langTexts[$label] = $content;
513
				}
514
			}
515
		}
516
 
517
		return $langTexts;
518
	}
519
 
520
	# func to set language texts
521
	function setPluginTextsForRender($category='', $table='') {
522
 
523
		if (empty($this->pluginText)) {
524
			$this->pluginText = $this->getPluginLanguageTexts($category, $_SESSION['lang_code'], $table);
525
			$this->set('pluginText', $this->pluginText);
526
		}
527
	}
528
 
529
	# function to check whether a plugin is installed and active
530
	function isPluginActive($value, $col = 'name') {
531
		$sql = "select * from seoplugins where $col='".addslashes($value)."' and installed=1 and status=1";
532
		$pluginInfo = $this->db->select($sql, true);
533
		return empty($pluginInfo['id']) ? false : $pluginInfo;
534
	}
535
 
536
	# function to create plugin object
537
	function createPluginObject($pluginName, $info = array()) {
538
		$pluginInfo = $this->__getSeoPluginInfo($pluginName, 'name');
539
		$info['pid'] = $pluginInfo['id'];
540
		$info['action'] = "get_plugin_object";
541
		$pluginCtrler = $this->manageSeoPlugins($info, 'get', true);
542
		return $pluginCtrler;
543
	}
544
 
545
}
546
?>