install.core.inc 75 KB

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