PageRenderTime 56ms CodeModel.GetById 13ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 1ms

/inc/mod/modules.inc.php

https://gitlab.com/dddudeman/Haruko
PHP | 773 lines | 738 code | 0 blank | 35 comment | 1 complexity | 86a19b2ca5154493eeaad15e5ec4c0f1 MD5 | raw file
  1<?php
  2if (!defined("IN_MOD"))
  3{
  4	die("Nah, I won't serve that file to you.");
  5}
  6$mitsuba->admin->reqPermission("modules.view");
  7		$search = "";
  8		$replace = "";
  9		
 10		if ((!empty($_GET['cfg'])) && ($_GET['cfg'] == 1) && (!empty($_GET['n'])))
 11		{
 12			$mitsuba->admin->reqPermission("modules.config");
 13
 14			$result = $conn->query("SELECT * FROM modules WHERE namespace='".$conn->real_escape_string($_GET['n'])."';");
 15			if ($result->num_rows != 1)
 16			{
 17				echo "<b style='color: red;'>".$lang['mod/module_not_installed']."</b>";
 18			} else {
 19				$dir = "./modules/".$_GET['n'];
 20				include($dir."/install.php");
 21				$installer = new $json->install_class($conn, $mitsuba);
 22				$installer->uninstall();
 23				exit;
 24			}
 25		}
 26
 27		if ((!empty($_POST['mode'])) && ($_POST['mode'] == "upload"))
 28		{
 29			$mitsuba->admin->reqPermission("modules.upload");
 30			$mitsuba->admin->ui->checkToken($_POST['token']);
 31			if (empty($_FILES['upfile']['tmp_name']))
 32			{
 33				echo "<b style='color: red;'>".$lang['mod/no_file']."</b>";
 34			} else {
 35				$filename = strtolower(preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $_FILES['upfile']['name']));
 36				//Zip file checks
 37				$tmpname = $_FILES['upfile']['tmp_name'];
 38				$zip = new ZipArchive();
 39				if ($zip->open($tmpname) !== TRUE) {
 40					echo "<b style='color: red;'>".$lang['mod/no_file']."</b>";
 41				} elseif (is_null($zip->locateName("module.json")))
 42				{
 43					echo "<b style='color: red;'>".$lang['mod/invalid_module']."</b>";	
 44				} elseif ((is_null($zip->locateName("install.php"))) || (is_null($zip->locateName("config.php")))) //Does the archive contain install.php and config.php?
 45				{
 46					echo "<b style='color: red;'>".$lang['mod/invalid_module']."</b>";	
 47				} else {
 48					//Is the JSON valid?
 49					$json = json_decode($zip->getFromIndex($zip->locateName("module.json")));
 50					if (is_null($json))
 51					{
 52						echo "<b style='color: red;'>".$lang['mod/invalid_module']."</b>";	
 53					} elseif ((empty($json->name)) || (empty($json->namespace)) || (empty($json->description)) || (empty($json->author)) || (empty($json->version)) || (empty($json->install_class)) || (empty($json->config_class)))
 54					{
 55						echo "<b style='color: red;'>".$lang['mod/invalid_module']."</b>";	
 56					} elseif (is_dir("./modules/".$json->namespace."/")) {
 57						echo "<b style='color: red;'>".sprintf($lang['mod/module_dir_exists'], $json->namespace)."</b>";	
 58					} else {
 59						mkdir("./modules/".$json->namespace);
 60						$zip->extractTo("./modules/".$json->namespace."/");
 61						echo "<b style='color: green;'>".$lang['mod/module_uploaded']."</b>";
 62					}
 63				}
 64				$zip->close();
 65			}
 66		}
 67		
 68		
 69		if ((!empty($_GET['ins'])) && ($_GET['ins'] == 1) && (!empty($_GET['n'])))
 70		{
 71			$mitsuba->admin->reqPermission("modules.install");
 72			$result = $conn->query("SELECT * FROM modules WHERE namespace='".$conn->real_escape_string($_GET['n'])."';");
 73			if ($result->num_rows == 1)
 74			{
 75				echo "<b style='color: red;'>".$lang['mod/module_already_installed']."</b>";
 76			} else {
 77				$dir = "./modules/".$_GET['n'];
 78				//Validate JSON
 79				if ((is_dir($dir)) &&(file_exists($dir."/module.json")) && (file_exists($dir."/install.php")) && (file_exists($dir."/config.php")))
 80				{
 81					$json = json_decode(file_get_contents($dir."/module.json"));
 82					if ((!is_null($json)) && (!empty($json->name)) && (!empty($json->namespace)) && (!empty($json->description)) && (!empty($json->author)) && (!empty($json->version)) && (!empty($json->install_class)) && (!empty($json->config_class)))
 83					{
 84						//We can haz module!
 85						$continue = 1;
 86
 87						//Check post fields
 88						if ($continue)
 89						{
 90							if ((!empty($json->new_post_fields)) && (is_array($json->new_post_fields)))
 91							{
 92								$all_fields = $conn->query("SHOW COLUMNS FROM posts;");
 93								$fields = array();
 94								while ($row = $all_fields->fetch_assoc())
 95								{
 96									$fields[$row['Field']] = 1;
 97								}
 98								foreach ($json->new_post_fields as $field) {
 99									if (!empty($fields[$field->name]))
100									{
101										//We haz a conflicting field o_O
102										echo "<b style='color:red;'>".$lang['mod/module_install_error'].sprintf($lang['mod/module_post_field_conflict'], $field->name)."</b>";
103										$continue = 0;
104										break;
105									}
106								}
107							}
108						}
109						//Check permissions
110						if ($continue)
111						{
112							if ((!empty($json->new_permissions)) && (is_array($json->new_permissions)))
113							{
114								$all_permissions = $conn->query("SELECT * FROM permissions");
115								$permissions = array();
116								while ($row = $all_permissions->fetch_assoc())
117								{
118									$permissions[$row['name']] = 1;
119								}
120								foreach ($json->new_permissions as $permission) {
121									if (!empty($permissions["modules.".$json->namespace.".".$permission->name]))
122									{
123										echo "<b style='color:red;'>".$lang['mod/module_install_error'].sprintf($lang['mod/module_permission_conflict'], "modules.".$json->namespace.".".$permission->name)."</b>";
124										$continue = 0;
125										break;
126									}
127								}
128							}
129						}
130						//Check config
131						if ($continue)
132						{
133							if ((!empty($json->new_config)) && (is_array($json->new_config)))
134							{
135								$all_config = $conn->query("SELECT * FROM module_config WHERE namespace='".$conn->real_escape_string($json->namespace)."'");
136								$econfig = array();
137								while ($row = $all_config->fetch_assoc())
138								{
139									$econfig[$row['name']] = 1;
140								}
141								foreach ($json->new_config as $config) {
142									if (!empty($econfig[$config->name]))
143									{
144										echo "<b style='color:red;'>".$lang['mod/module_install_error'].sprintf($lang['mod/module_config_conflict'], $config->name)."</b>";
145										$continue = 0;
146										break;
147									}
148								}
149							}
150						}
151						//Check board config
152						if ($continue)
153						{
154							if ((!empty($json->new_boardconfig)) && (is_array($json->new_boardconfig)))
155							{
156								$all_fields = $conn->query("SHOW COLUMNS FROM boards;");
157								$fields = array();
158								while ($row = $all_fields->fetch_assoc())
159								{
160									$fields[$row['Field']] = 1;
161								}
162								foreach ($json->new_boardconfig as $field) {
163									if (!empty($fields[$field->name]))
164									{
165										//We haz a conflicting field o_O
166										echo "<b style='color:red;'>".$lang['mod/module_install_error'].sprintf($lang['mod/module_board_field_conflict'], $field->name)."</b>";
167										$continue = 0;
168										break;
169									}
170								}
171							}
172						}
173						//Check new classes
174						if ($continue)
175						{
176							if ((!empty($json->new_classes)) && (is_array($json->new_classes)))
177							{
178								foreach ($json->new_classes as $field) {
179									$name = $field->name;
180									if (!empty($mitsuba->$name))
181									{
182										//We haz a conflicting field o_O
183										echo "<b style='color:red;'>".$lang['mod/module_install_error'].sprintf($lang['mod/module_class_conflict'], $field->name)."</b>";
184										$continue = 0;
185										break;
186									}
187								}
188							}
189						}
190						//Check extra tables
191						if ($continue)
192						{
193							if ((!empty($json->new_tables)) && (is_array($json->new_tables)))
194							{
195								$all_fields = $conn->query("SHOW TABLES;");
196								$fields = array();
197								while ($row = $all_fields->fetch_row())
198								{
199									$fields[$row[0]] = 1;
200								}
201								foreach ($json->new_tables as $table) {
202									if (!empty($fields[$table]))
203									{
204										//We haz a conflicting field o_O
205										echo "<b style='color:red;'>".$lang['mod/module_install_error'].sprintf($lang['mod/module_table_conflict'], $table)."</b>";
206										$continue = 0;
207										break;
208									}
209								}
210							}
211						}
212						//Check panel pages
213						if ($continue)
214						{
215							if ((!empty($json->panel_pages)) && (is_array($json->panel_pages)))
216							{
217								$all_fields = $conn->query("SELECT * FROM module_pages;");
218								$fields = array();
219								while ($row = $all_fields->fetch_assoc())
220								{
221									$fields[$row['url']] = 1;
222								}
223								foreach ($json->panel_pages as $pages) {
224									if (!empty($fields[$pages->url]))
225									{
226										//We haz a conflicting field o_O
227										echo "<b style='color:red;'>".$lang['mod/module_install_error'].sprintf($lang['mod/module_page_conflict'], $field->page)."</b>";
228										$continue = 0;
229										break;
230									}
231								}
232							}
233						}
234
235						$continue = 1;
236						$undo_queries = array();
237						//Create post fields
238						if ($continue)
239						{
240							if ((!empty($json->new_post_fields)) && (is_array($json->new_post_fields)))
241							{
242								foreach ($json->new_post_fields as $field) {
243									if (!$conn->query("ALTER TABLE posts ADD `".$conn->real_escape_string($field->name)."` ".$conn->real_escape_string($field->definition)))
244									{
245										//Oh crap we haz an error!
246										$continue = 0;
247										echo "<b style='color:red;'>".$lang['mod/module_install_error'].$conn->error."</b>";
248										foreach ($undo_queries as $query) {
249											$conn->query($query);
250										}
251										break;
252									} else {
253										$undo_queries[] = "ALTER TABLE posts DROP `".$conn->real_escape_string($field->name)."`;";
254										if (!$conn->query("INSERT INTO module_fields (`namespace`, `name`, `type`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($field->name)."', 'postfield');"))
255										{
256											//Oh crap we haz an error!
257											$continue = 0;
258											echo "<b style='color:red;'>".$lang['mod/module_install_error'].$conn->error."</b>";
259											foreach ($undo_queries as $query) {
260												$conn->query($query);
261											}
262											break;
263										} else {
264											$undo_queries[] = "DELETE FROM module_fields WHERE namespace='".$conn->real_escape_string($json->namespace)."' AND name='".$conn->real_escape_string($field->name)."';";
265										}
266									}
267
268								}
269							}
270						}
271						//Create permissions
272						if ($continue)
273						{
274							if ((!empty($json->new_permissions)) && (is_array($json->new_permissions)))
275							{
276								$id = $conn->query("SELECT * FROM permissions_categories WHERE name='module';")->fetch_assoc()['id'];
277								foreach ($json->new_permissions as $permission) {
278									if (!$conn->query("INSERT INTO `permissions` (`name`, `description`, `category`) VALUES ('".$conn->real_escape_string($permission->name)."', '".$conn->real_escape_string($permission->description)."', ".$id.")"))
279									{
280										//Oh crap we haz an error!
281										$continue = 0;
282										echo "<b style='color:red;'>".$lang['mod/module_install_error'].$conn->error."</b>";
283										foreach ($undo_queries as $query) {
284											$conn->query($query);
285										}
286										break;
287									} else {
288										$undo_queries[] = "DELETE FROM `permissions` WHERE name='".$conn->real_escape_string($permission->name)."';";
289									}
290
291								}
292							}
293						}
294						//Create config
295						if ($continue)
296						{
297							if ((!empty($json->new_config)) && (is_array($json->new_config)))
298							{
299								foreach ($json->new_config as $config) {
300									if (!$conn->query("INSERT INTO `module_config` (`namespace`, `name`, `description`, `default_value`, `value`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($config->name)."', '".$conn->real_escape_string($config->description)."', '".$conn->real_escape_string($config->default_value)."', '".$conn->real_escape_string($config->default_value)."')"))
301									{
302										//Oh crap we haz an error!
303										$continue = 0;
304										echo "<b style='color:red;'>".$lang['mod/module_install_error'].$conn->error."</b>";
305										foreach ($undo_queries as $query) {
306											$conn->query($query);
307										}
308										break;
309									} else {
310										$undo_queries[] = "DELETE FROM `module_config` WHERE name='".$conn->real_escape_string($config->name)."' AND namespace='".$conn->real_escape_string($json->namespace)."';";
311									}
312
313								}
314							}
315						}
316						//Create board config
317						if ($continue)
318						{
319							if ((!empty($json->new_boardconfig)) && (is_array($json->new_boardconfig)))
320							{
321								foreach ($json->new_boardconfig as $config) {
322									if (!$conn->query("INSERT INTO `module_boardconfig` (`namespace`, `name`, `description`, `default_value`, `value`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($config->name)."', '".$conn->real_escape_string($config->description)."', '".$conn->real_escape_string($config->default_value)."', '".$conn->real_escape_string($config->default_value)."')"))
323									{
324										//Oh crap we haz an error!
325										$continue = 0;
326										echo "<b style='color:red;'>".$lang['mod/module_install_error'].$conn->error."</b>";
327										foreach ($undo_queries as $query) {
328											$conn->query($query);
329										}
330										break;
331									} else {
332										$undo_queries[] = "DELETE FROM `module_boardconfig` WHERE name='".$conn->real_escape_string($config->name)."' AND namespace='".$conn->real_escape_string($json->namespace)."';";
333										if (!$conn->query("ALTER TABLE boards ADD `".$conn->real_escape_string($config->name)."` ".$conn->real_escape_string($config->definition)))
334										{
335											//Oh crap we haz an error!
336											$continue = 0;
337											echo "<b style='color:red;'>".$lang['mod/module_install_error'].$conn->error."</b>";
338											foreach ($undo_queries as $query) {
339												$conn->query($query);
340											}
341											break;
342										} else {
343											$undo_queries[] = "ALTER TABLE posts DROP `".$conn->real_escape_string($config->name)."`;";
344										}
345									}
346
347								}
348							}
349						}
350						//Create new classes
351						if ($continue)
352						{
353							if ((!empty($json->new_classes)) && (is_array($json->new_classes)))
354							{
355								foreach ($json->new_classes as $class) {
356									if (!$conn->query("INSERT INTO `module_classes` (`namespace`, `name`, `file`, `class`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($class->name)."', '".$conn->real_escape_string($class->file)."', '".$conn->real_escape_string($class->class)."')"))
357									{
358										//Oh crap we haz an error!
359										$continue = 0;
360										echo "<b style='color:red;'>".$lang['mod/module_install_error'].$conn->error."</b>";
361										foreach ($undo_queries as $query) {
362											$conn->query($query);
363										}
364										break;
365									} else {
366										$undo_queries[] = "DELETE FROM `module_classes` WHERE name='".$conn->real_escape_string($class->name)."';";
367									}
368								}
369							}
370						}
371						//Create panel pages
372						if ($continue)
373						{
374							if ((!empty($json->panel_pages)) && (is_array($json->panel_pages)))
375							{
376								foreach ($json->panel_pages as $page) {
377									if (!$conn->query("INSERT INTO `module_pages` (`namespace`, `url`, `file`, `class`, `method`) VALUES ('".$conn->real_escape_string($page->namespace)."', '".$conn->real_escape_string($page->url)."', '".$conn->real_escape_string($page->file)."', '".$conn->real_escape_string($page->class)."', '".$conn->real_escape_string($page->method)."')"))
378									{
379										//Oh crap we haz an error!
380										$continue = 0;
381										echo "<b style='color:red;'>".$lang['mod/module_install_error'].$conn->error."</b>";
382										foreach ($undo_queries as $query) {
383											$conn->query($query);
384										}
385										break;
386									} else {
387										$undo_queries[] = "DELETE FROM `module_pages` WHERE url='".$conn->real_escape_string($page->url)."' AND namespace='".$conn->real_escape_string($json->namespace)."';";
388									}
389								}
390							}
391						}
392						//Create events
393						if ($continue)
394						{
395							if ((!empty($json->events)) && (is_array($json->events)))
396							{
397								foreach ($json->events as $event) {
398									if (!$conn->query("INSERT INTO `module_events` (`namespace`, `event`, `file`, `class`, `method`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($event->event)."', '".$conn->real_escape_string($event->file)."', '".$conn->real_escape_string($event->class)."', '".$conn->real_escape_string($event->method)."')"))
399									{
400										//Oh crap we haz an error!
401										$continue = 0;
402										echo "<b style='color:red;'>".$lang['mod/module_install_error'].$conn->error."</b>";
403										foreach ($undo_queries as $query) {
404											$conn->query($query);
405										}
406										break;
407									} else {
408										$undo_queries[] = "DELETE FROM `module_events` WHERE event='".$conn->real_escape_string($event->event)."' AND namespace='".$conn->real_escape_string($json->namespace)."';";
409									}
410								}
411							}
412						}
413
414						//Add to modules table
415						if ($continue)
416						{
417							if (!$conn->query("INSERT INTO `modules` (`namespace`, `name`, `description`, `author`, `version`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($json->name)."', '".$conn->real_escape_string($json->description)."', '".$conn->real_escape_string($json->author)."', '".$conn->real_escape_string($json->version)."');"))
418							{
419								//Oh crap we haz an error!
420								$continue = 0;
421								echo "<b style='color:red;'>".$lang['mod/module_install_error'].$conn->error."</b>";
422								foreach ($undo_queries as $query) {
423									$conn->query($query);
424								}
425								break;
426							} else {
427								$undo_queries[] = "DELETE FROM `modules` WHERE namespace='".$conn->real_escape_string($json->namespace)."';";
428							}
429						}
430
431
432						//Execute install.php (create extra tables etc.)
433						if ($continue)
434						{
435							include($dir."/install.php");
436							$installer = new $json->install_class($conn, $mitsuba);
437							$installer->install();
438							echo "<b style='color: green;'>".$lang['mod/module_installed']."</b>";
439						}
440					} else {
441						echo "<b style='color: red;'>".$lang['mod/module_invalid_json']."</b>";
442					}
443				} else {
444					echo "<b style='color: red;'>".$lang['mod/module_no_file']."</b>";
445				}
446			}
447		}
448
449		if ((!empty($_GET['unins'])) && ($_GET['unins'] == 1) && (!empty($_GET['n'])))
450		{
451			$mitsuba->admin->reqPermission("modules.uninstall");
452			$result = $conn->query("SELECT * FROM modules WHERE namespace='".$conn->real_escape_string($_GET['n'])."';");
453			if ($result->num_rows != 1)
454			{
455				echo "<b style='color: red;'>".$lang['mod/module_not_installed']."</b>";
456			} else {
457				$dir = "./modules/".$_GET['n'];
458				//Validate JSON
459				if ((is_dir($dir)) &&(file_exists($dir."/module.json")) && (file_exists($dir."/install.php")) && (file_exists($dir."/config.php")))
460				{
461					$json = json_decode(file_get_contents($dir."/module.json"));
462					if ((!is_null($json)) && (!empty($json->name)) && (!empty($json->namespace)) && (!empty($json->description)) && (!empty($json->author)) && (!empty($json->version)) && (!empty($json->install_class)) && (!empty($json->config_class)))
463					{
464						$continue = 1;
465						$undo_queries = array();
466						//Create post fields
467						if ($continue)
468						{
469							if ((!empty($json->new_post_fields)) && (is_array($json->new_post_fields)))
470							{
471								foreach ($json->new_post_fields as $field) {
472									if (!$conn->query("ALTER TABLE posts DROP `".$conn->real_escape_string($field->name)."`;"))
473									{
474										//Oh crap we haz an error!
475										$continue = 0;
476										echo "<b style='color:red;'>".$lang['mod/module_uninstall_error'].$conn->error."</b>";
477										foreach ($undo_queries as $query) {
478											$conn->query($query);
479										}
480										break;
481									} else {
482										$undo_queries[] = "ALTER TABLE posts ADD `".$conn->real_escape_string($field->name)."` ".$conn->real_escape_string($field->definition);
483										if (!$conn->query("DELETE FROM module_fields WHERE namespace='".$conn->real_escape_string($json->namespace)."' AND name='".$conn->real_escape_string($field->name)."';"))
484										{
485											//Oh crap we haz an error!
486											$continue = 0;
487											echo "<b style='color:red;'>".$lang['mod/module_uninstall_error'].$conn->error."</b>";
488											foreach ($undo_queries as $query) {
489												$conn->query($query);
490											}
491											break;
492										} else {
493											$undo_queries[] = "INSERT INTO module_fields (`namespace`, `name`, `type`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($field->name)."', 'postfield');";
494										}
495									}
496
497								}
498							}
499						}
500						//Create permissions
501						if ($continue)
502						{
503							if ((!empty($json->new_permissions)) && (is_array($json->new_permissions)))
504							{
505								$id = $conn->query("SELECT * FROM permissions_categories WHERE name='module';")->fetch_assoc()['id'];
506								foreach ($json->new_permissions as $permission) {
507									if (!$conn->query("DELETE FROM `permissions` WHERE name='".$conn->real_escape_string($permission->name)."';"))
508									{
509										//Oh crap we haz an error!
510										$continue = 0;
511										echo "<b style='color:red;'>".$lang['mod/module_uninstall_error'].$conn->error."</b>";
512										foreach ($undo_queries as $query) {
513											$conn->query($query);
514										}
515										break;
516									} else {
517										$undo_queries[] = "INSERT INTO `permissions` (`name`, `description`, `category`) VALUES ('".$conn->real_escape_string($permission->name)."', '".$conn->real_escape_string($permission->description)."', ".$id.")";
518									}
519
520								}
521							}
522						}
523						//Create config
524						if ($continue)
525						{
526							if ((!empty($json->new_config)) && (is_array($json->new_config)))
527							{
528								foreach ($json->new_config as $config) {
529									if (!$conn->query("DELETE FROM `module_config` WHERE name='".$conn->real_escape_string($config->name)."' AND namespace='".$conn->real_escape_string($json->namespace)."';"))
530									{
531										//Oh crap we haz an error!
532										$continue = 0;
533										echo "<b style='color:red;'>".$lang['mod/module_uninstall_error'].$conn->error."</b>";
534										foreach ($undo_queries as $query) {
535											$conn->query($query);
536										}
537										break;
538									} else {
539										$undo_queries[] = "INSERT INTO `module_config` (`namespace`, `name`, `description`, `default_value`, `value`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($config->name)."', '".$conn->real_escape_string($config->description)."', '".$conn->real_escape_string($config->default_value)."', '".$conn->real_escape_string($config->default_value)."')";
540									}
541
542								}
543							}
544						}
545						//Create board config
546						if ($continue)
547						{
548							if ((!empty($json->new_boardconfig)) && (is_array($json->new_boardconfig)))
549							{
550								foreach ($json->new_boardconfig as $config) {
551									if (!$conn->query("DELETE FROM `module_boardconfig` WHERE name='".$conn->real_escape_string($config->name)."' AND namespace='".$conn->real_escape_string($json->namespace)."';"))
552									{
553										//Oh crap we haz an error!
554										$continue = 0;
555										echo "<b style='color:red;'>".$lang['mod/module_uninstall_error'].$conn->error."</b>";
556										foreach ($undo_queries as $query) {
557											$conn->query($query);
558										}
559										break;
560									} else {
561										$undo_queries[] = "INSERT INTO `module_boardconfig` (`namespace`, `name`, `description`, `default_value`, `value`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($config->name)."', '".$conn->real_escape_string($config->description)."', '".$conn->real_escape_string($config->default_value)."', '".$conn->real_escape_string($config->default_value)."')";
562										if (!$conn->query("ALTER TABLE posts DROP `".$conn->real_escape_string($config->name)."`;"))
563										{
564											//Oh crap we haz an error!
565											$continue = 0;
566											echo "<b style='color:red;'>".$lang['mod/module_uninstall_error'].$conn->error."</b>";
567											foreach ($undo_queries as $query) {
568												$conn->query($query);
569											}
570											break;
571										} else {
572											$undo_queries[] = "ALTER TABLE boards ADD `".$conn->real_escape_string($config->name)."` ".$conn->real_escape_string($config->definition);
573										}
574									}
575
576								}
577							}
578						}
579						//Create new classes
580						if ($continue)
581						{
582							if ((!empty($json->new_classes)) && (is_array($json->new_classes)))
583							{
584								foreach ($json->new_classes as $class) {
585									if (!$conn->query("DELETE FROM `module_classes` WHERE name='".$conn->real_escape_string($class->name)."';"))
586									{
587										//Oh crap we haz an error!
588										$continue = 0;
589										echo "<b style='color:red;'>".$lang['mod/module_uninstall_error'].$conn->error."</b>";
590										foreach ($undo_queries as $query) {
591											$conn->query($query);
592										}
593										break;
594									} else {
595										$undo_queries[] = "INSERT INTO `module_classes` (`namespace`, `name`, `file`, `class`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($class->name)."', '".$conn->real_escape_string($class->file)."', '".$conn->real_escape_string($class->class)."')";
596									}
597								}
598							}
599						}
600						//Create panel pages
601						if ($continue)
602						{
603							if ((!empty($json->panel_pages)) && (is_array($json->panel_pages)))
604							{
605								foreach ($json->panel_pages as $page) {
606									if (!$conn->query("DELETE FROM `module_pages` WHERE url='".$conn->real_escape_string($page->url)."' AND namespace='".$conn->real_escape_string($json->namespace)."';"))
607									{
608										//Oh crap we haz an error!
609										$continue = 0;
610										echo "<b style='color:red;'>".$lang['mod/module_uninstall_error'].$conn->error."</b>";
611										foreach ($undo_queries as $query) {
612											$conn->query($query);
613										}
614										break;
615									} else {
616										$undo_queries[] = "INSERT INTO `module_pages` (`namespace`, `url`, `file`, `class`, `method`) VALUES ('".$conn->real_escape_string($page->namespace)."', '".$conn->real_escape_string($page->url)."', '".$conn->real_escape_string($page->file)."', '".$conn->real_escape_string($page->class)."', '".$conn->real_escape_string($page->method)."')";
617									}
618								}
619							}
620						}
621						//Create events
622						if ($continue)
623						{
624							if ((!empty($json->events)) && (is_array($json->events)))
625							{
626								foreach ($json->events as $event) {
627									if (!$conn->query("DELETE FROM `module_events` WHERE event='".$conn->real_escape_string($event->event)."' AND namespace='".$conn->real_escape_string($json->namespace)."';"))
628									{
629										//Oh crap we haz an error!
630										$continue = 0;
631										echo "<b style='color:red;'>".$lang['mod/module_uninstall_error'].$conn->error."</b>";
632										foreach ($undo_queries as $query) {
633											$conn->query($query);
634										}
635										break;
636									} else {
637										$undo_queries[] = "INSERT INTO `module_events` (`namespace`, `event`, `file`, `class`, `method`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($event->event)."', '".$conn->real_escape_string($event->file)."', '".$conn->real_escape_string($event->class)."', '".$conn->real_escape_string($event->method)."')";
638									}
639								}
640							}
641						}
642
643						//Add to modules table
644						if ($continue)
645						{
646							if (!$conn->query("DELETE FROM `modules` WHERE namespace='".$conn->real_escape_string($json->namespace)."';"))
647							{
648								//Oh crap we haz an error!
649								$continue = 0;
650								echo "<b style='color:red;'>".$lang['mod/module_uninstall_error'].$conn->error."</b>";
651								foreach ($undo_queries as $query) {
652									$conn->query($query);
653								}
654								break;
655							} else {
656								$undo_queries[] = "INSERT INTO `modules` (`namespace`, `name`, `description`, `author`, `version`) VALUES ('".$conn->real_escape_string($json->namespace)."', '".$conn->real_escape_string($json->name)."', '".$conn->real_escape_string($json->description)."', '".$conn->real_escape_string($json->author)."', '".$conn->real_escape_string($json->version)."');";
657							}
658						}
659
660
661						//Execute install.php (create extra tables etc.)
662						if ($continue)
663						{
664							include($dir."/install.php");
665							$installer = new $json->install_class($conn, $mitsuba);
666							$installer->uninstall();
667							echo "<b style='color: green;'>".$lang['mod/module_uninstalled']."</b>";
668						}
669					} else {
670						echo "<b style='color: red;'>".$lang['mod/module_invalid_json']."</b>";
671					}
672				} else {
673					echo "<b style='color: red;'>".$lang['mod/module_no_file']."</b>";
674				}
675			}
676		}
677		
678		if ((!empty($_GET['del'])) && ($_GET['del'] == 1) && (!empty($_GET['n'])))
679		{
680			$mitsuba->admin->reqPermission("modules.delete");
681			$result = $conn->query("SELECT * FROM modules WHERE namespace='".$conn->real_escape_string($_GET['n'])."';");
682			if ($result->num_rows == 1)
683			{
684				echo "<b style='color: red;'>".$lang['mod/module_installed_delete']."</b>";
685			} else {
686				$dir = "./modules/".$_GET['n'];
687				$mitsuba->common->delTree("./".$dir);
688				echo "<b style='color: green;'>".$lang['mod/module_deleted']."</b>";
689			}
690		}
691		?>
692<b><?php echo $lang['mod/rebuild_notice']; ?></b><br />
693<?php $mitsuba->admin->ui->startSection($lang['mod/manage_modules']); ?>
694
695<table>
696<thead>
697<tr>
698<td><?php echo $lang['mod/name']; ?></td>
699<td><?php echo $lang['mod/description']; ?></td>
700<td><?php echo $lang['mod/author']; ?></td>
701<td><?php echo $lang['mod/version']; ?></td>
702<td><?php echo $lang['mod/actions']; ?></td>
703</tr>
704</thead>
705<tbody>
706<?php
707$installed = array();
708$result = $conn->query("SELECT * FROM modules ORDER BY name ASC");
709while ($row = $result->fetch_assoc())
710{
711$installed[$row['namespace']] = 1;
712echo "<tr>";
713echo "<td class='text-center'>".htmlspecialchars($row['name'])."</td>";
714echo "<td>".htmlspecialchars($row['description'])."</td>";
715echo "<td class='text-center'>".htmlspecialchars($row['author'])."</td>";
716echo "<td class='text-center'>".htmlspecialchars($row['version'])."</td>";
717echo "<td class='text-center'><a href='?/modules&unins=1&n=".$row['namespace']."'>".$lang['mod/uninstall']."</a></td>";
718echo "</tr>";
719}
720?>
721</tbody>
722</table>
723<?php $mitsuba->admin->ui->endSection(); ?>
724<br /><br />
725<?php $mitsuba->admin->ui->startSection($lang['mod/uninstalled_modules']); ?>
726<table>
727<thead>
728<tr>
729<td class='text-center'><?php echo $lang['mod/name']; ?></td>
730<td><?php echo $lang['mod/description']; ?></td>
731<td class='text-center'><?php echo $lang['mod/author']; ?></td>
732<td class='text-center'><?php echo $lang['mod/version']; ?></td>
733<td class='text-center'><?php echo $lang['mod/actions']; ?></td>
734</tr>
735</thead>
736<tbody>
737<?php
738$dirs = array_filter(glob('./modules/*'), 'is_dir');
739foreach ($dirs as $dir) {
740	if (file_exists($dir."/module.json"))
741	{
742		$json = json_decode(file_get_contents($dir."/module.json"));
743		if ((!is_null($json)) && (!empty($json->name)) && (!empty($json->namespace)) && (!empty($json->description)) && (!empty($json->author)) && (!empty($json->version)) && (!empty($json->install_class)) && (!empty($json->config_class)))
744		{
745			if (!empty($installed[$json->namespace]))
746			{
747				continue;
748			}
749			echo "<tr>";
750			echo "<td class='text-center'>".$json->name."</td>";
751			echo "<td>".$json->description."</td>";
752			echo "<td class='text-center'>".$json->author."</td>";
753			echo "<td class='text-center'>".$json->version."</td>";
754			echo "<td class='text-center'><a href='?/modules&ins=1&n=".$json->namespace."'>".$lang['mod/install']."</a> <a href='?/modules&del=1&n=".$json->namespace."'>".$lang['mod/delete']."</a></td>";
755			echo "</tr>";
756		}
757	}
758}
759?>
760</tbody>
761</table>
762<?php $mitsuba->admin->ui->endSection(); ?>
763<br /><br />
764<?php $mitsuba->admin->ui->startSection($lang['mod/upload_module']); ?>
765
766<form action="?/modules" method="POST" enctype="multipart/form-data">
767<?php $mitsuba->admin->ui->getToken($path); ?>
768<input type="hidden" name="MAX_FILE_SIZE" value="2097152">
769<input type="hidden" name="mode" value="upload">
770<?php echo $lang['mod/file']; ?>: <input id="postFile" name="upfile" type="file"><br />
771<input type="submit" value="<?php echo $lang['mod/submit']; ?>" />
772</form>
773<?php $mitsuba->admin->ui->endSection(); ?>