configure.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. ];
  57. if ($sClientUrl !== '') {
  58. $aOptions['sys_api_url_root_email'] = $sClientUrl . '/';
  59. $aOptions['sys_api_url_root_push'] = $sClientUrl . '/';
  60. }
  61. foreach ($aOptions as $sName => $sValue)
  62. setOption($oDb, $sName, $sValue);
  63. // Same "tmp fix for search via API" as the Jenkinsfile.
  64. $oDb->query("UPDATE `sys_objects_search` SET `GlobalSearch` = '0' WHERE `ObjectName` IN ('sys_pages', 'bx_timeline')");
  65. if (($sKey = trim((string)getenv('UNA_API_KEY'))) !== '') {
  66. $oStmt = $oDb->prepare("INSERT INTO `sys_api_keys` (`title`, `key`, `order`) VALUES ('NEO', ?, 1) ON DUPLICATE KEY UPDATE `title` = `title`");
  67. $oStmt->bind_param('s', $sKey);
  68. $oStmt->execute();
  69. }
  70. $aOrigins = listFromEnv('UNA_API_ORIGINS');
  71. if ($sClientUrl !== '')
  72. $aOrigins[] = $sClientUrl;
  73. foreach (array_unique($aOrigins) as $iOrder => $sOrigin) {
  74. $oStmt = $oDb->prepare('SELECT 1 FROM `sys_api_origins` WHERE `url` = ?');
  75. $oStmt->bind_param('s', $sOrigin);
  76. $oStmt->execute();
  77. if ($oStmt->get_result()->num_rows)
  78. continue;
  79. $oStmt = $oDb->prepare('INSERT INTO `sys_api_origins` (`url`, `order`) VALUES (?, ?)');
  80. $iPos = $iOrder + 1;
  81. $oStmt->bind_param('si', $sOrigin, $iPos);
  82. $oStmt->execute();
  83. }
  84. echo "API configured" . ($sClientUrl !== '' ? " for $sClientUrl" : '') . "\n";