Project

General

Profile

1 4 ryan
<?php
2
3 40 stefan
// $Id$
4 4 ryan
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8 519 Ruebenwurz
 Copyright (C) 2004-2008, Ryan Djurovich
9 4 ryan
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24
*/
25
26
// Create new admin object and print admin header
27
require('../../config.php');
28
require_once(WB_PATH.'/framework/class.admin.php');
29
$admin = new admin('Pages', 'pages_add');
30
31
// Include the WB functions file
32
require_once(WB_PATH.'/framework/functions.php');
33
34
// Get values
35 488 Ruebenwurz
$title = $admin->get_post_escaped('title');
36 657 thorn
$title = htmlspecialchars($title);
37 4 ryan
$module = $admin->get_post('type');
38
$parent = $admin->get_post('parent');
39
$visibility = $admin->get_post('visibility');
40
$admin_groups = $admin->get_post('admin_groups');
41
$viewing_groups = $admin->get_post('viewing_groups');
42
43 733 thorn
// work-around: $viewing_groups contains group-numbers for both private _and_ registered - keep group-numbers which appears twice only
44 738 thorn
$view_groups=array();
45
if($viewing_groups===NULL)
46
	$viewing_groups=array();
47 733 thorn
foreach($viewing_groups as $a) {
48
	if(isset($view_groups[$a])) $view_groups[$a]++;
49
	else $view_groups[$a] = 1;
50
}
51
$viewing_groups=array();
52
foreach($view_groups as $k=>$v) {
53
	if($v==2)
54
		$viewing_groups[]=$k;
55
}
56
57 319 stefan
if ($parent!=0) {
58
	if (!$admin->get_page_permission($parent,'admin'))
59
		$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
60
} elseif (!$admin->get_permission('pages_add_l0','system')) {
61
	$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
62
}
63
64 4 ryan
// Validate data
65 428 Ruebenwurz
if($title == '' || substr($title,0,1)=='.') {
66 392 Ruebenwurz
	$admin->print_error($MESSAGE['PAGES']['BLANK_PAGE_TITLE']);
67 4 ryan
}
68
69
// Setup admin groups
70
$admin_groups[] = 1;
71 733 thorn
//if(!in_array(1, $admin->get_groups_id())) {
72
//	$admin_groups[] = implode(",",$admin->get_groups_id());
73
//}
74 4 ryan
// Setup viewing groups
75
$viewing_groups[] = 1;
76 733 thorn
//if(!in_array(1, $admin->get_groups_id())) {
77
//	$viewing_groups[] = implode(",",$admin->get_groups_id());
78
//}
79
80
// Check to see if page created has needed permissions
81 546 doc
if(!in_array(1, $admin->get_groups_id())) {
82 733 thorn
	$admin_perm_ok = false;
83
	foreach ($admin_groups as $adm_group) {
84
		if (in_array($adm_group, $admin->get_groups_id())) {
85
			$admin_perm_ok = true;
86
		}
87
	}
88
	if ($admin_perm_ok == false) {
89
		$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
90
	}
91
	$admin_perm_ok = false;
92
	foreach ($viewing_groups as $view_group) {
93
		if (in_array($view_group, $admin->get_groups_id())) {
94
			$admin_perm_ok = true;
95
		}
96
	}
97
	if ($admin_perm_ok == false) {
98
		$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
99
	}
100 4 ryan
}
101 733 thorn
102
$admin_groups = implode(',', $admin_groups);
103 4 ryan
$viewing_groups = implode(',', $viewing_groups);
104
105
// Work-out what the link and page filename should be
106
if($parent == '0') {
107
	$link = '/'.page_filename($title);
108 555 Ruebenwurz
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($title).PAGE_EXTENSION;
109 4 ryan
} else {
110
	$parent_section = '';
111
	$parent_titles = array_reverse(get_parent_titles($parent));
112
	foreach($parent_titles AS $parent_title) {
113
		$parent_section .= page_filename($parent_title).'/';
114
	}
115
	if($parent_section == '/') { $parent_section = ''; }
116
	$link = '/'.$parent_section.page_filename($title);
117 555 Ruebenwurz
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.$parent_section.page_filename($title).PAGE_EXTENSION;
118 4 ryan
	make_dir(WB_PATH.PAGES_DIRECTORY.'/'.$parent_section);
119
}
120
121
// Check if a page with same page filename exists
122
$get_same_page = $database->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE link = '$link'");
123 555 Ruebenwurz
if($get_same_page->numRows() > 0 OR file_exists(WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION) OR file_exists(WB_PATH.PAGES_DIRECTORY.$link.'/')) {
124 46 stefan
	$admin->print_error($MESSAGE['PAGES']['PAGE_EXISTS']);
125 4 ryan
}
126
127
// Include the ordering class
128
require(WB_PATH.'/framework/class.order.php');
129
$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
130
// First clean order
131
$order->clean($parent);
132
// Get new order
133
$position = $order->get_new($parent);
134
135
// Work-out if the page parent (if selected) has a seperate template to the default
136
$query_parent = $database->query("SELECT template FROM ".TABLE_PREFIX."pages WHERE page_id = '$parent'");
137
if($query_parent->numRows() > 0) {
138
	$fetch_parent = $query_parent->fetchRow();
139
	$template = $fetch_parent['template'];
140
} else {
141
	$template = '';
142
}
143
144
// Insert page into pages table
145
$query = "INSERT INTO ".TABLE_PREFIX."pages (page_title,menu_title,parent,template,target,position,visibility,searching,menu,language,admin_groups,viewing_groups,modified_when,modified_by) VALUES ('$title','$title','$parent','$template','_top','$position','$visibility','1','1','".DEFAULT_LANGUAGE."','$admin_groups','$viewing_groups','".mktime()."','".$admin->get_user_id()."')";
146
$database->query($query);
147
if($database->is_error()) {
148
	$admin->print_error($database->get_error());
149
}
150
151
// Get the page id
152
$page_id = $database->get_one("SELECT LAST_INSERT_ID()");
153
154
// Work out level
155
$level = level_count($page_id);
156
// Work out root parent
157
$root_parent = root_parent($page_id);
158
// Work out page trail
159
$page_trail = get_page_trail($page_id);
160
161
// Update page with new level and link
162
$database->query("UPDATE ".TABLE_PREFIX."pages SET link = '$link', level = '$level', root_parent = '$root_parent', page_trail = '$page_trail' WHERE page_id = '$page_id'");
163
164
// Create a new file in the /pages dir
165
create_access_file($filename, $page_id, $level);
166
167
// Get new order for section
168
$order = new order(TABLE_PREFIX.'sections', 'position', 'section_id', 'page_id');
169
$position = $order->get_new($parent);
170
171
// Add new record into the sections table
172
$database->query("INSERT INTO ".TABLE_PREFIX."sections (page_id,position,module,block) VALUES ('$page_id','$position', '$module','1')");
173
174
// Get the section id
175
$section_id = $database->get_one("SELECT LAST_INSERT_ID()");
176
177
// Include the selected modules add file if it exists
178
if(file_exists(WB_PATH.'/modules/'.$module.'/add.php')) {
179
	require(WB_PATH.'/modules/'.$module.'/add.php');
180
}
181
182
// Check if there is a db error, otherwise say successful
183
if($database->is_error()) {
184
	$admin->print_error($database->get_error());
185
} else {
186
	$admin->print_success($MESSAGE['PAGES']['ADDED'], ADMIN_URL.'/pages/modify.php?page_id='.$page_id);
187
}
188
189
// Print admin footer
190
$admin->print_footer();
191
192 239 stefan
?>