1<?php
2/*
3 * e107 website system
4 *
5 * Copyright (C) 2008-2013 e107 Inc (e107.org)
6 * Released under the terms and conditions of the
7 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
8 *
9 * Administration - Database Utilities
10 *
11*/
12
13require_once ("../class2.php");
14$theme = e107::getPref('sitetheme');
15define("EXPORT_PATH","{e_THEME}".$theme."/install/");
16
17if(!getperms('0'))
18{
19	e107::redirect('admin');
20	exit();
21}
22
23if(isset($_POST['back']))
24{
25	header("location: ".e_SELF);
26	exit();
27}
28
29e107::coreLan('db', true);
30
31$e_sub_cat = 'database';
32
33$frm = e107::getForm();
34$mes = e107::getMessage();
35
36if(isset($_GET['mode']))
37{
38    $_GET['mode'] = preg_replace('/[^\w\-]/', '', $_GET['mode']);
39}
40
41if(isset($_GET['type']))
42{
43    $_GET['type'] = preg_replace('/[^\w\-]/', '', $_GET['type']);
44}
45
46/*
47 * Execute trigger
48 */
49if(isset($_POST['db_execute']))
50{
51	$type = key($_POST['db_execute']);
52
53	if(!varset($_POST['db_execute']))
54	{
55		$mes->add(DBLAN_53, E_MESSAGE_WARNING);
56	}
57	else
58	{
59		$_POST[$type] = true;
60	}
61}
62
63
64
65
66
67if(isset($_POST['exportXmlFile']))
68{
69
70
71	if(exportXmlFile($_POST['xml_prefs'],$_POST['xml_tables'],$_POST['xml_plugprefs'],$_POST['xml_themeprefs'], $_POST['package_images'], false))
72	{
73		$mes = e107::getMessage();
74		$mes->add(LAN_CREATED, E_MESSAGE_SUCCESS);
75	}
76
77}
78
79if(e_AJAX_REQUEST )
80{
81
82	session_write_close();
83	while (@ob_end_clean());
84
85	if(varset($_GET['mode']) == 'backup') //FIXME - not displaying progress until complete. Use e-progress?
86	{
87		echo "".DBLAN_120."<br />";
88
89		$data = array();
90		$data[] = e_MEDIA;
91		$data[] = e_LOG;
92		$data[] = e_IMPORT;
93		$data[] = e_TEMP;
94		$data[] = e_SYSTEM."filetypes.xml";
95		$data[] = e_THEME.e107::getPref('sitetheme');
96
97		$plugins = e107::getPlugin()->getOtherPlugins();
98		foreach($plugins as $dir)
99		{
100			$data[] = e_PLUGIN.$dir;
101		}
102
103		$newFile = eHelper::title2sef(SITENAME)."_".date("Y-m-d-H-i-s");
104
105		$zip = e107::getFile()->zip($data, e_BACKUP.$newFile.".zip");
106
107		echo DBLAN_60." <small>(".$zip.")</small><br />";
108
109		echo DBLAN_61."<br />";
110
111		$dbfile = e107::getDb()->backup('*', $newFile.".sql", array('nologs'=>1, 'droptable'=>1));
112
113		echo DBLAN_62." <small>(".$dbfile.")</small>";
114
115		e107::getAdminLog()->addSuccess($zip." ".$dbfile, false)->save(DBLAN_63);
116
117	}
118
119	exit;
120
121}
122
123require_once ("auth.php");
124
125$st = new system_tools;
126
127
128/* No longer needed after XML feature added.
129
130if(isset($_POST['backup_core']) || $_GET['mode']=='backup_core')
131{
132	backup_core();
133	//message_handler("MESSAGE", DBLAN_1);
134	$mes->add(DBLAN_1, E_MESSAGE_SUCCESS);
135}
136
137*/
138
139
140
141
142
143
144
145
146
147
148
149require_once ("footer.php");
150
151class system_tools
152{
153
154	public $_options = array();
155
156	private $_utf8_exclude = array();
157
158
159	function __construct()
160	{
161		global $mySQLdefaultdb;
162
163		$this->_utf8_exclude = array(MPREFIX."core");
164
165		$this->_options = array(
166			"db_update"				=> array('diz'=>DBLAN_15, 'label'=>DBLAN_16),
167			"verify_sql"			=> array('diz'=>DBLAN_4, 'label'=>DBLAN_5),
168			'optimize_sql'			=> array('diz'=>DBLAN_6, 'label'=> DBLAN_7),
169			'plugin_scan'			=> array('diz'=>DBLAN_28, 'label'=> DBLAN_29),
170			'pref_editor'			=> array('diz'=>DBLAN_19, 'label'=> DBLAN_20),
171		//	'backup_core'			=> array('diz'=>DBLAN_8, 'label'=> DBLAN_9),
172		//	'verify_sql_record'		=> array('diz'=>DBLAN_35, 'label'=> DBLAN_36),
173			'importForm'			=> array('diz'=>DBLAN_59, 'label'=> DBLAN_59),
174			'exportForm'			=> array('diz'=>DBLAN_58, 'label'=> DBLAN_58),
175			'sc_override_scan'		=> array('diz'=>DBLAN_55, 'label'=> DBLAN_56),
176			'convert_to_utf8'		=> array('diz'=>DBLAN_64,'label'=>DBLAN_65),
177			'correct_perms'			=> array('diz'=>DBLAN_66,'label'=>DBLAN_67),
178			'backup'				=> array('diz'=>DBLAN_68,'label'=>DBLAN_69)
179		);
180
181		if(deftrue('e_DEVELOPER'))
182		{
183			$this->_options['multisite'] = array('diz'=>"<span class='label label-warning'>".DBLAN_114."</span>", 'label'=> 'Multi-Site' );
184			$this->_options['github'] = array('diz'=>"<span class='label label-warning'>".DBLAN_114."</span> ".DBLAN_115."", 'label'=> DBLAN_112 );
185		}
186
187
188
189		$this->_options = multiarray_sort($this->_options, 'label');
190
191		if(isset($_POST['delplug']))
192		{
193			$this->delete_plugin_entry(); // $_POST['pref_type']
194		}
195
196		if(isset($_POST['upload']))
197		{
198			$this->importXmlFile();
199		}
200
201		if(isset($_POST['delpref']) || (isset($_POST['delpref_checked']) && isset($_POST['delpref2'])))
202		{
203			$this->del_pref_val($_POST['pref_type']);
204		}
205
206		if(isset($_POST['verify_sql']) || !empty($_POST['verify_table']) || varset($_GET['mode']) =='verify_sql')
207		{
208			e107::getCache()->clear('Dbverify',true);
209			require_once(e_HANDLER."db_verify_class.php");
210			$dbv = new db_verify;
211			$dbv->backUrl = e_SELF."?mode=verify_sql";
212			$dbv->verify();
213
214			//echo e107::getMessage()->render();
215			return;
216		}
217
218		// ----------------- Processes ------------------
219
220	//	if(isset($_POST['verify_sql_record']) || varset($_GET['mode'])=='verify_sql_record' || isset($_POST['check_verify_sql_record']) || isset($_POST['delete_verify_sql_record']))
221	//	{
222
223			 //$this->verify_sql_record(); // - currently performed in db_verify_class.php
224	//	}
225
226		if(isset($_POST['importForm']) ||  $_GET['mode']=='importForm')
227		{
228			$this->importForm();
229		}
230
231		if(isset($_POST['db_update']) || varset($_GET['mode'])=='db_update') // Requires further testing.
232		{
233		//	header("location: ".e_ADMIN."e107_update.php");
234			$dbupdate = null;
235			require_once(e_ADMIN."update_routines.php");
236			new e107Update($dbupdate);
237			return;
238		}
239
240		if(isset($_POST['convert_to_utf8']) ||  $_GET['mode'] =='convert_to_utf8')
241		{
242			$this->convertUTF8Form();
243		}
244
245		if(isset($_POST['exportForm']) ||  $_GET['mode']=='exportForm')
246		{
247			$this->exportXmlForm();
248		}
249
250		if(isset($_POST['optimize_sql']) || $_GET['mode']=='optimize_sql')
251		{
252			$this->optimizesql($mySQLdefaultdb);
253		}
254
255		if(isset($_POST['pref_editor']) || $_GET['mode']=='pref_editor' || isset($_POST['delpref']) || isset($_POST['delpref_checked']))
256		{
257			$type = isset($_GET['type']) ? $_GET['type'] : "core";
258			$this->pref_editor($type);
259		}
260
261		if(isset($_POST['sc_override_scan']) || $_GET['mode']=='sc_override_scan')
262		{
263			$this->scan_override();
264		}
265
266		if(isset($_POST['plugin_scan']) || e_QUERY == "plugin" || isset($_POST['delplug']) || $_GET['mode']=='plugin_scan')
267		{
268			$this->plugin_viewscan('refresh');
269		}
270
271		if(!empty($_POST['create_multisite']))
272		{
273			$this->multiSiteProcess();
274		}
275
276		if(!empty($_POST['perform_utf8_convert']))
277		{
278			$this->perform_utf8_convert();
279			return;
280		}
281
282		if(!empty($_POST['githubSyncProcess']))
283		{
284			$this->githubSyncProcess();
285			return;
286		}
287
288
289
290		// --------------------- Modes --------------------------------.
291
292
293		if(varset($_GET['mode'])=='correct_perms')
294		{
295			$this->correct_perms();
296			return;
297		}
298
299		if(varset($_GET['mode'])=='multisite')
300		{
301			$this->multiSite();
302			return;
303		}
304
305		if(varset($_GET['mode']) == 'github')
306		{
307			$this->githubSync();
308		}
309
310		if(varset($_GET['mode']) == 'backup')
311		{
312			$this->backup();
313			return;
314		}
315
316		if(!vartrue($_GET['mode']) && !isset($_POST['db_execute']))
317		{
318			$this->render_options();
319		}
320
321
322
323	}
324
325
326	// Developer Mode ONly.. No LANS required.
327	private function githubSync()
328	{
329		$frm = e107::getForm();
330		$mes = e107::getMessage();
331		$pref = e107::pref();
332
333		if(empty($pref['developer']))
334		{
335			e107::getMessage()->addError("Developer mode has to be enabled in order to use this functionality!");
336			e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_112, $mes->render());
337			return;
338		}
339
340		// Check for minimum required PHP version, and display warning instead of sync button to avoid broken functionality after syncing
341		// MIN_PHP_VERSION constant only defined in install.php, thus hardcoded here
342		$min_php_version = '5.6';
343
344		if(version_compare(PHP_VERSION, $min_php_version, "<"))
345		{
346			$mes->addWarning("The minimum required PHP version is <strong>".$min_php_version."</strong>. You are using PHP version <strong>".PHP_VERSION."</strong>. <br /> Syncing with Github has been disabled to avoid broken fuctionality."); // No need to translate, developer mode only
347		}
348		else
349		{
350			$message = $frm->open('githubSync');
351			$message .= "<p>".DBLAN_116." <b>".e_SYSTEM."temp</b> ".DBLAN_117." </p>";
352			$message .= $frm->button('githubSyncProcess',1,'delete', DBLAN_113);
353			$message .= $frm->close();
354
355			$mes->addInfo($message);
356		}
357
358
359		e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_112, $mes->render());
360	}
361
362
363
364	// Developer Mode ONly.. No LANS.
365	private function githubSyncProcess()
366	{
367		$result = e107::getFile()->unzipGithubArchive('core');
368
369		if($result === false)
370		{
371			e107::getMessage()->addError( DBLAN_118 );
372			return null;
373		}
374
375		$success = $result['success'];
376		$error = $result['error'];
377
378	//		$message = e107::getParser()->lanVars(DBLAN_121, array('x'=>$oldPath, 'y'=>$newPath));
379
380		if(!empty($success))
381		{
382			e107::getMessage()->addSuccess(print_a($success,true));
383		}
384
385		if(!empty($skipped))
386		{
387			e107::getMessage()->setTitle("Skipped",E_MESSAGE_INFO)->addInfo(print_a($skipped,true));
388		}
389
390		if(!empty($error))
391		{
392			//e107::getMessage()->addError(print_a($error,true));
393			e107::getMessage()->setTitle("Ignored",E_MESSAGE_WARNING)->addWarning(print_a($error,true));
394		}
395
396		e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_112, e107::getMessage()->render());
397
398		e107::getCache()->clearAll('system');
399
400	}
401
402
403
404
405
406	private function backup()
407	{
408
409		$mes = e107::getMessage();
410
411		$message = DBLAN_70;
412		$message .= "<br /><a class='e-ajax btn btn-success' data-loading-text='".DBLAN_71."' href='#backupstatus' data-src='".e_SELF."?mode=backup' >".LAN_CREATE."</a>";
413
414
415		$mes->addInfo($message);
416
417		$text = "<div id='backupstatus' style='margin-top:20px'></div>";
418
419
420		e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_119, $mes->render().$text);
421	}
422
423
424
425	/**
426	 * Correct Folder and File permissions.
427	 */
428	function correct_perms()
429	{
430		$mes = e107::getMessage();
431		$fl = e107::getFile();
432		ob_start();
433		$fl->chmod(e_BASE);
434		$fl->chmod(e_BASE."cron.php",0755);
435		$errors = ob_get_clean();
436
437		if($errors !='')
438		{
439			$mes->addError($errors);
440		}
441		else
442		{
443			$mes->addSuccess(DBLAN_72);
444		}
445
446		e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_73, $mes->render());
447
448	}
449
450	private function multiSiteProcess()
451	{
452		$sql 		= e107::getDb('new');
453		$mes 		= e107::getMessage();
454
455		$user 		= $_POST['name'];
456		$pass 		= $_POST['password'];
457		$server 	= e107::getMySQLConfig('server'); // $_POST['server'];
458		$database 	= $_POST['db'];
459		$prefix		= $_POST['prefix'];
460
461		if($connect = $sql->connect($server,$user, $pass, true))
462		{
463			$mes->addSuccess(DBLAN_74);
464
465			if(vartrue($_POST['createdb']))
466			{
467
468				if($sql->gen("CREATE DATABASE ".$database." CHARACTER SET `utf8`"))
469				{
470					$mes->addSuccess(DBLAN_75);
471
472				//	$sql->gen("CREATE USER ".$user."@'".$server."' IDENTIFIED BY '".$pass."';");
473					$sql->gen("GRANT ALL ON `".$database."`.* TO ".$user."@'".$server."';");
474					$sql->gen("FLUSH PRIVILEGES;");
475				}
476				else
477				{
478					$mes->addError(DBLAN_75);
479					return;
480				}
481			}
482
483			if(!$sql->database($database))
484			{
485				$mes->addError(DBLAN_76);
486			}
487
488			$mes->addSuccess(DBLAN_76);
489
490			if($this->multiSiteCreateTables($sql, $prefix))
491			{
492				$coreConfig = e_CORE. "xml/default_install.xml";
493				$ret = e107::getXml()->e107Import($coreConfig, 'add', true, false, $sql); // Add core pref values
494				$mes->addInfo(print_a($ret,true));
495			}
496
497		}
498		else
499		{
500			$mes->addSuccess(DBLAN_74);
501		}
502
503		if($error = $sql->getLastErrorText())
504		{
505			$mes->addError($error);
506		}
507
508		//	print_a($_POST);
509
510
511	}
512
513	/**
514	 * @param object $sql
515	 * @param $prefix
516	 * @return bool
517	 */
518	private function multiSiteCreateTables($sql, $prefix)
519	{
520		$mes = e107::getMessage();
521
522		$sql_data = file_get_contents(e_CORE."sql/core_sql.php");
523		$sql_data = preg_replace("#\/\*.*?\*\/#mis", '', $sql_data);		// Strip comments
524
525		if (!$sql_data)
526		{
527			$mes->addError(DBLAN_77);
528		}
529
530		preg_match_all("/create(.*?)(?:myisam|innodb);/si", $sql_data, $result );
531
532
533		$sql->gen('SET NAMES `utf8`');
534
535		foreach ($result[0] as $sql_table)
536		{
537			$sql_table = preg_replace("/create table\s/si", "CREATE TABLE ".$prefix, $sql_table);
538
539			if (!$sql->gen($sql_table))
540			{
541				$mes->addError($sql->getLastErrorText());
542				return false;
543			}
544			else
545			{
546				// $mes->addDebug($sql_table);
547			}
548		}
549
550		return true;
551	}
552
553
554	private function multiSite()
555	{
556
557		if(!deftrue('e_DEVELOPER'))
558		{
559			return false;
560		}
561
562		$mes = e107::getMessage();
563		$frm = e107::getForm();
564
565		e107::lan('core','installer');
566
567		// Leave here until no longer experimental. - Should be placed inside lan_db.php and LANS renamed.
568		define('LANINS_130', "Parked Domain");
569		define('LANINS_131', "The parked domain which will become a new e107 website.");
570		define('LANINS_132', "mydomain.com");
571		define('LANINS_133', "This will create a fresh installation of e107 at the domain you specify. Using your server administration software (e.g. cPanel) - park your other domain on top of [x]");
572
573
574		$config = e107::getMySQLConfig(); // prefix|server|user|password|defaultdb
575
576		if(!isset($POST['create_multisite']))
577		{
578			$info = str_replace('[x]', e_DOMAIN, LANINS_133);
579			$mes->addInfo($info);
580		}
581
582		$text = $frm->open('multisite')."
583			<table class='table table-striped' >
584			<tr>
585					<td><label for='server'>".LANINS_130."</label></td>
586					<td>
587						<input class='tbox' type='text' placeholder='".LANINS_132."' id='domain' name='domain' autofocus size='40' value='' maxlength='100' required='required' />
588						<span class='field-help'>".LANINS_131."</span>
589					</td>
590				</tr>
591				";
592			/*
593				$text .= "
594				<tr>
595					<td><label for='server'>".LANINS_024."</label></td>
596					<td>
597						<input class='tbox' type='text' id='server' name='server' autofocus size='40' value='localhost' maxlength='100' required='required' />
598						<span class='field-help'>".LANINS_030."</span>
599					</td>
600				</tr>";
601			*/
602				$text .= "
603
604				<tr>
605					<td><label for='name'>".LANINS_025."</label></td>
606					<td>
607						<input class='tbox' type='text' name='name' id='name' size='40' value='".e107::getMySQLConfig('user')."' maxlength='100' required='required' />
608						<span class='field-help'>".LANINS_031."</span>
609					</td>
610				</tr>
611
612				<tr>
613					<td><label for='password'>".LANINS_026."</label></td>
614					<td>
615						<input class='tbox' type='password' name='password' size='40' id='password' value='".e107::getMySQLConfig('password')."' maxlength='100'  />
616						<span class='field-help'>".LANINS_032."</span>
617					</td>
618				</tr>
619				";
620
621				$text .= "
622				<tr>
623					<td><label for='db'>".LANINS_027."</label></td>
624					<td class='input-inline'>
625						<input type='text' name='db' size='20' id='db' value='' maxlength='100' required='required' />
626						<label class='checkbox inline'><input type='checkbox' name='createdb' value='1' />".LANINS_028."</label>
627						<span class='field-help'>".LANINS_033."</span>
628					</td>
629				</tr>";
630
631
632
633				$text .= "
634
635				<tr>
636					<td><label for='prefix'>".LANINS_029."</label></td>
637					<td>
638						<input type='text' name='prefix' size='20' id='prefix' value='e107_'  pattern='[a-z0-9]*_$' maxlength='100' required='required' />
639						<span class='field-help'>".LANINS_034."</span>
640					</td>
641				</tr>
642
643
644			\n";
645
646		$text .= "
647
648				<tr>
649					<td><label for='u_name'>".LANINS_072."</label></td>
650					<td>
651						<input class='tbox' type='text' autofocus name='u_name' id='u_name' placeholder='admin' size='30' required='required' value='".USERNAME."' maxlength='60' />
652						<span class='field-help'>".LANINS_073."</span>
653					</td>
654				</tr>
655
656				<tr>
657					<td><label for='d_name'>".LANINS_074."</label></td>
658					<td>
659						<input class='tbox' type='text' name='d_name' id='d_name' size='30' placeholder='Administrator'  value='".USERNAME."' maxlength='60' />
660						<span class='field-help'>".LANINS_123."</span>
661					</td>
662				</tr>
663
664				<tr>
665					<td><label for='pass1'>".LANINS_076."</label></td>
666					<td>
667						<input type='password' name='pass1' size='30' id='pass1' value='' maxlength='60' required='required' />
668						<span class='field-help'>".LANINS_124."</span>
669					</td>
670				</tr>
671
672				<tr>
673					<td><label for='pass2'>".LANINS_078."</label></td>
674					<td>
675						<input type='password' name='pass2' size='30' id='pass2' value='' maxlength='60' required='required' />
676						<span class='field-help'>".LANINS_079."</span>
677					</td>
678				</tr>
679
680				<tr>
681					<td><label for='email'>".LANINS_080."</label></td>
682					<td>
683						<input type='text' name='email' size='30' id='email' required='required' placeholder='admin@mysite.com' value='".USEREMAIL."' maxlength='100' />
684					<span class='field-help'>".LANINS_081."</span>
685					</td>
686				</tr>
687			</table>
688			<div class='buttons-bar text-center'>
689			".$frm->admin_button('create_multisite',1,'submit','Create New Site')."
690			</div>
691			\n";
692
693		$text .= $frm->close();
694
695
696		e107::getRender()->tablerender(DBLAN_10.SEP."Multi-Site".SEP.$config['mySQLdefaultdb'], $mes->render().$text);
697
698	}
699
700
701	private function convertUTF8Form()
702	{
703		$mes 	= e107::getMessage();
704		$frm 	= e107::getForm();
705		$config = e107::getMySQLConfig();
706		$sql 	= e107::getDb();
707		$tp = e107::getParser();
708
709		$sql->gen('SHOW TABLE STATUS WHERE Name LIKE "'.$config['mySQLprefix'].'%" ');
710
711
712		$text = "<table class='table adminlist'>
713							<colgroup>
714								<col style='width: auto' />
715								<col style='width: auto' />
716								<col style='width: auto' />
717								<col style='width: auto' />
718							</colgroup>
719							<thead>
720								<tr>
721
722									<th>".DBLAN_78."</th>
723									<th>".DBLAN_79."</th>
724									<th>".DBLAN_80."</th>
725									<th>".DBLAN_81."</th>
726								</tr>
727							</thead>
728							<tbody>";
729
730
731
732		$invalidCollations = false;
733		while($row = $sql->fetch())
734		{
735				if(in_array($row['Name'],$this->_utf8_exclude))
736				{
737					continue;
738				}
739
740
741				$text .= "<tr>
742					<td>".$row['Name']."</td>
743					<td>".$row['Engine']."</td>
744					<td>".$row['Collation']."</td>
745					<td>".(($row['Collation'] == 'utf8_general_ci') ? ADMIN_TRUE_ICON : ADMIN_FALSE_ICON)."</td>
746					</tr>";
747			//	 print_a($row);
748
749				if($row['Collation'] != 'utf8_general_ci')
750				{
751					$invalidCollations = true;
752				}
753
754		}
755
756		$text .= "</tbody></table>";
757
758
759		if($invalidCollations == true)
760		{
761			$message = str_replace('[database]', $config['mySQLdefaultdb'], DBLAN_82);
762			$message .= '<br/>';
763			$message .= DBLAN_83;
764			$message .= '<br/>';
765			$message .= '<br/>';
766			$message .= DBLAN_84;
767			$message .= '<ul>';
768			$message .= '<li>'.DBLAN_85.'</li>';
769			$message .= '<li>'.DBLAN_86.'</li>';
770			$message .= '<li>'.DBLAN_87.'</li>';
771			$message .= '<li>'.DBLAN_88.'</li>';
772			$message .= '</ul>';
773
774			$mes->add($tp->toHTML($message,true), E_MESSAGE_WARNING);
775
776			$text .= "
777				<form method='post' action='".e_SELF."' id='linkform'>
778					<fieldset id='core-db-utf8-convert'>
779						<legend class='e-hideme'>".DBLAN_89."</legend>
780						<div class='buttons-bar center'>
781							".$frm->admin_button('perform_utf8_convert', DBLAN_90,false,DBLAN_90,'class=btn-success&data-loading-text='.DBLAN_91)."
782						</div>
783					</fieldset>
784				</form>";
785
786		}
787		else
788		{
789			$mes->addSuccess(DBLAN_92);
790		}
791
792
793		e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_65.SEP.$config['mySQLdefaultdb'], $mes->render().$text);
794
795	}
796
797	private function perform_utf8_convert()
798	{
799		$config = e107::getMySQLConfig();
800		$dbtable = $config['mySQLdefaultdb'];
801
802		//TODO Add a check to be sure the database is not already utf-8.
803		// yep, needs more methods - possibly a class in e107_handler
804
805		$sql = e107::getDb('utf8-convert');
806		$mes = e107::getMessage();
807
808		$ERROR = FALSE;
809
810	//	if(!$sql->gen("USE information_schema;"))
811	//	{
812	//		$mes->add("Couldn't read information_schema", E_MESSAGE_ERROR);
813	//		return;
814	//	}
815
816
817		$queries = array();
818		$queries[] = $this->getQueries("SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY ', column_name, ' ', REPLACE(column_type, 'char', 'binary'), ';') FROM information_schema.columns WHERE TABLE_SCHEMA = '".$dbtable."' AND TABLE_NAME LIKE '".$config['mySQLprefix']."%' AND  COLLATION_NAME != 'utf8_general_ci'  and data_type LIKE '%char%';");
819		$queries[] = $this->getQueries("SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY ', column_name, ' ', REPLACE(column_type, 'text', 'blob'), ';') FROM information_schema.columns WHERE TABLE_SCHEMA = '".$dbtable."' AND TABLE_NAME LIKE '".$config['mySQLprefix']."%' AND  COLLATION_NAME != 'utf8_general_ci' and data_type LIKE '%text%';");
820
821		$queries2 = array();
822		$queries2[] = $this->getQueries("SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY ', column_name, ' ', column_type, ' CHARACTER SET utf8;') FROM information_schema.columns WHERE TABLE_SCHEMA ='".$dbtable."' AND TABLE_NAME LIKE '".$config['mySQLprefix']."%'  AND COLLATION_NAME != 'utf8_general_ci' and data_type LIKE '%char%';");
823		$queries2[] = $this->getQueries("SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY ', column_name, ' ', column_type, ' CHARACTER SET utf8;') FROM information_schema.columns WHERE TABLE_SCHEMA = '".$dbtable."' AND TABLE_NAME LIKE '".$config['mySQLprefix']."%' AND  COLLATION_NAME != 'utf8_general_ci' and data_type LIKE '%text%';");
824
825
826	//	$sql->gen("USE ".$dbtable);
827
828
829	//	print_a($queries2);
830	//	echo $mes->render();
831	//	return;
832
833
834		// Convert Text tables to Binary.
835		foreach($queries as $qry)
836		{
837
838			foreach($qry as $q)
839			{
840				if(!$sql->db_Query($q))
841				{
842					$mes->addError($q);
843					$ERROR = TRUE;
844				}
845				else
846				{
847					$mes->addDebug($q);
848				}
849			}
850		}
851
852		//------------
853
854		// Convert Table Fields to utf8
855		$sql2 = e107::getDb('sql2');
856
857		$sql->gen('SHOW TABLE STATUS WHERE Collation != "utf8_general_ci" ');
858		while ($row = $sql->fetch())
859		{
860   			$table = $row['Name'];
861
862			if(in_array($row['Name'], $this->_utf8_exclude))
863			{
864				continue;
865			}
866
867
868			$tab_query = "ALTER TABLE ".$table."  DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ";
869
870			//echo "TABQRT= ".$tab_query;
871
872			if(!$sql2->db_Query($tab_query))
873			{
874				$mes->addError($tab_query);
875				$ERROR = TRUE;
876			}
877			else
878			{
879				$mes->addDebug($tab_query);
880			}
881		}
882
883		// ---------------
884		// Convert Table Fields back to Text/varchar etc.
885		foreach($queries2 as $qry)
886		{
887			foreach($qry as $q)
888			{
889				if(!$sql->db_Query($q))
890				{
891					$mes->addError($q);
892					$ERROR = TRUE;
893				}
894				else
895				{
896					$mes->addDebug($q);
897				}
898			}
899		}
900
901		//------------
902
903		$lastQry = "ALTER DATABASE `".$dbtable."` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
904
905		if(!$sql->db_Query($lastQry))
906		{
907			$mes->add($lastQry, E_MESSAGE_ERROR);
908		}
909		elseif($ERROR != TRUE)
910		{
911			$message = DBLAN_93;
912			//$message .= "<br />Please now add the following line to your e107_config.php file:<br /><b>\$mySQLcharset   = 'utf8';</b>";
913
914			$mes->add($message, E_MESSAGE_SUCCESS);
915			$mes->addSuccess(DBLAN_94);
916			$mes->addSuccess('$mySQLcharset   = "utf8";');
917
918		}
919
920		echo $mes->render();
921	}
922
923	function getQueries($query)
924	{
925
926		$mes = e107::getMessage();
927		$sql = e107::getDb('utf8-convert');
928
929		if($sql->gen($query))
930		{
931			while ($row = $sql->fetch('num'))
932			{
933	   			 $qry[] = $row[0];
934			}
935		}
936		else
937		{
938			$mes->addError($query);
939		}
940
941		return $qry;
942
943
944		/*
945		if(!$result = mysql_query($query))
946		{
947			$mes->addError("Query Failed: ".$query);
948			return;
949		}
950		while ($row = mysql_fetch_array($result, 'num'))
951		{
952   			 $qry[] = $row[0];
953		}
954
955		return $qry;
956		 * */
957	}
958
959
960	/**
961	 * Delete selected preferences.
962	 * @return null
963	 */
964	private function del_pref_val($mode='core')
965	{
966		$mes = e107::getMessage();
967
968		$deleted_list = "";
969
970		$config = ($mode == 'core' || $mode=='') ? e107::getConfig('core') : e107::getPlugConfig($mode);
971
972
973		// Single Pref Deletion	using button
974		if(varset($_POST['delpref']))
975		{
976			$delpref = key($_POST['delpref']);
977			if($config->remove($delpref))
978			{
979				$deleted_list .= "<li>".$delpref."</li>";
980			}
981		}
982
983		// Multiple Pref deletion using checkboxes
984		if(varset($_POST['delpref2']))
985		{
986			foreach($_POST['delpref2'] as $k => $v)
987			{
988				if($config->remove($k))
989				{
990					$deleted_list .= "<li>".$k."</li>";
991				}
992			}
993		}
994
995		if($deleted_list && $config->save())
996		{
997			$mes->add(LAN_DELETED."<ul>".$deleted_list."</ul>");
998			e107::getCache()->clear();
999		}
1000
1001		return null;
1002
1003	}
1004
1005	private function delete_plugin_entry()
1006	{
1007
1008		$mes = e107::getMessage();
1009		$sql = e107::getDb();
1010
1011		$del = array_keys($_POST['delplug']);
1012		if($sql->delete("plugin", "plugin_id='".intval($del[0])."'"))
1013		{
1014			$mes->add(LAN_DELETED, E_MESSAGE_SUCCESS);
1015		}
1016		else
1017		{
1018			$mes->add(LAN_DELETED_FAILED, E_MESSAGE_WARNING);
1019		}
1020
1021	}
1022
1023
1024	/**
1025	 * Render Options
1026	 * @return null
1027	 */
1028	private function render_options()
1029	{
1030
1031		$mes = e107::getMessage();
1032
1033		$text = "
1034		<form method='post' action='".e_SELF."' id='core-db-main-form'>
1035			<fieldset id='core-db-plugin-scan'>
1036			<legend class='e-hideme'>".DBLAN_10."</legend>
1037				<table class='table table-striped adminlist'>
1038				<colgroup>
1039					<col style='width: 60%' />
1040					<col style='width: 40%' />
1041				</colgroup>
1042				<tbody>";
1043
1044		$text = "<div>";
1045
1046
1047		foreach($this->_options as $key=>$val)
1048		{
1049
1050			$text .= "<div class='pull-left' style='width:50%;padding-bottom:10px'>
1051			<a class='btn btn-default btn-secondary btn-large pull-left' style='margin-right:10px' href='".e_SELF."?mode=".$key."' title=\"".$val['label']."\">".ADMIN_EXECUTE_ICON."</a>
1052			<h4 style='margin-bottom:3px'><a href='".e_SELF."?mode=".$key."' title=\"".$val['label']."\">".$val['label']."</a></h4><small>".$val['diz']."</small>
1053			</div>";
1054
1055		}
1056/*
1057		$text .= "
1058
1059				</tbody>
1060				</table>";
1061		// $text .= "<div class='buttons-bar center'>
1062					// ".$frm->admin_button('trigger_db_execute', DBLAN_51, 'execute')."
1063				// </div>";
1064		$text .= "
1065			</fieldset>
1066		</form>
1067		";
1068*/
1069		e107::getRender()->tablerender(DBLAN_10, $mes->render().$text);
1070
1071		return null;
1072	}
1073
1074
1075	/**
1076	 * Import XML Form
1077	 * @return null
1078	 */
1079	private function importForm()
1080	{
1081		 // Get largest allowable file upload
1082
1083		$frm = e107::getSingleton('e_form');
1084		$mes = e107::getMessage();
1085
1086				require_once(e_HANDLER.'upload_handler.php');
1087				  $max_file_size = get_user_max_upload();
1088
1089				  $text = "
1090					<form enctype='multipart/form-data' method='post' action='".e_SELF."?mode=".$_GET['mode']."'>
1091	                <table class='table adminform'>
1092	                	<colgroup>
1093	                		<col class='col-label' />
1094	                		<col class='col-control' />
1095	                	</colgroup>
1096
1097
1098					<tbody>
1099					<tr>
1100					<td>".LAN_UPLOAD."</td>
1101					<td>
1102						<input type='hidden' name='MAX_FILE_SIZE' value='{$max_file_size}' />
1103						<input type='hidden' name='ac' value='".md5(ADMINPWCHANGE)."' />
1104						<input class='tbox' type='file' name='file_userfile[]' accept='text/xml' size='50' />
1105					</td>
1106					</tr>
1107					</tbody>
1108					</table>
1109
1110					<div class='center buttons-bar'>";
1111	                $text .= $frm->admin_button('upload', LAN_UPLOAD, 'submit', LAN_UPLOAD);
1112
1113					$text .= "
1114					</div>
1115
1116					</form>\n";
1117
1118
1119		e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_59, $mes->render().$text);
1120
1121		return null;
1122	}
1123
1124	/**
1125	 * Export XML Dump
1126	 * @return null
1127	 */
1128	private function exportXmlForm()
1129	{
1130		$mes = e107::getMessage();
1131		$frm = e107::getSingleton('e_form');
1132
1133		$text = "<form method='post' action='".e_SELF."?".e_QUERY."' id='core-db-export-form'>
1134			<fieldset id='core-db-export'>
1135			<legend class='e-hideme'>".DBLAN_95."</legend>
1136				<table class='table adminlist'>
1137				<colgroup>
1138					<col style='width: 80%' />
1139					<col style='width: 20%' />
1140				</colgroup>
1141				<thead>
1142				<tr>
1143					<th class='form-inline'>".$frm->checkbox_toggle('check-all-verify', 'xml_prefs')." &nbsp;".LAN_PREFS."</th>
1144					<th class='right'>".DBLAN_98."</th>
1145
1146				</tr>
1147				</thead>
1148				<tbody>
1149
1150				";
1151
1152					$pref_types  = e107::getConfig()->aliases;
1153					unset($pref_types['core_old'], $pref_types['core_backup']);
1154			//		$exclusions = array('core_old'=>1,'core_backup'=>1);
1155				//	$filteredprefs = array_diff($pref_types,$exclusions);
1156
1157					foreach($pref_types as $key=>$description)
1158					{
1159						$data = e107::getConfig($key)->getPref();
1160
1161						$rows = count($data);
1162
1163						$checked = (vartrue($_POST['xml_prefs'][$key]) == $key) ? 1: 0;
1164
1165						$text .= "<tr>
1166							<td>
1167								".$frm->checkbox("xml_prefs[".$key."]", $key, $checked, array('label'=>LAN_PREFS.": ".$key))."
1168							</td>
1169							<td class='text-right'>".intval($rows)."</td>
1170
1171							</tr>";
1172
1173					}
1174
1175
1176					// Plugin Preferences ----------------------------
1177					$pluglist = e107::pref('core','plug_installed');
1178
1179					$text .= "</tbody><thead><tr>
1180					<th class='form-inline'>".$frm->checkbox_toggle('check-all-verify', 'xml_plugprefs')." &nbsp;Plugin ".LAN_PREFS."</th>
1181					<th class='right'>".DBLAN_98."</th>
1182
1183					</tr></thead><tbody>";
1184
1185					ksort($pluglist);
1186
1187					foreach($pluglist as $plug=>$ver)
1188					{
1189						$data = e107::getPlugConfig($plug)->getPref();
1190
1191						$key = $plug;
1192
1193						$checked = false;
1194
1195						if(!empty($data))
1196						{
1197							$rows = count($data);
1198
1199							$text .= "<tr>
1200							<td>
1201								".$frm->checkbox("xml_plugprefs[".$key."]",$key, $checked, array('label'=>LAN_PREFS.": ".$key))."
1202							</td>
1203							<td class='text-right'>".$rows."</td>
1204
1205							</tr>";
1206						}
1207					}
1208
1209
1210				// theme preferences
1211					$sitetheme = e107::pref('core','sitetheme'); // currently just sitetheme, but could easily be expanded.
1212					$themelist = array($sitetheme);
1213
1214					$text .= "</tbody><thead><tr>
1215					<th class='form-inline'>".$frm->checkbox_toggle('check-all-verify', 'xml_plugprefs')." &nbsp;Theme ".LAN_PREFS."</th>
1216					<th class='right'>".DBLAN_98."</th>
1217
1218					</tr></thead><tbody>";
1219
1220				//	ksort($themelist);
1221
1222					foreach($themelist as $plug)
1223					{
1224						$data = e107::getThemeConfig($plug)->getPref();
1225
1226						$key = $plug;
1227
1228						$checked = false;
1229
1230						if(!empty($data))
1231						{
1232							$rows = count($data);
1233
1234							$text .= "<tr>
1235							<td>
1236								".$frm->checkbox("xml_themeprefs[".$key."]",$key, $checked, array('label'=>LAN_PREFS.": ".$key))."
1237							</td>
1238							<td class='text-right'>".$rows."</td>
1239
1240							</tr>";
1241						}
1242					}
1243
1244
1245
1246
1247					$text .= "</tbody>
1248				</table>
1249				<table class='table adminlist'>
1250				<colgroup>
1251					<col style='width: 80%' />
1252					<col style='width: 20%' />
1253				</colgroup>
1254				<thead>
1255				<tr>
1256					<th class='form-inline'>".$frm->checkbox_toggle('check-all-verify', 'xml_tables')." &nbsp;".DBLAN_97."</th>
1257					<th class='right'>".DBLAN_98."</th>
1258
1259				</tr>
1260				</thead>
1261				<tbody>\n";
1262
1263					$tables = table_list();
1264
1265					foreach($tables as $name=>$count)
1266					{
1267						$checked = (vartrue($_POST['xml_tables'][$name]) == $name) ? 1: 0;
1268						$text .= "<tr>
1269							<td>
1270								".$frm->checkbox("xml_tables[".$name."]", $name, $checked, array('label'=>DBLAN_99." ".$name)).
1271							"</td>
1272							<td class='right'>$count</td>
1273						</tr>";
1274					}
1275
1276					$text .="
1277
1278					</tbody>
1279				</table>
1280
1281				<table class='table adminlist'>
1282				<colgroup>
1283					<col style='width: 80%' />
1284					<col style='width: 20%' />
1285				</colgroup>
1286				<thead>
1287				<tr>
1288					<th colspan='2'>".LAN_OPTIONS."</th>
1289				</tr>
1290				</thead>
1291				<tbody>
1292				<tr>
1293						<td colspan='2'>";
1294						$checked = (vartrue($_POST['package_images'])) ? 1: 0;
1295						$text .= $frm->checkbox("package_images",'package_images', $checked)." ".DBLAN_100." <i>".e107::getParser()->replaceConstants(EXPORT_PATH)."</i>
1296
1297						</td>
1298					</tr>
1299				</tbody>
1300				</table>
1301
1302				<div class='buttons-bar center'>
1303					".$frm->admin_button('exportXmlFile', DBLAN_101, 'other')."
1304				</div>
1305			</fieldset>
1306		</form>	";
1307
1308
1309		// display differences between default and core prefs.
1310/*
1311		$corePrefs = e107::pref('core');
1312
1313		$defaultArray = e107::getXml()->loadXMLfile(e_CORE."xml/default_install.xml", 'advanced');
1314		$defaultPrefs = e107::getXml()->e107ImportPrefs($defaultArray);
1315
1316		$text .= "<table class='table'>";
1317		foreach($defaultPrefs as $k=> $val)
1318		{
1319			if($val ==  $corePrefs[$k] || substr($k,-5) === '_list' || substr($k,0,9) == 'sitetheme')
1320			{
1321				continue;
1322			}
1323
1324
1325			$text .= "<tr>
1326				<td>".$k."</td>
1327				<td>".print_a($val,true)."<td><td>".print_a($corePrefs[$k],true)."</td>
1328				</tr>";
1329
1330		}
1331		$text .= "</table>";
1332*/
1333
1334
1335		e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_102,$mes->render(). $text);
1336
1337		return null;
1338	}
1339
1340	/**
1341	 * Import XML Dump
1342	 * @return null
1343	 */
1344	private function importXmlFile()
1345	{
1346		$ret = e107::getSingleton('xmlClass')->e107Import($_FILES['file_userfile']['tmp_name'][0]);
1347
1348		foreach($ret['success'] as $table)
1349		{
1350			e107::getMessage()->addSuccess(DBLAN_103." $table");
1351		}
1352
1353		foreach($ret['failed'] as $table)
1354		{
1355			e107::getMessage()->addError(DBLAN_104." $table");
1356		}
1357
1358		return null;
1359	}
1360
1361	/**
1362	 * Optimize SQL
1363	 * @param $mySQLdefaultdb
1364	 * @return null
1365	 */
1366	private function optimizesql($mySQLdefaultdb)
1367	{
1368		$mes = e107::getMessage();
1369		$tables = e107::getDb()->tables();
1370
1371		foreach($tables as $table)
1372		{
1373			e107::getDb()->gen("OPTIMIZE TABLE ".$table);
1374		}
1375
1376		$mes->addSuccess(e107::getParser()->lanVars(DBLAN_11, $mySQLdefaultdb));
1377		e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_7, $mes->render());
1378
1379		return null;
1380	}
1381
1382	/**
1383	 * Preferences Editor
1384	 * @param string $type
1385	 * @return string text for display
1386	 */
1387	private function pref_editor($type='core')
1388	{
1389		//TODO Add drop-down for editing personal perfs also. ie. user pref of self. (admin)
1390
1391		global $e107;
1392		$frm = e107::getForm();
1393		$mes = e107::getMessage();
1394		$tp = e107::getParser();
1395		$pref = e107::getPref();
1396
1397		$config = ($type == 'core' || $type == 'search'  || $type == 'notify') ? e107::getConfig($type) : e107::getPlugConfig($type);
1398
1399		$spref = $config->getPref();
1400
1401		ksort($spref);
1402
1403		$text = "
1404				<form method='post' action='".e_ADMIN."db.php?mode=".$_GET['mode']."&amp;type=".$type."' id='pref_edit'>
1405					<fieldset id='core-db-pref-edit'>
1406						<legend class='e-hideme'>".DBLAN_20."</legend>";
1407
1408		$text .= "<select class='tbox form-control input-large' name='type_select' onchange='urljump(this.options[selectedIndex].value)' >
1409		<option value='".e_ADMIN."db.php?mode=".$_GET['mode']."&amp;type=core'>Core</option>\n
1410		<option value='".e_ADMIN."db.php?mode=".$_GET['mode']."&amp;type=search'>Search</option>
1411		<option value='".e_ADMIN."db.php?mode=".$_GET['mode']."&amp;type=notify'>Notify</option>\n";
1412
1413	//	e107::getConfig($type)->aliases
1414
1415		e107::getDb()->gen("SELECT e107_name FROM #core WHERE e107_name LIKE ('plugin_%') ORDER BY e107_name");
1416		while ($row = e107::getDb()->fetch())
1417		{
1418			$key = str_replace("plugin_","",$row['e107_name']);
1419			$selected = (varset($_GET['type'])==$key) ? "selected='selected'" : "";
1420			$text .= "<option value='".e_ADMIN."db.php?mode=".$_GET['mode']."&amp;type=".$key."' {$selected}>".ucwords($key)."</option>\n";
1421		}
1422
1423
1424		$text .= "</select></div>
1425						<table class='table adminlist'>
1426							<colgroup>
1427								<col style='width: 5%' />
1428								<col style='width: 20%' />
1429								<col style='width: 70%' />
1430								<col style='width: 5%' />
1431							</colgroup>
1432							<thead>
1433								<tr>
1434									<th class='center'>".LAN_DELETE."</th>
1435									<th>".DBLAN_17."</th>
1436									<th>".DBLAN_18."</th>
1437									<th class='center last'>".LAN_OPTIONS."</th>
1438								</tr>
1439							</thead>
1440							<tbody>
1441			";
1442
1443		foreach($spref as $key => $val)
1444		{
1445			$ptext = (is_array($val)) ? "<pre>".print_r($val, TRUE)."</pre>" : htmlspecialchars($val, ENT_QUOTES, 'utf-8');
1446			$ptext = $tp->textclean($ptext, 80);
1447
1448			$text .= "
1449				<tr>
1450					<td class='center autocheck e-pointer'>".$frm->checkbox("delpref2[$key]", 1)."</td>
1451					<td>{$key}</td>
1452					<td>{$ptext}</td>
1453					<td class='center'>".$frm->submit_image("delpref[$key]", LAN_DELETE, 'delete', LAN_CONFIRMDEL." [$key]")."</td>
1454				</tr>
1455				";
1456		}
1457
1458		$text .= "
1459							</tbody>
1460						</table>
1461						<div class='buttons-bar center'>
1462							".$frm->admin_button('delpref_checked', LAN_DELCHECKED, 'delete')."
1463							".$frm->admin_button('back', LAN_BACK, 'back')."
1464							<input type='hidden' name='pref_type' value='".$type."' />
1465						</div>
1466					</fieldset>
1467				</form>\n\n";
1468
1469		e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_20.SEP.ucwords($type), $mes->render().$text);
1470
1471		return $text;
1472	}
1473
1474	/**
1475	 * Preferences Editor
1476	 * @return null
1477	 */
1478	private function scan_override()
1479	{
1480		$pref = e107::getPref();
1481		$mes = e107::getMessage();
1482		$f = e107::getFile();
1483		$config = e107::getConfig();
1484
1485
1486		$fList = $f->get_files(e_CORE.'override/shortcodes/single', '\.php$');
1487		$scList = array();
1488		if(count($fList))
1489		{
1490			foreach($fList as $file)
1491			{
1492				$scList[] = strtoupper(substr($file['fname'], 0, -4));
1493			}
1494			$scList = implode(',', $scList);
1495		}
1496		$config->set('sc_override', $scList)->save(false);
1497
1498		// core batch overrides
1499		$fList = $f->get_files(e_CORE.'override/shortcodes/batch', '\.php$');
1500		$scList = array();
1501		if(count($fList))
1502		{
1503			foreach($fList as $file)
1504			{
1505				$scList[] = substr($file['fname'], 0, -4);
1506			}
1507			$scList = implode(',', $scList);
1508		}
1509
1510		$config->set('sc_batch_override', $scList)->save(false);
1511		//$pref['sc_override'] = $scList;
1512		//save_prefs();
1513	//	$mes->add(DBLAN_57.':<br />'.$pref['sc_override'], E_MESSAGE_SUCCESS);
1514		e107::getRender()->tablerender(
1515			'<strong>'.DBLAN_56, DBLAN_57.':</strong> '
1516			.($config->get('sc_override') ? '<br />'.$config->get('sc_override') : DBLAN_106)
1517			.'<br /><br /><strong>'.DBLAN_105.'</strong>'
1518			.($config->get('sc_batch_override') ? '<br />'.$config->get('sc_batch_override') : DBLAN_106)
1519		);
1520	}
1521
1522
1523
1524	/**
1525	 * Plugin Folder Scanner
1526	 * @return null
1527	 */
1528	private function plugin_viewscan($mode = 'update')
1529	{
1530		$error_messages = array(0 => DBLAN_31, 1 => LAN_ERROR, 2 => DBLAN_33, 3 => DBLAN_34);
1531	//	$error_image = array("integrity_pass.png", "integrity_fail.png", "warning.png", "blank.png");
1532		$error_glyph = array(ADMIN_TRUE_ICON,ADMIN_FALSE_ICON,ADMIN_WARNING_ICON,"<i style='display:inline-block;width:17px;height:16px;'> </i>");
1533		$error_type = array('warning'=>2, 'error'=>1);
1534
1535
1536		global $e107;
1537		$sql = e107::getDb();
1538		$tp = e107::getParser();
1539		$frm = e107::getForm();
1540		$mes = e107::getMessage();
1541
1542	//	require_once (e_HANDLER."plugin_class.php");
1543	//	$ep = new e107plugin();
1544	//	$ep->update_plugins_table($mode); // scan for e_xxx changes and save to plugin table.
1545	//	$ep->save_addon_prefs($mode); // generate global e_xxx_list prefs from plugin table.
1546
1547		$mes->add(DBLAN_23, E_MESSAGE_SUCCESS);
1548		$mes->add("<a href='".e_SELF."'>".LAN_BACK."</a>", E_MESSAGE_SUCCESS);
1549		$mes->add(DBLAN_30);
1550
1551		$text = "
1552				<form method='post' action='".e_ADMIN."db.php?mode=".$_GET['mode']."' id='plug_edit'>
1553					<fieldset id='core-db-plugin-scan'>
1554						<legend class='e-hideme'>".ADLAN_CL_7."</legend>
1555						<table class='table adminlist'>
1556							<colgroup>
1557								<col style='width: 20%' />
1558								<col style='width: 20%' />
1559								<col style='width: 35%' />
1560								<col style='width: 25%' />
1561							</colgroup>
1562							<thead>
1563								<tr>
1564									<th>".LAN_NAME."</th>
1565									<th>".DBLAN_25."</th>
1566									<th>".DBLAN_26."</th>
1567									<th class='center last'>".DBLAN_27."</th>
1568								</tr>
1569							</thead>
1570							<tbody>
1571			";
1572
1573
1574		$plg = e107::getPlug()->clearCache();
1575
1576		$plg->buildAddonPrefLists();
1577
1578		$plgClass = e107::getPlugin();
1579
1580
1581		foreach($plg->getDetected() as $folder)
1582		{
1583			$plg->load($folder);
1584			$plgClass->plugFolder = $folder;
1585			$plgClass->XmlLanguageFiles('refresh');
1586
1587			$name   = $plg->getName();
1588			$addons = $plg->getAddons();
1589
1590				$text .= "
1591								<tr>
1592									<td>".$name."</td>
1593	               					<td>".$folder."</td>
1594									<td>";
1595
1596				if(!empty($addons))
1597				{
1598
1599					foreach(explode(',', $addons) as $this_addon)
1600					{
1601						$ret_code = 3; // Default to 'not checked
1602						if((strpos($this_addon, 'e_') === 0) || (substr($this_addon, - 4, 4) == '_sql'))
1603						{
1604							$ret_code = $plg->getAddonErrors($this_addon); // See whether spaces before opening tag or after closing tag
1605						}
1606						elseif(strpos($this_addon, 'sc_') === 0)
1607						{
1608							$this_addon = substr($this_addon, 3). ' (sc)';
1609						}
1610
1611						if(!is_numeric($ret_code))
1612						{
1613							$errorMessage = $ret_code['msg'];
1614							$ret_code = $error_type[$ret_code['type']];
1615						}
1616						else
1617						{
1618							$errorMessage  = $error_messages[$ret_code];
1619						}
1620
1621						$text .= "<span class='clear e-tip' style='cursor:pointer' title='".$errorMessage."'>";
1622						$text .= $error_glyph[$ret_code]."&nbsp;";
1623
1624						$text .= trim($this_addon); // $ret_code - 0=OK, 1=content error, 2=access error
1625						$text .= "</span><br />";
1626					}
1627				}
1628
1629
1630				$text .= "	</td>
1631								<td class='center'>";
1632
1633				$text .= ($plg->isInstalled() === true) ? "<span class='label label-warning'>".DBLAN_27."</span>" : " ";
1634
1635
1636				$text .= " </td>
1637							</tr>
1638				";
1639
1640
1641		}
1642
1643
1644
1645		$text .= "
1646							</tbody>
1647						</table>
1648					</fieldset>
1649				</form>
1650			";
1651
1652		e107::getRender()->tablerender(DBLAN_10.SEP.DBLAN_22, $mes->render().$text);
1653
1654		return null;
1655	}
1656
1657
1658
1659
1660}
1661
1662//XXX - what is this for (backup core)? <input type='hidden' name='sqltext' value='{$sqltext}' />
1663
1664function db_adminmenu() //FIXME - has problems when navigation is on the LEFT instead of the right.
1665{
1666	global $st;
1667
1668	$var = array();
1669
1670	foreach($st->_options as $key=>$val)
1671	{
1672		$var[$key]['text'] = $val['label'];
1673		$var[$key]['link'] = e_SELF."?mode=".$key;
1674	}
1675
1676	$icon  = e107::getParser()->toIcon('e-database-24');
1677	$caption = $icon."<span>".DBLAN_10."</span>";
1678
1679	e107::getNav()->admin($caption, $_GET['mode'], $var);
1680}
1681
1682
1683/**
1684 * Export XML File and Copy Images.
1685 * @param object $prefs
1686 * @param object $tables
1687 * @param object $debug [optional]
1688 * @return bool|null
1689 */
1690function exportXmlFile($prefs,$tables=array(),$plugPrefs=array(), $themePrefs=array(), $package=FALSE,$debug=FALSE)
1691{
1692	$xml = e107::getXml();
1693	$tp = e107::getParser();
1694	$mes = e107::getMessage();
1695
1696	$desinationFolder = null;
1697
1698	if(vartrue($package))
1699	{
1700
1701		$xml->convertFilePaths = TRUE;
1702		$xml->modifiedPrefsOnly = true;
1703		$xml->filePathDestination = EXPORT_PATH;
1704		$xml->filePathPrepend = array(
1705			'news_thumbnail'	=> "{e_IMAGE}newspost_images/"
1706		);
1707
1708
1709		$desinationFolder = $tp->replaceConstants($xml->filePathDestination);
1710
1711		if(!is_writable($desinationFolder))
1712		{
1713			$message = str_replace('[folder]', $desinationFolder, DBLAN_107);
1714			$mes->add($message, E_MESSAGE_ERROR);
1715			return false ;
1716		}
1717	}
1718
1719	$mode = ($debug === true) ? array( "debug" =>1) : null;
1720
1721	if($xml->e107Export($prefs,$tables,$plugPrefs, $themePrefs, $mode))
1722	{
1723		$mes->add(DBLAN_108." ".$desinationFolder."install.xml", E_MESSAGE_SUCCESS);
1724		if(varset($xml->fileConvertLog))
1725		{
1726			foreach($xml->fileConvertLog as $oldfile)
1727			{
1728				$file = basename($oldfile);
1729				$newfile = $desinationFolder.$file;
1730				if($oldfile == $newfile || (copy($oldfile,$newfile)))
1731				{
1732					$mes->add(DBLAN_109." ".$newfile, E_MESSAGE_SUCCESS);
1733				}
1734				elseif(!file_exists($newfile))
1735				{
1736					$mes->add(DBLAN_110." ".$newfile, E_MESSAGE_ERROR);
1737				}
1738			}
1739		}
1740
1741	}
1742
1743	return null;
1744}
1745
1746
1747
1748function table_list()
1749{
1750	// grab default language lists.
1751	//TODO - a similar function is in db_verify.php. Should probably all be moved to mysql_class.php.
1752
1753	$exclude = array();
1754	$exclude[] = "core";
1755	$exclude[] = "rbinary";
1756	$exclude[] = "parser";
1757	$exclude[] = "tmp";
1758	$exclude[] = "online";
1759	$exclude[] = "upload";
1760	$exclude[] = "user_extended_country";
1761//	$exclude[] = "plugin";
1762
1763	$coreTables = e107::getDb()->tables('nolan');
1764
1765	$tables = array_diff($coreTables,$exclude);
1766
1767	$tabs = array();
1768
1769	foreach($tables as $e107tab)
1770	{
1771		$count = e107::getDb()->gen("SELECT * FROM #".$e107tab);
1772
1773		if($count)
1774		{
1775			$tabs[$e107tab] = $count;
1776		}
1777	}
1778
1779	return $tabs;
1780}
1781
1782
1783
1784
1785?>
1786