install.core.inc 78 KB

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