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

/xoops/installer/2.3.2b/htdocs/install/class/pathcontroller.php

http://xoopsfrance.codeplex.com
PHP | 258 lines | 215 code | 18 blank | 25 comment | 58 complexity | 0e0986b13a82ee2a62b65fdec3446037 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * See the enclosed file license.txt for licensing information.
  4. * If you did not receive this file, get it at http://www.fsf.org/copyleft/gpl.html
  5. *
  6. * @copyright The XOOPS project http://www.xoops.org/
  7. * @license http://www.fsf.org/copyleft/gpl.html GNU General Public License (GPL)
  8. * @package installer
  9. * @since 2.3.0
  10. * @author Haruki Setoyama <haruki@planewave.org>
  11. * @author Kazumi Ono <webmaster@myweb.ne.jp>
  12. * @author Skalpa Keo <skalpa@xoops.org>
  13. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  14. * @author DuGris (aka L. JEN) <dugris@frxoops.org>
  15. * @version $Id: pathcontroller 1429 2008-04-02 12:27:19Z phppp $
  16. **/
  17. class PathStuffController {
  18. var $xoopsPath = array(
  19. 'root' => '',
  20. 'lib' => '',
  21. 'data' => '',
  22. );
  23. var $xoopsPathDefault = array(
  24. 'lib' => 'xoops_lib',
  25. 'data' => 'xoops_data',
  26. );
  27. var $dataPath = array(
  28. 'caches' => array(
  29. 'xoops_cache',
  30. 'smarty_cache',
  31. 'smarty_compile',
  32. ),
  33. 'configs',
  34. );
  35. var $path_lookup = array(
  36. 'root' => 'ROOT_PATH',
  37. 'data' => 'VAR_PATH',
  38. 'lib' => 'PATH',
  39. );
  40. var $xoopsUrl = '';
  41. var $validPath = array(
  42. 'root' => 0,
  43. 'data' => 0,
  44. 'lib' => 0,
  45. );
  46. var $validUrl = false;
  47. var $permErrors = array(
  48. 'root' => null,
  49. 'data' => null,
  50. );
  51. function PathStuffController( $xoopsPathDefault, $dataPath ) {
  52. $this->xoopsPathDefault = $xoopsPathDefault;
  53. $this->dataPath = $dataPath;
  54. if ( isset( $_SESSION['settings']['ROOT_PATH'] ) ) {
  55. foreach ($this->path_lookup as $req => $sess) {
  56. $this->xoopsPath[$req] = $_SESSION['settings'][$sess];
  57. }
  58. } else {
  59. $path = str_replace( "\\", "/", realpath( '../' ) );
  60. if ( substr( $path, -1 ) == '/' ) {
  61. $path = substr( $path, 0, -1 );
  62. }
  63. if ( file_exists( "$path/mainfile.php" ) ) {
  64. $this->xoopsPath['root'] = $path;
  65. }
  66. // Firstly, locate XOOPS lib folder out of XOOPS root folder
  67. $this->xoopsPath['lib'] = dirname($path) . "/" . ($this->xoopsPathDefault['lib']);
  68. // If the folder is not created, re-locate XOOPS lib folder inside XOOPS root folder
  69. if ( !is_dir($this->xoopsPath['lib'] . "/") ) {
  70. $this->xoopsPath['lib'] = $path . "/" . ($this->xoopsPathDefault['lib']);
  71. }
  72. // Firstly, locate XOOPS data folder out of XOOPS root folder
  73. $this->xoopsPath['data'] = dirname($path) . "/" . ($this->xoopsPathDefault['data']);
  74. // If the folder is not created, re-locate XOOPS data folder inside XOOPS root folder
  75. if ( !is_dir($this->xoopsPath['data'] . "/") ) {
  76. $this->xoopsPath['data'] = $path . "/" . ($this->xoopsPathDefault['data']);
  77. }
  78. }
  79. if ( isset( $_SESSION['settings']['URL'] ) ) {
  80. $this->xoopsUrl = $_SESSION['settings']['URL'];
  81. } else {
  82. $path = $GLOBALS['wizard']->baseLocation();
  83. $this->xoopsUrl = substr( $path, 0, strrpos( $path, '/' ) );
  84. }
  85. }
  86. function execute() {
  87. $this->readRequest();
  88. $valid = $this->validate();
  89. if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  90. foreach ($this->path_lookup as $req => $sess) {
  91. $_SESSION['settings'][$sess] = $this->xoopsPath[$req];
  92. }
  93. $_SESSION['settings']['URL'] = $this->xoopsUrl;
  94. if ( $valid ) {
  95. $GLOBALS['wizard']->redirectToPage( '+1' );
  96. } else {
  97. $GLOBALS['wizard']->redirectToPage( '+0' );
  98. }
  99. }
  100. }
  101. function readRequest() {
  102. if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  103. $request = $_POST;
  104. foreach ($this->path_lookup as $req => $sess) {
  105. if ( isset($request[$req]) ) {
  106. $request[$req] = str_replace( "\\", "/", trim($request[$req]) );
  107. if ( substr( $request[$req], -1 ) == '/' ) {
  108. $request[$req] = substr( $request[$req], 0, -1 );
  109. }
  110. $this->xoopsPath[$req] = $request[$req];
  111. }
  112. }
  113. if ( isset( $request['URL'] ) ) {
  114. $request['URL'] = trim($request['URL']);
  115. if ( substr( $request['URL'], -1 ) == '/' ) {
  116. $request['URL'] = substr( $request['URL'], 0, -1 );
  117. }
  118. $this->xoopsUrl = $request['URL'];
  119. }
  120. }
  121. }
  122. function validate() {
  123. foreach (array_keys($this->xoopsPath) as $path) {
  124. if ($this->checkPath($path)) {
  125. $this->checkPermissions($path);
  126. }
  127. }
  128. $this->validUrl = !empty($this->xoopsUrl);
  129. $validPaths = ( array_sum(array_values($this->validPath)) == count(array_keys($this->validPath)) ) ? 1 : 0;
  130. $validPerms = true;
  131. foreach ($this->permErrors as $key => $errs) {
  132. if (empty($errs)) continue;
  133. foreach ($errs as $path => $status) {
  134. if (empty($status)) {
  135. $validPerms = false;
  136. break;
  137. }
  138. }
  139. }
  140. return ( $validPaths && $this->validUrl && $validPerms );
  141. }
  142. function checkPath($PATH = '') {
  143. $ret = 1;
  144. if ( $PATH == 'root' || empty($PATH) ) {
  145. $path = 'root';
  146. if ( is_dir( $this->xoopsPath[$path] ) && is_readable( $this->xoopsPath[$path] ) ) {
  147. @include_once "{$this->xoopsPath[$path]}/include/version.php";
  148. if ( file_exists( "{$this->xoopsPath[$path]}/mainfile.php" ) && defined( 'XOOPS_VERSION' ) ) {
  149. $this->validPath[$path] = 1;
  150. }
  151. }
  152. $ret *= $this->validPath[$path];
  153. }
  154. if ( $PATH == 'lib' || empty($PATH) ) {
  155. $path = 'lib';
  156. if ( is_dir( $this->xoopsPath[$path] ) && is_readable( $this->xoopsPath[$path] ) ) {
  157. $this->validPath[$path] = 1;
  158. }
  159. $ret *= $this->validPath[$path];
  160. }
  161. if ( $PATH == 'data' || empty($PATH) ) {
  162. $path = 'data';
  163. if ( is_dir( $this->xoopsPath[$path] ) && is_readable( $this->xoopsPath[$path] ) ) {
  164. $this->validPath[$path] = 1;
  165. }
  166. $ret *= $this->validPath[$path];
  167. }
  168. return $ret;
  169. }
  170. function setPermission($parent, $path, &$error) {
  171. if (is_array($path)) {
  172. foreach ( array_keys($path) as $item ) {
  173. if (is_string($item)) {
  174. $error[$parent . "/" . $item] = $this->makeWritable( $parent . "/" . $item );
  175. if (empty($path[$item])) continue;
  176. foreach ($path[$item] as $child) {
  177. $this->setPermission( $parent . "/" . $item, $child, $error );
  178. }
  179. } else {
  180. $error[$parent . "/" . $path[$item]] = $this->makeWritable( $parent . "/" . $path[$item] );
  181. }
  182. }
  183. } else {
  184. $error[$parent . "/" . $path] = $this->makeWritable( $parent . "/" . $path );
  185. }
  186. return;
  187. }
  188. function checkPermissions($path) {
  189. $paths = array(
  190. 'root' => array( 'mainfile.php', 'uploads', /*'templates_c', 'cache'*/ ),
  191. 'data' => $this->dataPath
  192. );
  193. $errors = array(
  194. 'root' => null,
  195. 'data' => null,
  196. );
  197. if (!isset($this->xoopsPath[$path])) {
  198. return false;
  199. }
  200. if (!isset($errors[$path])) {
  201. return true;
  202. }
  203. $this->setPermission($this->xoopsPath[$path], $paths[$path], $errors[$path]);
  204. if ( in_array( false, $errors[$path] ) ) {
  205. $this->permErrors[$path] = $errors[$path];
  206. }
  207. return true;
  208. }
  209. /**
  210. * Write-enable the specified folder
  211. * @param string $path
  212. * @param bool $recurse
  213. * @return false on failure, method (u-ser,g-roup,w-orld) on success
  214. */
  215. function makeWritable( $path, $create = true ) {
  216. $mode = intval('0777', 8);
  217. if ( !file_exists( $path ) ) {
  218. if (!$create) {
  219. return false;
  220. } else {
  221. mkdir($path, $mode);
  222. }
  223. }
  224. if ( !is_writable($path) ) {
  225. chmod( $path, $mode );
  226. }
  227. clearstatcache();
  228. if ( is_writable( $path ) ) {
  229. $info = stat( $path );
  230. if ( $info['mode'] & 0002 ) {
  231. return 'w';
  232. } elseif ( $info['mode'] & 0020 ) {
  233. return 'g';
  234. }
  235. return 'u';
  236. }
  237. return false;
  238. }
  239. }
  240. ?>