install.php

  1. 7.x install.php
  2. 6.x install.php
  3. 4.x install.php
  4. 5.x install.php

This is the initial installation file for FlightPath.

This script will handle the initial installation of FlightPath, which entails creating its database tables and settings.php file.

File

install.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * This is the initial installation file for FlightPath.
  5. *
  6. * This script will handle the initial installation of FlightPath, which
  7. * entails creating its database tables and settings.php file.
  8. */
  9. // Set the PHP error reporting level for FlightPath. In this case,
  10. // only show us errors and warnings. (Hide "notice" and "strict" messages)
  11. error_reporting(E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_WARNING);
  12. session_start();
  13. header("Cache-control: private");
  14. // Load our bootstrap (skipping over loads we don't need)
  15. $skip_flightpath_settings = TRUE;
  16. $skip_flightpath_modules = TRUE;
  17. require("bootstrap.inc");
  18. // Load needed modules
  19. require_once("modules/system/system.module");
  20. // Check here to see if FlightPath has already been installed.
  21. // We will do this by simply looking for the settings.php file.
  22. if (file_exists("custom/settings.php")) {
  23. die("FlightPath has already been installed. If you wish to re-install FlightPath,
  24. DELETE the custom/settings.php file, and drop all of the tables in FlightPath's
  25. database.");
  26. }
  27. /*
  28. * To begin setting up FlightPath, the user must have completed
  29. * two other steps-- select language, and pass the requirements
  30. * check.
  31. */
  32. $lang = $_REQUEST["lang"];
  33. if ($lang == "") {
  34. install_display_lang_selection();
  35. die;
  36. }
  37. // If we made it here, the language must have been set. So,
  38. // now check the requirements, and display the results if there
  39. // are any.
  40. if ($req_array = install_check_requirements()) {
  41. install_display_requirements($req_array);
  42. die;
  43. }
  44. if ($_REQUEST["perform_action"] != "install") {
  45. // If we made it this far, it means we have no unfulfilled requirements.
  46. // Let's go ahead and ask the user for their database information.
  47. install_display_db_form();
  48. die;
  49. }
  50. else {
  51. // We ARE trying to install. Let's give it a go!
  52. install_perform_install();
  53. die;
  54. }
  55. die;
  56. /**
  57. * Actually performs the installation of FlightPath
  58. */
  59. function install_perform_install() {
  60. global $user;
  61. if (!isset($user)) {
  62. $user = new stdClass();
  63. }
  64. $user->id = 1; // set to admin during install
  65. $db_name = trim($_POST["db_name"]);
  66. $db_host = trim($_POST["db_host"]);
  67. $db_port = trim($_POST["db_port"]);
  68. $db_user = trim($_POST["db_user"]);
  69. $db_pass = trim($_POST["db_pass"]);
  70. $admin_pass = trim($_POST["admin_pass"]);
  71. $admin_pass2 = trim($_POST["admin_pass2"]);
  72. $admin_name = trim($_POST["admin_name"]);
  73. if (strlen($admin_name) < 3) {
  74. return install_display_db_form("<font color='red'>" . st("Please select another
  75. username for Admin (ex: admin)
  76. which is at least 3 characters long.") . "</font>");
  77. }
  78. if (strlen($admin_pass) < 5) {
  79. return install_display_db_form("<font color='red'>" . st("Admin password must be at least 5 characters long.") . "</font>");
  80. }
  81. if ($admin_pass != $admin_pass2) {
  82. return install_display_db_form("<font color='red'>" . st("You must enter the same Admin password for both the
  83. 'Admin Password' field and the 'Re-enter Password'
  84. field.") . "</font>");
  85. }
  86. // Place into settings so our installation procedures will work.
  87. $GLOBALS["fp_system_settings"]["db_host"] = $db_host;
  88. $GLOBALS["fp_system_settings"]["db_port"] = $db_port;
  89. $GLOBALS["fp_system_settings"]["db_user"] = $db_user;
  90. $GLOBALS["fp_system_settings"]["db_pass"] = $db_pass;
  91. $GLOBALS["fp_system_settings"]["db_name"] = $db_name;
  92. // Make sure admin information is OK.
  93. // We will attempt to connect to this database. If we have any problems, we will go back to
  94. // the form and inform the user.
  95. try {
  96. $pdo = new PDO("mysql:host=$db_host;port=$db_port;dbname=$db_name;charset=utf8", $db_user, $db_pass);
  97. }
  98. catch (Exception $e) {
  99. // Connection failed!
  100. return install_display_db_form("<div style='color:red;'>" . st("Could not connect. Please check that you have
  101. created the database already, and given the user all of the permissions
  102. (except Grant). Then, make sure you typed the username and
  103. password correctly, as well as the database name itself.
  104. <br><br>Full exception message: " . $e->getMessage()) . "</div>");
  105. }
  106. ///////////////////////////////
  107. // If we have made it here, then we have been provided valid database credentials.
  108. // Let's write out our settings.php file.
  109. $settings_template = trim(install_get_settings_file_template());
  110. // Add in our replacements
  111. $settings_template = str_replace("%DB_HOST%", $db_host, $settings_template);
  112. $settings_template = str_replace("%DB_PORT%", $db_port, $settings_template);
  113. $settings_template = str_replace("%DB_NAME%", $db_name, $settings_template);
  114. $settings_template = str_replace("%DB_USER%", $db_user, $settings_template);
  115. $settings_template = str_replace("%DB_PASS%", $db_pass, $settings_template);
  116. $settings_template = str_replace("%CRON_SECURITY_TOKEN%", md5(time()), $settings_template);
  117. // Attempt to figure out the file_system_path based on __FILE__
  118. $file_system_path = str_replace("install.php", "", __FILE__);
  119. // Convert \ to / in the file system path.
  120. $file_system_path = str_replace("\\", "/", $file_system_path);
  121. // Get rid of the last character, which should be a / at this point.
  122. $file_system_path = substr($file_system_path, 0, -1);
  123. $settings_template = str_replace("%FILE_SYSTEM_PATH%", $file_system_path, $settings_template);
  124. // Attempt to figure out the base URL.
  125. $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === FALSE ? 'http' : 'https';
  126. $host = $_SERVER['HTTP_HOST'];
  127. $script = $_SERVER['SCRIPT_NAME'];
  128. $base_url = $protocol . "://" . $host . $script;
  129. $base_url = str_replace("/install.php", "", $base_url);
  130. $settings_template = str_replace("%BASE_URL%", $base_url, $settings_template);
  131. // Figure out the base_path and add it in.
  132. $base_path = str_replace($protocol . "://" . $host, "", $base_url);
  133. $settings_template = str_replace("%BASE_PATH%", $base_path, $settings_template);
  134. // Okay, we have completed all the changes to the settings template string, we can
  135. // write it out to a file now.
  136. if (!file_put_contents("custom/settings.php", $settings_template)) {
  137. die("There was an error trying to write out the /custom/settings.php file. Please
  138. make sure the /custom directory is writable to the webserver.");
  139. }
  140. ///////////////////////////////////
  141. // Okay, we have just written out our settings.php file.
  142. // We now need to install our database. We will do this by
  143. // running the system module's hook_install, as it contains all
  144. // of our various tables needed to run FlightPath.
  145. include_once("modules/system/system.install");
  146. $GLOBALS["fp_die_mysql_errors"] = TRUE;
  147. // call system_install() to perform our numerous DB table creations.
  148. system_install();
  149. // With db tables created, let's include our settings file so we can get some
  150. // important GLOBAL variables set up.
  151. include("custom/settings.php");
  152. // Re-establish DatabaseHandler object connection since we just re-loaded the settings file.
  153. $temp_db = new DatabaseHandler();
  154. // Get our hash of the admin password
  155. $new_pass = user_hash_password($admin_pass);
  156. // Add the admin user to the newly-created users table and the "faculty" table.
  157. db_query("INSERT INTO users (user_id, user_name, cwid, password, is_faculty, f_name, l_name)
  158. VALUES ('1', '?', '1', '?', '1', 'Admin', 'User') ", $admin_name, $new_pass);
  159. db_query("INSERT INTO faculty (cwid) VALUES ('1') ");
  160. // Having made it here, we now need to call system_enable,
  161. // which will in turn enable all of the other modules which
  162. // we will need to have, as well as other database changes.
  163. system_enable();
  164. // Now that we have enabled all of the modules (and made other database changes)
  165. // let's re-include the bootstrap file, which will re-init our GLOBAL settings,
  166. // as well as load all of our new modules.
  167. $skip_flightpath_settings = FALSE;
  168. $skip_flightpath_modules = FALSE;
  169. include("bootstrap.inc");
  170. // Re-establish DatabaseHandler object connection since we just re-loaded the settings file.
  171. $temp_db = new DatabaseHandler();
  172. /////////////////////////
  173. // Now, we need to clear our caches and re-build the menu router.
  174. fp_clear_cache();
  175. // wipe out the SESSION to remove any extraneous messages.
  176. session_destroy();
  177. // Okay, now we are done!
  178. // let's re-direct to a new page.
  179. fp_goto("install-finished");
  180. }
  181. /**
  182. * Returns a template for a new settings file.
  183. *
  184. * The only role of this function is to provide a settings
  185. * template, with replacement patterns which we will use to create
  186. * a new settings.php file.
  187. */
  188. function install_get_settings_file_template() {
  189. return '
  190. <?php
  191. /**
  192. * @file
  193. * The settings file for FlightPath, containing database and other settings.
  194. *
  195. * Once you have made changes to this file, it would be best to change
  196. * the permissions to "read-only" to prevent unauthorized users
  197. * from altering it.
  198. */
  199. // Set the PHP error reporting level for FlightPath. In this case,
  200. // only show us errors and warnings. (Hide "notice" and "strict" messages)
  201. error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT);
  202. // Set the PHP max time limit which any one page is allowed to take up while
  203. // running. The default is 30 seconds. Change this value (or remove it)
  204. // as needed.
  205. set_time_limit(300); // 300 seconds = 5 minutes.
  206. /**
  207. * All system settings will be placed (at the end of this script)
  208. * into a $GLOBALS variable, but for now will be placed into an
  209. * array.
  210. */
  211. $system_settings = array();
  212. ////////////////////////////////////
  213. // !!! *** IMPORTANT !!! *** //
  214. ////////////////////////////////////
  215. // If this variable is set to TRUE, then anyone who attempts to log in
  216. // will have full, admin access.
  217. // Only set this to TRUE if you have run into trouble, and cannot log into
  218. // FlightPath normally!
  219. // Otherwise, leave it set to FALSE!
  220. $system_settings["GRANT_FULL_ACCESS"] = FALSE;
  221. ////////////////////////////////////
  222. // This should be the actual filesystem path to the directory
  223. // where FlightPath is installed. Do NOT include a trailing slash!
  224. // Ex: /var/www/public_html/flightpath or, for Windows: C:/htdocs/flightpath
  225. // ** Depending on your webserver, you may be required to use forward-slashes! **
  226. // use the following line to help you figure out the fileSystemPath, by seeing
  227. // what the path is to this file:
  228. // print "<br>System path to settings.php: " . __FILE__ . "<br><br>";
  229. $system_settings["file_system_path"] = "%FILE_SYSTEM_PATH%";
  230. // The base URL is the actual URL a user would type to visit your site.
  231. // Do NOT enter a trailing slash!
  232. // Ex: http://localhost/flightpath
  233. $system_settings["base_url"] = "%BASE_URL%";
  234. // The basePath is related to the baseURL. It is the part of the URL which comes after
  235. // your domain name.
  236. // It MUST begin with a preceeding slash.
  237. // Ex: If your site is example.com/dev/flightpath, then you should
  238. // enter "/dev/flightpath". If you are hosting on a bare domain name (https://abc.example.com/)
  239. // then simply enter "/"
  240. $system_settings["base_path"] = "%BASE_PATH%";
  241. ////////////////////////////////////
  242. // *** Database-related settings ***
  243. ////////////////////////////////////
  244. $system_settings["db_host"] = "%DB_HOST%"; // domain/ip address of the mysql host. ex: localhost or example.com
  245. $system_settings["db_port"] = "%DB_PORT%";
  246. $system_settings["db_user"] = "%DB_USER%";
  247. $system_settings["db_pass"] = "%DB_PASS%";
  248. $system_settings["db_name"] = "%DB_NAME%"; // Name of the actual database where
  249. // flightpath\'s tables are located.
  250. // Usually just "flightpath"
  251. /////////////////////////////////////
  252. // *** Defaults *** //
  253. /////////////////////////////////////
  254. // These default settings are for installation reasons only.
  255. // They will be overwritten in memory
  256. // once the flightpath_system_settings table is read in at the end of the file.
  257. // Do not alter them.
  258. $system_settings["display_mysql_errors"] = TRUE;
  259. $system_settings["theme"] = "themes/classic";
  260. ////////////////////////////////////
  261. // *** Misc Settings ***
  262. ////////////////////////////////////
  263. // To cut down on load times when the user loads a large elective group
  264. // containing many courses, FlightPath can load some of the course inventory
  265. // upon login. Set the number of courses to load here.
  266. $system_settings["load_course_inventory_on_login_number"] = 2000;
  267. ////////////////////////////////////
  268. // *** Cron-related ***
  269. ////////////////////////////////////
  270. // If you wish to use cron.php (which will call every module\'s
  271. // hook_cron() function), you may set up a cron job like this:
  272. // php cron.php security_token_string
  273. // SecurityToken: This is something which
  274. // must be the first argument passed to cron.php. It can be any continuous
  275. // string of *alpha-numeric* characters.
  276. // This is a security measure to prevent unauthorized users (or web-users) from
  277. // running cron.php, and is REQUIRED!
  278. // For example, if the token is "qwss34frwquu" then to run the script you would need
  279. // to use: http://url/cron.php?t=CRON_TOKEN (use wget to access from a system cron job.)
  280. $system_settings["cron_security_token"] = "%CRON_SECURITY_TOKEN%";
  281. /////////////////////////////////////
  282. /////////////////////////////////////
  283. //
  284. // The end of the settings file!
  285. /////////////////////////////////////
  286. /////////////////////////////////////
  287. /////////////////////////////////////
  288. // Do not alter or remove!!
  289. // This will load the contents of the flightpath_system_settings
  290. // table into the $system_settings variable. These are extra settings
  291. // which were set via the web using the system module.
  292. $db_host = $system_settings["db_host"];
  293. $db_port = $system_settings["db_port"];
  294. $db_user = $system_settings["db_user"];
  295. $db_pass = $system_settings["db_pass"];
  296. $db_name = $system_settings["db_name"];
  297. $pdo = new PDO("mysql:host=$db_host;port=$db_port;dbname=$db_name;charset=utf8", $db_user, $db_pass);
  298. if (!$pdo) die("Could not connect to database.");
  299. $res = $pdo->prepare("SELECT * FROM variables");
  300. $res->execute();
  301. while ($cur = $res->fetch(PDO::FETCH_ASSOC)) {
  302. if (@$val = unserialize($cur["value"])) {
  303. $system_settings[$cur["name"]] = $val;
  304. }
  305. }
  306. $res = $pdo->prepare("SELECT * FROM modules WHERE enabled = 1
  307. ORDER BY weight, name");
  308. $res->execute();
  309. while ($cur = $res->fetch(PDO::FETCH_ASSOC)) {
  310. $system_settings["modules"][$cur["name"]] = $cur;
  311. }
  312. // Close our pdo connection.
  313. $pdo = NULL;
  314. // We want to make sure the "system" module is enabled, so we will hard-code
  315. // its values.
  316. if ($system_settings["modules"]["system"]["enabled"] != 1) {
  317. $system_settings["modules"]["system"]["path"] = "modules/system";
  318. $system_settings["modules"]["system"]["enabled"] = 1;
  319. }
  320. ////////////////////////////////////////////
  321. ////////////////////////////////////////////
  322. // This must appear at the VERY end! Nothing should come after it....
  323. //
  324. // Assign our systemSettings to the GLOBALS array so we can access it anywhere.
  325. $GLOBALS["fp_system_settings"] = $system_settings;
  326. //////////////////////////////////////////////
  327. //////////////////////////////////////////////
  328. // PUT NOTHING BELOW THIS LINE!!!!
  329. ';
  330. }
  331. /**
  332. * Displays the form to let a user set up a new database
  333. */
  334. function install_display_db_form($msg = "") {
  335. global $lang;
  336. $db_name = $_POST["db_name"];
  337. $db_host = $_POST["db_host"];
  338. $db_port = $_POST["db_port"];
  339. $db_user = $_POST["db_user"];
  340. $db_pass = $_POST["db_pass"];
  341. $admin_pass = $_POST["admin_pass"];
  342. $admin_pass2 = $_POST["admin_pass2"];
  343. $admin_name = $_POST["admin_name"];
  344. if ($db_port == "") $db_port = "3306";
  345. $pC = "";
  346. $pC .= "<h2 class='title'>" . st("Setup Database and Admin") . "</h2>$msg
  347. <p>" . st("You should have already set up a database and database user
  348. (with all privileges except Grant) for FlightPath. Please
  349. enter that information below.") . "</p>
  350. <hr>
  351. <form action='install.php?lang=$lang' method='POST'>
  352. <input type='hidden' name='perform_action' value='install'>
  353. <table border='0' cellpadding='3' style='margin-left: 20px;'>
  354. <tr>
  355. <td colspan='2'><b>" . st("FlightPath administrator information") . "</b></td>
  356. </tr>
  357. <tr>
  358. <td valign='top'>" . st("Admin Username:") . "</td>
  359. <td valign='top'><input type='text' name='admin_name' value='$admin_name' size='15' maxlength='50'> Ex: admin</td>
  360. </tr>
  361. <tr>
  362. <td valign='top'>" . st("Admin Password:") . "</td>
  363. <td valign='top'><input type='password' name='admin_pass' value='$admin_pass' size='20'></td>
  364. </tr>
  365. <tr>
  366. <td valign='top'>" . st("Re-enter Password:") . "</td>
  367. <td valign='top'><input type='password' name='admin_pass2' value='$admin_pass2' size='20'></td>
  368. </tr>
  369. <tr>
  370. <td colspan='2'><hr>
  371. <b>" . st("Database information") . "</b></td>
  372. </tr>
  373. <tr>
  374. <td valign='top'>" . st("Database Name:") . "</td>
  375. <td valign='top'><input type='text' name='db_name' value='$db_name' size='50'></td>
  376. </tr>
  377. <tr>
  378. <td valign='top'>" . st("Database Host/IP:") . "</td>
  379. <td valign='top'><input type='text' name='db_host' value='$db_host' size='50'></td>
  380. </tr>
  381. <tr>
  382. <td valign='top'>" . st("Database Port:") . "</td>
  383. <td valign='top'><input type='text' name='db_port' value='$db_port' size='10'> Ex: 3306</td>
  384. </tr>
  385. <tr>
  386. <td valign='top'>" . st("Database Username:") . "</td>
  387. <td valign='top'><input type='text' name='db_user' value='$db_user' size='50'></td>
  388. </tr>
  389. <tr>
  390. <td valign='top'>" . st("Database Password:") . "</td>
  391. <td valign='top'><input type='password' name='db_pass' value='$db_pass' size='50'></td>
  392. </tr>
  393. </table>
  394. <br><br>
  395. <input type='submit' value='" . st("Install") . "'>
  396. <br>
  397. <b>" . st("Please click only once. May take several seconds to install.") . "
  398. </form>";
  399. // Display the screen
  400. $page_content = $pC;
  401. $page_title = "Install FlightPath";
  402. $page_hide_report_error = TRUE;
  403. include("themes/classic/fp_template.php");
  404. }
  405. /**
  406. * Check for missing requirements of the system.
  407. *
  408. * Returns an array of missing requirements which the user must fix before
  409. * installation can continue.
  410. *
  411. */
  412. function install_check_requirements() {
  413. $rtn = array();
  414. // Is the /custom directory writable?
  415. if (!is_writable("custom")) {
  416. $rtn[] = st("Please make sure the <em>/custom</em> directory is writable to the web server.
  417. <br>Ex: chmod 777 custom");
  418. }
  419. if (count($rtn) == 0) return FALSE;
  420. return $rtn;
  421. }
  422. /**
  423. * Displays the requirements on screen for the user.
  424. */
  425. function install_display_requirements($req_array) {
  426. global $lang;
  427. $pC = "";
  428. $pC .= "<h2 class='title'>" . st("Check Requirements") . "</h2>
  429. <p>" . st("The following requirements must be fixed before installation of FlightPath
  430. can continue.") . "</p>";
  431. foreach ($req_array as $req) {
  432. $pC .= "<div style='padding: 5px; margin: 10px; border: 1px solid red;
  433. font-family: Courier New, serif; font-size:0.9em'>$req</div>";
  434. }
  435. $pC .= "<p>" . st("Please fix the problems listed, then reload to try again:") . "
  436. <br><a href='install.php?lang=$lang'>" . st("Click here to try again") . "</a>";
  437. // Display the screen
  438. $page_content = $pC;
  439. $page_title = "Install FlightPath";
  440. $page_hide_report_error = TRUE;
  441. include("themes/classic/fp_template.php");
  442. }
  443. function install_display_lang_selection() {
  444. $pC = "";
  445. $pC .= "<h2 class='title'>Install FlightPath</h2>
  446. Please follow the instructions on the following pages to complete
  447. your installation of FlightPath.
  448. <h3 class='title'>Select language</h3>
  449. Please begin by selecting an installation language.
  450. <ul>
  451. <li><a href='install.php?lang=en'>English</a></li>
  452. </ul>
  453. <br><br><br>
  454. <b>Please note:</b> By proceeding with this installation, you affirm that you
  455. have read, understand, and agree with the LICENSE.txt file and the COPYRIGHT.txt file
  456. included with this software package.
  457. Specifically, that you accept and agree with the GNU GPL license, and with the statement
  458. that this software is provided to you without warranty. If you have any questions,
  459. please visit http://getflightpath.com/contact before proceeding with installation.";
  460. // Display the screen
  461. $page_content = $pC;
  462. $page_title = "Install FlightPath";
  463. $page_hide_report_error = TRUE;
  464. include("themes/classic/fp_template.php");
  465. }

Functions

Namesort descending Description
install_check_requirements Check for missing requirements of the system.
install_display_db_form Displays the form to let a user set up a new database
install_display_lang_selection
install_display_requirements Displays the requirements on screen for the user.
install_get_settings_file_template Returns a template for a new settings file.
install_perform_install Actually performs the installation of FlightPath