<?php

/*
	[Discuz!] (C)2001-2006 Comsenz Inc.
	This is NOT a freeware, use is subject to license terms

	$RCSfile: templates.inc.php,v $
	$Revision: 1.23.2.1 $
	$Date: 2006/09/28 02:34:15 $
*/

if(!defined('IN_DISCUZ') || !isset($PHP_SELF) || !preg_match("/[\/\\\\]admincp\.php$/", $PHP_SELF)) {
        exit('Access Denied');
}

cpheader();
if(!isfounder()) cpmsg('noaccess');

if($action == 'templates') {

	if(!$edit) {

		if(!submitcheck('tplsubmit')) {

			$templates = '';
			$query = $db->query("SELECT * FROM {$tablepre}templates");
			while($tpl = $db->fetch_array($query)) {
				$templates .= "<tr align=\"center\"><td class=\"altbg1\"><input class=\"checkbox\" type=\"checkbox\" name=\"delete[]\" value=\"$tpl[templateid]\"></td>\n".
					"<td class=\"altbg2\"><input type=\"text\" size=\"8\" name=\"namenew[$tpl[templateid]]\" value=\"$tpl[name]\"></td>\n".
					"<td class=\"altbg1\"><input type=\"text\" size=\"20\" name=\"directorynew[$tpl[templateid]]\" value=\"$tpl[directory]\"></td>\n".
					"<td class=\"altbg2\">$tpl[copyright]</td>\n".
					"<td class=\"altbg1\"><a href=\"admincp.php?action=templates&edit=$tpl[templateid]\">[$lang[detail]]</a></td></tr>\n";
			}

			shownav('menu_styles_templates');

?>
<form method="post" action="admincp.php?action=templates&">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header">
<td width="48"><input class="checkbox" type="checkbox" name="chkall" onclick="checkall(this.form)"><?=$lang['del']?></td>
<td><?=$lang['templates_name']?></td><td><?=$lang['templates_directory']?></td><td><?=$lang['copyright']?></td><td><?=$lang['edit']?></td></tr>
<?=$templates?>
<tr align="center" class="altbg1"><td><?=$lang['add_new']?></td>
<td><input type="text" size="8" name="newname"></td>
<td><input type="text" size="20" name="newdirectory"></td>
<td><input type="text" size="25" name="newcopyright"></td>
<td>&nbsp;</td>
</tr></table><br>
<center><input class="button" type="submit" name="tplsubmit" value="<?=$lang['submit']?>"></center></form>
<?

		} else {

			if($newname) {
				if(!$newdirectory) {
					cpmsg('templates_new_directory_invalid');
				} elseif(!istpldir($newdirectory)) {
					$directory = $newdirectory;
					cpmsg('templates_directory_invalid');
				}
				$db->query("INSERT INTO {$tablepre}templates (name, directory, copyright)
					VALUES ('$newname', '$newdirectory', '$newcopyright')", 'UNBUFFERED');
			}

			foreach($directorynew as $id => $directory) {
				if(!$delete || ($delete && !in_array($id, $delete))) {
					if(!istpldir($directory)) {
						cpmsg('templates_directory_invalid');
					} elseif($id == 1 && $directory != './templates/default') {
						cpmsg('templates_default_directory_invalid');
					}
					$db->query("UPDATE {$tablepre}templates SET name='$namenew[$id]', directory='$directorynew[$id]' WHERE templateid='$id'", 'UNBUFFERED');
				}
			}

			if(is_array($delete)) {
				if(in_array('1', $delete)) {
					cpmsg('templates_delete_invalid');
				}
				$ids = $comma = '';
				foreach($delete as $id) {
					$ids .= "$comma'$id'";
					$comma = ', ';
				}
				$db->query("DELETE FROM {$tablepre}templates WHERE templateid IN ($ids) AND templateid<>'1'", 'UNBUFFERED');
				$db->query("UPDATE {$tablepre}styles SET templateid='1' WHERE templateid IN ($ids)", 'UNBUFFERED');
			}

			updatecache('styles');
			cpmsg('templates_update_succeed', 'admincp.php?action=templates');

		}

	} else {


		$query = $db->query("SELECT * FROM {$tablepre}templates WHERE templateid='$edit'");
		if(!$template = $db->fetch_array($query)) {
			cpmsg('undefined_action');
		} elseif(!istpldir($template['directory'])) {
			$directory = $template['directory'];
			cpmsg('templates_directory_invalid');
		}

		$warning = $template['templateid'] == 1 ?
				$lang['templates_edit_default_comment'] :
				$lang['templates_edit_nondefault_comment'];
		if($keyword) {
			$keywordadd = " - $lang[templates_keyword] <i>".dhtmlspecialchars(stripslashes($keyword))."</i> - <a href=\"admincp.php?action=templates&edit=$edit\" style=\"color: ".HEADERTEXT."\">[ $lang[templates_view_all] ]</a>";
			$keywordenc = rawurlencode($keyword);
		}

		$tpldir = dir(DISCUZ_ROOT.'./'.$template['directory']);
		$tplarray = $langarray = array();
		while($entry = $tpldir->read()) {
			$extension = strtolower(fileext($entry));
			if($extension == 'htm') {
				$tplname = substr($entry, 0, -4);
				$pos = strpos($tplname, '_');
				if($keyword) {
					if(!stristr(implode("\n", file(DISCUZ_ROOT."./$template[directory]/$entry")), $keyword)) {
						continue;
					}
				}
				if(!$pos) {
					$tplarray[$tplname][] = $tplname;
				} else {
					$tplarray[substr($tplname, 0, $pos)][] = $tplname;
				}
			} elseif($extension == 'php') {
				$langarray[] = substr($entry, 0, -9);
			}
		}
		$tpldir->close();

		ksort($tplarray);
		ksort($langarray);
		$templates = $languages = '';

		foreach($tplarray as $tpl => $subtpls) {
			$templates .= "<ul><li><b>$tpl</b><ul>\n";
			foreach($subtpls as $subtpl) {
				$filename = "$subtpl.htm";
				$templates .= "<li>$subtpl &nbsp; <a href=\"admincp.php?action=tpledit&templateid=$template[templateid]&fn=$filename&keyword=$keywordenc\">[$lang[edit]]</a> ".
					"<a href=\"admincp.php?action=tpledit&templateid=$template[templateid]&fn=$filename&delete=yes\">[$lang[delete]]</a>";
			}
			$templates .= "</ul></ul>\n";
		}
		foreach($langarray as $langpack) {
			$languages .= "<ul><li>$langpack &nbsp; <a href=\"admincp.php?action=tpledit&templateid=$template[templateid]&fn=$langpack.lang.php\">[$lang[edit]]</a></ul>\n";
		}

		shownav('templates_maint');

?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td colspan="3"><?=$lang['templates_maint']?></td></tr>

<form method="post" action="admincp.php?action=tpladd&edit=<?=$edit?>&">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<tr class="altbg2"><td width="25%"><?=$lang['templates_maint_new']?></td>
<td width="55%"><input type="text" name="name" size="40" maxlength="40"></td>
<td width="20%"><input class="button" type="submit" value="<?=$lang['submit']?>"></td></tr></form>

<form method="get" action="admincp.php">
<input type="hidden" name="action" value="templates">
<input type="hidden" name="edit" value="<?=$edit?>">
<tr class="altbg1"><td><?=$lang['templates_maint_search']?></td><td><input type="text" name="keyword" size="40"></td>
<td><input class="button" type="submit" value="<?=$lang['submit']?>"></td></tr></form>

</table><br><br>

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td><?=$lang['templates_select']?><?=$keywordadd?></td></tr>
<tr class="altbg1"><td><br><center><b><?=$warning?></b></center><br>
<ul><li><b>Discuz! <?=$lang['templates_language_pack']?></b><?=$languages?></ul>
<ul><li><b>Discuz! <?=$lang['templates_html']?></b><?=$templates?></ul>
</td></tr></table>
<?

	}

} elseif($action == 'tpledit') {

	$query = $db->query("SELECT * FROM {$tablepre}templates WHERE templateid='$templateid'");
	if(!$template = $db->fetch_array($query)) {
		cpmsg('templates_edit_nonexistence');
	}

	$fn = str_replace(array('..', '/', '\\'), array('', '', ''), $fn);
	$filename = DISCUZ_ROOT."./$template[directory]/$fn";
	if(!is_writeable($filename)) {
		cpmsg('templates_edit_invalid');
	}

	if(!submitcheck('editsubmit') && $delete != 'yes') {

		$keywordenc = rawurlencode($keyword);

		$fp = @fopen($filename, 'rb');
		$content = dhtmlspecialchars(fread($fp, filesize($filename)));
		fclose($fp);

		shownav('templates_edit');

?>
<script language="JavaScript">
var n = 0;
function displayHTML(obj) {
	win = window.open(" ", 'popup', 'toolbar = no, status = no, scrollbars=yes');
	win.document.write("" + obj.value + "");
}
function HighlightAll(obj) {
	obj.focus();
	obj.select();
	if(document.all) {
		obj.createTextRange().execCommand("Copy");
		window.status = "<?=$lang['templates_edit_clickboard']?>";
		setTimeout("window.status=''", 1800);
	}
}
function findInPage(obj, str) {
	var txt, i, found;
	if(str == "") {
		return false;
	}
	if(document.layers) {
		if(!obj.find(str)) {
			while(obj.find(str, false, true)) {
				n++;
			}
		} else {
			n++;
		}
		if(n == 0) {
			alert("<?=$lang['templates_edit_keyword_not_found']?>");
		}
	}
	if(document.all) {
		txt = obj.createTextRange();
		for(i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
			txt.moveStart('character', 1);
			txt.moveEnd('textedit');
		}
		if(found) {
			txt.moveStart('character', -1);
			txt.findText(str);
			txt.select();
			txt.scrollIntoView();
			n++;
		} else {
			if(n > 0) {
				n = 0;
				findInPage(str);
			} else {
				alert("<?=$lang['templates_edit_keyword_not_found']?>");
			}
		}
	}
	return false;
}
</script>
<form method="post" action="admincp.php?action=tpledit&templateid=<?=$templateid?>&fn=<?=$fn?>&">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<input type="hidden" name="keyword" value="<?=$keywordenc?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header"><td><?=$lang['templates_edit']?> - <?=$fn?></td></tr>
<tr><td class="altbg1" align="center"><div align="center">
<textarea cols="100" rows="25" name="templatenew"><?=$content?></textarea><br>
<input name="search" type="text" accesskey="t" size="20" onChange="n=0;">
<input class="button" type="button" value="<?=$lang['search']?>" accesskey="f" onClick="findInPage(this.form.templatenew, this.form.search.value)">&nbsp;&nbsp;&nbsp;
<input class="button" type="button" value="<?=$lang['return']?>" accesskey="e" onClick="history.go(-1)">
<input class="button" type="button" value="<?=$lang['preview']?>" accesskey="p" onClick="displayHTML(this.form.templatenew)">
<input class="button" type="button" value="<?=$lang['copy']?>" accesskey="c" onClick="HighlightAll(this.form.templatenew)">&nbsp;&nbsp;&nbsp;
<input class="button" type="submit" name="editsubmit" value="<?=$lang['submit']?>"></div>
</td></tr></table>
</form>

<?

	} elseif($delete == 'yes') {

		if(!$confirmed) {
			cpmsg('templates_delete_confirm', "admincp.php?action=tpledit&templateid=$templateid&fn=$fn&delete=yes", 'form');
		} else {
			if(@unlink($filename)) {
				cpmsg('templates_delete_succeed', "admincp.php?action=templates&edit=$templateid");
			} else {
				cpmsg('templates_delete_fail');
			}
		}

	} else {

		$fp = fopen($filename, 'wb');
		flock($fp, 2);
		fwrite($fp, stripslashes(str_replace("\x0d\x0a", "\x0a", $templatenew)));
		fclose($fp);

		if(substr(basename($filename), 0, 3) == 'css') updatecache('styles');

		cpmsg('templates_edit_succeed', "admincp.php?action=templates&edit=$templateid&keyword=$keyword");

	}

} elseif($action == 'tpladd') {

	$query = $db->query("SELECT * FROM {$tablepre}templates WHERE templateid='$edit'");
	if(!$template = $db->fetch_array($query)) {
		cpmsg('templates_add_invalid');
	} elseif(!istpldir($template['directory'])) {
		$directory = $template['directory'];
		cpmsg('templates_directory_invalid');
	} elseif(file_exists(DISCUZ_ROOT."./$template[directory]/$name.htm")) {
		cpmsg('templates_add_duplicate');
	} elseif(!@$fp = fopen(DISCUZ_ROOT."./$template[directory]/$name.htm", 'wb')) {
		cpmsg('templates_add_file_invalid');
	}

	@fclose($fp);
	cpmsg('templates_add_succeed', "admincp.php?action=tpledit&templateid=1&fn=$name.htm");

}

?>