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 1825 2012-11-20 17:46:08Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/pages/settings_save.php $
14
 * @lastmodified    $Date: 2012-11-20 18:46:08 +0100 (Tue, 20 Nov 2012) $
15
 *
16
 */
17

    
18
// Create new admin object and print admin header
19
if(!defined('WB_URL'))
20
{
21
    $config_file = realpath('../../config.php');
22
    if(file_exists($config_file) && !defined('WB_URL'))
23
    {
24
    	require($config_file);
25
    }
26
}
27
if(!class_exists('admin', false)){ include(WB_PATH.'/framework/class.admin.php'); }
28

    
29
$lang_dir = dirname(__FILE__).'/languages/';
30
$lang = file_exists($lang_dir.LANGUAGE.'.php') ? LANGUAGE : 'EN';
31
require_once($lang_dir.$lang.'.php');
32

    
33
// suppress to print the header, so no new FTAN will be set
34
$admin = new admin('Pages', 'pages_settings',false);
35
$pagetree_url = ADMIN_URL.'/pages/index.php';
36

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

    
51
/*
52
if( (!($page_id = $admin->checkIDKEY('page_id', 0, $_SERVER['REQUEST_METHOD']))) )
53
{
54
	$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS']);
55
}
56
*/
57
$target_url = ADMIN_URL.'/pages/settings.php?page_id='.$page_id;
58

    
59
if (!$admin->checkFTAN())
60
{
61
	$admin->print_header();
62
	$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS'],$target_url);
63
}
64

    
65
// After check print the header
66
$admin->print_header();
67

    
68
if(isset($_POST['extended_submit'])) {
69
	$sql = 'SELECT `value` FROM `'.TABLE_PREFIX.'settings` WHERE `name` = \'page_exented\'';
70
	if( ($page_exented = $database->get_one($sql)) != '' ) {
71
		$sql = 'UPDATE ';
72
		$sql_where = 'WHERE `name` = \'page_exented\'';
73
	} else {
74
		$sql = 'INSERT INTO ';
75
		$sql_where = '';
76
	}
77
	$val = (((($page_exented=='1') ? $page_exented : 0)) + 1) % 2;
78

    
79
	$sql .= '`'.TABLE_PREFIX.'settings` ';
80
	$sql .= 'SET `name` = \'page_exented\', ';
81
	$sql .= '`value` = \''.$val.'\' '.$sql_where;
82

    
83
    if($database->query($sql)) {
84
        // redirect to backend
85
echo "<p style=\"text-align:center;\"> If the script</strong> could not be start automatically.\n" .
86
     "Please click <a style=\"font-weight:bold;\" " .
87
     "href=\"".$target_url."\">on this link</a> to start the script!</p>\n";
88
echo "<script type=\"text/javascript\">
89
<!--
90
// Get the location object
91
var locationObj = document.location;
92
// Set the value of the location object
93
document.location = '$target_url';
94
-->
95
</script>";
96

    
97
    } else {
98
    	$admin->print_error($database->get_error(), $target_url );
99
    }
100
}
101

    
102
// Include the WB functions file
103
if(!function_exists('directory_list')) { require(WB_PATH.'/framework/functions.php'); }
104

    
105
// Get values
106
$page_title = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->get_post_escaped('page_title')));
107
$menu_title = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->get_post_escaped('menu_title')));
108
$seo_title = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->get_post_escaped('seo_title')));
109
$page_code = intval($admin->get_post('page_code')) ;
110
$description = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->add_slashes($admin->get_post('description'))));
111
$keywords = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->add_slashes($admin->get_post('keywords'))));
112
$parent = intval($admin->get_post('parent')); // fix secunia 2010-91-3
113
$visibility = $admin->get_post_escaped('visibility');
114
if (!in_array($visibility, array('public', 'private', 'registered', 'hidden', 'none'))) {$visibility = 'public';} // fix secunia 2010-93-3
115
$template = preg_replace('/[^a-z0-9_-]/i', "", $admin->get_post('template')); // fix secunia 2010-93-3
116
$template = (($template == DEFAULT_TEMPLATE ) ? '' : $template);
117
$target = preg_replace("/\W/", "", $admin->get_post('target'));
118
$aAdminGroups   = (isset($_POST['admin_groups']) ? $_POST['admin_groups'] : array('1'));
119
$aAdminUsers    = (isset($_POST['admin_users']) ? $_POST['admin_users'] : array());
120
$aViewingGroups = (isset($_POST['viewing_groups']) ? $_POST['viewing_groups'] : array('1'));
121
$aViewingUsers  = (isset($_POST['viewing_users']) ? $_POST['viewing_users'] : array());
122
$searching = intval($admin->get_post('searching'));
123
$language = strtoupper($admin->get_post('language'));
124
$language = (preg_match('/^[A-Z]{2}$/', $language) ? $language : DEFAULT_LANGUAGE);
125
$menu = intval($admin->get_post('menu')); // fix secunia 2010-91-3
126
$menu = ($menu==0) ? 1 : $menu;
127
$page_code = (isset($_POST['page_code']) ? intval($_POST['page_code']) : 0);
128
$sPageIcon = (isset($_POST['page_icon']) ? $_POST['page_icon'] : 0);
129
$sMenuIcon0 = (isset($_POST['menu_icon_0']) ? $_POST['menu_icon_0'] : 0);
130
$sMenuIcon1 = (isset($_POST['menu_icon_1']) ? $_POST['menu_icon_1'] : 0);
131

    
132
// Validate data
133
if($page_title == '' || substr($page_title,0,1)=='.')
134
{
135
	$admin->print_error($MESSAGE['PAGES_BLANK_PAGE_TITLE'],$target_url);
136
}
137
if($menu_title == '' || substr($menu_title,0,1)=='.')
138
{
139
	$admin->print_error($MESSAGE['PAGES_BLANK_MENU_TITLE'],$target_url);
140
}
141
if($seo_title == '' || substr($seo_title,0,1)=='.')
142
{
143
    $seo_title = $menu_title;
144
}
145

    
146
// Get existing perms
147
$sql  = 'SELECT `parent`,`link`,`position`,`admin_groups`,`admin_users`,`menu_title` ';
148
$sql .= 'FROM `'.TABLE_PREFIX.'pages` WHERE `page_id`='.$page_id;
149

    
150
$results = $database->query($sql);
151

    
152
$results_array = $results->fetchRow(MYSQL_ASSOC);
153
$old_parent = $results_array['parent'];
154
$old_link = $results_array['link'];
155
$old_position = $results_array['position'];
156

    
157
if($admin->ami_group_member('1')) {
158
	if(!$admin->ami_group_member($results_array['admin_groups']) &&
159
	   !$admin->is_group_match($admin->get_user_id(), $results_array['admin_users']))
160
	{
161
		$admin->print_error($MESSAGE['PAGES_INSUFFICIENT_PERMISSIONS']);
162
	}
163
	// Setup admin groups
164
	$aAdminGroups = (is_array($aAdminGroups) ? $aAdminGroups : array(1));
165
	array_unshift($aAdminGroups, 1);
166
	$sAdminGroups = implode(',', array_unique($aAdminGroups));
167
	$sAdminGroups = (preg_match('/^,|[^0-9,]|,,|,$/', $sAdminGroups) ? '1' : $sAdminGroups);
168

    
169
	$aAdminUsers = (is_array($aAdminUsers) ? $aAdminUsers : array());
170
	$sAdminUsers = implode(',', array_diff($aAdminUsers, array(0)));
171
	$sAdminUsers = (preg_match('/^,|[^0-9,]|,,|,$/', $sAdminUsers) ? array() : $sAdminUsers);
172
}
173

    
174
$aViewingGroups = (is_array($aViewingGroups) ? $aViewingGroups : array(1));
175
array_unshift($aViewingGroups, 1);
176
$sViewingGroups = implode(',', array_unique($aViewingGroups));
177
$sViewingGroups = (preg_match('/^,|[^0-9,]|,,|,$/', $sViewingGroups) ? '1' : $sViewingGroups);
178

    
179
$aViewingUsers = (is_array($aViewingUsers) ? $aViewingUsers : array());
180
$sViewingUsers = implode(',', array_diff($aViewingUsers, array(0)));
181
$sViewingUsers = (preg_match('/^,|[^0-9,]|,,|,$/', $sViewingUsers) ? array() : $sViewingUsers);
182

    
183
$sPageIcon = (($sPageIcon == '0') ? '' : $sPageIcon);
184
if(!is_readable(WB_PATH.$sPageIcon)) { $sPageIcon = ''; }
185
$sMenuIcon0 = (($sMenuIcon0 == '0') ? '' : $sMenuIcon0);
186
if(!is_readable(WB_PATH.$sMenuIcon0)) { $sMenuIcon0 = ''; }
187
$sMenuIcon1 = (($sMenuIcon1 == '0') ? '' : $sMenuIcon1);
188
if(!is_readable(WB_PATH.$sMenuIcon1)) { $sMenuIcon1 = ''; }
189

    
190
// If needed, get new order
191
if($parent != $old_parent)
192
{
193
	// Include ordering class
194
	require(WB_PATH.'/framework/class.order.php');
195
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
196
	// Get new order
197
	$position = $order->get_new($parent);
198
	// Clean new order
199
	$order->clean($parent);
200
} else {
201
	$position = $old_position;
202
}
203

    
204
// Work out level and root parent
205
if ($parent!='0')
206
{
207
	$level = level_count($parent)+1;
208
	$root_parent = root_parent($parent);
209
} else {
210
// Work out level
211
    $level = level_count($page_id);
212
// Work out root parent
213
    $root_parent = root_parent($page_id);
214
}
215

    
216
$link = '/'.page_filename($seo_title);
217

    
218
// Work-out what the link should be
219
if($parent == '0') {
220
    if($link == '/index' || $link == '/intro')
221
    {
222
    	$link .= '_' .$page_id;
223
    	$filename = WB_PATH.PAGES_DIRECTORY.$link .PAGE_EXTENSION;
224

    
225
    } else {
226
        $filename = WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
227
    }
228
} else {
229
	$parent_section = '';
230
	$parent_titles = array_reverse(get_parent_titles($parent));
231

    
232
	foreach($parent_titles AS $parent_title)
233
    {
234
		$parent_section .= '/'.page_filename($parent_title);
235
	}
236

    
237
	if($parent_section == '/')
238
    {
239
      $parent_section = '';
240
    }
241

    
242
	$link = $parent_section.$link;
243
    $filename = WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
244
}
245

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

    
250
$get_same_page = $database->query($sql);
251

    
252
if($get_same_page->numRows() > 0)
253
{
254
	$admin->print_error($MESSAGE['PAGES_PAGE_EXISTS'], $target_url);
255
}
256

    
257
// Update page with new order
258
$sql = 'UPDATE `'.TABLE_PREFIX.'pages` SET `parent`='.$parent.', `position`='.$position.' WHERE `page_id`='.$page_id.'';
259
// $database = new database();
260
$database->query($sql);
261

    
262
// Get page trail
263
$page_trail = get_page_trail($page_id);
264

    
265
// Update page settings in the pages table
266
$sql = 'UPDATE `'.TABLE_PREFIX.'pages` '
267
     . 'SET `parent`='.$parent.', '
268
     .     '`page_title`=\''.$page_title.'\', '
269
     .     '`tooltip`=\''.$page_title.'\', '
270
     .     '`page_icon` =\''.mysql_real_escape_string($sPageIcon).'\', '
271
     .     '`menu_title`=\''.$menu_title.'\', '
272
     .     '`menu_icon_0` =\''.mysql_real_escape_string($sMenuIcon0).'\', '
273
     .     '`menu_icon_1` =\''.mysql_real_escape_string($sMenuIcon1).'\', '
274
     .     '`menu`='.$menu.', '
275
     .     '`level`='.$level.', '
276
     .     '`page_trail`=\''.$page_trail.'\', '
277
     .     '`root_parent`='.$root_parent.', '
278
     .     '`link`=\''.$link.'\', '
279
     .     '`template`=\''.$template.'\', '
280
     .     '`target`=\''.$target.'\', '
281
     .     '`description`=\''.$description.'\', '
282
     .     '`keywords`=\''.$keywords.'\', '
283
     .     '`position`='.$position.', '
284
     .     '`visibility`=\''.$visibility.'\', '
285
     .     '`searching`='.$searching.', '
286
     .     '`language`=\''.$language.'\', ';
287
if($admin->ami_group_member('1')) {
288
	$sql .= ''
289
	     . '`admin_groups`=\''.$sAdminGroups.'\', '
290
	     . '`admin_users`=\''.$sAdminUsers.'\', ';
291
}
292
    $sql .= ''
293
         . '`viewing_groups`=\''.$sViewingGroups.'\', '
294
         . '`viewing_users`=\''.$sViewingUsers.'\', '
295
         . '`page_code`='.$page_code.' '
296
         . 'WHERE `page_id`='.$page_id;
297

    
298
if(!$database->query($sql)) {
299
	$target_url = ADMIN_URL.'/pages/settings.php?page_id='.$page_id;
300
	$admin->print_error($database->get_error(), $target_url );
301
}
302

    
303
// Clean old order if needed
304
if($parent != $old_parent)
305
{
306
	$order->clean($old_parent);
307
}
308

    
309
/* BEGIN page "access file" code */
310

    
311
// Create a new file in the /pages dir if title changed
312
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/'))
313
{
314
	$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'], $target_url);
315
} else {
316
    $old_filename = WB_PATH.PAGES_DIRECTORY.$old_link.PAGE_EXTENSION;
317

    
318
	// First check if we need to create a new file
319
	if(($old_link != $link) || (!file_exists($old_filename)))
320
    {
321
		// Delete old file
322
		$old_filename = WB_PATH.PAGES_DIRECTORY.$old_link.PAGE_EXTENSION;
323
		if(file_exists($old_filename))
324
        {
325
			unlink($old_filename);
326
		}
327

    
328
		// Create access file
329
		create_access_file($filename,$page_id,$level);
330
        if(!file_exists($filename)) {
331
        	$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
332
        }
333
		// Move a directory for this page
334
		if(file_exists(WB_PATH.PAGES_DIRECTORY.$old_link.'/') && is_dir(WB_PATH.PAGES_DIRECTORY.$old_link.'/'))
335
        {
336
			rename(WB_PATH.PAGES_DIRECTORY.$old_link.'/', WB_PATH.PAGES_DIRECTORY.$link.'/');
337
		}
338
		// Update any pages that had the old link with the new one
339
		$old_link_len = strlen($old_link);
340
        $sql = '';
341
		$query_subs = $database->query("SELECT page_id,link,level FROM ".TABLE_PREFIX."pages WHERE link LIKE '%$old_link/%' ORDER BY LEVEL ASC");
342

    
343
		if($query_subs->numRows() > 0)
344
        {
345
			while($sub = $query_subs->fetchRow())
346
            {
347
				// Double-check to see if it contains old link
348
				if(substr($sub['link'], 0, $old_link_len) == $old_link)
349
                {
350
					// Get new link
351
					$replace_this = $old_link;
352
					$old_sub_link_len =strlen($sub['link']);
353
					$new_sub_link = $link.'/'.substr($sub['link'],$old_link_len+1,$old_sub_link_len);
354
					// Work out level
355
					$new_sub_level = level_count($sub['page_id']);
356
					// Update level and link
357
					$database->query("UPDATE ".TABLE_PREFIX."pages SET link = '$new_sub_link', level = '$new_sub_level' WHERE page_id = '".$sub['page_id']."' LIMIT 1");
358
					// Re-write the access file for this page
359
					$old_subpage_file = WB_PATH.PAGES_DIRECTORY.$new_sub_link.PAGE_EXTENSION;
360
					if(file_exists($old_subpage_file))
361
                    {
362
						unlink($old_subpage_file);
363
					}
364
                    $sAccessFile = WB_PATH.PAGES_DIRECTORY.$new_sub_link.PAGE_EXTENSION;
365
					create_access_file($sAccessFile, $sub['page_id'], $new_sub_level);
366
                    if(!file_exists($sAccessFile)) {
367
                    	$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
368
                    }
369

    
370
				}
371
			}
372
		}
373
	}
374
}
375

    
376
// using standard function by core,
377
function fix_page_trail($page_id) {
378
    global $database,$admin,$target_url,$pagetree_url,$MESSAGE;
379

    
380
    $target_url = (isset($_POST['back_submit'])) ? $pagetree_url : $target_url;
381

    
382
    // Work out level
383
    $level = level_count($page_id);
384
    // Work out root parent
385
    $root_parent = root_parent($page_id);
386
    // Work out page trail
387
    $page_trail = get_page_trail($page_id);
388
    // Update page with new level and link
389
    $sql  = 'UPDATE `'.TABLE_PREFIX.'pages` SET ';
390
    $sql .= '`root_parent` = '.$root_parent.', ';
391
    $sql .= '`level` = '.$level.', ';
392
    $sql .= '`page_trail` = "'.$page_trail.'" ';
393
    $sql .= 'WHERE `page_id` = '.$page_id;
394

    
395
    if($database->query($sql)) {
396
    	$admin->print_success($MESSAGE['PAGES_SAVED_SETTINGS'], $target_url );
397
    } else {
398
    	$admin->print_error($database->get_error(), $target_url );
399
    }
400
}
401

    
402
// Fix sub-pages page trail
403
fix_page_trail($page_id);
404

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