Project

General

Profile

1 1358 Luisehahne
<?php
2
/**
3
 *
4
 * @category        admin
5
 * @package         pages
6 1707 Luisehahne
 * @author          Ryan Djurovich, WebsiteBaker Project
7 1914 Luisehahne
 * @copyright       2009-2013, WebsiteBaker Org. e.V.
8
 * @link            http://www.websitebaker.org/
9 1358 Luisehahne
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11 1373 Luisehahne
 * @requirements    PHP 5.2.2 and higher
12 1358 Luisehahne
 * @version         $Id$
13 1914 Luisehahne
 * @filesource      $HeadURL$
14 1358 Luisehahne
 * @lastmodified    $Date$
15
 *
16
 */
17
18 1868 Luisehahne
if(!defined('WB_URL'))
19
{
20
    $config_file = realpath('../../config.php');
21
    if(file_exists($config_file) && !defined('WB_URL'))
22
    {
23
    	require($config_file);
24
    }
25
}
26 1914 Luisehahne
// Create new admin object and print admin header
27 1868 Luisehahne
if(!class_exists('admin', false)){ include(WB_PATH.'/framework/class.admin.php'); }
28 2087 darkviper
$oReg  = WbAdaptor::getInstance();
29
$oDb   = WbDatabase::getInstance();
30 2091 darkviper
$mLang = Translate::getInstance();
31 1914 Luisehahne
$mLang->enableAddon('admin\pages');
32
33 1457 Luisehahne
// suppress to print the header, so no new FTAN will be set
34
$admin = new admin('Pages', 'pages_add', false);
35 1358 Luisehahne
if (!$admin->checkFTAN())
36
{
37 1457 Luisehahne
	$admin->print_header();
38 1914 Luisehahne
	$admin->print_error($mLang->MESSAGE_GENERIC_SECURITY_ACCESS);
39 1358 Luisehahne
}
40
41
// Include the WB functions file
42
require_once(WB_PATH.'/framework/functions.php');
43
44
// Get values
45 1914 Luisehahne
//$title = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->get_post_escaped('title')));
46
$title = ($admin->StripCodeFromText($admin->get_post('title')));
47 1494 Luisehahne
$module = preg_replace('/[^a-z0-9_-]/i', "", $admin->get_post('type')); // fix secunia 2010-93-4
48 1475 Luisehahne
$parent = intval($admin->get_post('parent')); // fix secunia 2010-91-2
49 1358 Luisehahne
$visibility = $admin->get_post('visibility');
50 1384 Luisehahne
if (!in_array($visibility, array('public', 'private', 'registered', 'hidden', 'none'))) {$visibility = 'public';} // fix secunia 2010-91-2
51 1358 Luisehahne
$admin_groups = $admin->get_post('admin_groups');
52
$viewing_groups = $admin->get_post('viewing_groups');
53
54
// Work-out if we should check for existing page_code
55 2087 darkviper
$field_set = $oDb->isField($oDb->TablePrefix.'pages', 'page_code');
56 1358 Luisehahne
57
// add Admin to admin and viewing-groups
58
$admin_groups[] = 1;
59
$viewing_groups[] = 1;
60
61 1473 Luisehahne
// After check print the header
62
$admin->print_header();
63 1406 FrankH
// check parent page permissions:
64 1358 Luisehahne
if ($parent!=0) {
65
	if (!$admin->get_page_permission($parent,'admin'))
66
    {
67 1914 Luisehahne
        $admin->print_error($mLang->MESSAGE_PAGES_INSUFFICIENT_PERMISSIONS);
68 1358 Luisehahne
    }
69
70
} elseif (!$admin->get_permission('pages_add_l0','system'))
71
{
72 1914 Luisehahne
	$admin->print_error($mLang->MESSAGE_PAGES_INSUFFICIENT_PERMISSIONS);
73 1707 Luisehahne
}
74 1358 Luisehahne
75 1406 FrankH
// check module permissions:
76
if (!$admin->get_permission($module, 'module'))
77
{
78 1914 Luisehahne
	$admin->print_error($mLang->MESSAGE_PAGES_INSUFFICIENT_PERMISSIONS);
79 1707 Luisehahne
}
80 1406 FrankH
81 1358 Luisehahne
// Validate data
82
if($title == '' || substr($title,0,1)=='.')
83
{
84 1914 Luisehahne
	$admin->print_error($title.'::'.$mLang->MESSAGE_PAGES_BLANK_PAGE_TITLE);
85 1358 Luisehahne
}
86
87
// Check to see if page created has needed permissions
88
if(!in_array(1, $admin->get_groups_id()))
89
{
90
	$admin_perm_ok = false;
91
	foreach ($admin_groups as $adm_group)
92
    {
93
		if (in_array($adm_group, $admin->get_groups_id()))
94
        {
95
			$admin_perm_ok = true;
96 1707 Luisehahne
		}
97 1358 Luisehahne
	}
98
	if ($admin_perm_ok == false)
99
    {
100 1914 Luisehahne
		$admin->print_error($mLang->MESSAGE_PAGES_INSUFFICIENT_PERMISSIONS);
101 1358 Luisehahne
	}
102
	$admin_perm_ok = false;
103
	foreach ($viewing_groups as $view_group)
104
    {
105
		if (in_array($view_group, $admin->get_groups_id()))
106
        {
107
			$admin_perm_ok = true;
108
		}
109
	}
110
	if ($admin_perm_ok == false)
111
    {
112 1914 Luisehahne
		$admin->print_error($mLang->MESSAGE_PAGES_INSUFFICIENT_PERMISSIONS);
113 1358 Luisehahne
	}
114
}
115
116
$admin_groups = implode(',', $admin_groups);
117
$viewing_groups = implode(',', $viewing_groups);
118
119 1914 Luisehahne
// preparing root_check to protect system directories and important files from being overwritten if PAGES_DIR = '/'
120
$denied = false;
121
$forbidden  = array();
122
$aTempIniList  = array();
123
$aTempIniList = parse_ini_file(dirname(__FILE__).'/default.ini',true);
124
$bAccessFileOverwrite = $aTempIniList['PagesEnvironment']['AccessFileOverwrite'];
125
$aTempIniList['ProtectedNames']['List'][] = (defined('ADMIN_DIRECTORY') ? trim(ADMIN_DIRECTORY,'/') : 'admin');
126
$aTempIniList['ProtectedNames']['List'][] = (defined('MEDIA_DIRECTORY') ? trim(MEDIA_DIRECTORY,'/') : 'media');
127
$aTempIniList['ProtectedNames']['List'][] = (defined('PAGES_DIRECTORY') ? trim(PAGES_DIRECTORY,'/') : 'pages');
128
$forbidden = $aTempIniList['ProtectedNames'];
129
130
$link = '/'.page_filename($title);
131 1358 Luisehahne
// Work-out what the link and page filename should be
132
if($parent == '0')
133
{
134
	// rename menu titles: index && intro to prevent clashes with intro page feature and WB core file /pages/index.php
135 1914 Luisehahne
	if( defined('PAGES_DIRECTORY') && trim(PAGES_DIRECTORY,'/')=='' ) {
136
// Work-out what the link should be
137
		$denied = in_array(trim($link,'/'), $forbidden['List']);
138
		if( $denied )
139
		{
140
//			$link .= '_'.$iNextPageId;
141
			$admin->print_error($mLang->MESSAGE_PAGES_CANNOT_CREATE_PROTECTED_FILE);
142
		}
143 1358 Luisehahne
	}
144 1914 Luisehahne
	$filename = WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
145 1760 Luisehahne
146 1358 Luisehahne
} else {
147
	$parent_section = '';
148
	$parent_titles = array_reverse(get_parent_titles($parent));
149
	foreach($parent_titles AS $parent_title)
150
    {
151
		$parent_section .= page_filename($parent_title).'/';
152
	}
153
	if($parent_section == '/') { $parent_section = ''; }
154
	$link = '/'.$parent_section.page_filename($title);
155
	$filename = WB_PATH.PAGES_DIRECTORY.'/'.$parent_section.page_filename($title).PAGE_EXTENSION;
156
	make_dir(WB_PATH.PAGES_DIRECTORY.'/'.$parent_section);
157
}
158
159
// Check if a page with same page filename exists
160 2087 darkviper
//$get_same_page = $oDb->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE link = '$link'");
161 1914 Luisehahne
//if($get_same_page->numRows() > 0 OR file_exists(WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION) OR file_exists(WB_PATH.PAGES_DIRECTORY.$link.'/'))
162
//{
163
//	$admin->print_error($MESSAGE['PAGES_PAGE_EXISTS']);
164
//}
165
$bLinkExists = file_exists(WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION) || file_exists(WB_PATH.PAGES_DIRECTORY.$link);
166
167
// UNLOCK TABLES
168 2087 darkviper
$sql = 'SELECT COUNT(*) FROM `'.$oDb->TablePrefix.'pages` '
169 1914 Luisehahne
     . 'WHERE `link` = \''.$link.'\' ';
170 2087 darkviper
if( (($iSamePages = intval($oDb->getOne($sql))) > 0) || $bLinkExists ){
171 1707 Luisehahne
	$admin->print_error($MESSAGE['PAGES_PAGE_EXISTS']);
172 1358 Luisehahne
}
173
174
// Include the ordering class
175
require(WB_PATH.'/framework/class.order.php');
176 2087 darkviper
$order = new order($oDb->TablePrefix.'pages', 'position', 'page_id', 'parent');
177 1358 Luisehahne
// First clean order
178
$order->clean($parent);
179
// Get new order
180
$position = $order->get_new($parent);
181
182
// Work-out if the page parent (if selected) has a seperate template or language to the default
183 2087 darkviper
$query_parent = $oDb->doQuery("SELECT template, language FROM ".$oDb->TablePrefix."pages WHERE page_id = '$parent'");
184 1358 Luisehahne
if($query_parent->numRows() > 0)
185
{
186
	$fetch_parent = $query_parent->fetchRow();
187
	$template = $fetch_parent['template'];
188
	$language = $fetch_parent['language'];
189
} else {
190
	$template = '';
191
	$language = DEFAULT_LANGUAGE;
192
}
193
194
// Insert page into pages table
195 2087 darkviper
$sql = 'INSERT INTO `'.$oDb->TablePrefix.'pages` '
196
     . 'SET `parent` = '.$parent.', '
197
     .     '`target` = \'_top\', '
198
     .     '`page_title` = \''.$title.'\', '
199
     .     '`menu_title` = \''.$title.'\', '
200
     .     '`tooltip` = \''.$title.'\', '
201
     .     '`template` = \''.$template.'\', '
202
     .     '`visibility` = \''.$visibility.'\', '
203
     .     '`position` = '.$position.', '
204
     .     '`menu` = 1, '
205
     .     '`language` = \''.$language.'\', '
206
     .     '`searching` = 1, '
207
     .     '`modified_when` = '.time().', '
208
     .     '`modified_by` = '.$admin->get_user_id().', '
209
     .     '`admin_groups` = \''.$admin_groups.'\', '
210
     .     '`viewing_groups` = \''.$viewing_groups.'\'';
211 1358 Luisehahne
212 2087 darkviper
if(!$oDb->doQuery($sql)) {
213
	if($oDb->isError())
214 1914 Luisehahne
	{
215 2087 darkviper
		$admin->print_error($oDb->getError());
216 1914 Luisehahne
	}
217 1358 Luisehahne
}
218
219
// Get the page id
220 2087 darkviper
//$page_id = $oDb->getOne("SELECT LAST_INSERT_ID()");
221
$page_id = $oDb->LastInsertId;
222 1358 Luisehahne
// Work out level
223
$level = level_count($page_id);
224
// Work out root parent
225
$root_parent = root_parent($page_id);
226
// Work out page trail
227
$page_trail = get_page_trail($page_id);
228
229 1914 Luisehahne
/*
230 2087 darkviper
$oDb->doQuery("UPDATE ".$oDb->TablePrefix."pages SET link = '$link', level = '$level', root_parent = '$root_parent', page_trail = '$page_trail' WHERE page_id = '$page_id'");
231 1914 Luisehahne
*/
232 1358 Luisehahne
// Update page with new level and link
233 2087 darkviper
$sql = 'UPDATE `'.$oDb->TablePrefix.'pages` '
234
     . 'SET `root_parent` = '.$root_parent.', '
235
     .     '`level` = '.$level.', '
236
     .     '`link` = \''.$link.'\', '
237
     .     '`page_trail` = \''.$page_trail.'\' '
238
     .     ( (defined('PAGE_LANGUAGES') && PAGE_LANGUAGES) && $field_set
239
              && ($language == DEFAULT_LANGUAGE) && class_exists('m_MultiLingual_Lib')
240
             ? ', `page_code` = '.(int)$page_id.' '
241
             : ''
242
           )
243
     . 'WHERE `page_id` = '.$page_id;
244
$oDb->doQuery($sql);
245
if($oDb->isError())
246 1358 Luisehahne
{
247 2087 darkviper
	$admin->print_error($oDb->getError());
248 1358 Luisehahne
}
249
250 1868 Luisehahne
// add position 1 to new page section
251 1358 Luisehahne
$position = 1;
252
253
// Add new record into the sections table
254 1754 Luisehahne
// Insert module into DB
255 2087 darkviper
$sql = 'INSERT INTO `'.$oDb->TablePrefix.'sections` '
256
     . 'SET `page_id` = '.(int)$page_id.', '
257
     .     '`module` = \''.$module.'\', '
258
     .     '`position` = '.(int)$position.', '
259
     .     '`block` = \'1\', '
260
     .     '`publ_start` = \'0\','
261
     .     '`publ_end` = \'0\' ';
262
if($oDb->doQuery($sql)) {
263 1754 Luisehahne
	// Get the section id
264 2087 darkviper
	$section_id = $oDb->getOne("SELECT LAST_INSERT_ID()");
265 1754 Luisehahne
	// Include the selected modules add file if it exists
266
	if(file_exists(WB_PATH.'/modules/'.$module.'/add.php'))
267
    {
268
		require(WB_PATH.'/modules/'.$module.'/add.php');
269
	}
270 1358 Luisehahne
}
271 1868 Luisehahne
// Create a new file in the /pages dir
272 2087 darkviper
$sNewLink = str_replace($oReg->AppPath.$oReg->PagesDir, '', str_replace('\\', '/', $filename));
273
try{
274
    $oAccFile = new AccessFile($oReg->AppPath.$oReg->PagesDir, $sNewLink, $page_id);
275
    $oAccFile->write();
276
    unset($oAccFile);
277
} catch (AccessFileException $e) {
278
    $sMsg = $oLang->MESSAGE_PAGES_CANNOT_CREATE_ACCESS_FILE
279
          . '<br />'.$e->getMessage();
280
    $admin->print_error($sMsg);
281 1868 Luisehahne
}
282 1358 Luisehahne
// Check if there is a db error, otherwise say successful
283 2087 darkviper
if($oDb->isError()) {
284
	$admin->print_error($oDb->getError().' (sections)');
285 1358 Luisehahne
} else {
286 1914 Luisehahne
	$admin->print_success($mLang->MESSAGE_PAGES_ADDED, ADMIN_URL.'/pages/modify.php?page_id='.$page_id);
287 1358 Luisehahne
}
288
289 1914 Luisehahne
$mLang->disableAddon();
290 1358 Luisehahne
// Print admin footer
291
$admin->print_footer();