Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6KB

  1. <?php
  2. namespace Friendica\Factory;
  3. use Friendica\Core\Config\Cache;
  4. use Friendica\Database;
  5. class DBFactory
  6. {
  7. /**
  8. * Initialize the DBA connection
  9. *
  10. * @param Cache\ConfigCache $configCache The configuration cache
  11. * @param array $server The $_SERVER variables
  12. *
  13. * @throws \Exception if connection went bad
  14. */
  15. public static function init(Cache\ConfigCache $configCache, array $server)
  16. {
  17. if (Database\DBA::connected()) {
  18. return;
  19. }
  20. $db_host = $configCache->get('database', 'hostname');
  21. $db_user = $configCache->get('database', 'username');
  22. $db_pass = $configCache->get('database', 'password');
  23. $db_data = $configCache->get('database', 'database');
  24. $charset = $configCache->get('database', 'charset');
  25. // Use environment variables for mysql if they are set beforehand
  26. if (!empty($server['MYSQL_HOST'])
  27. && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
  28. && $server['MYSQL_PASSWORD'] !== false
  29. && !empty($server['MYSQL_DATABASE']))
  30. {
  31. $db_host = $server['MYSQL_HOST'];
  32. if (!empty($server['MYSQL_PORT'])) {
  33. $db_host .= ':' . $server['MYSQL_PORT'];
  34. }
  35. if (!empty($server['MYSQL_USERNAME'])) {
  36. $db_user = $server['MYSQL_USERNAME'];
  37. } else {
  38. $db_user = $server['MYSQL_USER'];
  39. }
  40. $db_pass = (string) $server['MYSQL_PASSWORD'];
  41. $db_data = $server['MYSQL_DATABASE'];
  42. }
  43. if (Database\DBA::connect($configCache, $db_host, $db_user, $db_pass, $db_data, $charset)) {
  44. // Loads DB_UPDATE_VERSION constant
  45. Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
  46. }
  47. unset($db_host, $db_user, $db_pass, $db_data, $charset);
  48. }
  49. }