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 1706 2012-08-29 10:40:15Z 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-29 12:40:15 +0200 (Wed, 29 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-f]/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_error($MESSAGE['GENERIC_SECURITY_ACCESS'], ADMIN_URL.'/pages/index.php');
36
	}
37
}
38

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

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

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

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

    
84

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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