PageRenderTime 1321ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/install.php

https://github.com/copyninja/friendika
PHP | 249 lines | 197 code | 51 blank | 1 comment | 35 complexity | 5f46c79158bdc2a5cdb403bfedd2b1d4 MD5 | raw file
  1. <?php
  2. function install_post(&$a) {
  3. global $db;
  4. $urlpath = $a->get_path();
  5. $dbhost = notags(trim($_POST['dbhost']));
  6. $dbuser = notags(trim($_POST['dbuser']));
  7. $dbpass = notags(trim($_POST['dbpass']));
  8. $dbdata = notags(trim($_POST['dbdata']));
  9. $timezone = notags(trim($_POST['timezone']));
  10. $phpath = notags(trim($_POST['phpath']));
  11. $adminmail = notags(trim($_POST['adminmail']));
  12. require_once("dba.php");
  13. unset($db);
  14. $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
  15. if(mysqli_connect_errno()) {
  16. unset($db);
  17. $db = new dba($dbhost, $dbuser, $dbpass, '', true);
  18. if(! mysqli_connect_errno()) {
  19. $r = q("CREATE DATABASE '%s'",
  20. dbesc($dbdata)
  21. );
  22. if($r) {
  23. unset($db);
  24. $db = new dba($dbhost, $dbuser, $dbpass, $dbdata, true);
  25. }
  26. }
  27. if(mysqli_connect_errno()) {
  28. notice( t('Could not create/connect to database.') . EOL);
  29. return;
  30. }
  31. }
  32. info( t('Connected to database.') . EOL);
  33. $tpl = get_intltext_template('htconfig.tpl');
  34. $txt = replace_macros($tpl,array(
  35. '$dbhost' => $dbhost,
  36. '$dbuser' => $dbuser,
  37. '$dbpass' => $dbpass,
  38. '$dbdata' => $dbdata,
  39. '$timezone' => $timezone,
  40. '$urlpath' => $urlpath,
  41. '$phpath' => $phpath,
  42. '$adminmail' => $adminmail
  43. ));
  44. $result = file_put_contents('.htconfig.php', $txt);
  45. if(! $result) {
  46. $a->data = $txt;
  47. }
  48. $errors = load_database($db);
  49. if($errors)
  50. $a->data['db_failed'] = true;
  51. else
  52. $a->data['db_installed'] = true;
  53. return;
  54. }
  55. function install_content(&$a) {
  56. global $db;
  57. $o = '';
  58. if(x($a->data,'db_installed')) {
  59. $o .= '<h2>' . t('Proceed with Installation') . '</h2>';
  60. $o .= '<p style="font-size: 130%;">';
  61. $o .= t('Your Friendika site database has been installed.') . EOL;
  62. $o .= t('IMPORTANT: You will need to [manually] setup a scheduled task for the poller.') . EOL ;
  63. $o .= t('Please see the file "INSTALL.txt".') . EOL ;
  64. $o .= '<br />';
  65. $o .= '<a href="' . $a->get_baseurl() . '/register' . '">' . t('Proceed to registration') . '</a>' ;
  66. $o .= '</p>';
  67. return $o;
  68. }
  69. if(x($a->data,'db_failed')) {
  70. $o .= t('Database import failed.') . EOL;
  71. $o .= t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;
  72. $o .= t('Please see the file "INSTALL.txt".') . EOL ;
  73. return $o;
  74. }
  75. if($db && $db->connected) {
  76. $r = q("SELECT COUNT(*) as `total` FROM `user`");
  77. if($r && count($r) && $r[0]['total']) {
  78. notice( t('Permission denied.') . EOL);
  79. return '';
  80. }
  81. }
  82. info( t('Welcome to Friendika.') . EOL);
  83. check_funcs();
  84. $o .= check_htconfig();
  85. if(strlen($o))
  86. return $o;
  87. if(strlen($a->data)) {
  88. $o .= manual_config($a);
  89. return;
  90. }
  91. $o .= check_php($phpath);
  92. $o .= check_keys();
  93. require_once('datetime.php');
  94. $tpl = get_markup_template('install_db.tpl');
  95. $o .= replace_macros($tpl, array(
  96. '$lbl_01' => t('Friendika Social Network'),
  97. '$lbl_02' => t('Installation'),
  98. '$lbl_03' => t('In order to install Friendika we need to know how to connect to your database.'),
  99. '$lbl_04' => t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
  100. '$lbl_05' => t('The database you specify below should already exist. If it does not, please create it before continuing.'),
  101. '$lbl_06' => t('Database Server Name'),
  102. '$lbl_07' => t('Database Login Name'),
  103. '$lbl_08' => t('Database Login Password'),
  104. '$lbl_09' => t('Database Name'),
  105. '$lbl_10' => t('Please select a default timezone for your website'),
  106. '$lbl_11' => t('Site administrator email address. Your account email address must match this in order to use the web admin panel.'),
  107. '$baseurl' => $a->get_baseurl(),
  108. '$tzselect' => ((x($_POST,'timezone')) ? select_timezone($_POST['timezone']) : select_timezone()),
  109. '$submit' => t('Submit'),
  110. '$dbhost' => ((x($_POST,'dbhost')) ? notags(trim($_POST['dbhost'])) : 'localhost'),
  111. '$dbuser' => notags(trim($_POST['dbuser'])),
  112. '$dbpass' => notags(trim($_POST['dbpass'])),
  113. '$dbdata' => notags(trim($_POST['dbdata'])),
  114. '$phpath' => $phpath,
  115. '$adminmail' => notags(trim($_POST['adminmail']))
  116. ));
  117. return $o;
  118. }
  119. function check_php(&$phpath) {
  120. $o = '';
  121. $phpath = trim(shell_exec('which php'));
  122. if(! strlen($phpath)) {
  123. $o .= t('Could not find a command line version of PHP in the web server PATH.') . EOL;
  124. $o .= t('This is required. Please adjust the configuration file .htconfig.php accordingly.') . EOL;
  125. }
  126. if(strlen($phpath)) {
  127. $str = autoname(8);
  128. $cmd = "$phpath testargs.php $str";
  129. $result = trim(shell_exec($cmd));
  130. if($result != $str) {
  131. $o .= t('The command line version of PHP on your system does not have "register_argc_argv" enabled.') . EOL;
  132. $o .= t('This is required for message delivery to work.') . EOL;
  133. }
  134. }
  135. return $o;
  136. }
  137. function check_keys() {
  138. $o = '';
  139. $res = false;
  140. if(function_exists('openssl_pkey_new'))
  141. $res=openssl_pkey_new(array(
  142. 'digest_alg' => 'sha1',
  143. 'private_key_bits' => 4096,
  144. 'encrypt_key' => false ));
  145. // Get private key
  146. if(! $res) {
  147. $o .= t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys') . EOL;
  148. $o .= t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".') . EOL;
  149. }
  150. return $o;
  151. }
  152. function check_funcs() {
  153. if((function_exists('apache_get_modules')) && (! in_array('mod_rewrite',apache_get_modules())))
  154. notice( t('Error: Apache webserver mod-rewrite module is required but not installed.') . EOL);
  155. if(! function_exists('curl_init'))
  156. notice( t('Error: libCURL PHP module required but not installed.') . EOL);
  157. if(! function_exists('imagecreatefromjpeg'))
  158. notice( t('Error: GD graphics PHP module with JPEG support required but not installed.') . EOL);
  159. if(! function_exists('openssl_public_encrypt'))
  160. notice( t('Error: openssl PHP module required but not installed.') . EOL);
  161. if(! function_exists('mysqli_connect'))
  162. notice( t('Error: mysqli PHP module required but not installed.') . EOL);
  163. if(! function_exists('mb_strlen'))
  164. notice( t('Error: mb_string PHP module required but not installed.') . EOL);
  165. if((x($_SESSION,'sysmsg')) && strlen($_SESSION['sysmsg']))
  166. notice( t('Please see the file "INSTALL.txt".') . EOL);
  167. }
  168. function check_htconfig() {
  169. if(((file_exists('.htconfig.php')) && (! is_writable('.htconfig.php')))
  170. || (! is_writable('.'))) {
  171. $o = t('The web installer needs to be able to create a file called ".htconfig.php" in the top folder of your web server and it is unable to do so.');
  172. $o .= t('This is most often a permission setting, as the web server may not be able to write files in your folder - even if you can.');
  173. $o .= t('Please check with your site documentation or support people to see if this situation can be corrected.');
  174. $o .= t('If not, you may be required to perform a manual installation. Please see the file "INSTALL.txt" for instructions.');
  175. }
  176. return $o;
  177. }
  178. function manual_config(&$a) {
  179. $data = htmlentities($a->data);
  180. $o = t('The database configuration file ".htconfig.php" could not be written. Please use the enclosed text to create a configuration file in your web server root.');
  181. $o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
  182. return $o;
  183. }
  184. function load_database($db) {
  185. $str = file_get_contents('database.sql');
  186. $arr = explode(';',$str);
  187. $errors = 0;
  188. foreach($arr as $a) {
  189. if(strlen(trim($a))) {
  190. $r = @$db->q(trim($a));
  191. if(! $r) {
  192. notice( t('Errors encountered creating database tables.') . $a . EOL);
  193. $errors ++;
  194. }
  195. }
  196. }
  197. return $errors;
  198. }