00001 <?php
00002
00003
00007 define('SCHEMA_UNINSTALLED', -1);
00008
00012 define('SCHEMA_INSTALLED', 0);
00013
00017 define('REQUIREMENT_INFO', -1);
00018
00022 define('REQUIREMENT_OK', 0);
00023
00027 define('REQUIREMENT_WARNING', 1);
00028
00032 define('REQUIREMENT_ERROR', 2);
00033
00037 define('FILE_EXIST', 1);
00038
00042 define('FILE_READABLE', 2);
00043
00047 define('FILE_WRITABLE', 4);
00048
00052 define('FILE_EXECUTABLE', 8);
00053
00057 define('FILE_NOT_EXIST', 16);
00058
00062 define('FILE_NOT_READABLE', 32);
00063
00067 define('FILE_NOT_WRITABLE', 64);
00068
00072 define('FILE_NOT_EXECUTABLE', 128);
00073
00077 function drupal_load_updates() {
00078 foreach (drupal_get_installed_schema_version(NULL, FALSE, TRUE) as $module => $schema_version) {
00079 if ($schema_version > -1) {
00080 module_load_install($module);
00081 }
00082 }
00083 }
00084
00094 function drupal_get_schema_versions($module) {
00095 $updates = array();
00096 $functions = get_defined_functions();
00097 foreach ($functions['user'] as $function) {
00098 if (strpos($function, $module . '_update_') === 0) {
00099 $version = substr($function, strlen($module . '_update_'));
00100 if (is_numeric($version)) {
00101 $updates[] = $version;
00102 }
00103 }
00104 }
00105 if (count($updates) == 0) {
00106 return FALSE;
00107 }
00108 return $updates;
00109 }
00110
00124 function drupal_get_installed_schema_version($module, $reset = FALSE, $array = FALSE) {
00125 static $versions = array();
00126
00127 if ($reset) {
00128 $versions = array();
00129 }
00130
00131 if (!$versions) {
00132 $versions = array();
00133 $result = db_query("SELECT name, schema_version FROM {system} WHERE type = '%s'", 'module');
00134 while ($row = db_fetch_object($result)) {
00135 $versions[$row->name] = $row->schema_version;
00136 }
00137 }
00138
00139 return $array ? $versions : $versions[$module];
00140 }
00141
00150 function drupal_set_installed_schema_version($module, $version) {
00151 db_query("UPDATE {system} SET schema_version = %d WHERE name = '%s'", $version, $module);
00152 }
00153
00160 function drupal_install_profile_name() {
00161 global $profile;
00162 static $name = NULL;
00163
00164 if (!isset($name)) {
00165
00166 $function = $profile . '_profile_details';
00167 if (function_exists($function)) {
00168 $details = $function();
00169 }
00170 $name = isset($details['name']) ? $details['name'] : 'Drupal';
00171 }
00172
00173 return $name;
00174 }
00175
00186 function drupal_detect_baseurl($file = 'install.php') {
00187 global $profile;
00188 $proto = $_SERVER['HTTPS'] ? 'https://' : 'http://';
00189 $host = $_SERVER['SERVER_NAME'];
00190 $port = ($_SERVER['SERVER_PORT'] == 80 ? '' : ':' . $_SERVER['SERVER_PORT']);
00191 $uri = preg_replace("/\?.*/", '', $_SERVER['REQUEST_URI']);
00192 $dir = str_replace("/$file", '', $uri);
00193
00194 return "$proto$host$port$dir";
00195 }
00196
00204 function drupal_detect_database_types() {
00205 $databases = array();
00206
00207 foreach (array('mysql', 'mysqli', 'pgsql') as $type) {
00208 if (file_exists('./includes/install.' . $type . '.inc')) {
00209 include_once './includes/install.' . $type . '.inc';
00210 $function = $type . '_is_available';
00211 if ($function()) {
00212 $databases[$type] = $type;
00213 }
00214 }
00215 }
00216
00217 return $databases;
00218 }
00219
00227 function drupal_rewrite_settings($settings = array(), $prefix = '') {
00228 $default_settings = './sites/default/default.settings.php';
00229 $settings_file = './' . conf_path(FALSE, TRUE) . '/' . $prefix . 'settings.php';
00230
00231
00232 $keys = array();
00233 foreach ($settings as $setting => $data) {
00234 $GLOBALS[$setting] = $data['value'];
00235 $keys[] = $setting;
00236 }
00237
00238 $buffer = NULL;
00239 $first = TRUE;
00240 if ($fp = fopen($default_settings, 'r')) {
00241
00242 while (!feof($fp)) {
00243 $line = fgets($fp);
00244 if ($first && substr($line, 0, 5) != '<?php') {
00245 $buffer = "<?php\n\n";
00246 }
00247 $first = FALSE;
00248
00249 if (substr($line, 0, 7) == 'define(') {
00250 preg_match('/define\(\s*[\'"]([A-Z_-]+)[\'"]\s*,(.*?)\);/', $line, $variable);
00251 if (in_array($variable[1], $keys)) {
00252 $setting = $settings[$variable[1]];
00253 $buffer .= str_replace($variable[2], " '" . $setting['value'] . "'", $line);
00254 unset($settings[$variable[1]]);
00255 unset($settings[$variable[2]]);
00256 }
00257 else {
00258 $buffer .= $line;
00259 }
00260 }
00261
00262 elseif (substr($line, 0, 1) == '$') {
00263 preg_match('/\$([^ ]*) /', $line, $variable);
00264 if (in_array($variable[1], $keys)) {
00265
00266
00267 $setting = $settings[$variable[1]];
00268 $buffer .= '$' . $variable[1] . " = '" . $setting['value'] . "';" . (!empty($setting['comment']) ? ' // ' . $setting['comment'] . "\n" : "\n");
00269 unset($settings[$variable[1]]);
00270 }
00271 else {
00272 $buffer .= $line;
00273 }
00274 }
00275 else {
00276 $buffer .= $line;
00277 }
00278 }
00279 fclose($fp);
00280
00281
00282 foreach ($settings as $setting => $data) {
00283 if ($data['required']) {
00284 $buffer .= "\$$setting = '" . $data['value'] . "';\n";
00285 }
00286 }
00287
00288 $fp = fopen($settings_file, 'w');
00289 if ($fp && fwrite($fp, $buffer) === FALSE) {
00290 drupal_set_message(st('Failed to modify %settings, please verify the file permissions.', array('%settings' => $settings_file)), 'error');
00291 }
00292 }
00293 else {
00294 drupal_set_message(st('Failed to open %settings, please verify the file permissions.', array('%settings' => $default_settings)), 'error');
00295 }
00296 }
00297
00304 function drupal_get_install_files($module_list = array()) {
00305 $installs = array();
00306 foreach ($module_list as $module) {
00307 $installs = array_merge($installs, drupal_system_listing($module . '.install$', 'modules'));
00308 }
00309 return $installs;
00310 }
00311
00322 function drupal_verify_profile($profile, $locale) {
00323 include_once './includes/file.inc';
00324 include_once './includes/common.inc';
00325
00326 $profile_file = "./profiles/$profile/$profile.profile";
00327
00328 if (!isset($profile) || !file_exists($profile_file)) {
00329 install_no_profile_error();
00330 }
00331
00332 require_once($profile_file);
00333
00334
00335 $function = $profile . '_profile_modules';
00336 $module_list = array_merge(drupal_required_modules(), $function(), ($locale != 'en' ? array('locale') : array()));
00337
00338
00339 $present_modules = array();
00340 foreach (drupal_system_listing('\.module$', 'modules', 'name', 0) as $present_module) {
00341 $present_modules[] = $present_module->name;
00342 }
00343
00344
00345 $missing_modules = array_diff($module_list, $present_modules);
00346 if (count($missing_modules)) {
00347 foreach ($missing_modules as $module) {
00348 drupal_set_message(st('The %module module is required but was not found. Please move it into the <em>modules</em> subdirectory.', array('%module' => $module)), 'error');
00349 }
00350 }
00351 else {
00352 return $module_list;
00353 }
00354 }
00355
00363 function drupal_install_modules($module_list = array()) {
00364 $files = module_rebuild_cache();
00365 $module_list = array_flip(array_values($module_list));
00366 do {
00367 $moved = FALSE;
00368 foreach ($module_list as $module => $weight) {
00369 $file = $files[$module];
00370 if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) {
00371 foreach ($file->info['dependencies'] as $dependency) {
00372 if (isset($module_list[$dependency]) && $module_list[$module] < $module_list[$dependency] +1) {
00373 $module_list[$module] = $module_list[$dependency] +1;
00374 $moved = TRUE;
00375 }
00376 }
00377 }
00378 }
00379 } while ($moved);
00380 asort($module_list);
00381 $module_list = array_keys($module_list);
00382 array_filter($module_list, '_drupal_install_module');
00383 module_enable($module_list);
00384 }
00385
00393 function _drupal_install_module($module) {
00394 if (drupal_get_installed_schema_version($module, TRUE) == SCHEMA_UNINSTALLED) {
00395 module_load_install($module);
00396 module_invoke($module, 'install');
00397 $versions = drupal_get_schema_versions($module);
00398 drupal_set_installed_schema_version($module, $versions ? max($versions) : SCHEMA_INSTALLED);
00399 return TRUE;
00400 }
00401 }
00402
00409 function drupal_install_system() {
00410 $system_path = dirname(drupal_get_filename('module', 'system', NULL));
00411 require_once './' . $system_path . '/system.install';
00412 module_invoke('system', 'install');
00413 $system_versions = drupal_get_schema_versions('system');
00414 $system_version = $system_versions ? max($system_versions) : SCHEMA_INSTALLED;
00415 db_query("INSERT INTO {system} (filename, name, type, owner, status, bootstrap, schema_version) VALUES('%s', '%s', '%s', '%s', %d, %d, %d)", $system_path . '/system.module', 'system', 'module', '', 1, 0, $system_version);
00416
00417 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
00418 module_rebuild_cache();
00419 }
00420
00421
00428 function drupal_uninstall_module($module) {
00429
00430 drupal_load('module', $module);
00431 $paths = module_invoke($module, 'menu');
00432
00433
00434 module_load_install($module);
00435 module_invoke($module, 'uninstall');
00436
00437
00438 if (!empty($paths)) {
00439 $paths = array_keys($paths);
00440
00441 foreach ($paths as $index => $path) {
00442 $parts = explode('/', $path, MENU_MAX_PARTS);
00443 foreach ($parts as $k => $part) {
00444 if (preg_match('/^%[a-z_]*$/', $part)) {
00445 $parts[$k] = '%';
00446 }
00447 }
00448 $paths[$index] = implode('/', $parts);
00449 }
00450 $placeholders = implode(', ', array_fill(0, count($paths), "'%s'"));
00451
00452 $result = db_query('SELECT * FROM {menu_links} WHERE router_path IN (' . $placeholders . ') AND external = 0 ORDER BY depth DESC', $paths);
00453
00454
00455 while ($item = db_fetch_array($result)) {
00456 _menu_delete_item($item, TRUE);
00457 }
00458 }
00459
00460 drupal_set_installed_schema_version($module, SCHEMA_UNINSTALLED);
00461 }
00462
00475 function drupal_verify_install_file($file, $mask = NULL, $type = 'file') {
00476 $return = TRUE;
00477
00478 if (isset($mask) && ($mask & FILE_NOT_EXIST) && file_exists($file)) {
00479 return FALSE;
00480 }
00481
00482 if (isset($type) && file_exists($file)) {
00483 $check = 'is_' . $type;
00484 if (!function_exists($check) || !$check($file)) {
00485 $return = FALSE;
00486 }
00487 }
00488
00489
00490 if (isset($mask)) {
00491 $masks = array(FILE_EXIST, FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE);
00492 foreach ($masks as $current_mask) {
00493 if ($mask & $current_mask) {
00494 switch ($current_mask) {
00495 case FILE_EXIST:
00496 if (!file_exists($file)) {
00497 if ($type == 'dir') {
00498 drupal_install_mkdir($file, $mask);
00499 }
00500 if (!file_exists($file)) {
00501 $return = FALSE;
00502 }
00503 }
00504 break;
00505 case FILE_READABLE:
00506 if (!is_readable($file) && !drupal_install_fix_file($file, $mask)) {
00507 $return = FALSE;
00508 }
00509 break;
00510 case FILE_WRITABLE:
00511 if (!is_writable($file) && !drupal_install_fix_file($file, $mask)) {
00512 $return = FALSE;
00513 }
00514 break;
00515 case FILE_EXECUTABLE:
00516 if (!is_executable($file) && !drupal_install_fix_file($file, $mask)) {
00517 $return = FALSE;
00518 }
00519 break;
00520 case FILE_NOT_READABLE:
00521 if (is_readable($file) && !drupal_install_fix_file($file, $mask)) {
00522 $return = FALSE;
00523 }
00524 break;
00525 case FILE_NOT_WRITABLE:
00526 if (is_writable($file) && !drupal_install_fix_file($file, $mask)) {
00527 $return = FALSE;
00528 }
00529 break;
00530 case FILE_NOT_EXECUTABLE:
00531 if (is_executable($file) && !drupal_install_fix_file($file, $mask)) {
00532 $return = FALSE;
00533 }
00534 break;
00535 }
00536 }
00537 }
00538 }
00539 return $return;
00540 }
00541
00555 function drupal_install_mkdir($file, $mask, $message = TRUE) {
00556 $mod = 0;
00557 $masks = array(FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE);
00558 foreach ($masks as $m) {
00559 if ($mask & $m) {
00560 switch ($m) {
00561 case FILE_READABLE:
00562 $mod += 444;
00563 break;
00564 case FILE_WRITABLE:
00565 $mod += 222;
00566 break;
00567 case FILE_EXECUTABLE:
00568 $mod += 111;
00569 break;
00570 }
00571 }
00572 }
00573
00574 if (@mkdir($file, intval("0$mod", 8))) {
00575 return TRUE;
00576 }
00577 else {
00578 return FALSE;
00579 }
00580 }
00581
00604 function drupal_install_fix_file($file, $mask, $message = TRUE) {
00605 $mod = fileperms($file) & 0777;
00606 $masks = array(FILE_READABLE, FILE_WRITABLE, FILE_EXECUTABLE, FILE_NOT_READABLE, FILE_NOT_WRITABLE, FILE_NOT_EXECUTABLE);
00607
00608
00609
00610
00611
00612 foreach ($masks as $m) {
00613 if ($mask & $m) {
00614 switch ($m) {
00615 case FILE_READABLE:
00616 if (!is_readable($file)) {
00617 $mod |= 0444;
00618 }
00619 break;
00620 case FILE_WRITABLE:
00621 if (!is_writable($file)) {
00622 $mod |= 0222;
00623 }
00624 break;
00625 case FILE_EXECUTABLE:
00626 if (!is_executable($file)) {
00627 $mod |= 0111;
00628 }
00629 break;
00630 case FILE_NOT_READABLE:
00631 if (is_readable($file)) {
00632 $mod &= ~0444;
00633 }
00634 break;
00635 case FILE_NOT_WRITABLE:
00636 if (is_writable($file)) {
00637 $mod &= ~0222;
00638 }
00639 break;
00640 case FILE_NOT_EXECUTABLE:
00641 if (is_executable($file)) {
00642 $mod &= ~0111;
00643 }
00644 break;
00645 }
00646 }
00647 }
00648
00649
00650
00651
00652 if (@chmod($file, $mod)) {
00653 return TRUE;
00654 }
00655 else {
00656 return FALSE;
00657 }
00658 }
00659
00660
00668 function install_goto($path) {
00669 global $base_url;
00670 header('Location: ' . $base_url . '/' . $path);
00671 header('Cache-Control: no-cache');
00672 exit();
00673 }
00674
00680 function st($string, $args = array()) {
00681 static $locale_strings = NULL;
00682 global $profile, $install_locale;
00683
00684 if (!isset($locale_strings)) {
00685 $locale_strings = array();
00686 $filename = './profiles/' . $profile . '/translations/' . $install_locale . '.po';
00687 if (file_exists($filename)) {
00688 require_once './includes/locale.inc';
00689 $file = (object) array('filepath' => $filename);
00690 _locale_import_read_po('mem-store', $file);
00691 $locale_strings = _locale_import_one_string('mem-report');
00692 }
00693 }
00694
00695 require_once './includes/theme.inc';
00696
00697 foreach ($args as $key => $value) {
00698 switch ($key[0]) {
00699
00700 case '@':
00701 $args[$key] = check_plain($value);
00702 break;
00703
00704 case '%':
00705 default:
00706 $args[$key] = '<em>' . check_plain($value) . '</em>';
00707 break;
00708
00709 case '!':
00710 }
00711 }
00712 return strtr((!empty($locale_strings[$string]) ? $locale_strings[$string] : $string), $args);
00713 }
00714
00721 function drupal_check_profile($profile) {
00722 include_once './includes/file.inc';
00723
00724 $profile_file = "./profiles/$profile/$profile.profile";
00725
00726 if (!isset($profile) || !file_exists($profile_file)) {
00727 install_no_profile_error();
00728 }
00729
00730 require_once($profile_file);
00731
00732
00733 $function = $profile . '_profile_modules';
00734 $module_list = array_unique(array_merge(drupal_required_modules(), $function()));
00735
00736
00737 $installs = drupal_get_install_files($module_list);
00738
00739
00740 $requirements = array();
00741 foreach ($installs as $install) {
00742 require_once $install->filename;
00743 $function = $install->name. '_requirements';
00744 if (function_exists($function)) {
00745 $requirements = array_merge($requirements, $function('install'));
00746 }
00747 }
00748 return $requirements;
00749 }
00750
00754 function drupal_requirements_severity(&$requirements) {
00755 $severity = REQUIREMENT_OK;
00756 foreach ($requirements as $requirement) {
00757 if (isset($requirement['severity'])) {
00758 $severity = max($severity, $requirement['severity']);
00759 }
00760 }
00761 return $severity;
00762 }
00763
00767 function drupal_check_module($module) {
00768
00769 $install = drupal_get_install_files(array($module));
00770 if (isset($install[$module])) {
00771 require_once $install[$module]->filename;
00772
00773
00774 $requirements = module_invoke($module, 'requirements', 'install');
00775 if (is_array($requirements) && drupal_requirements_severity($requirements) == REQUIREMENT_ERROR) {
00776
00777 foreach ($requirements as $requirement) {
00778 if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
00779 $message = $requirement['description'];
00780 if (isset($requirement['value']) && $requirement['value']) {
00781 $message .= ' (' . t('Currently using !item !version', array('!item' => $requirement['title'], '!version' => $requirement['value'])) . ')';
00782 }
00783 drupal_set_message($message, 'error');
00784 }
00785 }
00786 return FALSE;
00787 }
00788 }
00789 return TRUE;
00790 }