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
 Copyright (C) 2004-2005, Ryan Djurovich
9
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
} else {
30
	$page_id = $_POST['page_id'];
31
}
32
33
// Create new admin object and print admin header
34
require('../../config.php');
35
require_once(WB_PATH.'/framework/class.admin.php');
36
$admin = new admin('Pages', 'pages_settings');
37
38
// Include the WB functions file
39
require_once(WB_PATH.'/framework/functions.php');
40
41
// Get values
42 40 stefan
$page_title = $admin->add_slashes($admin->get_post('page_title'));
43
$menu_title = $admin->add_slashes($admin->get_post('menu_title'));
44
$description = $admin->add_slashes($admin->get_post('description'));
45
$keywords = $admin->add_slashes($admin->get_post('keywords'));
46 4 ryan
$parent = $admin->get_post('parent');
47
$visibility = $admin->get_post('visibility');
48
$template = $admin->get_post('template');
49
$target = $admin->get_post('target');
50
$admin_groups = $admin->get_post('admin_groups');
51
$viewing_groups = $admin->get_post('viewing_groups');
52
$searching = $admin->get_post('searching');
53
$language = $admin->get_post('language');
54
$menu = $admin->get_post('menu');
55
56
// Validate data
57
if($page_title == '') {
58
	$admin->print_error($MESSAGE['PAGES']['BLANK_PAGE_TITLE']);
59
}
60
if($menu_title == '') {
61
	$admin->print_error($MESSAGE['PAGES']['BLANK_MENU_TITLE']);
62
}
63
64
// Get existing perms
65
$database = new database();
66
$results = $database->query("SELECT parent,link,position,admin_groups,admin_users FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'");
67
$results_array = $results->fetchRow();
68
$old_parent = $results_array['parent'];
69
$old_link = $results_array['link'];
70
$old_position = $results_array['position'];
71
$old_admin_groups = explode(',', str_replace('_', '', $results_array['admin_groups']));
72
$old_admin_users = explode(',', str_replace('_', '', $results_array['admin_users']));
73
if(!is_numeric(array_search($admin->get_group_id(), $old_admin_groups)) AND !is_numeric(array_search($admin->get_user_id(), $old_admin_users))) {
74
	$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
75
}
76
77
// Setup admin groups
78
$admin_groups[] = 1;
79
if($admin->get_group_id() != 1) {
80
	$admin_groups[] = $admin->get_group_id();
81
}
82
$admin_groups = implode(',', $admin_groups);
83
// Setup viewing groups
84
$viewing_groups[] = 1;
85
if($admin->get_group_id() != 1) {
86
	$viewing_groups[] = $admin->get_group_id();
87
}
88
$viewing_groups = implode(',', $viewing_groups);
89
90
// If needed, get new order
91
if($parent != $old_parent) {
92
	// Include ordering class
93
	require(WB_PATH.'/framework/class.order.php');
94
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
95
	// Get new order
96
	$position = $order->get_new($parent);
97
	// Clean new order
98
	$order->clean($parent);
99
} else {
100
	$position = $old_position;
101
}
102
103
// Work out level and root parent
104
if ($parent!='0') {
105
	$level = level_count($parent)+1;
106
	$root_parent = root_parent($parent);
107
}
108
else {
109
	$level = '0';
110
	$root_parent = '0';
111
}
112
113
// Work-out what the link should be
114
if($parent == '0') {
115
	$link = '/'.page_filename($menu_title);
116
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($menu_title).'.php';
117
} else {
118
	$parent_section = '';
119
	$parent_titles = array_reverse(get_parent_titles($parent));
120
	foreach($parent_titles AS $parent_title) {
121
		$parent_section .= page_filename($parent_title).'/';
122
	}
123
	if($parent_section == '/') { $parent_section = ''; }
124
	$link = '/'.$parent_section.page_filename($menu_title);
125
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.$parent_section.page_filename($menu_title).'.php';
126
}
127
128
// Check if a page with same page filename exists
129
$database = new database();
130
$get_same_page = $database->query("SELECT page_id,page_title FROM ".TABLE_PREFIX."pages WHERE link = '$link' and page_id != '$page_id'");
131
if($get_same_page->numRows() > 0) {
132
	$admin->print_error($MESSAGE['PAGES']['PAGE_EXISTS']);
133
}
134
135
// Update page with new order
136
$query = "UPDATE ".TABLE_PREFIX."pages SET parent = '$parent', position = '$position' WHERE page_id = '$page_id'";
137
$database = new database();
138
$database->query($query);
139
140
// Get page trail
141
$page_trail = get_page_trail($page_id);
142
143
// Make sure link is not overwritten if page uses the menu link module
144
if(strstr($old_link, '://') != '') {
145
	$link = $old_link;
146
}
147
148
// Update page settings in the pages table
149
$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'";
150
$database->query($query);
151
152
// Clean old order if needed
153
if($parent != $old_parent) {
154
	$order->clean($old_parent);
155
}
156
157
/* BEGIN page "access file" code */
158
159
// Create a new file in the /pages dir if title changed
160
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
161
	$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
162
} else {
163
	// First check if we need to create a new file
164
	if($old_link != $link) {
165
		// Delete old file
166
		unlink(WB_PATH.PAGES_DIRECTORY.$old_link.'.php');
167
		// Create access file
168
		create_access_file($filename,$page_id,$level);
169
		// Move a directory for this page
170
		if(file_exists(WB_PATH.PAGES_DIRECTORY.$old_link.'/') AND is_dir(WB_PATH.PAGES_DIRECTORY.$old_link.'/')) {
171
			rename(WB_PATH.PAGES_DIRECTORY.$old_link.'/', WB_PATH.PAGES_DIRECTORY.$link.'/');
172
		}
173
		// Update any pages that had the old link with the new one
174
		$old_link_len = strlen($old_link);
175
		$query_subs = $database->query("SELECT page_id,link,level FROM ".TABLE_PREFIX."pages WHERE link LIKE '%$old_link/%'");
176
		if($query_subs->numRows() > 0) {
177
			while($sub = $query_subs->fetchRow()) {
178
				// Double-check to see if it contains old link
179
				if(substr($sub['link'], 0, $old_link_len) == $old_link) {
180
					// Get new link
181
					$replace_this = $old_link;
182
					$with_this = $link;
183
					$old_value = $sub['link'];
184
					$new_value = '';
185
					$explode = explode('/',$old_value);
186
					$replaced = false;
187
					foreach($explode AS $exploded) {
188
						if($exploded != '') {
189
							$exploded_value = '/'.$exploded;
190
							if($replaced != true AND strstr($exploded_value,$replace_this) != '') {
191
								$new_value .= str_replace($replace_this,$with_this,$exploded_value);
192
								$replaced = true;
193
							} else {
194
								$new_value .= $exploded_value;
195
							}
196
						}
197
					}
198
					$new_sub_link = $new_value;
199
					// Work out level
200
					$new_sub_level = level_count($sub['page_id']);
201
					// If level has changed update level as well as link
202
					if($new_sub_level != $sub['level']) {
203
						// Update level and link
204
						$database->query("UPDATE ".TABLE_PREFIX."pages SET link = '$new_sub_link', level = '$new_sub_level' WHERE page_id = '".$sub['page_id']."' LIMIT 1");
205
					} else {
206
						// Update link
207
						$database->query("UPDATE ".TABLE_PREFIX."pages SET link = '$new_sub_link' WHERE page_id = '".$sub['page_id']."' LIMIT 1");
208
					}
209
					// Re-write the access file for this page
210
					$old_subpage_file = WB_PATH.PAGES_DIRECTORY.$sub['link'].'.php';
211
					if(file_exists($old_subpage_file)) {
212
						unlink($old_subpage_file);
213
					}
214
					create_access_file(WB_PATH.PAGES_DIRECTORY.$new_sub_link.'.php', $sub['page_id'], $new_sub_level);
215
				}
216
			}
217
		}
218
	}
219
}
220
221
// Function to fix page trail of subs
222
function fix_page_trail($parent) {
223
	// Get objects and vars from outside this function
224
	global $admin, $template, $database, $TEXT, $MESSAGE;
225
	// Get page list from database
226
	$database = new database();
227
	$query = "SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'";
228
	$get_pages = $database->query($query);
229
	// Insert values into main page list
230
	if($get_pages->numRows() > 0)	{
231
		while($page = $get_pages->fetchRow()) {
232
			// Fix page trail
233
			$database->query("UPDATE ".TABLE_PREFIX."pages SET page_trail = '".get_page_trail($page['page_id'])."' WHERE page_id = '".$page['page_id']."'");
234
			// Run this query on subs
235
			fix_page_trail($page['page_id']);
236
		}
237
	}
238
}
239
// Fix sub-pages page trail
240
fix_page_trail($page_id);
241
242
/* END page "access file" code */
243
244
// Check if there is a db error, otherwise say successful
245
if($database->is_error()) {
246
	$admin->print_error($database->get_error(), ADMIN_URL.'/pages/settings.php?page_id='.$page_id);
247
} else {
248
	$admin->print_success($MESSAGE['PAGES']['SAVED_SETTINGS'], ADMIN_URL.'/pages/index.php');
249
}
250
251
// Print admin footer
252
$admin->print_footer();
253
254
?>