A tumblelog CMS built on AJAX, PHP and MySQL.

install.class.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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[] = "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']."');";
  89. $sqlStr[] = "INSERT INTO `".Table_prefix."users` VALUES ('', '', '".$this->data['login']."', '".md5($this->data['password'])."', '".$this->data['email']."', '".$this->data['website']."', '".$this->data['about']."');";
  90. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('url_friendly', '1');";
  91. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('rich_text', '0');";
  92. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('allow_comments', '0');";
  93. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('offset_city', '".$this->data['offset_city']."');";
  94. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('offset_time', '".$this->data['offset_time']."');";
  95. foreach($sqlStr as $key => $query){
  96. if(!$db->ejecutarConsulta($query)){
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. function inerrors($n) {
  103. if ( strpos($this->errors,$n)===false) {
  104. return false;
  105. } else {
  106. return true;
  107. }
  108. }
  109. function mostrarerror($n) {
  110. if ($this->inerrors($n)) {
  111. return '<span class="error">'.$this->errors_d[$n].'</span>';
  112. } else {
  113. return "";
  114. }
  115. }
  116. function is_gelato_installed(){
  117. if (!$this->check_for_config()){
  118. return false;
  119. } else {/*
  120. if (!$this->is_db_installed()){
  121. return false;
  122. }
  123. */
  124. }
  125. return true;
  126. }
  127. /*
  128. function is_db_installed(){
  129. global $db;
  130. if (function_exists($db->ejecutarConsulta)){
  131. $sqlStr = "SELECT * FROM `".Table_prefix."config`";
  132. if($db->ejecutarConsulta($sqlStr)) {
  133. return ($db->contarRegistros() > 0);
  134. }
  135. } else {
  136. false;
  137. }
  138. }
  139. */
  140. function check_for_config(){
  141. if(!file_exists('config.php')) return false;
  142. if(!defined('DB_Server')) return false;
  143. if(!defined('DB_name')) return false;
  144. if(!defined('DB_User')) return false;
  145. if(!defined('DB_Password')) return false;
  146. return true;
  147. }
  148. function create_config(){
  149. $config = fopen("config.php", 'w+');
  150. $contents = '<?php
  151. if(!defined(\'entry\') || !entry) die(\'Not a valid page\');
  152. /* ===========================
  153. gelato CMS - A PHP based tumblelog CMS
  154. development version
  155. http://www.gelatocms.com/
  156. gelato CMS is a free software licensed under the GPL 2.0
  157. Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
  158. =========================== */
  159. define(\'DB_Server\', \''. $this->data['db_host'] . '\');
  160. define(\'DB_name\', \''. $this->data['db_name'] . '\');
  161. define(\'DB_User\', \''. $this->data['db_login'] . '\');
  162. define(\'DB_Password\', \''. $this->data['db_password'] . '\');
  163. define(\'Table_prefix\', \'gel_\');
  164. define(\'Absolute_Path\', dirname(__FILE__).DIRECTORY_SEPARATOR);
  165. ?>';
  166. if (fwrite($config, $contents) === FALSE) {
  167. $this->errors = "Could not write config file to directory";
  168. return false;
  169. }
  170. fclose($config);
  171. return true;
  172. }
  173. function check_form(){
  174. $action="";
  175. if (isset($this->data['action'])){
  176. $action=$this->data['action'];
  177. }
  178. if (!$this->is_gelato_installed()){
  179. $this->showForm = true;
  180. if ($action=="config") {
  181. $sep_err="";
  182. $this->errors = false;
  183. if (!$this->data['login'] || !$this->data['db_login']) {
  184. $this->errors =$this->errors.$sep_err."1";
  185. $sep_err="|";
  186. }
  187. if (!$this->data['password'] || !$this->data['db_password']) {
  188. $this->errors=$this->errors.$sep_err."2";
  189. $sep_err="|";
  190. }
  191. if (!$this->data['email']) {
  192. $this->errors=$this->errors.$sep_err."4";
  193. $sep_err="|";
  194. }
  195. if (!$this->data['url_installation'] ) {
  196. $this->errors=$this->errors.$sep_err."5";
  197. $sep_err="|";
  198. }
  199. if (!$this->data['db_host'] ) {
  200. $this->errors=$this->errors.$sep_err."7";
  201. $sep_err="|";
  202. }
  203. if (!$this->data['db_name'] ) {
  204. $this->errors=$this->errors.$sep_err."8";
  205. $sep_err="|";
  206. }
  207. if ($this->data['password']!=$_POST['password2'] || $_POST['db_password']!=$_POST['db_password2'] ) {
  208. $this->errors=$this->errors.$sep_err."3";
  209. $sep_err="|";
  210. }
  211. $off_r= split("," , $this->data['time_offsets']);
  212. $this->data['offset_time'] = $off_r[0];
  213. $this->data['offset_city'] = $off_r[1];
  214. unset($this->data['time_offsets']);
  215. if (!$this->errors) {
  216. if ($this->run($this->data)) {
  217. $this->showForm=false;
  218. } else {
  219. $this->errors=$this->errors.$sep_err."6";
  220. $sep_err="|";
  221. $this->showForm=true;
  222. }
  223. } else {
  224. $this->showForm=true;
  225. }
  226. }
  227. }
  228. }
  229. }
  230. ?>