Project

General

Profile

1 4 ryan
<?php
2 1392 Luisehahne
/**
3
 *
4
 * @category        admin
5
 * @package         templates
6 1712 Luisehahne
 * @author          Ryan Djurovich, WebsiteBaker Project
7
 * @copyright       2009-2012, WebsiteBaker Org. e.V.
8 1392 Luisehahne
 * @link			http://www.websitebaker2.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11
 * @requirements    PHP 5.2.2 and higher
12
 * @version         $Id$
13 1457 Luisehahne
 * @filesource		$HeadURL$
14
 * @lastmodified    $Date$
15 1392 Luisehahne
 *
16
 */
17 4 ryan
18 1457 Luisehahne
// Setup admin object
19
require('../../config.php');
20
require_once(WB_PATH.'/framework/class.admin.php');
21
// suppress to print the header, so no new FTAN will be set
22
$admin = new admin('Addons', 'templates_uninstall', false);
23
if( !$admin->checkFTAN() )
24
{
25 1467 Luisehahne
	$admin->print_header();
26 1457 Luisehahne
	$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS']);
27
}
28
// After check print the header
29
$admin->print_header();
30 1712 Luisehahne
if(!isset($_POST['file']) OR $_POST['file'] == "") {
31
	$admin->print_error($MESSAGE['GENERIC_FORGOT_OPTIONS']);
32
} else {
33
	$file = preg_replace('/[^a-z0-9_-]/i', "", $_POST['file']);  // fix secunia 2010-92-2
34
}
35 1457 Luisehahne
36 1712 Luisehahne
// Check if the template exists
37
if(!is_dir(WB_PATH.'/templates/'.$file)) {
38
	$admin->print_error($MESSAGE['GENERIC_NOT_INSTALLED']);
39
}
40
41
// Check if the template exists
42
if(!is_readable(WB_PATH.'/templates/'.$file)) {
43
	$admin->print_error($MESSAGE['ADMIN_INSUFFICIENT_PRIVELLIGES']);
44
}
45
46 4 ryan
// Check if user selected template
47 1712 Luisehahne
/*
48 4 ryan
if(!isset($_POST['file']) OR $_POST['file'] == "") {
49
	header("Location: index.php");
50 286 stefan
	exit(0);
51 4 ryan
} else {
52
	$file = $_POST['file'];
53
}
54
55 268 ryan
// Extra protection
56
if(trim($file) == '') {
57
	header("Location: index.php");
58 286 stefan
	exit(0);
59 268 ryan
}
60 1712 Luisehahne
*/
61 268 ryan
62 4 ryan
// Include the WB functions file
63
require_once(WB_PATH.'/framework/functions.php');
64 862 aldus
if (!function_exists("replace_all")) {
65
	function replace_all ($aStr = "", &$aArray ) {
66
		foreach($aArray as $k=>$v) $aStr = str_replace("{{".$k."}}", $v, $aStr);
67
		return $aStr;
68
	}
69
}
70
71
/**
72
*	Check if the template is the standard-template or still in use
73
*/
74 893 aldus
if (!array_key_exists('CANNOT_UNINSTALL_IS_DEFAULT_TEMPLATE', $MESSAGE['GENERIC'] ) )
75 1712 Luisehahne
	$MESSAGE['GENERIC_CANNOT_UNINSTALL_IS_DEFAULT_TEMPLATE'] = "Can't uninstall this template <b>{{name}}</b> because it's the standardtemplate!";
76 862 aldus
77 944 Ruebenwurz
// check whether the template is used as default wb theme
78
if($file == DEFAULT_THEME) {
79
	$temp = array ('name' => $file );
80 1712 Luisehahne
	$msg = replace_all( $MESSAGE['GENERIC_CANNOT_UNINSTALL_IS_DEFAULT_TEMPLATE'], $temp );
81 944 Ruebenwurz
	$admin->print_error( $msg );
82
}
83
84 862 aldus
if ($file == DEFAULT_TEMPLATE) {
85 893 aldus
	$temp = array ('name' => $file );
86 1712 Luisehahne
	$msg = replace_all( $MESSAGE['GENERIC_CANNOT_UNINSTALL_IS_DEFAULT_TEMPLATE'], $temp );
87 893 aldus
	$admin->print_error( $msg );
88 862 aldus
89 4 ryan
} else {
90 1712 Luisehahne
91 862 aldus
	/**
92
	*	Check if the template is still in use by a page ...
93
	*/
94
	$info = $database->query("SELECT page_id, page_title FROM ".TABLE_PREFIX."pages WHERE template='".$file."' order by page_title");
95 1712 Luisehahne
96 862 aldus
	if ($info->numRows() > 0) {
97
		/**
98
		*	Template is still in use, so we're collecting the page-titles
99
		*/
100 1712 Luisehahne
101 862 aldus
		/**
102
		*	The base-message template-string for the top of the message
103
		*/
104 893 aldus
		if (!array_key_exists("CANNOT_UNINSTALL_IN_USE_TMPL", $MESSAGE['GENERIC'])) {
105
			$add = $info->numRows() == 1 ? "this page" : "these pages";
106
			$msg_template_str  = "<br /><br />{{type}} <b>{{type_name}}</b> could not be uninstalled because it is still in use by {{pages}}";
107
			$msg_template_str .= ":<br /><i>click for editing.</i><br /><br />";
108
		} else {
109 1712 Luisehahne
			$msg_template_str = $MESSAGE['GENERIC_CANNOT_UNINSTALL_IN_USE_TMPL'];
110
			$temp = explode(";",$MESSAGE['GENERIC_CANNOT_UNINSTALL_IN_USE_TMPL_PAGES']);
111 893 aldus
			$add = $info->numRows() == 1 ? $temp[0] : $temp[1];
112
		}
113 862 aldus
		/**
114
		*	The template-string for displaying the Page-Titles ... in this case as a link
115
		*/
116
		$page_template_str = "- <b><a href='../pages/settings.php?page_id={{id}}'>{{title}}</a></b><br />";
117 1712 Luisehahne
118 893 aldus
		$values = array ('type' => 'Template', 'type_name' => $file, 'pages' => $add);
119 862 aldus
		$msg = replace_all ( $msg_template_str,  $values );
120 1712 Luisehahne
121 862 aldus
		$page_names = "";
122 1712 Luisehahne
123 880 aldus
		while ($data = $info->fetchRow() ) {
124 1712 Luisehahne
125 862 aldus
			$page_info = array(
126 1712 Luisehahne
				'id'	=> $data['page_id'],
127 862 aldus
				'title' => $data['page_title']
128
			);
129 1712 Luisehahne
130 862 aldus
			$page_names .= replace_all ( $page_template_str, $page_info );
131
		}
132 1712 Luisehahne
133 862 aldus
		/**
134
		*	Printing out the error-message and die().
135
		*/
136 1712 Luisehahne
		$admin->print_error($MESSAGE['GENERIC_CANNOT_UNINSTALL_IN_USE'].$msg.$page_names);
137 4 ryan
	}
138
}
139
140
// Check if we have permissions on the directory
141
if(!is_writable(WB_PATH.'/templates/'.$file)) {
142 1712 Luisehahne
	$admin->print_error($MESSAGE['GENERIC_CANNOT_UNINSTALL'].WB_PATH.'/templates/'.$file);
143 4 ryan
}
144
145
// Try to delete the template dir
146
if(!rm_full_dir(WB_PATH.'/templates/'.$file)) {
147 1712 Luisehahne
	$admin->print_error($MESSAGE['GENERIC_CANNOT_UNINSTALL']);
148 170 ryan
} else {
149
	// Remove entry from DB
150 211 stefan
	$database->query("DELETE FROM ".TABLE_PREFIX."addons WHERE directory = '".$file."' AND type = 'template'");
151 4 ryan
}
152
153
// Update pages that use this template with default template
154 1386 Luisehahne
// $database = new database();
155 4 ryan
$database->query("UPDATE ".TABLE_PREFIX."pages SET template = '".DEFAULT_TEMPLATE."' WHERE template = '$file'");
156
157
// Print success message
158 1712 Luisehahne
$admin->print_success($MESSAGE['GENERIC_UNINSTALLED']);
159 4 ryan
160
// Print admin footer
161
$admin->print_footer();