A tumblelog CMS built on AJAX, PHP and MySQL.

install.class.php 8.1KB

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