4
0

lib_rss.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <?php
  2. if (version_compare(PHP_VERSION, '5.6.0', '<')) {
  3. die('FreshRSS error: FreshRSS requires PHP 5.6.0+!');
  4. }
  5. if (!function_exists('mb_strcut')) {
  6. function mb_strcut($str, $start, $length = null, $encoding = 'UTF-8') {
  7. return substr($str, $start, $length);
  8. }
  9. }
  10. /**
  11. * Build a directory path by concatenating a list of directory names.
  12. *
  13. * @param $path_parts a list of directory names
  14. * @return a string corresponding to the final pathname
  15. */
  16. function join_path() {
  17. $path_parts = func_get_args();
  18. return join(DIRECTORY_SEPARATOR, $path_parts);
  19. }
  20. //<Auto-loading>
  21. function classAutoloader($class) {
  22. if (strpos($class, 'FreshRSS') === 0) {
  23. $components = explode('_', $class);
  24. switch (count($components)) {
  25. case 1:
  26. include(APP_PATH . '/' . $components[0] . '.php');
  27. return;
  28. case 2:
  29. include(APP_PATH . '/Models/' . $components[1] . '.php');
  30. return;
  31. case 3: //Controllers, Exceptions
  32. include(APP_PATH . '/' . $components[2] . 's/' . $components[1] . $components[2] . '.php');
  33. return;
  34. }
  35. } elseif (strpos($class, 'Minz') === 0) {
  36. include(LIB_PATH . '/' . str_replace('_', '/', $class) . '.php');
  37. } elseif (strpos($class, 'SimplePie') === 0) {
  38. include(LIB_PATH . '/SimplePie/' . str_replace('_', '/', $class) . '.php');
  39. } elseif (strpos($class, 'PHPMailer') === 0) {
  40. include(LIB_PATH . '/' . str_replace('\\', '/', $class) . '.php');
  41. }
  42. }
  43. spl_autoload_register('classAutoloader');
  44. //</Auto-loading>
  45. function idn_to_puny($url) {
  46. if (function_exists('idn_to_ascii')) {
  47. $idn = parse_url($url, PHP_URL_HOST);
  48. if ($idn != '') {
  49. // https://wiki.php.net/rfc/deprecate-and-remove-intl_idna_variant_2003
  50. if (defined('INTL_IDNA_VARIANT_UTS46')) {
  51. $puny = idn_to_ascii($idn, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
  52. } elseif (defined('INTL_IDNA_VARIANT_2003')) {
  53. $puny = idn_to_ascii($idn, IDNA_DEFAULT, INTL_IDNA_VARIANT_2003);
  54. } else {
  55. $puny = idn_to_ascii($idn);
  56. }
  57. $pos = strpos($url, $idn);
  58. if ($puny != '' && $pos !== false) {
  59. $url = substr_replace($url, $puny, $pos, strlen($idn));
  60. }
  61. }
  62. }
  63. return $url;
  64. }
  65. function checkUrl($url) {
  66. if ($url == '') {
  67. return '';
  68. }
  69. if (!preg_match('#^https?://#i', $url)) {
  70. $url = 'http://' . $url;
  71. }
  72. $url = idn_to_puny($url); //PHP bug #53474 IDN
  73. if (filter_var($url, FILTER_VALIDATE_URL)) {
  74. return $url;
  75. } else {
  76. return false;
  77. }
  78. }
  79. function safe_ascii($text) {
  80. return filter_var($text, FILTER_DEFAULT, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
  81. }
  82. if (function_exists('mb_convert_encoding')) {
  83. function safe_utf8($text) { return mb_convert_encoding($text, 'UTF-8', 'UTF-8'); }
  84. } elseif (function_exists('iconv')) {
  85. function safe_utf8($text) { return iconv('UTF-8', 'UTF-8//IGNORE', $text); }
  86. } else {
  87. function safe_utf8($text) { return $text; }
  88. }
  89. function escapeToUnicodeAlternative($text, $extended = true) {
  90. $text = htmlspecialchars_decode($text, ENT_QUOTES);
  91. //Problematic characters
  92. $problem = array('&', '<', '>');
  93. //Use their fullwidth Unicode form instead:
  94. $replace = array('&', '<', '>');
  95. // https://raw.githubusercontent.com/mihaip/google-reader-api/master/wiki/StreamId.wiki
  96. if ($extended) {
  97. $problem += array("'", '"', '^', '?', '\\', '/', ',', ';');
  98. $replace += array("’", '"', '^', '?', '\', '/', ',', ';');
  99. }
  100. return trim(str_replace($problem, $replace, $text));
  101. }
  102. /**
  103. * Test if a given server address is publicly accessible.
  104. *
  105. * Note: for the moment it tests only if address is corresponding to a
  106. * localhost address.
  107. *
  108. * @param $address the address to test, can be an IP or a URL.
  109. * @return true if server is accessible, false otherwise.
  110. * @todo improve test with a more valid technique (e.g. test with an external server?)
  111. */
  112. function server_is_public($address) {
  113. $host = parse_url($address, PHP_URL_HOST);
  114. $is_public = !in_array($host, array(
  115. 'localhost',
  116. 'localhost.localdomain',
  117. '[::1]',
  118. 'ip6-localhost',
  119. 'localhost6',
  120. 'localhost6.localdomain6',
  121. ));
  122. if ($is_public) {
  123. $is_public &= !preg_match('/^(10|127|172[.]16|192[.]168)[.]/', $host);
  124. $is_public &= !preg_match('/^(\[)?(::1$|fc00::|fe80::)/i', $host);
  125. }
  126. return (bool)$is_public;
  127. }
  128. function format_number($n, $precision = 0) {
  129. // number_format does not seem to be Unicode-compatible
  130. return str_replace(' ', ' ', //Espace fine insécable
  131. number_format($n, $precision, '.', ' ')
  132. );
  133. }
  134. function format_bytes($bytes, $precision = 2, $system = 'IEC') {
  135. if ($system === 'IEC') {
  136. $base = 1024;
  137. $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB');
  138. } elseif ($system === 'SI') {
  139. $base = 1000;
  140. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  141. } else {
  142. return format_number($bytes, $precision);
  143. }
  144. $bytes = max(intval($bytes), 0);
  145. $pow = $bytes === 0 ? 0 : floor(log($bytes) / log($base));
  146. $pow = min($pow, count($units) - 1);
  147. $bytes /= pow($base, $pow);
  148. return format_number($bytes, $precision) . ' ' . $units[$pow];
  149. }
  150. function timestamptodate ($t, $hour = true) {
  151. $month = _t('gen.date.' . date('M', $t));
  152. if ($hour) {
  153. $date = _t('gen.date.format_date_hour', $month);
  154. } else {
  155. $date = _t('gen.date.format_date', $month);
  156. }
  157. return @date ($date, $t);
  158. }
  159. function html_only_entity_decode($text) {
  160. static $htmlEntitiesOnly = null;
  161. if ($htmlEntitiesOnly === null) {
  162. $htmlEntitiesOnly = array_flip(array_diff(
  163. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  164. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  165. ));
  166. }
  167. return strtr($text, $htmlEntitiesOnly);
  168. }
  169. function prepareSyslog() {
  170. return COPY_SYSLOG_TO_STDERR ? openlog("FreshRSS", LOG_PERROR | LOG_PID, LOG_USER) : false;
  171. }
  172. function customSimplePie($attributes = array()) {
  173. $system_conf = Minz_Configuration::get('system');
  174. $limits = $system_conf->limits;
  175. $simplePie = new SimplePie();
  176. $simplePie->set_useragent(FRESHRSS_USERAGENT);
  177. $simplePie->set_syslog($system_conf->simplepie_syslog_enabled);
  178. if ($system_conf->simplepie_syslog_enabled) {
  179. prepareSyslog();
  180. }
  181. $simplePie->set_cache_location(CACHE_PATH);
  182. $simplePie->set_cache_duration($limits['cache_duration']);
  183. $feed_timeout = empty($attributes['timeout']) ? 0 : intval($attributes['timeout']);
  184. $simplePie->set_timeout($feed_timeout > 0 ? $feed_timeout : $limits['timeout']);
  185. $curl_options = $system_conf->curl_options;
  186. if (isset($attributes['ssl_verify'])) {
  187. $curl_options[CURLOPT_SSL_VERIFYHOST] = $attributes['ssl_verify'] ? 2 : 0;
  188. $curl_options[CURLOPT_SSL_VERIFYPEER] = $attributes['ssl_verify'] ? true : false;
  189. }
  190. $simplePie->set_curl_options($curl_options);
  191. $simplePie->strip_htmltags(array(
  192. 'base', 'blink', 'body', 'doctype', 'embed',
  193. 'font', 'form', 'frame', 'frameset', 'html',
  194. 'link', 'input', 'marquee', 'meta', 'noscript',
  195. 'object', 'param', 'plaintext', 'script', 'style',
  196. 'svg', //TODO: Support SVG after sanitizing and URL rewriting of xlink:href
  197. ));
  198. $simplePie->strip_attributes(array_merge($simplePie->strip_attributes, array(
  199. 'autoplay', 'class', 'onload', 'onunload', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup',
  200. 'onmouseover', 'onmousemove', 'onmouseout', 'onfocus', 'onblur',
  201. 'onkeypress', 'onkeydown', 'onkeyup', 'onselect', 'onchange', 'seamless', 'sizes', 'srcset')));
  202. $simplePie->add_attributes(array(
  203. 'audio' => array('controls' => 'controls', 'preload' => 'none'),
  204. 'iframe' => array('sandbox' => 'allow-scripts allow-same-origin'),
  205. 'video' => array('controls' => 'controls', 'preload' => 'none'),
  206. ));
  207. $simplePie->set_url_replacements(array(
  208. 'a' => 'href',
  209. 'area' => 'href',
  210. 'audio' => 'src',
  211. 'blockquote' => 'cite',
  212. 'del' => 'cite',
  213. 'form' => 'action',
  214. 'iframe' => 'src',
  215. 'img' => array(
  216. 'longdesc',
  217. 'src'
  218. ),
  219. 'input' => 'src',
  220. 'ins' => 'cite',
  221. 'q' => 'cite',
  222. 'source' => 'src',
  223. 'track' => 'src',
  224. 'video' => array(
  225. 'poster',
  226. 'src',
  227. ),
  228. ));
  229. $https_domains = array();
  230. $force = @file(FRESHRSS_PATH . '/force-https.default.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  231. if (is_array($force)) {
  232. $https_domains = array_merge($https_domains, $force);
  233. }
  234. $force = @file(DATA_PATH . '/force-https.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  235. if (is_array($force)) {
  236. $https_domains = array_merge($https_domains, $force);
  237. }
  238. $simplePie->set_https_domains($https_domains);
  239. return $simplePie;
  240. }
  241. function sanitizeHTML($data, $base = '') {
  242. if (!is_string($data)) {
  243. return '';
  244. }
  245. static $simplePie = null;
  246. if ($simplePie == null) {
  247. $simplePie = customSimplePie();
  248. $simplePie->init();
  249. }
  250. return html_only_entity_decode($simplePie->sanitize->sanitize($data, SIMPLEPIE_CONSTRUCT_HTML, $base));
  251. }
  252. /**
  253. * Validate an email address, supports internationalized addresses.
  254. *
  255. * @param string $email The address to validate
  256. *
  257. * @return bool true if email is valid, else false
  258. */
  259. function validateEmailAddress($email) {
  260. $mailer = new PHPMailer\PHPMailer\PHPMailer();
  261. $mailer->Charset = 'utf-8';
  262. $punyemail = $mailer->punyencodeAddress($email);
  263. return PHPMailer\PHPMailer\PHPMailer::validateAddress($punyemail, 'html5');
  264. }
  265. /**
  266. * Add support of image lazy loading
  267. * Move content from src attribute to data-original
  268. * @param content is the text we want to parse
  269. */
  270. function lazyimg($content) {
  271. return preg_replace(
  272. '/<((?:img|iframe)[^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  273. '<$1src="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  274. $content
  275. );
  276. }
  277. function uTimeString() {
  278. $t = @gettimeofday();
  279. return $t['sec'] . str_pad($t['usec'], 6, '0', STR_PAD_LEFT);
  280. }
  281. function invalidateHttpCache($username = '') {
  282. if (!FreshRSS_user_Controller::checkUsername($username)) {
  283. Minz_Session::_param('touch', uTimeString());
  284. $username = Minz_Session::param('currentUser', '_');
  285. }
  286. $ok = @touch(DATA_PATH . '/users/' . $username . '/log.txt');
  287. //if (!$ok) {
  288. //TODO: Display notification error on front-end
  289. //}
  290. return $ok;
  291. }
  292. function listUsers() {
  293. $final_list = array();
  294. $base_path = join_path(DATA_PATH, 'users');
  295. $dir_list = array_values(array_diff(
  296. scandir($base_path),
  297. array('..', '.', '_')
  298. ));
  299. foreach ($dir_list as $file) {
  300. if ($file[0] !== '.' && is_dir(join_path($base_path, $file)) && file_exists(join_path($base_path, $file, 'config.php'))) {
  301. $final_list[] = $file;
  302. }
  303. }
  304. return $final_list;
  305. }
  306. /**
  307. * Return if the maximum number of registrations has been reached.
  308. *
  309. * Note a max_regstrations of 0 means there is no limit.
  310. *
  311. * @return true if number of users >= max registrations, false else.
  312. */
  313. function max_registrations_reached() {
  314. $system_conf = Minz_Configuration::get('system');
  315. $limit_registrations = $system_conf->limits['max_registrations'];
  316. $number_accounts = count(listUsers());
  317. return $limit_registrations > 0 && $number_accounts >= $limit_registrations;
  318. }
  319. /**
  320. * Register and return the configuration for a given user.
  321. *
  322. * Note this function has been created to generate temporary configuration
  323. * objects. If you need a long-time configuration, please don't use this function.
  324. *
  325. * @param $username the name of the user of which we want the configuration.
  326. * @return a Minz_Configuration object, null if the configuration cannot be loaded.
  327. */
  328. function get_user_configuration($username) {
  329. if (!FreshRSS_user_Controller::checkUsername($username)) {
  330. return null;
  331. }
  332. $namespace = 'user_' . $username;
  333. try {
  334. Minz_Configuration::register($namespace,
  335. join_path(USERS_PATH, $username, 'config.php'),
  336. join_path(FRESHRSS_PATH, 'config-user.default.php'));
  337. } catch (Minz_ConfigurationNamespaceException $e) {
  338. // namespace already exists, do nothing.
  339. Minz_Log::warning($e->getMessage(), USERS_PATH . '/_/log.txt');
  340. } catch (Minz_FileNotExistException $e) {
  341. Minz_Log::warning($e->getMessage(), USERS_PATH . '/_/log.txt');
  342. return null;
  343. }
  344. return Minz_Configuration::get($namespace);
  345. }
  346. function httpAuthUser() {
  347. if (!empty($_SERVER['REMOTE_USER'])) {
  348. return $_SERVER['REMOTE_USER'];
  349. } elseif (!empty($_SERVER['REDIRECT_REMOTE_USER'])) {
  350. return $_SERVER['REDIRECT_REMOTE_USER'];
  351. } elseif (!empty($_SERVER['HTTP_X_WEBAUTH_USER'])) {
  352. return $_SERVER['HTTP_X_WEBAUTH_USER'];
  353. }
  354. return '';
  355. }
  356. function cryptAvailable() {
  357. try {
  358. $hash = '$2y$04$usesomesillystringfore7hnbRJHxXVLeakoG8K30oukPsA.ztMG';
  359. return $hash === @crypt('password', $hash);
  360. } catch (Exception $e) {
  361. Minz_Log::warning($e->getMessage());
  362. }
  363. return false;
  364. }
  365. function is_referer_from_same_domain() {
  366. if (empty($_SERVER['HTTP_REFERER'])) {
  367. return true; //Accept empty referer while waiting for good support of meta referrer same-origin policy in browsers
  368. }
  369. $host = parse_url(((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? 'https://' : 'http://') .
  370. (empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST']));
  371. $referer = parse_url($_SERVER['HTTP_REFERER']);
  372. if (empty($host['host']) || empty($referer['host']) || $host['host'] !== $referer['host']) {
  373. return false;
  374. }
  375. //TODO: check 'scheme', taking into account the case of a proxy
  376. if ((isset($host['port']) ? $host['port'] : 0) !== (isset($referer['port']) ? $referer['port'] : 0)) {
  377. return false;
  378. }
  379. return true;
  380. }
  381. /**
  382. * Check PHP and its extensions are well-installed.
  383. *
  384. * @return array of tested values.
  385. */
  386. function check_install_php() {
  387. $pdo_mysql = extension_loaded('pdo_mysql');
  388. $pdo_pgsql = extension_loaded('pdo_pgsql');
  389. $pdo_sqlite = extension_loaded('pdo_sqlite');
  390. return array(
  391. 'php' => version_compare(PHP_VERSION, '5.5.0') >= 0,
  392. 'minz' => file_exists(LIB_PATH . '/Minz'),
  393. 'curl' => extension_loaded('curl'),
  394. 'pdo' => $pdo_mysql || $pdo_sqlite || $pdo_pgsql,
  395. 'pcre' => extension_loaded('pcre'),
  396. 'ctype' => extension_loaded('ctype'),
  397. 'fileinfo' => extension_loaded('fileinfo'),
  398. 'dom' => class_exists('DOMDocument'),
  399. 'json' => extension_loaded('json'),
  400. 'mbstring' => extension_loaded('mbstring'),
  401. 'zip' => extension_loaded('zip'),
  402. );
  403. }
  404. /**
  405. * Check different data files and directories exist.
  406. *
  407. * @return array of tested values.
  408. */
  409. function check_install_files() {
  410. return array(
  411. 'data' => DATA_PATH && is_writable(DATA_PATH),
  412. 'cache' => CACHE_PATH && is_writable(CACHE_PATH),
  413. 'users' => USERS_PATH && is_writable(USERS_PATH),
  414. 'favicons' => is_writable(DATA_PATH . '/favicons'),
  415. 'tokens' => is_writable(DATA_PATH . '/tokens'),
  416. );
  417. }
  418. /**
  419. * Check database is well-installed.
  420. *
  421. * @return array of tested values.
  422. */
  423. function check_install_database() {
  424. $status = array(
  425. 'connection' => true,
  426. 'tables' => false,
  427. 'categories' => false,
  428. 'feeds' => false,
  429. 'entries' => false,
  430. 'entrytmp' => false,
  431. 'tag' => false,
  432. 'entrytag' => false,
  433. );
  434. try {
  435. $dbDAO = FreshRSS_Factory::createDatabaseDAO();
  436. $status['tables'] = $dbDAO->tablesAreCorrect();
  437. $status['categories'] = $dbDAO->categoryIsCorrect();
  438. $status['feeds'] = $dbDAO->feedIsCorrect();
  439. $status['entries'] = $dbDAO->entryIsCorrect();
  440. $status['entrytmp'] = $dbDAO->entrytmpIsCorrect();
  441. $status['tag'] = $dbDAO->tagIsCorrect();
  442. $status['entrytag'] = $dbDAO->entrytagIsCorrect();
  443. } catch(Minz_PDOConnectionException $e) {
  444. $status['connection'] = false;
  445. }
  446. return $status;
  447. }
  448. /**
  449. * Remove a directory recursively.
  450. *
  451. * From http://php.net/rmdir#110489
  452. *
  453. * @param $dir the directory to remove
  454. */
  455. function recursive_unlink($dir) {
  456. if (!is_dir($dir)) {
  457. return true;
  458. }
  459. $files = array_diff(scandir($dir), array('.', '..'));
  460. foreach ($files as $filename) {
  461. $filename = $dir . '/' . $filename;
  462. if (is_dir($filename)) {
  463. @chmod($filename, 0777);
  464. recursive_unlink($filename);
  465. } else {
  466. unlink($filename);
  467. }
  468. }
  469. return rmdir($dir);
  470. }
  471. /**
  472. * Remove queries where $get is appearing.
  473. * @param $get the get attribute which should be removed.
  474. * @param $queries an array of queries.
  475. * @return the same array whithout those where $get is appearing.
  476. */
  477. function remove_query_by_get($get, $queries) {
  478. $final_queries = array();
  479. foreach ($queries as $key => $query) {
  480. if (empty($query['get']) || $query['get'] !== $get) {
  481. $final_queries[$key] = $query;
  482. }
  483. }
  484. return $final_queries;
  485. }
  486. //RFC 4648
  487. function base64url_encode($data) {
  488. return strtr(rtrim(base64_encode($data), '='), '+/', '-_');
  489. }
  490. //RFC 4648
  491. function base64url_decode($data) {
  492. return base64_decode(strtr($data, '-_', '+/'));
  493. }
  494. function _i($icon, $url_only = false) {
  495. return FreshRSS_Themes::icon($icon, $url_only);
  496. }
  497. const SHORTCUT_KEYS = [
  498. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  499. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  500. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  501. 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12',
  502. 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'Backspace', 'Delete',
  503. 'End', 'Enter', 'Escape', 'Home', 'Insert', 'PageDown', 'PageUp', 'Space', 'Tab',
  504. ];
  505. function validateShortcutList($shortcuts) {
  506. $legacy = array(
  507. 'down' => 'ArrowDown', 'left' => 'ArrowLeft', 'page_down' => 'PageDown', 'page_up' => 'PageUp',
  508. 'right' => 'ArrowRight', 'up' => 'ArrowUp',
  509. );
  510. $upper = null;
  511. $shortcuts_ok = array();
  512. foreach ($shortcuts as $key => $value) {
  513. if (in_array($value, SHORTCUT_KEYS)) {
  514. $shortcuts_ok[$key] = $value;
  515. } elseif (isset($legacy[$value])) {
  516. $shortcuts_ok[$key] = $legacy[$value];
  517. } else { //Case-insensitive search
  518. if ($upper === null) {
  519. $upper = array_map('strtoupper', SHORTCUT_KEYS);
  520. }
  521. $i = array_search(strtoupper($value), $upper);
  522. if ($i !== false) {
  523. $shortcuts_ok[$key] = SHORTCUT_KEYS[$i];
  524. }
  525. }
  526. }
  527. return $shortcuts_ok;
  528. }