]> git.openstreetmap.org Git - nominatim.git/blob - lib/website.php
fix syntax errors and update tests
[nominatim.git] / lib / website.php
1 <?php
2
3 /***************************************************************************
4  *
5  * Error handling functions
6  *
7  */
8         function chksql($oSql, $sMsg = "Database request failed")
9         {
10                 if (!PEAR::isError($oSql)) return $oSql;
11
12                 header('HTTP/1.0 500 Internal Server Error');
13                 header('Content-type: text/html; charset=utf-8');
14
15                 $sSqlError = $oSql->getMessage();
16
17                 echo <<<INTERNALFAIL
18         <html>
19           <head><title>Internal Server Error</title></head>
20           <body>
21                 <h1>Internal Server Error</h1>
22                 <p>Nominatim has encountered an internal error while accessing the database.
23                    This may happen because the database is broken or because of a bug in
24                    the software. If you think it is a bug, feel free to report
25                    it over on <a href="https://github.com/twain47/Nominatim/issues">
26                    Github</a>. Please include the URL that caused the problem and the
27                    complete error details below.</p>
28                 <p><b>Message:</b> $sMsg</p>
29                 <p><b>SQL Error:</b> $sSqlError</p>
30                 <p><b>Details:</b> <pre>
31 INTERNALFAIL;
32
33                 if (CONST_Debug)
34                 {
35                         var_dump($oSql);
36                 }
37                 else
38                 {
39                         echo "<pre>\n".$oSql->getUserInfo()."</pre>";
40                 }
41
42                 echo "</pre></p></body></head></html>";
43                 exit;
44         }
45
46         function failInternalError($sError, $sSQL = false, $vDumpVar = false)
47         {
48                 header('HTTP/1.0 500 Internal Server Error');
49                 header('Content-type: text/html; charset=utf-8');
50                 echo "<html><body><h1>Internal Server Error</h1>";
51                 echo '<p>Nominatim has encountered an internal error while processing your request. This is most likely because of a bug in the software.</p>';
52                 echo "<p><b>Details:</b> ".$sError,"</p>";
53                 echo '<p>Feel free to file an issue on <a href="https://github.com/twain47/Nominatim/issues">Github</a>. Please include the error message above and the URL you used.</p>';
54                 if (CONST_Debug)
55                 {
56                         echo "<hr><h2>Debugging Information</h2><br>";
57                         if ($sSQL)
58                         {
59                                 echo "<h3>SQL query</h3><code>".$sSQL."</code>";
60                         }
61                         if ($vDumpVar)
62                         {
63                                 echo "<h3>Result</h3> <code>";
64                                 var_dump($vDumpVar);
65                                 echo "</code>";
66                         }
67                 }
68                 echo "\n</body></html>\n";
69                 exit;
70         }
71
72
73         function userError($sError)
74         {
75                 header('HTTP/1.0 400 Bad Request');
76                 header('Content-type: text/html; charset=utf-8');
77                 echo "<html><body><h1>Bad Request</h1>";
78                 echo '<p>Nominatim has encountered an error with your request.</p>';
79                 echo "<p><b>Details:</b> ".$sError,"</p>";
80                 echo '<p>If you feel this error is incorrect feel file an issue on <a href="https://github.com/twain47/Nominatim/issues">Github</a>. Please include the error message above and the URL you used.</p>';
81                 echo "\n</body></html>\n";
82                 exit;
83         }
84
85
86 /***************************************************************************
87  *
88  * Functions for parsing URL parameters
89  *
90  */
91
92         function getParamBool($sName, $bDefault=false)
93         {
94                 if (!isset($_GET[$sName])) return $bDefault;
95
96                 return (bool) $_GET[$sName];
97         }
98
99         function getParamInt($sName, $bDefault=false)
100         {
101                 if (!isset($_GET[$sName])) return $bDefault;
102
103                 if (!preg_match('/^[+-]?[0-9]+$/', $_GET[$sName]))
104                 {
105                         userError("Integer number expected for parameter '$sName'");
106                 }
107
108                 return (int) $_GET[$sName];
109         }
110
111         function getParamFloat($sName, $bDefault=false)
112         {
113                 if (!isset($_GET[$sName])) return $bDefault;
114
115                 if (!preg_match('/^[+-]?[0-9]*\.?[0-9]+$/', $_GET[$sName]))
116                 {
117                         userError("Floating-point number expected for parameter '$sName'");
118                 }
119
120                 return (float) $_GET[$sName];
121         }
122
123         function getParamString($sName, $bDefault=false)
124         {
125                 if (!isset($_GET[$sName])) return $bDefault;
126
127                 return $_GET[$sName];
128         }
129
130         function getParamSet($sName, $aValues, $sDefault=false)
131         {
132                 if (!isset($_GET[$sName])) return $sDefault;
133
134                 if (!in_array($_GET[$sName], $aValues))
135                 {
136                         userError("Parameter '$sName' must be one of: ".join(', ', $aValues));
137                 }
138
139                 return $_GET[$sName];
140         }