install.core.inc 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. <?php
  2. /**
  3. * @file
  4. * API functions for installing Drupal.
  5. */
  6. /**
  7. * Do not run the task during the current installation request.
  8. *
  9. * This can be used to skip running an installation task when certain
  10. * conditions are met, even though the task may still show on the list of
  11. * installation tasks presented to the user. For example, the Drupal installer
  12. * uses this flag to skip over the database configuration form when valid
  13. * database connection information is already available from settings.php. It
  14. * also uses this flag to skip language import tasks when the installation is
  15. * being performed in English.
  16. */
  17. define('INSTALL_TASK_SKIP', 1);
  18. /**
  19. * Run the task on each installation request until the database is set up.
  20. *
  21. * This is primarily used by the Drupal installer for bootstrap-related tasks.
  22. */
  23. define('INSTALL_TASK_RUN_IF_REACHED', 2);
  24. /**
  25. * Global flag to indicate that a task should be run on each installation
  26. * request that reaches it, until the database is set up and we are able to
  27. * record the fact that it already ran.
  28. *
  29. * This is the default method for running tasks and should be used for most
  30. * tasks that occur after the database is set up; these tasks will then run
  31. * once and be marked complete once they are successfully finished. For
  32. * example, the Drupal installer uses this flag for the batch installation of
  33. * modules on the new site, and also for the configuration form that collects
  34. * basic site information and sets up the site maintenance account.
  35. */
  36. define('INSTALL_TASK_RUN_IF_NOT_COMPLETED', 3);
  37. /**
  38. * Installs Drupal either interactively or via an array of passed-in settings.
  39. *
  40. * The Drupal installation happens in a series of steps, which may be spread
  41. * out over multiple page requests. Each request begins by trying to determine
  42. * the last completed installation step (also known as a "task"), if one is
  43. * available from a previous request. Control is then passed to the task
  44. * handler, which processes the remaining tasks that need to be run until (a)
  45. * an error is thrown, (b) a new page needs to be displayed, or (c) the
  46. * installation finishes (whichever happens first).
  47. *
  48. * @param $settings
  49. * An optional array of installation settings. Leave this empty for a normal,
  50. * interactive, browser-based installation intended to occur over multiple
  51. * page requests. Alternatively, if an array of settings is passed in, the
  52. * installer will attempt to use it to perform the installation in a single
  53. * page request (optimized for the command line) and not send any output
  54. * intended for the web browser. See install_state_defaults() for a list of
  55. * elements that are allowed to appear in this array.
  56. *
  57. * @see install_state_defaults()
  58. */
  59. function install_drupal($settings = array()) {
  60. global $install_state;
  61. // Initialize the installation state with the settings that were passed in,
  62. // as well as a boolean indicating whether or not this is an interactive
  63. // installation.
  64. $interactive = empty($settings);
  65. $install_state = $settings + array('interactive' => $interactive) + install_state_defaults();
  66. try {
  67. // Begin the page request. This adds information about the current state of
  68. // the Drupal installation to the passed-in array.
  69. install_begin_request($install_state);
  70. // Based on the installation state, run the remaining tasks for this page
  71. // request, and collect any output.
  72. $output = install_run_tasks($install_state);
  73. }
  74. catch (Exception $e) {
  75. // When an installation error occurs, either send the error to the web
  76. // browser or pass on the exception so the calling script can use it.
  77. if ($install_state['interactive']) {
  78. install_display_output($e->getMessage(), $install_state);
  79. }
  80. else {
  81. throw $e;
  82. }
  83. }
  84. // All available tasks for this page request are now complete. Interactive
  85. // installations can send output to the browser or redirect the user to the
  86. // next page.
  87. if ($install_state['interactive']) {
  88. if ($install_state['parameters_changed']) {
  89. // Redirect to the correct page if the URL parameters have changed.
  90. install_goto(install_redirect_url($install_state));
  91. }
  92. elseif (isset($output)) {
  93. // Display a page only if some output is available. Otherwise it is
  94. // possible that we are printing a JSON page and theme output should
  95. // not be shown.
  96. install_display_output($output, $install_state);
  97. }
  98. }
  99. }
  100. /**
  101. * Returns an array of default settings for the global installation state.
  102. *
  103. * The installation state is initialized with these settings at the beginning
  104. * of each page request. They may evolve during the page request, but they are
  105. * initialized again once the next request begins.
  106. *
  107. * Non-interactive Drupal installations can override some of these default
  108. * settings by passing in an array to the installation script, most notably
  109. * 'parameters' (which contains one-time parameters such as 'profile' and
  110. * 'locale' that are normally passed in via the URL) and 'forms' (which can
  111. * be used to programmatically submit forms during the installation; the keys
  112. * of each element indicate the name of the installation task that the form
  113. * submission is for, and the values are used as the $form_state['values']
  114. * array that is passed on to the form submission via drupal_form_submit()).
  115. *
  116. * @see drupal_form_submit()
  117. */
  118. function install_state_defaults() {
  119. $defaults = array(
  120. // The current task being processed.
  121. 'active_task' => NULL,
  122. // The last task that was completed during the previous installation
  123. // request.
  124. 'completed_task' => NULL,
  125. // This becomes TRUE only when Drupal's system module is installed.
  126. 'database_tables_exist' => FALSE,
  127. // An array of forms to be programmatically submitted during the
  128. // installation. The keys of each element indicate the name of the
  129. // installation task that the form submission is for, and the values are
  130. // used as the $form_state['values'] array that is passed on to the form
  131. // submission via drupal_form_submit().
  132. 'forms' => array(),
  133. // This becomes TRUE only at the end of the installation process, after
  134. // all available tasks have been completed and Drupal is fully installed.
  135. // It is used by the installer to store correct information in the database
  136. // about the completed installation, as well as to inform theme functions
  137. // that all tasks are finished (so that the task list can be displayed
  138. // correctly).
  139. 'installation_finished' => FALSE,
  140. // Whether or not this installation is interactive. By default this will
  141. // be set to FALSE if settings are passed in to install_drupal().
  142. 'interactive' => TRUE,
  143. // An array of available languages for the installation.
  144. 'locales' => array(),
  145. // An array of parameters for the installation, pre-populated by the URL
  146. // or by the settings passed in to install_drupal(). This is primarily
  147. // used to store 'profile' (the name of the chosen installation profile)
  148. // and 'locale' (the name of the chosen installation language), since
  149. // these settings need to persist from page request to page request before
  150. // the database is available for storage.
  151. 'parameters' => array(),
  152. // Whether or not the parameters have changed during the current page
  153. // request. For interactive installations, this will trigger a page
  154. // redirect.
  155. 'parameters_changed' => FALSE,
  156. // An array of information about the chosen installation profile. This will
  157. // be filled in based on the profile's .info file.
  158. 'profile_info' => array(),
  159. // An array of available installation profiles.
  160. 'profiles' => array(),
  161. // An array of server variables that will be substituted into the global
  162. // $_SERVER array via drupal_override_server_variables(). Used by
  163. // non-interactive installations only.
  164. 'server' => array(),
  165. // This becomes TRUE only when a valid database connection can be
  166. // established.
  167. 'settings_verified' => FALSE,
  168. // Installation tasks can set this to TRUE to force the page request to
  169. // end (even if there is no themable output), in the case of an interactive
  170. // installation. This is needed only rarely; for example, it would be used
  171. // by an installation task that prints JSON output rather than returning a
  172. // themed page. The most common example of this is during batch processing,
  173. // but the Drupal installer automatically takes care of setting this
  174. // parameter properly in that case, so that individual installation tasks
  175. // which implement the batch API do not need to set it themselves.
  176. 'stop_page_request' => FALSE,
  177. // Installation tasks can set this to TRUE to indicate that the task should
  178. // be run again, even if it normally wouldn't be. This can be used, for
  179. // example, if a single task needs to be spread out over multiple page
  180. // requests, or if it needs to perform some validation before allowing
  181. // itself to be marked complete. The most common examples of this are batch
  182. // processing and form submissions, but the Drupal installer automatically
  183. // takes care of setting this parameter properly in those cases, so that
  184. // individual installation tasks which implement the batch API or form API
  185. // do not need to set it themselves.
  186. 'task_not_complete' => FALSE,
  187. // A list of installation tasks which have already been performed during
  188. // the current page request.
  189. 'tasks_performed' => array(),
  190. );
  191. return $defaults;
  192. }
  193. /**
  194. * Begins an installation request, modifying the installation state as needed.
  195. *
  196. * This function performs commands that must run at the beginning of every page
  197. * request. It throws an exception if the installation should not proceed.
  198. *
  199. * @param $install_state
  200. * An array of information about the current installation state. This is
  201. * modified with information gleaned from the beginning of the page request.
  202. */
  203. function install_begin_request(&$install_state) {
  204. // Add any installation parameters passed in via the URL.
  205. $install_state['parameters'] += $_GET;
  206. // Validate certain core settings that are used throughout the installation.
  207. if (!empty($install_state['parameters']['profile'])) {
  208. $install_state['parameters']['profile'] = preg_replace('/[^a-zA-Z_0-9]/', '', $install_state['parameters']['profile']);
  209. }
  210. if (!empty($install_state['parameters']['locale'])) {
  211. $install_state['parameters']['locale'] = preg_replace('/[^a-zA-Z_0-9\-]/', '', $install_state['parameters']['locale']);
  212. }
  213. // Allow command line scripts to override server variables used by Drupal.
  214. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  215. if (!$install_state['interactive']) {
  216. drupal_override_server_variables($install_state['server']);
  217. }
  218. // The user agent header is used to pass a database prefix in the request when
  219. // running tests. However, for security reasons, it is imperative that no
  220. // installation be permitted using such a prefix.
  221. if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], "simpletest") !== FALSE) {
  222. header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
  223. exit;
  224. }
  225. drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
  226. // This must go after drupal_bootstrap(), which unsets globals!
  227. global $conf;
  228. require_once DRUPAL_ROOT . '/modules/system/system.install';
  229. require_once DRUPAL_ROOT . '/includes/common.inc';
  230. require_once DRUPAL_ROOT . '/includes/file.inc';
  231. require_once DRUPAL_ROOT . '/includes/install.inc';
  232. require_once DRUPAL_ROOT . '/' . variable_get('path_inc', 'includes/path.inc');
  233. // Load module basics (needed for hook invokes).
  234. include_once DRUPAL_ROOT . '/includes/module.inc';
  235. include_once DRUPAL_ROOT . '/includes/session.inc';
  236. // Set up $language, so t() caller functions will still work.
  237. drupal_language_initialize();
  238. include_once DRUPAL_ROOT . '/includes/entity.inc';
  239. require_once DRUPAL_ROOT . '/includes/ajax.inc';
  240. $module_list['system']['filename'] = 'modules/system/system.module';
  241. $module_list['user']['filename'] = 'modules/user/user.module';
  242. module_list(TRUE, FALSE, FALSE, $module_list);
  243. drupal_load('module', 'system');
  244. drupal_load('module', 'user');
  245. // Load the cache infrastructure using a "fake" cache implementation that
  246. // does not attempt to write to the database. We need this during the initial
  247. // part of the installer because the database is not available yet. We
  248. // continue to use it even when the database does become available, in order
  249. // to preserve consistency between interactive and command-line installations
  250. // (the latter complete in one page request and therefore are forced to
  251. // continue using the cache implementation they started with) and also
  252. // because any data put in the cache during the installer is inherently
  253. // suspect, due to the fact that Drupal is not fully set up yet.
  254. require_once DRUPAL_ROOT . '/includes/cache.inc';
  255. require_once DRUPAL_ROOT . '/includes/cache-install.inc';
  256. $conf['cache_default_class'] = 'DrupalFakeCache';
  257. // Prepare for themed output. We need to run this at the beginning of the
  258. // page request to avoid a different theme accidentally getting set. (We also
  259. // need to run it even in the case of command-line installations, to prevent
  260. // any code in the installer that happens to initialize the theme system from
  261. // accessing the database before it is set up yet.)
  262. drupal_maintenance_theme();
  263. // Check existing settings.php.
  264. $install_state['settings_verified'] = install_verify_settings();
  265. if ($install_state['settings_verified']) {
  266. // Initialize the database system. Note that the connection
  267. // won't be initialized until it is actually requested.
  268. require_once DRUPAL_ROOT . '/includes/database/database.inc';
  269. // Verify the last completed task in the database, if there is one.
  270. $task = install_verify_completed_task();
  271. }
  272. else {
  273. $task = NULL;
  274. // Do not install over a configured settings.php. Check the 'db_url'
  275. // variable in addition to 'databases', since previous versions of Drupal
  276. // used that (and we do not want to allow installations on an existing site
  277. // whose settings file has not yet been updated).
  278. if (!empty($GLOBALS['databases']) || !empty($GLOBALS['db_url'])) {
  279. throw new Exception(install_already_done_error());
  280. }
  281. }
  282. // Modify the installation state as appropriate.
  283. $install_state['completed_task'] = $task;
  284. $install_state['database_tables_exist'] = !empty($task);
  285. }
  286. /**
  287. * Runs all tasks for the current installation request.
  288. *
  289. * In the case of an interactive installation, all tasks will be attempted
  290. * until one is reached that has output which needs to be displayed to the
  291. * user, or until a page redirect is required. Otherwise, tasks will be
  292. * attempted until the installation is finished.
  293. *
  294. * @param $install_state
  295. * An array of information about the current installation state. This is
  296. * passed along to each task, so it can be modified if necessary.
  297. *
  298. * @return
  299. * HTML output from the last completed task.
  300. */
  301. function install_run_tasks(&$install_state) {
  302. do {
  303. // Obtain a list of tasks to perform. The list of tasks itself can be
  304. // dynamic (e.g., some might be defined by the installation profile,
  305. // which is not necessarily known until the earlier tasks have run),
  306. // so we regenerate the remaining tasks based on the installation state,
  307. // each time through the loop.
  308. $tasks_to_perform = install_tasks_to_perform($install_state);
  309. // Run the first task on the list.
  310. reset($tasks_to_perform);
  311. $task_name = key($tasks_to_perform);
  312. $task = array_shift($tasks_to_perform);
  313. $install_state['active_task'] = $task_name;
  314. $original_parameters = $install_state['parameters'];
  315. $output = install_run_task($task, $install_state);
  316. $install_state['parameters_changed'] = ($install_state['parameters'] != $original_parameters);
  317. // Store this task as having been performed during the current request,
  318. // and save it to the database as completed, if we need to and if the
  319. // database is in a state that allows us to do so. Also mark the
  320. // installation as 'done' when we have run out of tasks.
  321. if (!$install_state['task_not_complete']) {
  322. $install_state['tasks_performed'][] = $task_name;
  323. $install_state['installation_finished'] = empty($tasks_to_perform);
  324. if ($install_state['database_tables_exist'] && ($task['run'] == INSTALL_TASK_RUN_IF_NOT_COMPLETED || $install_state['installation_finished'])) {
  325. variable_set('install_task', $install_state['installation_finished'] ? 'done' : $task_name);
  326. }
  327. }
  328. // Stop when there are no tasks left. In the case of an interactive
  329. // installation, also stop if we have some output to send to the browser,
  330. // the URL parameters have changed, or an end to the page request was
  331. // specifically called for.
  332. $finished = empty($tasks_to_perform) || ($install_state['interactive'] && (isset($output) || $install_state['parameters_changed'] || $install_state['stop_page_request']));
  333. } while (!$finished);
  334. return $output;
  335. }
  336. /**
  337. * Runs an individual installation task.
  338. *
  339. * @param $task
  340. * An array of information about the task to be run.
  341. * @param $install_state
  342. * An array of information about the current installation state. This is
  343. * passed in by reference so that it can be modified by the task.
  344. *
  345. * @return
  346. * The output of the task function, if there is any.
  347. */
  348. function install_run_task($task, &$install_state) {
  349. $function = $task['function'];
  350. if ($task['type'] == 'form') {
  351. require_once DRUPAL_ROOT . '/includes/form.inc';
  352. if ($install_state['interactive']) {
  353. // For interactive forms, build the form and ensure that it will not
  354. // redirect, since the installer handles its own redirection only after
  355. // marking the form submission task complete.
  356. $form_state = array(
  357. // We need to pass $install_state by reference in order for forms to
  358. // modify it, since the form API will use it in call_user_func_array(),
  359. // which requires that referenced variables be passed explicitly.
  360. 'build_info' => array('args' => array(&$install_state)),
  361. 'no_redirect' => TRUE,
  362. );
  363. $form = drupal_build_form($function, $form_state);
  364. // If a successful form submission did not occur, the form needs to be
  365. // rendered, which means the task is not complete yet.
  366. if (empty($form_state['executed'])) {
  367. $install_state['task_not_complete'] = TRUE;
  368. return drupal_render($form);
  369. }
  370. // Otherwise, return nothing so the next task will run in the same
  371. // request.
  372. return;
  373. }
  374. else {
  375. // For non-interactive forms, submit the form programmatically with the
  376. // values taken from the installation state. Throw an exception if any
  377. // errors were encountered.
  378. $form_state = array(
  379. 'values' => !empty($install_state['forms'][$function]) ? $install_state['forms'][$function] : array(),
  380. // We need to pass $install_state by reference in order for forms to
  381. // modify it, since the form API will use it in call_user_func_array(),
  382. // which requires that referenced variables be passed explicitly.
  383. 'build_info' => array('args' => array(&$install_state)),
  384. );
  385. drupal_form_submit($function, $form_state);
  386. $errors = form_get_errors();
  387. if (!empty($errors)) {
  388. throw new Exception(implode("\n", $errors));
  389. }
  390. }
  391. }
  392. elseif ($task['type'] == 'batch') {
  393. // Start a new batch based on the task function, if one is not running
  394. // already.
  395. $current_batch = variable_get('install_current_batch');
  396. if (!$install_state['interactive'] || !$current_batch) {
  397. $batch = $function($install_state);
  398. if (empty($batch)) {
  399. // If the task did some processing and decided no batch was necessary,
  400. // there is nothing more to do here.
  401. return;
  402. }
  403. batch_set($batch);
  404. // For interactive batches, we need to store the fact that this batch
  405. // task is currently running. Otherwise, we need to make sure the batch
  406. // will complete in one page request.
  407. if ($install_state['interactive']) {
  408. variable_set('install_current_batch', $function);
  409. }
  410. else {
  411. $batch =& batch_get();
  412. $batch['progressive'] = FALSE;
  413. }
  414. // Process the batch. For progressive batches, this will redirect.
  415. // Otherwise, the batch will complete.
  416. batch_process(install_redirect_url($install_state), install_full_redirect_url($install_state));
  417. }
  418. // If we are in the middle of processing this batch, keep sending back
  419. // any output from the batch process, until the task is complete.
  420. elseif ($current_batch == $function) {
  421. include_once DRUPAL_ROOT . '/includes/batch.inc';
  422. $output = _batch_page();
  423. // The task is complete when we try to access the batch page and receive
  424. // FALSE in return, since this means we are at a URL where we are no
  425. // longer requesting a batch ID.
  426. if ($output === FALSE) {
  427. // Return nothing so the next task will run in the same request.
  428. variable_del('install_current_batch');
  429. return;
  430. }
  431. else {
  432. // We need to force the page request to end if the task is not
  433. // complete, since the batch API sometimes prints JSON output
  434. // rather than returning a themed page.
  435. $install_state['task_not_complete'] = $install_state['stop_page_request'] = TRUE;
  436. return $output;
  437. }
  438. }
  439. }
  440. else {
  441. // For normal tasks, just return the function result, whatever it is.
  442. return $function($install_state);
  443. }
  444. }
  445. /**
  446. * Returns a list of tasks to perform during the current installation request.
  447. *
  448. * Note that the list of tasks can change based on the installation state as
  449. * the page request evolves (for example, if an installation profile hasn't
  450. * been selected yet, we don't yet know which profile tasks need to be run).
  451. *
  452. * @param $install_state
  453. * An array of information about the current installation state.
  454. *
  455. * @return
  456. * A list of tasks to be performed, with associated metadata.
  457. */
  458. function install_tasks_to_perform($install_state) {
  459. // Start with a list of all currently available tasks.
  460. $tasks = install_tasks($install_state);
  461. foreach ($tasks as $name => $task) {
  462. // Remove any tasks that were already performed or that never should run.
  463. // Also, if we started this page request with an indication of the last
  464. // task that was completed, skip that task and all those that come before
  465. // it, unless they are marked as always needing to run.
  466. if ($task['run'] == INSTALL_TASK_SKIP || in_array($name, $install_state['tasks_performed']) || (!empty($install_state['completed_task']) && empty($completed_task_found) && $task['run'] != INSTALL_TASK_RUN_IF_REACHED)) {
  467. unset($tasks[$name]);
  468. }
  469. if (!empty($install_state['completed_task']) && $name == $install_state['completed_task']) {
  470. $completed_task_found = TRUE;
  471. }
  472. }
  473. return $tasks;
  474. }
  475. /**
  476. * Returns a list of all tasks the installer currently knows about.
  477. *
  478. * This function will return tasks regardless of whether or not they are
  479. * intended to run on the current page request. However, the list can change
  480. * based on the installation state (for example, if an installation profile
  481. * hasn't been selected yet, we don't yet know which profile tasks will be
  482. * available).
  483. *
  484. * @param $install_state
  485. * An array of information about the current installation state.
  486. *
  487. * @return
  488. * A list of tasks, with associated metadata.
  489. */
  490. function install_tasks($install_state) {
  491. // Determine whether translation import tasks will need to be performed.
  492. $needs_translations = count($install_state['locales']) > 1 && !empty($install_state['parameters']['locale']) && $install_state['parameters']['locale'] != 'en';
  493. // Start with the core installation tasks that run before handing control
  494. // to the installation profile.
  495. $tasks = array(
  496. 'install_select_profile' => array(
  497. 'display_name' => st('Choose profile'),
  498. 'display' => count($install_state['profiles']) != 1,
  499. 'run' => INSTALL_TASK_RUN_IF_REACHED,
  500. ),
  501. 'install_select_locale' => array(
  502. 'display_name' => st('Choose language'),
  503. 'run' => INSTALL_TASK_RUN_IF_REACHED,
  504. ),
  505. 'install_load_profile' => array(
  506. 'run' => INSTALL_TASK_RUN_IF_REACHED,
  507. ),
  508. 'install_verify_requirements' => array(
  509. 'display_name' => st('Verify requirements'),
  510. ),
  511. 'install_settings_form' => array(
  512. 'display_name' => st('Set up database'),
  513. 'type' => 'form',
  514. 'run' => $install_state['settings_verified'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED,
  515. ),
  516. 'install_system_module' => array(
  517. ),
  518. 'install_bootstrap_full' => array(
  519. 'run' => INSTALL_TASK_RUN_IF_REACHED,
  520. ),
  521. 'install_profile_modules' => array(
  522. 'display_name' => count($install_state['profiles']) == 1 ? st('Install site') : st('Install profile'),
  523. 'type' => 'batch',
  524. ),
  525. 'install_import_locales' => array(
  526. 'display_name' => st('Set up translations'),
  527. 'display' => $needs_translations,
  528. 'type' => 'batch',
  529. 'run' => $needs_translations ? INSTALL_TASK_RUN_IF_NOT_COMPLETED : INSTALL_TASK_SKIP,
  530. ),
  531. 'install_configure_form' => array(
  532. 'display_name' => st('Configure site'),
  533. 'type' => 'form',
  534. ),
  535. );
  536. // Now add any tasks defined by the installation profile.
  537. if (!empty($install_state['parameters']['profile'])) {
  538. // Load the profile install file, because it is not always loaded when
  539. // hook_install_tasks() is invoked (e.g. batch processing).
  540. $profile_install_file = DRUPAL_ROOT . '/profiles/' . $install_state['parameters']['profile'] . '/' . $install_state['parameters']['profile'] . '.install';
  541. if (file_exists($profile_install_file)) {
  542. include_once $profile_install_file;
  543. }
  544. $function = $install_state['parameters']['profile'] . '_install_tasks';
  545. if (function_exists($function)) {
  546. $result = $function($install_state);
  547. if (is_array($result)) {
  548. $tasks += $result;
  549. }
  550. }
  551. }
  552. // Finish by adding the remaining core tasks.
  553. $tasks += array(
  554. 'install_import_locales_remaining' => array(
  555. 'display_name' => st('Finish translations'),
  556. 'display' => $needs_translations,
  557. 'type' => 'batch',
  558. 'run' => $needs_translations ? INSTALL_TASK_RUN_IF_NOT_COMPLETED : INSTALL_TASK_SKIP,
  559. ),
  560. 'install_finished' => array(
  561. 'display_name' => st('Finished'),
  562. ),
  563. );
  564. // Allow the installation profile to modify the full list of tasks.
  565. if (!empty($install_state['parameters']['profile'])) {
  566. $profile_file = DRUPAL_ROOT . '/profiles/' . $install_state['parameters']['profile'] . '/' . $install_state['parameters']['profile'] . '.profile';
  567. if (file_exists($profile_file)) {
  568. include_once $profile_file;
  569. $function = $install_state['parameters']['profile'] . '_install_tasks_alter';
  570. if (function_exists($function)) {
  571. $function($tasks, $install_state);
  572. }
  573. }
  574. }
  575. // Fill in default parameters for each task before returning the list.
  576. foreach ($tasks as $task_name => &$task) {
  577. $task += array(
  578. 'display_name' => NULL,
  579. 'display' => !empty($task['display_name']),
  580. 'type' => 'normal',
  581. 'run' => INSTALL_TASK_RUN_IF_NOT_COMPLETED,
  582. 'function' => $task_name,
  583. );
  584. }
  585. return $tasks;
  586. }
  587. /**
  588. * Returns a list of tasks that should be displayed to the end user.
  589. *
  590. * The output of this function is a list suitable for sending to
  591. * theme_task_list().
  592. *
  593. * @param $install_state
  594. * An array of information about the current installation state.
  595. *
  596. * @return
  597. * A list of tasks, with keys equal to the machine-readable task name and
  598. * values equal to the name that should be displayed.
  599. *
  600. * @see theme_task_list()
  601. */
  602. function install_tasks_to_display($install_state) {
  603. $displayed_tasks = array();
  604. foreach (install_tasks($install_state) as $name => $task) {
  605. if ($task['display']) {
  606. $displayed_tasks[$name] = $task['display_name'];
  607. }
  608. }
  609. return $displayed_tasks;
  610. }
  611. /**
  612. * Returns the URL that should be redirected to during an installation request.
  613. *
  614. * The output of this function is suitable for sending to install_goto().
  615. *
  616. * @param $install_state
  617. * An array of information about the current installation state.
  618. *
  619. * @return
  620. * The URL to redirect to.
  621. *
  622. * @see install_full_redirect_url()
  623. */
  624. function install_redirect_url($install_state) {
  625. return 'install.php?' . drupal_http_build_query($install_state['parameters']);
  626. }
  627. /**
  628. * Returns the complete URL redirected to during an installation request.
  629. *
  630. * @param $install_state
  631. * An array of information about the current installation state.
  632. *
  633. * @return
  634. * The complete URL to redirect to.
  635. *
  636. * @see install_redirect_url()
  637. */
  638. function install_full_redirect_url($install_state) {
  639. global $base_url;
  640. return $base_url . '/' . install_redirect_url($install_state);
  641. }
  642. /**
  643. * Displays themed installer output and ends the page request.
  644. *
  645. * Installation tasks should use drupal_set_title() to set the desired page
  646. * title, but otherwise this function takes care of theming the overall page
  647. * output during every step of the installation.
  648. *
  649. * @param $output
  650. * The content to display on the main part of the page.
  651. * @param $install_state
  652. * An array of information about the current installation state.
  653. */
  654. function install_display_output($output, $install_state) {
  655. drupal_page_header();
  656. // Prevent install.php from being indexed when installed in a sub folder.
  657. // robots.txt rules are not read if the site is within domain.com/subfolder
  658. // resulting in /subfolder/install.php being found through search engines.
  659. // When settings.php is writeable this can be used via an external database
  660. // leading a malicious user to gain php access to the server.
  661. $noindex_meta_tag = array(
  662. '#tag' => 'meta',
  663. '#attributes' => array(
  664. 'name' => 'robots',
  665. 'content' => 'noindex, nofollow',
  666. ),
  667. );
  668. drupal_add_html_head($noindex_meta_tag, 'install_meta_robots');
  669. // Only show the task list if there is an active task; otherwise, the page
  670. // request has ended before tasks have even been started, so there is nothing
  671. // meaningful to show.
  672. if (isset($install_state['active_task'])) {
  673. // Let the theming function know when every step of the installation has
  674. // been completed.
  675. $active_task = $install_state['installation_finished'] ? NULL : $install_state['active_task'];
  676. drupal_add_region_content('sidebar_first', theme('task_list', array('items' => install_tasks_to_display($install_state), 'active' => $active_task)));
  677. }
  678. print theme('install_page', array('content' => $output));
  679. exit;
  680. }
  681. /**
  682. * Verifies the requirements for installing Drupal.
  683. *
  684. * @param $install_state
  685. * An array of information about the current installation state.
  686. *
  687. * @return
  688. * A themed status report, or an exception if there are requirement errors.
  689. * If there are only requirement warnings, a themed status report is shown
  690. * initially, but the user is allowed to bypass it by providing 'continue=1'
  691. * in the URL. Otherwise, no output is returned, so that the next task can be
  692. * run in the same page request.
  693. */
  694. function install_verify_requirements(&$install_state) {
  695. // Check the installation requirements for Drupal and this profile.
  696. $requirements = install_check_requirements($install_state);
  697. // Verify existence of all required modules.
  698. $requirements += drupal_verify_profile($install_state);
  699. // Check the severity of the requirements reported.
  700. $severity = drupal_requirements_severity($requirements);
  701. // If there are errors, always display them. If there are only warnings, skip
  702. // them if the user has provided a URL parameter acknowledging the warnings
  703. // and indicating a desire to continue anyway. See drupal_requirements_url().
  704. if ($severity == REQUIREMENT_ERROR || ($severity == REQUIREMENT_WARNING && empty($install_state['parameters']['continue']))) {
  705. if ($install_state['interactive']) {
  706. drupal_set_title(st('Requirements problem'));
  707. $status_report = theme('status_report', array('requirements' => $requirements));
  708. $status_report .= st('Check the error messages and <a href="!url">proceed with the installation</a>.', array('!url' => check_url(drupal_requirements_url($severity))));
  709. return $status_report;
  710. }
  711. else {
  712. // Throw an exception showing any unmet requirements.
  713. $failures = array();
  714. foreach ($requirements as $requirement) {
  715. // Skip warnings altogether for non-interactive installations; these
  716. // proceed in a single request so there is no good opportunity (and no
  717. // good method) to warn the user anyway.
  718. if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
  719. $failures[] = $requirement['title'] . ': ' . $requirement['value'] . "\n\n" . $requirement['description'];
  720. }
  721. }
  722. if (!empty($failures)) {
  723. throw new Exception(implode("\n\n", $failures));
  724. }
  725. }
  726. }
  727. }
  728. /**
  729. * Installation task; install the Drupal system module.
  730. *
  731. * @param $install_state
  732. * An array of information about the current installation state.
  733. */
  734. function install_system_module(&$install_state) {
  735. // Install system.module.
  736. drupal_install_system();
  737. // Call file_ensure_htaccess() to ensure that all of Drupal's standard
  738. // directories (e.g., the public and private files directories) have
  739. // appropriate .htaccess files. These directories will have already been
  740. // created by this point in the installer, since Drupal creates them during
  741. // the install_verify_requirements() task. Note that we cannot call
  742. // file_ensure_htaccess() any earlier than this, since it relies on
  743. // system.module in order to work.
  744. file_ensure_htaccess();
  745. // Enable the user module so that sessions can be recorded during the
  746. // upcoming bootstrap step.
  747. module_enable(array('user'), FALSE);
  748. // Save the list of other modules to install for the upcoming tasks.
  749. // variable_set() can be used now that system.module is installed.
  750. $modules = $install_state['profile_info']['dependencies'];
  751. // The installation profile is also a module, which needs to be installed
  752. // after all the dependencies have been installed.
  753. $modules[] = drupal_get_profile();
  754. variable_set('install_profile_modules', array_diff($modules, array('system')));
  755. $install_state['database_tables_exist'] = TRUE;
  756. }
  757. /**
  758. * Verifies and returns the last installation task that was completed.
  759. *
  760. * @return
  761. * The last completed task, if there is one. An exception is thrown if Drupal
  762. * is already installed.
  763. */
  764. function install_verify_completed_task() {
  765. try {
  766. if ($result = db_query("SELECT value FROM {variable} WHERE name = :name", array('name' => 'install_task'))) {
  767. $task = unserialize($result->fetchField());
  768. }
  769. }
  770. // Do not trigger an error if the database query fails, since the database
  771. // might not be set up yet.
  772. catch (Exception $e) {
  773. }
  774. if (isset($task)) {
  775. if ($task == 'done') {
  776. throw new Exception(install_already_done_error());
  777. }
  778. return $task;
  779. }
  780. }
  781. /**
  782. * Verifies the existing settings in settings.php.
  783. */
  784. function install_verify_settings() {
  785. global $databases;
  786. // Verify existing settings (if any).
  787. if (!empty($databases) && install_verify_pdo()) {
  788. $database = $databases['default']['default'];
  789. drupal_static_reset('conf_path');
  790. $settings_file = './' . conf_path(FALSE) . '/settings.php';
  791. $errors = install_database_errors($database, $settings_file);
  792. if (empty($errors)) {
  793. return TRUE;
  794. }
  795. }
  796. return FALSE;
  797. }
  798. /**
  799. * Verifies the PDO library.
  800. */
  801. function install_verify_pdo() {
  802. // PDO was moved to PHP core in 5.2.0, but the old extension (targeting 5.0
  803. // and 5.1) is still available from PECL, and can still be built without
  804. // errors. To verify that the correct version is in use, we check the
  805. // PDO::ATTR_DEFAULT_FETCH_MODE constant, which is not available in the
  806. // PECL extension.
  807. return extension_loaded('pdo') && defined('PDO::ATTR_DEFAULT_FETCH_MODE');
  808. }
  809. /**
  810. * Form constructor for a form to configure and rewrite settings.php.
  811. *
  812. * @param $install_state
  813. * An array of information about the current installation state.
  814. *
  815. * @see install_settings_form_validate()
  816. * @see install_settings_form_submit()
  817. * @ingroup forms
  818. */
  819. function install_settings_form($form, &$form_state, &$install_state) {
  820. global $databases;
  821. $profile = $install_state['parameters']['profile'];
  822. $install_locale = $install_state['parameters']['locale'];
  823. drupal_static_reset('conf_path');
  824. $conf_path = './' . conf_path(FALSE);
  825. $settings_file = $conf_path . '/settings.php';
  826. $database = isset($databases['default']['default']) ? $databases['default']['default'] : array();
  827. drupal_set_title(st('Database configuration'));
  828. $drivers = drupal_get_database_types();
  829. $drivers_keys = array_keys($drivers);
  830. $form['driver'] = array(
  831. '#type' => 'radios',
  832. '#title' => st('Database type'),
  833. '#required' => TRUE,
  834. '#default_value' => !empty($database['driver']) ? $database['driver'] : current($drivers_keys),
  835. '#description' => st('The type of database your @drupal data will be stored in.', array('@drupal' => drupal_install_profile_distribution_name())),
  836. );
  837. if (count($drivers) == 1) {
  838. $form['driver']['#disabled'] = TRUE;
  839. $form['driver']['#description'] .= ' ' . st('Your PHP configuration only supports a single database type, so it has been automatically selected.');
  840. }
  841. // Add driver specific configuration options.
  842. foreach ($drivers as $key => $driver) {
  843. $form['driver']['#options'][$key] = $driver->name();
  844. $form['settings'][$key] = $driver->getFormOptions($database);
  845. $form['settings'][$key]['#prefix'] = '<h2 class="js-hide">' . st('@driver_name settings', array('@driver_name' => $driver->name())) . '</h2>';
  846. $form['settings'][$key]['#type'] = 'container';
  847. $form['settings'][$key]['#tree'] = TRUE;
  848. $form['settings'][$key]['advanced_options']['#parents'] = array($key);
  849. $form['settings'][$key]['#states'] = array(
  850. 'visible' => array(
  851. ':input[name=driver]' => array('value' => $key),
  852. )
  853. );
  854. }
  855. $form['actions'] = array('#type' => 'actions');
  856. $form['actions']['save'] = array(
  857. '#type' => 'submit',
  858. '#value' => st('Save and continue'),
  859. '#limit_validation_errors' => array(
  860. array('driver'),
  861. array(isset($form_state['input']['driver']) ? $form_state['input']['driver'] : current($drivers_keys)),
  862. ),
  863. '#submit' => array('install_settings_form_submit'),
  864. );
  865. $form['errors'] = array();
  866. $form['settings_file'] = array('#type' => 'value', '#value' => $settings_file);
  867. return $form;
  868. }
  869. /**
  870. * Form validation handler for install_settings_form().
  871. *
  872. * @see install_settings_form_submit()
  873. */
  874. function install_settings_form_validate($form, &$form_state) {
  875. $driver = $form_state['values']['driver'];
  876. $database = $form_state['values'][$driver];
  877. $database['driver'] = $driver;
  878. // TODO: remove when PIFR will be updated to use 'db_prefix' instead of
  879. // 'prefix' in the database settings form.
  880. $database['prefix'] = $database['db_prefix'];
  881. unset($database['db_prefix']);
  882. $form_state['storage']['database'] = $database;
  883. $errors = install_database_errors($database, $form_state['values']['settings_file']);
  884. foreach ($errors as $name => $message) {
  885. form_set_error($name, $message);
  886. }
  887. }
  888. /**
  889. * Checks a database connection and returns any errors.
  890. */
  891. function install_database_errors($database, $settings_file) {
  892. global $databases;
  893. $errors = array();
  894. // Check database type.
  895. $database_types = drupal_get_database_types();
  896. $driver = $database['driver'];
  897. if (!isset($database_types[$driver])) {
  898. $errors['driver'] = st("In your %settings_file file you have configured @drupal to use a %driver server, however your PHP installation currently does not support this database type.", array('%settings_file' => $settings_file, '@drupal' => drupal_install_profile_distribution_name(), '%driver' => $driver));
  899. }
  900. else {
  901. // Run driver specific validation
  902. $errors += $database_types[$driver]->validateDatabaseSettings($database);
  903. // Run tasks associated with the database type. Any errors are caught in the
  904. // calling function.
  905. $databases['default']['default'] = $database;
  906. // Just changing the global doesn't get the new information processed.
  907. // We tell tell the Database class to re-parse $databases.
  908. Database::parseConnectionInfo();
  909. try {
  910. db_run_tasks($driver);
  911. }
  912. catch (DatabaseTaskException $e) {
  913. // These are generic errors, so we do not have any specific key of the
  914. // database connection array to attach them to; therefore, we just put
  915. // them in the error array with standard numeric keys.
  916. $errors[$driver . '][0'] = $e->getMessage();
  917. }
  918. }
  919. return $errors;
  920. }
  921. /**
  922. * Form submission handler for install_settings_form().
  923. *
  924. * @see install_settings_form_validate()
  925. */
  926. function install_settings_form_submit($form, &$form_state) {
  927. global $install_state;
  928. // Update global settings array and save.
  929. $settings['databases'] = array(
  930. 'value' => array('default' => array('default' => $form_state['storage']['database'])),
  931. 'required' => TRUE,
  932. );
  933. $settings['drupal_hash_salt'] = array(
  934. 'value' => drupal_random_key(),
  935. 'required' => TRUE,
  936. );
  937. drupal_rewrite_settings($settings);
  938. // Indicate that the settings file has been verified, and check the database
  939. // for the last completed task, now that we have a valid connection. This
  940. // last step is important since we want to trigger an error if the new
  941. // database already has Drupal installed.
  942. $install_state['settings_verified'] = TRUE;
  943. $install_state['completed_task'] = install_verify_completed_task();
  944. }
  945. /**
  946. * Finds all .profile files.
  947. */
  948. function install_find_profiles() {
  949. return file_scan_directory('./profiles', '/\.profile$/', array('key' => 'name'));
  950. }
  951. /**
  952. * Selects which profile to install.
  953. *
  954. * @param $install_state
  955. * An array of information about the current installation state. The chosen
  956. * profile will be added here, if it was not already selected previously, as
  957. * will a list of all available profiles.
  958. *
  959. * @return
  960. * For interactive installations, a form allowing the profile to be selected,
  961. * if the user has a choice that needs to be made. Otherwise, an exception is
  962. * thrown if a profile cannot be chosen automatically.
  963. */
  964. function install_select_profile(&$install_state) {
  965. $install_state['profiles'] += install_find_profiles();
  966. if (empty($install_state['parameters']['profile'])) {
  967. // Try to find a profile.
  968. $profile = _install_select_profile($install_state['profiles']);
  969. if (empty($profile)) {
  970. // We still don't have a profile, so display a form for selecting one.
  971. // Only do this in the case of interactive installations, since this is
  972. // not a real form with submit handlers (the database isn't even set up
  973. // yet), rather just a convenience method for setting parameters in the
  974. // URL.
  975. if ($install_state['interactive']) {
  976. include_once DRUPAL_ROOT . '/includes/form.inc';
  977. drupal_set_title(st('Select an installation profile'));
  978. $form = drupal_get_form('install_select_profile_form', $install_state['profiles']);
  979. return drupal_render($form);
  980. }
  981. else {
  982. throw new Exception(install_no_profile_error());
  983. }
  984. }
  985. else {
  986. $install_state['parameters']['profile'] = $profile;
  987. }
  988. }
  989. }
  990. /**
  991. * Selects an installation profile.
  992. *
  993. * A profile will be selected if:
  994. * - Only one profile is available,
  995. * - A profile was submitted through $_POST,
  996. * - Exactly one of the profiles is marked as "exclusive".
  997. * If multiple profiles are marked as "exclusive" then no profile will be
  998. * selected.
  999. *
  1000. * @param array $profiles
  1001. * An associative array of profiles with the machine-readable names as keys.
  1002. *
  1003. * @return
  1004. * The machine-readable name of the selected profile or NULL if no profile was
  1005. * selected.
  1006. */
  1007. function _install_select_profile($profiles) {
  1008. if (sizeof($profiles) == 0) {
  1009. throw new Exception(install_no_profile_error());
  1010. }
  1011. // Don't need to choose profile if only one available.
  1012. if (sizeof($profiles) == 1) {
  1013. $profile = array_pop($profiles);
  1014. // TODO: is this right?
  1015. require_once DRUPAL_ROOT . '/' . $profile->uri;
  1016. return $profile->name;
  1017. }
  1018. else {
  1019. foreach ($profiles as $profile) {
  1020. if (!empty($_POST['profile']) && ($_POST['profile'] == $profile->name)) {
  1021. return $profile->name;
  1022. }
  1023. }
  1024. }
  1025. // Check for a profile marked as "exclusive" and ensure that only one
  1026. // profile is marked as such.
  1027. $exclusive_profile = NULL;
  1028. foreach ($profiles as $profile) {
  1029. $profile_info = install_profile_info($profile->name);
  1030. if (!empty($profile_info['exclusive'])) {
  1031. if (empty($exclusive_profile)) {
  1032. $exclusive_profile = $profile->name;
  1033. }
  1034. else {
  1035. // We found a second "exclusive" profile. There's no way to choose
  1036. // between them, so we ignore the property.
  1037. return;
  1038. }
  1039. }
  1040. }
  1041. return $exclusive_profile;
  1042. }
  1043. /**
  1044. * Form constructor for the profile selection form.
  1045. *
  1046. * @param $form_state
  1047. * Array of metadata about state of form processing.
  1048. * @param $profile_files
  1049. * Array of .profile files, as returned from file_scan_directory().
  1050. *
  1051. * @ingroup forms
  1052. */
  1053. function install_select_profile_form($form, &$form_state, $profile_files) {
  1054. $profiles = array();
  1055. $names = array();
  1056. foreach ($profile_files as $profile) {
  1057. // TODO: is this right?
  1058. include_once DRUPAL_ROOT . '/' . $profile->uri;
  1059. $details = install_profile_info($profile->name);
  1060. // Don't show hidden profiles. This is used by to hide the testing profile,
  1061. // which only exists to speed up test runs.
  1062. if ($details['hidden'] === TRUE) {
  1063. continue;
  1064. }
  1065. $profiles[$profile->name] = $details;
  1066. // Determine the name of the profile; default to file name if defined name
  1067. // is unspecified.
  1068. $name = isset($details['name']) ? $details['name'] : $profile->name;
  1069. $names[$profile->name] = $name;
  1070. }
  1071. // Display radio buttons alphabetically by human-readable name, but always
  1072. // put the core profiles first (if they are present in the filesystem).
  1073. natcasesort($names);
  1074. if (isset($names['minimal'])) {
  1075. // If the expert ("Minimal") core profile is present, put it in front of
  1076. // any non-core profiles rather than including it with them alphabetically,
  1077. // since the other profiles might be intended to group together in a
  1078. // particular way.
  1079. $names = array('minimal' => $names['minimal']) + $names;
  1080. }
  1081. if (isset($names['standard'])) {
  1082. // If the default ("Standard") core profile is present, put it at the very
  1083. // top of the list. This profile will have its radio button pre-selected,
  1084. // so we want it to always appear at the top.
  1085. $names = array('standard' => $names['standard']) + $names;
  1086. }
  1087. foreach ($names as $profile => $name) {
  1088. $form['profile'][$name] = array(
  1089. '#type' => 'radio',
  1090. '#value' => 'standard',
  1091. '#return_value' => $profile,
  1092. '#title' => $name,
  1093. '#description' => isset($profiles[$profile]['description']) ? $profiles[$profile]['description'] : '',
  1094. '#parents' => array('profile'),
  1095. );
  1096. }
  1097. $form['actions'] = array('#type' => 'actions');
  1098. $form['actions']['submit'] = array(
  1099. '#type' => 'submit',
  1100. '#value' => st('Save and continue'),
  1101. );
  1102. return $form;
  1103. }
  1104. /**
  1105. * Find all .po files for the current profile.
  1106. */
  1107. function install_find_locales($profilename) {
  1108. $locales = file_scan_directory('./profiles/' . $profilename . '/translations', '/\.po$/', array('recurse' => FALSE));
  1109. array_unshift($locales, (object) array('name' => 'en'));
  1110. foreach ($locales as $key => $locale) {
  1111. // The locale (file name) might be drupal-7.2.cs.po instead of cs.po.
  1112. $locales[$key]->langcode = preg_replace('!^(.+\.)?([^\.]+)$!', '\2', $locale->name);
  1113. // Language codes cannot exceed 12 characters to fit into the {languages}
  1114. // table.
  1115. if (strlen($locales[$key]->langcode) > 12) {
  1116. unset($locales[$key]);
  1117. }
  1118. }
  1119. return $locales;
  1120. }
  1121. /**
  1122. * Installation task; select which locale to use for the current profile.
  1123. *
  1124. * @param $install_state
  1125. * An array of information about the current installation state. The chosen
  1126. * locale will be added here, if it was not already selected previously, as
  1127. * will a list of all available locales.
  1128. *
  1129. * @return
  1130. * For interactive installations, a form or other page output allowing the
  1131. * locale to be selected or providing information about locale selection, if
  1132. * a locale has not been chosen. Otherwise, an exception is thrown if a
  1133. * locale cannot be chosen automatically.
  1134. */
  1135. function install_select_locale(&$install_state) {
  1136. // Find all available locales.
  1137. $profilename = $install_state['parameters']['profile'];
  1138. $locales = install_find_locales($profilename);
  1139. $install_state['locales'] += $locales;
  1140. if (!empty($_POST['locale'])) {
  1141. foreach ($locales as $locale) {
  1142. if ($_POST['locale'] == $locale->langcode) {
  1143. $install_state['parameters']['locale'] = $locale->langcode;
  1144. return;
  1145. }
  1146. }
  1147. }
  1148. if (empty($install_state['parameters']['locale'])) {
  1149. // If only the built-in (English) language is available, and we are
  1150. // performing an interactive installation, inform the user that the
  1151. // installer can be localized. Otherwise we assume the user knows what he
  1152. // is doing.
  1153. if (count($locales) == 1) {
  1154. if ($install_state['interactive']) {
  1155. drupal_set_title(st('Choose language'));
  1156. if (!empty($install_state['parameters']['localize'])) {
  1157. $output = '<p>Follow these steps to translate Drupal into your language:</p>';
  1158. $output .= '<ol>';
  1159. $output .= '<li>Download a translation from the <a href="http://localize.drupal.org/download" target="_blank">translation server</a>.</li>';
  1160. $output .= '<li>Place it into the following directory:
  1161. <pre>
  1162. /profiles/' . $profilename . '/translations/
  1163. </pre></li>';
  1164. $output .= '</ol>';
  1165. $output .= '<p>For more information on installing Drupal in different languages, visit the <a href="http://drupal.org/localize" target="_blank">drupal.org handbook page</a>.</p>';
  1166. $output .= '<p>How should the installation continue?</p>';
  1167. $output .= '<ul>';
  1168. $output .= '<li><a href="install.php?profile=' . $profilename . '">Reload the language selection page after adding translations</a></li>';
  1169. $output .= '<li><a href="install.php?profile=' . $profilename . '&amp;locale=en">Continue installation in English</a></li>';
  1170. $output .= '</ul>';
  1171. }
  1172. else {
  1173. include_once DRUPAL_ROOT . '/includes/form.inc';
  1174. $elements = drupal_get_form('install_select_locale_form', $locales, $profilename);
  1175. $output = drupal_render($elements);
  1176. }
  1177. return $output;
  1178. }
  1179. // One language, but not an interactive installation. Assume the user
  1180. // knows what he is doing.
  1181. $locale = current($locales);
  1182. $install_state['parameters']['locale'] = $locale->name;
  1183. return;
  1184. }
  1185. else {
  1186. // Allow profile to pre-select the language, skipping the selection.
  1187. $function = $profilename . '_profile_details';
  1188. if (function_exists($function)) {
  1189. $details = $function();
  1190. if (isset($details['language'])) {
  1191. foreach ($locales as $locale) {
  1192. if ($details['language'] == $locale->name) {
  1193. $install_state['parameters']['locale'] = $locale->name;
  1194. return;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. // We still don't have a locale, so display a form for selecting one.
  1200. // Only do this in the case of interactive installations, since this is
  1201. // not a real form with submit handlers (the database isn't even set up
  1202. // yet), rather just a convenience method for setting parameters in the
  1203. // URL.
  1204. if ($install_state['interactive']) {
  1205. drupal_set_title(st('Choose language'));
  1206. include_once DRUPAL_ROOT . '/includes/form.inc';
  1207. $elements = drupal_get_form('install_select_locale_form', $locales, $profilename);
  1208. return drupal_render($elements);
  1209. }
  1210. else {
  1211. throw new Exception(st('Sorry, you must select a language to continue the installation.'));
  1212. }
  1213. }
  1214. }
  1215. }
  1216. /**
  1217. * Form constructor for the language selection form.
  1218. *
  1219. * @ingroup forms
  1220. */
  1221. function install_select_locale_form($form, &$form_state, $locales, $profilename) {
  1222. include_once DRUPAL_ROOT . '/includes/iso.inc';
  1223. $languages = _locale_get_predefined_list();
  1224. foreach ($locales as $locale) {
  1225. $name = $locale->langcode;
  1226. if (isset($languages[$name])) {
  1227. $name = $languages[$name][0] . (isset($languages[$name][1]) ? ' ' . st('(@language)', array('@language' => $languages[$name][1])) : '');
  1228. }
  1229. $form['locale'][$locale->langcode] = array(
  1230. '#type' => 'radio',
  1231. '#return_value' => $locale->langcode,
  1232. '#default_value' => $locale->langcode == 'en' ? 'en' : '',
  1233. '#title' => $name . ($locale->langcode == 'en' ? ' ' . st('(built-in)') : ''),
  1234. '#parents' => array('locale')
  1235. );
  1236. }
  1237. if (count($locales) == 1) {
  1238. $form['help'] = array(
  1239. '#markup' => '<p><a href="install.php?profile=' . $profilename . '&amp;localize=true">' . st('Learn how to install Drupal in other languages') . '</a></p>',
  1240. );
  1241. }
  1242. $form['actions'] = array('#type' => 'actions');
  1243. $form['actions']['submit'] = array(
  1244. '#type' => 'submit',
  1245. '#value' => st('Save and continue'),
  1246. );
  1247. return $form;
  1248. }
  1249. /**
  1250. * Indicates that there are no profiles available.
  1251. */
  1252. function install_no_profile_error() {
  1253. drupal_set_title(st('No profiles available'));
  1254. return st('We were unable to find any installation profiles. Installation profiles tell us what modules to enable and what schema to install in the database. A profile is necessary to continue with the installation process.');
  1255. }
  1256. /**
  1257. * Indicates that Drupal has already been installed.
  1258. */
  1259. function install_already_done_error() {
  1260. global $base_url;
  1261. drupal_set_title(st('Drupal already installed'));
  1262. return st('<ul><li>To start over, you must empty your existing database.</li><li>To install to a different database, edit the appropriate <em>settings.php</em> file in the <em>sites</em> folder.</li><li>To upgrade an existing installation, proceed to the <a href="@base-url/update.php">update script</a>.</li><li>View your <a href="@base-url">existing site</a>.</li></ul>', array('@base-url' => $base_url));
  1263. }
  1264. /**
  1265. * Loads information about the chosen profile during installation.
  1266. *
  1267. * @param $install_state
  1268. * An array of information about the current installation state. The loaded
  1269. * profile information will be added here, or an exception will be thrown if
  1270. * the profile cannot be loaded.
  1271. */
  1272. function install_load_profile(&$install_state) {
  1273. $profile_file = DRUPAL_ROOT . '/profiles/' . $install_state['parameters']['profile'] . '/' . $install_state['parameters']['profile'] . '.profile';
  1274. if (file_exists($profile_file)) {
  1275. include_once $profile_file;
  1276. $install_state['profile_info'] = install_profile_info($install_state['parameters']['profile'], $install_state['parameters']['locale']);
  1277. }
  1278. else {
  1279. throw new Exception(st('Sorry, the profile you have chosen cannot be loaded.'));
  1280. }
  1281. }
  1282. /**
  1283. * Performs a full bootstrap of Drupal during installation.
  1284. *
  1285. * @param $install_state
  1286. * An array of information about the current installation state.
  1287. */
  1288. function install_bootstrap_full(&$install_state) {
  1289. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  1290. }
  1291. /**
  1292. * Installs required modules via a batch process.
  1293. *
  1294. * @param $install_state
  1295. * An array of information about the current installation state.
  1296. *
  1297. * @return
  1298. * The batch definition.
  1299. */
  1300. function install_profile_modules(&$install_state) {
  1301. $modules = variable_get('install_profile_modules', array());
  1302. $files = system_rebuild_module_data();
  1303. variable_del('install_profile_modules');
  1304. // Always install required modules first. Respect the dependencies between
  1305. // the modules.
  1306. $required = array();
  1307. $non_required = array();
  1308. // Although the profile module is marked as required, it needs to go after
  1309. // every dependency, including non-required ones. So clear its required
  1310. // flag for now to allow it to install late.
  1311. $files[$install_state['parameters']['profile']]->info['required'] = FALSE;
  1312. // Add modules that other modules depend on.
  1313. foreach ($modules as $module) {
  1314. if ($files[$module]->requires) {
  1315. $modules = array_merge($modules, array_keys($files[$module]->requires));
  1316. }
  1317. }
  1318. $modules = array_unique($modules);
  1319. foreach ($modules as $module) {
  1320. if (!empty($files[$module]->info['required'])) {
  1321. $required[$module] = $files[$module]->sort;
  1322. }
  1323. else {
  1324. $non_required[$module] = $files[$module]->sort;
  1325. }
  1326. }
  1327. arsort($required);
  1328. arsort($non_required);
  1329. $operations = array();
  1330. foreach ($required + $non_required as $module => $weight) {
  1331. $operations[] = array('_install_module_batch', array($module, $files[$module]->info['name']));
  1332. }
  1333. $batch = array(
  1334. 'operations' => $operations,
  1335. 'title' => st('Installing @drupal', array('@drupal' => drupal_install_profile_distribution_name())),
  1336. 'error_message' => st('The installation has encountered an error.'),
  1337. 'finished' => '_install_profile_modules_finished',
  1338. );
  1339. return $batch;
  1340. }
  1341. /**
  1342. * Imports languages via a batch process during installation.
  1343. *
  1344. * @param $install_state
  1345. * An array of information about the current installation state.
  1346. *
  1347. * @return
  1348. * The batch definition, if there are language files to import.
  1349. */
  1350. function install_import_locales(&$install_state) {
  1351. include_once DRUPAL_ROOT . '/includes/locale.inc';
  1352. $install_locale = $install_state['parameters']['locale'];
  1353. include_once DRUPAL_ROOT . '/includes/iso.inc';
  1354. $predefined = _locale_get_predefined_list();
  1355. if (!isset($predefined[$install_locale])) {
  1356. // Drupal does not know about this language, so we prefill its values with
  1357. // our best guess. The user will be able to edit afterwards.
  1358. locale_add_language($install_locale, $install_locale, $install_locale, LANGUAGE_LTR, '', '', TRUE, TRUE);
  1359. }
  1360. else {
  1361. // A known predefined language, details will be filled in properly.
  1362. locale_add_language($install_locale, NULL, NULL, NULL, '', '', TRUE, TRUE);
  1363. }
  1364. // Collect files to import for this language.
  1365. $batch = locale_batch_by_language($install_locale, NULL);
  1366. if (!empty($batch)) {
  1367. // Remember components we cover in this batch set.
  1368. variable_set('install_locale_batch_components', $batch['#components']);
  1369. return $batch;
  1370. }
  1371. }
  1372. /**
  1373. * Form constructor for a form to configure the new site.
  1374. *
  1375. * @param $install_state
  1376. * An array of information about the current installation state.
  1377. *
  1378. * @see install_configure_form_validate()
  1379. * @see install_configure_form_submit()
  1380. * @ingroup forms
  1381. */
  1382. function install_configure_form($form, &$form_state, &$install_state) {
  1383. drupal_set_title(st('Configure site'));
  1384. // Warn about settings.php permissions risk
  1385. $settings_dir = conf_path();
  1386. $settings_file = $settings_dir . '/settings.php';
  1387. // Check that $_POST is empty so we only show this message when the form is
  1388. // first displayed, not on the next page after it is submitted. (We do not
  1389. // want to repeat it multiple times because it is a general warning that is
  1390. // not related to the rest of the installation process; it would also be
  1391. // especially out of place on the last page of the installer, where it would
  1392. // distract from the message that the Drupal installation has completed
  1393. // successfully.)
  1394. if (empty($_POST) && (!drupal_verify_install_file(DRUPAL_ROOT . '/' . $settings_file, FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE) || !drupal_verify_install_file(DRUPAL_ROOT . '/' . $settings_dir, FILE_NOT_WRITABLE, 'dir'))) {
  1395. drupal_set_message(st('All necessary changes to %dir and %file have been made, so you should remove write permissions to them now in order to avoid security risks. If you are unsure how to do so, consult the <a href="@handbook_url">online handbook</a>.', array('%dir' => $settings_dir, '%file' => $settings_file, '@handbook_url' => 'http://drupal.org/server-permissions')), 'warning');
  1396. }
  1397. drupal_add_js(drupal_get_path('module', 'system') . '/system.js');
  1398. // Add JavaScript time zone detection.
  1399. drupal_add_js('misc/timezone.js');
  1400. // We add these strings as settings because JavaScript translation does not
  1401. // work during installation.
  1402. drupal_add_js(array('copyFieldValue' => array('edit-site-mail' => array('edit-account-mail'))), 'setting');
  1403. drupal_add_js('jQuery(function () { Drupal.cleanURLsInstallCheck(); });', 'inline');
  1404. // Add JS to show / hide the 'Email administrator about site updates' elements
  1405. drupal_add_js('jQuery(function () { Drupal.hideEmailAdministratorCheckbox() });', 'inline');
  1406. // Build menu to allow clean URL check.
  1407. menu_rebuild();
  1408. // Cache a fully-built schema. This is necessary for any invocation of
  1409. // index.php because: (1) setting cache table entries requires schema
  1410. // information, (2) that occurs during bootstrap before any module are
  1411. // loaded, so (3) if there is no cached schema, drupal_get_schema() will
  1412. // try to generate one but with no loaded modules will return nothing.
  1413. //
  1414. // This logically could be done during the 'install_finished' task, but the
  1415. // clean URL check requires it now.
  1416. drupal_get_schema(NULL, TRUE);
  1417. // Return the form.
  1418. return _install_configure_form($form, $form_state, $install_state);
  1419. }
  1420. /**
  1421. * Installation task; import remaining languages via a batch process.
  1422. *
  1423. * @param $install_state
  1424. * An array of information about the current installation state.
  1425. *
  1426. * @return
  1427. * The batch definition, if there are language files to import.
  1428. */
  1429. function install_import_locales_remaining(&$install_state) {
  1430. include_once DRUPAL_ROOT . '/includes/locale.inc';
  1431. // Collect files to import for this language. Skip components already covered
  1432. // in the initial batch set.
  1433. $install_locale = $install_state['parameters']['locale'];
  1434. $batch = locale_batch_by_language($install_locale, NULL, variable_get('install_locale_batch_components', array()));
  1435. // Remove temporary variable.
  1436. variable_del('install_locale_batch_components');
  1437. return $batch;
  1438. }
  1439. /**
  1440. * Finishes importing files at end of installation.
  1441. *
  1442. * @param $install_state
  1443. * An array of information about the current installation state.
  1444. *
  1445. * @return
  1446. * A message informing the user that the installation is complete.
  1447. */
  1448. function install_finished(&$install_state) {
  1449. drupal_set_title(st('@drupal installation complete', array('@drupal' => drupal_install_profile_distribution_name())), PASS_THROUGH);
  1450. $messages = drupal_set_message();
  1451. $output = '<p>' . st('Congratulations, you installed @drupal!', array('@drupal' => drupal_install_profile_distribution_name())) . '</p>';
  1452. $output .= '<p>' . (isset($messages['error']) ? st('Review the messages above before visiting <a href="@url">your new site</a>.', array('@url' => url(''))) : st('<a href="@url">Visit your new site</a>.', array('@url' => url('')))) . '</p>';
  1453. // Flush all caches to ensure that any full bootstraps during the installer
  1454. // do not leave stale cached data, and that any content types or other items
  1455. // registered by the installation profile are registered correctly.
  1456. drupal_flush_all_caches();
  1457. // Remember the profile which was used.
  1458. variable_set('install_profile', drupal_get_profile());
  1459. // Installation profiles are always loaded last
  1460. db_update('system')
  1461. ->fields(array('weight' => 1000))
  1462. ->condition('type', 'module')
  1463. ->condition('name', drupal_get_profile())
  1464. ->execute();
  1465. // Cache a fully-built schema.
  1466. drupal_get_schema(NULL, TRUE);
  1467. // Run cron to populate update status tables (if available) so that users
  1468. // will be warned if they've installed an out of date Drupal version.
  1469. // Will also trigger indexing of profile-supplied content or feeds.
  1470. drupal_cron_run();
  1471. return $output;
  1472. }
  1473. /**
  1474. * Batch callback for batch installation of modules.
  1475. */
  1476. function _install_module_batch($module, $module_name, &$context) {
  1477. // Install and enable the module right away, so that the module will be
  1478. // loaded by drupal_bootstrap in subsequent batch requests, and other
  1479. // modules possibly depending on it can safely perform their installation
  1480. // steps.
  1481. module_enable(array($module), FALSE);
  1482. $context['results'][] = $module;
  1483. $context['message'] = st('Installed %module module.', array('%module' => $module_name));
  1484. }
  1485. /**
  1486. * 'Finished' callback for module installation batch.
  1487. */
  1488. function _install_profile_modules_finished($success, $results, $operations) {
  1489. // Flush all caches to complete the module installation process. Subsequent
  1490. // installation tasks will now have full access to the profile's modules.
  1491. drupal_flush_all_caches();
  1492. }
  1493. /**
  1494. * Checks installation requirements and reports any errors.
  1495. */
  1496. function install_check_requirements($install_state) {
  1497. $profile = $install_state['parameters']['profile'];
  1498. // Check the profile requirements.
  1499. $requirements = drupal_check_profile($profile);
  1500. // If Drupal is not set up already, we need to create a settings file.
  1501. if (!$install_state['settings_verified']) {
  1502. $writable = FALSE;
  1503. $conf_path = './' . conf_path(FALSE, TRUE);
  1504. $settings_file = $conf_path . '/settings.php';
  1505. $default_settings_file = './sites/default/default.settings.php';
  1506. $file = $conf_path;
  1507. $exists = FALSE;
  1508. // Verify that the directory exists.
  1509. if (drupal_verify_install_file($conf_path, FILE_EXIST, 'dir')) {
  1510. // Check if a settings.php file already exists.
  1511. $file = $settings_file;
  1512. if (drupal_verify_install_file($settings_file, FILE_EXIST)) {
  1513. // If it does, make sure it is writable.
  1514. $writable = drupal_verify_install_file($settings_file, FILE_READABLE|FILE_WRITABLE);
  1515. $exists = TRUE;
  1516. }
  1517. }
  1518. // If default.settings.php does not exist, or is not readable, throw an
  1519. // error.
  1520. if (!drupal_verify_install_file($default_settings_file, FILE_EXIST|FILE_READABLE)) {
  1521. $requirements['default settings file exists'] = array(
  1522. 'title' => st('Default settings file'),
  1523. 'value' => st('The default settings file does not exist.'),
  1524. 'severity' => REQUIREMENT_ERROR,
  1525. 'description' => st('The @drupal installer requires that the %default-file file not be modified in any way from the original download.', array('@drupal' => drupal_install_profile_distribution_name(), '%default-file' => $default_settings_file)),
  1526. );
  1527. }
  1528. // Otherwise, if settings.php does not exist yet, we can try to copy
  1529. // default.settings.php to create it.
  1530. elseif (!$exists) {
  1531. $copied = drupal_verify_install_file($conf_path, FILE_EXIST|FILE_WRITABLE, 'dir') && @copy($default_settings_file, $settings_file);
  1532. if ($copied) {
  1533. // If the new settings file has the same owner as default.settings.php,
  1534. // this means default.settings.php is owned by the webserver user.
  1535. // This is an inherent security weakness because it allows a malicious
  1536. // webserver process to append arbitrary PHP code and then execute it.
  1537. // However, it is also a common configuration on shared hosting, and
  1538. // there is nothing Drupal can do to prevent it. In this situation,
  1539. // having settings.php also owned by the webserver does not introduce
  1540. // any additional security risk, so we keep the file in place.
  1541. if (fileowner($default_settings_file) === fileowner($settings_file)) {
  1542. $writable = drupal_verify_install_file($settings_file, FILE_READABLE|FILE_WRITABLE);
  1543. $exists = TRUE;
  1544. }
  1545. // If settings.php and default.settings.php have different owners, this
  1546. // probably means the server is set up "securely" (with the webserver
  1547. // running as its own user, distinct from the user who owns all the
  1548. // Drupal PHP files), although with either a group or world writable
  1549. // sites directory. Keeping settings.php owned by the webserver would
  1550. // therefore introduce a security risk. It would also cause a usability
  1551. // problem, since site owners who do not have root access to the file
  1552. // system would be unable to edit their settings file later on. We
  1553. // therefore must delete the file we just created and force the
  1554. // administrator to log on to the server and create it manually.
  1555. else {
  1556. $deleted = @drupal_unlink($settings_file);
  1557. // We expect deleting the file to be successful (since we just
  1558. // created it ourselves above), but if it fails somehow, we set a
  1559. // variable so we can display a one-time error message to the
  1560. // administrator at the bottom of the requirements list. We also try
  1561. // to make the file writable, to eliminate any conflicting error
  1562. // messages in the requirements list.
  1563. $exists = !$deleted;
  1564. if ($exists) {
  1565. $settings_file_ownership_error = TRUE;
  1566. $writable = drupal_verify_install_file($settings_file, FILE_READABLE|FILE_WRITABLE);
  1567. }
  1568. }
  1569. }
  1570. }
  1571. // If settings.php does not exist, throw an error.
  1572. if (!$exists) {
  1573. $requirements['settings file exists'] = array(
  1574. 'title' => st('Settings file'),
  1575. 'value' => st('The settings file does not exist.'),
  1576. 'severity' => REQUIREMENT_ERROR,
  1577. 'description' => st('The @drupal installer requires that you create a settings file as part of the installation process. Copy the %default_file file to %file. More details about installing Drupal are available in <a href="@install_txt">INSTALL.txt</a>.', array('@drupal' => drupal_install_profile_distribution_name(), '%file' => $file, '%default_file' => $default_settings_file, '@install_txt' => base_path() . 'INSTALL.txt')),
  1578. );
  1579. }
  1580. else {
  1581. $requirements['settings file exists'] = array(
  1582. 'title' => st('Settings file'),
  1583. 'value' => st('The %file file exists.', array('%file' => $file)),
  1584. );
  1585. // If settings.php is not writable, throw an error.
  1586. if (!$writable) {
  1587. $requirements['settings file writable'] = array(
  1588. 'title' => st('Settings file'),
  1589. 'value' => st('The settings file is not writable.'),
  1590. 'severity' => REQUIREMENT_ERROR,
  1591. 'description' => st('The @drupal installer requires write permissions to %file during the installation process. If you are unsure how to grant file permissions, consult the <a href="@handbook_url">online handbook</a>.', array('@drupal' => drupal_install_profile_distribution_name(), '%file' => $file, '@handbook_url' => 'http://drupal.org/server-permissions')),
  1592. );
  1593. }
  1594. else {
  1595. $requirements['settings file'] = array(
  1596. 'title' => st('Settings file'),
  1597. 'value' => st('The settings file is writable.'),
  1598. );
  1599. }
  1600. if (!empty($settings_file_ownership_error)) {
  1601. $requirements['settings file ownership'] = array(
  1602. 'title' => st('Settings file'),
  1603. 'value' => st('The settings file is owned by the web server.'),
  1604. 'severity' => REQUIREMENT_ERROR,
  1605. 'description' => st('The @drupal installer failed to create a settings file with proper file ownership. Log on to your web server, remove the existing %file file, and create a new one by copying the %default_file file to %file. More details about installing Drupal are available in <a href="@install_txt">INSTALL.txt</a>. If you have problems with the file permissions on your server, consult the <a href="@handbook_url">online handbook</a>.', array('@drupal' => drupal_install_profile_distribution_name(), '%file' => $file, '%default_file' => $default_settings_file, '@install_txt' => base_path() . 'INSTALL.txt', '@handbook_url' => 'http://drupal.org/server-permissions')),
  1606. );
  1607. }
  1608. }
  1609. }
  1610. return $requirements;
  1611. }
  1612. /**
  1613. * Form constructor for a site configuration form.
  1614. *
  1615. * @param $install_state
  1616. * An array of information about the current installation state.
  1617. *
  1618. * @see install_configure_form()
  1619. * @see install_configure_form_validate()
  1620. * @see install_configure_form_submit()
  1621. * @ingroup forms
  1622. */
  1623. function _install_configure_form($form, &$form_state, &$install_state) {
  1624. include_once DRUPAL_ROOT . '/includes/locale.inc';
  1625. $form['site_information'] = array(
  1626. '#type' => 'fieldset',
  1627. '#title' => st('Site information'),
  1628. '#collapsible' => FALSE,
  1629. );
  1630. $form['site_information']['site_name'] = array(
  1631. '#type' => 'textfield',
  1632. '#title' => st('Site name'),
  1633. '#required' => TRUE,
  1634. '#weight' => -20,
  1635. );
  1636. $form['site_information']['site_mail'] = array(
  1637. '#type' => 'textfield',
  1638. '#title' => st('Site e-mail address'),
  1639. '#default_value' => ini_get('sendmail_from'),
  1640. '#description' => st("Automated e-mails, such as registration information, will be sent from this address. Use an address ending in your site's domain to help prevent these e-mails from being flagged as spam."),
  1641. '#required' => TRUE,
  1642. '#weight' => -15,
  1643. );
  1644. $form['admin_account'] = array(
  1645. '#type' => 'fieldset',
  1646. '#title' => st('Site maintenance account'),
  1647. '#collapsible' => FALSE,
  1648. );
  1649. $form['admin_account']['account']['#tree'] = TRUE;
  1650. $form['admin_account']['account']['name'] = array('#type' => 'textfield',
  1651. '#title' => st('Username'),
  1652. '#maxlength' => USERNAME_MAX_LENGTH,
  1653. '#description' => st('Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.'),
  1654. '#required' => TRUE,
  1655. '#weight' => -10,
  1656. '#attributes' => array('class' => array('username')),
  1657. );
  1658. $form['admin_account']['account']['mail'] = array('#type' => 'textfield',
  1659. '#title' => st('E-mail address'),
  1660. '#maxlength' => EMAIL_MAX_LENGTH,
  1661. '#required' => TRUE,
  1662. '#weight' => -5,
  1663. );
  1664. $form['admin_account']['account']['pass'] = array(
  1665. '#type' => 'password_confirm',
  1666. '#required' => TRUE,
  1667. '#size' => 25,
  1668. '#weight' => 0,
  1669. );
  1670. $form['server_settings'] = array(
  1671. '#type' => 'fieldset',
  1672. '#title' => st('Server settings'),
  1673. '#collapsible' => FALSE,
  1674. );
  1675. $countries = country_get_list();
  1676. $form['server_settings']['site_default_country'] = array(
  1677. '#type' => 'select',
  1678. '#title' => st('Default country'),
  1679. '#empty_value' => '',
  1680. '#default_value' => variable_get('site_default_country', NULL),
  1681. '#options' => $countries,
  1682. '#description' => st('Select the default country for the site.'),
  1683. '#weight' => 0,
  1684. );
  1685. $form['server_settings']['date_default_timezone'] = array(
  1686. '#type' => 'select',
  1687. '#title' => st('Default time zone'),
  1688. '#default_value' => date_default_timezone_get(),
  1689. '#options' => system_time_zones(),
  1690. '#description' => st('By default, dates in this site will be displayed in the chosen time zone.'),
  1691. '#weight' => 5,
  1692. '#attributes' => array('class' => array('timezone-detect')),
  1693. );
  1694. $form['server_settings']['clean_url'] = array(
  1695. '#type' => 'hidden',
  1696. '#default_value' => 0,
  1697. '#attributes' => array('id' => 'edit-clean-url', 'class' => array('install')),
  1698. );
  1699. $form['update_notifications'] = array(
  1700. '#type' => 'fieldset',
  1701. '#title' => st('Update notifications'),
  1702. '#collapsible' => FALSE,
  1703. );
  1704. $form['update_notifications']['update_status_module'] = array(
  1705. '#type' => 'checkboxes',
  1706. '#options' => array(
  1707. 1 => st('Check for updates automatically'),
  1708. 2 => st('Receive e-mail notifications'),
  1709. ),
  1710. '#default_value' => array(1, 2),
  1711. '#description' => st('The system will notify you when updates and important security releases are available for installed components. Anonymous information about your site is sent to <a href="@drupal">Drupal.org</a>.', array('@drupal' => 'http://drupal.org')),
  1712. '#weight' => 15,
  1713. );
  1714. $form['actions'] = array('#type' => 'actions');
  1715. $form['actions']['submit'] = array(
  1716. '#type' => 'submit',
  1717. '#value' => st('Save and continue'),
  1718. '#weight' => 15,
  1719. );
  1720. return $form;
  1721. }
  1722. /**
  1723. * Form validation handler for install_configure_form().
  1724. *
  1725. * @see install_configure_form_submit()
  1726. */
  1727. function install_configure_form_validate($form, &$form_state) {
  1728. if ($error = user_validate_name($form_state['values']['account']['name'])) {
  1729. form_error($form['admin_account']['account']['name'], $error);
  1730. }
  1731. if ($error = user_validate_mail($form_state['values']['account']['mail'])) {
  1732. form_error($form['admin_account']['account']['mail'], $error);
  1733. }
  1734. if ($error = user_validate_mail($form_state['values']['site_mail'])) {
  1735. form_error($form['site_information']['site_mail'], $error);
  1736. }
  1737. }
  1738. /**
  1739. * Form submission handler for install_configure_form().
  1740. *
  1741. * @see install_configure_form_validate()
  1742. */
  1743. function install_configure_form_submit($form, &$form_state) {
  1744. global $user;
  1745. variable_set('site_name', $form_state['values']['site_name']);
  1746. variable_set('site_mail', $form_state['values']['site_mail']);
  1747. variable_set('date_default_timezone', $form_state['values']['date_default_timezone']);
  1748. variable_set('site_default_country', $form_state['values']['site_default_country']);
  1749. // Enable update.module if this option was selected.
  1750. if ($form_state['values']['update_status_module'][1]) {
  1751. module_enable(array('update'), FALSE);
  1752. // Add the site maintenance account's email address to the list of
  1753. // addresses to be notified when updates are available, if selected.
  1754. if ($form_state['values']['update_status_module'][2]) {
  1755. variable_set('update_notify_emails', array($form_state['values']['account']['mail']));
  1756. }
  1757. }
  1758. // We precreated user 1 with placeholder values. Let's save the real values.
  1759. $account = user_load(1);
  1760. $merge_data = array('init' => $form_state['values']['account']['mail'], 'roles' => !empty($account->roles) ? $account->roles : array(), 'status' => 1, 'timezone' => $form_state['values']['date_default_timezone']);
  1761. user_save($account, array_merge($form_state['values']['account'], $merge_data));
  1762. // Load global $user and perform final login tasks.
  1763. $user = user_load(1);
  1764. user_login_finalize();
  1765. if (isset($form_state['values']['clean_url'])) {
  1766. variable_set('clean_url', $form_state['values']['clean_url']);
  1767. }
  1768. // Record when this install ran.
  1769. variable_set('install_time', $_SERVER['REQUEST_TIME']);
  1770. }