Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        admin
5
 * @package         pages
6
 * @author          Ryan Djurovich, WebsiteBaker Project
7
 * @copyright       2009-2013, WebsiteBaker Org. e.V.
8
 * @link            http://www.websitebaker.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11
 * @requirements    PHP 5.2.2 and higher
12
 * @version         $Id: add.php 2091 2014-01-20 14:16:54Z darkviper $
13
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/pages/add.php $
14
 * @lastmodified    $Date: 2014-01-20 15:16:54 +0100 (Mon, 20 Jan 2014) $
15
 *
16
 */
17

    
18
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
// Create new admin object and print admin header
27
if(!class_exists('admin', false)){ include(WB_PATH.'/framework/class.admin.php'); }
28
$oReg  = WbAdaptor::getInstance();
29
$oDb   = WbDatabase::getInstance();
30
$mLang = Translate::getInstance();
31
$mLang->enableAddon('admin\pages');
32

    
33
// suppress to print the header, so no new FTAN will be set
34
$admin = new admin('Pages', 'pages_add', false);
35
if (!$admin->checkFTAN())
36
{
37
	$admin->print_header();
38
	$admin->print_error($mLang->MESSAGE_GENERIC_SECURITY_ACCESS);
39
}
40

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

    
44
// Get values
45
//$title = str_replace(array("[[", "]]"), '', htmlspecialchars($admin->get_post_escaped('title')));
46
$title = ($admin->StripCodeFromText($admin->get_post('title')));
47
$module = preg_replace('/[^a-z0-9_-]/i', "", $admin->get_post('type')); // fix secunia 2010-93-4
48
$parent = intval($admin->get_post('parent')); // fix secunia 2010-91-2
49
$visibility = $admin->get_post('visibility');
50
if (!in_array($visibility, array('public', 'private', 'registered', 'hidden', 'none'))) {$visibility = 'public';} // fix secunia 2010-91-2
51
$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
$field_set = $oDb->isField($oDb->TablePrefix.'pages', 'page_code');
56

    
57
// add Admin to admin and viewing-groups
58
$admin_groups[] = 1;
59
$viewing_groups[] = 1;
60

    
61
// After check print the header
62
$admin->print_header();
63
// check parent page permissions:
64
if ($parent!=0) {
65
	if (!$admin->get_page_permission($parent,'admin'))
66
    {
67
        $admin->print_error($mLang->MESSAGE_PAGES_INSUFFICIENT_PERMISSIONS);
68
    }
69

    
70
} elseif (!$admin->get_permission('pages_add_l0','system'))
71
{
72
	$admin->print_error($mLang->MESSAGE_PAGES_INSUFFICIENT_PERMISSIONS);
73
}
74

    
75
// check module permissions:
76
if (!$admin->get_permission($module, 'module'))
77
{
78
	$admin->print_error($mLang->MESSAGE_PAGES_INSUFFICIENT_PERMISSIONS);
79
}
80

    
81
// Validate data
82
if($title == '' || substr($title,0,1)=='.')
83
{
84
	$admin->print_error($title.'::'.$mLang->MESSAGE_PAGES_BLANK_PAGE_TITLE);
85
}
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
		}
97
	}
98
	if ($admin_perm_ok == false)
99
    {
100
		$admin->print_error($mLang->MESSAGE_PAGES_INSUFFICIENT_PERMISSIONS);
101
	}
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
		$admin->print_error($mLang->MESSAGE_PAGES_INSUFFICIENT_PERMISSIONS);
113
	}
114
}
115

    
116
$admin_groups = implode(',', $admin_groups);
117
$viewing_groups = implode(',', $viewing_groups);
118

    
119
// 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
// 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
	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
	}
144
	$filename = WB_PATH.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
145

    
146
} 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
//$get_same_page = $oDb->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE link = '$link'");
161
//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
$sql = 'SELECT COUNT(*) FROM `'.$oDb->TablePrefix.'pages` '
169
     . 'WHERE `link` = \''.$link.'\' ';
170
if( (($iSamePages = intval($oDb->getOne($sql))) > 0) || $bLinkExists ){
171
	$admin->print_error($MESSAGE['PAGES_PAGE_EXISTS']);
172
}
173

    
174
// Include the ordering class
175
require(WB_PATH.'/framework/class.order.php');
176
$order = new order($oDb->TablePrefix.'pages', 'position', 'page_id', 'parent');
177
// 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
$query_parent = $oDb->doQuery("SELECT template, language FROM ".$oDb->TablePrefix."pages WHERE page_id = '$parent'");
184
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
$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

    
212
if(!$oDb->doQuery($sql)) {
213
	if($oDb->isError())
214
	{
215
		$admin->print_error($oDb->getError());
216
	}
217
}
218

    
219
// Get the page id
220
//$page_id = $oDb->getOne("SELECT LAST_INSERT_ID()");
221
$page_id = $oDb->LastInsertId;
222
// 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
/*
230
$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
*/
232
// Update page with new level and link
233
$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
{
247
	$admin->print_error($oDb->getError());
248
}
249

    
250
// add position 1 to new page section
251
$position = 1;
252

    
253
// Add new record into the sections table
254
// Insert module into DB
255
$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
	// Get the section id
264
	$section_id = $oDb->getOne("SELECT LAST_INSERT_ID()");
265
	// 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
}
271
// Create a new file in the /pages dir
272
$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
}
282
// Check if there is a db error, otherwise say successful
283
if($oDb->isError()) {
284
	$admin->print_error($oDb->getError().' (sections)');
285
} else {
286
	$admin->print_success($mLang->MESSAGE_PAGES_ADDED, ADMIN_URL.'/pages/modify.php?page_id='.$page_id);
287
}
288

    
289
$mLang->disableAddon();
290
// Print admin footer
291
$admin->print_footer();
(3-3/25)