4
0

misc.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. $extensionName = is_string($_GET['ext'] ?? null) ? $_GET['ext'] : '';
  10. if ($extensionName === '') {
  11. $pathInfo = '';
  12. if (empty($_SERVER['PATH_INFO']) || !is_string($_SERVER['PATH_INFO'] ?? null)) {
  13. if (!empty($_SERVER['ORIG_PATH_INFO']) && is_string($_SERVER['ORIG_PATH_INFO'])) {
  14. // Compatibility https://php.net/reserved.variables.server
  15. $pathInfo = $_SERVER['ORIG_PATH_INFO'];
  16. }
  17. } else {
  18. $pathInfo = $_SERVER['PATH_INFO'];
  19. }
  20. $pathInfo = rawurldecode($pathInfo);
  21. $pathInfo = preg_replace('%^(/api)?(/misc\.php)?%', '', $pathInfo); //Discard common errors
  22. if ($pathInfo !== '' && is_string($pathInfo)) {
  23. $pathInfos = explode('/', $pathInfo, limit: 3);
  24. if (count($pathInfos) > 1) {
  25. $extensionName = $pathInfos[1];
  26. }
  27. }
  28. }
  29. if ($extensionName === '') {
  30. header('HTTP/1.1 400 Bad Request');
  31. header('Content-Type: text/plain; charset=UTF-8');
  32. die('Bad Request!');
  33. }
  34. Minz_Session::init('FreshRSS', volatile: true);
  35. FreshRSS_Context::initSystem();
  36. if (!FreshRSS_Context::hasSystemConf()) {
  37. header('HTTP/1.1 500 Internal Server Error');
  38. header('Content-Type: text/plain; charset=UTF-8');
  39. die('Internal Server Error!');
  40. }
  41. if (!FreshRSS_Context::systemConf()->api_enabled) {
  42. header('HTTP/1.1 503 Service Unavailable');
  43. header('Content-Type: text/plain; charset=UTF-8');
  44. die('Service Unavailable!');
  45. }
  46. if (empty(FreshRSS_Context::systemConf()->extensions_enabled[$extensionName])) {
  47. header('HTTP/1.1 404 Not Found');
  48. header('Content-Type: text/plain; charset=UTF-8');
  49. die('Not Found!');
  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(Minz_HookType::ApiMisc)) {
  56. header('HTTP/1.1 501 Not Implemented');
  57. header('Content-Type: text/plain; charset=UTF-8');
  58. die('Not Implemented!');
  59. }