]>
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 | |
64bc92e3 | 46 | /** |
47 | * htmlspecialchars wrapper | |
48 | */ | |
49 | function escape($str) | |
50 | { | |
51 | return htmlspecialchars($str, ENT_COMPAT, 'UTF-8', false); | |
52 | } | |
53 | ||
54 | /** | |
55 | * Link sanitization before templating | |
56 | */ | |
57 | function sanitizeLink(&$link) | |
58 | { | |
59 | $link['url'] = escape($link['url']); // useful? | |
60 | $link['title'] = escape($link['title']); | |
61 | $link['description'] = escape($link['description']); | |
62 | $link['tags'] = escape($link['tags']); | |
63 | } | |
9186ab95 V |
64 | |
65 | /** | |
66 | * Checks if a string represents a valid date | |
67 | * | |
68 | * @param string a string-formatted date | |
69 | * @param format the expected DateTime format of the string | |
70 | * @return whether the string is a valid date | |
71 | * @see http://php.net/manual/en/class.datetime.php | |
72 | * @see http://php.net/manual/en/datetime.createfromformat.php | |
73 | */ | |
74 | function checkDateFormat($format, $string) | |
75 | { | |
76 | $date = DateTime::createFromFormat($format, $string); | |
77 | return $date && $date->format($string) == $string; | |
78 | } | |
775803a0 A |
79 | |
80 | /** | |
81 | * Generate a header location from HTTP_REFERER. | |
82 | * Make sure the referer is Shaarli itself and prevent redirection loop. | |
83 | * | |
84 | * @param string $referer - HTTP_REFERER. | |
85 | * @param string $host - Server HOST. | |
86 | * @param array $loopTerms - Contains list of term to prevent redirection loop. | |
87 | * | |
88 | * @return string $referer - final referer. | |
89 | */ | |
90 | function generateLocation($referer, $host, $loopTerms = array()) | |
91 | { | |
d01c2342 | 92 | $finalReferer = '?'; |
775803a0 A |
93 | |
94 | // No referer if it contains any value in $loopCriteria. | |
95 | foreach ($loopTerms as $value) { | |
96 | if (strpos($referer, $value) !== false) { | |
d01c2342 | 97 | return $finalReferer; |
775803a0 A |
98 | } |
99 | } | |
100 | ||
101 | // Remove port from HTTP_HOST | |
102 | if ($pos = strpos($host, ':')) { | |
103 | $host = substr($host, 0, $pos); | |
104 | } | |
105 | ||
d01c2342 A |
106 | $refererHost = parse_url($referer, PHP_URL_HOST); |
107 | if (!empty($referer) && (strpos($refererHost, $host) !== false || startsWith('?', $refererHost))) { | |
108 | $finalReferer = $referer; | |
775803a0 A |
109 | } |
110 | ||
d01c2342 | 111 | return $finalReferer; |
775803a0 | 112 | } |
d1e2f8e5 | 113 | |
06b6660a A |
114 | /** |
115 | * Validate session ID to prevent Full Path Disclosure. | |
68bc2135 | 116 | * |
06b6660a | 117 | * See #298. |
68bc2135 | 118 | * The session ID's format depends on the hash algorithm set in PHP settings |
06b6660a A |
119 | * |
120 | * @param string $sessionId Session ID | |
121 | * | |
122 | * @return true if valid, false otherwise. | |
68bc2135 V |
123 | * |
124 | * @see http://php.net/manual/en/function.hash-algos.php | |
125 | * @see http://php.net/manual/en/session.configuration.php | |
06b6660a A |
126 | */ |
127 | function is_session_id_valid($sessionId) | |
128 | { | |
129 | if (empty($sessionId)) { | |
130 | return false; | |
131 | } | |
132 | ||
133 | if (!$sessionId) { | |
134 | return false; | |
135 | } | |
136 | ||
68bc2135 | 137 | if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) { |
06b6660a A |
138 | return false; |
139 | } | |
140 | ||
141 | return true; | |
142 | } | |
90e5bd65 A |
143 | |
144 | /** | |
145 | * In a string, converts URLs to clickable links. | |
146 | * | |
147 | * @param string $text input string. | |
148 | * @param string $redirector if a redirector is set, use it to gerenate links. | |
149 | * | |
150 | * @return string returns $text with all links converted to HTML links. | |
151 | * | |
152 | * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722 | |
153 | */ | |
154 | function text2clickable($text, $redirector) | |
155 | { | |
156 | $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[[:alnum:]]/?)!si'; | |
157 | ||
158 | if (empty($redirector)) { | |
159 | return preg_replace($regex, '<a href="$1">$1</a>', $text); | |
160 | } | |
161 | // Redirector is set, urlencode the final URL. | |
162 | return preg_replace_callback( | |
163 | $regex, | |
164 | function ($matches) use ($redirector) { | |
165 | return '<a href="' . $redirector . urlencode($matches[1]) .'">'. $matches[1] .'</a>'; | |
166 | }, | |
167 | $text | |
168 | ); | |
169 | } | |
170 | ||
171 | /** | |
172 | * This function inserts where relevant so that multiple spaces are properly displayed in HTML | |
173 | * even in the absence of <pre> (This is used in description to keep text formatting). | |
174 | * | |
175 | * @param string $text input text. | |
176 | * | |
177 | * @return string formatted text. | |
178 | */ | |
179 | function space2nbsp($text) | |
180 | { | |
181 | return preg_replace('/(^| ) /m', '$1 ', $text); | |
182 | } | |
183 | ||
184 | /** | |
185 | * Format Shaarli's description | |
186 | * TODO: Move me to ApplicationUtils when it's ready. | |
187 | * | |
188 | * @param string $description shaare's description. | |
189 | * @param string $redirector if a redirector is set, use it to gerenate links. | |
190 | * | |
191 | * @return string formatted description. | |
192 | */ | |
193 | function format_description($description, $redirector) { | |
194 | return nl2br(space2nbsp(text2clickable($description, $redirector))); | |
195 | } |