A tumblelog CMS built on AJAX, PHP and MySQL.

install.class.php 7.8KB

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