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