Project

General

Profile

1
<?php
2

    
3
// $Id: add.php 915 2009-01-21 19:27:01Z Ruebenwurzel $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, 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
// add Admin to admin and viewing-groups
44
$admin_groups[] = 1;
45
$viewing_groups[] = 1;
46

    
47
if ($parent!=0) {
48
	if (!$admin->get_page_permission($parent,'admin'))
49
		$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
50
} elseif (!$admin->get_permission('pages_add_l0','system')) {
51
	$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
52
}	
53

    
54
// Validate data
55
if($title == '' || substr($title,0,1)=='.') {
56
	$admin->print_error($MESSAGE['PAGES']['BLANK_PAGE_TITLE']);
57
}
58

    
59
// Check to see if page created has needed permissions
60
if(!in_array(1, $admin->get_groups_id())) {
61
	$admin_perm_ok = false;
62
	foreach ($admin_groups as $adm_group) {
63
		if (in_array($adm_group, $admin->get_groups_id())) {
64
			$admin_perm_ok = true;
65
		}
66
	}
67
	if ($admin_perm_ok == false) {
68
		$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
69
	}
70
	$admin_perm_ok = false;
71
	foreach ($viewing_groups as $view_group) {
72
		if (in_array($view_group, $admin->get_groups_id())) {
73
			$admin_perm_ok = true;
74
		}
75
	}
76
	if ($admin_perm_ok == false) {
77
		$admin->print_error($MESSAGE['PAGES']['INSUFFICIENT_PERMISSIONS']);
78
	}
79
}
80

    
81
$admin_groups = implode(',', $admin_groups);
82
$viewing_groups = implode(',', $viewing_groups);
83

    
84
// Work-out what the link and page filename should be
85
if($parent == '0') {
86
	$link = '/'.page_filename($title);
87
	// rename menu titles: index && intro to prevent clashes with intro page feature and WB core file /pages/index.php
88
	if($link == '/index' || $link == '/intro') {
89
		$link .= '_0';
90
		$filename = WB_PATH .PAGES_DIRECTORY .'/' .page_filename($title) .'_0' .PAGE_EXTENSION;
91
	} else {
92
		$filename = WB_PATH.PAGES_DIRECTORY.'/'.page_filename($title).PAGE_EXTENSION;
93
	}
94
} else {
95
	$parent_section = '';
96
	$parent_titles = array_reverse(get_parent_titles($parent));
97
	foreach($parent_titles AS $parent_title) {
98
		$parent_section .= page_filename($parent_title).'/';
99
	}
100
	if($parent_section == '/') { $parent_section = ''; }
101
	$link = '/'.$parent_section.page_filename($title);
102
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.$parent_section.page_filename($title).PAGE_EXTENSION;
103
	make_dir(WB_PATH.PAGES_DIRECTORY.'/'.$parent_section);
104
}
105

    
106
// Check if a page with same page filename exists
107
$get_same_page = $database->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE link = '$link'");
108
if($get_same_page->numRows() > 0 OR file_exists(WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION) OR file_exists(WB_PATH.PAGES_DIRECTORY.$link.'/')) {
109
	$admin->print_error($MESSAGE['PAGES']['PAGE_EXISTS']);
110
}
111

    
112
// Include the ordering class
113
require(WB_PATH.'/framework/class.order.php');
114
$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
115
// First clean order
116
$order->clean($parent);
117
// Get new order
118
$position = $order->get_new($parent);
119

    
120
// Work-out if the page parent (if selected) has a seperate template to the default
121
$query_parent = $database->query("SELECT template FROM ".TABLE_PREFIX."pages WHERE page_id = '$parent'");
122
if($query_parent->numRows() > 0) {
123
	$fetch_parent = $query_parent->fetchRow();
124
	$template = $fetch_parent['template'];
125
} else {
126
	$template = '';
127
}
128

    
129
// Insert page into pages table
130
$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()."')";
131
$database->query($query);
132
if($database->is_error()) {
133
	$admin->print_error($database->get_error());
134
}
135

    
136
// Get the page id
137
$page_id = $database->get_one("SELECT LAST_INSERT_ID()");
138

    
139
// Work out level
140
$level = level_count($page_id);
141
// Work out root parent
142
$root_parent = root_parent($page_id);
143
// Work out page trail
144
$page_trail = get_page_trail($page_id);
145

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

    
149
// Create a new file in the /pages dir
150
create_access_file($filename, $page_id, $level);
151

    
152
// Get new order for section
153
$order = new order(TABLE_PREFIX.'sections', 'position', 'section_id', 'page_id');
154
$position = $order->get_new($parent);
155

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

    
159
// Get the section id
160
$section_id = $database->get_one("SELECT LAST_INSERT_ID()");
161

    
162
// Include the selected modules add file if it exists
163
if(file_exists(WB_PATH.'/modules/'.$module.'/add.php')) {
164
	require(WB_PATH.'/modules/'.$module.'/add.php');
165
}
166

    
167
// Check if there is a db error, otherwise say successful
168
if($database->is_error()) {
169
	$admin->print_error($database->get_error());
170
} else {
171
	$admin->print_success($MESSAGE['PAGES']['ADDED'], ADMIN_URL.'/pages/modify.php?page_id='.$page_id);
172
}
173

    
174
// Print admin footer
175
$admin->print_footer();
176

    
177
?>
(1-1/16)