install.core.inc 75 KB

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