| 1 | <?php
 | 
  
    | 2 | /**
 | 
  
    | 3 |  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 | 
  
    | 4 |  *
 | 
  
    | 5 |  * This program is free software: you can redistribute it and/or modify
 | 
  
    | 6 |  * it under the terms of the GNU General Public License as published by
 | 
  
    | 7 |  * the Free Software Foundation, either version 3 of the License, or
 | 
  
    | 8 |  * (at your option) any later version.
 | 
  
    | 9 |  *
 | 
  
    | 10 |  * This program is distributed in the hope that it will be useful,
 | 
  
    | 11 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
  
    | 12 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
  
    | 13 |  * GNU General Public License for more details.
 | 
  
    | 14 |  *
 | 
  
    | 15 |  * You should have received a copy of the GNU General Public License
 | 
  
    | 16 |  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
  
    | 17 |  */
 | 
  
    | 18 | 
 | 
  
    | 19 | /**
 | 
  
    | 20 |  * save.php
 | 
  
    | 21 |  *
 | 
  
    | 22 |  * @category     Core
 | 
  
    | 23 |  * @package      Core_Environment
 | 
  
    | 24 |  * @subpackage   Installer
 | 
  
    | 25 |  * @author       Dietmar Wöllbrink <dietmar.woellbrink@websitebaker.org>
 | 
  
    | 26 |  * @copyright    Werner v.d.Decken <wkl@isteam.de>
 | 
  
    | 27 |  * @license      http://www.gnu.org/licenses/gpl.html   GPL License
 | 
  
    | 28 |  * @version      0.0.2
 | 
  
    | 29 |  * @revision     $Revision: 2110 $
 | 
  
    | 30 |  * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/install/save.php $
 | 
  
    | 31 |  * @lastmodified $Date: 2014-11-25 15:14:54 +0100 (Tue, 25 Nov 2014) $
 | 
  
    | 32 |  * @since        File available since 2012-04-01
 | 
  
    | 33 |  * @description  xyz
 | 
  
    | 34 |  */
 | 
  
    | 35 | 
 | 
  
    | 36 | $debug = true;
 | 
  
    | 37 | if(!defined('DEBUG')) { define('DEBUG', true); }
 | 
  
    | 38 | 
 | 
  
    | 39 | include(dirname(__DIR__).'/framework/globalExceptionHandler.php');
 | 
  
    | 40 | include(dirname(__DIR__).'/framework/WbAutoloader.php');
 | 
  
    | 41 | WbAutoloader::doRegister(array('admin'=>'a', 'modules'=>'m', 'templates'=>'t', 'include'=>'i'));
 | 
  
    | 42 | include(__DIR__.'/InstallHelper.php');
 | 
  
    | 43 | // register PHPMailer autoloader ---
 | 
  
    | 44 | if (!function_exists('PHPMailerAutoload')) {
 | 
  
    | 45 |     require(dirname(__DIR__).'/include/phpmailer/PHPMailerAutoload.php');
 | 
  
    | 46 | }
 | 
  
    | 47 | 
 | 
  
    | 48 | function errorLogs($error_str)
 | 
  
    | 49 | {
 | 
  
    | 50 |     $log_error = true;
 | 
  
    | 51 |     if ( ! function_exists('error_log') ) { $log_error = false; }
 | 
  
    | 52 |     $log_file = @ini_get('error_log');
 | 
  
    | 53 |     if ( !empty($log_file) && ('syslog' != $log_file) && !@is_writable($log_file) ) {
 | 
  
    | 54 |         $log_error = false;
 | 
  
    | 55 |     }
 | 
  
    | 56 |     if ( $log_error ) {@error_log($error_str, 0);}
 | 
  
    | 57 | }
 | 
  
    | 58 | 
 | 
  
    | 59 | /**
 | 
  
    | 60 |  * Read DB settings from configuration file
 | 
  
    | 61 |  * @return string
 | 
  
    | 62 |  * @throws RuntimeException
 | 
  
    | 63 |  *
 | 
  
    | 64 |  */
 | 
  
    | 65 |     function _readConfiguration($sRetvalType = 'url') {
 | 
  
    | 66 |         // check for valid file request. Becomes more stronger in next version
 | 
  
    | 67 |         $x = debug_backtrace();
 | 
  
    | 68 |         $bValidRequest = false;
 | 
  
    | 69 |         if(sizeof($x) != 0) {
 | 
  
    | 70 |             foreach($x as $aStep) {
 | 
  
    | 71 |                 // define the scripts which can read the configuration
 | 
  
    | 72 |                 if(preg_match('/(save.php|index.php|config.php|upgrade-script.php)$/si', $aStep['file'])) {
 | 
  
    | 73 |                     $bValidRequest = true;
 | 
  
    | 74 |                     break;
 | 
  
    | 75 |                 }
 | 
  
    | 76 |             }
 | 
  
    | 77 |         }else {
 | 
  
    | 78 |             $bValidRequest = true;
 | 
  
    | 79 |         }
 | 
  
    | 80 |         if(!$bValidRequest) {
 | 
  
    | 81 |             throw new RuntimeException('illegal function request!');
 | 
  
    | 82 |         }
 | 
  
    | 83 |         $aRetval = array();
 | 
  
    | 84 |         $sSetupFile = dirname(dirname(__FILE__)).'/setup.ini.php';
 | 
  
    | 85 |         if(is_readable($sSetupFile)) {
 | 
  
    | 86 |             $aCfg = parse_ini_file($sSetupFile, true);
 | 
  
    | 87 |             foreach($aCfg['Constants'] as $key=>$value) {
 | 
  
    | 88 |                 switch($key):
 | 
  
    | 89 |                     case 'DEBUG':
 | 
  
    | 90 |                         $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
 | 
  
    | 91 |                         break;
 | 
  
    | 92 |                     case 'WB_URL':
 | 
  
    | 93 |                     case 'AppUrl':
 | 
  
    | 94 |                         $value = trim(str_replace('\\', '/', $value), '/');
 | 
  
    | 95 |                         if(!defined('WB_URL')) { define('WB_URL', $value); }
 | 
  
    | 96 |                         break;
 | 
  
    | 97 |                     case 'ADMIN_DIRECTORY':
 | 
  
    | 98 |                     case 'AcpDir':
 | 
  
    | 99 |                         $value = trim(str_replace('\\', '/', $value), '/');
 | 
  
    | 100 |                         if(!defined('ADMIN_DIRECTORY')) { define('ADMIN_DIRECTORY', $value); }
 | 
  
    | 101 |                         break;
 | 
  
    | 102 |                     default:
 | 
  
    | 103 |                         if(!defined($key)) { define($key, $value); }
 | 
  
    | 104 |                         break;
 | 
  
    | 105 |                 endswitch;
 | 
  
    | 106 |             }
 | 
  
    | 107 |             $db = $aCfg['DataBase'];
 | 
  
    | 108 |             $db['type'] = isset($db['type']) ? $db['type'] : 'mysql';
 | 
  
    | 109 |             $db['user'] = isset($db['user']) ? $db['user'] : 'foo';
 | 
  
    | 110 |             $db['pass'] = isset($db['pass']) ? $db['pass'] : 'bar';
 | 
  
    | 111 |             $db['host'] = isset($db['host']) ? $db['host'] : 'localhost';
 | 
  
    | 112 |             $db['port'] = isset($db['port']) ? $db['port'] : '3306';
 | 
  
    | 113 |             $db['port'] = ($db['port'] != '3306') ? $db['port'] : '';
 | 
  
    | 114 |             $db['name'] = isset($db['name']) ? $db['name'] : 'dummy';
 | 
  
    | 115 |             $db['charset'] = isset($db['charset']) ? $db['charset'] : 'utf8';
 | 
  
    | 116 |             $db['table_prefix'] = (isset($db['table_prefix']) ? $db['table_prefix'] : '');
 | 
  
    | 117 |             if(!defined('TABLE_PREFIX')) {define('TABLE_PREFIX', $db['table_prefix']);}
 | 
  
    | 118 |             if($sRetvalType == 'dsn') {
 | 
  
    | 119 |                 $aRetval[0] = $db['type'].':dbname='.$db['name'].';host='.$db['host'].';'
 | 
  
    | 120 |                             . ($db['port'] != '' ? 'port='.(int)$db['port'].';' : '');
 | 
  
    | 121 |                 $aRetval[1] = array('CHARSET' => $db['charset'], 'TABLE_PREFIX' => $db['table_prefix']);
 | 
  
    | 122 |                 $aRetval[2] = array( 'user' => $db['user'], 'pass' => $db['pass']);
 | 
  
    | 123 |             }else { // $sRetvalType == 'url'
 | 
  
    | 124 |                 $aRetval[0] = $db['type'].'://'.$db['user'].':'.$db['pass'].'@'
 | 
  
    | 125 |                             . $db['host'].($db['port'] != '' ? ':'.$db['port'] : '').'/'.$db['name']
 | 
  
    | 126 |                             . '?Charset='.$db['charset'].'&TablePrefix='.$db['table_prefix'];
 | 
  
    | 127 |             }
 | 
  
    | 128 |             unset($db, $aCfg);
 | 
  
    | 129 |             return $aRetval;
 | 
  
    | 130 |         }
 | 
  
    | 131 |         throw new RuntimeException('unable to read setup.ini.php');
 | 
  
    | 132 |     }
 | 
  
    | 133 | 
 | 
  
    | 134 | if (true === $debug) {
 | 
  
    | 135 |     ini_set('display_errors', 1);
 | 
  
    | 136 |     error_reporting(E_ALL);
 | 
  
    | 137 | }
 | 
  
    | 138 | // Start a session
 | 
  
    | 139 | if(!defined('SESSION_STARTED')) {
 | 
  
    | 140 |     session_name('wb_session_id');
 | 
  
    | 141 |     session_start();
 | 
  
    | 142 |     define('SESSION_STARTED', true);
 | 
  
    | 143 | }
 | 
  
    | 144 | // get random-part for session_name()
 | 
  
    | 145 | list($usec,$sec) = explode(' ',microtime());
 | 
  
    | 146 | srand((float)$sec+((float)$usec*100000));
 | 
  
    | 147 | $session_rand = rand(1000,9999);
 | 
  
    | 148 | 
 | 
  
    | 149 | // Function to set error
 | 
  
    | 150 | function set_error($message, $field_name = '') {
 | 
  
    | 151 |     global $_POST;
 | 
  
    | 152 |     if(isset($message) AND $message != '') {
 | 
  
    | 153 |         // Copy values entered into session so user doesn't have to re-enter everything
 | 
  
    | 154 |         if(isset($_POST['website_title'])) {
 | 
  
    | 155 |             $_SESSION['website_title'] = $_POST['website_title'];
 | 
  
    | 156 |             $_SESSION['default_timezone'] = $_POST['default_timezone'];
 | 
  
    | 157 |             $_SESSION['default_language'] = $_POST['default_language'];
 | 
  
    | 158 |             if(!isset($_POST['operating_system'])) {
 | 
  
    | 159 |                 $_SESSION['operating_system'] = 'linux';
 | 
  
    | 160 |             } else {
 | 
  
    | 161 |                 $_SESSION['operating_system'] = $_POST['operating_system'];
 | 
  
    | 162 |             }
 | 
  
    | 163 |             if(!isset($_POST['world_writeable'])) {
 | 
  
    | 164 |                 $_SESSION['world_writeable'] = false;
 | 
  
    | 165 |             } else {
 | 
  
    | 166 |                 $_SESSION['world_writeable'] = true;
 | 
  
    | 167 |             }
 | 
  
    | 168 |             $_SESSION['database_host'] = $_POST['database_host'];
 | 
  
    | 169 |             $_SESSION['database_username'] = $_POST['database_username'];
 | 
  
    | 170 |             $_SESSION['database_password'] = '';
 | 
  
    | 171 |             $_SESSION['database_name'] = $_POST['database_name'];
 | 
  
    | 172 |             $_SESSION['table_prefix'] = $_POST['table_prefix'];
 | 
  
    | 173 |             if(!isset($_POST['install_tables'])) {
 | 
  
    | 174 |                 $_SESSION['install_tables'] = true;
 | 
  
    | 175 |             } else {
 | 
  
    | 176 |                 $_SESSION['install_tables'] = true;
 | 
  
    | 177 |             }
 | 
  
    | 178 |             $_SESSION['website_title'] = $_POST['website_title'];
 | 
  
    | 179 |             $_SESSION['admin_username'] = $_POST['admin_username'];
 | 
  
    | 180 |             $_SESSION['admin_email'] = $_POST['admin_email'];
 | 
  
    | 181 |             $_SESSION['admin_password'] = '';
 | 
  
    | 182 |             $_SESSION['admin_repassword'] = '';
 | 
  
    | 183 |         }
 | 
  
    | 184 |         // Set the message
 | 
  
    | 185 |         $_SESSION['message'] = $message;
 | 
  
    | 186 |         // Set the element(s) to highlight
 | 
  
    | 187 |         if($field_name != '') {
 | 
  
    | 188 |             $_SESSION['ERROR_FIELD'] = $field_name;
 | 
  
    | 189 |         }
 | 
  
    | 190 |         // Specify that session support is enabled
 | 
  
    | 191 |         $_SESSION['session_support'] = '<font class="good">Enabled</font>';
 | 
  
    | 192 |         // Redirect to first page again and exit
 | 
  
    | 193 |         header('Location: index.php?sessions_checked=true');
 | 
  
    | 194 |         exit();
 | 
  
    | 195 |     }
 | 
  
    | 196 | }
 | 
  
    | 197 | /* */
 | 
  
    | 198 | 
 | 
  
    | 199 | // Function to workout what the default permissions are for files created by the webserver
 | 
  
    | 200 | function default_file_mode($temp_dir) {
 | 
  
    | 201 |     $v = explode(".",PHP_VERSION);
 | 
  
    | 202 |     $v = $v[0].$v[1];
 | 
  
    | 203 |     if($v > 41 AND is_writable($temp_dir)) {
 | 
  
    | 204 |         $filename = $temp_dir.'/test_permissions.txt';
 | 
  
    | 205 |         $handle = fopen($filename, 'w');
 | 
  
    | 206 |         fwrite($handle, 'This file is to get the default file permissions');
 | 
  
    | 207 |         fclose($handle);
 | 
  
    | 208 |         $default_file_mode = '0'.substr(sprintf('%o', fileperms($filename)), -3);
 | 
  
    | 209 |         unlink($filename);
 | 
  
    | 210 |     } else {
 | 
  
    | 211 |         $default_file_mode = '0666';
 | 
  
    | 212 |     }
 | 
  
    | 213 |     return $default_file_mode;
 | 
  
    | 214 | }
 | 
  
    | 215 | 
 | 
  
    | 216 | // Function to workout what the default permissions are for directories created by the webserver
 | 
  
    | 217 | function default_dir_mode($temp_dir) {
 | 
  
    | 218 |     $v = explode(".",PHP_VERSION);
 | 
  
    | 219 |     $v = $v[0].$v[1];
 | 
  
    | 220 |     if($v > 41 AND is_writable($temp_dir)) {
 | 
  
    | 221 |         $dirname = $temp_dir.'/test_permissions/';
 | 
  
    | 222 |         mkdir($dirname);
 | 
  
    | 223 |         $default_dir_mode = '0'.substr(sprintf('%o', fileperms($dirname)), -3);
 | 
  
    | 224 |         rmdir($dirname);
 | 
  
    | 225 |     } else {
 | 
  
    | 226 |         $default_dir_mode = '0777';
 | 
  
    | 227 |     }
 | 
  
    | 228 |     return $default_dir_mode;
 | 
  
    | 229 | }
 | 
  
    | 230 | 
 | 
  
    | 231 | function add_slashes($input) {
 | 
  
    | 232 |     if ( get_magic_quotes_gpc() || ( !is_string($input) ) ) {
 | 
  
    | 233 |         return $input;
 | 
  
    | 234 |     }
 | 
  
    | 235 |     $output = addslashes($input);
 | 
  
    | 236 |     return $output;
 | 
  
    | 237 | }
 | 
  
    | 238 | 
 | 
  
    | 239 | // Begin check to see if form was even submitted
 | 
  
    | 240 | // Set error if no post vars found
 | 
  
    | 241 | if(!isset($_POST['website_title'])) {
 | 
  
    | 242 |     set_error('Please fill-in the wesite title below');
 | 
  
    | 243 | }
 | 
  
    | 244 | // End check to see if form was even submitted
 | 
  
    | 245 | 
 | 
  
    | 246 | // Begin path and timezone details code
 | 
  
    | 247 | 
 | 
  
    | 248 | // Check if user has entered the installation url
 | 
  
    | 249 | if(!isset($_POST['wb_url']) OR $_POST['wb_url'] == '') {
 | 
  
    | 250 |     set_error('Please enter an absolute URL', 'wb_url');
 | 
  
    | 251 | } else {
 | 
  
    | 252 |     $wb_url = $_POST['wb_url'];
 | 
  
    | 253 | }
 | 
  
    | 254 | // Remove any slashes at the end of the URL
 | 
  
    | 255 | $wb_url = trim(str_replace('\\', '/', $wb_url), '/').'/';
 | 
  
    | 256 | // Get the default time zone
 | 
  
    | 257 | if(!isset($_POST['default_timezone']) OR !is_numeric($_POST['default_timezone'])) {
 | 
  
    | 258 |     set_error('Please select a valid default timezone', 'default_timezone');
 | 
  
    | 259 | } else {
 | 
  
    | 260 |     $default_timezone = $_POST['default_timezone']*60*60;
 | 
  
    | 261 | }
 | 
  
    | 262 | // End path and timezone details code
 | 
  
    | 263 | 
 | 
  
    | 264 | // Get the default language
 | 
  
    | 265 | $sLanguageDirectory = dirname(__DIR__).'/languages/';
 | 
  
    | 266 | $allowed_languages = InstallHelper::getAvailableLanguages($sLanguageDirectory);
 | 
  
    | 267 | if (
 | 
  
    | 268 |     !isset($_POST['default_language']) ||
 | 
  
    | 269 |     !array_key_exists($_POST['default_language'], $allowed_languages)
 | 
  
    | 270 | ) {
 | 
  
    | 271 |     set_error('Please select a valid default backend language','default_language');
 | 
  
    | 272 | } else {
 | 
  
    | 273 |     $default_language = $_POST['default_language'];
 | 
  
    | 274 |     // make sure the selected language file exists in the language folder
 | 
  
    | 275 |     if(!file_exists('../languages/' .$default_language .'.php')) {
 | 
  
    | 276 |         set_error('The language file: \'' .$default_language .'.php\' is missing. Upload file to language folder or choose another language','default_language');
 | 
  
    | 277 |     }
 | 
  
    | 278 | }
 | 
  
    | 279 | // End default language details code
 | 
  
    | 280 | 
 | 
  
    | 281 | // Begin operating system specific code
 | 
  
    | 282 | // Get operating system
 | 
  
    | 283 | if(!isset($_POST['operating_system']) OR $_POST['operating_system'] != 'linux' AND $_POST['operating_system'] != 'windows') {
 | 
  
    | 284 |     set_error('Please select a valid operating system');
 | 
  
    | 285 | } else {
 | 
  
    | 286 |     $operating_system = $_POST['operating_system'];
 | 
  
    | 287 | }
 | 
  
    | 288 | // Work-out file permissions
 | 
  
    | 289 | if($operating_system == 'windows') {
 | 
  
    | 290 |     $file_mode = '0777';
 | 
  
    | 291 |     $dir_mode = '0666';
 | 
  
    | 292 | } elseif(isset($_POST['world_writeable']) AND $_POST['world_writeable'] == 'true') {
 | 
  
    | 293 |     $file_mode = '0666';
 | 
  
    | 294 |     $dir_mode = '0777';
 | 
  
    | 295 | } else {
 | 
  
    | 296 |     $file_mode = default_file_mode('../temp');
 | 
  
    | 297 |     $dir_mode = default_dir_mode('../temp');
 | 
  
    | 298 | }
 | 
  
    | 299 | // End operating system specific code
 | 
  
    | 300 | 
 | 
  
    | 301 | // Begin database details code
 | 
  
    | 302 | // Check if user has entered a database host
 | 
  
    | 303 | if(!isset($_POST['database_host']) OR $_POST['database_host'] == '') {
 | 
  
    | 304 |     set_error('Please enter a database host name', 'database_host');
 | 
  
    | 305 | } else {
 | 
  
    | 306 |     $database_host = $_POST['database_host'];
 | 
  
    | 307 |  }
 | 
  
    | 308 | // Check if user has entered a database name
 | 
  
    | 309 | if(!isset($_POST['database_name']) OR $_POST['database_name'] == '') {
 | 
  
    | 310 |     set_error('Please enter a database name', 'database_name');
 | 
  
    | 311 | } else {
 | 
  
    | 312 |     // make sure only allowed characters are specified
 | 
  
    | 313 |     if(!preg_match('/^[a-z0-9_-]*$/i', $_POST['database_name'])) {
 | 
  
    | 314 |         // contains invalid characters (only a-z, A-Z, 0-9 and _ allowed to avoid problems with table/field names)
 | 
  
    | 315 |         set_error('Only characters a-z, A-Z, 0-9, - and _ allowed in database name.', 'database_name');
 | 
  
    | 316 |     }
 | 
  
    | 317 |     $database_name = $_POST['database_name'];
 | 
  
    | 318 | }
 | 
  
    | 319 | // Get table prefix
 | 
  
    | 320 | if(!preg_match('/^[a-z0-9_]*$/i', $_POST['table_prefix'])) {
 | 
  
    | 321 |     // contains invalid characters (only a-z, A-Z, 0-9 and _ allowed to avoid problems with table/field names)
 | 
  
    | 322 |     set_error('Only characters a-z, A-Z, 0-9 and _ allowed in table_prefix.', 'table_prefix');
 | 
  
    | 323 | } else {
 | 
  
    | 324 |     $table_prefix = $_POST['table_prefix'];
 | 
  
    | 325 | }
 | 
  
    | 326 | 
 | 
  
    | 327 | // Check if user has entered a database username
 | 
  
    | 328 | if(!isset($_POST['database_username']) OR $_POST['database_username'] == '') {
 | 
  
    | 329 |     set_error('Please enter a database username','database_username');
 | 
  
    | 330 | } else {
 | 
  
    | 331 |     $database_username = $_POST['database_username'];
 | 
  
    | 332 | }
 | 
  
    | 333 | // Check if user has entered a database password
 | 
  
    | 334 | if(!isset($_POST['database_password'])&& ($_POST['database_password']==='') ) {
 | 
  
    | 335 |     set_error('Please enter a database password', 'database_password');
 | 
  
    | 336 | } else {
 | 
  
    | 337 |     $database_password = $_POST['database_password'];
 | 
  
    | 338 | }
 | 
  
    | 339 | 
 | 
  
    | 340 | // Find out if the user wants to install tables and data
 | 
  
    | 341 | $install_tables = ((isset($_POST['install_tables']) AND $_POST['install_tables'] == 'true'));
 | 
  
    | 342 | // End database details code
 | 
  
    | 343 | 
 | 
  
    | 344 | // Begin website title code
 | 
  
    | 345 | // Get website title
 | 
  
    | 346 | if(!isset($_POST['website_title']) OR $_POST['website_title'] == '') {
 | 
  
    | 347 |     set_error('Please enter a website title', 'website_title');
 | 
  
    | 348 | } else {
 | 
  
    | 349 |     $website_title = add_slashes($_POST['website_title']);
 | 
  
    | 350 | }
 | 
  
    | 351 | // End website title code
 | 
  
    | 352 | 
 | 
  
    | 353 | // Begin admin user details code
 | 
  
    | 354 | // Get admin username
 | 
  
    | 355 | if(!isset($_POST['admin_username']) OR $_POST['admin_username'] == '') {
 | 
  
    | 356 |     set_error('Please enter a username for the Administrator account','admin_username');
 | 
  
    | 357 | } else {
 | 
  
    | 358 |     $admin_username = $_POST['admin_username'];
 | 
  
    | 359 | }
 | 
  
    | 360 | // Get admin email and validate it
 | 
  
    | 361 | if(!isset($_POST['admin_email']) OR $_POST['admin_email'] == '') {
 | 
  
    | 362 |     set_error('Please enter an email for the Administrator account','admin_email');
 | 
  
    | 363 | } else {
 | 
  
    | 364 |     if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i', $_POST['admin_email'])) {
 | 
  
    | 365 |         $admin_email = $_POST['admin_email'];
 | 
  
    | 366 |     } else {
 | 
  
    | 367 |         set_error('Please enter a valid email address for the Administrator account','admin_email');
 | 
  
    | 368 |     }
 | 
  
    | 369 | }
 | 
  
    | 370 | // Get the two admin passwords entered, and check that they match
 | 
  
    | 371 | if(!isset($_POST['admin_password']) OR $_POST['admin_password'] == '') {
 | 
  
    | 372 |     set_error('Please enter a password for the Administrator account','admin_password');
 | 
  
    | 373 | } else {
 | 
  
    | 374 |     $admin_password = $_POST['admin_password'];
 | 
  
    | 375 | }
 | 
  
    | 376 | if(!isset($_POST['admin_repassword']) OR $_POST['admin_repassword'] == '') {
 | 
  
    | 377 |     set_error('Please make sure you re-enter the password for the Administrator account','admin_repassword');
 | 
  
    | 378 | } else {
 | 
  
    | 379 |     $admin_repassword = $_POST['admin_repassword'];
 | 
  
    | 380 | }
 | 
  
    | 381 | if($admin_password != $admin_repassword) {
 | 
  
    | 382 |     set_error('Sorry, the two Administrator account passwords you entered do not match','admin_repassword');
 | 
  
    | 383 | }
 | 
  
    | 384 | if (mb_strlen($admin_password) < 6) {
 | 
  
    | 385 |     set_error('Sorry, the password must have 6 chars at last','admin_password');
 | 
  
    | 386 | }
 | 
  
    | 387 | 
 | 
  
    | 388 | // End admin user details code
 | 
  
    | 389 | 
 | 
  
    | 390 | // Try and write settings to config file
 | 
  
    | 391 | $sConfigContent =
 | 
  
    | 392 |  ";<?php die('sorry, illegal file access'); ?>#####\n"
 | 
  
    | 393 | .";################################################\n"
 | 
  
    | 394 | ."; WebsiteBaker configuration file\n"
 | 
  
    | 395 | ."; auto generated ".date('Y-m-d h:i:s A e ')."\n"
 | 
  
    | 396 | .";################################################\n"
 | 
  
    | 397 | ."[Constants]\n"
 | 
  
    | 398 | ."DEBUG   = false\n"
 | 
  
    | 399 | ."AppUrl  = \"".$wb_url."\"\n"
 | 
  
    | 400 | ."AcpDir  = \"admin/\"\n"
 | 
  
    | 401 | .";##########\n"
 | 
  
    | 402 | ."[DataBase]\n"
 | 
  
    | 403 | ."type    = \"mysql\"\n"
 | 
  
    | 404 | ."user    = \"".$database_username."\"\n"
 | 
  
    | 405 | ."pass    = \"".$database_password."\"\n"
 | 
  
    | 406 | ."host    = \"".$database_host."\"\n"
 | 
  
    | 407 | ."port    = \"3306\"\n"
 | 
  
    | 408 | ."socket  = \"\"\n"
 | 
  
    | 409 | ."name    = \"".$database_name."\"\n"
 | 
  
    | 410 | ."charset = \"utf8\"\n"
 | 
  
    | 411 | ."table_prefix = \"".$table_prefix."\"\n"
 | 
  
    | 412 | .";\n"
 | 
  
    | 413 | .";################################################\n";
 | 
  
    | 414 | $sConfigFile = realpath('../setup.ini.php');
 | 
  
    | 415 | $sConfigName = basename($sConfigFile);
 | 
  
    | 416 | // Check if the file exists and is writable first.
 | 
  
    | 417 | if(file_exists($sConfigFile) && is_writable($sConfigFile)) {
 | 
  
    | 418 |     if(!$handle = fopen($sConfigFile, 'w')) {
 | 
  
    | 419 |         set_error("Cannot open the configuration file ($sConfigName)");
 | 
  
    | 420 |     } else {
 | 
  
    | 421 |         if (fwrite($handle, $sConfigContent) === FALSE) {
 | 
  
    | 422 |             set_error("Cannot write to the configuration file ($sConfigName)");
 | 
  
    | 423 |         }
 | 
  
    | 424 |         // Close file
 | 
  
    | 425 |         fclose($handle);
 | 
  
    | 426 |     }
 | 
  
    | 427 | } else {
 | 
  
    | 428 |     set_error("The configuration file $sConfigName is not writable. Change its permissions so it is, then re-run step 4.");
 | 
  
    | 429 | }
 | 
  
    | 430 | 
 | 
  
    | 431 | //_SetInstallPathConstants();
 | 
  
    | 432 | if(!defined('WB_INSTALL_PROCESS')){ define('WB_INSTALL_PROCESS', true ); }
 | 
  
    | 433 | if(!defined('WB_URL')){ define('WB_URL', trim( $wb_url,'/' )); }
 | 
  
    | 434 | if(!defined('WB_PATH')){ define('WB_PATH', dirname(dirname(__FILE__))); }
 | 
  
    | 435 | if(!defined('ADMIN_URL')){ define('ADMIN_URL', WB_URL.'/admin'); }
 | 
  
    | 436 | if(!defined('ADMIN_PATH')){ define('ADMIN_PATH', WB_PATH.'/admin'); }
 | 
  
    | 437 | // load db configuration ---
 | 
  
    | 438 | $sDbConnectType = 'url'; // depending from class WbDatabase it can be 'url' or 'dsn'
 | 
  
    | 439 | $aSqlData = _readConfiguration($sDbConnectType);
 | 
  
    | 440 | 
 | 
  
    | 441 | if(!file_exists(WB_PATH.'/framework/class.admin.php')) {
 | 
  
    | 442 |     set_error('It appears the Absolute path that you entered is incorrect');
 | 
  
    | 443 | }
 | 
  
    | 444 | 
 | 
  
    | 445 | $database = WbDatabase::getInstance();
 | 
  
    | 446 | try{
 | 
  
    | 447 |     if($sDbConnectType == 'dsn') {
 | 
  
    | 448 |         $bTmp = @$database->doConnect($aSqlData[0], $aSqlData[1]['user'], $aSqlData[1]['pass'], $aSqlData[2]);
 | 
  
    | 449 |     }else {
 | 
  
    | 450 |         $bTmp = @$database->doConnect($aSqlData[0], TABLE_PREFIX);
 | 
  
    | 451 |     }
 | 
  
    | 452 | } catch (WbDatabaseException $e) {
 | 
  
    | 453 |     if(!file_put_contents($sConfigFile,"<?php\n")) {
 | 
  
    | 454 |         set_error("Cannot write to the configuration file ($sSetupFile)");
 | 
  
    | 455 |     }
 | 
  
    | 456 |     set_error($e->getMessage());
 | 
  
    | 457 | }
 | 
  
    | 458 | 
 | 
  
    | 459 | unset($aSqlData);
 | 
  
    | 460 | // write the config.php
 | 
  
    | 461 | $sConfigContent = "<?php\n"
 | 
  
    | 462 |     ."/* this file is for backward compatibility only */\n"
 | 
  
    | 463 |     ."/* never put any code in this file! */\n"
 | 
  
    | 464 |     ."include_once(dirname(__FILE__).'/framework/initialize.php');\n";
 | 
  
    | 465 | $sSetupFile = WB_PATH.'/config.php';
 | 
  
    | 466 | if(!file_put_contents($sSetupFile,$sConfigContent)) {
 | 
  
    | 467 |     set_error("Cannot write to the configuration file ($sSetupFile)");
 | 
  
    | 468 | }
 | 
  
    | 469 | $sSecMod = (defined('SECURE_FORM_MODULE') && SECURE_FORM_MODULE != '') ? '.'.SECURE_FORM_MODULE : '';
 | 
  
    | 470 | $sSecMod = WB_PATH.'/framework/SecureForm'.$sSecMod.'.php';
 | 
  
    | 471 | require_once($sSecMod);
 | 
  
    | 472 | require(ADMIN_PATH.'/interface/version.php');
 | 
  
    | 473 | 
 | 
  
    | 474 | /*****************************
 | 
  
    | 475 | Begin Create Database Tables
 | 
  
    | 476 | *****************************/
 | 
  
    | 477 | $oSqlInst = new SqlImport($database, __DIR__.'/sql/install-struct.sql');
 | 
  
    | 478 | if ($oSqlInst) {
 | 
  
    | 479 |     if (!$oSqlInst->doImport('install')) {
 | 
  
    | 480 |         set_error($oSqlInst->getError());
 | 
  
    | 481 |     }
 | 
  
    | 482 | }
 | 
  
    | 483 | $oSqlInst = new SqlImport($database, __DIR__.'/sql/install-data.sql');
 | 
  
    | 484 | if ($oSqlInst) {
 | 
  
    | 485 |     if (!$oSqlInst->doImport('install')) {
 | 
  
    | 486 |         set_error($oSqlInst->getError());
 | 
  
    | 487 |     }
 | 
  
    | 488 | }
 | 
  
    | 489 | unset($oSqlInst);
 | 
  
    | 490 | $sql = // additional settings from install input
 | 
  
    | 491 |        'REPLACE INTO `'.TABLE_PREFIX.'settings` (`name`, `value`) VALUES '
 | 
  
    | 492 |      .        '(\'wb_version\', \''.VERSION.'\'), '
 | 
  
    | 493 |      .        '(\'website_title\', \''.$website_title.'\'), '
 | 
  
    | 494 |      .        '(\'default_language\', \''.$default_language.'\'), '
 | 
  
    | 495 |      .        '(\'app_name\', \'wb_'.$session_rand.'\'), '
 | 
  
    | 496 |      .        '(\'default_timezone\', \''.$default_timezone.'\'), '
 | 
  
    | 497 |      .        '(\'operating_system\', \''.$operating_system.'\'), '
 | 
  
    | 498 |      .        '(\'string_file_mode\', \''.$file_mode.'\'), '
 | 
  
    | 499 |      .        '(\'string_dir_mode\', \''.$dir_mode.'\'), '
 | 
  
    | 500 |      .        '(\'server_email\', \''.$admin_email.'\'), '
 | 
  
    | 501 |      .        '(\'wb_revision\', \''.REVISION.'\'), '
 | 
  
    | 502 |      .        '(\'wb_sp\', \''.SP.'\'), '
 | 
  
    | 503 |      .        '(\'groups_updated\', \''.time().'\')';
 | 
  
    | 504 | if (! ($database->query($sql))) {
 | 
  
    | 505 |     set_error('unable to write \'install presets\' into table \'settings\'');
 | 
  
    | 506 | }
 | 
  
    | 507 | $sql = // add the Admin user
 | 
  
    | 508 |      'INSERT INTO `'.TABLE_PREFIX.'users` '
 | 
  
    | 509 |     .'SET `user_id`=1, '
 | 
  
    | 510 |     .    '`group_id`=1, '
 | 
  
    | 511 |     .    '`groups_id`=\'1\', '
 | 
  
    | 512 |     .    '`active`=\'1\', '
 | 
  
    | 513 |     .    '`username`=\''.$admin_username.'\', '
 | 
  
    | 514 |     .    '`password`=\''.md5($admin_password).'\', '
 | 
  
    | 515 |     .    '`email`=\''.$admin_email.'\', '
 | 
  
    | 516 |     .    '`timezone`=\''.$default_timezone.'\', '
 | 
  
    | 517 |     .    '`language`=\''.$default_language.'\', '
 | 
  
    | 518 |     .    '`display_name`=\'Administrator\'';
 | 
  
    | 519 | if (! ($database->query($sql))) {
 | 
  
    | 520 |     set_error('unable to write Administrator account into table \'users\'');
 | 
  
    | 521 | }
 | 
  
    | 522 | /**********************
 | 
  
    | 523 | END OF TABLES IMPORT
 | 
  
    | 524 | **********************/
 | 
  
    | 525 | // initialize the system
 | 
  
    | 526 | require_once(WB_PATH.'/framework/initialize.php');
 | 
  
    | 527 | require_once(WB_PATH.'/framework/class.admin.php');
 | 
  
    | 528 | /***********************
 | 
  
    | 529 | // Dummy class to allow modules' install scripts to call $admin->print_error
 | 
  
    | 530 | ***********************/
 | 
  
    | 531 | class admin_dummy extends admin
 | 
  
    | 532 | {
 | 
  
    | 533 |     public $error='';
 | 
  
    | 534 |     public function print_error($message, $link = 'index.php', $auto_footer = true)
 | 
  
    | 535 |     {
 | 
  
    | 536 |         $this->error=$message;
 | 
  
    | 537 |     }
 | 
  
    | 538 | }
 | 
  
    | 539 | // Include WB functions file
 | 
  
    | 540 |     require_once(WB_PATH.'/framework/functions.php');
 | 
  
    | 541 | // Re-connect to the database, this time using in-build database class
 | 
  
    | 542 | //  require_once(WB_PATH.'/framework/class.login.php');
 | 
  
    | 543 |     // Include the PclZip class file (thanks to
 | 
  
    | 544 |     require_once(WB_PATH.'/include/pclzip/pclzip.lib.php');
 | 
  
    | 545 |     // Install add-ons
 | 
  
    | 546 |     $admin=new admin_dummy('Start','',false,false);
 | 
  
    | 547 | // Load addons and templates into DB
 | 
  
    | 548 |     $aScanDirs = array(
 | 
  
    | 549 |         'module'   => dirname(__DIR__).'/modules/',
 | 
  
    | 550 |         'template' => dirname(__DIR__).'/templates/',
 | 
  
    | 551 |         'language' => dirname(__DIR__).'/languages/'
 | 
  
    | 552 |     );
 | 
  
    | 553 |     foreach ($aScanDirs as $sType => $sPath) {
 | 
  
    | 554 |         $sCommand = 'load_'.$sType;
 | 
  
    | 555 |         if ($sType != 'language') {
 | 
  
    | 556 |             foreach (glob($sPath, GLOB_ONLYDIR) as $sMatchingPath) {
 | 
  
    | 557 |                 if ($sType == 'module') {
 | 
  
    | 558 |                     $sCommand($sMatchingPath, true);
 | 
  
    | 559 |                     if ($admin->error) { set_error($admin->error); }
 | 
  
    | 560 |                 } elseif ($sType == 'template') {
 | 
  
    | 561 |                     $sCommand($sMatchingPath);
 | 
  
    | 562 |                 }
 | 
  
    | 563 |             }
 | 
  
    | 564 |         } else {
 | 
  
    | 565 |             foreach (glob(dirname(__DIR__).'/languages/??.php') as $sMatchingPath) {
 | 
  
    | 566 |                 if (preg_match('/\/[A-Z]{2}\.php$/sU', $sMatchingPath)) {
 | 
  
    | 567 |                     $sCommand($sMatchingPath);
 | 
  
    | 568 |                 }
 | 
  
    | 569 |             }
 | 
  
    | 570 |         }
 | 
  
    | 571 |     }
 | 
  
    | 572 | 
 | 
  
    | 573 | // Check if there was a database error
 | 
  
    | 574 |     if($database->is_error()) {
 | 
  
    | 575 |         set_error($database->get_error());
 | 
  
    | 576 |     }
 | 
  
    | 577 | 
 | 
  
    | 578 |     if ( sizeof(createFolderProtectFile( WB_PATH.MEDIA_DIRECTORY )) ) {  }
 | 
  
    | 579 |     if ( sizeof(createFolderProtectFile( WB_PATH.MEDIA_DIRECTORY.'/home' )) ) {  }
 | 
  
    | 580 |     if ( sizeof(createFolderProtectFile( WB_PATH.PAGES_DIRECTORY )) ) {  }
 | 
  
    | 581 | 
 | 
  
    | 582 | // end of if install_tables
 | 
  
    | 583 | 
 | 
  
    | 584 | $ThemeUrl = WB_URL.$admin->correct_theme_source('warning.html');
 | 
  
    | 585 | // Setup template object, parse vars to it, then parse it
 | 
  
    | 586 | $ThemePath = realpath(WB_PATH.$admin->correct_theme_source('loginBox.htt'));
 | 
  
    | 587 | 
 | 
  
    | 588 | // Log the user in and go to Website Baker Administration
 | 
  
    | 589 | $thisApp = new Login(
 | 
  
    | 590 |     array(
 | 
  
    | 591 |             "MAX_ATTEMPS" => "50",
 | 
  
    | 592 |             "WARNING_URL" => $ThemeUrl."/warning.html",
 | 
  
    | 593 |             "USERNAME_FIELDNAME" => 'admin_username',
 | 
  
    | 594 |             "PASSWORD_FIELDNAME" => 'admin_password',
 | 
  
    | 595 |             "REMEMBER_ME_OPTION" => SMART_LOGIN,
 | 
  
    | 596 |             "MIN_USERNAME_LEN" => "2",
 | 
  
    | 597 |             "MIN_PASSWORD_LEN" => "3",
 | 
  
    | 598 |             "MAX_USERNAME_LEN" => "30",
 | 
  
    | 599 |             "MAX_PASSWORD_LEN" => "30",
 | 
  
    | 600 |             'LOGIN_URL' => ADMIN_URL."/login/index.php",
 | 
  
    | 601 |             'DEFAULT_URL' => ADMIN_URL."/start/index.php",
 | 
  
    | 602 |             'TEMPLATE_DIR' => $ThemePath,
 | 
  
    | 603 |             'TEMPLATE_FILE' => 'loginBox.htt',
 | 
  
    | 604 |             'FRONTEND' => false,
 | 
  
    | 605 |             'FORGOTTEN_DETAILS_APP' => ADMIN_URL."/login/forgot/index.php",
 | 
  
    | 606 |             'USERS_TABLE' => TABLE_PREFIX."users",
 | 
  
    | 607 |             'GROUPS_TABLE' => TABLE_PREFIX."groups",
 | 
  
    | 608 |     )
 | 
  
    | 609 | );
 |