Project

General

Profile

1 4 ryan
<?php
2
3 40 stefan
// $Id$
4 4 ryan
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8 399 Ruebenwurz
 Copyright (C) 2004-2007, Ryan Djurovich
9 4 ryan
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24
*/
25
26
// Get page id
27
if(!isset($_POST['page_id']) OR !is_numeric($_POST['page_id'])) {
28
	header("Location: index.php");
29 286 stefan
	exit(0);
30 4 ryan
} else {
31
	$page_id = $_POST['page_id'];
32
}
33
34
// Create new admin object and print admin header
35
require('../../config.php');
36
require_once(WB_PATH.'/framework/class.admin.php');
37
$admin = new admin('Pages', 'pages_settings');
38
39
// Include the WB functions file
40
require_once(WB_PATH.'/framework/functions.php');
41
42
// Get values
43 396 Ruebenwurz
$page_title = $admin->add_slashes($admin->get_post_escaped('page_title'));
44
$menu_title = $admin->add_slashes($admin->get_post_escaped('menu_title'));
45 40 stefan
$description = $admin->add_slashes($admin->get_post('description'));
46
$keywords = $admin->add_slashes($admin->get_post('keywords'));
47 4 ryan
$parent = $admin->get_post('parent');
48
$visibility = $admin->get_post('visibility');
49
$template = $admin->get_post('template');
50
$target = $admin->get_post('target');
51
$admin_groups = $admin->get_post('admin_groups');
52
$viewing_groups = $admin->get_post('viewing_groups');
53
$searching = $admin->get_post('searching');
54
$language = $admin->get_post('language');
55
$menu = $admin->get_post('menu');
56
57
// Validate data
58
if($page_title == '') {
59
	$admin->print_error($MESSAGE['PAGES']['BLANK_PAGE_TITLE']);
60
}
61
if($menu_title == '') {
62
	$admin->print_error($MESSAGE['PAGES']['BLANK_MENU_TITLE']);
63
}
64
65
// Get existing perms
66
$database = new database();
67
$results = $database->query("SELECT parent,link,position,admin_groups,admin_users FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'");
68
$results_array = $results->fetchRow();
69
$old_parent = $results_array['parent'];
70
$old_link = $results_array['link'];
71
$old_position = $results_array['position'];
72
$old_admin_groups = explode(',', str_replace('_', '', $results_array['admin_groups']));
73
$old_admin_users = explode(',', str_replace('_', '', $results_array['admin_users']));
74
if(!is_numeric(array_search($admin->get_group_id(), $old_admin_groups)) AND !is_numeric(array_search($admin->get_user_id(), $old_admin_users))) {
75
	$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
76
}
77
78
// Setup admin groups
79
$admin_groups[] = 1;
80
if($admin->get_group_id() != 1) {
81
	$admin_groups[] = $admin->get_group_id();
82
}
83
$admin_groups = implode(',', $admin_groups);
84
// Setup viewing groups
85
$viewing_groups[] = 1;
86
if($admin->get_group_id() != 1) {
87
	$viewing_groups[] = $admin->get_group_id();
88
}
89
$viewing_groups = implode(',', $viewing_groups);
90
91
// If needed, get new order
92
if($parent != $old_parent) {
93
	// Include ordering class
94
	require(WB_PATH.'/framework/class.order.php');
95
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
96
	// Get new order
97
	$position = $order->get_new($parent);
98
	// Clean new order
99
	$order->clean($parent);
100
} else {
101
	$position = $old_position;
102
}
103
104
// Work out level and root parent
105
if ($parent!='0') {
106
	$level = level_count($parent)+1;
107
	$root_parent = root_parent($parent);
108
}
109
else {
110
	$level = '0';
111
	$root_parent = '0';
112
}
113
114
// Work-out what the link should be
115
if($parent == '0') {
116
	$link = '/'.page_filename($menu_title);
117
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($menu_title).'.php';
118
} else {
119
	$parent_section = '';
120
	$parent_titles = array_reverse(get_parent_titles($parent));
121
	foreach($parent_titles AS $parent_title) {
122
		$parent_section .= page_filename($parent_title).'/';
123
	}
124
	if($parent_section == '/') { $parent_section = ''; }
125
	$link = '/'.$parent_section.page_filename($menu_title);
126
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.$parent_section.page_filename($menu_title).'.php';
127
}
128
129
// Check if a page with same page filename exists
130
$database = new database();
131
$get_same_page = $database->query("SELECT page_id,page_title FROM ".TABLE_PREFIX."pages WHERE link = '$link' and page_id != '$page_id'");
132
if($get_same_page->numRows() > 0) {
133
	$admin->print_error($MESSAGE['PAGES']['PAGE_EXISTS']);
134
}
135
136
// Update page with new order
137
$query = "UPDATE ".TABLE_PREFIX."pages SET parent = '$parent', position = '$position' WHERE page_id = '$page_id'";
138
$database = new database();
139
$database->query($query);
140
141
// Get page trail
142
$page_trail = get_page_trail($page_id);
143
144
// Make sure link is not overwritten if page uses the menu link module
145
if(strstr($old_link, '://') != '') {
146
	$link = $old_link;
147
}
148
149
// Update page settings in the pages table
150
$query = "UPDATE ".TABLE_PREFIX."pages SET parent = '$parent', page_title = '$page_title', menu_title = '$menu_title', menu = '$menu', level = '$level', page_trail = '$page_trail', root_parent = '$root_parent', link = '$link', template = '$template', target = '$target', description = '$description', keywords = '$keywords', position = '$position', visibility = '$visibility', searching = '$searching', language = '$language', admin_groups = '$admin_groups', viewing_groups = '$viewing_groups' WHERE page_id = '$page_id'";
151
$database->query($query);
152
153
// Clean old order if needed
154
if($parent != $old_parent) {
155
	$order->clean($old_parent);
156
}
157
158
/* BEGIN page "access file" code */
159
160
// Create a new file in the /pages dir if title changed
161
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
162
	$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
163
} else {
164
	// First check if we need to create a new file
165
	if($old_link != $link) {
166
		// Delete old file
167
		unlink(WB_PATH.PAGES_DIRECTORY.$old_link.'.php');
168
		// Create access file
169
		create_access_file($filename,$page_id,$level);
170
		// Move a directory for this page
171
		if(file_exists(WB_PATH.PAGES_DIRECTORY.$old_link.'/') AND is_dir(WB_PATH.PAGES_DIRECTORY.$old_link.'/')) {
172
			rename(WB_PATH.PAGES_DIRECTORY.$old_link.'/', WB_PATH.PAGES_DIRECTORY.$link.'/');
173
		}
174
		// Update any pages that had the old link with the new one
175
		$old_link_len = strlen($old_link);
176 54 stefan
		$query_subs = $database->query("SELECT page_id,link,level FROM ".TABLE_PREFIX."pages WHERE link LIKE '%$old_link/%' ORDER BY LEVEL ASC");
177 4 ryan
		if($query_subs->numRows() > 0) {
178
			while($sub = $query_subs->fetchRow()) {
179
				// Double-check to see if it contains old link
180
				if(substr($sub['link'], 0, $old_link_len) == $old_link) {
181
					// Get new link
182
					$replace_this = $old_link;
183 54 stefan
					$old_sub_link_len =strlen($sub['link']);
184
					$new_sub_link = $link.'/'.substr($sub['link'],$old_link_len+1,$old_sub_link_len);
185 4 ryan
					// Work out level
186
					$new_sub_level = level_count($sub['page_id']);
187 54 stefan
					// Update level and link
188
					$database->query("UPDATE ".TABLE_PREFIX."pages SET link = '$new_sub_link', level = '$new_sub_level' WHERE page_id = '".$sub['page_id']."' LIMIT 1");
189 4 ryan
					// Re-write the access file for this page
190 54 stefan
					$old_subpage_file = WB_PATH.PAGES_DIRECTORY.$new_sub_link.'.php';
191 4 ryan
					if(file_exists($old_subpage_file)) {
192
						unlink($old_subpage_file);
193
					}
194
					create_access_file(WB_PATH.PAGES_DIRECTORY.$new_sub_link.'.php', $sub['page_id'], $new_sub_level);
195
				}
196
			}
197
		}
198
	}
199
}
200
201
// Function to fix page trail of subs
202
function fix_page_trail($parent) {
203
	// Get objects and vars from outside this function
204
	global $admin, $template, $database, $TEXT, $MESSAGE;
205
	// Get page list from database
206
	$database = new database();
207
	$query = "SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'";
208
	$get_pages = $database->query($query);
209
	// Insert values into main page list
210
	if($get_pages->numRows() > 0)	{
211
		while($page = $get_pages->fetchRow()) {
212
			// Fix page trail
213
			$database->query("UPDATE ".TABLE_PREFIX."pages SET page_trail = '".get_page_trail($page['page_id'])."' WHERE page_id = '".$page['page_id']."'");
214
			// Run this query on subs
215
			fix_page_trail($page['page_id']);
216
		}
217
	}
218
}
219
// Fix sub-pages page trail
220
fix_page_trail($page_id);
221
222
/* END page "access file" code */
223
224
// Check if there is a db error, otherwise say successful
225
if($database->is_error()) {
226
	$admin->print_error($database->get_error(), ADMIN_URL.'/pages/settings.php?page_id='.$page_id);
227
} else {
228
	$admin->print_success($MESSAGE['PAGES']['SAVED_SETTINGS'], ADMIN_URL.'/pages/index.php');
229
}
230
231
// Print admin footer
232
$admin->print_footer();
233
234
?>