/default_www/install/engine/step_1.php

https://github.com/zakgrant/forkcms · PHP · 150 lines · 76 code · 25 blank · 49 comment · 12 complexity · 20a6975cadb30d38b7983209f1bf1b4e MD5 · raw file

  1. <?php
  2. /**
  3. * Step 1 of the Fork installer
  4. *
  5. * @package install
  6. * @subpackage installer
  7. *
  8. * @author Davy Hellemans <davy@netlash.com>
  9. * @author Tijs Verkoyen <tijs@netlash.com>
  10. * @author Matthias Mullie <matthias@netlash.com>
  11. * @since 2.0
  12. */
  13. class InstallerStep1 extends InstallerStep
  14. {
  15. /**
  16. * Execute this step
  17. *
  18. * @return void
  19. */
  20. public function execute()
  21. {
  22. // init vars
  23. $possiblePaths = array();
  24. $variables = array();
  25. // head
  26. $variables['head'] = file_get_contents('layout/templates/head.tpl');
  27. $variables['foot'] = file_get_contents('layout/templates/foot.tpl');
  28. // get the possible library paths
  29. self::guessLibraryPath(dirname(dirname(dirname(realpath($_SERVER['SCRIPT_FILENAME'])))), $possiblePaths);
  30. // was the form submitted?
  31. if(isset($_GET['spoon_location']) && in_array($_GET['spoon_location'], $possiblePaths))
  32. {
  33. // store in session
  34. $_SESSION['path_library'] = $_GET['spoon_location'];
  35. // redirect to step 2
  36. header('Location: index.php?step=2');
  37. exit;
  38. }
  39. // count
  40. $count = count($possiblePaths);
  41. // just one found? add it into the session
  42. if($count == 1)
  43. {
  44. $_SESSION['path_library'] = $possiblePaths[0];
  45. // redirect to step 2
  46. header('Location: index.php?step=2');
  47. exit;
  48. }
  49. // nothing found
  50. elseif($count > 0)
  51. {
  52. $variables['content'] = '<h3>Location of Spoon</h3>
  53. <div class="horizontal">
  54. <p>We detected multiple folders containing Spoon. Below you can select the correct folder.</p>
  55. <p>
  56. <label for="spoonLocation">Paths<abbr title="Required field">*</abbr></label>
  57. <select id="spoonLocation" name="spoon_location" class="input-select" style="width: 350px;">';
  58. // loop locations
  59. foreach($possiblePaths as $path)
  60. {
  61. $variables['content'] .= '<option value="' . $path . '">' . $path . '</option>';
  62. }
  63. $variables['content'] .= '</select>
  64. </p>
  65. <p class="buttonHolder">
  66. <input id="installerButton" class="button inputButton mainButton" type="submit" name="installer" value="Next" />
  67. </p>';
  68. }
  69. // nothing found
  70. else
  71. {
  72. $variables['content'] = '<div class="formMessage errorMessage">
  73. <p>We couldn\'t locate Spoon Library. Make sure you uploaded the <code>library</code>-folder.</p>
  74. </div>';
  75. }
  76. // template contents
  77. $tpl = file_get_contents('layout/templates/1.tpl');
  78. // build the search & replace array
  79. $search = array_keys($variables);
  80. $replace = array_values($variables);
  81. // loop search values
  82. foreach($search as $key => $value) $search[$key] = '{$' . $value . '}';
  83. // build output
  84. $output = str_replace($search, $replace, $tpl);
  85. // show output
  86. echo $output;
  87. // stop the script
  88. exit;
  89. }
  90. /**
  91. * Try to guess the location of the library based on spoon library
  92. *
  93. * @return void
  94. * @param string $directory The directory to start from.
  95. * @param array[optional] $library An array to hold the paths that were guesed.
  96. */
  97. private static function guessLibraryPath($directory, array &$library = null)
  98. {
  99. // loop directories
  100. foreach((array) glob($directory . '/*') as $filename)
  101. {
  102. // not a directory and equals 'spoon.php'
  103. if(!is_dir($filename) && substr($filename, -9) == 'spoon.php')
  104. {
  105. // get real path
  106. $path = realpath(str_replace('spoon.php', '..', $filename));
  107. // only unique values should be added
  108. if(is_array($library))
  109. {
  110. // add
  111. if(!in_array($path, $library)) $library[] = $path;
  112. }
  113. // not an array
  114. else $library = array($path);
  115. }
  116. // directory
  117. elseif(is_dir($filename) && substr($filename, -4) != '.svn')
  118. {
  119. // new location
  120. self::guessLibraryPath($filename, $library);
  121. }
  122. }
  123. }
  124. }
  125. ?>