PageRenderTime 110ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/installers/php/dev_installer.php

https://github.com/letolabs/DIY
PHP | 223 lines | 198 code | 4 blank | 21 comment | 15 complexity | 04f7abe2d8f5d6cf7eb573d56aac670b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * CASH Music Dev Installer
  4. *
  5. * Takes the CASH DIY repo and sets it up as a working platform instance that
  6. * can be directly coded against. Command-line script.
  7. *
  8. * USAGE:
  9. * php installers/php/dev_installer.php
  10. * follow prompts.
  11. *
  12. *
  13. * @package diy.org.cashmusic
  14. * @author CASH Music
  15. * @link http://cashmusic.org/
  16. *
  17. * Copyright (c) 2011, CASH Music
  18. * Licensed under the Affero General Public License version 3.
  19. * See http://www.gnu.org/licenses/agpl-3.0.html
  20. */
  21. function readStdin($prompt, $valid_inputs = false, $default = '') {
  22. // Courtesy of http://us3.php.net/manual/en/features.commandline.io-streams.php#101307
  23. while(!isset($input) || (is_array($valid_inputs) && !in_array(strtolower($input), $valid_inputs))) {
  24. echo $prompt;
  25. $input = strtolower(trim(fgets(STDIN)));
  26. if(empty($input) && !empty($default)) {
  27. $input = $default;
  28. }
  29. }
  30. return $input;
  31. }
  32. if(!defined('STDIN')) { // force CLI, the browser is *so* 2007...
  33. echo "Please run installer from the command line. usage:<br / >&gt; php installers/php/dev_installer.php";
  34. } else {
  35. function findReplaceInFile($filename,$find,$replace) {
  36. if (is_file($filename)) {
  37. $file = file_get_contents($filename);
  38. $file = str_replace($find, $replace, $file);
  39. if (file_put_contents($filename, $file)) {
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. } else {
  45. return false;
  46. }
  47. }
  48. $success = false;
  49. echo "\n :+#\n"
  50. . " # +###+###`\n"
  51. . " ############\n"
  52. . " ###########:\n"
  53. . " ,###########`\n"
  54. . " ############\n"
  55. . " #############;\n"
  56. . " .################\n"
  57. . " :#################\n"
  58. . " ;##################\n"
  59. . " .##################\n"
  60. . " ;#################\n"
  61. . " #################;\n"
  62. . " ############ ###;\n"
  63. . " ############;###\n"
  64. . " ###,####+,###'##\n"
  65. . " ### ,# ### ##\n"
  66. . " ### # +## #+\n"
  67. . " ,## # .## ;+\n"
  68. . " ## #+ ## :#\n"
  69. . " ## ## ## '#\n"
  70. . " ## . ## ##\n"
  71. . " ## ## ##\n"
  72. . " `` \n\n"
  73. . " C A S H M U S I C\n"
  74. . " PLATFORM DEV INSTALLER";
  75. echo "\n\nYou are in an open field west of a big white house with a boarded front door.\n\n";
  76. // you can input <Enter> or 1, 2, 3
  77. $db_engine = readStdin('What database engine do you want to use? (\'mysql\'|\'sqlite\'): ', array('mysql', 'sqlite'));
  78. $installer_root = dirname(__FILE__);
  79. if ($db_engine == 'mysql') {
  80. if (@new PDO()) {
  81. echo "\nOh. Shit. Something's wrong: PDO is required.\n\n";
  82. die();
  83. }
  84. $db_server = readStdin('Database server (default: \'127.0.0.1:3306\'): ', false,'127.0.0.1:3306');
  85. $db_name = readStdin('Database name: ');
  86. $db_username = readStdin('Database username: ');
  87. $db_password = readStdin('Database password: ');
  88. // set up database, add user / password
  89. $user_password = substr(md5($system_salt . 'password'),4,7);
  90. $db_port = 3306;
  91. if (strpos($db_server,':') !== false) {
  92. $host_and_port = explode(':',$db_server);
  93. $db_address = $host_and_port[0];
  94. $db_port = $host_and_port[1];
  95. } else {
  96. $db_address = $db_server;
  97. }
  98. try {
  99. $pdo = new PDO ("mysql:host=$db_address;port=$db_port;dbname=$db_name",$db_username,$db_password);
  100. } catch (PDOException $e) {
  101. echo "\nOh. Shit. Something's wrong: Couldn't connect to the database. $e\n\n";
  102. die();
  103. break;
  104. }
  105. if ($pdo->query(file_get_contents(dirname(__FILE__) . '/../../framework/php/settings/sql/cashmusic_db.sql'))) {
  106. $success = true;
  107. } else {
  108. echo "\nOh. Shit. Something's wrong. Couldn't create database tables.\n\n";
  109. die();
  110. break;
  111. }
  112. } else if ($db_engine == 'sqlite') {
  113. // if the file exists already, rename it as a backup
  114. if (file_exists($installer_root . '/../../framework/db/cashmusic.sqlite')) {
  115. rename($installer_root . '/../../framework/db/cashmusic.sqlite',$installer_root . '/../../framework/db/cashmusic.sqlite.bak');
  116. } else {
  117. // if the directory was never created then create it now
  118. if (!file_exists($installer_root . '/../../framework/db')) {
  119. mkdir($installer_root . '/../../framework/db');
  120. }
  121. }
  122. chmod($installer_root . '/../../framework/db',0755);
  123. // connect to the new db...will create if not found
  124. try {
  125. $pdo = new PDO ('sqlite:' . $installer_root . '/../../framework/db/cashmusic.sqlite');
  126. $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  127. chmod($installer_root . '/../../framework/db/cashmusic.sqlite',0755);
  128. } catch (PDOException $e) {
  129. echo "\nOh. Shit. Something's wrong: Couldn't connect to the database. $e\n\n";
  130. die();
  131. break;
  132. }
  133. // push in all the tables
  134. try {
  135. $pdo->exec(file_get_contents($installer_root . '/../../framework/php/settings/sql/cashmusic_db_sqlite.sql'));
  136. $success = true;
  137. } catch (PDOException $e) {
  138. echo "\nOh. Shit. Something's wrong: Couldn't write to the database. $e\n\n";
  139. die();
  140. break;
  141. }
  142. }
  143. if ($success) {
  144. $user_email = readStdin("\nLogin email address: ");
  145. $user_password = readStdin("Login password: ");
  146. $default_salt = readStdin("\nUse defaut system salt? (y/n): ", false, 'y');
  147. if (strtolower(substr($default_salt,0,1)) == 'y') {
  148. $system_salt = 'I was born of sun beams; Warming up our limbs';
  149. } else {
  150. $system_salt = md5($user_email . time());
  151. }
  152. $password_hash = hash_hmac('sha256', $user_password, $system_salt);
  153. $data = array(
  154. 'email_address' => $user_email,
  155. 'password' => $password_hash,
  156. 'is_admin' => true,
  157. 'api_key' => $api_key = hash_hmac('md5', time() . $password_hash . rand(976654,1234567267), $system_salt) . substr((string) time(),6),
  158. 'api_secret' => hash_hmac('sha256', time() . $password_hash . rand(976654,1234567267), $system_salt),
  159. 'creation_date' => time()
  160. );
  161. $query = "INSERT INTO people (email_address,password,is_admin,api_key,api_secret,creation_date) VALUES (:email_address,:password,:is_admin,:api_key,:api_secret,:creation_date)";
  162. try {
  163. $q = $pdo->prepare($query);
  164. $success = $q->execute($data);
  165. } catch(PDOException $e) {
  166. echo "\nOh. Shit. Something's wrong. Couldn't add the user to the database. $e\n\n";
  167. die();
  168. break;
  169. }
  170. // modify settings files
  171. if (
  172. !copy($installer_root.'/../../framework/php/settings/cashmusic_template.ini.php',$installer_root.'/../../framework/php/settings/cashmusic.ini.php')
  173. ) {
  174. echo '\nOh. Shit. Something\'s wrong. Couldn\'t write the config file.\n\n'
  175. . 'the directory you specified for the framework.</p>';
  176. break;
  177. }
  178. // move source files into place
  179. $file_write_success = false;
  180. if ($db_engine == "sqlite") {
  181. if (
  182. findReplaceInFile($installer_root.'/../../framework/php/settings/cashmusic.ini.php','driver = "mysql','driver = "sqlite') &&
  183. findReplaceInFile($installer_root.'/../../framework/php/settings/cashmusic.ini.php','database = "cashmusic','database = "cashmusic.sqlite') &&
  184. findReplaceInFile($installer_root.'/../../framework/php/settings/cashmusic.ini.php','salt = "I was born of sun beams; Warming up our limbs','salt = "' . $system_salt)
  185. ) {
  186. $file_write_success = true;
  187. }
  188. } else {
  189. if (
  190. findReplaceInFile($installer_root.'/../../framework/php/settings/cashmusic.ini.php','hostname = "127.0.0.1:8889','hostname = "' . $db_server) &&
  191. findReplaceInFile($installer_root.'/../../framework/php/settings/cashmusic.ini.php','username = "root','username = "' . $db_username) &&
  192. findReplaceInFile($installer_root.'/../../framework/php/settings/cashmusic.ini.php','password = "root','password = "' . $db_password) &&
  193. findReplaceInFile($installer_root.'/../../framework/php/settings/cashmusic.ini.php','database = "cashmusic','database = "' . $db_name) &&
  194. findReplaceInFile($installer_root.'/../../framework/php/settings/cashmusic.ini.php','salt = "I was born of sun beams; Warming up our limbs','salt = "' . $system_salt)
  195. ) {
  196. $file_write_success = true;
  197. }
  198. }
  199. if (!$file_write_success) {
  200. echo "\nOh. Shit. Something's wrong. We had trouble editing a few files. Please try again.\n\n";
  201. break;
  202. } else {
  203. echo "\nSUCCESS!\n\nLogin using:\n\nemail: $user_email\npassword: $user_password\n\n";
  204. }
  205. }
  206. }
  207. ?>