index.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. // == | Setup | =======================================================================================================
  3. // Enable Error Reporting
  4. error_reporting(E_ALL);
  5. ini_set("display_errors", "on");
  6. // This is the absolute webroot path
  7. // It does NOT have a trailing slash
  8. define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
  9. // Debug flag
  10. define('DEBUG_MODE', $_GET['debug'] ?? null);
  11. // Define basic constants for the software
  12. const SOFTWARE_NAME = 'Phobos';
  13. const SOFTWARE_VERSION = '0.9.0';
  14. const BASE_RELPATH = '/base/';
  15. const SKIN_RELPATH = '/skin/';
  16. // Include fundamental constants and global functions
  17. require_once(ROOT_PATH . BASE_RELPATH . 'binocUtils.php');
  18. // Include application-specific global constants and functions
  19. require_once(ROOT_PATH . BASE_RELPATH . 'appUtils.php');
  20. // ====================================================================================================================
  21. // == | Main | ========================================================================================================
  22. // Define an array that will hold the current application state
  23. $gaRuntime = array(
  24. 'currentPath' => null,
  25. 'currentDomain' => null,
  26. 'currentSubDomain' => null,
  27. 'currentScheme' => gfSuperVar('server', 'SCHEME') ?? (gfSuperVar('server', 'HTTPS') ? 'https' : 'http'),
  28. 'currentSkin' => 'default',
  29. 'debugMode' => DEBUG_MODE,
  30. 'offlineMode' => file_exists(ROOT_PATH . '/.offline') && !gfSuperVar('get', 'overrideOffline'),
  31. 'remoteAddr' => gfSuperVar('server', 'HTTP_X_FORWARDED_FOR') ?? gfSuperVar('server', 'REMOTE_ADDR'),
  32. 'userAgent' => gfSuperVar('server', 'HTTP_USER_AGENT'),
  33. 'phpServerName' => gfSuperVar('server', 'SERVER_NAME'),
  34. 'phpRequestURI' => gfSuperVar('server', 'REQUEST_URI'),
  35. 'qComponent' => gfSuperVar('get', 'component'),
  36. 'qPath' => gfSuperVar('get', 'path'),
  37. );
  38. // --------------------------------------------------------------------------------------------------------------------
  39. // Root (/) won't set a component or path
  40. if (!$gaRuntime['qComponent'] && !$gaRuntime['qPath']) {
  41. $gaRuntime['qComponent'] = 'site';
  42. $gaRuntime['qPath'] = SLASH;
  43. }
  44. // --------------------------------------------------------------------------------------------------------------------
  45. // Set the current domain and subdomain
  46. $gaRuntime['currentDomain'] = gfSuperVar('check', gfGetDomain($gaRuntime['phpServerName']));
  47. $gaRuntime['currentSubDomain'] = gfSuperVar('check', gfGetDomain($gaRuntime['phpServerName'], true));
  48. // --------------------------------------------------------------------------------------------------------------------
  49. // If we have a path we want to explode it into an array and count it
  50. if ($gaRuntime['qPath']) {
  51. // Explode the path if it exists
  52. $gaRuntime['currentPath'] = gfExplodePath($gaRuntime['qPath']);
  53. // Get a count of the exploded path
  54. $gaRuntime['pathCount'] = count($gaRuntime['currentPath']);
  55. }
  56. // --------------------------------------------------------------------------------------------------------------------
  57. // This is application-specific code that needs to run before a component is loaded but AFTER $gaRuntime
  58. if (defined("APP_UTILS")) {
  59. // Merge our application-specific runtime state with the generic
  60. $gaRuntime = array_merge($gaRuntime, array(
  61. 'qApplication' => gfSuperVar('get', 'appOverride'),
  62. 'currentApplication' => null,
  63. 'orginalApplication' => null,
  64. 'unifiedMode' => null,
  65. 'unifiedApps' => null,
  66. 'validClient' => null,
  67. 'validVersion' => null,
  68. 'debugMode' => (gfSuperVar('server', 'SERVER_NAME') == DEVELOPER_DOMAIN) ?
  69. !DEBUG_MODE : gfSuperVar('get', 'debugOverride'),
  70. ));
  71. // Decide which application by domain that the software will be serving
  72. $gaRuntime['currentApplication'] = APPLICATION_DOMAINS[$gaRuntime['currentDomain']] ?? null;
  73. if (!$gaRuntime['currentApplication']) {
  74. if ($gaRuntime['debugMode']) {
  75. gfError('Invalid domain/application');
  76. }
  77. // We want to be able to give blank responses to any invalid domain/application
  78. // when not in debug mode
  79. $gaRuntime['offlineMode'] = true;
  80. }
  81. // See if this is a unified add-ons site
  82. if (is_array($gaRuntime['currentApplication'])) {
  83. $gaRuntime['unifiedMode'] = true;
  84. $gaRuntime['unifiedApps'] = $gaRuntime['currentApplication'];
  85. $gaRuntime['currentApplication'] = true;
  86. }
  87. // ------------------------------------------------------------------------------------------------------------------
  88. // Site Offline
  89. if ($gaRuntime['offlineMode']) {
  90. $gvOfflineMessage = 'This site is currently unavailable. Please try again later.';
  91. // Development offline message
  92. if (str_contains(SOFTWARE_VERSION, 'a') || str_contains(SOFTWARE_VERSION, 'b') ||
  93. str_contains(SOFTWARE_VERSION, 'pre') || $gaRuntime['debugMode']) {
  94. $gvOfflineMessage = 'This in-development version of'. SPACE . SOFTWARE_NAME . SPACE . 'is not for public consumption.';
  95. }
  96. switch ($gaRuntime['qComponent']) {
  97. case 'aus':
  98. gfOutput(XML_TAG . RDF_AUS_BLANK, 'xml');
  99. break;
  100. case 'integration':
  101. $gaRuntime['qAPIScope'] = gfSuperVar('get', 'type');
  102. $gaRuntime['qAPIFunction'] = gfSuperVar('get', 'request');
  103. if ($gaRuntime['qAPIScope'] != 'internal') {
  104. gfHeader(404);
  105. }
  106. switch ($gaRuntime['qAPIFunction']) {
  107. case 'search':
  108. gfOutput(XML_TAG . XML_API_SEARCH_BLANK, 'xml');
  109. break;
  110. case 'get':
  111. case 'recommended':
  112. gfOutput(XML_TAG . XML_API_LIST_BLANK, 'xml');
  113. break;
  114. default:
  115. gfHeader(404);
  116. }
  117. break;
  118. case 'discover':
  119. gfHeader(404);
  120. default:
  121. gfError($gvOfflineMessage);
  122. }
  123. }
  124. // ------------------------------------------------------------------------------------------------------------------
  125. // Items that get changed depending on debug mode
  126. if ($gaRuntime['debugMode']) {
  127. // In debug mode we need to test other applications
  128. if ($gaRuntime['qApplication']) {
  129. // We can't test an application that doesn't exist
  130. if (!array_key_exists($gaRuntime['qApplication'], TARGET_APPLICATION)) {
  131. gfError('Invalid override application');
  132. }
  133. // Stupidity check
  134. if ($gaRuntime['qApplication'] == $gaRuntime['currentApplication']) {
  135. gfError('It makes no sense to override to the same application');
  136. }
  137. // Set the application
  138. $gaRuntime['orginalApplication'] = $gaRuntime['currentApplication'];
  139. $gaRuntime['currentApplication'] = $gaRuntime['qApplication'];
  140. // If this is a unified add-ons site then we need to try and figure out the domain
  141. if (in_array('unified', TARGET_APPLICATION[$gaRuntime['currentApplication']]['features'])) {
  142. // Switch unified mode on
  143. $gaRuntime['unifiedMode'] = true;
  144. // Loop through the domains
  145. foreach (APPLICATION_DOMAINS as $_key => $_value) {
  146. // Skip any value that isn't an array
  147. if (!is_array($_value)) {
  148. continue;
  149. }
  150. // If we hit a domain with the requested application then set unifiedApps
  151. if (in_array($gaRuntime['currentApplication'], $_value)) {
  152. $gaRuntime['unifiedApps'] = $_value;
  153. $gaRuntime['currentApplication'] = true;
  154. break;
  155. }
  156. }
  157. // Final check to make sure we have a unified domain figured out
  158. if (!$gaRuntime['unifiedApps']) {
  159. gfError('Unable to switch to unified mode');
  160. }
  161. }
  162. }
  163. }
  164. // ------------------------------------------------------------------------------------------------------------------
  165. // We need nsIVersionComparator from this point on
  166. gfImportModules('static:vc');
  167. // Set valid client
  168. $gaRuntime['validClient'] = gfValidClientVersion();
  169. $gaRuntime['validVersion'] = gfValidClientVersion(true);
  170. // Determine if we should redirect Pale Moon clients back to addons-legacy
  171. $gaRuntime['phoebusRedirect'] = ($gaRuntime['currentApplication'] == 'palemoon' &&
  172. $gaRuntime['validClient'] && !$gaRuntime['validVersion']);
  173. if ($gaRuntime['phoebusRedirect']) {
  174. switch ($gaRuntime['qComponent']) {
  175. case 'aus':
  176. case 'integration':
  177. case 'discover':
  178. gfRedirect('https://addons-legacy.palemoon.org/?' . gfSuperVar('server', 'QUERY_STRING'));
  179. break;
  180. case 'site':
  181. case 'panel':
  182. gfRedirect('https://addons-legacy.palemoon.org' . $gaRuntime['qPath']);
  183. break;
  184. default:
  185. gfErrorOr404('Invalid legacy request.');
  186. }
  187. }
  188. // ------------------------------------------------------------------------------------------------------------------
  189. // If we have a path then explode it and check for component pretty-paths
  190. if ($gaRuntime['currentPath']) {
  191. // These paths override the site component
  192. switch ($gaRuntime['currentPath'][0]) {
  193. case 'special':
  194. case 'panel':
  195. $gaRuntime['qComponent'] = $gaRuntime['currentPath'][0];
  196. break;
  197. }
  198. }
  199. }
  200. // --------------------------------------------------------------------------------------------------------------------
  201. if (!defined("COMPONENTS")) {
  202. define("COMPONENTS", ['site' => ROOT_PATH . BASE_RELPATH . 'site.php',
  203. 'special' => ROOT_PATH . BASE_RELPATH . 'special.php']);
  204. if (($gaRuntime['currentPath'][0] ?? null) == 'special') {
  205. $gaRuntime['qComponent'] = 'special';
  206. }
  207. }
  208. // Load component based on qComponent
  209. if ($gaRuntime['qComponent'] && array_key_exists($gaRuntime['qComponent'], COMPONENTS)) {
  210. $gvComponentFile = COMPONENTS[$gaRuntime['qComponent']];
  211. if (!file_exists($gvComponentFile)) {
  212. if ($gaRuntime['qComponent'] == 'site') {
  213. gfError('Could not load site component.');
  214. }
  215. else {
  216. gfErrorOr404('Cannot load the' . SPACE . $gaRuntime['qComponent'] . SPACE . 'component.');
  217. }
  218. }
  219. require_once($gvComponentFile);
  220. }
  221. else {
  222. gfErrorOr404('Invalid component.');
  223. }
  224. // ====================================================================================================================
  225. ?>