configure.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * Railway: post-install settings for an API-backed site (NEO client in front).
  7. * Runs from entrypoint.sh on every container start, after install, so it must be
  8. * idempotent. Plain mysqli on purpose: no UNA bootstrap, nothing cached yet.
  9. *
  10. * Environment:
  11. * DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD set by entrypoint.sh
  12. * UNA_API_KEY key the NEO server proxy sends as `Authorization: Bearer`
  13. * UNA_API_ORIGINS comma-separated browser origins allowed to call the API directly
  14. * UNA_CLIENT_URL public client URL (links in emails and push notifications)
  15. *
  16. * Mirrors the "Database Configuration" and "New API Setup" stages of scripts/Jenkinsfile,
  17. * except that unsafe API services (raw SQL through r=q) stay off.
  18. */
  19. $oDb = new mysqli(
  20. getenv('DB_HOST'),
  21. getenv('DB_USER'),
  22. getenv('DB_PASSWORD'),
  23. getenv('DB_NAME'),
  24. (int)(getenv('DB_PORT') ?: 3306)
  25. );
  26. $oDb->set_charset('utf8mb4');
  27. function setOption(mysqli $oDb, string $sName, string $sValue): void
  28. {
  29. $oStmt = $oDb->prepare('UPDATE `sys_options` SET `value` = ? WHERE `name` = ?');
  30. $oStmt->bind_param('ss', $sValue, $sName);
  31. $oStmt->execute();
  32. }
  33. function listFromEnv(string $sName): array
  34. {
  35. return array_values(array_filter(array_map('trim', explode(',', (string)getenv($sName)))));
  36. }
  37. $sClientUrl = rtrim((string)getenv('UNA_CLIENT_URL'), '/');
  38. $aOptions = [
  39. // API: the NEO proxy authenticates with a key; browsers (Expo web, local dev) by origin.
  40. 'sys_api_enable' => 'on',
  41. 'sys_api_access_by_key' => 'on',
  42. 'sys_api_access_by_origin' => 'on',
  43. 'sys_api_access_unsafe_services' => '',
  44. 'sys_session_auth' => 'on',
  45. 'sys_api_cookie_path' => '/',
  46. 'sys_api_cookie_samesite' => 'Lax',
  47. 'sys_api_cookie_secure' => 'on',
  48. // Same API behaviour as api.neo.so.
  49. 'sys_api_search_sections' => 'bx_posts,bx_persons,bx_groups,bx_events,bx_spaces,bx_market,bx_organizations,bx_ads,bx_videos,sys_pages,bx_timeline',
  50. 'sys_api_conn_in_prof_units' => 'on',
  51. 'sys_api_context_connection' => 'subscriptions',
  52. 'sys_api_context_switcher' => 'bx_spaces',
  53. 'sys_api_root_page_guest' => 'splash',
  54. 'sys_api_root_page_member' => 'home',
  55. 'sys_iconset_default' => 'sys_lucide',
  56. // Deploys bring the code. UNA's nightly auto-update (a cron job, run by entrypoint.sh) would
  57. // upgrade files and database to the latest release, and the next deploy would put this
  58. // build's files back over the upgraded database.
  59. 'sys_autoupdate' => '',
  60. ];
  61. if ($sClientUrl !== '') {
  62. $aOptions['sys_api_url_root_email'] = $sClientUrl . '/';
  63. $aOptions['sys_api_url_root_push'] = $sClientUrl . '/';
  64. }
  65. foreach ($aOptions as $sName => $sValue)
  66. setOption($oDb, $sName, $sValue);
  67. // Same "tmp fix for search via API" as the Jenkinsfile.
  68. $oDb->query("UPDATE `sys_objects_search` SET `GlobalSearch` = '0' WHERE `ObjectName` IN ('sys_pages', 'bx_timeline')");
  69. if (($sKey = trim((string)getenv('UNA_API_KEY'))) !== '') {
  70. $oStmt = $oDb->prepare("INSERT INTO `sys_api_keys` (`title`, `key`, `order`) VALUES ('NEO', ?, 1) ON DUPLICATE KEY UPDATE `title` = `title`");
  71. $oStmt->bind_param('s', $sKey);
  72. $oStmt->execute();
  73. }
  74. $aOrigins = listFromEnv('UNA_API_ORIGINS');
  75. if ($sClientUrl !== '')
  76. $aOrigins[] = $sClientUrl;
  77. foreach (array_unique($aOrigins) as $iOrder => $sOrigin) {
  78. $oStmt = $oDb->prepare('SELECT 1 FROM `sys_api_origins` WHERE `url` = ?');
  79. $oStmt->bind_param('s', $sOrigin);
  80. $oStmt->execute();
  81. if ($oStmt->get_result()->num_rows)
  82. continue;
  83. $oStmt = $oDb->prepare('INSERT INTO `sys_api_origins` (`url`, `order`) VALUES (?, ?)');
  84. $iPos = $iOrder + 1;
  85. $oStmt->bind_param('si', $sOrigin, $iPos);
  86. $oStmt->execute();
  87. }
  88. echo "API configured" . ($sClientUrl !== '' ? " for $sClientUrl" : '') . "\n";