Project

General

Profile

1
<?php
2

    
3
// $Id: add.php 733 2008-03-01 18:24:11Z thorn $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, Ryan Djurovich
9

    
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
$title = $admin->get_post_escaped('title');
36
$title = htmlspecialchars($title);
37
$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
// work-around: $viewing_groups contains group-numbers for both private _and_ registered - keep group-numbers which appears twice only
44
$view_groups=array(); 
45
foreach($viewing_groups as $a) {
46
	if(isset($view_groups[$a])) $view_groups[$a]++;
47
	else $view_groups[$a] = 1;
48
}
49
$viewing_groups=array();
50
foreach($view_groups as $k=>$v) {
51
	if($v==2)
52
		$viewing_groups[]=$k;
53
}
54

    
55
if ($parent!=0) {
56
	if (!$admin->get_page_permission($parent,'admin'))
57
		$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
58
} elseif (!$admin->get_permission('pages_add_l0','system')) {
59
	$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
60
}	
61

    
62
// Validate data
63
if($title == '' || substr($title,0,1)=='.') {
64
	$admin->print_error($MESSAGE['PAGES']['BLANK_PAGE_TITLE']);
65
}
66

    
67
// Setup admin groups
68
$admin_groups[] = 1;
69
//if(!in_array(1, $admin->get_groups_id())) {
70
//	$admin_groups[] = implode(",",$admin->get_groups_id());
71
//}
72
// Setup viewing groups
73
$viewing_groups[] = 1;
74
//if(!in_array(1, $admin->get_groups_id())) {
75
//	$viewing_groups[] = implode(",",$admin->get_groups_id());
76
//}
77

    
78
// Check to see if page created has needed permissions
79
if(!in_array(1, $admin->get_groups_id())) {
80
	$admin_perm_ok = false;
81
	foreach ($admin_groups as $adm_group) {
82
		if (in_array($adm_group, $admin->get_groups_id())) {
83
			$admin_perm_ok = true;
84
		}
85
	}
86
	if ($admin_perm_ok == false) {
87
		$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
88
	}
89
	$admin_perm_ok = false;
90
	foreach ($viewing_groups as $view_group) {
91
		if (in_array($view_group, $admin->get_groups_id())) {
92
			$admin_perm_ok = true;
93
		}
94
	}
95
	if ($admin_perm_ok == false) {
96
		$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
97
	}
98
}
99

    
100
$admin_groups = implode(',', $admin_groups);
101
$viewing_groups = implode(',', $viewing_groups);
102

    
103
// Work-out what the link and page filename should be
104
if($parent == '0') {
105
	$link = '/'.page_filename($title);
106
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($title).PAGE_EXTENSION;
107
} else {
108
	$parent_section = '';
109
	$parent_titles = array_reverse(get_parent_titles($parent));
110
	foreach($parent_titles AS $parent_title) {
111
		$parent_section .= page_filename($parent_title).'/';
112
	}
113
	if($parent_section == '/') { $parent_section = ''; }
114
	$link = '/'.$parent_section.page_filename($title);
115
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.$parent_section.page_filename($title).PAGE_EXTENSION;
116
	make_dir(WB_PATH.PAGES_DIRECTORY.'/'.$parent_section);
117
}
118

    
119
// Check if a page with same page filename exists
120
$get_same_page = $database->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE link = '$link'");
121
if($get_same_page->numRows() > 0 OR file_exists(WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION) OR file_exists(WB_PATH.PAGES_DIRECTORY.$link.'/')) {
122
	$admin->print_error($MESSAGE['PAGES']['PAGE_EXISTS']);
123
}
124

    
125
// Include the ordering class
126
require(WB_PATH.'/framework/class.order.php');
127
$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
128
// First clean order
129
$order->clean($parent);
130
// Get new order
131
$position = $order->get_new($parent);
132

    
133
// Work-out if the page parent (if selected) has a seperate template to the default
134
$query_parent = $database->query("SELECT template FROM ".TABLE_PREFIX."pages WHERE page_id = '$parent'");
135
if($query_parent->numRows() > 0) {
136
	$fetch_parent = $query_parent->fetchRow();
137
	$template = $fetch_parent['template'];
138
} else {
139
	$template = '';
140
}
141

    
142
// Insert page into pages table
143
$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()."')";
144
$database->query($query);
145
if($database->is_error()) {
146
	$admin->print_error($database->get_error());
147
}
148

    
149
// Get the page id
150
$page_id = $database->get_one("SELECT LAST_INSERT_ID()");
151

    
152
// Work out level
153
$level = level_count($page_id);
154
// Work out root parent
155
$root_parent = root_parent($page_id);
156
// Work out page trail
157
$page_trail = get_page_trail($page_id);
158

    
159
// Update page with new level and link
160
$database->query("UPDATE ".TABLE_PREFIX."pages SET link = '$link', level = '$level', root_parent = '$root_parent', page_trail = '$page_trail' WHERE page_id = '$page_id'");
161

    
162
// Create a new file in the /pages dir
163
create_access_file($filename, $page_id, $level);
164

    
165
// Get new order for section
166
$order = new order(TABLE_PREFIX.'sections', 'position', 'section_id', 'page_id');
167
$position = $order->get_new($parent);
168

    
169
// Add new record into the sections table
170
$database->query("INSERT INTO ".TABLE_PREFIX."sections (page_id,position,module,block) VALUES ('$page_id','$position', '$module','1')");
171

    
172
// Get the section id
173
$section_id = $database->get_one("SELECT LAST_INSERT_ID()");
174

    
175
// Include the selected modules add file if it exists
176
if(file_exists(WB_PATH.'/modules/'.$module.'/add.php')) {
177
	require(WB_PATH.'/modules/'.$module.'/add.php');
178
}
179

    
180
// Check if there is a db error, otherwise say successful
181
if($database->is_error()) {
182
	$admin->print_error($database->get_error());
183
} else {
184
	$admin->print_success($MESSAGE['PAGES']['ADDED'], ADMIN_URL.'/pages/modify.php?page_id='.$page_id);
185
}
186

    
187
// Print admin footer
188
$admin->print_footer();
189

    
190
?>
(1-1/19)