Project

General

Profile

1 1203 Luisehahne
<?php
2 1277 Luisehahne
/**
3
 *
4
 * @category        admin
5
 * @package         pages
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8 1349 Luisehahne
 * @copyright       2009-2011, Website Baker Org. e.V.
9 1277 Luisehahne
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12 1349 Luisehahne
 * @requirements    PHP 5.2.2 and higher
13 1277 Luisehahne
 * @version         $Id$
14 1280 Luisehahne
 * @filesource		$HeadURL$
15
 * @lastmodified    $Date$
16 1277 Luisehahne
 *
17
 */
18 1203 Luisehahne
19
// Get page id
20 1277 Luisehahne
if(!isset($_POST['page_id']) OR !is_numeric($_POST['page_id']))
21
{
22 1203 Luisehahne
	header("Location: index.php");
23
	exit(0);
24
} else {
25
	$page_id = $_POST['page_id'];
26
}
27
28
// Create new admin object and print admin header
29
require('../../config.php');
30
require_once(WB_PATH.'/framework/class.admin.php');
31
$admin = new admin('Pages', 'pages_settings');
32
33
// Include the WB functions file
34
require_once(WB_PATH.'/framework/functions.php');
35
36
// Get values
37 1301 Luisehahne
$page_title = htmlspecialchars($admin->get_post_escaped('page_title') );
38
$menu_title = htmlspecialchars($admin->get_post_escaped('menu_title') );
39 1353 FrankH
$page_code = (int) $admin->get_post_escaped('page_code');
40 1301 Luisehahne
$description = htmlspecialchars($admin->add_slashes($admin->get_post('description')) );
41
$keywords = htmlspecialchars($admin->add_slashes($admin->get_post('keywords')) );
42 1353 FrankH
$parent = (int) $admin->get_post_escaped('parent'); // fix secunia 2010-91-3
43 1203 Luisehahne
$visibility = $admin->get_post_escaped('visibility');
44 1353 FrankH
if (!in_array($visibility, array('public', 'private', 'registered', 'hidden', 'none'))) $visibility = 'public'; // fix
45
$template = preg_replace("/\W/", "", $admin->get_post_escaped('template')); // fix secunia 2010-93-3
46
$target = preg_replace("/\W/", "", $admin->get_post_escaped('target'));
47 1203 Luisehahne
$admin_groups = $admin->get_post_escaped('admin_groups');
48
$viewing_groups = $admin->get_post_escaped('viewing_groups');
49
$searching = $admin->get_post_escaped('searching');
50 1353 FrankH
$language = strtoupper($admin->get_post('language'));
51
$language = (preg_match('/^[A-Z]{2}$/', $language) ? $language : DEFAULT_LANGUAGE);
52
$menu = (int) $admin->get_post_escaped('menu'); // fix secunia 2010-91-3
53 1203 Luisehahne
54
// Validate data
55 1277 Luisehahne
if($page_title == '' || substr($page_title,0,1)=='.')
56
{
57 1203 Luisehahne
	$admin->print_error($MESSAGE['PAGES']['BLANK_PAGE_TITLE']);
58
}
59 1277 Luisehahne
if($menu_title == '' || substr($menu_title,0,1)=='.')
60
{
61 1203 Luisehahne
	$admin->print_error($MESSAGE['PAGES']['BLANK_MENU_TITLE']);
62
}
63
64
// Get existing perms
65 1277 Luisehahne
// $database = new database();
66
67
$sql = 'SELECT `parent`,`link`,`position`,`admin_groups`,`admin_users` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id`='.$page_id;
68
$results = $database->query($sql);
69
70 1203 Luisehahne
$results_array = $results->fetchRow();
71
$old_parent = $results_array['parent'];
72
$old_link = $results_array['link'];
73
$old_position = $results_array['position'];
74
$old_admin_groups = explode(',', str_replace('_', '', $results_array['admin_groups']));
75
$old_admin_users = explode(',', str_replace('_', '', $results_array['admin_users']));
76
77
// Work-out if we should check for existing page_code
78 1277 Luisehahne
$sql = 'DESCRIBE `'.TABLE_PREFIX.'pages` `page_code`';
79
$field_sql = $database->query($sql);
80 1203 Luisehahne
$field_set = $field_sql->numRows();
81
82
$in_old_group = FALSE;
83 1301 Luisehahne
foreach($admin->get_groups_id() as $cur_gid)
84
{
85
    if (in_array($cur_gid, $old_admin_groups))
86
    {
87 1203 Luisehahne
	$in_old_group = TRUE;
88
    }
89
}
90 1277 Luisehahne
if((!$in_old_group) AND !is_numeric(array_search($admin->get_user_id(), $old_admin_users)))
91
{
92 1203 Luisehahne
	$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
93
}
94
95
// Setup admin groups
96
$admin_groups[] = 1;
97
//if(!in_array(1, $admin->get_groups_id())) {
98
//	$admin_groups[] = implode(",",$admin->get_groups_id());
99
//}
100 1353 FrankH
$admin_groups = preg_replace("/[^\d,]/", "", implode(',', $admin_groups));
101 1203 Luisehahne
// Setup viewing groups
102
$viewing_groups[] = 1;
103
//if(!in_array(1, $admin->get_groups_id())) {
104
//	$viewing_groups[] = implode(",",$admin->get_groups_id());
105
//}
106 1353 FrankH
$viewing_groups = preg_replace("/[^\d,]/", "", implode(',', $viewing_groups));
107 1203 Luisehahne
108
// If needed, get new order
109 1277 Luisehahne
if($parent != $old_parent)
110
{
111 1203 Luisehahne
	// Include ordering class
112
	require(WB_PATH.'/framework/class.order.php');
113
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
114
	// Get new order
115
	$position = $order->get_new($parent);
116
	// Clean new order
117
	$order->clean($parent);
118
} else {
119
	$position = $old_position;
120
}
121
122
// Work out level and root parent
123 1277 Luisehahne
if ($parent!='0')
124
{
125 1203 Luisehahne
	$level = level_count($parent)+1;
126
	$root_parent = root_parent($parent);
127
}
128
else {
129
	$level = '0';
130
	$root_parent = '0';
131
}
132
133
// Work-out what the link should be
134 1277 Luisehahne
if($parent == '0')
135
{
136 1203 Luisehahne
	$link = '/'.page_filename($menu_title);
137
	// rename menu titles: index && intro to prevent clashes with intro page feature and WB core file /pages/index.php
138 1277 Luisehahne
	if($link == '/index' || $link == '/intro')
139
    {
140 1203 Luisehahne
		$link .= '_' .$page_id;
141
		$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($menu_title).'_'.$page_id .PAGE_EXTENSION;
142
	} else {
143 1277 Luisehahne
		$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($menu_title).PAGE_EXTENSION;
144 1203 Luisehahne
	}
145
} else {
146
	$parent_section = '';
147
	$parent_titles = array_reverse(get_parent_titles($parent));
148 1277 Luisehahne
	foreach($parent_titles AS $parent_title)
149
    {
150 1203 Luisehahne
		$parent_section .= page_filename($parent_title).'/';
151
	}
152 1277 Luisehahne
	if($parent_section == '/')
153
    {
154
      $parent_section = '';
155
    }
156 1203 Luisehahne
	$link = '/'.$parent_section.page_filename($menu_title);
157 1277 Luisehahne
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.$parent_section.page_filename($menu_title).PAGE_EXTENSION;
158 1203 Luisehahne
}
159
160
// Check if a page with same page filename exists
161 1277 Luisehahne
// $database = new database();
162
$sql = 'SELECT `page_id`,`page_title` FROM `'.TABLE_PREFIX.'pages` WHERE `link` = "'.$link.'" AND `page_id` != '.$page_id;
163
$get_same_page = $database->query($sql);
164
165
if($get_same_page->numRows() > 0)
166
{
167 1203 Luisehahne
	$admin->print_error($MESSAGE['PAGES']['PAGE_EXISTS']);
168
}
169
170
// Update page with new order
171 1277 Luisehahne
$sql = 'UPDATE `'.TABLE_PREFIX.'pages` SET `parent`='.$parent.', `position`='.$position.' WHERE `page_id`='.$page_id.'';
172
// $database = new database();
173
$database->query($sql);
174 1203 Luisehahne
175
// Get page trail
176
$page_trail = get_page_trail($page_id);
177
178
// Update page settings in the pages table
179 1277 Luisehahne
$sql  = 'UPDATE `'.TABLE_PREFIX.'pages` SET ';
180
$sql .= '`parent` = '.$parent.', ';
181
$sql .= '`page_title` = "'.$page_title.'", ';
182
$sql .= '`menu_title` = "'.$menu_title.'", ';
183
$sql .= '`menu` = '.$menu.', ';
184
$sql .= '`level` = '.$level.', ';
185
$sql .= '`page_trail` = "'.$page_trail.'", ';
186
$sql .= '`root_parent` = '.$root_parent.', ';
187
$sql .= '`link` = "'.$link.'", ';
188
$sql .= '`template` = "'.$template.'", ';
189
$sql .= '`target` = "'.$target.'", ';
190
$sql .= '`description` = "'.$description.'", ';
191
$sql .= '`keywords` = "'.$keywords.'", ';
192
$sql .= '`position` = '.$position.', ';
193
$sql .= '`visibility` = "'.$visibility.'", ';
194
$sql .= '`searching` = '.$searching.', ';
195
$sql .= '`language` = "'.$language.'", ';
196
$sql .= '`admin_groups` = "'.$admin_groups.'", ';
197
$sql .= '`viewing_groups` = "'.$viewing_groups.'"';
198 1353 FrankH
$sql .= (defined('PAGE_LANGUAGES') && PAGE_LANGUAGES) && $field_set && (file_exists(WB_PATH.'/modules/mod_multilingual/update_keys.php')) ? ', `page_code` = '.$page_code.' ' : ' ';
199 1277 Luisehahne
$sql .= 'WHERE `page_id` = '.$page_id;
200
$database->query($sql);
201
202
$target_url = ADMIN_URL.'/pages/settings.php?page_id='.$page_id;
203
if($database->is_error())
204 1203 Luisehahne
{
205 1277 Luisehahne
	$admin->print_error($database->get_error(), $target_url );
206 1203 Luisehahne
}
207
// Clean old order if needed
208 1277 Luisehahne
if($parent != $old_parent)
209
{
210 1203 Luisehahne
	$order->clean($old_parent);
211
}
212
213
/* BEGIN page "access file" code */
214
215
// Create a new file in the /pages dir if title changed
216 1277 Luisehahne
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/'))
217
{
218 1203 Luisehahne
	$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
219
} else {
220 1301 Luisehahne
    $old_filename = WB_PATH.PAGES_DIRECTORY.$old_link.PAGE_EXTENSION;
221 1203 Luisehahne
	// First check if we need to create a new file
222 1301 Luisehahne
	if(($old_link != $link) || (!file_exists($old_filename)))
223 1277 Luisehahne
    {
224 1203 Luisehahne
		// Delete old file
225
		$old_filename = WB_PATH.PAGES_DIRECTORY.$old_link.PAGE_EXTENSION;
226 1277 Luisehahne
		if(file_exists($old_filename))
227
        {
228 1203 Luisehahne
			unlink($old_filename);
229
		}
230
		// Create access file
231
		create_access_file($filename,$page_id,$level);
232
		// Move a directory for this page
233 1277 Luisehahne
		if(file_exists(WB_PATH.PAGES_DIRECTORY.$old_link.'/') AND is_dir(WB_PATH.PAGES_DIRECTORY.$old_link.'/'))
234
        {
235 1203 Luisehahne
			rename(WB_PATH.PAGES_DIRECTORY.$old_link.'/', WB_PATH.PAGES_DIRECTORY.$link.'/');
236
		}
237
		// Update any pages that had the old link with the new one
238
		$old_link_len = strlen($old_link);
239 1277 Luisehahne
        $sql = '';
240 1203 Luisehahne
		$query_subs = $database->query("SELECT page_id,link,level FROM ".TABLE_PREFIX."pages WHERE link LIKE '%$old_link/%' ORDER BY LEVEL ASC");
241 1277 Luisehahne
242
		if($query_subs->numRows() > 0)
243
        {
244
			while($sub = $query_subs->fetchRow())
245
            {
246 1203 Luisehahne
				// Double-check to see if it contains old link
247 1277 Luisehahne
				if(substr($sub['link'], 0, $old_link_len) == $old_link)
248
                {
249 1203 Luisehahne
					// Get new link
250
					$replace_this = $old_link;
251
					$old_sub_link_len =strlen($sub['link']);
252
					$new_sub_link = $link.'/'.substr($sub['link'],$old_link_len+1,$old_sub_link_len);
253
					// Work out level
254
					$new_sub_level = level_count($sub['page_id']);
255
					// Update level and link
256
					$database->query("UPDATE ".TABLE_PREFIX."pages SET link = '$new_sub_link', level = '$new_sub_level' WHERE page_id = '".$sub['page_id']."' LIMIT 1");
257
					// Re-write the access file for this page
258
					$old_subpage_file = WB_PATH.PAGES_DIRECTORY.$new_sub_link.PAGE_EXTENSION;
259 1277 Luisehahne
					if(file_exists($old_subpage_file))
260
                    {
261 1203 Luisehahne
						unlink($old_subpage_file);
262
					}
263
					create_access_file(WB_PATH.PAGES_DIRECTORY.$new_sub_link.PAGE_EXTENSION, $sub['page_id'], $new_sub_level);
264
				}
265
			}
266
		}
267
	}
268
}
269
270
// Function to fix page trail of subs
271 1277 Luisehahne
function fix_page_trail($parent,$root_parent)
272
{
273 1203 Luisehahne
	// Get objects and vars from outside this function
274
	global $admin, $template, $database, $TEXT, $MESSAGE;
275
	// Get page list from database
276 1277 Luisehahne
	// $database = new database();
277 1203 Luisehahne
	$query = "SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'";
278
	$get_pages = $database->query($query);
279
	// Insert values into main page list
280 1277 Luisehahne
	if($get_pages->numRows() > 0)
281
    {
282
		while($page = $get_pages->fetchRow())
283
        {
284 1203 Luisehahne
			// Fix page trail
285 1277 Luisehahne
286 1203 Luisehahne
			$database->query("UPDATE ".TABLE_PREFIX."pages SET ".($root_parent != 0 ?"root_parent = '$root_parent', ":"")." page_trail = '".get_page_trail($page['page_id'])."' WHERE page_id = '".$page['page_id']."'");
287
			// Run this query on subs
288
			fix_page_trail($page['page_id'],$root_parent);
289
		}
290
	}
291
}
292 1277 Luisehahne
293 1203 Luisehahne
// Fix sub-pages page trail
294
fix_page_trail($page_id,$root_parent);
295
296
/* END page "access file" code */
297
298 1277 Luisehahne
$pagetree_url = ADMIN_URL.'/pages/index.php';
299
$target_url = ADMIN_URL.'/pages/settings.php?page_id='.$page_id;
300 1203 Luisehahne
// Check if there is a db error, otherwise say successful
301 1277 Luisehahne
if($database->is_error())
302
{
303
	$admin->print_error($database->get_error(), $target_url );
304 1203 Luisehahne
} else {
305 1277 Luisehahne
	$admin->print_success($MESSAGE['PAGES']['SAVED_SETTINGS'], $target_url );
306 1203 Luisehahne
}
307
308
// Print admin footer
309
$admin->print_footer();
310
311 1277 Luisehahne
?>