A tumblelog CMS built on AJAX, PHP and MySQL.

install.class.php 8.6KB

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