4
0

misc.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * API entry point for FreshRSS extensions on
  5. * `/api/misc.php/Extension%20name/` or `/api/misc.php?ext=Extension%20name`
  6. */
  7. require dirname(__DIR__, 2) . '/constants.php';
  8. require LIB_PATH . '/lib_rss.php'; //Includes class autoloader
  9. function badRequest(): never {
  10. header('HTTP/1.1 400 Bad Request');
  11. header('Content-Type: text/plain; charset=UTF-8');
  12. die('Bad Request!');
  13. }
  14. function serviceUnavailable(): never {
  15. header('HTTP/1.1 503 Service Unavailable');
  16. header('Content-Type: text/plain; charset=UTF-8');
  17. die('Service Unavailable!');
  18. }
  19. $extensionName = is_string($_GET['ext'] ?? null) ? $_GET['ext'] : '';
  20. if ($extensionName === '') {
  21. $pathInfo = '';
  22. if (empty($_SERVER['PATH_INFO']) || !is_string($_SERVER['PATH_INFO'] ?? null)) {
  23. if (!empty($_SERVER['ORIG_PATH_INFO']) && is_string($_SERVER['ORIG_PATH_INFO'])) {
  24. // Compatibility https://php.net/reserved.variables.server
  25. $pathInfo = $_SERVER['ORIG_PATH_INFO'];
  26. }
  27. } else {
  28. $pathInfo = $_SERVER['PATH_INFO'];
  29. }
  30. $pathInfo = rawurldecode($pathInfo);
  31. $pathInfo = preg_replace('%^(/api)?(/misc\.php)?%', '', $pathInfo); //Discard common errors
  32. if ($pathInfo !== '' && is_string($pathInfo)) {
  33. $pathInfos = explode('/', $pathInfo, limit: 3);
  34. if (count($pathInfos) > 1) {
  35. $extensionName = $pathInfos[1];
  36. }
  37. }
  38. }
  39. if ($extensionName === '') {
  40. badRequest();
  41. }
  42. Minz_Session::init('FreshRSS', volatile: true);
  43. FreshRSS_Context::initSystem();
  44. if (
  45. !FreshRSS_Context::hasSystemConf() ||
  46. !FreshRSS_Context::systemConf()->api_enabled ||
  47. empty(FreshRSS_Context::systemConf()->extensions_enabled[$extensionName])
  48. ) {
  49. serviceUnavailable();
  50. }
  51. // Only enable the extension that is being called
  52. FreshRSS_Context::systemConf()->extensions_enabled = [$extensionName => true];
  53. Minz_ExtensionManager::init();
  54. Minz_Translate::init();
  55. if (!Minz_ExtensionManager::callHookUnique('api_misc')) {
  56. serviceUnavailable();
  57. }