Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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.

DBFactory.php 1.9KB

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