summaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/functions.php
blob: 802a7f1ad67ff45cd5957f9f8e8fe865d2db7a44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
 * Defines some commonly used functions.
 *
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

// Converts tags:
// - direction = out: convert spaces to underscores;
// - direction = in: convert underscores to spaces.
function convertTag($tag, $direction = 'out') {
	if ($direction == 'out') {
		$tag = str_replace(' ', '_', $tag);
	} else {
		$tag = str_replace('_', ' ', $tag);
	}
	return $tag;
}

function filter($data, $type = NULL) {
	if (is_string($data)) {
		$data = trim($data);
		$data = stripslashes($data);
		switch ($type) {
			case 'url':
				$data = rawurlencode($data);
				break;
			default:
				$data = htmlspecialchars($data);
				break;
		}
	} else if (is_array($data)) {
		foreach(array_keys($data) as $key) {
			$row =& $data[$key];
			$row = filter($row, $type);
		}
	}
	return $data;
}

/**
 * Returns the number of bookmarks that shall be displayed on one page.
 *
 * @param SemanticScuttle_Model_User $userObject Object of the current user
 *
 * @return integer Number of bookmarks per page
 *
 * @uses $defaultPerPage
 * @uses $defaultPerPageForAdmins
 */
function getPerPageCount($userObject = null)
{
	global $defaultPerPage, $defaultPerPageForAdmins;

	if (isset($defaultPerPageForAdmins)
        && $userObject != null && $userObject->isAdmin()
    ) {
		return $defaultPerPageForAdmins;
	} else {
		return $defaultPerPage;
	}
}

function getSortOrder($override = NULL) {
	global $defaultOrderBy;

	if (isset($_GET['sort'])) {
		return preg_replace('/[^a-z_]/', '', $_GET['sort']);
	} else if (isset($override)) {
		return $override;
	} else {
		return $defaultOrderBy;
	}
}

function multi_array_search($needle, $haystack) {
	if (is_array($haystack)) {
		foreach(array_keys($haystack) as $key) {
			$value =& $haystack[$key];
			$result = multi_array_search($needle, $value);
			if (is_array($result)) {
				$return = $result;
				array_unshift($return, $key);
				return $return;
			} elseif ($result == true) {
				$return[] = $key;
				return $return;
			}
		}
		return false;
	} else {
		if ($needle === $haystack) {
			return true;
		} else {
			return false;
		}
	}
}

function createURL($page = '', $ending = '') {
	global $cleanurls;
	if (!$cleanurls && $page != '') {
		$page .= '.php';
	}
	if(strlen($ending)>0) {
		return ROOT . $page .'/'. $ending;
	} else {
		return ROOT . $page;
	}
}

/**
 * Adds the protocol to the URL if it's missing.
 * If the current URL is served via HTTPS, https will be used as protocol.
 *
 * Useful to fix ROOT urls of SemanticScuttle when it's needed, e.g.
 * in the bookmarklet or the feed.
 *
 * @param string $url Url with or without the protocol ("//example.org/foo")
 *
 * @return string URL with a HTTP protocol
 */
function addProtocolToUrl($url)
{
    if (substr($url, 0, 2) != '//') {
        return $url;
    }

    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) {
        $protocol = 'https:';
    } else {
        $protocol = 'http:';
    }
    return $protocol . $url;
}
/**
 * Creates a "vote for/against this bookmark" URL.
 * Also runs htmlspecialchars() on them to prevent XSS.
 *
 * @param boolean $for For the bookmark (true) or against (false)
 * @param integer $bId Bookmark ID
 *
 * @return string URL to use
 */
function createVoteURL($for, $bId)
{
    return htmlspecialchars(
        createURL(
            'vote',
            ($for ? 'for' : 'against') . '/' . $bId
        ),
        ENT_QUOTES
    );
}

/* Shorten a string like a URL for example by cutting the middle of it */
function shortenString($string, $maxSize=75) {
	$output = '';
	if(strlen($string) > $maxSize) {
		$output = substr($string, 0, $maxSize/2).'...'.substr($string, -$maxSize/2);
	} else {
		$output = $string;
	}
	return $output;
}

/* Check url format and check online if the url is a valid page (Not a 404 error for example) */
function checkUrl($url, $checkOnline = true) {
	//check format
	if(!preg_match("#(ht|f)tp(s?)\://\S+\.\S+#i",$url)) {
		return false;
	}

	if($checkOnline) {
		//look if the page doesn't return a void or 40X or 50X HTTP code error
		$h = @get_headers($url);
		if(is_array($h) && strpos($h[0], '40') === false && strpos($h[0], '50') === false) {
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}

/* Returns a concatenated String
 * including all the tags from the array $arrayTags (excepted of the $exceptedTag)
 * separated by the $separator.
 * */
function aggregateTags($arrayTags, $separator = ' + ', $exceptedTag = '') {
	$output = '';

	for($i = 0; $i<count($arrayTags); $i++) {
		if($arrayTags[$i] != $exceptedTag) {
			$output.= $arrayTags[$i] . $separator;
		}
	}
	return substr($output, 0, strlen($output) - strlen($separator) );
}

function message_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '', $err_file = '', $sql = '', $db = NULL) {
	if(defined('HAS_DIED'))
	die(T_('message_die() was called multiple times.'));
	define('HAS_DIED', 1);

	$sql_store = $sql;

	// Get SQL error if we are debugging. Do this as soon as possible to prevent
	// subsequent queries from overwriting the status of sql_error()
	if (DEBUG_MODE && ($msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR)) {
		$sql_error = is_null($db) ? '' : $db->sql_error();
		$debug_text = '';

		if ($sql_error['message'] != '')
		$debug_text .= '<br /><br />'. T_('SQL Error') .' : '. $sql_error['code'] .' '. $sql_error['message'];

		if ($sql_store != '')
		$debug_text .= '<br /><br />'. $sql_store;

		if ($err_line != '' && $err_file != '')
		$debug_text .= '</br /><br />'. T_('Line') .' : '. $err_line .'<br />'. T_('File') .' :'. $err_file;

		debug_print_backtrace();
	}

	switch($msg_code) {
		case GENERAL_MESSAGE:
			if ($msg_title == '')
			$msg_title = T_('Information');
			break;

		case CRITICAL_MESSAGE:
			if ($msg_title == '')
			$msg_title = T_('Critical Information');
			break;

		case GENERAL_ERROR:
			if ($msg_text == '')
			$msg_text = T_('An error occured');

			if ($msg_title == '')
			$msg_title = T_('General Error');
			break;

		case CRITICAL_ERROR:
			// Critical errors mean we cannot rely on _ANY_ DB information being
			// available so we're going to dump out a simple echo'd statement

			if ($msg_text == '')
			$msg_text = T_('An critical error occured');

			if ($msg_title == '')
			$msg_title = T_('Critical Error');
			break;
	}

	// Add on DEBUG_MODE info if we've enabled debug mode and this is an error. This
	// prevents debug info being output for general messages should DEBUG_MODE be
	// set TRUE by accident (preventing confusion for the end user!)
	if (DEBUG_MODE && ($msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR)) {
		if ($debug_text != '')
		$msg_text = $msg_text . '<br /><br /><strong>'. T_('DEBUG MODE') .'</strong>'. $debug_text;
	}

	echo "<html>\n<body>\n". $msg_title ."\n<br /><br />\n". $msg_text ."</body>\n</html>";
	exit;
}

/**
 * Calls reset() on the given arg, without the E_STRICT error
 * "Only variables should be passed by reference"
 *
 * @param array $arg Array to return first element of
 *
 * @return mixed First element of the array
 */
function rreset($array)
{
    return reset($array);
}
?>