PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gallery2/install/index.php

https://github.com/justinlyon/scc
PHP | 344 lines | 212 code | 41 blank | 91 comment | 49 complexity | 711b54753764418a95aa6708c99fbf14 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, MIT, LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /*
  3. * ATTENTION:
  4. *
  5. * If you're seeing this in your browser, and are trying to install Gallery,
  6. * you either do not have PHP installed, or if it is installed, it is not
  7. * properly enabled. Please visit the following page for assistance:
  8. *
  9. * http://gallery.sourceforge.net/
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * $Id: index.php 17580 2008-04-13 00:38:13Z tnalmdal $
  14. *
  15. * Gallery - a web based photo album viewer and editor
  16. * Copyright (C) 2000-2008 Bharat Mediratta
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or (at
  21. * your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful, but
  24. * WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  31. */
  32. /**
  33. * Gallery Installer
  34. * @package Install
  35. */
  36. /* Show all errors. */
  37. @ini_set('display_errors', 1);
  38. /*
  39. * Disable magic_quotes runtime -- it causes problems with legitimate quotes
  40. * in our SQL, as well as reading/writing the config.php
  41. */
  42. @ini_set('magic_quotes_runtime', 0);
  43. $g2Base = dirname(dirname(__FILE__)) . '/';
  44. require_once($g2Base . 'install/GalleryStub.class');
  45. require_once($g2Base . 'install/InstallStep.class');
  46. require_once($g2Base . 'install/StatusTemplate.class');
  47. require_once($g2Base . 'modules/core/classes/GalleryUtilities.class');
  48. require_once($g2Base . 'modules/core/classes/GalleryDataCache.class');
  49. require_once($g2Base . 'lib/support/GallerySetupUtilities.class');
  50. define('INDEX_PHP', basename(__FILE__));
  51. /*
  52. * If gettext isn't enabled, subvert the _() text translation function
  53. * and just pass the string on through in English
  54. */
  55. if (!function_exists('_')) {
  56. function _($s) {
  57. return $s;
  58. }
  59. }
  60. /* Our install steps, in order */
  61. $stepOrder = array();
  62. $stepOrder[] = 'Welcome';
  63. $stepOrder[] = 'Authenticate';
  64. $stepOrder[] = 'SystemChecks';
  65. $stepOrder[] = 'Multisite';
  66. $stepOrder[] = 'StorageSetup';
  67. $stepOrder[] = 'DatabaseSetup';
  68. $stepOrder[] = 'AdminUserSetup';
  69. $stepOrder[] = 'CreateConfigFile';
  70. $stepOrder[] = 'InstallCoreModule';
  71. $stepOrder[] = 'InstallOtherModules';
  72. $stepOrder[] = 'Secure';
  73. $stepOrder[] = 'Finished';
  74. foreach ($stepOrder as $stepName) {
  75. $className = $stepName . 'Step';
  76. require("steps/$className.class");
  77. }
  78. GallerySetupUtilities::startSession();
  79. require_once($g2Base . 'modules/core/classes/GalleryStatus.class');
  80. require_once($g2Base . 'modules/core/classes/GalleryTranslator.class');
  81. if (empty($_SESSION['language'])) {
  82. /* Select language based on preferences sent from browser */
  83. $_SESSION['language'] = GalleryTranslator::getLanguageCodeFromRequest();
  84. }
  85. if (function_exists('dgettext')) {
  86. $gallery = new GalleryStub();
  87. $translator = new GalleryTranslator();
  88. $translator->init($_SESSION['language'], true);
  89. unset($gallery);
  90. bindtextdomain('gallery2_install', dirname(dirname(__FILE__)) . '/locale');
  91. textdomain('gallery2_install');
  92. if (function_exists('bind_textdomain_codeset')) {
  93. bind_textdomain_codeset('gallery2_install', 'UTF-8');
  94. }
  95. /* Set the appropriate charset in our HTTP header */
  96. if (!headers_sent()) {
  97. header('Content-Type: text/html; charset=UTF-8');
  98. }
  99. }
  100. /*
  101. * If register_globals is on then a global $galleryStub may have already been created.
  102. * Clear it here and initialize ourselves.
  103. */
  104. unset($galleryStub);
  105. if (!isset($_GET['startOver']) && !empty($_SESSION['install_steps'])) {
  106. $steps = unserialize($_SESSION['install_steps']);
  107. if (isset($_SESSION['galleryStub'])) {
  108. $galleryStub = unserialize($_SESSION['galleryStub']);
  109. }
  110. }
  111. /* If we don't have our steps in our session, initialize them now. */
  112. if (empty($steps) || !is_array($steps)) {
  113. $steps = array();
  114. for ($i = 0; $i < count($stepOrder); $i++) {
  115. $className = $stepOrder[$i] . 'Step';
  116. $step = new $className();
  117. if ($step->isRelevant()) {
  118. $step->setIsLastStep(false);
  119. $step->setStepNumber($i);
  120. $step->setInError(false);
  121. $step->setComplete(false);
  122. $steps[] = $step;
  123. }
  124. }
  125. /* Don't do this in the loop, since not all steps are relevant */
  126. $steps[count($steps)-1]->setIsLastStep(true);
  127. }
  128. $stepNumber = isset($_GET['step']) ? (int)$_GET['step'] : 0;
  129. /* Make sure all steps up to the current one are ok */
  130. for ($i = 0; $i < $stepNumber; $i++) {
  131. if (!$steps[$i]->isComplete() && !$steps[$i]->isOptional()) {
  132. $stepNumber = $i;
  133. break;
  134. }
  135. }
  136. $currentStep =& $steps[$stepNumber];
  137. if (!empty($_GET['doOver'])) {
  138. $currentStep->setComplete(false);
  139. }
  140. /* If the current step is incomplete, the rest of the steps can't be complete either */
  141. if (!$currentStep->isComplete()) {
  142. for ($i = $stepNumber+1; $i < count($steps); $i++) {
  143. $steps[$i]->setComplete(false);
  144. $steps[$i]->setInError(false);
  145. }
  146. }
  147. if ($currentStep->processRequest()) {
  148. /* Load up template data from the current step */
  149. $templateData = array();
  150. /* Round percentage to the nearest 5 */
  151. $templateData['errors'] = array();
  152. $currentStep->loadTemplateData($templateData);
  153. /* Render the output */
  154. $template = new StatusTemplate();
  155. $template->renderHeaderBodyAndFooter($templateData);
  156. }
  157. function processAutoCompleteRequest() {
  158. $path = !empty($_GET['path']) ? $_GET['path'] : '';
  159. /* Undo the damage caused by magic_quotes */
  160. if (get_magic_quotes_gpc()) {
  161. $path = stripslashes($path);
  162. }
  163. if (is_dir($path)) {
  164. $match = '';
  165. } else {
  166. $match = basename($path);
  167. $matchLength = strlen($match);
  168. $path = dirname($path);
  169. if (!is_dir($path)) {
  170. return;
  171. }
  172. }
  173. $dirList = array();
  174. if ($dir = opendir($path)) {
  175. if ($path{strlen($path)-1} != DIRECTORY_SEPARATOR) {
  176. $path .= DIRECTORY_SEPARATOR;
  177. }
  178. while (($file = readdir($dir)) !== false) {
  179. if ($file == '.' || $file == '..' || ($match && strncmp($file, $match, $matchLength))) {
  180. continue;
  181. }
  182. $file = $path . $file;
  183. if (is_dir($file)) {
  184. $dirList[] = $file;
  185. }
  186. }
  187. closedir($dir);
  188. sort($dirList);
  189. }
  190. header("Content-Type: text/plain");
  191. print implode("\n", $dirList);
  192. }
  193. /**
  194. * (Re-) Create the gallery filesystem data structure
  195. *
  196. * @param string $dataBase absolute filesystem path of the storage directory
  197. * @return boolean success whether the structure was created successfully
  198. */
  199. function populateDataDirectory($dataBase) {
  200. /* Use non-restrictive umask to create directories with lax permissions */
  201. umask(0);
  202. if ($dataBase{strlen($dataBase)-1} != DIRECTORY_SEPARATOR) {
  203. $dataBase .= DIRECTORY_SEPARATOR;
  204. }
  205. /* Create the sub directories, if necessary */
  206. foreach (array('albums',
  207. 'cache',
  208. 'locks',
  209. 'tmp',
  210. 'plugins_data',
  211. 'plugins_data/modules',
  212. 'plugins_data/themes',
  213. 'smarty',
  214. 'smarty/templates_c') as $key) {
  215. $dir = $dataBase . $key;
  216. if (file_exists($dir) && !is_dir($dir)) {
  217. return false;
  218. }
  219. if (!file_exists($dir)) {
  220. if (!@mkdir($dir, 0755)) {
  221. return false;
  222. }
  223. }
  224. if (!is_writeable($dir)) {
  225. return false;
  226. }
  227. }
  228. return secureStorageFolder($dataBase);
  229. }
  230. /**
  231. * Secure the storage folder from attempts to access it directly via the web by adding a
  232. * .htaccess with a "Deny from all" directive. This won't have any effect on webservers other
  233. * than Apache 1.2+ though.
  234. * Since we can't reliably tell whether the storage folder is web-accessible or not,
  235. * we add this in all cases. It doesn't hurt.
  236. * @param string $dataBase absolute filesystem path to the storage folder
  237. * @return boolean true if the .htaccess file has been created successfully
  238. */
  239. function secureStorageFolder($dataBase) {
  240. $htaccessPath = $dataBase . '.htaccess';
  241. $fh = @fopen($htaccessPath, 'w');
  242. if ($fh) {
  243. $htaccessContents = "DirectoryIndex .htaccess\n" .
  244. "SetHandler Gallery_Security_Do_Not_Remove\n" .
  245. "Options None\n" .
  246. "<IfModule mod_rewrite.c>\n" .
  247. "RewriteEngine off\n" .
  248. "</IfModule>\n" .
  249. "Order allow,deny\n" .
  250. "Deny from all\n";
  251. fwrite($fh, $htaccessContents);
  252. fclose($fh);
  253. }
  254. return file_exists($htaccessPath);
  255. }
  256. /* Returns something like https://example.com */
  257. function getBaseUrl() {
  258. /* Can't use GalleryUrlGenerator::makeUrl since it's an object method */
  259. if (!($hostName = GalleryUtilities::getServerVar('HTTP_X_FORWARDED_HOST'))) {
  260. $hostName = GalleryUtilities::getServerVar('HTTP_HOST');
  261. }
  262. $protocol = (GalleryUtilities::getServerVar('HTTPS') == 'on') ? 'https' : 'http';
  263. return sprintf('%s://%s', $protocol, $hostName);
  264. }
  265. /** Returns the URL to the G2 folder, e.g. http://example.com/gallery2/. */
  266. function getGalleryDirUrl() {
  267. global $g2Base;
  268. require_once($g2Base . 'modules/core/classes/GalleryUrlGenerator.class');
  269. $urlPath = preg_replace('|^(.*/)install/index.php(?:\?.*)?$|s', '$1',
  270. GalleryUrlGenerator::getCurrentRequestUri());
  271. return getBaseUrl() . $urlPath;
  272. }
  273. /**
  274. * Mini url generator for the installer
  275. */
  276. function generateUrl($uri, $print=true) {
  277. if (!strncmp($uri, 'index.php', 9)) {
  278. /* Cookieless browsing: If session.use_trans_sid is on then it will add the session id. */
  279. if (!GallerySetupUtilities::areCookiesSupported() && !ini_get('session.use_trans_sid')) {
  280. /*
  281. * Don't use SID since it's a constant and we change (regenerate) the session id
  282. * in the request
  283. */
  284. $sid = session_name() . '=' . session_id();
  285. $uri .= !strpos($uri, '?') ? '?' : '&amp;';
  286. $uri .= $sid;
  287. }
  288. }
  289. if ($print) {
  290. print $uri;
  291. }
  292. return $uri;
  293. }
  294. /*
  295. * We don't store the steps in the session in raw form because that
  296. * will break in environments where session.auto_start is on since
  297. * it will try to instantiate the classes before they've been defined
  298. */
  299. $_SESSION['install_steps'] = serialize($steps);
  300. if (isset($galleryStub)) {
  301. $_SESSION['galleryStub'] = serialize($galleryStub);
  302. }
  303. ?>