]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/prepend.php
log
[SourceForge/phpwiki.git] / lib / prepend.php
1 <?php
2 /* lib/prepend.php
3  *
4  * Things which must be done and defined before anything else.
5  */
6 $RCS_IDS = '';
7 function rcs_id ($id) { $GLOBALS['RCS_IDS'] .= "$id\n"; }
8 rcs_id('$Id: prepend.php,v 1.2 2001-02-14 05:22:49 dairiki Exp $');
9
10 error_reporting(E_ALL);
11
12 define ('FATAL_ERRORS',
13         E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
14 define ('WARNING_ERRORS',
15         E_WARNING | E_CORE_WARNING | E_COMPILE_WARNING | E_USER_WARNING);
16 define ('NOTICE_ERRORS', E_NOTICE | E_USER_NOTICE);
17
18 $PostponedErrorMask = 0;
19 $PostponedErrors = array();
20
21 function PostponeErrorMessages ($newmask = -1)
22 {
23    global $PostponedErrorMask, $PostponedErrors;
24
25    if ($newmask < 0)
26       return $PostponedErrorMask;
27    
28    $oldmask = $PostponedErrorMask;
29    $PostponedErrorMask = $newmask;
30
31    $i = 0;
32    while ($i < sizeof($PostponedErrors))
33    {
34       list ($errno, $message) = $PostponedErrors[$i];
35       if (($errno & $newmask) == 0)
36       {
37          echo $message;
38          array_splice($PostponedErrors, $i, 1);
39       }
40       else
41          $i++;
42    }
43    
44    return $oldmask;
45 }
46
47 function ExitWiki($errormsg = false)
48 {
49    static $exitwiki = 0;
50    global $dbi;
51
52    if($exitwiki)                // just in case CloseDataBase calls us
53       exit();
54    $exitwiki = 1;
55
56    PostponeErrorMessages(0);    // Spew postponed messages.
57    
58    if(!empty($errormsg)) {
59       print "<P><hr noshade><h2>" . gettext("WikiFatalError") . "</h2>\n";
60       print $errormsg;
61       print "\n</BODY></HTML>";
62    }
63
64    if (isset($dbi))
65       CloseDataBase($dbi);
66    exit;
67 }
68
69 function PostponeErrorHandler ($errno, $errstr, $errfile, $errline)
70 {
71    global $PostponedErrorMask, $PostponedErrors;
72    static $inHandler = 0;
73
74    if ($inHandler++ != 0)
75       return;                   // prevent recursion.
76
77    if (($errno & NOTICE_ERRORS) != 0)
78       $what = 'Notice';
79    else if (($errno & WARNING_ERRORS) != 0)
80       $what = 'Warning';
81    else
82       $what = 'Fatal';
83
84    $errfile = ereg_replace('^' . getcwd() . '/', '', $errfile);
85    $message = sprintf("<br>%s:%d: <b>%s</b>[%d]: %s<br>\n",
86                       htmlspecialchars($errfile),
87                       $errline, $what, $errno,
88                       htmlspecialchars($errstr));
89
90
91    if ($what == 'Fatal')
92    {
93       PostponeErrorMessages(0); // Spew postponed messages.
94       echo $message;
95       ExitWiki();
96       exit -1;
97    }
98    else if (($errno & error_reporting()) != 0)
99    {
100       if (($errno & $PostponedErrorMask) != 0)
101       {
102          $PostponedErrors[] = array($errno, $message);
103       }
104       
105       else
106          echo $message;
107    }
108
109    $inHandler = 0;
110 }
111
112 set_error_handler('PostponeErrorHandler');
113
114 PostponeErrorMessages(E_ALL);
115
116
117 function SearchPath ($file, $missing_ok = false, $path = false) 
118 {
119    if (ereg('^/', $file))
120       return $file;             // absolute path.
121
122    if (!$path)
123       $path = $GLOBALS['DataPath'];
124    
125    while (list($i, $dir) = each($path))
126    {
127       if (file_exists("$dir/$file"))
128          return "$dir/$file";
129    }
130    if ($missing_ok)
131       return false;
132    ExitWiki("$file: file not found");
133 }
134
135 // For emacs users
136 // Local Variables:
137 // mode: php
138 // c-file-style: "ellemtel"
139 // End:   
140 ?>