| [ Index ] |
PHP Cross Reference of Zikula Core 1.3.2 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Copyright Zikula Foundation 2009 - Zikula Application Framework 4 * 5 * This work is contributed to the Zikula Foundation under one or more 6 * Contributor Agreements and licensed to You under the following license: 7 * 8 * @license GNU/LGPLv3 (or at your option, any later version). 9 * @package Installer 10 * 11 * Please see the NOTICE file distributed with this source code for further 12 * information regarding copyright and licensing. 13 */ 14 15 ini_set('memory_limit', '84M'); 16 ini_set('max_execution_time', 300); 17 18 /** 19 * Install controller. 20 * 21 * @return void 22 */ 23 function install(Zikula_Core $core) 24 { 25 define('_ZINSTALLVER', Zikula_Core::VERSION_NUM); 26 27 $serviceManager = $core->getServiceManager(); 28 $eventManager = $core->getEventManager(); 29 30 // Lazy load DB connection to avoid testing DSNs that are not yet valid (e.g. no DB created yet) 31 $dbEvent = new Zikula_Event('doctrine.init_connection', null, array('lazy' => true)); 32 $eventManager->notify($dbEvent); 33 34 $core->init(Zikula_Core::STAGE_ALL & ~Zikula_Core::STAGE_THEME & ~Zikula_Core::STAGE_MODS & ~Zikula_Core::STAGE_LANGS & ~Zikula_Core::STAGE_DECODEURLS & ~Zikula_Core::STAGE_SESSIONS); 35 36 // Power users might have moved the temp folder out of the root and changed the config.php 37 // accordingly. Make sure we respect this security related settings 38 $tempDir = (isset($GLOBALS['ZConfig']['System']['temp']) ? $GLOBALS['ZConfig']['System']['temp'] : 'ztemp'); 39 40 // define our smarty object 41 $smarty = new Smarty(); 42 $smarty->caching = false; 43 $smarty->compile_check = true; 44 $smarty->left_delimiter = '{'; 45 $smarty->right_delimiter = '}'; 46 $smarty->compile_dir = $tempDir . '/view_compiled'; 47 $smarty->template_dir = 'install/templates'; 48 $smarty->plugins_dir = array( 49 'plugins', 50 'install/templates/plugins', 51 ); 52 $smarty->clear_compiled_tpl(); 53 file_put_contents("$tempDir/view_compiled/index.html", ''); 54 55 $lang = FormUtil::getPassedValue('lang', '', 'GETPOST'); 56 $dbhost = FormUtil::getPassedValue('dbhost', '', 'GETPOST'); 57 $dbusername = FormUtil::getPassedValue('dbusername', '', 'GETPOST'); 58 $dbpassword = FormUtil::getPassedValue('dbpassword', '', 'GETPOST'); 59 $dbname = FormUtil::getPassedValue('dbname', '', 'GETPOST'); 60 $dbprefix = ''; 61 $dbdriver = FormUtil::getPassedValue('dbdriver', '', 'GETPOST'); 62 $dbtabletype = FormUtil::getPassedValue('dbtabletype', '', 'GETPOST'); 63 $username = FormUtil::getPassedValue('username', '', 'POST'); 64 $password = FormUtil::getPassedValue('password', '', 'POST'); 65 $repeatpassword = FormUtil::getPassedValue('repeatpassword', '', 'POST'); 66 $email = FormUtil::getPassedValue('email', '', 'GETPOST'); 67 $action = FormUtil::getPassedValue('action', '', 'GETPOST'); 68 69 $notinstalled = isset($_GET['notinstalled']); 70 $installedState = (isset($GLOBALS['ZConfig']['System']['installed']) ? $GLOBALS['ZConfig']['System']['installed'] : 0); 71 72 // If somehow we are browsing the not installed page but installed, redirect back to homepage 73 if ($installedState && $notinstalled) { 74 return System::redirect(System::getHomepageUrl()); 75 } 76 77 // see if the language was already selected 78 $languageAlreadySelected = ($lang) ? true : false; 79 if (!$notinstalled && $languageAlreadySelected && empty($action)) { 80 return System::redirect(System::getBaseUri() . "/install.php?action=requirements&lang=$lang"); 81 } 82 83 // see if the language was already selected 84 $languageAlreadySelected = ($lang) ? true : false; 85 if (!$notinstalled && $languageAlreadySelected && empty($action)) { 86 return System::redirect(System::getBaseUri() . "/install.php?action=requirements&lang=$lang"); 87 } 88 89 // load the installer language files 90 if (!$notinstalled && empty($lang)) { 91 $available = ZLanguage::getInstalledLanguages(); 92 $detector = new ZLanguageBrowser($available); 93 $lang = $detector->discover(); 94 } elseif ($notinstalled) { 95 $installerConfig = array('language' => 'en'); 96 if (is_readable('config/installer.ini')) { 97 $test = parse_ini_file('config/installer.ini'); 98 $installerConfig = isset($test['language']) ? $test : $installerConfig; 99 } 100 $lang = DataUtil::formatForDisplay($installerConfig['language']); 101 } 102 103 // setup multilingual 104 $GLOBALS['ZConfig']['System']['language_i18n'] = $lang; 105 $GLOBALS['ZConfig']['System']['multilingual'] = true; 106 $GLOBALS['ZConfig']['System']['languageurl'] = true; 107 $GLOBALS['ZConfig']['System']['language_detect'] = false; 108 $serviceManager->loadArguments($GLOBALS['ZConfig']['System']); 109 110 $_lang = ZLanguage::getInstance(); 111 $_lang->setup(); 112 113 $lang = ZLanguage::getLanguageCode(); 114 115 $installbySQL = (file_exists("install/sql/custom-$lang.sql") ? "install/sql/custom-$lang.sql" : false); 116 117 $smarty->assign('lang', $lang); 118 $smarty->assign('installbySQL', $installbySQL); 119 $smarty->assign('langdirection', ZLanguage::getDirection()); 120 $smarty->assign('charset', ZLanguage::getEncoding()); 121 122 // show not installed case 123 if ($notinstalled) { 124 header('HTTP/1.1 503 Service Unavailable'); 125 $smarty->display('notinstalled.tpl'); 126 $smarty->clear_compiled_tpl(); 127 file_put_contents("$tempDir/view_compiled/index.html", ''); 128 exit; 129 } 130 131 // assign the values from config.php 132 $smarty->assign($GLOBALS['ZConfig']['System']); 133 134 // if the system is already installed, halt. 135 if ($GLOBALS['ZConfig']['System']['installed']) { 136 _installer_alreadyinstalled($smarty); 137 } 138 139 // check for an empty action - if so then show the first installer page 140 if (empty($action)) { 141 $action = 'lang'; 142 } 143 144 // perform tasks based on our action 145 switch ($action) { 146 case 'processBDInfo': 147 $dbname = trim($dbname); 148 $dbusername = trim($dbusername); 149 if (empty($dbname) || empty($dbusername)) { 150 $action = 'dbinformation'; 151 $smarty->assign('dbconnectmissing', true); 152 } elseif (!preg_match('/^[\w-]*$/', $dbname) || 153 strlen($dbname) > 64) { 154 $action = 'dbinformation'; 155 $smarty->assign('dbinvalidname', true); 156 } else { 157 update_config_php($dbhost, $dbusername, $dbpassword, $dbname, $dbdriver, $dbtabletype); 158 update_installed_status(0); 159 try { 160 $dbh = new PDO("$dbdriver:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword); 161 } catch (PDOException $e) { 162 $action = 'dbinformation'; 163 $smarty->assign('reason', $e->getMessage()); 164 $smarty->assign('dbconnectfailed', true); 165 } 166 } 167 if ($action != 'dbinformation') { 168 $action = 'createadmin'; 169 } 170 break; 171 172 case 'finish': 173 if ((!$username) || preg_match('/[^\p{L}\p{N}_\.\-]/u', $username)) { 174 $action = 'createadmin'; 175 $smarty->assign('uservalidatefailed', true); 176 $smarty->assign(array( 177 'username' => $username, 178 'password' => $password, 179 'repeatpassword' => $repeatpassword, 180 'email' => $email)); 181 } elseif (mb_strlen($password) < 7) { 182 $action = 'createadmin'; 183 $smarty->assign('badpassword', true); 184 $smarty->assign(array( 185 'username' => $username, 186 'password' => $password, 187 'repeatpassword' => $repeatpassword, 188 'email' => $email)); 189 } elseif ($password !== $repeatpassword) { 190 $action = 'createadmin'; 191 $smarty->assign('passwordcomparefailed', true); 192 $smarty->assign(array( 193 'username' => $username, 194 'password' => $password, 195 'repeatpassword' => $repeatpassword, 196 'email' => $email)); 197 } elseif (!validateMail($email)) { 198 $action = 'createadmin'; 199 $smarty->assign('emailvalidatefailed', true); 200 $smarty->assign(array( 201 'username' => $username, 202 'password' => $password, 203 'repeatpassword' => $repeatpassword, 204 'email' => $email)); 205 } else { 206 $installedOk = false; 207 // if it is the distribution and the process have not failed in a previous step 208 if ($installbySQL) { 209 // checks if exists a previous installation with the same prefix 210 $proceed = true; 211 $dbnameConfig = $GLOBALS['ZConfig']['DBInfo']['databases']['default']['dbname']; 212 $exec = ($dbdriver == 'mysql' || $dbdriver == 'mysqli') ? 213 "SHOW TABLES FROM `$dbnameConfig` LIKE '%'" : 214 "SHOW TABLES FROM $dbnameConfig LIKE '%'"; 215 $tables = DBUtil::executeSQL($exec); 216 if ($tables->rowCount() > 0) { 217 $proceed = false; 218 $action = 'dbinformation'; 219 $smarty->assign('dbexists', true); 220 } 221 if ($proceed) { 222 // checks if file exists 223 if (!file_exists($installbySQL)) { 224 $action = 'dbinformation'; 225 $smarty->assign('dbdumpfailed', true); 226 } else { 227 // execute the SQL dump 228 $lines = file($installbySQL); 229 $exec = ''; 230 foreach ($lines as $line_num => $line) { 231 $line = trim($line); 232 if (empty($line) || strpos($line, '--') === 0) 233 continue; 234 $exec .= $line; 235 if (strrpos($line, ';') === strlen($line) - 1) { 236 if (!DBUtil::executeSQL($exec)) { 237 $action = 'dbinformation'; 238 $smarty->assign('dbdumpfailed', true); 239 break; 240 } 241 $exec = ''; 242 } 243 } 244 ModUtil::dbInfoLoad('Users', 'Users'); 245 ModUtil::dbInfoLoad('Extensions', 'Extensions'); 246 ModUtil::initCoreVars(true); 247 createuser($username, $password, $email); 248 $installedOk = true; 249 } 250 } 251 } else { 252 installmodules($lang); 253 createuser($username, $password, $email); 254 $installedOk = true; 255 } 256 257 if ($installedOk) { 258 259 // create our new site admin 260 // TODO: Email username/password to administrator email address. Cannot use ModUtil::apiFunc for this. 261 $serviceManager->getService('session')->start(); 262 263 $authenticationInfo = array( 264 'login_id' => $username, 265 'pass' => $password 266 ); 267 $authenticationMethod = array( 268 'modname' => 'Users', 269 'method' => 'uname', 270 ); 271 UserUtil::loginUsing($authenticationMethod, $authenticationInfo); 272 273 // add admin email as site email 274 System::setVar('adminmail', $email); 275 276 if (!$installbySQL) { 277 Theme_Util::regenerate(); 278 } 279 280 // set site status as installed and protect config.php file 281 update_installed_status(1); 282 @chmod('config/config.php', 0400); 283 if (!is_readable('config/config.php')) { 284 @chmod('config/config.php', 0440); 285 if (!is_readable('config/config.php')) { 286 @chmod('config/config.php', 0444); 287 } 288 } 289 // install all plugins 290 $systemPlugins = PluginUtil::loadAllSystemPlugins(); 291 foreach ($systemPlugins as $plugin) { 292 PluginUtil::install($plugin); 293 } 294 295 LogUtil::registerStatus(__('Congratulations! Zikula has been successfullly installed.')); 296 System::redirect(ModUtil::url('Admin', 'admin', 'adminpanel')); 297 exit; 298 } 299 } 300 break; 301 302 case 'requirements': 303 $checks = _check_requirements(); 304 $ok = true; 305 306 foreach ($checks as $check) { 307 if (!$check) { 308 $ok = false; 309 break; 310 } 311 } 312 313 foreach ($checks['files'] as $check) { 314 if (!$check['writable']) { 315 $ok = false; 316 break; 317 } 318 } 319 if ($ok) { 320 System::redirect(System::getBaseUri() . "/install.php?action=dbinformation&lang=$lang"); 321 exit; 322 } 323 324 $smarty->assign('checks', $checks); 325 326 break; 327 } 328 329 // check our action template exists 330 $action = DataUtil::formatForOS($action); 331 if ($smarty->template_exists("installer_$action.tpl")) { 332 $smarty->assign('action', $action); 333 $templateName = "installer_$action.tpl"; 334 } else { 335 $smarty->assign('action', 'error'); 336 $templateName = 'installer_error.tpl'; 337 } 338 339 $smarty->assign('maincontent', $smarty->fetch($templateName)); 340 $smarty->display('installer_page.tpl'); 341 $smarty->clear_compiled_tpl(); 342 file_put_contents("$tempDir/view_compiled/index.html", ''); 343 } 344 345 /** 346 * This function inserts the default data on new installs 347 */ 348 function createuser($username, $password, $email) 349 { 350 if (!class_exists('Users_Constant')) { 351 require_once 'system/Users/lib/Users/Constant.php'; 352 } 353 $connection = Doctrine_Manager::connection(); 354 355 // get the database connection 356 ModUtil::dbInfoLoad('Users', 'Users'); 357 ModUtil::dbInfoLoad('Extensions', 'Extensions'); 358 $dbtables = DBUtil::getTables(); 359 360 // create the password hash 361 $password = UserUtil::getHashedPassword($password); 362 363 // prepare the data 364 $username = mb_strtolower(DataUtil::formatForStore($username)); 365 $password = DataUtil::formatForStore($password); 366 $email = mb_strtolower(DataUtil::formatForStore($email)); 367 368 $nowUTC = new DateTime(null, new DateTimeZone('UTC')); 369 $nowUTCStr = $nowUTC->format(Users_Constant::DATETIME_FORMAT); 370 371 // create the admin user 372 $sql = "UPDATE {$dbtables['users']} 373 SET uname = '{$username}', 374 email = '{$email}', 375 pass = '{$password}', 376 activated = 1, 377 user_regdate = '{$nowUTCStr}', 378 lastlogin = '{$nowUTCStr}' 379 WHERE uid = 2"; 380 381 $result = DBUtil::executeSQL($sql); 382 383 return ($result) ? true : false; 384 } 385 386 function installmodules($lang = 'en') 387 { 388 $connection = Doctrine_Manager::connection(); 389 390 // Lang validation 391 $lang = DataUtil::formatForOS($lang); 392 393 // create a result set 394 $results = array(); 395 396 $sm = ServiceUtil::getManager(); 397 $em = EventUtil::getManager(); 398 399 $coremodules = array('Extensions', 400 'Settings', 401 'Theme', 402 'Admin', 403 'Permissions', 404 'Groups', 405 'Blocks', 406 'Users', 407 ); 408 409 // manually install the modules module 410 foreach ($coremodules as $coremodule) { 411 $modpath = 'system'; 412 if (is_dir("$modpath/$coremodule/lib")) { 413 ZLoader::addAutoloader($coremodule, "$modpath/$coremodule/lib"); 414 } 415 416 $bootstrap = "$modpath/$coremodule/bootstrap.php"; 417 if (file_exists($bootstrap)) { 418 include_once $bootstrap; 419 } 420 421 ModUtil::dbInfoLoad($coremodule, $coremodule); 422 $className = "{$coremodule}_Installer"; 423 $instance = new $className($sm); 424 if ($instance->install()) { 425 $results[$coremodule] = true; 426 } 427 } 428 429 // regenerate modules list 430 $filemodules = ModUtil::apiFunc('Extensions', 'admin', 'getfilemodules'); 431 ModUtil::apiFunc('Extensions', 'admin', 'regenerate', 432 array('filemodules' => $filemodules)); 433 434 // set each of the core modules to active 435 reset($coremodules); 436 foreach ($coremodules as $coremodule) { 437 $mid = ModUtil::getIdFromName($coremodule, true); 438 ModUtil::apiFunc('Extensions', 'admin', 'setstate', 439 array('id' => $mid, 440 'state' => ModUtil::STATE_INACTIVE)); 441 ModUtil::apiFunc('Extensions', 'admin', 'setstate', 442 array('id' => $mid, 443 'state' => ModUtil::STATE_ACTIVE)); 444 } 445 // Add them to the appropriate category 446 reset($coremodules); 447 448 $coremodscat = array('Extensions' => __('System'), 449 'Permissions' => __('Users'), 450 'Groups' => __('Users'), 451 'Blocks' => __('Layout'), 452 'Users' => __('Users'), 453 'Theme' => __('Layout'), 454 'Admin' => __('System'), 455 'Settings' => __('System')); 456 457 $categories = ModUtil::apiFunc('Admin', 'admin', 'getall'); 458 $modscat = array(); 459 foreach ($categories as $category) { 460 $modscat[$category['catname']] = $category['cid']; 461 } 462 foreach ($coremodules as $coremodule) { 463 $category = $coremodscat[$coremodule]; 464 ModUtil::apiFunc('Admin', 'admin', 'addmodtocategory', 465 array('module' => $coremodule, 466 'category' => $modscat[$category])); 467 } 468 // create the default blocks. 469 $blockInstance = new Blocks_Installer($sm); 470 $blockInstance->defaultdata(); 471 472 // install all the basic modules 473 $modules = array(array('module' => 'SecurityCenter', 474 'category' => __('Security')), 475 array('module' => 'Tour', 476 'category' => __('Content')), 477 array('module' => 'Categories', 478 'category' => __('Content')), 479 array('module' => 'Legal', 480 'category' => __('Content')), 481 array('module' => 'Mailer', 482 'category' => __('System')), 483 array('module' => 'Errors', 484 'category' => __('System')), 485 array('module' => 'Theme', 486 'category' => __('Layout')), 487 array('module' => 'Search', 488 'category' => __('Content'))); 489 490 foreach ($modules as $module) { 491 // sanity check - check if module is already installed 492 if (ModUtil::available($module['module'])) { 493 continue; 494 } 495 $modpath = 'modules'; 496 if (is_dir("$modpath/$module/lib")) { 497 ZLoader::addAutoloader($module, "$modpath/$module/lib"); 498 } 499 $bootstrap = "$modpath/$module/bootstrap.php"; 500 if (file_exists($bootstrap)) { 501 include_once $bootstrap; 502 } 503 504 ZLanguage::bindModuleDomain($module); 505 506 $results[$module['module']] = false; 507 508 // #6048 - prevent trying to install modules which are contained in an install type, but are not available physically 509 if (!file_exists('system/' . $module['module'] . '/') && !file_exists('modules/' . $module['module'] . '/')) { 510 continue; 511 } 512 513 $mid = ModUtil::getIdFromName($module['module']); 514 515 // init it 516 if (ModUtil::apiFunc('Extensions', 'admin', 'initialise', 517 array('id' => $mid)) == true) { 518 // activate it 519 if (ModUtil::apiFunc('Extensions', 'admin', 'setstate', 520 array('id' => $mid, 521 'state' => ModUtil::STATE_ACTIVE))) { 522 $results[$module['module']] = true; 523 } 524 // Set category 525 ModUtil::apiFunc('Admin', 'admin', 'addmodtocategory', 526 array('module' => $module['module'], 527 'category' => $modscat[$module['category']])); 528 } 529 } 530 531 System::setVar('language_i18n', $lang); 532 return $results; 533 } 534 535 function _installer_alreadyinstalled(Smarty $smarty) 536 { 537 header('HTTP/1.1 500 Internal Server Error'); 538 $smarty->display('installer_alreadyinstalled.tpl'); 539 System::shutDown(); 540 exit; 541 } 542 543 function validateMail($mail) 544 { 545 if (!preg_match('/^(?:[^\s\000-\037\177\(\)<>@,;:\\"\[\]]\.?)+@(?:[^\s\000-\037\177\(\)<>@,;:\\\"\[\]]\.?)+\.[a-z]{2,6}$/Ui', $mail)) { 546 return false; 547 } 548 549 return true; 550 } 551 552 function _check_requirements() 553 { 554 $results = array(); 555 556 $x = explode('.', str_replace('-', '.', phpversion())); 557 $phpVersion = "$x[0].$x[1].$x[2]"; 558 $results['phpsatisfied'] = version_compare($phpVersion, Zikula_Core::PHP_MINIMUM_VERSION, ">="); 559 $results['datetimezone'] = ini_get('date.timezone'); 560 561 $results['pdo'] = extension_loaded('pdo'); 562 $results['register_globals'] = !ini_get('register_globals'); 563 $results['magic_quotes_gpc'] = !ini_get('magic_quotes_gpc'); 564 $results['phptokens'] = function_exists('token_get_all'); 565 $results['mbstring'] = function_exists('mb_get_info'); 566 $isEnabled = @preg_match('/^\p{L}+$/u', 'TheseAreLetters'); 567 $results['pcreUnicodePropertiesEnabled'] = (isset($isEnabled) && (bool)$isEnabled); 568 $results['json_encode'] = function_exists('json_encode'); 569 $temp = (isset($GLOBALS['ZConfig']['System']['temp']) ? $GLOBALS['ZConfig']['System']['temp'] : 'ztemp'); 570 $datadir = (isset($GLOBALS['ZConfig']['System']['datadir']) ? $GLOBALS['ZConfig']['System']['datadir'] : 'data'); 571 $results['config_personal_config_php'] = !is_writable('config/personal_config.php'); 572 $files = array('config/config.php', "$datadir/", "$temp/", "$temp/error_logs/", "$temp/view_compiled/", 573 "$temp/view_cache/", "$temp/Theme_compiled/", "$temp/Theme_cache/", "$temp/Theme_Config/"); 574 $results['files'] = array(); 575 foreach ($files as $file) { 576 $results['files'][] = array('filename' => $file, 'writable' => is_writable($file)); 577 } 578 579 return $results; 580 } 581 582 function _installer_replace_keys($searchKey, $replaceWith, $string) 583 { 584 $search = array("#\['$searchKey'\]\s*=\s*('|\")(.*)('|\")\s*;#", "#\['$searchKey'\]\s*=\s*(\d)\s*;#"); 585 $replace = array("['$searchKey'] = '$replaceWith';", "['$searchKey'] = $replaceWith;"); 586 return preg_replace($search, $replace, $string); 587 } 588 589 function update_config_php($dbhost, $dbusername, $dbpassword, $dbname, $dbdriver, $dbtabletype) 590 { 591 $file = file_get_contents('config/config.php'); 592 $file = _installer_replace_keys('dbname', $dbname, $file); 593 $file = _installer_replace_keys('dbdriver', $dbdriver, $file); 594 $file = _installer_replace_keys('dbtabletype', $dbtabletype, $file); 595 $file = _installer_replace_keys('user', $dbusername, $file); 596 $file = _installer_replace_keys('password', $dbpassword, $file); 597 $file = _installer_replace_keys('host', $dbhost, $file); 598 $file = _installer_replace_keys('dbname', $dbname, $file); 599 file_put_contents('config/config.php', $file); 600 } 601 602 function update_installed_status($state) 603 { 604 $file = _installer_replace_keys('installed', $state, file_get_contents('config/config.php')); 605 file_put_contents('config/config.php', $file); 606 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Feb 20 12:35:30 2012 | Cross-referenced by PHPXref 0.7.1 |