source: trunk/bookmarks/functions.inc.php @ 814

Revision 814, 5.9 KB checked in by Xiping.Wang, 3 years ago (diff)

[trunk]#106, omBookmarks deletion error. I suggest to set DEBUG as false to remove trace message.

Line 
1<?php
2// UTF-8 functions
3require_once(dirname(__FILE__) .'/includes/utf8.php');
4
5// Translation
6require_once(dirname(__FILE__) .'/includes/php-gettext/gettext.inc');
7$domain = 'messages';
8T_setlocale(LC_MESSAGES, $locale);
9T_bindtextdomain($domain, dirname(__FILE__) .'/locales');
10T_bind_textdomain_codeset($domain, 'UTF-8');
11T_textdomain($domain);
12
13// Converts tags:
14// - direction = out: convert spaces to underscores;
15// - direction = in: convert underscores to spaces.
16function 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
25function filter($data, $type = NULL) {
26    if (is_string($data)) {
27        $data = trim($data);
28        $data = stripslashes($data);
29        switch (strtolower($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/**
47 * Returns a string of text noramlised for SEO friendly URLs
48 *
49 * @return string
50 */
51function NormaliseForSeo( $sText ){
52        return  trim( preg_replace( '/(_+)/'
53                                  , '_'
54                                  , preg_replace( '/(\s+|_+|\-+|[^A-Za-z0-9])/'
55                                                , '_'
56                                                , $sText ) )
57                    , '_' );
58}
59
60function getPerPageCount() {
61    global $defaultPerPage;
62    return $defaultPerPage;
63}
64
65function getSortStyles(){
66    $sPopularitySortStyle  = '';
67    $sDateSortStyle        = '';
68    $sTitleSortStyle       = '';
69        $sUrlSortStyle         = '';
70        $sHighlightedSortStyle = ' style="background-color:white;padding:3px;"';
71        $aRequestedSort = explode( '_', getSortOrder() );
72        if( count( $aRequestedSort ) == 2 ){
73                switch( strtolower( $aRequestedSort[0] ) ){
74                        case 'date':
75                                $sDateSortStyle = $sHighlightedSortStyle;
76                                break;
77                        case 'title':
78                                $sTitleSortStyle = $sHighlightedSortStyle;
79                                break;
80                        case 'url':
81                                $sUrlSortStyle = $sHighlightedSortStyle;
82                                break;
83                        default:
84                                $sPopularitySortStyle = $sHighlightedSortStyle;
85                }
86        }else{
87                $sPopularitySortStyle = $sHighlightedSortStyle;
88        }
89        return array( 'popularity' => $sPopularitySortStyle
90                    , 'date'       => $sDateSortStyle
91                    , 'title'      => $sTitleSortStyle
92                    , 'url'        => $sUrlSortStyle );
93}
94
95function getSortOrder($override = NULL) {
96    global $defaultOrderBy;
97
98    if (isset($_GET['sort'])) {
99        return $_GET['sort'];
100    } else if (isset($override)) {
101        return $override;
102    } else {
103        return $defaultOrderBy;
104    }
105}
106
107function multi_array_search($needle, $haystack) {
108    if (is_array($haystack)) {
109        foreach(array_keys($haystack) as $key) {
110            $value =& $haystack[$key];
111            $result = multi_array_search($needle, $value);
112            if (is_array($result)) {
113                $return = $result;
114                array_unshift($return, $key);
115                return $return;
116            } elseif ($result == true) {
117                $return[] = $key;
118                return $return;
119            }
120        }
121        return false;
122    } else {
123        if ($needle === $haystack) {
124            return true;
125        } else {
126            return false;
127        }
128    }
129}
130
131function createURL($page = '', $ending = '') {
132    global $cleanurls, $root;
133    if (!$cleanurls && $page != '') {
134        $page .= '.php';
135    }
136    return $root . $page .'/'. $ending;
137}
138
139function message_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '', $err_file = '', $sql = '', $db = NULL) {
140    if(defined('HAS_DIED'))
141        die(T_('message_die() was called multiple times.'));
142    define('HAS_DIED', 1);
143       
144        $sql_store = $sql;
145       
146        // Get SQL error if we are debugging. Do this as soon as possible to prevent
147        // subsequent queries from overwriting the status of sql_error()
148        if (DEBUG && ($msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR)) {
149                $sql_error = empty($db) ? '' : $db->sql_error();
150                $debug_text = '';
151               
152                if ($sql_error['message'] != '')
153                        $debug_text .= '<br /><br />'. T_('SQL Error') .' : '. $sql_error['code'] .' '. $sql_error['message'];
154
155                if ($sql_store != '')
156                        $debug_text .= '<br /><br />'. $sql_store;
157
158                if ($err_line != '' && $err_file != '')
159                        $debug_text .= '</br /><br />'. T_('Line') .' : '. $err_line .'<br />'. T_('File') .' :'. $err_file;
160        }
161
162        switch($msg_code) {
163                case GENERAL_MESSAGE:
164                        if ($msg_title == '')
165                                $msg_title = T_('Information');
166                        break;
167
168                case CRITICAL_MESSAGE:
169                        if ($msg_title == '')
170                                $msg_title = T_('Critical Information');
171                        break;
172
173                case GENERAL_ERROR:
174                        if ($msg_text == '')
175                                $msg_text = T_('An error occured');
176
177                        if ($msg_title == '')
178                                $msg_title = T_('General Error');
179                        break;
180
181                case CRITICAL_ERROR:
182                        // Critical errors mean we cannot rely on _ANY_ DB information being
183                        // available so we're going to dump out a simple echo'd statement
184
185                        if ($msg_text == '')
186                                $msg_text = T_('An critical error occured');
187
188                        if ($msg_title == '')
189                                $msg_title = T_('Critical Error');
190                        break;
191        }
192
193        // Add on DEBUG info if we've enabled debug mode and this is an error. This
194        // prevents debug info being output for general messages should DEBUG be
195        // set TRUE by accident (preventing confusion for the end user!)
196        if (DEBUG && ($msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR)) {
197                if ($debug_text != ''){
198                        $msg_text = $msg_text
199                                  . '<br /><br /><strong>'
200                                  . T_('DEBUG MODE')
201                                  . '</strong>'
202                                  . $debug_text
203                                  . '<br /><br /><b>Backtrace:</b><br />'
204                                  . backtrace();
205                }
206        }
207
208        echo "<html>\n<body>\n". $msg_title ."\n<br /><br />\n". $msg_text ."</body>\n</html>";
209        exit;
210}
211?>
Note: See TracBrowser for help on using the repository browser.