Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        admin
5
 * @package         pages
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, Website Baker Org. e.V.
9
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version         $Id: settings2.php 1357 2010-12-27 19:50:23Z FrankH $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/pages/settings2.php $
15
 * @lastmodified    $Date: 2010-12-27 20:50:23 +0100 (Mon, 27 Dec 2010) $
16
 *
17
 */
18

    
19
// Get page id
20
if(!isset($_POST['page_id']) OR !is_numeric($_POST['page_id']))
21
{
22
	header("Location: index.php");
23
	exit(0);
24
} else {
25
	$page_id = $_POST['page_id'];
26
}
27

    
28
// Create new admin object and print admin header
29
require('../../config.php');
30
require_once(WB_PATH.'/framework/class.admin.php');
31
$admin = new admin('Pages', 'pages_settings');
32

    
33
if (!$admin->checkFTAN())
34
{
35
	$admin->print_error($MESSAGE['PAGES_NOT_SAVED'],'index.php');
36
	exit();
37
}
38

    
39
// Include the WB functions file
40
require_once(WB_PATH.'/framework/functions.php');
41

    
42
// Get values
43
$page_title = htmlspecialchars($admin->get_post_escaped('page_title') );
44
$menu_title = htmlspecialchars($admin->get_post_escaped('menu_title') );
45
$page_code = (int) $admin->get_post_escaped('page_code');
46
$description = htmlspecialchars($admin->add_slashes($admin->get_post('description')) );
47
$keywords = htmlspecialchars($admin->add_slashes($admin->get_post('keywords')) );
48
$parent = (int) $admin->get_post_escaped('parent'); // fix secunia 2010-91-3
49
$visibility = $admin->get_post_escaped('visibility');
50
if (!in_array($visibility, array('public', 'private', 'registered', 'hidden', 'none'))) $visibility = 'public'; // fix
51
$template = preg_replace("/\W/", "", $admin->get_post_escaped('template')); // fix secunia 2010-93-3
52
$target = preg_replace("/\W/", "", $admin->get_post_escaped('target'));
53
$admin_groups = $admin->get_post_escaped('admin_groups');
54
$viewing_groups = $admin->get_post_escaped('viewing_groups');
55
$searching = $admin->get_post_escaped('searching');
56
$language = strtoupper($admin->get_post('language'));
57
$language = (preg_match('/^[A-Z]{2}$/', $language) ? $language : DEFAULT_LANGUAGE);
58
$menu = (int) $admin->get_post_escaped('menu'); // fix secunia 2010-91-3
59

    
60
// Validate data
61
if($page_title == '' || substr($page_title,0,1)=='.')
62
{
63
	$admin->print_error($MESSAGE['PAGES']['BLANK_PAGE_TITLE']);
64
}
65
if($menu_title == '' || substr($menu_title,0,1)=='.')
66
{
67
	$admin->print_error($MESSAGE['PAGES']['BLANK_MENU_TITLE']);
68
}
69

    
70
// Get existing perms
71
// $database = new database();
72

    
73
$sql = 'SELECT `parent`,`link`,`position`,`admin_groups`,`admin_users` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id`='.$page_id;
74
$results = $database->query($sql);
75

    
76
$results_array = $results->fetchRow();
77
$old_parent = $results_array['parent'];
78
$old_link = $results_array['link'];
79
$old_position = $results_array['position'];
80
$old_admin_groups = explode(',', str_replace('_', '', $results_array['admin_groups']));
81
$old_admin_users = explode(',', str_replace('_', '', $results_array['admin_users']));
82

    
83
// Work-out if we should check for existing page_code
84
$sql = 'DESCRIBE `'.TABLE_PREFIX.'pages` `page_code`';
85
$field_sql = $database->query($sql);
86
$field_set = $field_sql->numRows();
87

    
88
$in_old_group = FALSE;
89
foreach($admin->get_groups_id() as $cur_gid)
90
{
91
    if (in_array($cur_gid, $old_admin_groups))
92
    {
93
	$in_old_group = TRUE;
94
    }
95
}
96
if((!$in_old_group) AND !is_numeric(array_search($admin->get_user_id(), $old_admin_users)))
97
{
98
	$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
99
}
100

    
101
// Setup admin groups
102
$admin_groups[] = 1;
103
//if(!in_array(1, $admin->get_groups_id())) {
104
//	$admin_groups[] = implode(",",$admin->get_groups_id());
105
//}
106
$admin_groups = preg_replace("/[^\d,]/", "", implode(',', $admin_groups));
107
// Setup viewing groups
108
$viewing_groups[] = 1;
109
//if(!in_array(1, $admin->get_groups_id())) {
110
//	$viewing_groups[] = implode(",",$admin->get_groups_id());
111
//}
112
$viewing_groups = preg_replace("/[^\d,]/", "", implode(',', $viewing_groups));
113

    
114
// If needed, get new order
115
if($parent != $old_parent)
116
{
117
	// Include ordering class
118
	require(WB_PATH.'/framework/class.order.php');
119
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
120
	// Get new order
121
	$position = $order->get_new($parent);
122
	// Clean new order
123
	$order->clean($parent);
124
} else {
125
	$position = $old_position;
126
}
127

    
128
// Work out level and root parent
129
if ($parent!='0')
130
{
131
	$level = level_count($parent)+1;
132
	$root_parent = root_parent($parent);
133
}
134
else {
135
	$level = '0';
136
	$root_parent = '0';
137
}
138

    
139
// Work-out what the link should be
140
if($parent == '0')
141
{
142
	$link = '/'.page_filename($menu_title);
143
	// rename menu titles: index && intro to prevent clashes with intro page feature and WB core file /pages/index.php
144
	if($link == '/index' || $link == '/intro')
145
    {
146
		$link .= '_' .$page_id;
147
		$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($menu_title).'_'.$page_id .PAGE_EXTENSION;
148
	} else {
149
		$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($menu_title).PAGE_EXTENSION;
150
	}
151
} else {
152
	$parent_section = '';
153
	$parent_titles = array_reverse(get_parent_titles($parent));
154
	foreach($parent_titles AS $parent_title)
155
    {
156
		$parent_section .= page_filename($parent_title).'/';
157
	}
158
	if($parent_section == '/')
159
    {
160
      $parent_section = '';
161
    }
162
	$link = '/'.$parent_section.page_filename($menu_title);
163
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.$parent_section.page_filename($menu_title).PAGE_EXTENSION;
164
}
165

    
166
// Check if a page with same page filename exists
167
// $database = new database();
168
$sql = 'SELECT `page_id`,`page_title` FROM `'.TABLE_PREFIX.'pages` WHERE `link` = "'.$link.'" AND `page_id` != '.$page_id;
169
$get_same_page = $database->query($sql);
170

    
171
if($get_same_page->numRows() > 0)
172
{
173
	$admin->print_error($MESSAGE['PAGES']['PAGE_EXISTS']);
174
}
175

    
176
// Update page with new order
177
$sql = 'UPDATE `'.TABLE_PREFIX.'pages` SET `parent`='.$parent.', `position`='.$position.' WHERE `page_id`='.$page_id.'';
178
// $database = new database();
179
$database->query($sql);
180

    
181
// Get page trail
182
$page_trail = get_page_trail($page_id);
183

    
184
// Update page settings in the pages table
185
$sql  = 'UPDATE `'.TABLE_PREFIX.'pages` SET ';
186
$sql .= '`parent` = '.$parent.', ';
187
$sql .= '`page_title` = "'.$page_title.'", ';
188
$sql .= '`menu_title` = "'.$menu_title.'", ';
189
$sql .= '`menu` = '.$menu.', ';
190
$sql .= '`level` = '.$level.', ';
191
$sql .= '`page_trail` = "'.$page_trail.'", ';
192
$sql .= '`root_parent` = '.$root_parent.', ';
193
$sql .= '`link` = "'.$link.'", ';
194
$sql .= '`template` = "'.$template.'", ';
195
$sql .= '`target` = "'.$target.'", ';
196
$sql .= '`description` = "'.$description.'", ';
197
$sql .= '`keywords` = "'.$keywords.'", ';
198
$sql .= '`position` = '.$position.', ';
199
$sql .= '`visibility` = "'.$visibility.'", ';
200
$sql .= '`searching` = '.$searching.', ';
201
$sql .= '`language` = "'.$language.'", ';
202
$sql .= '`admin_groups` = "'.$admin_groups.'", ';
203
$sql .= '`viewing_groups` = "'.$viewing_groups.'"';
204
$sql .= (defined('PAGE_LANGUAGES') && PAGE_LANGUAGES) && $field_set && (file_exists(WB_PATH.'/modules/mod_multilingual/update_keys.php')) ? ', `page_code` = '.$page_code.' ' : ' ';
205
$sql .= 'WHERE `page_id` = '.$page_id;
206
$database->query($sql);
207

    
208
$ftan2 = $admin->getFTAN(2);
209
$target_url = ADMIN_URL."/pages/settings.php?page_id=$page_id&amp;$ftan2";
210
if($database->is_error())
211
{
212
	$admin->print_error($database->get_error(), $target_url );
213
}
214
// Clean old order if needed
215
if($parent != $old_parent)
216
{
217
	$order->clean($old_parent);
218
}
219

    
220
/* BEGIN page "access file" code */
221

    
222
// Create a new file in the /pages dir if title changed
223
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/'))
224
{
225
	$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
226
} else {
227
    $old_filename = WB_PATH.PAGES_DIRECTORY.$old_link.PAGE_EXTENSION;
228
	// First check if we need to create a new file
229
	if(($old_link != $link) || (!file_exists($old_filename)))
230
    {
231
		// Delete old file
232
		$old_filename = WB_PATH.PAGES_DIRECTORY.$old_link.PAGE_EXTENSION;
233
		if(file_exists($old_filename))
234
        {
235
			unlink($old_filename);
236
		}
237
		// Create access file
238
		create_access_file($filename,$page_id,$level);
239
		// Move a directory for this page
240
		if(file_exists(WB_PATH.PAGES_DIRECTORY.$old_link.'/') AND is_dir(WB_PATH.PAGES_DIRECTORY.$old_link.'/'))
241
        {
242
			rename(WB_PATH.PAGES_DIRECTORY.$old_link.'/', WB_PATH.PAGES_DIRECTORY.$link.'/');
243
		}
244
		// Update any pages that had the old link with the new one
245
		$old_link_len = strlen($old_link);
246
        $sql = '';
247
		$query_subs = $database->query("SELECT page_id,link,level FROM ".TABLE_PREFIX."pages WHERE link LIKE '%$old_link/%' ORDER BY LEVEL ASC");
248

    
249
		if($query_subs->numRows() > 0)
250
        {
251
			while($sub = $query_subs->fetchRow())
252
            {
253
				// Double-check to see if it contains old link
254
				if(substr($sub['link'], 0, $old_link_len) == $old_link)
255
                {
256
					// Get new link
257
					$replace_this = $old_link;
258
					$old_sub_link_len =strlen($sub['link']);
259
					$new_sub_link = $link.'/'.substr($sub['link'],$old_link_len+1,$old_sub_link_len);
260
					// Work out level
261
					$new_sub_level = level_count($sub['page_id']);
262
					// Update level and link
263
					$database->query("UPDATE ".TABLE_PREFIX."pages SET link = '$new_sub_link', level = '$new_sub_level' WHERE page_id = '".$sub['page_id']."' LIMIT 1");
264
					// Re-write the access file for this page
265
					$old_subpage_file = WB_PATH.PAGES_DIRECTORY.$new_sub_link.PAGE_EXTENSION;
266
					if(file_exists($old_subpage_file))
267
                    {
268
						unlink($old_subpage_file);
269
					}
270
					create_access_file(WB_PATH.PAGES_DIRECTORY.$new_sub_link.PAGE_EXTENSION, $sub['page_id'], $new_sub_level);
271
				}
272
			}
273
		}
274
	}
275
}
276

    
277
// Function to fix page trail of subs
278
function fix_page_trail($parent,$root_parent)
279
{
280
	// Get objects and vars from outside this function
281
	global $admin, $template, $database, $TEXT, $MESSAGE;
282
	// Get page list from database
283
	// $database = new database();
284
	$query = "SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'";
285
	$get_pages = $database->query($query);
286
	// Insert values into main page list
287
	if($get_pages->numRows() > 0)
288
    {
289
		while($page = $get_pages->fetchRow())
290
        {
291
			// Fix page trail
292

    
293
			$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']."'");
294
			// Run this query on subs
295
			fix_page_trail($page['page_id'],$root_parent);
296
		}
297
	}
298
}
299

    
300
// Fix sub-pages page trail
301
fix_page_trail($page_id,$root_parent);
302

    
303
/* END page "access file" code */
304

    
305
$pagetree_url = ADMIN_URL.'/pages/index.php';
306
$ftan2 = $admin->getFTAN(2);
307
$target_url = ADMIN_URL."/pages/settings.php?page_id=$page_id&amp;$ftan2";
308
// Check if there is a db error, otherwise say successful
309
if($database->is_error())
310
{
311
	$admin->print_error($database->get_error(), $target_url );
312
} else {
313
	$admin->print_success($MESSAGE['PAGES']['SAVED_SETTINGS'], $target_url );
314
}
315

    
316
// Print admin footer
317
$admin->print_footer();
318

    
319
?>
(20-20/21)