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 1782 2012-10-11 12:29:11Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/pages/settings_save.php $
14
 * @lastmodified    $Date: 2012-10-11 14:29:11 +0200 (Thu, 11 Oct 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
$lang_dir = dirname(__FILE__).'/languages/';
24
$lang = file_exists($lang_dir.LANGUAGE.'.php') ? LANGUAGE : 'EN';
25
require_once($lang_dir.$lang.'.php');
26

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

    
30
$pagetree_url = ADMIN_URL.'/pages/index.php';
31

    
32
// Get page id
33
if(!isset($_POST['page_id']) || (isset($_POST['page_id']) && preg_match('/[^0-9a-z]/i',$_POST['page_id'])) )
34
{
35
	header("Location: index.php");
36
	exit(0);
37
} else {
38
//	$page_id = $admin->checkIDKEY('page_id');
39
//	$page_id = (int)$_POST['page_id']; || preg_match('/[^0-9a-f]/i',$_POST['page_id'])
40
	if((!($page_id = $admin->checkIDKEY('page_id')))) {
41
		$admin->print_header();
42
		$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS'], $pagetree_url);
43
	}
44
}
45

    
46
/*
47
if( (!($page_id = $admin->checkIDKEY('page_id', 0, $_SERVER['REQUEST_METHOD']))) )
48
{
49
	$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS']);
50
}
51
*/
52
$target_url = ADMIN_URL.'/pages/settings.php?page_id='.$page_id;
53

    
54
if (!$admin->checkFTAN())
55
{
56
	$admin->print_header();
57
	$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS'],$target_url);
58
}
59
// After check print the header
60
$admin->print_header();
61

    
62
// Include the WB functions file
63
require_once(WB_PATH.'/framework/functions.php');
64

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

    
92
// Validate data
93
if($page_title == '' || substr($page_title,0,1)=='.')
94
{
95
	$admin->print_error($MESSAGE['PAGES_BLANK_PAGE_TITLE'],$target_url);
96
}
97
if($menu_title == '' || substr($menu_title,0,1)=='.')
98
{
99
	$admin->print_error($MESSAGE['PAGES_BLANK_MENU_TITLE'],$target_url);
100
}
101
if($seo_title == '' || substr($seo_title,0,1)=='.')
102
{
103
    $seo_title = $menu_title;
104
}
105

    
106
// Get existing perms
107
$sql  = 'SELECT `parent`,`link`,`position`,`admin_groups`,`admin_users`,`menu_title` ';
108
$sql .= 'FROM `'.TABLE_PREFIX.'pages` WHERE `page_id`='.$page_id;
109

    
110
$results = $database->query($sql);
111

    
112
$results_array = $results->fetchRow(MYSQL_ASSOC);
113
$old_parent = $results_array['parent'];
114
$old_link = $results_array['link'];
115
$old_position = $results_array['position'];
116

    
117
if($admin->ami_group_member('1')) {
118
	if(!$admin->ami_group_member($results_array['admin_groups']) &&
119
	   !$admin->is_group_match($admin->get_user_id(), $results_array['admin_users']))
120
	{
121
		$admin->print_error($MESSAGE['PAGES_INSUFFICIENT_PERMISSIONS']);
122
	}
123
	// Setup admin groups
124
	$aAdminGroups = (is_array($aAdminGroups) ? $aAdminGroups : array(1));
125
	array_unshift($aAdminGroups, 1);
126
	$sAdminGroups = implode(',', array_unique($aAdminGroups, SORT_REGULAR));
127
	$sAdminGroups = (preg_match('/^,|[^0-9,]|,,|,$/', $sAdminGroups) ? '1' : $sAdminGroups);
128

    
129
	$aAdminUsers = (is_array($aAdminUsers) ? $aAdminUsers : array());
130
	$sAdminUsers = implode(',', array_diff($aAdminUsers, array(0)));
131
	$sAdminUsers = (preg_match('/^,|[^0-9,]|,,|,$/', $sAdminUsers) ? array() : $sAdminUsers);
132
}
133

    
134
$aViewingGroups = (is_array($aViewingGroups) ? $aViewingGroups : array(1));
135
array_unshift($aViewingGroups, 1);
136
$sViewingGroups = implode(',', array_unique($aViewingGroups, SORT_REGULAR));
137
$sViewingGroups = (preg_match('/^,|[^0-9,]|,,|,$/', $sViewingGroups) ? '1' : $sViewingGroups);
138

    
139
$aViewingUsers = (is_array($aViewingUsers) ? $aViewingUsers : array());
140
$sViewingUsers = implode(',', array_diff($aViewingUsers, array(0)));
141
$sViewingUsers = (preg_match('/^,|[^0-9,]|,,|,$/', $sViewingUsers) ? array() : $sViewingUsers);
142

    
143
$sPageIcon = (($sPageIcon == '0') ? '' : $sPageIcon);
144
if(!is_readable(WB_PATH.$sPageIcon)) { $sPageIcon = ''; }
145
$sMenuIcon0 = (($sMenuIcon0 == '0') ? '' : $sMenuIcon0);
146
if(!is_readable(WB_PATH.$sMenuIcon0)) { $sMenuIcon0 = ''; }
147
$sMenuIcon1 = (($sMenuIcon1 == '0') ? '' : $sMenuIcon1);
148
if(!is_readable(WB_PATH.$sMenuIcon1)) { $sMenuIcon1 = ''; }
149

    
150
// If needed, get new order
151
if($parent != $old_parent)
152
{
153
	// Include ordering class
154
	require(WB_PATH.'/framework/class.order.php');
155
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
156
	// Get new order
157
	$position = $order->get_new($parent);
158
	// Clean new order
159
	$order->clean($parent);
160
} else {
161
	$position = $old_position;
162
}
163

    
164
// Work out level and root parent
165
if ($parent!='0')
166
{
167
	$level = level_count($parent)+1;
168
	$root_parent = root_parent($parent);
169
} else {
170
// Work out level
171
    $level = level_count($page_id);
172
// Work out root parent
173
    $root_parent = root_parent($page_id);
174
}
175

    
176
$link = '/'.page_filename($seo_title);
177

    
178
// Work-out what the link should be
179
if($parent == '0') {
180
    if($link == '/index' || $link == '/intro')
181
    {
182
    	$link .= '_' .$page_id;
183
    	$filename = WB_PATH.PAGES_DIRECTORY.$link .PAGE_EXTENSION;
184

    
185
    } else {
186
        $filename = WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
187
    }
188
} else {
189
	$parent_section = '';
190
	$parent_titles = array_reverse(get_parent_titles($parent));
191

    
192
	foreach($parent_titles AS $parent_title)
193
    {
194
		$parent_section .= '/'.page_filename($parent_title);
195
	}
196

    
197
	if($parent_section == '/')
198
    {
199
      $parent_section = '';
200
    }
201

    
202
	$link = $parent_section.$link;
203
    $filename = WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
204
}
205

    
206
// Check if a page with same page filename exists
207
// $database = new database();
208
$sql = 'SELECT `page_id`,`page_title` FROM `'.TABLE_PREFIX.'pages` WHERE `link` = "'.$link.'" AND `page_id` != '.$page_id;
209

    
210
$get_same_page = $database->query($sql);
211

    
212
if($get_same_page->numRows() > 0)
213
{
214
	$admin->print_error($MESSAGE['PAGES_PAGE_EXISTS'], $target_url);
215
}
216

    
217
// Update page with new order
218
$sql = 'UPDATE `'.TABLE_PREFIX.'pages` SET `parent`='.$parent.', `position`='.$position.' WHERE `page_id`='.$page_id.'';
219
// $database = new database();
220
$database->query($sql);
221

    
222
// Get page trail
223
$page_trail = get_page_trail($page_id);
224

    
225
// Update page settings in the pages table
226
$sql = 'UPDATE `'.TABLE_PREFIX.'pages` '
227
     . 'SET `parent`='.$parent.', '
228
     .     '`page_title`=\''.$page_title.'\', '
229
     .     '`tooltip`=\''.$page_title.'\', '
230
     .     '`page_icon` =\''.mysql_real_escape_string($sPageIcon).'\', '
231
     .     '`menu_title`=\''.$menu_title.'\', '
232
     .     '`menu_icon_0` =\''.mysql_real_escape_string($sMenuIcon0).'\', '
233
     .     '`menu_icon_1` =\''.mysql_real_escape_string($sMenuIcon1).'\', '
234
     .     '`menu`='.$menu.', '
235
     .     '`level`='.$level.', '
236
     .     '`page_trail`=\''.$page_trail.'\', '
237
     .     '`root_parent`='.$root_parent.', '
238
     .     '`link`=\''.$link.'\', '
239
     .     '`template`=\''.$template.'\', '
240
     .     '`target`=\''.$target.'\', '
241
     .     '`description`=\''.$description.'\', '
242
     .     '`keywords`=\''.$keywords.'\', '
243
     .     '`position`='.$position.', '
244
     .     '`visibility`=\''.$visibility.'\', '
245
     .     '`searching`='.$searching.', '
246
     .     '`language`=\''.$language.'\', ';
247
if($admin->ami_group_member('1')) {
248
	$sql .= ''
249
	     . '`admin_groups`=\''.$sAdminGroups.'\', '
250
	     . '`admin_users`=\''.$sAdminUsers.'\', ';
251
}
252
$sql .= ''
253
     .     '`viewing_groups`=\''.$sViewingGroups.'\', '
254
     .     '`viewing_users`=\''.$sViewingUsers.'\', '
255
     .     '`page_code`='.$page_code.' '
256
     . 'WHERE `page_id`='.$page_id;
257

    
258
if(!$database->query($sql)) {
259
	$target_url = ADMIN_URL.'/pages/settings.php?page_id='.$page_id;
260
	$admin->print_error($database->get_error(), $target_url );
261
}
262

    
263
// Clean old order if needed
264
if($parent != $old_parent)
265
{
266
	$order->clean($old_parent);
267
}
268

    
269
/* BEGIN page "access file" code */
270

    
271
// Create a new file in the /pages dir if title changed
272
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/'))
273
{
274
	$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'], $target_url);
275
} else {
276
    $old_filename = WB_PATH.PAGES_DIRECTORY.$old_link.PAGE_EXTENSION;
277

    
278
	// First check if we need to create a new file
279
	if(($old_link != $link) || (!file_exists($old_filename)))
280
    {
281
		// Delete old file
282
		$old_filename = WB_PATH.PAGES_DIRECTORY.$old_link.PAGE_EXTENSION;
283
		if(file_exists($old_filename))
284
        {
285
			unlink($old_filename);
286
		}
287

    
288
		// Create access file
289
		create_access_file($filename,$page_id,$level);
290
        if(!file_exists($filename)) {
291
        	$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
292
        }
293
		// Move a directory for this page
294
		if(file_exists(WB_PATH.PAGES_DIRECTORY.$old_link.'/') && is_dir(WB_PATH.PAGES_DIRECTORY.$old_link.'/'))
295
        {
296
			rename(WB_PATH.PAGES_DIRECTORY.$old_link.'/', WB_PATH.PAGES_DIRECTORY.$link.'/');
297
		}
298
		// Update any pages that had the old link with the new one
299
		$old_link_len = strlen($old_link);
300
        $sql = '';
301
		$query_subs = $database->query("SELECT page_id,link,level FROM ".TABLE_PREFIX."pages WHERE link LIKE '%$old_link/%' ORDER BY LEVEL ASC");
302

    
303
		if($query_subs->numRows() > 0)
304
        {
305
			while($sub = $query_subs->fetchRow())
306
            {
307
				// Double-check to see if it contains old link
308
				if(substr($sub['link'], 0, $old_link_len) == $old_link)
309
                {
310
					// Get new link
311
					$replace_this = $old_link;
312
					$old_sub_link_len =strlen($sub['link']);
313
					$new_sub_link = $link.'/'.substr($sub['link'],$old_link_len+1,$old_sub_link_len);
314
					// Work out level
315
					$new_sub_level = level_count($sub['page_id']);
316
					// Update level and link
317
					$database->query("UPDATE ".TABLE_PREFIX."pages SET link = '$new_sub_link', level = '$new_sub_level' WHERE page_id = '".$sub['page_id']."' LIMIT 1");
318
					// Re-write the access file for this page
319
					$old_subpage_file = WB_PATH.PAGES_DIRECTORY.$new_sub_link.PAGE_EXTENSION;
320
					if(file_exists($old_subpage_file))
321
                    {
322
						unlink($old_subpage_file);
323
					}
324
                    $sAccessFile = WB_PATH.PAGES_DIRECTORY.$new_sub_link.PAGE_EXTENSION;
325
					create_access_file($sAccessFile, $sub['page_id'], $new_sub_level);
326
                    if(!file_exists($sAccessFile)) {
327
                    	$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
328
                    }
329

    
330
				}
331
			}
332
		}
333
	}
334
}
335

    
336
// using standard function by core,
337
function fix_page_trail($page_id) {
338
    global $database,$admin,$target_url,$MESSAGE;
339
    // Work out level
340
    $level = level_count($page_id);
341
    // Work out root parent
342
    $root_parent = root_parent($page_id);
343
    // Work out page trail
344
    $page_trail = get_page_trail($page_id);
345
    // Update page with new level and link
346
    $sql  = 'UPDATE `'.TABLE_PREFIX.'pages` SET ';
347
    $sql .= '`root_parent` = '.$root_parent.', ';
348
    $sql .= '`level` = '.$level.', ';
349
    $sql .= '`page_trail` = "'.$page_trail.'" ';
350
    $sql .= 'WHERE `page_id` = '.$page_id;
351

    
352
    if($database->query($sql)) {
353
    	$admin->print_success($MESSAGE['PAGES_SAVED_SETTINGS'], $target_url );
354
    } else {
355
    	$admin->print_error($database->get_error(), $target_url );
356
    }
357
}
358

    
359
// Fix sub-pages page trail
360
fix_page_trail($page_id);
361

    
362
$admin->print_footer();
(21-21/22)