<?php
if ( !eregi("admin.php", $PHP_SELF) ) {
	 redirect_header(XOOPS_URL."/",3,_NOPERM);
}
if( $xoopsUser->isAdmin($xoopsModule->mid()) ) {

function module_list(){
	global $xoopsConfig, $xoopsUser, $xoopsDB, $xoopsModule;
        xoops_cp_header();
	OpenTable();
	echo "
	<h4 style='text-align:left'>"._MD_AM_MODADMIN."</h4>
	<form action='admin.php' method='post' name='moduleadmin' id='moduleadmin'>
	<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>
	<table width='100%' border='0' cellpadding='4' cellspacing='1'>
	<tr valign='middle' class='bg3' align='center'><td><b>"._MD_AM_MODULE."</b></td><td><b>"._MD_AM_VERSION."</b></td><td><b>"._MD_AM_LASTUP."</b></td><td><b>"._MD_AM_ACTION."</b></td><td><b>"._MD_AM_ORDER."</b><br /><small>"._MD_AM_ORDER0."</small></td><td><b>"._INFO."</b></td></tr>
	";
	$installed_mods = XoopsModule::getInstalledModules();
	$listed_mods = array();
	foreach ( $installed_mods as $module ) {
		echo "<tr align='center' valign='middle' class='bg1'>\n";
		echo "<td valign='bottom'>";
		if ( $module->hasAdmin() && $module->isActivated() ) {
			echo "<a href='".XOOPS_URL."/modules/".$module->dirname()."/".$module->adminindex()."'><img src='".XOOPS_URL."/modules/".$module->dirname()."/".$module->image()."' alt='".htmlspecialchars($module->name())."' border='0'><br><b>" .trim($module->name())."</b></a>";
		} else {
			echo "<img src='".XOOPS_URL."/modules/".$module->dirname()."/".$module->image()."' alt='".htmlspecialchars($module->name())."' border='0'><br><b>" .trim($module->name())."</b>";
		}
		echo "<input type='hidden' name='name[]' value='" .htmlspecialchars($module->name())."' /><input type='hidden' name='oldstatus[]' value='1' /></td>\n";
		echo "<td align='center'>".$module->currentVersion()."</td><td align='center'>".formatTimestamp($module->last_update(),"m")."<br />";
		if ( $module->dirname() != "system" && $module->isActivated() ) {
			echo "<span style='color:#ff0000;'>"._MD_AM_ACTIVE."</span></td><td><select name='newstatus[]'><option value='deactivate'>"._MD_AM_DEACTIVATE."</option><option value='update'>"._MD_AM_UPDATE."</option>\n";
		} elseif ( $module->dirname() != "system" ) {
			echo _MD_AM_INACTIVE."</td><td><select name='newstatus[]'><option value='activate'>"._MD_AM_ACTIVATE."</option><option value='update'>"._MD_AM_UPDATE."</option><option value='uninstall'>"._MD_AM_UNINSTALL."</option>\n";
		} else {
			echo "<span style='color:#ff0000;'>"._MD_AM_ACTIVE."</span></td><td><select name='newstatus[]'><option value='update'>"._MD_AM_UPDATE."</option>\n";
		}
		echo "<option value='nochange' selected='selected'>"._MD_AM_NOCHANGE."</option>\n";
		echo "</select></td>\n";
		echo "<td>";
		if ( $module->hasMain() ) {
			echo "<input type='hidden' name='oldweight[]' value='".$module->weight()."' /><input type='text' name='weight[]' size='3' maxlength='5' value='".$module->weight()."' />";
		} else {
			echo "<input type='hidden' name='oldweight[]' value='0' /><input type='hidden' name='weight[]' value='0' />";
		}
		echo "
		</td>
		<td><a href='javascript:openWithSelfMain(\"".XOOPS_URL."/modules/system/admin.php?fct=version&amp;mid=".$module->mid()."\",\"Info\",300,230);'>"._INFO."</a><input type='hidden' name='module[]' value='".$module->dirname()."' /></td>
		</tr>
		";
		$listed_mods[] = trim($module->dirname());
	}
	echo "
	</table></td></tr></table>
	<br />
	<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>
	<table width='100%' border='0' cellpadding='4' cellspacing='1'>
	<tr valign='middle' class='bg3' align='center'><td><b>"._MD_AM_MODULE."</b></td><td><b>"._MD_AM_VERSION."</b></td><td><b>"._MD_AM_LASTUP."</b></td><td><b>"._MD_AM_ACTION."</b></td><td><b>"._MD_AM_ORDER."</b><br /><small>"._MD_AM_ORDER0."</small></td><td><b>"._INFO."</b></td></tr>
	";
	$modules_dir = XOOPS_ROOT_PATH."/modules";
	$handle = opendir($modules_dir);
	while ($file = readdir($handle)) {
		clearstatcache();
		if ( !ereg("^[.]{1,2}$",$file) && is_dir($modules_dir."/".$file) ) {
			if ( !in_array(trim($file), $listed_mods) ) {
				$module = new XoopsModule();
				$module->loadModInfo($file);
				echo "<tr align='center' valign='middle' class='bg1'>
				<td align='center' valign='bottom'><img src='".XOOPS_URL."/modules/".$module->dirname()."/".$module->image()."' alt='".htmlspecialchars($module->name())."' border='0'><br><b>" .trim($module->name())."</b><input type='hidden' name='name[]' value='" .htmlspecialchars($module->name())."' /></td>
				<td align='center'>".$module->currentVersion()."</td>
				<td align='center'>"._MD_AM_NOTINSTALLED."</td>
				<td><input type='hidden' name='oldstatus[]' value='0' /><select name='newstatus[]'><option value='nochange' selected='selected'>"._MD_AM_NOCHANGE."</option><option value='install'>"._MD_AM_INSTALL."</option></select></td>
				<td>
				";
				if ( $module->hasMain() ) {
					echo "<input type='hidden' name='oldweight[]' value='0' /><input type='text' name='weight[]' size='3' maxlength='5' />";
				} else {
					echo "<input type='hidden' name='oldweight[]' value='0' /><input type='hidden' name='weight[]' value='0' />&nbsp;";
				}
				echo "
				</td>
				<td><a href='javascript:openWithSelfMain(\"".XOOPS_URL."/modules/system/admin.php?fct=version&amp;mid=".$module->dirname()."\",\"Info\",300,230);'>"._INFO."</a>
				<input type='hidden' name='module[]' value='".$module->dirname()."' /></td></tr>
				";
				unset($module);
			}
		}
	}
	echo "
	</table></td></tr>
	<tr align='center'><td><br />
	<input type='hidden' name='fct' value='modulesadmin' />
	<input type='hidden' name='op' value='confirm' />
	<input type='submit' name='submit' value='"._MD_AM_SUBMIT."' />
	</td></tr></table>
	</form>
	";
	CloseTable();
	xoops_cp_footer();
}

function module_install($module){
	global $xoopsConfig, $xoopsUser, $xoopsDB;
	if ( !XoopsModule::moduleExists($module) ) {
		$mymodule = new XoopsModule();
		$mymodule->loadModInfo($module);
		$newmid = $mymodule->install();
		//update the main menu cache
		if ( !$newmid ) {
			$ret = "<p>".sprintf(_MD_AM_FAILINS, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br />";
			$errs = $mymodule->errors();
			foreach ( $errs as $err ) {
				$ret .= " - ".$err."<br />";
			}
			$ret .= "</p>";
			return $ret;
		} else {
			$groups = $xoopsUser->groups();
			// retrieve all block ids for this module
			$blocks = XoopsBlock::getByModule($newmid, false);
			foreach ( $groups as $mygroup ){
				if ( XoopsGroup::checkRight("module", 0, $mygroup, "A") ) {
					$sql = "INSERT INTO ".$xoopsDB->prefix("groups_modules_link")." (groupid, mid, type) VALUES ($mygroup, $newmid, 'A')";
					$xoopsDB->query($sql);
					$sql = "INSERT INTO ".$xoopsDB->prefix("groups_modules_link")." (groupid, mid, type) VALUES ($mygroup, $newmid, 'R')";
					$xoopsDB->query($sql);
					foreach ( $blocks as $blc) {
						$sql = "INSERT INTO ".$xoopsDB->prefix("groups_blocks_link")." (groupid, block_id, type) VALUES ($mygroup, $blc, 'R')";
						$xoopsDB->query($sql);
					}
				}
			}
			return "<p>".sprintf(_MD_AM_OKINS, "<b>".$mymodule->name()."</b>")."</p>";
		}
	}
	return "<p>".sprintf(_MD_AM_FAILINS, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br /> - ".sprintf(_MD_AM_ALEXISTS, $module)."</p>";
}

function module_uninstall($module){
	global $xoopsConfig, $HTTP_GET_VARS;
	$mymodule = XoopsModule::getByDirname($module);
	if ($mymodule->dirname()=="system") {
		return "<p>".sprintf(_MD_AM_FAILUNINS, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br /> - "._MD_AM_SYSNO."</p>";
	} elseif ($mymodule->dirname() == $xoopsConfig['startpage']) {
		return "<p>".sprintf(_MD_AM_FAILUNINS, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br /> - "._MD_AM_STRTNO."</p>";
	} else {
		if ( !$mymodule->uninstall() ) {
			$ret = "<p>".sprintf(_MD_AM_FAILUNINS, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br />";
			$errs = $mymodule->errors();
			foreach ( $errs as $err ) {
				$ret .= " - ".$err."<br />";
			}
			return $ret."</p>";
		}
		return "<p>".sprintf(_MD_AM_OKUNINS, "<b>".$mymodule->name()."</b>")."</p>";
	}
}

function module_update($module){
	global $xoopsConfig;
	$mymodule = XoopsModule::getByDirname($module);
	if ( !$mymodule->update() ) {
		$ret = "<p>".sprintf(_MD_AM_FAILUPD, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br />";
		$errs = $mymodule->errors();
		foreach ( $errs as $err ) {
			$ret .= " - ".$err."<br />";
		}
		return $ret."</p>";
	}
	return "<p>".sprintf(_MD_AM_OKUPD, "<b>".$mymodule->name()."</b>")."</p>";
}

function module_activate($module){
	global $xoopsConfig;
	$mymodule = XoopsModule::getByDirname($module);
	if ( !$mymodule->activate() ) {
		$ret = "<p>".sprintf(_MD_AM_FAILACT, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br />";
		$errs = $mymodule->errors();
		foreach ( $errs as $err ) {
			echo " - ".$err."<br />";
		}
		return $ret."</p>";
	}
	return "<p>".sprintf(_MD_AM_OKACT, "<b>".$mymodule->name()."</b>")."</p>";
}

function module_deactivate($module){
	global $xoopsConfig;
	$mymodule = XoopsModule::getByDirname($module);
	if ($mymodule->dirname() == "system") {
		return "<p>".sprintf(_MD_AM_FAILDEACT, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br /> - "._MD_AM_SYSNO."</p>";
	} elseif ($mymodule->dirname() == $xoopsConfig['startpage']) {
		return "<p>".sprintf(_MD_AM_FAILDEACT, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br /> - "._MD_AM_STRTNO."</p>";
	} else {
		if ( !$mymodule->deactivate() ) {
			$ret = "<p>".sprintf(_MD_AM_FAILDEACT, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br />";
			$errs = $mymodule->errors();
			foreach ( $errs as $err ) {
				$ret .= " - ".$err."<br />";
			}
			return $ret."</p>";
		}
		return "<p>".sprintf(_MD_AM_OKDEACT, "<b>".$mymodule->name()."</b>")."</p>";
	}
}

function module_order($module, $order){
	global $xoopsConfig;
	$mymodule = XoopsModule::getByDirname($module);
	if ( !$mymodule->changeOrder($order) ) {
		$ret = "<p>".sprintf(_MD_AM_FAILORDER, "<b>".$mymodule->name()."</b>")."&nbsp;"._MD_AM_ERRORSC."<br />";
		$errs = $mymodule->errors();
		foreach ( $errs as $err ) {
			$ret .= " - ".$err."<br />";
		}
		return $ret."</p>";
	}
	return "<p>".sprintf(_MD_AM_OKORDER, "<b>".$mymodule->name()."</b>")."</p>";
}

function get_module_main_menu(){
	global $xoopsDB;
	$sublinks = array();
	$content = "<"."?php\n\$mainmenu = \"";
	$content .= "&nbsp;<a href='\".XOOPS_URL.\"/'>\"._MB_SYSTEM_HOME.\"</a>";
	$sql = "SELECT * FROM ".$xoopsDB->prefix("modules")." WHERE hasmain=1 AND isactive=1 AND weight>0 ORDER BY weight ASC";
	$result = $xoopsDB->query($sql);
	while($myrow = $xoopsDB->fetchArray($result)){
		if ( file_exists(XOOPS_ROOT_PATH."/modules/".$myrow['dirname']."/xoops_version.php") ) {
			$module = new XoopsModule($myrow);
			$content .= "<br />&nbsp;".$module->mainLink()."\n";
			if($sublinks = $module->subLink()){
				foreach($sublinks as $sublink){
					$content .= "<br />&nbsp;&nbsp;&nbsp;".$sublink;
				}
			}
		}
	}
	$content .= "\";\n?".">";
	return $content;
}

function write_module_main_menu($content){
	if (!xoopsfwrite()) {
		return false;
	}
	$filename = XOOPS_ROOT_PATH."/modules/system/cache/mainmenu.php";
	if (!$file = fopen($filename, "w")) {
		return false;
	}
	fwrite($file, $content);
	fclose($file);
	return true;
}

function get_module_admin_menu(){
	/************************************************************
	 * Based on:
	 * - PHP Layers Menu 1.0.7(c)2001,2002 Marco Pratesi <pratesi@telug.it>
	 * - TreeMenu 1.1 - Bjorge Dijkstra <bjorge@gmx.net>
	 ************************************************************/

	$abscissa_step = 90;	// step for the left boundaries of layers
	$abscissa_offset = 15;	// to choose the horizontal coordinate start offset for the layers
	$rightarrow = "";
	// the following is to support browsers not detecting the mouse position
	$ordinata_step = 15;	// estimated value of the number of pixels between links on a layer
	$ordinata[1] = 150-$ordinata_step;// to choose the vertical coordinate start offset for the layers

	$moveLayers = array();
	$shutdown = array();
	$firstleveltable = array();

	/*********************************************/
	/* read file to $tree array                  */
	/* tree[x][0] -> tree level                  */
	/* tree[x][1] -> item text                   */
	/* tree[x][2] -> item link                   */
	/* tree[x][3] -> link target                 */
	/* tree[x][4] -> module id                   */
	/*********************************************/

	$js = "";
	$maxlevel = 0;
	$cnt = 1;
	$mids = XoopsModule::getHasAdminModulesList(false);
	foreach ( $mids as $mid ) {
		$mod = new XoopsModule($mid);
		$adminmenu = $mod->getAdminMenu();
		if ( !empty($adminmenu) || $mod->adminindex() ) {
			$tree[$cnt][0] = 1;
			$tree[$cnt][5] = "<img src='\".XOOPS_URL.\"/modules/".$mod->dirname()."/".$mod->image()."' />";
			$tree[$cnt][1] .= $mod->name();
			$tree[$cnt][2] = "\".XOOPS_URL.\"/modules/".$mod->dirname()."/".$mod->adminindex();
			$tree[$cnt][3] = "";
			$tree[$cnt][4] = $mid;
			$tree[$cnt][6] = "<b>"._VERSION.":</b> ".$mod->currentVersion()."<br /><b>"._DESCRIPTION.":</b> ".$mod->description()."";
			$layer_label[$cnt] = "L" . $cnt;
			if ( $tree[$cnt][0] > $maxlevel ) {
				$maxlevel = $tree[$cnt][0];
			}
			$cnt++;
			if ( !empty($adminmenu) ) {
				foreach ( $adminmenu as $menuitem ) {
					$menuitem['link'] = trim($menuitem['link']);
					$menuitem['target'] = trim($menuitem['target']);
					$tree[$cnt][0] = 2;
					$tree[$cnt][1] = trim($menuitem['title']);
					$tree[$cnt][2] = (empty($menuitem['link'])) ? "#" : "\".XOOPS_URL.\"/modules/".$mod->dirname()."/".$menuitem['link'];
					$tree[$cnt][3] = (empty($menuitem['target'])) ? "" : $menuitem['target'];
					$tree[$cnt][4] = $mid;
					$layer_label[$cnt] = "L" . $cnt;
					if ($tree[$cnt][0] > $maxlevel) {
						$maxlevel = $tree[$cnt][0];
					}
					$cnt++;
				}
			}
		}
	}
	$tmpcount = count($tree);
	$tree[$tmpcount+1][0] = 0;
	for ( $i = 0; $i < $maxlevel; $i++) {
		$abscissa[$i] = $i * $abscissa_step + $abscissa_offset;
	}
	for ( $cnt = 1; $cnt <= $tmpcount; $cnt++) {	// this counter scans all nodes
		// assign the layers name to the current hierarchical level,
		// to keep trace of the route leading to the current node on the tree
		$layername[$tree[$cnt][0]] = $layer_label[$cnt];

		// assign the starting vertical coordinates for all sublevels
		for ( $i = $tree[$cnt][0] + 1; $i < $maxlevel; $i++) {
			$ordinata[$i] = $ordinata[$i-1] + 1.5*$ordinata_step;
		}
		// increment the starting vertical coordinate for the current sublevel
		if ($tree[$cnt][0] < $maxlevel) {
			$ordinata[$tree[$cnt][0]] += $ordinata_step;
		}
		if ($tree[$cnt+1][0]>$tree[$cnt][0] && $cnt<$tmpcount) {			// the node is not a leaf, hence it has at least a child
			// initialize the corresponding layer content trought a void string
			$layer[$layer_label[$cnt]] = "";
			// prepare the popUp function related to the children
			$js .= "function popUp" . $layer_label[$cnt] . "() {\n" . "shutdown();\n";
			for ($i=1; $i<=$tree[$cnt][0]; $i++) {
				$js .= "popUp(\\\"" . $layername[$i] . "\\\",true);\n";
			}
			$js .= "}\n";

			// geometrical parameters are assigned to the new layer, related to the above mentioned children
			$moveLayers[$tree[$cnt][4]] .= "setleft('" . $layer_label[$cnt] . "'," . $abscissa[$tree[$cnt][0]] . ");\n";
			$moveLayers[$tree[$cnt][4]] .= "settop('" . $layer_label[$cnt] . "'," . $ordinata[$tree[$cnt][0]] . ");\n";
			//$moveLayers[$tree[$cnt][4]] .= "setwidth('" . $layer_label[$cnt] . "'," . $abscissa_step . ");\n";

			// the new layer is accounted for in the shutdown() function
			$shutdown[$tree[$cnt][4]] .= "popUp('" . $layer_label[$cnt] . "',false);\n"; 
		}
		if ($tree[$cnt+1][0]>$tree[$cnt][0] && $cnt<$tmpcount) {
			// not a leaf
			$currentarrow = $rightarrow;
		} else {
			// a leaf
			$currentarrow = "";
		}
		/* */
		$currentlink = $tree[$cnt][2];
		/* */
		/*
		if ( $tree[$cnt+1][0] > $tree[$cnt][0] && $cnt < $tmpcount) {
		// not a leaf
			$currentlink = "#";
		} else {	
		// a leaf
			$currentlink = $tree[$cnt][2];
		}
		*/
		if ($tree[$cnt][3] != "") {
			$currenttarget = " target='" . $tree[$cnt][3] . "'";
		} else {
			$currenttarget = "";
		}
		if ($tree[$cnt][0] > 1) {
			// the hierarchical level is > 1, hence the current node is not a child of the root node
			// handle accordingly the corresponding link, distinguishing if the current node is a leaf or not
			if ( $tree[$cnt+1][0] > $tree[$cnt][0] && $cnt < $tmpcount ) {	// not a leaf
				$onmouseover = " onMouseover='moveLayerY(\\\"" . $layer_label[$cnt] . "\\\", currentY) ; popUp" . $layer_label[$cnt] . "();";
			} else {	// a leaf
				$onmouseover = " onMouseover='popUp" . $layername[$tree[$cnt][0]-1] . "();";
			}
			$layer[$layername[$tree[$cnt][0]-1]] .= "<img src='\".XOOPS_URL.\"/images/pointer.gif' width='8' height='8' />&nbsp;<a href='" . $currentlink . "'" . $onmouseover . "'" . $currenttarget . ">" .$tree[$cnt][1]. "</a>" . $currentarrow . "<br />\n";
		} elseif ($tree[$cnt][0] == 1) {
			// the hierarchical level is = 1, hence the current node is a child of the root node
			// handle accordingly the corresponding link, distinguishing if the current node is a leaf or not
			if ($tree[$cnt+1][0]>$tree[$cnt][0] && $cnt<$tmpcount) {
				// not a leaf
				$onmouseover = " onMouseover='moveLayerY(\\\"" . $layer_label[$cnt] . "\\\", currentY) ; popUp" . $layer_label[$cnt] . "();";
			} else {	
				// a leaf
				$onmouseover = " onMouseover='shutdown();";
			}
			$firstleveltable[$tree[$cnt][4]] .= "<a href='" . $currentlink . "'" . $onmouseover . "'" . $currenttarget . ">" . $tree[$cnt][5] . "</a>" . $currentarrow . "<br />\n";
		}
	}	// end of the "for" cycle scanning all nodes

	$cellpadding = 10;
	$width = $abscissa_step - $cellpadding;
	$menu_layers = "";
	for ( $cnt = 1; $cnt <= $tmpcount; $cnt++ ) {
		if (!($tree[$cnt+1][0]<=$tree[$cnt][0])) {
			$menu_layers .= "<div id='".$layer_label[$cnt]."' style='position: absolute; visibility: hidden; z-index:1000;'><table border='0' cellpadding='0' cellspacing='0' valign='top' width='".$abscissa_step."'><tr><td class='bg2'><table width='100%' border='0' cellpadding='4' cellspacing='1'><tr><td class='bg3' align='center' nowrap='nowrap'><b>".$tree[$cnt][1]."</b></td></tr><tr><td class='bg1' nowrap='nowrap'>".$layer[$layer_label[$cnt]]."<br /></td></tr><tr><td class='bg3'>".$tree[$cnt][5]."<br /><small>".$tree[$cnt][6]."</small></td></tr></table></td></tr></table></div>\n";
		}
	}
	$menu_layers .= "<script language='JavaScript'>\n<!--\nmoveLayers();\nloaded = 1;\n// -->\n</script>\n";
	$content = "<"."?php\n";
	$content .= "\$xoops_admin_menu_js = \"".$js."\";\n";
	foreach ( $moveLayers as $k => $v ){
		$content .= "\$xoops_admin_menu_ml[$k] = \"".$v."\";\n";
	}
	foreach ( $shutdown as $k => $v ){
		$content .= "\$xoops_admin_menu_sd[$k] = \"".$v."\";\n";
	}
	foreach ( $firstleveltable as $k => $v ){
		$content .= "\$xoops_admin_menu_ft[$k] = \"".$v."\";\n";
	}
	$content .= "\$xoops_admin_menu_dv = \"".$menu_layers."\";\n";
	$content .= "\n?".">";
	return $content;
}

function write_module_admin_menu($content){
	if (!xoopsfwrite()) {
		echo "post fail";
		return false;
	}
	$filename = XOOPS_ROOT_PATH."/modules/system/cache/adminmenu.php";
	if ( !$file = fopen($filename, "w") ) {
		echo "open fail fail";
		return false;
	}
	if ( fwrite($file, $content) == -1 ) {
		echo "write fail";
		return false;
	}
	fclose($file);
	return true;
}

} else {
    redirect_header(XOOPS_URL."/",3,_NOPERM);
}

?>