<?php global $session;
/**
 * Project:     BeCast WebEngine - simple site engine
 * File:        /admin/module.apnl.php
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.becast.at
 * @copyright 2009-2025 becast.at
 * @author Bernhard Jaud <bernhard at becast dot at>
 * @package BcWe core
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version $Id: ecde576dbc87289a7c16f818ddc7e96a4f826f52 $
 */

If (!defined("IN_BCWE_ADMIN")) {
    die("Dieses Script kann nicht ausserhalb des Frameworks laufen!");
} 

/** 
*  fetch Userdata
*  
*/
$session->page_begin("Module", True);


class module_panel extends admin_module{

	/**
	* Main Output of the panel
	*
	*/
	function output(){
		global $module,$config,$db,$panel,$userinfo,$root,$core;
		
		/** 		
		*  If action and module are set either Install or Deinstall the module
		*  		
		*/
		if((isset($_GET["action"]) && $_GET["action"]!="") && (isset($_GET["module"]) && $_GET["module"]!="")){
		
			if($_GET["action"]=="install"){
				$this->install($_GET["module"]);
			}elseif($_GET["action"]=="uninstall"){
				$this->uninstall($_GET["module"]);			
			}
		
		/**
		* Else create the Module-Panel
		*
		*/
		}else{
		
			$panel->title="Modulverwaltung";
			$panel->content.="<h2>Installierte Module</h2><br />
			<table width=\"90%\" cellspacing=\"2\">
					 <tr>	
					 <th style=\"text-align='center';\">Modul</th>
					 <th style=\"text-align='center';\">Version</th>
					 <th style=\"text-align='center';\">Autor</th>
					 <th style=\"text-align='center';\">Homepage</th>
					 <th style=\"text-align='center';\">Aktion</th>
					 </tr>";
			if(!empty($core->mod_)){
				foreach($core->mod_ as $mod){
					$depends="";
					if(isset($mod["depends"]) && $mod["depends"]!=""){
						$dependands=unserialize($mod["depends"]);
						foreach($dependands as $dep){
							if(!$deps){
								$deps='<a href=\"\">'.$dep.'</a>';
							}else{
								$deps.=', <a href=\"\">'.$dep.'</a>';
							}
						}
						$depends.='<br /><span style="font-size: xx-small;">h&auml;ngt ab von: '.$deps;
					}
					$panel->content.="<tr>
					<td style=\"text-align='center';\">".$mod["name"].$depends."</td>
					<td style=\"text-align='center';\">".$mod["version"]."</td>
					<td style=\"text-align='center';\">".$mod["author"]."</td>
					<td style=\"text-align='center';\"><a href=\"".$mod["url"]."\">".$mod["url"]."</a></td>
					<td style=\"text-align='center';\"><a onclick=\"return confirmLink(this, 'Willst du wirklich das Modul ".$mod["name"]." deinstallieren? Alle Daten diese Plugins werden gel&ouml;scht!')\" href=\"".$config["path"]."/admin/index.php?panel=module&amp;action=uninstall&amp;module=".$mod["file"]."\">Deinstallieren</a></td>
					</tr>";
					unset($deps);
					unset($depends);
					unset($mod);
				}
			}else{
			$panel->content.="<tr>
					  <td align=\"center\" colspan=\"5\">Keine Module verf&uuml;gbar.</td>
					  </tr>";
			}
			$panel->content.="</table><br />";

			$panel->content.="<h2>Nicht Installierte Module</h2><br />";
			$panel->content.="<table width=\"90%\" cellspacing=\"2\">
					  <tr>	
					  <th style=\"text-align='center';\">Modul</th>
					  <th style=\"text-align='center';\">Version</th>
					  <th style=\"text-align='center';\">Autor</th>
					  <th style=\"text-align='center';\">Homepage</th>
					  <th style=\"text-align='center';\">Aktion</th>
					  </tr>";

			$modules=$this->avaiable_modules();
			if(is_array($modules)){
				foreach($modules as $mod){
					$depends="";
					if(isset($mod["depends"]) && $mod["depends"]!=""){
						$dependands=explode(",",$mod["depends"]);
						foreach($dependands as $dep){
							if(!$deps){
								$deps='<a href=\"\">'.$dep.'</a>';
							}else{
								$deps.=', <a href=\"\">'.$dep.'</a>';
							}
						}
						$depends.='<br /><span style="font-size: xx-small;">h&auml;ngt ab von: '.$deps;
					}
					$panel->content.="<tr>
					<td style=\"text-align='center';\">".$mod["name"].$depends."</td>
					<td style=\"text-align='center';\">".$mod["version"]."</td>
					<td style=\"text-align='center';\">".$mod["author"]."</td>
					<td style=\"text-align='center';\"><a href=\"".$mod["url"]."\">".$mod["url"]."</a></td>
					<td style=\"text-align='center';\"><a href=\"".$config["path"]."/admin/index.php?panel=module&amp;action=install&amp;module=".$mod["file"]."\">Installieren</a></td>
					</tr>";
					unset($deps);
					unset($depends);
					unset($mod);
				}
			}else{
			$panel->content.="<tr>
					<td align=\"center\" colspan=\"5\">Keine Module verf&uuml;gbar.</td>
					</tr>";
			}
			$panel->content.="</table>";
		}
	}

	/**
	* Lists all installable Modules
	*
	* @return string|array
	*/
	function avaiable_modules() {
		global $module,$config,$db,$panel,$userinfo,$root,$core;
 		$install_mods = array();
		$root = $_SERVER['DOCUMENT_ROOT'] . $config["path"];

		if ($dir = @opendir($root."/modules")){
			while ($file = readdir($dir)){
				if (is_dir($root."/modules/".$file) && is_file($root."/modules/".$file."/".$file.".module.php")){
					if (empty($core->mod_[$file]))
					{
						include_once($root."/modules/".$file."/".$file.".module.php");
						$class="module_".$file;
						$imod=new $class();
						$install_mods[$file]=$imod->get_info();
						$install_mods[$file]["file"]=$file;
					}
				}
			}
		return $install_mods;
		}
	}

	/**
	* Installs a Module
	*
	* @param Modul
	*/
	function install($module) {
	global $config,$core,$db,$panel,$userinfo,$root;
		$root = $_SERVER['DOCUMENT_ROOT'] . $config["path"];
		if(file_exists($root."/modules/".$module."/".$module.".module.php")){
			include_once($root."/modules/".$module."/".$module.".module.php");
			$class="module_".$module;
			$install=new $class();
			$info=$install->get_info();
			If($info["depends"]!=""){
				$dependands=explode(",",$info["depends"]);
				foreach($dependands as $dep){
					If(empty($core->mod_[$dep])){
						$inst_result="Die Abh&auml;ngigkeit zu dem Modul ".$dep." ist nicht erf&uuml;llt";
					}
				}
			}
			if(!$inst_result){
				$inst_result=$install->install();
			}
			
			if($inst_result===True){
				if($info["depends"]!=""){
					$dependands=serialize(explode(",",$info["depends"]));
				}
				$db->query("INSERT INTO `" . $config["prefix"] . "role_values` (`name`,`text`, `type`) VALUES ('".$info['file']."_admin','".$info['name']." - Administration','yesno')");
				$db->query("INSERT INTO `" . $config["prefix"] . "module` (`file`,`name`,`version`,`author`,`depends`, `url`) VALUES ('".$info['file']."','".$info['name']."','".$info['version']."','".$info['author']."','".$dependands."','".$info['url']."')");
				$panel->admin_message("Modul installiert!","Das Modul wurde erfolgreich installiert.",True,"module",3);
			}else{
				$install->uninstall();
				$panel->admin_message("Fehler!","Das Modul konnte nicht installiert werden. Der Fehler war: ".$inst_result,True,"module",3);
			}
					
		}
	}

	/**
	* Uninstalls a Module
	*
	* @param Modul
	*/
	function uninstall($module) {
		global $config,$db,$panel,$userinfo,$root;
		$root = $_SERVER['DOCUMENT_ROOT'] . $config["path"];
		if(file_exists($root."/modules/".$module."/".$module.".module.php")){
			include_once($root."/modules/".$module."/".$module.".module.php");
			$class="module_".$module;
			$uninstall=new $class();
			$info=$uninstall->get_info();
			$deps=$this->depencys($info["file"]);
			if(is_array($deps)){
				$dmod=implode(",",$deps);
				$uninst_result='<br />Die folgenden Module sind von dem Modul '.$info["name"].' abh&auml;ngig: '.$dmod;
			}else{
				$uninst_result=$uninstall->uninstall();
			}
			if($uninst_result===True){
				$result=$db->query("SELECT `id` FROM `" . $config["prefix"] . "role_values` WHERE `name`='".$info["file"]."_admin'");
				$id=$db->fetch_array($result);
				$db->query("DELETE FROM `" . $config["prefix"] . "roleset` WHERE `role_value_id`='".$id['id']."'");
				$db->query("DELETE FROM `" . $config["prefix"] . "role_values` WHERE `id`='".$id['id']."'");
				$db->query("DELETE FROM `" . $config["prefix"] . "module` WHERE `file`='".$info["file"]."'");
				$panel->admin_message("Modul deinstalliert!","Das Modul wurde erfolgreich deinstalliert.",True,"module",3);
			}else{
				$panel->admin_message("Fehler!","Das Modul konnte nicht deinstalliert werden. Der Fehler war: ".$uninst_result,True,"module",5);
			}	
		}
	}
	
	/**
	* Gets the dependance of a Module
	*
	* @param Modul
	* @return array|false
	*/
	function depencys($module) {
		global $config,$db,$core, $panel,$userinfo,$root;
		$deps=FALSE;
		foreach($core->mod_ as $mod){
			$depend=unserialize($mod["depends"]);
			if(is_array($depend)){
				if(in_array($module,$depend)){
					$deps[]=$mod['name'];
				}
			}
		}
		return $deps; 
	}
	
	/**
	* Metaheader output
	*
	*/
	function meta() {
		global $module,$config,$panel,$userinfo,$root;
		$meta="<script type=\"text/javascript\">
			function confirmLink(theLink, text)
			{
			// Confirmation is not required in the configuration file
			// or browser is Opera (crappy js implementation)
			if (typeof(window.opera) != 'undefined') {
			return true;
			}
			
			var is_confirmed = confirm(text);
			if (is_confirmed) {
			if ( typeof(theLink.href) != 'undefined' ) {
			theLink.href += '&is_js_confirmed=1';
			} else if ( typeof(theLink.form) != 'undefined' ) {
			theLink.form.action += '?is_js_confirmed=1';
			}
			}
			
			return is_confirmed;
			} // end of the 'confirmLink()' function
			</script>";
		return $meta;
			
	}
	
}
?>
