b22747e996444f2a59b7a7b60e7deaef2cfd3ab4
[scuttle] / functions.inc.php
1 <?php
2 // UTF-8 functions
3 require_once(dirname(__FILE__) .'/includes/utf8.php');
4
5 // Translation
6 require_once(dirname(__FILE__) .'/includes/php-gettext/gettext.inc');
7 $domain = 'messages';
8 T_setlocale(LC_ALL, $locale);
9 T_bindtextdomain($domain, dirname(__FILE__) .'/locales');
10 T_bind_textdomain_codeset($domain, 'UTF-8');
11 T_textdomain($domain);
12
13 // Converts tags:
14 // - direction = out: convert spaces to underscores;
15 // - direction = in: convert underscores to spaces.
16 function convertTag($tag, $direction = 'out') {
17     if ($direction == 'out') {
18         $tag = str_replace(' ', '_', $tag);
19     } else {
20         $tag = str_replace('_', ' ', $tag);
21     }
22     return $tag;
23 }
24
25 function filter($data, $type = NULL) {
26     if (is_string($data)) {
27         $data = trim($data);
28         $data = stripslashes($data);
29         switch ($type) {
30             case 'url':
31                 $data = rawurlencode($data);
32                 break;
33             default:
34                 $data = htmlspecialchars($data);
35                 break;
36         }
37     } else if (is_array($data)) {
38         foreach(array_keys($data) as $key) {
39             $row =& $data[$key];
40             $row = filter($row, $type);
41         }
42     }
43     return $data;
44 }
45
46 function getPerPageCount() {
47     global $defaultPerPage;
48     return $defaultPerPage;
49 }
50
51 function getSortOrder($override = NULL) {
52     global $defaultOrderBy;
53
54     if (isset($_GET['sort'])) {
55         return $_GET['sort'];
56     } else if (isset($override)) {
57         return $override;
58     } else {
59         return $defaultOrderBy;
60     }
61 }
62
63 function multi_array_search($needle, $haystack) {
64     if (is_array($haystack)) {
65         foreach(array_keys($haystack) as $key) {
66             $value =& $haystack[$key];
67             $result = multi_array_search($needle, $value);
68             if (is_array($result)) {
69                 $return = $result;
70                 array_unshift($return, $key);
71                 return $return;
72             } elseif ($result == true) {
73                 $return[] = $key;
74                 return $return;
75             }
76         }
77         return false;
78     } else {
79         if ($needle === $haystack) {
80             return true;
81         } else {
82             return false;
83         }
84     }
85 }
86
87 function createURL($page = '', $ending = '') {
88     global $cleanurls, $root;
89     if (!$cleanurls && $page != '') {
90         $page .= '.php';
91     }
92     return $root . $page .'/'. $ending;
93 }
94
95 function message_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '', $err_file = '', $sql = '', $db = NULL) {
96     if(defined('HAS_DIED'))
97         die(T_('message_die() was called multiple times.'));
98     define('HAS_DIED', 1);
99         
100         $sql_store = $sql;
101         
102         // Get SQL error if we are debugging. Do this as soon as possible to prevent 
103         // subsequent queries from overwriting the status of sql_error()
104         if (DEBUG && ($msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR)) {
105                 $sql_error = is_null($db) ? '' : $db->sql_error();
106                 $debug_text = '';
107                 
108                 if ($sql_error['message'] != '')
109                         $debug_text .= '<br /><br />'. T_('SQL Error') .' : '. $sql_error['code'] .' '. $sql_error['message'];
110
111                 if ($sql_store != '')
112                         $debug_text .= '<br /><br />'. $sql_store;
113
114                 if ($err_line != '' && $err_file != '')
115                         $debug_text .= '</br /><br />'. T_('Line') .' : '. $err_line .'<br />'. T_('File') .' :'. $err_file;
116         }
117
118         switch($msg_code) {
119                 case GENERAL_MESSAGE:
120                         if ($msg_title == '')
121                                 $msg_title = T_('Information');
122                         break;
123
124                 case CRITICAL_MESSAGE:
125                         if ($msg_title == '')
126                                 $msg_title = T_('Critical Information');
127                         break;
128
129                 case GENERAL_ERROR:
130                         if ($msg_text == '')
131                                 $msg_text = T_('An error occured');
132
133                         if ($msg_title == '')
134                                 $msg_title = T_('General Error');
135                         break;
136
137                 case CRITICAL_ERROR:
138                         // Critical errors mean we cannot rely on _ANY_ DB information being
139                         // available so we're going to dump out a simple echo'd statement
140
141                         if ($msg_text == '')
142                                 $msg_text = T_('An critical error occured');
143
144                         if ($msg_title == '')
145                                 $msg_title = T_('Critical Error');
146                         break;
147         }
148
149         // Add on DEBUG info if we've enabled debug mode and this is an error. This
150         // prevents debug info being output for general messages should DEBUG be
151         // set TRUE by accident (preventing confusion for the end user!)
152         if (DEBUG && ($msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR)) {
153                 if ($debug_text != '')
154                         $msg_text = $msg_text . '<br /><br /><strong>'. T_('DEBUG MODE') .'</strong>'. $debug_text;
155         }
156
157         echo "<html>\n<body>\n". $msg_title ."\n<br /><br />\n". $msg_text ."</body>\n</html>";
158         exit;
159 }
160 ?>

Benjamin Mako Hill || Want to submit a patch?