A tumblelog CMS built on AJAX, PHP and MySQL.

install.class.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. if(!defined('entry') || !entry) die('Not a valid page');
  3. require('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. if (!$this->create_config()) return false;
  24. $this->create_db();
  25. if (!$this->install_db()) return false;
  26. return true;
  27. }
  28. function create_db(){
  29. $link = mysql_connect($this->data['db_host'], $this->data['db_login'], $this->data['db_password']);
  30. if (!$link) {
  31. die('Could not connect: ' . mysql_error());
  32. }
  33. $sql = 'CREATE DATABASE ' . $this->data['db_name'];
  34. if (!mysql_query($sql, $link)) {
  35. $link = mysql_close($link);
  36. return false;
  37. }
  38. return true;
  39. }
  40. function install_db(){
  41. require('config.php');
  42. $db = new Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
  43. $sqlStr = array();
  44. $sqlStr[] = "CREATE TABLE `".Table_prefix."data` (
  45. `id_post` int(11) NOT NULL auto_increment,
  46. `title` text NULL,
  47. `url` varchar(250) default NULL,
  48. `description` text NULL,
  49. `type` tinyint(4) NOT NULL default '1',
  50. `date` datetime NOT NULL,
  51. `id_user` int(10) NOT NULL,
  52. PRIMARY KEY (`id_post`)
  53. ) ENGINE = MYISAM ;";
  54. $sqlStr[] = "CREATE TABLE `".Table_prefix."users` (
  55. `id_user` int(10) unsigned NOT NULL auto_increment,
  56. `name` varchar(100) default NULL,
  57. `login` varchar(100) NOT NULL default '',
  58. `password` varchar(64) NOT NULL default '',
  59. `email` varchar(100) default NULL,
  60. `website` varchar(150) default NULL,
  61. `about` text,
  62. PRIMARY KEY (`id_user`)
  63. ) ENGINE = MYISAM;";
  64. $sqlStr[] = "CREATE TABLE `".Table_prefix."config` (
  65. `posts_limit` int(3) NOT NULL,
  66. `title` varchar(250) NOT NULL,
  67. `description` text NOT NULL,
  68. `lang` varchar(10) NOT NULL,
  69. `template` varchar(100) NOT NULL,
  70. `url_installation` varchar(250) NOT NULL,
  71. PRIMARY KEY (`title`)
  72. ) ENGINE = MYISAM ;";
  73. $sqlStr[] = "CREATE TABLE `".Table_prefix."options` (
  74. `name` varchar(100) NOT NULL,
  75. `val` varchar(255) NOT NULL,
  76. PRIMARY KEY (`name`)
  77. ) ENGINE = MYISAM ;";
  78. $sqlStr[] = "CREATE TABLE `".Table_prefix."comments` (
  79. `id_comment` int(11) NOT NULL auto_increment,
  80. `id_post` int(11) NOT NULL,
  81. `username` varchar(50) NOT NULL,
  82. `email` varchar(100) NOT NULL,
  83. `web` varchar(250) default NULL,
  84. `content` text NOT NULL,
  85. `ip_user` varchar(50) NOT NULL,
  86. `comment_date` datetime NOT NULL,
  87. `spam` tinyint(4) NOT NULL,
  88. PRIMARY KEY (`id_comment`)
  89. ) ENGINE = MYISAM ;";
  90. $sqlStr[] = "CREATE TABLE `".Table_prefix."feeds` (
  91. `id_feed` int(11) NOT NULL auto_increment,
  92. `url` varchar(255) NOT NULL,
  93. `title` varchar(255) NOT NULL,
  94. `type` tinyint(4) NOT NULL default '1',
  95. `updated_at` datetime NOT NULL,
  96. `error` tinyint(1) NOT NULL default '0',
  97. `credits` int(1) NOT NULL default '0',
  98. `site_url` varchar(255) NOT NULL,
  99. `id_user` int(10) NOT NULL,
  100. PRIMARY KEY (`id_feed`)
  101. ) ENGINE=MyISAM ;";
  102. $sqlStr[] = "INSERT INTO `".Table_prefix."config` VALUES (". $this->data['posts_limit'] .", '".$this->data['title']."', '".$this->data['description']."', '".$this->data['lang']."', '".$this->data['template']."', '".$this->data['url_installation']."');";
  103. $sqlStr[] = "INSERT INTO `".Table_prefix."users` VALUES ('', '', '".$this->data['login']."', '".md5($this->data['password'])."', '".$this->data['email']."', '".$this->data['website']."', '".$this->data['about']."');";
  104. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('url_friendly', '0');";
  105. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('rich_text', '0');";
  106. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('allow_comments', '0');";
  107. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('offset_city', '".$this->data['offset_city']."');";
  108. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('offset_time', '".$this->data['offset_time']."');";
  109. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('shorten_links', '0');";
  110. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('rss_import_frec', '5 minutes');";
  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,$n)===false) {
  120. return false;
  121. } else {
  122. return true;
  123. }
  124. }
  125. function mostrarerror($n) {
  126. if ($this->inerrors($n)) {
  127. return '<span class="error">'.$this->errors_d[$n].'</span>';
  128. } else {
  129. return "";
  130. }
  131. }
  132. function is_gelato_installed(){
  133. if(file_exists('config.php')) {
  134. @include("config.php");
  135. if (!$this->check_for_config()){
  136. return false;
  137. } else {
  138. if (!$this->is_db_installed()){
  139. return false;
  140. }
  141. }
  142. return true;
  143. }else{
  144. return false;
  145. }
  146. }
  147. function is_db_installed(){
  148. $db = new Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
  149. $sqlStr = "SELECT * FROM `".Table_prefix."config`";
  150. if($db->ejecutarConsulta($sqlStr)) {
  151. return ($db->contarRegistros() > 0);
  152. }else{
  153. return false;
  154. }
  155. }
  156. function check_for_config(){
  157. if(!defined('DB_Server')) return false;
  158. if(!defined('DB_name')) return false;
  159. if(!defined('DB_User')) return false;
  160. if(!defined('DB_Password')) return false;
  161. return true;
  162. }
  163. function create_config(){
  164. $config = fopen("config.php", 'w+');
  165. $contents = '<?php
  166. if(!defined(\'entry\') || !entry) die(\'Not a valid page\');
  167. /* ===========================
  168. gelato CMS - A PHP based tumblelog CMS
  169. development version
  170. http://www.gelatocms.com/
  171. gelato CMS is a free software licensed under the GPL 2.0
  172. Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
  173. =========================== */
  174. define(\'DB_Server\', \''. $this->data['db_host'] . '\');
  175. define(\'DB_name\', \''. $this->data['db_name'] . '\');
  176. define(\'DB_User\', \''. $this->data['db_login'] . '\');
  177. define(\'DB_Password\', \''. $this->data['db_password'] . '\');
  178. define(\'Table_prefix\', \'gel_\');
  179. define(\'Absolute_Path\', dirname(__FILE__).DIRECTORY_SEPARATOR);
  180. ?>';
  181. if (fwrite($config, $contents) === FALSE) {
  182. $this->errors = "Could not write config file to directory";
  183. return false;
  184. }
  185. fclose($config);
  186. return true;
  187. }
  188. function check_form(){
  189. $action="";
  190. if (isset($this->data['action'])){
  191. $action=$this->data['action'];
  192. }
  193. if (!$this->is_gelato_installed()){
  194. $this->showForm = true;
  195. if ($action=="config") {
  196. $sep_err="";
  197. $this->errors = false;
  198. if (!$this->data['login']) {
  199. $this->errors =$this->errors.$sep_err."1";
  200. $sep_err="|";
  201. }
  202. if (!$this->data['db_login']) {
  203. $this->errors =$this->errors.$sep_err."10";
  204. $sep_err="|";
  205. }
  206. if (!$this->data['password']) {
  207. $this->errors=$this->errors.$sep_err."2";
  208. $sep_err="|";
  209. }
  210. if (!$this->data['email']) {
  211. $this->errors=$this->errors.$sep_err."4";
  212. $sep_err="|";
  213. }
  214. if (!$this->data['url_installation'] ) {
  215. $this->errors=$this->errors.$sep_err."5";
  216. $sep_err="|";
  217. }
  218. if (!$this->data['db_host'] ) {
  219. $this->errors=$this->errors.$sep_err."7";
  220. $sep_err="|";
  221. }
  222. if (!$this->data['db_name'] ) {
  223. $this->errors=$this->errors.$sep_err."8";
  224. $sep_err="|";
  225. }
  226. if ($this->data['password']!=$_POST['password2']) {
  227. $this->errors=$this->errors.$sep_err."3";
  228. $sep_err="|";
  229. }
  230. if ( $_POST['db_password']!=$_POST['db_password2']) {
  231. $this->errors=$this->errors.$sep_err."9";
  232. $sep_err="|";
  233. }
  234. $off_r= split("," , $this->data['time_offsets']);
  235. $this->data['offset_time'] = $off_r[0];
  236. $this->data['offset_city'] = $off_r[1];
  237. unset($this->data['time_offsets']);
  238. if (!$this->errors) {
  239. if ($this->run($this->data)) {
  240. $this->showForm=false;
  241. } else {
  242. $this->errors=$this->errors.$sep_err."6";
  243. $sep_err="|";
  244. $this->showForm=true;
  245. }
  246. } else {
  247. $this->showForm=true;
  248. }
  249. }
  250. }
  251. }
  252. }
  253. ?>