Utils.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /**
  3. * Shaarli utilities
  4. */
  5. /**
  6. * Logs a message to a text file
  7. *
  8. * The log format is compatible with fail2ban.
  9. *
  10. * @param string $logFile where to write the logs
  11. * @param string $clientIp the client's remote IPv4/IPv6 address
  12. * @param string $message the message to log
  13. */
  14. function logm($logFile, $clientIp, $message)
  15. {
  16. file_put_contents(
  17. $logFile,
  18. date('Y/m/d H:i:s').' - '.$clientIp.' - '.strval($message).PHP_EOL,
  19. FILE_APPEND
  20. );
  21. }
  22. /**
  23. * Returns the small hash of a string, using RFC 4648 base64url format
  24. *
  25. * Small hashes:
  26. * - are unique (well, as unique as crc32, at last)
  27. * - are always 6 characters long.
  28. * - only use the following characters: a-z A-Z 0-9 - _ @
  29. * - are NOT cryptographically secure (they CAN be forged)
  30. *
  31. * In Shaarli, they are used as a tinyurl-like link to individual entries,
  32. * built once with the combination of the date and item ID.
  33. * e.g. smallHash('20111006_131924' . 142) --> eaWxtQ
  34. *
  35. * @warning before v0.8.1, smallhashes were built only with the date,
  36. * and their value has been preserved.
  37. *
  38. * @param string $text Create a hash from this text.
  39. *
  40. * @return string generated small hash.
  41. */
  42. function smallHash($text)
  43. {
  44. $t = rtrim(base64_encode(hash('crc32', $text, true)), '=');
  45. return strtr($t, '+/', '-_');
  46. }
  47. /**
  48. * Tells if a string start with a substring
  49. *
  50. * @param string $haystack Given string.
  51. * @param string $needle String to search at the beginning of $haystack.
  52. * @param bool $case Case sensitive.
  53. *
  54. * @return bool True if $haystack starts with $needle.
  55. */
  56. function startsWith($haystack, $needle, $case = true)
  57. {
  58. if ($case) {
  59. return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
  60. }
  61. return (strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
  62. }
  63. /**
  64. * Tells if a string ends with a substring
  65. *
  66. * @param string $haystack Given string.
  67. * @param string $needle String to search at the end of $haystack.
  68. * @param bool $case Case sensitive.
  69. *
  70. * @return bool True if $haystack ends with $needle.
  71. */
  72. function endsWith($haystack, $needle, $case = true)
  73. {
  74. if ($case) {
  75. return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
  76. }
  77. return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
  78. }
  79. /**
  80. * Htmlspecialchars wrapper
  81. * Support multidimensional array of strings.
  82. *
  83. * @param mixed $input Data to escape: a single string or an array of strings.
  84. *
  85. * @return string escaped.
  86. */
  87. function escape($input)
  88. {
  89. if (is_bool($input)) {
  90. return $input;
  91. }
  92. if (is_array($input)) {
  93. $out = array();
  94. foreach($input as $key => $value) {
  95. $out[$key] = escape($value);
  96. }
  97. return $out;
  98. }
  99. return htmlspecialchars($input, ENT_COMPAT, 'UTF-8', false);
  100. }
  101. /**
  102. * Reverse the escape function.
  103. *
  104. * @param string $str the string to unescape.
  105. *
  106. * @return string unescaped string.
  107. */
  108. function unescape($str)
  109. {
  110. return htmlspecialchars_decode($str);
  111. }
  112. /**
  113. * Sanitize link before rendering.
  114. *
  115. * @param array $link Link to escape.
  116. */
  117. function sanitizeLink(&$link)
  118. {
  119. $link['url'] = escape($link['url']); // useful?
  120. $link['title'] = escape($link['title']);
  121. $link['description'] = escape($link['description']);
  122. $link['tags'] = escape($link['tags']);
  123. }
  124. /**
  125. * Checks if a string represents a valid date
  126. * @param string $format The expected DateTime format of the string
  127. * @param string $string A string-formatted date
  128. *
  129. * @return bool whether the string is a valid date
  130. *
  131. * @see http://php.net/manual/en/class.datetime.php
  132. * @see http://php.net/manual/en/datetime.createfromformat.php
  133. */
  134. function checkDateFormat($format, $string)
  135. {
  136. $date = DateTime::createFromFormat($format, $string);
  137. return $date && $date->format($string) == $string;
  138. }
  139. /**
  140. * Generate a header location from HTTP_REFERER.
  141. * Make sure the referer is Shaarli itself and prevent redirection loop.
  142. *
  143. * @param string $referer - HTTP_REFERER.
  144. * @param string $host - Server HOST.
  145. * @param array $loopTerms - Contains list of term to prevent redirection loop.
  146. *
  147. * @return string $referer - final referer.
  148. */
  149. function generateLocation($referer, $host, $loopTerms = array())
  150. {
  151. $finalReferer = '?';
  152. // No referer if it contains any value in $loopCriteria.
  153. foreach ($loopTerms as $value) {
  154. if (strpos($referer, $value) !== false) {
  155. return $finalReferer;
  156. }
  157. }
  158. // Remove port from HTTP_HOST
  159. if ($pos = strpos($host, ':')) {
  160. $host = substr($host, 0, $pos);
  161. }
  162. $refererHost = parse_url($referer, PHP_URL_HOST);
  163. if (!empty($referer) && (strpos($refererHost, $host) !== false || startsWith('?', $refererHost))) {
  164. $finalReferer = $referer;
  165. }
  166. return $finalReferer;
  167. }
  168. /**
  169. * Validate session ID to prevent Full Path Disclosure.
  170. *
  171. * See #298.
  172. * The session ID's format depends on the hash algorithm set in PHP settings
  173. *
  174. * @param string $sessionId Session ID
  175. *
  176. * @return true if valid, false otherwise.
  177. *
  178. * @see http://php.net/manual/en/function.hash-algos.php
  179. * @see http://php.net/manual/en/session.configuration.php
  180. */
  181. function is_session_id_valid($sessionId)
  182. {
  183. if (empty($sessionId)) {
  184. return false;
  185. }
  186. if (!$sessionId) {
  187. return false;
  188. }
  189. if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) {
  190. return false;
  191. }
  192. return true;
  193. }
  194. /**
  195. * Sniff browser language to set the locale automatically.
  196. * Note that is may not work on your server if the corresponding locale is not installed.
  197. *
  198. * @param string $headerLocale Locale send in HTTP headers (e.g. "fr,fr-fr;q=0.8,en;q=0.5,en-us;q=0.3").
  199. **/
  200. function autoLocale($headerLocale)
  201. {
  202. // Default if browser does not send HTTP_ACCEPT_LANGUAGE
  203. $locales = array('en_US', 'en_US.utf8', 'en_US.UTF-8');
  204. if (! empty($headerLocale)) {
  205. if (preg_match_all('/([a-z]{2,3})[-_]?([a-z]{2})?,?/i', $headerLocale, $matches, PREG_SET_ORDER)) {
  206. $attempts = [];
  207. foreach ($matches as $match) {
  208. $first = [strtolower($match[1]), strtoupper($match[1])];
  209. $separators = ['_', '-'];
  210. $encodings = ['utf8', 'UTF-8'];
  211. if (!empty($match[2])) {
  212. $second = [strtoupper($match[2]), strtolower($match[2])];
  213. $items = [$first, $separators, $second, ['.'], $encodings];
  214. } else {
  215. $items = [$first, $separators, $first, ['.'], $encodings];
  216. }
  217. $attempts = array_merge($attempts, iterator_to_array(cartesian_product_generator($items)));
  218. }
  219. if (! empty($attempts)) {
  220. $locales = array_merge(array_map('implode', $attempts), $locales);
  221. }
  222. }
  223. }
  224. setlocale(LC_ALL, $locales);
  225. }
  226. /**
  227. * Build a Generator object representing the cartesian product from given $items.
  228. *
  229. * Example:
  230. * [['a'], ['b', 'c']]
  231. * will generate:
  232. * [
  233. * ['a', 'b'],
  234. * ['a', 'c'],
  235. * ]
  236. *
  237. * @param array $items array of array of string
  238. *
  239. * @return Generator representing the cartesian product of given array.
  240. *
  241. * @see https://en.wikipedia.org/wiki/Cartesian_product
  242. */
  243. function cartesian_product_generator($items)
  244. {
  245. if (empty($items)) {
  246. yield [];
  247. }
  248. $subArray = array_pop($items);
  249. if (empty($subArray)) {
  250. return;
  251. }
  252. foreach (cartesian_product_generator($items) as $item) {
  253. foreach ($subArray as $value) {
  254. yield $item + [count($item) => $value];
  255. }
  256. }
  257. }
  258. /**
  259. * Generates a default API secret.
  260. *
  261. * Note that the random-ish methods used in this function are predictable,
  262. * which makes them NOT suitable for crypto.
  263. * BUT the random string is salted with the salt and hashed with the username.
  264. * It makes the generated API secret secured enough for Shaarli.
  265. *
  266. * PHP 7 provides random_int(), designed for cryptography.
  267. * More info: http://stackoverflow.com/questions/4356289/php-random-string-generator
  268. * @param string $username Shaarli login username
  269. * @param string $salt Shaarli password hash salt
  270. *
  271. * @return string|bool Generated API secret, 12 char length.
  272. * Or false if invalid parameters are provided (which will make the API unusable).
  273. */
  274. function generate_api_secret($username, $salt)
  275. {
  276. if (empty($username) || empty($salt)) {
  277. return false;
  278. }
  279. return str_shuffle(substr(hash_hmac('sha512', uniqid($salt), $username), 10, 12));
  280. }
  281. /**
  282. * Trim string, replace sequences of whitespaces by a single space.
  283. * PHP equivalent to `normalize-space` XSLT function.
  284. *
  285. * @param string $string Input string.
  286. *
  287. * @return mixed Normalized string.
  288. */
  289. function normalize_spaces($string)
  290. {
  291. return preg_replace('/\s{2,}/', ' ', trim($string));
  292. }
  293. /**
  294. * Format the date according to the locale.
  295. *
  296. * Requires php-intl to display international datetimes,
  297. * otherwise default format '%c' will be returned.
  298. *
  299. * @param DateTime $date to format.
  300. * @param bool $time Displays time if true.
  301. * @param bool $intl Use international format if true.
  302. *
  303. * @return bool|string Formatted date, or false if the input is invalid.
  304. */
  305. function format_date($date, $time = true, $intl = true)
  306. {
  307. if (! $date instanceof DateTime) {
  308. return false;
  309. }
  310. if (! $intl || ! class_exists('IntlDateFormatter')) {
  311. $format = $time ? '%c' : '%x';
  312. return strftime($format, $date->getTimestamp());
  313. }
  314. $formatter = new IntlDateFormatter(
  315. setlocale(LC_TIME, 0),
  316. IntlDateFormatter::LONG,
  317. $time ? IntlDateFormatter::LONG : IntlDateFormatter::NONE
  318. );
  319. return $formatter->format($date);
  320. }
  321. /**
  322. * Check if the input is an integer, no matter its real type.
  323. *
  324. * PHP is a bit messy regarding this:
  325. * - is_int returns false if the input is a string
  326. * - ctype_digit returns false if the input is an integer or negative
  327. *
  328. * @param mixed $input value
  329. *
  330. * @return bool true if the input is an integer, false otherwise
  331. */
  332. function is_integer_mixed($input)
  333. {
  334. if (is_array($input) || is_bool($input) || is_object($input)) {
  335. return false;
  336. }
  337. $input = strval($input);
  338. return ctype_digit($input) || (startsWith($input, '-') && ctype_digit(substr($input, 1)));
  339. }
  340. /**
  341. * Convert post_max_size/upload_max_filesize (e.g. '16M') parameters to bytes.
  342. *
  343. * @param string $val Size expressed in string.
  344. *
  345. * @return int Size expressed in bytes.
  346. */
  347. function return_bytes($val)
  348. {
  349. if (is_integer_mixed($val) || $val === '0' || empty($val)) {
  350. return $val;
  351. }
  352. $val = trim($val);
  353. $last = strtolower($val[strlen($val)-1]);
  354. $val = intval(substr($val, 0, -1));
  355. switch($last) {
  356. case 'g': $val *= 1024;
  357. case 'm': $val *= 1024;
  358. case 'k': $val *= 1024;
  359. }
  360. return $val;
  361. }
  362. /**
  363. * Return a human readable size from bytes.
  364. *
  365. * @param int $bytes value
  366. *
  367. * @return string Human readable size
  368. */
  369. function human_bytes($bytes)
  370. {
  371. if ($bytes === '') {
  372. return t('Setting not set');
  373. }
  374. if (! is_integer_mixed($bytes)) {
  375. return $bytes;
  376. }
  377. $bytes = intval($bytes);
  378. if ($bytes === 0) {
  379. return t('Unlimited');
  380. }
  381. $units = [t('B'), t('kiB'), t('MiB'), t('GiB')];
  382. for ($i = 0; $i < count($units) && $bytes >= 1024; ++$i) {
  383. $bytes /= 1024;
  384. }
  385. return round($bytes) . $units[$i];
  386. }
  387. /**
  388. * Try to determine max file size for uploads (POST).
  389. * Returns an integer (in bytes) or formatted depending on $format.
  390. *
  391. * @param mixed $limitPost post_max_size PHP setting
  392. * @param mixed $limitUpload upload_max_filesize PHP setting
  393. * @param bool $format Format max upload size to human readable size
  394. *
  395. * @return int|string max upload file size
  396. */
  397. function get_max_upload_size($limitPost, $limitUpload, $format = true)
  398. {
  399. $size1 = return_bytes($limitPost);
  400. $size2 = return_bytes($limitUpload);
  401. // Return the smaller of two:
  402. $maxsize = min($size1, $size2);
  403. return $format ? human_bytes($maxsize) : $maxsize;
  404. }
  405. /**
  406. * Sort the given array alphabetically using php-intl if available.
  407. * Case sensitive.
  408. *
  409. * Note: doesn't support multidimensional arrays
  410. *
  411. * @param array $data Input array, passed by reference
  412. * @param bool $reverse Reverse sort if set to true
  413. * @param bool $byKeys Sort the array by keys if set to true, by value otherwise.
  414. */
  415. function alphabetical_sort(&$data, $reverse = false, $byKeys = false)
  416. {
  417. $callback = function($a, $b) use ($reverse) {
  418. // Collator is part of PHP intl.
  419. if (class_exists('Collator')) {
  420. $collator = new Collator(setlocale(LC_COLLATE, 0));
  421. if (!intl_is_failure(intl_get_error_code())) {
  422. return $collator->compare($a, $b) * ($reverse ? -1 : 1);
  423. }
  424. }
  425. return strcasecmp($a, $b) * ($reverse ? -1 : 1);
  426. };
  427. if ($byKeys) {
  428. uksort($data, $callback);
  429. } else {
  430. usort($data, $callback);
  431. }
  432. }