XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
pathcontroller.php
Go to the documentation of this file.
1 <?php
19 {
20  var $xoopsPath = array(
21  'root' => '',
22  'lib' => '',
23  'data' => '',
24  );
25 
26  var $xoopsPathDefault = array(
27  'lib' => 'xoops_lib',
28  'data' => 'xoops_data',
29  );
30 
31  var $dataPath = array(
32  'caches' => array(
33  'xoops_cache',
34  'smarty_cache',
35  'smarty_compile',
36  ),
37  'configs',
38  );
39 
40  var $path_lookup = array(
41  'root' => 'ROOT_PATH',
42  'data' => 'VAR_PATH',
43  'lib' => 'PATH',
44  );
45 
46  var $xoopsUrl = '';
47 
48  var $validPath = array(
49  'root' => 0,
50  'data' => 0,
51  'lib' => 0,
52  );
53 
54  var $validUrl = false;
55 
56  var $permErrors = array(
57  'root' => null,
58  'data' => null,
59  );
60 
62  {
63  $this->xoopsPathDefault = $xoopsPathDefault;
64  $this->dataPath = $dataPath;
65 
66  if ( isset( $_SESSION['settings']['ROOT_PATH'] ) ) {
67  foreach ($this->path_lookup as $req => $sess) {
68  $this->xoopsPath[$req] = $_SESSION['settings'][$sess];
69  }
70  } else {
71  $path = str_replace( "\\", "/", realpath( '../' ) );
72  if ( substr( $path, -1 ) == '/' ) {
73  $path = substr( $path, 0, -1 );
74  }
75  if ( file_exists( "$path/mainfile.php" ) ) {
76  $this->xoopsPath['root'] = $path;
77  }
78  // Firstly, locate XOOPS lib folder out of XOOPS root folder
79  $this->xoopsPath['lib'] = dirname($path) . "/" . ($this->xoopsPathDefault['lib']);
80  // If the folder is not created, re-locate XOOPS lib folder inside XOOPS root folder
81  if ( !is_dir($this->xoopsPath['lib'] . "/") ) {
82  $this->xoopsPath['lib'] = $path . "/" . ($this->xoopsPathDefault['lib']);
83  }
84  // Firstly, locate XOOPS data folder out of XOOPS root folder
85  $this->xoopsPath['data'] = dirname($path) . "/" . ($this->xoopsPathDefault['data']);
86  // If the folder is not created, re-locate XOOPS data folder inside XOOPS root folder
87  if ( !is_dir($this->xoopsPath['data'] . "/") ) {
88  $this->xoopsPath['data'] = $path . "/" . ($this->xoopsPathDefault['data']);
89  }
90  }
91  if ( isset( $_SESSION['settings']['URL'] ) ) {
92  $this->xoopsUrl = $_SESSION['settings']['URL'];
93  } else {
94  $path = $GLOBALS['wizard']->baseLocation();
95  $this->xoopsUrl = substr( $path, 0, strrpos( $path, '/' ) );
96  }
97  }
98 
99  function execute()
100  {
101  $this->readRequest();
102  $valid = $this->validate();
103  if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
104  foreach ($this->path_lookup as $req => $sess) {
105  $_SESSION['settings'][$sess] = $this->xoopsPath[$req];
106  }
107  $_SESSION['settings']['URL'] = $this->xoopsUrl;
108  if ( $valid ) {
109  $GLOBALS['wizard']->redirectToPage( '+1' );
110  } else {
111  $GLOBALS['wizard']->redirectToPage( '+0' );
112  }
113  }
114  }
115 
116  function readRequest()
117  {
118  if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
119  $request = $_POST;
120  foreach ($this->path_lookup as $req => $sess) {
121  if ( isset($request[$req]) ) {
122  $request[$req] = str_replace( "\\", "/", trim($request[$req]) );
123  if ( substr( $request[$req], -1 ) == '/' ) {
124  $request[$req] = substr( $request[$req], 0, -1 );
125  }
126  $this->xoopsPath[$req] = $request[$req];
127  }
128  }
129  if ( isset( $request['URL'] ) ) {
130  $request['URL'] = trim($request['URL']);
131  if ( substr( $request['URL'], -1 ) == '/' ) {
132  $request['URL'] = substr( $request['URL'], 0, -1 );
133  }
134  $this->xoopsUrl = $request['URL'];
135  }
136  }
137  }
138 
139  function validate()
140  {
141  foreach (array_keys($this->xoopsPath) as $path) {
142  if ($this->checkPath($path)) {
143  $this->checkPermissions($path);
144  }
145  }
146  $this->validUrl = !empty($this->xoopsUrl);
147  $validPaths = ( array_sum(array_values($this->validPath)) == count(array_keys($this->validPath)) ) ? 1 : 0;
148  $validPerms = true;
149  foreach ($this->permErrors as $key => $errs) {
150  if (empty($errs)) continue;
151  foreach ($errs as $path => $status) {
152  if (empty($status)) {
153  $validPerms = false;
154  break;
155  }
156  }
157  }
158  return ( $validPaths && $this->validUrl && $validPerms );
159  }
160 
161  function checkPath($PATH = '')
162  {
163  $ret = 1;
164  if ( $PATH == 'root' || empty($PATH) ) {
165  $path = 'root';
166  if ( is_dir( $this->xoopsPath[$path] ) && is_readable( $this->xoopsPath[$path] ) ) {
167  @include_once "{$this->xoopsPath[$path]}/include/version.php";
168  if ( file_exists( "{$this->xoopsPath[$path]}/mainfile.php" ) && defined( 'XOOPS_VERSION' ) ) {
169  $this->validPath[$path] = 1;
170  }
171  }
172  $ret *= $this->validPath[$path];
173  }
174  if ( $PATH == 'lib' || empty($PATH) ) {
175  $path = 'lib';
176  if ( is_dir( $this->xoopsPath[$path] ) && is_readable( $this->xoopsPath[$path] ) ) {
177  $this->validPath[$path] = 1;
178  }
179  $ret *= $this->validPath[$path];
180  }
181  if ( $PATH == 'data' || empty($PATH) ) {
182  $path = 'data';
183  if ( is_dir( $this->xoopsPath[$path] ) && is_readable( $this->xoopsPath[$path] ) ) {
184  $this->validPath[$path] = 1;
185  }
186  $ret *= $this->validPath[$path];
187  }
188  return $ret;
189  }
190 
191  function setPermission($parent, $path, &$error)
192  {
193  if (is_array($path)) {
194  foreach ( array_keys($path) as $item ) {
195  if (is_string($item)) {
196  $error[$parent . "/" . $item] = $this->makeWritable( $parent . "/" . $item );
197  if (empty($path[$item])) continue;
198  foreach ($path[$item] as $child) {
199  $this->setPermission( $parent . "/" . $item, $child, $error );
200  }
201  } else {
202  $error[$parent . "/" . $path[$item]] = $this->makeWritable( $parent . "/" . $path[$item] );
203  }
204  }
205  } else {
206  $error[$parent . "/" . $path] = $this->makeWritable( $parent . "/" . $path );
207  }
208  return;
209  }
210 
212  {
213  $paths = array(
214  'root' => array( 'mainfile.php', 'uploads', /*'templates_c', 'cache'*/ ),
215  'data' => $this->dataPath
216  );
217  $errors = array(
218  'root' => null,
219  'data' => null,
220  );
221 
222  if (!isset($this->xoopsPath[$path])) {
223  return false;
224  }
225  if (!isset($errors[$path])) {
226  return true;
227  }
228  $this->setPermission($this->xoopsPath[$path], $paths[$path], $errors[$path]);
229  if ( in_array( false, $errors[$path] ) ) {
230  $this->permErrors[$path] = $errors[$path];
231  }
232  return true;
233  }
234 
241  function makeWritable( $path, $create = true )
242  {
243  $mode = intval('0777', 8);
244  if ( !file_exists( $path ) ) {
245  if (!$create) {
246  return false;
247  } else {
248  mkdir($path, $mode);
249  }
250  }
251  if ( !is_writable($path) ) {
252  chmod( $path, $mode );
253  }
254  clearstatcache();
255  if ( is_writable( $path ) ) {
256  $info = stat( $path );
257  if ( $info['mode'] & 0002 ) {
258  return 'w';
259  } elseif ( $info['mode'] & 0020 ) {
260  return 'g';
261  }
262  return 'u';
263  }
264  return false;
265  }
266 }
267 ?>