(no commit message)
[freeside.git] / fs_selfservice / fri / includes / bootstrap.php
1 <?php
2
3 /**
4  * @file
5  * Functions that need to be loaded on every request.
6  */
7
8 /**
9  * Sets doc root
10  */
11 function setARIRoot() {
12
13   $found = 0;
14   if (isset($_SERVER['PHP_SELF'])) {
15     if ($_SERVER['PHP_SELF']!='') {
16       $_SESSION['ARI_ROOT'] = $_SERVER['PHP_SELF'];
17     }
18   }
19   
20   if (!$found) {
21     $_SESSION['ARI_ROOT'] = "index.php";
22   }
23 }
24
25 /**
26  * Return a arguments.
27  *
28  * @param $args
29  *   The name of the array being acted upon.
30  * @param $name
31  *   The name of the variable to return.
32  * @return
33  *   The value of the variable.
34  */
35 function getArgument($args, $name) {
36
37   return isset($args[$name]) ? $args[$name] : '';
38 }
39
40 /*
41  * Gets top level directory names 
42  *
43  * @param $path
44  *   directory to search
45  * @param $filter
46  *   string to use as a filter to match files to return
47  * @return $directories
48  *   directories found
49  */
50 function getDirectories($path,$filter) {
51
52   $directories = array();
53
54   if (is_dir($path)) {
55
56     $dh = opendir($path);
57     while (false!== ($item = readdir($dh))) {
58       if($item!="." && $item!="..") {
59
60         $path = fixPathSlash($path);
61         $directory = $path;
62         $directory = appendPath($directory,$item);
63
64         if (is_dir($directory)) {
65
66           $found = 0;
67           if ($filter) {
68             if (strpos($directory,$filter)) {
69               $found = 1;
70             }
71           } else {
72             $found = 1;
73           }
74           if ($found) {
75             $directories[count($directories) + 1] = $directory;
76           }
77         }
78       }
79     } 
80   }
81
82   return $directories;
83 }
84
85 /*
86  * Gets file names recursively 6 folders deep
87  *
88  * @param $path
89  *   directory to search
90  * @param $filter
91  *   string to use as a filter to match files to return
92  * @param $recursive_max
93  *   max number of sub folders to search
94  * @param $recursive_count
95  *   current sub folder count
96  * @return $files
97  *   files found
98  */
99 function getFiles($path,$filter,$recursive_max,$recursive_count) {
100
101   $files = array();
102
103   if (@is_dir($path) && @is_readable($path)) {
104     $dh = opendir($path);
105     while (false!== ($item = readdir($dh))) {
106       if($item[0]!=".") {
107
108         $path = fixPathSlash($path);
109         $msg_path = appendPath($path,$item);
110
111         $fileCount++;
112         if ($fileCount>3000) {
113           $_SESSION['ari_error'] 
114             .= _("To many files in $msg_path Not all files processed") . "<br>";
115           return;
116         }
117
118         if ($recursive_count<$recursive_max && is_dir($msg_path)) {
119
120           $dirCount++;
121           if ($dirCount>10) {
122             $_SESSION['ari_error'] 
123               .= sprintf(_("To many directories in %s Not all files processed"),$msg_path) . "<br>";
124             return;
125           }
126
127           $count = $recursive_count + 1;
128           $path_files = getFiles($msg_path,$filter,$recursive_max,$count);
129           $files = array_merge($files,$path_files);
130         } 
131         else {
132           $found = 0;
133           if ($filter) {
134             if (strpos($msg_path,$filter)) {
135               $found = 1;
136             }
137           } else {
138             $found = 1;
139           }
140           if ($found) {
141             $files[count($files) + 1] = $msg_path;
142           }
143         }
144       }
145     } 
146   }
147
148   return $files;
149 }
150
151 /* Utilities */
152
153 /**
154  * Fixes the path for a trailing slash
155  *
156  * @param $path
157  *   path to append
158  * @return $ret
159  *   path to returned
160  */
161 function fixPathSlash($path) {
162
163   $ret = $path;
164
165   $slash = '';
166   if (!preg_match('/\/$/',$path)) {
167     $slash = '/';
168   } 
169   $ret .= $slash;
170
171   return $ret; 
172 }
173
174 /**
175  * Appends folder to end of path
176  *
177  * @param $path
178  *   path to append
179  * @param $folder
180  *   folder to append to path
181  * @return $ret
182  *   path to returned
183  */
184 function appendPath($path,$folder) {
185
186   $ret = $path;
187
188   $m = '';
189   if (!preg_match('/\/$/',$path)) {
190     $m = '/';
191   } 
192   $ret .= $m . $folder; 
193
194   return $ret;
195 }
196
197 /**
198  * Get Date format 
199  *
200  * @param $timestamp
201  *   timestamp to be converted
202  */
203 function getDateFormat($timestamp) {
204   return date('Y-m-d', $timestamp);
205 }
206
207 /**
208  * Get time format 
209  *
210  * @param $timestamp
211  *   timestamp to be converted
212  */
213 function getTimeFormat($timestamp) {   
214   return date('G:i:s', $timestamp);
215 }
216
217 /* */
218
219 /**
220  * Checks ARI dependencies
221  */
222 function checkDependencies() {
223
224   // check for PHP
225   if (!version_compare(phpversion(), '4.3', '>=')) {
226     echo _("ARI requires a version of PHP 4.3 or later");
227     exit();
228   }
229
230   // check for PEAR
231   $include_path = ini_get('include_path');
232   $buf = split(':|,',$include_path);
233
234   $found = 0;
235   foreach ($buf as $path) {
236     $path = fixPathSlash($path);
237     $pear_check_path = $path . "DB.php";
238     if (is_file($pear_check_path)) {
239       $found = 1;
240       break;
241     }
242   }
243
244   if (!$found) {
245     echo _("PHP PEAR must be installed.  Visit http://pear.php.net for help with installation.");
246     exit();
247   }
248 }
249
250 /**
251  * Starts the session
252  */
253 function startARISession() {
254
255   if (!isset($_SESSION['ari_user']) ) {
256
257     // start a new session for the user 
258     ini_set('session.name', 'ARI');             // prevent session name clashes
259     ini_set('session.gc_maxlifetime', '3900');  // make the session timeout a long time
260     set_time_limit(360);
261     session_start();
262   }
263 }
264
265 /**
266  * Bootstrap
267  *
268  * Loads critical variables needed for every page request
269  *
270  */
271 function bootstrap() {
272
273   // set error reporting
274   error_reporting (E_ALL & ~ E_NOTICE);  
275 }
276
277 /**
278  * Set HTTP headers in preparation for a page response.
279  *
280  * TODO: Figure out caching
281  */
282 function ariPageHeader() {
283
284   bootstrap();
285 }
286
287 /**
288  * Perform end-of-request tasks.
289  *
290  * This function sets the page cache if appropriate, and allows modules to
291  * react to the closing of the page by calling hook_exit().
292  */
293 function ariPageFooter() {
294
295 }
296
297 /**
298  * Includes and run functions
299  */
300
301 include_once("./includes/lang.php");
302 $language = new Language();
303 $language->set();
304
305 checkDependencies();
306 startARISession();
307 setARIRoot();
308
309 include_once("./includes/main.conf.php");
310 include_once("./version.php");
311 include_once("./includes/crypt.php"); 
312 include_once("./includes/login.php");
313
314
315 ?>