A tumblelog CMS built on AJAX, PHP and MySQL.

install.class.php 8.1KB

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 (". $db->sql_escape($this->data['posts_limit']).", '".$db->sql_escape($this->data['title'])."', '".$db->sql_escape($this->data['description'])."', '".$db->sql_escape($this->data['lang'])."', '".$db->sql_escape($this->data['template'])."', '".$db->sql_escape($this->data['url_installation'])."');";
  102. $sqlStr[] = "INSERT INTO `".Table_prefix."users` VALUES ('', '', '".$db->sql_escape($this->data['login'])."', '".md5($this->data['password'])."', '".$db->sql_escape($this->data['email'])."', '".$db->sql_escape($this->data['website'])."', '".$db->sql_escape($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', '".$db->sql_escape($this->data['offset_city'])."');";
  107. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('offset_time', '".$db->sql_escape($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. $sqlStr[] = "INSERT INTO `".Table_prefix."options` VALUES ('check_version', '1');";
  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(Absolute_Path.'config.php')) {
  134. include_once(Absolute_Path."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 check_form(){
  164. $action="";
  165. if (isset($this->data['action'])){
  166. $action=$this->data['action'];
  167. }
  168. if (!$this->is_gelato_installed()){
  169. $this->showForm = true;
  170. if ($action=="config") {
  171. $sep_err="";
  172. $this->errors = false;
  173. if (!$this->data['login']) {
  174. $this->errors =$this->errors.$sep_err."1";
  175. $sep_err="|";
  176. }
  177. if (!$this->data['db_login']) {
  178. $this->errors =$this->errors.$sep_err."10";
  179. $sep_err="|";
  180. }
  181. if (!$this->data['password']) {
  182. $this->errors=$this->errors.$sep_err."2";
  183. $sep_err="|";
  184. }
  185. if (!$this->data['email']) {
  186. $this->errors=$this->errors.$sep_err."4";
  187. $sep_err="|";
  188. }
  189. if (!$this->data['url_installation'] ) {
  190. $this->errors=$this->errors.$sep_err."5";
  191. $sep_err="|";
  192. }
  193. if (!$this->data['db_host'] ) {
  194. $this->errors=$this->errors.$sep_err."7";
  195. $sep_err="|";
  196. }
  197. if (!$this->data['db_name'] ) {
  198. $this->errors=$this->errors.$sep_err."8";
  199. $sep_err="|";
  200. }
  201. if ($this->data['password']!=$_POST['password2']) {
  202. $this->errors=$this->errors.$sep_err."3";
  203. $sep_err="|";
  204. }
  205. if ( $_POST['db_password']!=$_POST['db_password2']) {
  206. $this->errors=$this->errors.$sep_err."9";
  207. $sep_err="|";
  208. }
  209. $off_r= split("," , $this->data['time_offsets']);
  210. $this->data['offset_time'] = $off_r[0];
  211. $this->data['offset_city'] = $off_r[1];
  212. unset($this->data['time_offsets']);
  213. if (!$this->errors) {
  214. if ($this->run($this->data)) {
  215. $this->showForm=false;
  216. } else {
  217. $this->errors=$this->errors.$sep_err."6";
  218. $sep_err="|";
  219. $this->showForm=true;
  220. }
  221. } else {
  222. $this->showForm=true;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. ?>