A tumblelog CMS built on AJAX, PHP and MySQL.

install.class.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 (". $this->data['posts_limit'] .", '".$this->data['title']."', '".$this->data['description']."', '".$this->data['lang']."', '".$this->data['template']."', '".$this->data['url_installation']."');";
  102. $sqlStr[] = "INSERT INTO `".Table_prefix."users` VALUES ('', '', '".$this->data['login']."', '".md5($this->data['password'])."', '".$this->data['email']."', '".$this->data['website']."', '".$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', '".$this->data['offset_city']."');";
  107. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('offset_time', '".$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. foreach($sqlStr as $key => $query){
  111. if(!$db->ejecutarConsulta($query)){
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. function inerrors($n) {
  118. if ( strpos($this->errors,$n)===false) {
  119. return false;
  120. } else {
  121. return true;
  122. }
  123. }
  124. function mostrarerror($n) {
  125. if ($this->inerrors($n)) {
  126. return '<span class="error">'.$this->errors_d[$n].'</span>';
  127. } else {
  128. return "";
  129. }
  130. }
  131. function is_gelato_installed(){
  132. if(file_exists(Absolute_Path.'config.php')) {
  133. include_once(Absolute_Path."config.php");
  134. if (!$this->check_for_config()){
  135. return false;
  136. } else {
  137. if (!$this->is_db_installed()){
  138. return false;
  139. }
  140. }
  141. return true;
  142. }else{
  143. return false;
  144. }
  145. }
  146. function is_db_installed(){
  147. $db = new Conexion_Mysql(DB_name, DB_Server, DB_User, DB_Password);
  148. $sqlStr = "SELECT * FROM `".Table_prefix."config`";
  149. if($db->ejecutarConsulta($sqlStr)) {
  150. return ($db->contarRegistros() > 0);
  151. }else{
  152. return false;
  153. }
  154. }
  155. function check_for_config(){
  156. if(!defined('DB_Server')) return false;
  157. if(!defined('DB_name')) return false;
  158. if(!defined('DB_User')) return false;
  159. if(!defined('DB_Password')) return false;
  160. return true;
  161. }
  162. function check_form(){
  163. $action="";
  164. if (isset($this->data['action'])){
  165. $action=$this->data['action'];
  166. }
  167. if (!$this->is_gelato_installed()){
  168. $this->showForm = true;
  169. if ($action=="config") {
  170. $sep_err="";
  171. $this->errors = false;
  172. if (!$this->data['login']) {
  173. $this->errors =$this->errors.$sep_err."1";
  174. $sep_err="|";
  175. }
  176. if (!$this->data['db_login']) {
  177. $this->errors =$this->errors.$sep_err."10";
  178. $sep_err="|";
  179. }
  180. if (!$this->data['password']) {
  181. $this->errors=$this->errors.$sep_err."2";
  182. $sep_err="|";
  183. }
  184. if (!$this->data['email']) {
  185. $this->errors=$this->errors.$sep_err."4";
  186. $sep_err="|";
  187. }
  188. if (!$this->data['url_installation'] ) {
  189. $this->errors=$this->errors.$sep_err."5";
  190. $sep_err="|";
  191. }
  192. if (!$this->data['db_host'] ) {
  193. $this->errors=$this->errors.$sep_err."7";
  194. $sep_err="|";
  195. }
  196. if (!$this->data['db_name'] ) {
  197. $this->errors=$this->errors.$sep_err."8";
  198. $sep_err="|";
  199. }
  200. if ($this->data['password']!=$_POST['password2']) {
  201. $this->errors=$this->errors.$sep_err."3";
  202. $sep_err="|";
  203. }
  204. if ( $_POST['db_password']!=$_POST['db_password2']) {
  205. $this->errors=$this->errors.$sep_err."9";
  206. $sep_err="|";
  207. }
  208. $off_r= split("," , $this->data['time_offsets']);
  209. $this->data['offset_time'] = $off_r[0];
  210. $this->data['offset_city'] = $off_r[1];
  211. unset($this->data['time_offsets']);
  212. if (!$this->errors) {
  213. if ($this->run($this->data)) {
  214. $this->showForm=false;
  215. } else {
  216. $this->errors=$this->errors.$sep_err."6";
  217. $sep_err="|";
  218. $this->showForm=true;
  219. }
  220. } else {
  221. $this->showForm=true;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. ?>