A tumblelog CMS built on AJAX, PHP and MySQL.

install.class.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if (!$this->create_db()) {
  31. return false;
  32. }
  33. if (!$this->install_db()) {
  34. return false;
  35. }
  36. return true;
  37. }
  38. public function create_db()
  39. {
  40. $db_host = $this->data['db_host'];
  41. $db_name = $this->data['db_name'];
  42. $db_login = $this->data['db_login'];
  43. $db_password = $this->data['db_password'];
  44. try {
  45. $link = new PDO("mysql:host=$db_host;dbname=$db_name", $db_login, $db_password);
  46. }
  47. catch (Exception $e)
  48. {
  49. return false;
  50. }
  51. if (!$link) {
  52. die('Could not connect: ' . $link->errorInfo());
  53. }
  54. $sql = 'CREATE DATABASE IF NOT EXISTS ' . $this->data['db_name'];
  55. if (!$link->query($sql)) {
  56. $link = NULL;
  57. return false;
  58. }
  59. return true;
  60. }
  61. public function install_db()
  62. {
  63. $db = new Conexion_Mysql($this->data['db_name'], $this->data['db_host'], $this->data['db_login'], $this->data['db_password']);
  64. $sqlStr = array();
  65. $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;";
  66. $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;";
  67. $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;";
  68. $sqlStr[] = "CREATE TABLE `".Table_prefix."options` ( `name` VARCHAR(100) NOT NULL , `val` VARCHAR(255) NOT NULL , PRIMARY KEY (`name`)) ENGINE = MyISAM;";
  69. $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;";
  70. $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;";
  71. $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']).");";
  72. $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']).");";
  73. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('url_friendly', '0');";
  74. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('rich_text', '0');";
  75. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('allow_comments', '0');";
  76. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('offset_city', ".$db->sql_escape($this->data['offset_city']).");";
  77. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('offset_time', ".$db->sql_escape($this->data['offset_time']).");";
  78. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('shorten_links', '0');";
  79. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('rss_import_frec', '5 minutes');";
  80. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('check_version', '1');";
  81. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('active_plugins', '[{\"total\":0},[]]');";
  82. foreach ($sqlStr as $key => $query) {
  83. if (!$db->ejecutarConsulta($query)) {
  84. return false;
  85. }
  86. }
  87. if($this->writeConfig($this->data['db_name'], $this->data['db_host'], $this->data['db_login'], $this->data['db_password']) === FALSE)
  88. {
  89. return false;
  90. }
  91. return true;
  92. }
  93. public function inerrors($n)
  94. {
  95. if (strpos($this->errors, (string)$n)===false) {
  96. return false;
  97. }
  98. return true;
  99. }
  100. public function writeConfig($name, $host, $login, $password)
  101. {
  102. $strToWrite = "define('DB_Server', '$host');\ndefine('DB_name', '$name');\ndefine('DB_User', '$login');\ndefine('DB_Password', '$password');\n";
  103. return file_put_contents(Absolute_Path.'config.php', $strToWrite, FILE_APPEND | LOCK_EX);
  104. }
  105. public function mostrarerror($n)
  106. {
  107. if ($this->inerrors($n)) {
  108. return '<span class="error">'.$this->errors_d[$n].'</span>';
  109. } else {
  110. return "";
  111. }
  112. }
  113. public function is_gelato_installed()
  114. {
  115. if (file_exists(Absolute_Path.'config.php')) {
  116. include_once(Absolute_Path."config.php");
  117. if (!$this->check_for_config()) {
  118. return false;
  119. } else {
  120. if (!$this->is_db_installed()) {
  121. return false;
  122. }
  123. }
  124. return true;
  125. } else {
  126. return false;
  127. }
  128. }
  129. public function is_db_installed()
  130. {
  131. $db = new Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
  132. $sqlStr = "SELECT * FROM `".Table_prefix."config`";
  133. if ($db->ejecutarConsulta($sqlStr)) {
  134. return ($db->contarRegistros() > 0);
  135. } else {
  136. return false;
  137. }
  138. }
  139. public function check_for_config()
  140. {
  141. if (!defined('DB_Server')) {
  142. return false;
  143. }
  144. if (!defined('DB_name')) {
  145. return false;
  146. }
  147. if (!defined('DB_User')) {
  148. return false;
  149. }
  150. if (!defined('DB_Password')) {
  151. return false;
  152. }
  153. return true;
  154. }
  155. public function check_form()
  156. {
  157. $action="";
  158. if (isset($this->data['action'])) {
  159. $action=$this->data['action'];
  160. }
  161. if (!$this->is_gelato_installed()) {
  162. $this->showForm = true;
  163. if ($action=="config") {
  164. $sep_err="";
  165. $this->errors = false;
  166. if (!$this->data['login']) {
  167. $this->errors =$this->errors.$sep_err."1";
  168. $sep_err="|";
  169. }
  170. if (!$this->data['db_login']) {
  171. $this->errors =$this->errors.$sep_err."10";
  172. $sep_err="|";
  173. }
  174. if (!$this->data['password']) {
  175. $this->errors=$this->errors.$sep_err."2";
  176. $sep_err="|";
  177. }
  178. if (!$this->data['email']) {
  179. $this->errors=$this->errors.$sep_err."4";
  180. $sep_err="|";
  181. }
  182. if (!$this->data['url_installation']) {
  183. $this->errors=$this->errors.$sep_err."5";
  184. $sep_err="|";
  185. }
  186. if (!$this->data['db_host']) {
  187. $this->errors=$this->errors.$sep_err."7";
  188. $sep_err="|";
  189. }
  190. if (!$this->data['db_name']) {
  191. $this->errors=$this->errors.$sep_err."8";
  192. $sep_err="|";
  193. }
  194. if ($this->data['password']!=$_POST['password2']) {
  195. $this->errors=$this->errors.$sep_err."3";
  196. $sep_err="|";
  197. }
  198. if ($_POST['db_password']!=$_POST['db_password2']) {
  199. $this->errors=$this->errors.$sep_err."9";
  200. $sep_err="|";
  201. }
  202. $off_r= explode(",", $this->data['time_offsets']);
  203. $this->data['offset_time'] = $off_r[0];
  204. $this->data['offset_city'] = $off_r[1];
  205. unset($this->data['time_offsets']);
  206. if (!$this->errors) {
  207. if ($this->run($this->data)) {
  208. $this->showForm=false;
  209. } else {
  210. $this->errors=$this->errors.$sep_err."6";
  211. $sep_err="|";
  212. $this->showForm=true;
  213. }
  214. } else {
  215. $this->showForm=true;
  216. }
  217. }
  218. }
  219. }
  220. }