Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        admin
5
 * @package         pages
6
 * @author          Ryan Djurovich, WebsiteBaker Project
7
 * @copyright       2009-2012, WebsiteBaker Org. e.V.
8
 * @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: settings_save.php 1735 2012-08-31 15:24:45Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/pages/settings_save.php $
14
 * @lastmodified    $Date: 2012-08-31 17:24:45 +0200 (Fri, 31 Aug 2012) $
15
 *
16
 */
17
/* */
18

    
19
// Create new admin object and print admin header
20
require('../../config.php');
21
require_once(WB_PATH.'/framework/class.admin.php');
22

    
23
// suppress to print the header, so no new FTAN will be set
24
$admin = new admin('Pages', 'pages_settings',false);
25

    
26
// Get page id
27
if(!isset($_POST['page_id']) || (isset($_POST['page_id']) && preg_match('/[^0-9a-z]/i',$_POST['page_id'])) )
28
{
29
	header("Location: index.php");
30
	exit(0);
31
} else {
32
//	$page_id = $admin->checkIDKEY('page_id');
33
//	$page_id = (int)$_POST['page_id']; || preg_match('/[^0-9a-f]/i',$_POST['page_id'])
34
	if((!($page_id = $admin->checkIDKEY('page_id')))) {
35
		$admin->print_header();
36
		$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS'], ADMIN_URL.'/pages/index.php');
37
	}
38
}
39

    
40
/*
41
if( (!($page_id = $admin->checkIDKEY('page_id', 0, $_SERVER['REQUEST_METHOD']))) )
42
{
43
	$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS']);
44
}
45
*/
46
$pagetree_url = ADMIN_URL.'/pages/index.php';
47
$target_url = ADMIN_URL.'/pages/settings.php?page_id='.$page_id;
48

    
49
if (!$admin->checkFTAN())
50
{
51
	$admin->print_header();
52
	$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS'],$target_url);
53
}
54
// After check print the header
55
$admin->print_header();
56

    
57
// Include the WB functions file
58
require_once(WB_PATH.'/framework/functions.php');
59

    
60
// Get values
61
$page_title = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->get_post_escaped('page_title')));
62
$menu_title = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->get_post_escaped('menu_title')));
63
$page_code = intval($admin->get_post('page_code')) ;
64
$description = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->add_slashes($admin->get_post('description'))));
65
$keywords = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->add_slashes($admin->get_post('keywords'))));
66
$parent = intval($admin->get_post('parent')); // fix secunia 2010-91-3
67
$visibility = $admin->get_post_escaped('visibility');
68
if (!in_array($visibility, array('public', 'private', 'registered', 'hidden', 'none'))) {$visibility = 'public';} // fix secunia 2010-93-3
69
$template = preg_replace('/[^a-z0-9_-]/i', "", $admin->get_post('template')); // fix secunia 2010-93-3
70
$template = (($template == DEFAULT_TEMPLATE ) ? '' : $template);
71
$target = preg_replace("/\W/", "", $admin->get_post('target'));
72
$aAdminGroups   = (isset($_POST['admin_groups']) ? $_POST['admin_groups'] : array(1));
73
$aAdminUsers    = (isset($_POST['admin_users']) ? $_POST['admin_users'] : array());
74
$aViewingGroups = (isset($_POST['viewing_groups']) ? $_POST['viewing_groups'] : array(1));
75
$aViewingUsers  = (isset($_POST['viewing_users']) ? $_POST['viewing_users'] : array());
76
$searching = intval($admin->get_post('searching'));
77
$language = strtoupper($admin->get_post('language'));
78
$language = (preg_match('/^[A-Z]{2}$/', $language) ? $language : DEFAULT_LANGUAGE);
79
$menu = intval($admin->get_post('menu')); // fix secunia 2010-91-3
80
$page_code = (isset($_POST['page_code']) ? intval($_POST['page_code']) : 0);
81
$sPageIcon = (isset($_POST['page_icon']) ? $_POST['page_icon'] : 0);
82
$sMenuIcon0 = (isset($_POST['menu_icon_0']) ? $_POST['menu_icon_0'] : 0);
83
$sMenuIcon1 = (isset($_POST['menu_icon_1']) ? $_POST['menu_icon_1'] : 0);
84

    
85

    
86
// Validate data
87
if($page_title == '' || substr($page_title,0,1)=='.')
88
{
89
	$admin->print_error($MESSAGE['PAGES_BLANK_PAGE_TITLE']);
90
}
91
if($menu_title == '' || substr($menu_title,0,1)=='.')
92
{
93
	$admin->print_error($MESSAGE['PAGES_BLANK_MENU_TITLE']);
94
}
95

    
96
// Get existing perms
97
$sql = 'SELECT `parent`,`link`,`position`,`admin_groups`,`admin_users` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id`='.$page_id;
98
$results = $database->query($sql);
99

    
100
$results_array = $results->fetchRow();
101
$old_parent = $results_array['parent'];
102
$old_link = $results_array['link'];
103
$old_position = $results_array['position'];
104

    
105
if($admin->ami_group_member('1')) {
106
	if(!$admin->ami_group_member($results_array['admin_groups']) &&
107
	   !$admin->is_group_match($admin->get_user_id(), $results_array['admin_users']))
108
	{
109
		$admin->print_error($MESSAGE['PAGES_INSUFFICIENT_PERMISSIONS']);
110
	}
111
	// Setup admin groups
112
	$aAdminGroups = (is_array($aAdminGroups) ? $aAdminGroups : array(1));
113
	array_unshift($aAdminGroups, 1);
114
	$sAdminGroups = implode(',', array_unique($aAdminGroups, SORT_REGULAR));
115
	$sAdminGroups = (preg_match('/^,|[^0-9,]|,,|,$/', $sAdminGroups) ? '1' : $sAdminGroups);
116

    
117
	$aAdminUsers = (is_array($aAdminUsers) ? $aAdminUsers : array());
118
	$sAdminUsers = implode(',', array_diff($aAdminUsers, array(0)));
119
	$sAdminUsers = (preg_match('/^,|[^0-9,]|,,|,$/', $sAdminUsers) ? array() : $sAdminUsers);
120
}
121

    
122
$aViewingGroups = (is_array($aViewingGroups) ? $aViewingGroups : array(1));
123
array_unshift($aViewingGroups, 1);
124
$sViewingGroups = implode(',', array_unique($aViewingGroups, SORT_REGULAR));
125
$sViewingGroups = (preg_match('/^,|[^0-9,]|,,|,$/', $sViewingGroups) ? '1' : $sViewingGroups);
126

    
127
$aViewingUsers = (is_array($aViewingUsers) ? $aViewingUsers : array());
128
$sViewingUsers = implode(',', array_diff($aViewingUsers, array(0)));
129
$sViewingUsers = (preg_match('/^,|[^0-9,]|,,|,$/', $sViewingUsers) ? array() : $sViewingUsers);
130

    
131
$sPageIcon = (($sPageIcon == '0') ? '' : $sPageIcon);
132
if(!is_readable(WB_PATH.$sPageIcon)) { $sPageIcon = ''; }
133
$sMenuIcon0 = (($sMenuIcon0 == '0') ? '' : $sMenuIcon0);
134
if(!is_readable(WB_PATH.$sMenuIcon0)) { $sMenuIcon0 = ''; }
135
$sMenuIcon1 = (($sMenuIcon1 == '0') ? '' : $sMenuIcon1);
136
if(!is_readable(WB_PATH.$sMenuIcon1)) { $sMenuIcon1 = ''; }
137

    
138
// If needed, get new order
139
if($parent != $old_parent)
140
{
141
	// Include ordering class
142
	require(WB_PATH.'/framework/class.order.php');
143
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
144
	// Get new order
145
	$position = $order->get_new($parent);
146
	// Clean new order
147
	$order->clean($parent);
148
} else {
149
	$position = $old_position;
150
}
151

    
152
// Work out level and root parent
153
if ($parent!='0')
154
{
155
	$level = level_count($parent)+1;
156
	$root_parent = root_parent($parent);
157
}
158
else {
159
	$level = '0';
160
	$root_parent = '0';
161
}
162

    
163
// Work-out what the link should be
164
if($parent == '0')
165
{
166
	$link = '/'.page_filename($menu_title);
167
	// rename menu titles: index && intro to prevent clashes with intro page feature and WB core file /pages/index.php
168
	if($link == '/index' || $link == '/intro')
169
    {
170
		$link .= '_' .$page_id;
171
		$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($menu_title).'_'.$page_id .PAGE_EXTENSION;
172
	} else {
173
		$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($menu_title).PAGE_EXTENSION;
174
	}
175
} else {
176
	$parent_section = '';
177
	$parent_titles = array_reverse(get_parent_titles($parent));
178
	foreach($parent_titles AS $parent_title)
179
    {
180
		$parent_section .= page_filename($parent_title).'/';
181
	}
182
	if($parent_section == '/')
183
    {
184
      $parent_section = '';
185
    }
186
	$link = '/'.$parent_section.page_filename($menu_title);
187
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.$parent_section.page_filename($menu_title).PAGE_EXTENSION;
188
}
189

    
190
// Check if a page with same page filename exists
191
// $database = new database();
192
$sql = 'SELECT `page_id`,`page_title` FROM `'.TABLE_PREFIX.'pages` WHERE `link` = "'.$link.'" AND `page_id` != '.$page_id;
193
$get_same_page = $database->query($sql);
194

    
195
if($get_same_page->numRows() > 0)
196
{
197
	$admin->print_error($MESSAGE['PAGES_PAGE_EXISTS']);
198
}
199

    
200
// Update page with new order
201
$sql = 'UPDATE `'.TABLE_PREFIX.'pages` SET `parent`='.$parent.', `position`='.$position.' WHERE `page_id`='.$page_id.'';
202
// $database = new database();
203
$database->query($sql);
204

    
205
// Get page trail
206
$page_trail = get_page_trail($page_id);
207

    
208
// Update page settings in the pages table
209
$sql = 'UPDATE `'.TABLE_PREFIX.'pages` '
210
     . 'SET `parent`='.$parent.', '
211
     .     '`page_title`=\''.$page_title.'\', '
212
     .     '`tooltip`=\''.$page_title.'\', '
213
     .     '`page_icon` =\''.mysql_real_escape_string($sPageIcon).'\', '
214
     .     '`menu_title`=\''.$menu_title.'\', '
215
     .     '`menu_icon_0` =\''.mysql_real_escape_string($sMenuIcon0).'\', '
216
     .     '`menu_icon_1` =\''.mysql_real_escape_string($sMenuIcon1).'\', '
217
     .     '`menu`='.$menu.', '
218
     .     '`level`='.$level.', '
219
     .     '`page_trail`=\''.$page_trail.'\', '
220
     .     '`root_parent`='.$root_parent.', '
221
     .     '`link`=\''.$link.'\', '
222
     .     '`template`=\''.$template.'\', '
223
     .     '`target`=\''.$target.'\', '
224
     .     '`description`=\''.$description.'\', '
225
     .     '`keywords`=\''.$keywords.'\', '
226
     .     '`position`='.$position.', '
227
     .     '`visibility`=\''.$visibility.'\', '
228
     .     '`searching`='.$searching.', '
229
     .     '`language`=\''.$language.'\', ';
230
if($admin->ami_group_member('1')) {
231
	$sql .= ''
232
	     .     '`admin_groups`=\''.$sAdminGroups.'\', '
233
	     .     '`admin_users`=\''.$sAdminUsers.'\', ';
234
}
235
$sql .= ''
236
     .     '`viewing_groups`=\''.$sViewingGroups.'\', '
237
     .     '`viewing_users`=\''.$sViewingUsers.'\', '
238
     .     '`page_code`='.$page_code.' '
239
     . 'WHERE `page_id`='.$page_id;
240

    
241
if(!$database->query($sql)) {
242
	$target_url = ADMIN_URL.'/pages/settings.php?page_id='.$page_id;
243
	$admin->print_error($database->get_error(), $target_url );
244
}
245
// Clean old order if needed
246
if($parent != $old_parent)
247
{
248
	$order->clean($old_parent);
249
}
250

    
251
/* BEGIN page "access file" code */
252

    
253
// Create a new file in the /pages dir if title changed
254
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/'))
255
{
256
	$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
257
} else {
258
    $old_filename = WB_PATH.PAGES_DIRECTORY.$old_link.PAGE_EXTENSION;
259
	// First check if we need to create a new file
260
	if(($old_link != $link) || (!file_exists($old_filename)))
261
    {
262
		// Delete old file
263
		$old_filename = WB_PATH.PAGES_DIRECTORY.$old_link.PAGE_EXTENSION;
264
		if(file_exists($old_filename))
265
        {
266
			unlink($old_filename);
267
		}
268
		// Create access file
269
		create_access_file($filename,$page_id,$level);
270
		// Move a directory for this page
271
		if(file_exists(WB_PATH.PAGES_DIRECTORY.$old_link.'/') && is_dir(WB_PATH.PAGES_DIRECTORY.$old_link.'/'))
272
        {
273
			rename(WB_PATH.PAGES_DIRECTORY.$old_link.'/', WB_PATH.PAGES_DIRECTORY.$link.'/');
274
		}
275
		// Update any pages that had the old link with the new one
276
		$old_link_len = strlen($old_link);
277
        $sql = '';
278
		$query_subs = $database->query("SELECT page_id,link,level FROM ".TABLE_PREFIX."pages WHERE link LIKE '%$old_link/%' ORDER BY LEVEL ASC");
279

    
280
		if($query_subs->numRows() > 0)
281
        {
282
			while($sub = $query_subs->fetchRow())
283
            {
284
				// Double-check to see if it contains old link
285
				if(substr($sub['link'], 0, $old_link_len) == $old_link)
286
                {
287
					// Get new link
288
					$replace_this = $old_link;
289
					$old_sub_link_len =strlen($sub['link']);
290
					$new_sub_link = $link.'/'.substr($sub['link'],$old_link_len+1,$old_sub_link_len);
291
					// Work out level
292
					$new_sub_level = level_count($sub['page_id']);
293
					// Update level and link
294
					$database->query("UPDATE ".TABLE_PREFIX."pages SET link = '$new_sub_link', level = '$new_sub_level' WHERE page_id = '".$sub['page_id']."' LIMIT 1");
295
					// Re-write the access file for this page
296
					$old_subpage_file = WB_PATH.PAGES_DIRECTORY.$new_sub_link.PAGE_EXTENSION;
297
					if(file_exists($old_subpage_file))
298
                    {
299
						unlink($old_subpage_file);
300
					}
301
					create_access_file(WB_PATH.PAGES_DIRECTORY.$new_sub_link.PAGE_EXTENSION, $sub['page_id'], $new_sub_level);
302
				}
303
			}
304
		}
305
	}
306
}
307

    
308
// Function to fix page trail of subs
309
function fix_page_trail($parent,$root_parent)
310
{
311
	// Get objects and vars from outside this function
312
	global $admin, $template, $database, $TEXT, $MESSAGE;
313
	// Get page list from database
314
	// $database = new database();
315
	$query = "SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'";
316
	$get_pages = $database->query($query);
317
	// Insert values into main page list
318
	if($get_pages->numRows() > 0)
319
    {
320
		while($page = $get_pages->fetchRow())
321
        {
322
			// Fix page trail
323

    
324
			$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']."'");
325
			// Run this query on subs
326
			fix_page_trail($page['page_id'],$root_parent);
327
		}
328
	}
329
}
330

    
331
// Fix sub-pages page trail
332
fix_page_trail($page_id,$root_parent);
333

    
334
/* END page "access file" code */
335

    
336
//$pagetree_url = ADMIN_URL.'/pages/index.php';
337
//$target_url = ADMIN_URL.'/pages/settings.php?page_id='.$page_id;
338
// Check if there is a db error, otherwise say successful
339
if($database->is_error())
340
{
341
	$admin->print_error($database->get_error(), $target_url );
342
} else {
343
	$admin->print_success($MESSAGE['PAGES_SAVED_SETTINGS'], $target_url );
344
}
345

    
346
// Print admin footer
347
$admin->print_footer();
(21-21/22)