A tumblelog CMS built on AJAX, PHP and MySQL.

install.class.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. if (!defined('entry') || !entry) {
  3. die('Not a valid page');
  4. }
  5. require(Absolute_Path.'/classes/mysql_connection.class.php');
  6. class Install
  7. {
  8. public $data = null;
  9. public $errors = null;
  10. public $showForm;
  11. public $errors_d = array();
  12. public function __construct()
  13. {
  14. $this->errors_d[1]="The login field cannot be empty";
  15. $this->errors_d[2]="The password field cannot be empty";
  16. $this->errors_d[3]="Password does not match the confirm password";
  17. $this->errors_d[4]="The e-mail field cannot be empty";
  18. $this->errors_d[5]="The installation URL field cannot be empty";
  19. $this->errors_d[6]="Error establishing a database connection";
  20. $this->errors_d[7]="Please add a hostname for the database server";
  21. $this->errors_d[8]="Please name the database";
  22. $this->errors_d[9]="Password does not match the confirm password";
  23. $this->errors_d[10]="The login field cannot be empty";
  24. }
  25. public function run()
  26. {
  27. if (empty($this->data)) {
  28. false;
  29. }
  30. $this->create_db();
  31. if (!$this->install_db()) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. public function create_db()
  37. {
  38. $db_host = $this->data['db_host'];
  39. $db_name = $this->data['db_name'];
  40. $db_login = $this->data['db_login'];
  41. $db_password = $this->data['db_password'];
  42. $link = new PDO("mysql:host=$db_host;dbname=$db_name", $db_login, $db_password);
  43. if (!$link) {
  44. die('Could not connect: ' . $link->errorInfo());
  45. }
  46. $sql = 'CREATE DATABASE IF NOT EXISTS ' . $this->data['db_name'];
  47. if (!$link->query($sql)) {
  48. $link = NULL;
  49. return false;
  50. }
  51. return true;
  52. }
  53. public function install_db()
  54. {
  55. require_once(Absolute_Path.'config.php');
  56. $db = new Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
  57. $sqlStr = array();
  58. $sqlStr[] = "CREATE TABLE `".Table_prefix."data` ( `id_post` INT(11) NOT NULL AUTO_INCREMENT , `title` TEXT NULL , `url` VARCHAR(250) NULL DEFAULT NULL , `description` TEXT NULL , `type` TINYINT(4) NOT NULL DEFAULT '1' , `date` DATETIME NOT NULL , `id_user` INT(10) NOT NULL , PRIMARY KEY (`id_post`)) ENGINE = MyISAM;";
  59. $sqlStr[] = "CREATE TABLE `".Table_prefix."users` ( `id_user` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT , `name` VARCHAR(100) NULL DEFAULT NULL , `login` VARCHAR(100) NOT NULL DEFAULT '' , `password` VARCHAR(64) NOT NULL DEFAULT '' , `email` VARCHAR(100) NULL DEFAULT NULL , `website` VARCHAR(150) NULL DEFAULT NULL , `about` TEXT NOT NULL , PRIMARY KEY (`id_user`)) ENGINE = MyISAM;";
  60. $sqlStr[] = "CREATE TABLE `".Table_prefix."config` ( `posts_limit` INT(3) NOT NULL , `title` VARCHAR(250) NOT NULL , `description` TEXT NOT NULL , `lang` VARCHAR(10) NOT NULL , `template` VARCHAR(100) NOT NULL , `url_installation` VARCHAR(250) NOT NULL , PRIMARY KEY (`title`)) ENGINE = MyISAM;";
  61. $sqlStr[] = "CREATE TABLE `".Table_prefix."options` ( `name` VARCHAR(100) NOT NULL , `val` VARCHAR(255) NOT NULL , PRIMARY KEY (`name`)) ENGINE = MyISAM;";
  62. $sqlStr[] = "CREATE TABLE `".Table_prefix."comments` ( `id_comment` INT(11) NOT NULL AUTO_INCREMENT , `id_post` INT(11) NOT NULL , `username` VARCHAR(50) NOT NULL , `email` VARCHAR(100) NOT NULL , `web` VARCHAR(250) NULL DEFAULT NULL , `content` TEXT NOT NULL , `ip_user` VARCHAR(50) NOT NULL , `comment_date` DATETIME NOT NULL , `spam` TINYINT(4) NOT NULL , PRIMARY KEY (`id_comment`)) ENGINE = MyISAM;";
  63. $sqlStr[] = "CREATE TABLE `".Table_prefix."feeds` ( `id_feed` INT(11) NOT NULL AUTO_INCREMENT , `url` VARCHAR(255) NOT NULL , `title` VARCHAR(255) NOT NULL , `type` TINYINT(4) NOT NULL DEFAULT '1' , `updated_at` DATETIME NOT NULL , `error` TINYINT(1) NOT NULL DEFAULT '0' , `credits` INT(1) NOT NULL DEFAULT '0' , `site_url` VARCHAR(255) NOT NULL , `id_user` INT(10) NOT NULL , PRIMARY KEY (`id_feed`)) ENGINE = MyISAM;";
  64. $sqlStr[] = "INSERT INTO `".Table_prefix."config` VALUES (". $db->sql_escape($this->data['posts_limit']).", ".$db->sql_escape($this->data['title']).", ".$db->sql_escape($this->data['description']).", ".$db->sql_escape($this->data['lang']).", ".$db->sql_escape($this->data['template']).", ".$db->sql_escape($this->data['url_installation']).");";
  65. $sqlStr[] = "INSERT INTO `".Table_prefix."users` (name, login, password, email, website, about) VALUES ('', ".$db->sql_escape($this->data['login']).", '".md5($this->data['password'])."', ".$db->sql_escape($this->data['email']).", ".$db->sql_escape($this->data['website']).", ".$db->sql_escape($this->data['about']).");";
  66. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('url_friendly', '0');";
  67. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('rich_text', '0');";
  68. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('allow_comments', '0');";
  69. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('offset_city', ".$db->sql_escape($this->data['offset_city']).");";
  70. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('offset_time', ".$db->sql_escape($this->data['offset_time']).");";
  71. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('shorten_links', '0');";
  72. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('rss_import_frec', '5 minutes');";
  73. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('check_version', '1');";
  74. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('active_plugins', '[{\"total\":0},[]]');";
  75. foreach ($sqlStr as $key => $query) {
  76. if (!$db->ejecutarConsulta($query)) {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. public function inerrors($n)
  83. {
  84. if (strpos($this->errors, (string)$n)===false) {
  85. return false;
  86. }
  87. return true;
  88. }
  89. public function mostrarerror($n)
  90. {
  91. if ($this->inerrors($n)) {
  92. return '<span class="error">'.$this->errors_d[$n].'</span>';
  93. } else {
  94. return "";
  95. }
  96. }
  97. public function is_gelato_installed()
  98. {
  99. if (file_exists(Absolute_Path.'config.php')) {
  100. include_once(Absolute_Path."config.php");
  101. if (!$this->check_for_config()) {
  102. return false;
  103. } else {
  104. if (!$this->is_db_installed()) {
  105. return false;
  106. }
  107. }
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113. public function is_db_installed()
  114. {
  115. $db = new Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
  116. $sqlStr = "SELECT * FROM `".Table_prefix."config`";
  117. if ($db->ejecutarConsulta($sqlStr)) {
  118. return ($db->contarRegistros() > 0);
  119. } else {
  120. return false;
  121. }
  122. }
  123. public function check_for_config()
  124. {
  125. if (!defined('DB_Server')) {
  126. return false;
  127. }
  128. if (!defined('DB_name')) {
  129. return false;
  130. }
  131. if (!defined('DB_User')) {
  132. return false;
  133. }
  134. if (!defined('DB_Password')) {
  135. return false;
  136. }
  137. return true;
  138. }
  139. public function check_form()
  140. {
  141. $action="";
  142. if (isset($this->data['action'])) {
  143. $action=$this->data['action'];
  144. }
  145. if (!$this->is_gelato_installed()) {
  146. $this->showForm = true;
  147. if ($action=="config") {
  148. $sep_err="";
  149. $this->errors = false;
  150. if (!$this->data['login']) {
  151. $this->errors =$this->errors.$sep_err."1";
  152. $sep_err="|";
  153. }
  154. if (!$this->data['db_login']) {
  155. $this->errors =$this->errors.$sep_err."10";
  156. $sep_err="|";
  157. }
  158. if (!$this->data['password']) {
  159. $this->errors=$this->errors.$sep_err."2";
  160. $sep_err="|";
  161. }
  162. if (!$this->data['email']) {
  163. $this->errors=$this->errors.$sep_err."4";
  164. $sep_err="|";
  165. }
  166. if (!$this->data['url_installation']) {
  167. $this->errors=$this->errors.$sep_err."5";
  168. $sep_err="|";
  169. }
  170. if (!$this->data['db_host']) {
  171. $this->errors=$this->errors.$sep_err."7";
  172. $sep_err="|";
  173. }
  174. if (!$this->data['db_name']) {
  175. $this->errors=$this->errors.$sep_err."8";
  176. $sep_err="|";
  177. }
  178. if ($this->data['password']!=$_POST['password2']) {
  179. $this->errors=$this->errors.$sep_err."3";
  180. $sep_err="|";
  181. }
  182. if ($_POST['db_password']!=$_POST['db_password2']) {
  183. $this->errors=$this->errors.$sep_err."9";
  184. $sep_err="|";
  185. }
  186. $off_r= explode(",", $this->data['time_offsets']);
  187. $this->data['offset_time'] = $off_r[0];
  188. $this->data['offset_city'] = $off_r[1];
  189. unset($this->data['time_offsets']);
  190. if (!$this->errors) {
  191. if ($this->run($this->data)) {
  192. $this->showForm=false;
  193. } else {
  194. $this->errors=$this->errors.$sep_err."6";
  195. $sep_err="|";
  196. $this->showForm=true;
  197. }
  198. } else {
  199. $this->showForm=true;
  200. }
  201. }
  202. }
  203. }
  204. }