PageRenderTime 114ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/setup/lib/Setup.class.php

http://github.com/h9k/magirc
PHP | 290 lines | 237 code | 20 blank | 33 comment | 64 complexity | c8819f31d76724d2e13b032a729331f4 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. // Database configuration
  3. class Magirc_DB extends DB {
  4. private static $instance = NULL;
  5. public static function getInstance() {
  6. if (is_null(self::$instance) === true) {
  7. $db = array();
  8. if (file_exists(__DIR__.'/../../conf/magirc.cfg.php')) {
  9. include(__DIR__.'/../../conf/magirc.cfg.php');
  10. } else {
  11. die ('magirc.cfg.php configuration file missing');
  12. }
  13. $dsn = "mysql:dbname={$db['database']};host={$db['hostname']}";
  14. $args = array();
  15. if (isset($db['ssl']) && $db['ssl_key']) $args[PDO::MYSQL_ATTR_SSL_KEY] = $db['ssl_key'];
  16. if (isset($db['ssl']) && $db['ssl_cert']) $args[PDO::MYSQL_ATTR_SSL_CERT] = $db['ssl_cert'];
  17. if (isset($db['ssl']) && $db['ssl_ca']) $args[PDO::MYSQL_ATTR_SSL_CA] = $db['ssl_ca'];
  18. self::$instance = new DB($dsn, $db['username'], $db['password'], $args);
  19. }
  20. return self::$instance;
  21. }
  22. }
  23. class Setup {
  24. public $db;
  25. public $tpl;
  26. function __construct() {
  27. $loader = new Twig_Loader_Filesystem(__DIR__.'/../tpl');
  28. $this->tpl = new Twig_Environment($loader, array(
  29. 'cache' => __DIR__ . '/../../tmp',
  30. 'debug' => true
  31. ));
  32. // We skip db connection in the first steps for check purposes
  33. if (@$_GET['step'] > 2) {
  34. $this->db = Magirc_DB::getInstance();
  35. }
  36. }
  37. /**
  38. * Makes preliminary requirements checks
  39. * @return array
  40. */
  41. function requirementsCheck() {
  42. $status = array('error' => false);
  43. if (version_compare("5.5.0", phpversion(), "<") == 1) {
  44. $status['php'] = true;
  45. } else {
  46. $status['php'] = false;
  47. $status['error'] = true;
  48. }
  49. if (extension_loaded('pdo') == 1 && in_array('mysql', PDO::getAvailableDrivers())) {
  50. $status['pdo'] = true;
  51. } else {
  52. $status['pdo'] = false;
  53. $status['error'] = true;
  54. }
  55. if (extension_loaded('gettext') == 1) {
  56. $status['gettext'] = true;
  57. } else {
  58. $status['gettext'] = false;
  59. $status['error'] = true;
  60. }
  61. if (extension_loaded('xml') == 1) {
  62. $status['xml'] = true;
  63. } else {
  64. $status['xml'] = false;
  65. $status['error'] = true;
  66. }
  67. if (file_exists(MAGIRC_CFG_FILE)) {
  68. if (is_writable(MAGIRC_CFG_FILE)) {
  69. $status['writable'] = true;
  70. } else {
  71. $status['writable'] = false;
  72. }
  73. } else {
  74. if (@copy('../conf/magirc.cfg.dist.php', MAGIRC_CFG_FILE)) {
  75. $status['writable'] = true;
  76. } else {
  77. $status['writable'] = false;
  78. }
  79. }
  80. if (is_writable('../tmp')) {
  81. $status['tmp'] = true;
  82. } else {
  83. $status['tmp'] = false;
  84. $status['error'] = true;
  85. }
  86. return $status;
  87. }
  88. /**
  89. * Saves the MagIRC SQL configuration file
  90. */
  91. function saveConfig() {
  92. if (isset($_POST['savedb'])) {
  93. $ssl = isset($_POST['ssl']) ? 'true' : 'false';
  94. $db_buffer =
  95. "<?php
  96. \$db['username'] = '{$_POST['username']}';
  97. \$db['password'] = '{$_POST['password']}';
  98. \$db['database'] = '{$_POST['database']}';
  99. \$db['hostname'] = '{$_POST['hostname']}';
  100. \$db['port'] = '{$_POST['port']}';
  101. \$db['ssl'] = $ssl;
  102. \$db['ssl_key'] = '{$_POST['ssl_key']}';
  103. \$db['ssl_cert'] = '{$_POST['ssl_cert']}';
  104. \$db['ssl_ca'] = '{$_POST['ssl_ca']}';
  105. ";
  106. if (is_writable(MAGIRC_CFG_FILE)) {
  107. $writefile = fopen(MAGIRC_CFG_FILE,"w");
  108. fwrite($writefile, $db_buffer);
  109. fclose($writefile);
  110. }
  111. return $db_buffer;
  112. }
  113. return null;
  114. }
  115. /**
  116. * Checks if the configuration table is there
  117. * @return PDOStatement Configuration
  118. */
  119. function configCheck() {
  120. $query = "SHOW TABLES LIKE 'magirc_config'";
  121. $this->db->query($query, SQL_INIT);
  122. return $this->db->record;
  123. }
  124. /**
  125. * Gets the Database schema version
  126. * @return int Version
  127. */
  128. private function getDbVersion() {
  129. $result = $this->db->selectOne('magirc_config', array('parameter' => 'db_version'));
  130. return $result['value'];
  131. }
  132. /**
  133. * Loads the configuration table schema to the Denora database, for fresh installs
  134. * @return boolean
  135. */
  136. function configDump() {
  137. $file_content = file('sql/schema.sql');
  138. $query = "";
  139. foreach($file_content as $sql_line) {
  140. $tsl = trim($sql_line);
  141. if (($sql_line != "") && (substr($tsl, 0, 2) != "--") && (substr($tsl, 0, 1) != "#")) {
  142. $query .= $sql_line;
  143. if(preg_match("/;\s*$/", $sql_line)) {
  144. $query = str_replace(";", "", "$query");
  145. $result = $this->db->query($query);
  146. if (!$result) {
  147. return false;
  148. }
  149. $query = "";
  150. }
  151. }
  152. }
  153. return true;
  154. }
  155. /**
  156. * Generates the base url
  157. * @return string
  158. */
  159. function generateBaseUrl() {
  160. $base_url = @$_SERVER['HTTPS'] ? 'https://' : 'http://';
  161. $base_url .= $_SERVER['SERVER_NAME'];
  162. $base_url .= $_SERVER['SERVER_PORT'] == 80 ? '' : ':'.$_SERVER['SERVER_PORT'];
  163. $base_url .= str_replace('setup/index.php', '', $_SERVER['SCRIPT_NAME']);
  164. return (substr($base_url, -1) == "/") ? substr($base_url, 0, -1) : $base_url;
  165. }
  166. /**
  167. * Upgrade the MagIRC database
  168. * @return boolean true: updated, false: no update needed
  169. */
  170. function configUpgrade() {
  171. $version = $this->getDbVersion();
  172. $updated = false;
  173. if ($version != DB_VERSION) {
  174. if ($version < 2) {
  175. $this->db->insert('magirc_config', array('parameter' => 'live_interval', 'value' => 15));
  176. $this->db->insert('magirc_config', array('parameter' => 'cdn_enable', 'value' => 0));
  177. }
  178. if ($version < 3) {
  179. $this->db->insert('magirc_config', array('parameter' => 'rewrite_enable', 'value' => 0));
  180. }
  181. if ($version < 4) {
  182. $this->db->insert('magirc_config', array('parameter' => 'timezone', 'value' => 'UTC'));
  183. }
  184. if ($version < 5) {
  185. $this->db->insert('magirc_config', array('parameter' => 'welcome_mode', 'value' => 'statuspage'));
  186. $this->db->query("CREATE TABLE IF NOT EXISTS `magirc_content` (
  187. `name` varchar(16) NOT NULL default '', `text` text NOT NULL default '',
  188. PRIMARY KEY (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  189. $welcome_msg = $this->db->selectOne('magirc_config', array('parameter' => 'msg_welcome'));
  190. $this->db->insert('magirc_content', array('name' => 'welcome', 'text' => $welcome_msg['value']));
  191. $this->db->delete('magirc_config', array('parameter' => 'msg_welcome'));
  192. $this->db->query("ALTER TABLE `magirc_config` CHANGE `value` `value` VARCHAR( 64 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT ''");
  193. $this->db->query("ALTER TABLE `magirc_config` ENGINE = InnoDB");
  194. }
  195. if ($version < 6) {
  196. $this->db->insert('magirc_config', array('parameter' => 'block_spchans', 'value' => 0));
  197. $this->db->insert('magirc_config', array('parameter' => 'net_roundrobin', 'value' => ''));
  198. $this->db->insert('magirc_config', array('parameter' => 'service_adsense_id', 'value' => ''));
  199. $this->db->insert('magirc_config', array('parameter' => 'service_adsense_channel', 'value' => ''));
  200. $this->db->insert('magirc_config', array('parameter' => 'service_searchirc', 'value' => ''));
  201. $this->db->insert('magirc_config', array('parameter' => 'service_netsplit', 'value' => ''));
  202. }
  203. if ($version < 7) {
  204. $this->db->insert('magirc_config', array('parameter' => 'version_show', 'value' => '1'));
  205. }
  206. if ($version < 8) {
  207. $this->db->insert('magirc_config', array('parameter' => 'net_port', 'value' => '6667'));
  208. $this->db->insert('magirc_config', array('parameter' => 'net_port_ssl', 'value' => ''));
  209. $roundrobin = $this->db->selectOne('magirc_config', array('parameter' => 'net_roundrobin'));
  210. if ($roundrobin['value']) {
  211. $array = explode(':', $roundrobin['value']);
  212. $this->db->update('magirc_config', array('value' => $array[0]), array('parameter' => 'net_roundrobin'));
  213. if (count($array) > 1) {
  214. $this->db->update('magirc_config', array('value' => $array[1]), array('parameter' => 'net_port'));
  215. }
  216. }
  217. $this->db->insert('magirc_config', array('parameter' => 'service_webchat', 'value' => ''));
  218. $this->db->insert('magirc_config', array('parameter' => 'service_mibbit', 'value' => ''));
  219. $this->db->insert('magirc_config', array('parameter' => 'service_addthis', 'value' => '0'));
  220. }
  221. if ($version < 9) {
  222. $this->db->insert('magirc_config', array('parameter' => 'denora_version', 'value' => '1.4'));
  223. }
  224. if ($version < 10) {
  225. $this->db->query("ALTER TABLE magirc_config CHANGE value value VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT ''");
  226. }
  227. if ($version < 11) {
  228. $base_url = $this->generateBaseUrl();
  229. $this->db->insert('magirc_config', array('parameter' => 'base_url', 'value' => $base_url));
  230. }
  231. if ($version < 13) {
  232. $this->db->insert('magirc_config', array('parameter' => 'service_mibbitid', 'value' => ''));
  233. }
  234. if ($version < 14) {
  235. $this->db->delete('magirc_config', array('parameter' => 'denora_version'));
  236. $this->db->insert('magirc_config', array('parameter' => 'service', 'value' => 'denora'));
  237. }
  238. if ($version < 15) {
  239. $this->db->insert('magirc_config', array('parameter' => 'hide_nickaliases', 'value' => 0));
  240. }
  241. if ($version < 16) {
  242. $block_spchans = $this->db->selectOne('magirc_config', array('parameter' => 'block_spchans'));
  243. $this->db->insert('magirc_config', array('parameter' => 'block_schans', 'value' => $block_spchans['value']));
  244. $this->db->insert('magirc_config', array('parameter' => 'block_pchans', 'value' => $block_spchans['value']));
  245. $this->db->delete('magirc_config', array('parameter' => 'block_spchans'));
  246. }
  247. if ($version < 17) {
  248. $this->db->delete('magirc_config', array('parameter' => 'service_searchirc'));
  249. }
  250. if ($version < 18) {
  251. $base_url = $this->generateBaseUrl();
  252. $this->db->update('magirc_config', array('value' => $base_url), array('parameter' => 'base_url'));
  253. }
  254. if ($version < 19) {
  255. $this->db->insert('magirc_config', array('parameter' => 'service_webchat_urlencode', 'value' => 1));
  256. }
  257. $this->db->update('magirc_config', array('value' => DB_VERSION), array('parameter' => 'db_version'));
  258. $updated = true;
  259. }
  260. return $updated;
  261. }
  262. /**
  263. * Checks if there are any admins in the admin table
  264. * @return boolean true: yes, false: no
  265. */
  266. function checkAdmins() {
  267. $this->db->query("SELECT id FROM magirc_admin", SQL_INIT);
  268. return $this->db->record ? true : false;
  269. }
  270. }