PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/install/Plugin/Install/Model.php

https://gitlab.com/x33n/ImpressPages
PHP | 183 lines | 134 code | 36 blank | 13 comment | 7 complexity | fe251312d6b582b0292a5664581f975a MD5 | raw file
  1. <?php
  2. /**
  3. * @package ImpressPages
  4. */
  5. namespace Plugin\Install;
  6. class Model
  7. {
  8. protected static $installationDir = null;
  9. public static function setInstallationDir($installationDir)
  10. {
  11. self::$installationDir = $installationDir;
  12. }
  13. public static function ipFile($path)
  14. {
  15. $path = str_replace('Plugin/', 'install/Plugin/', $path);
  16. if (self::$installationDir) {
  17. return self::$installationDir . $path;
  18. } else {
  19. return ipFile($path);
  20. }
  21. }
  22. public static function createAndUseDatabase($database)
  23. {
  24. $db = ipDb();
  25. try {
  26. $db->execute('USE `' . $database . '`');
  27. } catch (\PDOException $e) {
  28. try {
  29. $db->execute("CREATE DATABASE `".$database."` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;");
  30. $db->execute('USE `' . $database . '`');
  31. } catch (\PDOException $e2) {
  32. throw new \Ip\Exception('Could not create database');
  33. }
  34. }
  35. return true;
  36. }
  37. public static function createDatabaseStructure($database, $tablePrefix)
  38. {
  39. $sql = file_get_contents(self::ipFile('Plugin/Install/sql/structure.sql'));
  40. $sql = str_replace("[[[[database]]]]", $database, $sql);
  41. $sql = str_replace("DROP TABLE IF EXISTS `ip_", "DROP TABLE IF EXISTS `". $tablePrefix, $sql);
  42. $sql = str_replace("CREATE TABLE `ip_", "CREATE TABLE `".$tablePrefix, $sql);
  43. $errors = array();
  44. ipDb()->execute($sql);
  45. return $errors;
  46. }
  47. public static function importData($tablePrefix)
  48. {
  49. $errors = array();
  50. $sqlFile = self::ipFile('Plugin/Install/sql/data.sql');
  51. $fh = fopen($sqlFile, 'r');
  52. $sql = fread($fh, utf8_decode(filesize($sqlFile)));
  53. fclose($fh);
  54. $sql = str_replace("INSERT INTO `ip_", "INSERT INTO `". $tablePrefix, $sql);
  55. $sql = str_replace("[[[[version]]]]", ipApplication()->getVersion(), $sql);
  56. $sql = str_replace("[[[[dbversion]]]]", \Ip\Internal\Update\Model::getDbVersion(), $sql);
  57. $sql = str_replace("[[[[time]]]]", date('Y-m-d H:i:s'), $sql);
  58. $sql = str_replace("[[[[timestamp]]]]", time(), $sql);
  59. ipDb()->execute($sql);
  60. return $errors;
  61. }
  62. public static function writeConfigFile($config, $filename)
  63. {
  64. $configInfo = array(
  65. // GLOBAL
  66. 'sessionName' => array(
  67. 'value' => 'changeThis',
  68. 'comment' => 'prevents session conflict when two sites runs on the same server',
  69. ),
  70. 'developmentEnvironment' => array(
  71. 'value' => 1,
  72. 'comment' => 'displays error and debug information. Change to 0 before deployment to production server',
  73. ),
  74. 'showErrors' => array(
  75. 'value' => 1,
  76. 'comment' => "0 if you don't wish to display errors on the page",
  77. ),
  78. 'debugMode' => array(
  79. 'value' => 0,
  80. 'comment' => "Debug mode loads raw unminified JavaScript files, alerts AJAX errors.",
  81. ),
  82. 'rewritesDisabled' => array(
  83. 'value' => null, // this value will not be written to config if not changed
  84. 'comment' => 'Install has not detected mod_rewrite. Delete this line if mod_rewrite (http://www.impresspages.org/help/mod-rewrite) is enabled or you are using Nginx and have made required changes to the configuration file (http://www.impresspages.org/help/nginx).'
  85. ),
  86. // END GLOBAL
  87. // FRONTEND
  88. 'timezone' => array(
  89. 'value' => 'changeThis',
  90. 'comment' => 'PHP 5 requires timezone to be set.',
  91. ),
  92. // DB
  93. 'db' => array(
  94. 'value' => array(
  95. 'hostname' => 'localhost',
  96. 'username' => '',
  97. 'password' => '',
  98. 'database' => '',
  99. 'tablePrefix' => 'ip_',
  100. 'charset' => 'utf8',
  101. ),
  102. 'comment' => "Database configuration",
  103. ),
  104. // END DB
  105. );
  106. // Override template values:
  107. foreach ($config as $key => $info) {
  108. $configInfo[$key]['value'] = $config[$key];
  109. }
  110. // Generate config code:
  111. $configCode = "";
  112. foreach ($configInfo as $key => $info) {
  113. if (is_null($info['value'])) {
  114. continue;
  115. }
  116. $exportedString = var_export($info['value'], true);
  117. if (is_array($info['value'])) {
  118. $exportedString = self::addSpacesOnNewLines($exportedString);
  119. }
  120. $configCode.= "\n '{$key}' => " . $exportedString . ",";
  121. if (!empty($info['comment'])) {
  122. $configCode.= " // " . $info['comment'];
  123. }
  124. }
  125. $configCode = "<"."?php
  126. /**
  127. * @package ImpressPages
  128. */
  129. return array(" . $configCode . "\n);";
  130. file_put_contents($filename, $configCode);
  131. }
  132. protected static function addSpacesOnNewLines($string)
  133. {
  134. return preg_replace('/([\r\n]+)/', '$1 ', $string);
  135. }
  136. public static function insertAdmin($user, $email, $pass)
  137. {
  138. $adminId = \Ip\Internal\Administrators\Service::add($user, $email, $pass);
  139. \Ip\Internal\AdminPermissionsModel::addPermission('Super admin', $adminId);
  140. }
  141. public static function generateCronPassword()
  142. {
  143. $password = \rand(100000, 999999);
  144. ipSetOption('Config.cronPassword', $password);
  145. return $password;
  146. }
  147. }