PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/install/install2.php

https://gitlab.com/mucill/sman7
PHP | 411 lines | 352 code | 30 blank | 29 comment | 110 complexity | ef2a4528cd94d875f34201e30027042d MD5 | raw file
  1. <?php
  2. /**
  3. * Slims Installer files
  4. *
  5. * Copyright © 2006 - 2012 Advanced Power of PHP
  6. * Some modifications & patches by Eddy Subratha (eddy.subratha@gmail.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. require "settings.php";
  24. $completed = false;
  25. $error_mg = array();
  26. $indexdbupgrade_max = 17;
  27. function apphp_db_install_core($link, $database, $sql_file)
  28. {
  29. $db_error = false;
  30. if (!@apphp_db_select_db($link, $database)) {
  31. if (@apphp_db_query('create database ' . $database)) {
  32. apphp_db_select_db($link, $database);
  33. } else {
  34. $db_error = mysqli_error($link);
  35. return false;
  36. }
  37. }
  38. if (!$db_error) {
  39. if (file_exists($sql_file)) {
  40. include_once $sql_file;
  41. if(array_key_exists('create', $sql))
  42. {
  43. foreach ($sql['create'] as $value) {
  44. @mysqli_query($link, $value);
  45. }
  46. }
  47. if(array_key_exists('insert', $sql))
  48. {
  49. foreach ($sql['insert'] as $value) {
  50. @mysqli_query($link, $value);
  51. }
  52. }
  53. if(array_key_exists('alter', $sql))
  54. {
  55. foreach ($sql['alter'] as $value) {
  56. @mysqli_query($link, $value);
  57. }
  58. }
  59. if(array_key_exists('update', $sql))
  60. {
  61. foreach ($sql['update'] as $value) {
  62. @mysqli_query($link, $value);
  63. }
  64. }
  65. if(array_key_exists('delete', $sql))
  66. {
  67. foreach ($sql['delete'] as $value) {
  68. @mysqli_query($link, $value);
  69. }
  70. }
  71. if(array_key_exists('truncate', $sql))
  72. {
  73. foreach ($sql['truncate'] as $value) {
  74. @mysqli_query($link, $value);
  75. }
  76. }
  77. if(array_key_exists('drop', $sql))
  78. {
  79. foreach ($sql['drop'] as $value) {
  80. @mysqli_query($link, $value);
  81. }
  82. }
  83. return true;
  84. } else {
  85. $db_error = 'SQL file does not exist: ' . $sql_file;
  86. return false;
  87. }
  88. }
  89. }
  90. function apphp_db_select_db($link, $database) {
  91. return mysqli_select_db($link, $database);
  92. }
  93. function apphp_db_query($query) {
  94. global $link;
  95. $res=mysqli_query($link, $query);
  96. return $res;
  97. }
  98. function apphp_db_install($link, $database, $sql_file)
  99. {
  100. $db_error = false;
  101. if (!@apphp_db_select_db($link, $database)) {
  102. if (@apphp_db_query('create database ' . $database)) {
  103. apphp_db_select_db($link, $database);
  104. } else {
  105. $db_error = mysqli_error($link);
  106. return false;
  107. }
  108. }
  109. if (!$db_error) {
  110. if (file_exists($sql_file)) {
  111. $fd = fopen($sql_file, 'rb');
  112. $restore_query = fread($fd, filesize($sql_file));
  113. fclose($fd);
  114. } else {
  115. $db_error = 'SQL file does not exist: ' . $sql_file;
  116. return false;
  117. }
  118. $sql_array = array();
  119. $sql_length = strlen($restore_query);
  120. $pos = strpos($restore_query, ';');
  121. for ($i=$pos; $i<$sql_length; $i++) {
  122. if ($restore_query[0] == '#') {
  123. $restore_query = ltrim(substr($restore_query, strpos($restore_query, "\n")));
  124. $sql_length = strlen($restore_query);
  125. $i = strpos($restore_query, ';')-1;
  126. continue;
  127. }
  128. if (@$restore_query[($i+1)] == "\n") {
  129. for ($j=($i+2); $j<$sql_length; $j++) {
  130. if (trim($restore_query[$j]) != '') {
  131. $next = substr($restore_query, $j, 6);
  132. if ($next[0] == '#') {
  133. // find out where the break position is so we can remove this line (#comment line)
  134. for ($k=$j; $k<$sql_length; $k++) {
  135. if ($restore_query[$k] == "\n") break;
  136. }
  137. $query = substr($restore_query, 0, $i+1);
  138. $restore_query = substr($restore_query, $k);
  139. // join the query before the comment appeared, with the rest of the dump
  140. $restore_query = $query . $restore_query;
  141. $sql_length = strlen($restore_query);
  142. $i = strpos($restore_query, ';')-1;
  143. continue 2;
  144. }
  145. break;
  146. }
  147. }
  148. if ($next == '') { // get the last insert query
  149. $next = 'insert';
  150. }
  151. if ( (preg_match('/create/i', $next)) || (preg_match('/insert/i', $next)) || (preg_match('/drop/i', $next)) ) {
  152. $next = '';
  153. $sql_array[] = substr($restore_query, 0, $i);
  154. $restore_query = ltrim(substr($restore_query, $i+1));
  155. $sql_length = strlen($restore_query);
  156. $i = strpos($restore_query, ';')-1;
  157. }
  158. }
  159. }
  160. for ($i=0; $i < sizeof($sql_array); $i++) {
  161. apphp_db_query($sql_array[$i]);
  162. }
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. }
  168. if ($_POST['submit'] == "step2") {
  169. $database_host = isset($_POST['database_host'])?$_POST['database_host']:"";
  170. $database_name = isset($_POST['database_name'])?$_POST['database_name']:"";
  171. $database_username = isset($_POST['database_username'])?$_POST['database_username']:"";
  172. $database_password = isset($_POST['database_password'])?$_POST['database_password']:"";
  173. $database_sample = isset($_POST['install_sample'])?$_POST['install_sample']:"";
  174. $username = isset($_POST['username'])?$_POST['username']:"";
  175. $password = isset($_POST['password'])?$_POST['password']:"";
  176. $retype_password = isset($_POST['retype_password'])?$_POST['retype_password']:"";
  177. if (empty($database_host)){
  178. $error_mg[] = "<li>Database host can not be empty </li>";
  179. }
  180. if (empty($database_name)){
  181. $error_mg[] = "<li>Database name can not be empty</li>";
  182. }
  183. if (empty($database_username)){
  184. $error_mg[] = "<li>Database username can not be empty</li>";
  185. }
  186. if(trim($username) <> 'admin')
  187. {
  188. if (!empty($password)){
  189. if (empty($retype_password)){
  190. $error_mg[] = "<li>Please retype your password</li>";
  191. }
  192. if ($password <> $retype_password){
  193. $error_mg[] = "<li>Your password did not match. Please try again</li>";
  194. }
  195. } else {
  196. $retype_password = 'admin';
  197. }
  198. } else {
  199. if (!empty($password)){
  200. if (empty($retype_password)){
  201. $error_mg[] = "<li>Please retype your password</li>";
  202. }
  203. if ($password <> $retype_password){
  204. $error_mg[] = "<li>Your password did not match. Please try again</li>";
  205. }
  206. } else {
  207. $retype_password = 'admin';
  208. }
  209. }
  210. $sql_update = " UPDATE user set
  211. username = '".$username."',
  212. passwd = '".password_hash($retype_password, PASSWORD_BCRYPT)."',
  213. realname = '".ucfirst($username)."',
  214. last_login = NULL,
  215. last_login_ip = '127.0.0.1',
  216. groups = 'a:1:{i:0;s:1:\"1\";}',
  217. input_date = DATE(NOW()),
  218. last_update = DATE(NOW())
  219. WHERE user_id = 1";
  220. if(empty($error_mg)){
  221. $config_file = file_get_contents($config_file_default);
  222. $config_file = str_replace("_DB_HOST_", $database_host, $config_file);
  223. $config_file = str_replace("_DB_NAME_", $database_name, $config_file);
  224. $config_file = str_replace("_DB_USER_", $database_username, $config_file);
  225. $config_file = str_replace("_DB_PASSWORD_", $database_password, $config_file);
  226. if(!copy('../config/sysconfig.local.inc-sample.php',$config_file_path))
  227. {
  228. $error_mg[] = "<li>Could not create file ".$config_file_name."! Please check if the sysconfig.local.inc-sample.php file is exists</li>";
  229. }
  230. else {
  231. @chmod($config_file_path,0777);
  232. $f = @fopen($config_file_path, "w+");
  233. if (@fwrite($f, $config_file) > 0) {
  234. $link = @mysqli_connect($database_host, $database_username, $database_password);
  235. if($link){
  236. if (@mysqli_select_db($link, $database_name)) {
  237. // upgrade db
  238. if (isset($_POST['indexdbupgrade'])) {
  239. $indexdbupgrade_start = $_POST['indexdbupgrade'];
  240. $completed_upgrade = 0;
  241. for ($i=$indexdbupgrade_start; $i <= $indexdbupgrade_max; $i++) {
  242. $v = $i + 1;
  243. $file_sql_path = ($i == $indexdbupgrade_max) ? './../upgrade/'.$sql_upgrade[$v] : './../upgrade/old_sql/'.$sql_upgrade[$v];
  244. $sql_php_path = 'sql_php_upgrade/'.$sql_upgrade[$v].'.php';
  245. if ($v >= 13 || $v == 11 || $v == 9) {
  246. if (false == ($db_error = apphp_db_install_core($link, $database_name, $sql_php_path))) {
  247. $error_mg[] = "<li>Could not read file ".$sql_php_path."! Please check if the file exists</li>";
  248. } else {
  249. $completed_upgrade++;
  250. }
  251. } else {
  252. if(false == ($db_error = apphp_db_install($link, $database_name, $file_sql_path))){
  253. $error_mg[] = "<li>Could not read file ".$file_sql_path."! Please check if the file exists</li>";
  254. }else{
  255. $completed_upgrade++;
  256. }
  257. }
  258. }
  259. if ($completed_upgrade != ($indexdbupgrade_max - $indexdbupgrade_start)) {
  260. $completed = true;
  261. } else {
  262. $error_mg[] = "<li>".$completed_upgrade." Database imported.</li>";
  263. @unlink($config_file_path);
  264. }
  265. } else {
  266. // fresh install db
  267. if(false == ($db_error = apphp_db_install_core($link, $database_name, $sql_dump))){
  268. $error_mg[] = "<li>Could not read file ".$sql_dump."! Please check if the file exists</li>";
  269. @unlink($config_file_path);
  270. }
  271. else{
  272. // install sampel data
  273. if($_POST['install_sample'] == 'yes'){
  274. if(false == ($db_error = apphp_db_install($link, $database_name, $sql_sample))){
  275. $error_mg[] = "<li>Could not read file ".$sql_sample."! Please check if the file exists</li>";
  276. }else{
  277. $completed = true;
  278. }
  279. } else {
  280. $completed = true;
  281. }
  282. if(!empty($retype_password))
  283. {
  284. apphp_db_query($sql_update);
  285. }
  286. }
  287. }
  288. }
  289. else {
  290. // create database
  291. $create = @mysqli_query($link, 'CREATE DATABASE '.$database_name.' CHARACTER SET utf8 COLLATE utf8_unicode_ci');
  292. if ($create) {
  293. // fresh install db
  294. if(false == ($db_error = apphp_db_install_core($link, $database_name, $sql_dump))){
  295. $error_mg[] = "<li>Could not read file ".$sql_dump."! Please check if the file exists</li>";
  296. @unlink($config_file_path);
  297. }
  298. else{
  299. // install sampel data
  300. if($_POST['install_sample'] == 'yes'){
  301. if(false == ($db_error = apphp_db_install($link, $database_name, $sql_sample))){
  302. $error_mg[] = "<li>Could not read file ".$sql_sample."! Please check if the file exists</li>";
  303. }else{
  304. $completed = true;
  305. }
  306. } else {
  307. $completed = true;
  308. }
  309. if(!empty($retype_password))
  310. {
  311. apphp_db_query($sql_update);
  312. }
  313. }
  314. }
  315. else {
  316. $error_mg[] = "<li>Database connecting error! Check your database exists or make sure you have privileges to create database.</li>";
  317. @unlink($config_file_path);
  318. }
  319. }
  320. }
  321. else {
  322. $error_mg[] = "<li>Database connecting error! Check your connection parameters</li>";
  323. @unlink($config_file_path);
  324. }
  325. }
  326. else {
  327. $error_mg[] = "<li>Can not open configuration file ".$config_file_directory.$config_file_name."</li>";
  328. }
  329. @fclose($f);
  330. @chmod($config_file_path,0755);
  331. }
  332. }
  333. }
  334. ?>
  335. <!DOCTYPE HTML>
  336. <html>
  337. <head>
  338. <title>Step 3 | Slims Installer</title>
  339. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  340. <link rel="stylesheet" type="text/css" href="styles.css">
  341. <link rel="shortcut icon" href="img/webicon.ico" type="image/x-icon"/>
  342. </head>
  343. <body>
  344. <div class="wrapper">
  345. <?php if(!$completed) { ?>
  346. <div class="content">
  347. <div class="title">
  348. <h2>Step 3 - Installation Not Completed</h2>
  349. </div>
  350. <p class="error">Please correct your information according to this message</p>
  351. <?php
  352. foreach($error_mg as $msg){
  353. echo "<ul class=\"list\">".$msg."</ul>";
  354. }
  355. ?>
  356. <hr>
  357. <div class="toright">
  358. <input type="button" class="button" value="Back" name="submit" onclick="javascript: history.go(-1);">
  359. <input type="button" class="button" value="Retry" name="submit" onclick="javascript: location.reload();">
  360. </div>
  361. <br/>
  362. </div>
  363. <?php } else { ?>
  364. <div class="content">
  365. <div class="title">
  366. <h2>Step 3 - Installation Completed</h2>
  367. </div>
  368. <p class="success">Hooray, the installation was successful</p>
  369. <p>The <?php echo $config_file_name;?> file was sucessfully created.</p>
  370. <p>For security reasons, please remove <code style="font-weight: bold;">install/</code> folder from your server.</p>
  371. <hr>
  372. <div class="toright">
  373. <?php if($application_start_file != ""){ ?><a href="<?php echo $application_start_file;?>" class="button">OK, start the SLiMS</a><?php } ?>
  374. </div>
  375. <br/>
  376. <?php } ?>
  377. </div>
  378. <?php include_once("footer.php"); ?>
  379. </div>
  380. </body>
  381. </html>