]>
Commit | Line | Data |
---|---|---|
ca74886f V |
1 | <?php |
2 | /** | |
3 | * Shaarli utilities | |
4 | */ | |
5 | ||
6 | /** | |
7 | * Returns the small hash of a string, using RFC 4648 base64url format | |
8 | * | |
9 | * Small hashes: | |
10 | * - are unique (well, as unique as crc32, at last) | |
11 | * - are always 6 characters long. | |
12 | * - only use the following characters: a-z A-Z 0-9 - _ @ | |
13 | * - are NOT cryptographically secure (they CAN be forged) | |
14 | * | |
15 | * In Shaarli, they are used as a tinyurl-like link to individual entries, | |
16 | * e.g. smallHash('20111006_131924') --> yZH23w | |
17 | */ | |
18 | function smallHash($text) | |
19 | { | |
20 | $t = rtrim(base64_encode(hash('crc32', $text, true)), '='); | |
21 | return strtr($t, '+/', '-_'); | |
22 | } | |
23 | ||
24 | /** | |
25 | * Tells if a string start with a substring | |
26 | */ | |
27 | function startsWith($haystack, $needle, $case=true) | |
28 | { | |
29 | if ($case) { | |
30 | return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0); | |
31 | } | |
32 | return (strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0); | |
33 | } | |
34 | ||
35 | /** | |
36 | * Tells if a string ends with a substring | |
37 | */ | |
38 | function endsWith($haystack, $needle, $case=true) | |
39 | { | |
40 | if ($case) { | |
41 | return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0); | |
42 | } | |
43 | return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0); | |
44 | } | |
64bc92e3 | 45 | |
46 | /** | |
47 | * Same as nl2br(), but escapes < and > | |
48 | */ | |
49 | function nl2br_escaped($html) | |
50 | { | |
d1e2f8e5 | 51 | return str_replace('>', '>', str_replace('<', '<', nl2br($html))); |
64bc92e3 | 52 | } |
53 | ||
54 | /** | |
55 | * htmlspecialchars wrapper | |
56 | */ | |
57 | function escape($str) | |
58 | { | |
59 | return htmlspecialchars($str, ENT_COMPAT, 'UTF-8', false); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Link sanitization before templating | |
64 | */ | |
65 | function sanitizeLink(&$link) | |
66 | { | |
67 | $link['url'] = escape($link['url']); // useful? | |
68 | $link['title'] = escape($link['title']); | |
69 | $link['description'] = escape($link['description']); | |
70 | $link['tags'] = escape($link['tags']); | |
71 | } | |
9186ab95 V |
72 | |
73 | /** | |
74 | * Checks if a string represents a valid date | |
75 | * | |
76 | * @param string a string-formatted date | |
77 | * @param format the expected DateTime format of the string | |
78 | * @return whether the string is a valid date | |
79 | * @see http://php.net/manual/en/class.datetime.php | |
80 | * @see http://php.net/manual/en/datetime.createfromformat.php | |
81 | */ | |
82 | function checkDateFormat($format, $string) | |
83 | { | |
84 | $date = DateTime::createFromFormat($format, $string); | |
85 | return $date && $date->format($string) == $string; | |
86 | } | |
775803a0 A |
87 | |
88 | /** | |
89 | * Generate a header location from HTTP_REFERER. | |
90 | * Make sure the referer is Shaarli itself and prevent redirection loop. | |
91 | * | |
92 | * @param string $referer - HTTP_REFERER. | |
93 | * @param string $host - Server HOST. | |
94 | * @param array $loopTerms - Contains list of term to prevent redirection loop. | |
95 | * | |
96 | * @return string $referer - final referer. | |
97 | */ | |
98 | function generateLocation($referer, $host, $loopTerms = array()) | |
99 | { | |
d01c2342 | 100 | $finalReferer = '?'; |
775803a0 A |
101 | |
102 | // No referer if it contains any value in $loopCriteria. | |
103 | foreach ($loopTerms as $value) { | |
104 | if (strpos($referer, $value) !== false) { | |
d01c2342 | 105 | return $finalReferer; |
775803a0 A |
106 | } |
107 | } | |
108 | ||
109 | // Remove port from HTTP_HOST | |
110 | if ($pos = strpos($host, ':')) { | |
111 | $host = substr($host, 0, $pos); | |
112 | } | |
113 | ||
d01c2342 A |
114 | $refererHost = parse_url($referer, PHP_URL_HOST); |
115 | if (!empty($referer) && (strpos($refererHost, $host) !== false || startsWith('?', $refererHost))) { | |
116 | $finalReferer = $referer; | |
775803a0 A |
117 | } |
118 | ||
d01c2342 | 119 | return $finalReferer; |
775803a0 | 120 | } |
d1e2f8e5 | 121 | |
06b6660a A |
122 | /** |
123 | * Validate session ID to prevent Full Path Disclosure. | |
68bc2135 | 124 | * |
06b6660a | 125 | * See #298. |
68bc2135 | 126 | * The session ID's format depends on the hash algorithm set in PHP settings |
06b6660a A |
127 | * |
128 | * @param string $sessionId Session ID | |
129 | * | |
130 | * @return true if valid, false otherwise. | |
68bc2135 V |
131 | * |
132 | * @see http://php.net/manual/en/function.hash-algos.php | |
133 | * @see http://php.net/manual/en/session.configuration.php | |
06b6660a A |
134 | */ |
135 | function is_session_id_valid($sessionId) | |
136 | { | |
137 | if (empty($sessionId)) { | |
138 | return false; | |
139 | } | |
140 | ||
141 | if (!$sessionId) { | |
142 | return false; | |
143 | } | |
144 | ||
68bc2135 | 145 | if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) { |
06b6660a A |
146 | return false; |
147 | } | |
148 | ||
149 | return true; | |
150 | } | |
90e5bd65 A |
151 | |
152 | /** | |
153 | * In a string, converts URLs to clickable links. | |
154 | * | |
155 | * @param string $text input string. | |
156 | * @param string $redirector if a redirector is set, use it to gerenate links. | |
157 | * | |
158 | * @return string returns $text with all links converted to HTML links. | |
159 | * | |
160 | * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722 | |
161 | */ | |
162 | function text2clickable($text, $redirector) | |
163 | { | |
164 | $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[[:alnum:]]/?)!si'; | |
165 | ||
166 | if (empty($redirector)) { | |
167 | return preg_replace($regex, '<a href="$1">$1</a>', $text); | |
168 | } | |
169 | // Redirector is set, urlencode the final URL. | |
170 | return preg_replace_callback( | |
171 | $regex, | |
172 | function ($matches) use ($redirector) { | |
173 | return '<a href="' . $redirector . urlencode($matches[1]) .'">'. $matches[1] .'</a>'; | |
174 | }, | |
175 | $text | |
176 | ); | |
177 | } | |
178 | ||
179 | /** | |
180 | * This function inserts where relevant so that multiple spaces are properly displayed in HTML | |
181 | * even in the absence of <pre> (This is used in description to keep text formatting). | |
182 | * | |
183 | * @param string $text input text. | |
184 | * | |
185 | * @return string formatted text. | |
186 | */ | |
187 | function space2nbsp($text) | |
188 | { | |
189 | return preg_replace('/(^| ) /m', '$1 ', $text); | |
190 | } | |
191 | ||
192 | /** | |
193 | * Format Shaarli's description | |
194 | * TODO: Move me to ApplicationUtils when it's ready. | |
195 | * | |
196 | * @param string $description shaare's description. | |
197 | * @param string $redirector if a redirector is set, use it to gerenate links. | |
198 | * | |
199 | * @return string formatted description. | |
200 | */ | |
201 | function format_description($description, $redirector) { | |
202 | return nl2br(space2nbsp(text2clickable($description, $redirector))); | |
203 | } |