prod_check.dbconnect.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @file
  4. * Simple database connection check that can be placed anywhere within a Drupal
  5. * installation. Does NOT need to be in the root where index.php resides!
  6. */
  7. /**
  8. * Locate the actual Drupal root. Based on drush_locate_root().
  9. */
  10. function locate_root() {
  11. $drupal_root = FALSE;
  12. $start_path = isset($_SERVER['PWD']) ? $_SERVER['PWD'] : '';
  13. if (empty($start_path)) {
  14. $start_path = getcwd();
  15. }
  16. foreach (array(TRUE, FALSE) as $follow_symlinks) {
  17. $path = $start_path;
  18. if ($follow_symlinks && is_link($path)) {
  19. $path = realpath($path);
  20. }
  21. // Check the start path.
  22. if (valid_root($path)) {
  23. $drupal_root = $path;
  24. break;
  25. }
  26. else {
  27. // Move up dir by dir and check each.
  28. while ($path = shift_path_up($path)) {
  29. if ($follow_symlinks && is_link($path)) {
  30. $path = realpath($path);
  31. }
  32. if (valid_root($path)) {
  33. $drupal_root = $path;
  34. break 2;
  35. }
  36. }
  37. }
  38. }
  39. return $drupal_root;
  40. }
  41. /**
  42. * Based on the DrupalBoot*::valid_root() from Drush.
  43. */
  44. function valid_root($path) {
  45. if (!empty($path) && is_dir($path) && file_exists($path . '/index.php')) {
  46. $candidate = 'includes/common.inc';
  47. if (file_exists($path . '/' . $candidate) && file_exists($path . '/misc/drupal.js')) {
  48. return TRUE;
  49. }
  50. }
  51. return FALSE;
  52. }
  53. /**
  54. * Based on _drush_shift_path_up().
  55. */
  56. function shift_path_up($path) {
  57. if (empty($path)) {
  58. return FALSE;
  59. }
  60. $path = explode('/', $path);
  61. // Move one directory up.
  62. array_pop($path);
  63. return implode('/', $path);
  64. }
  65. /**
  66. * Do the actual database connection check.
  67. */
  68. define('DRUPAL_ROOT', locate_root());
  69. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  70. drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
  71. $result = db_query('SELECT COUNT(filename) FROM {system}')->fetchField();
  72. if ($result) {
  73. $msg = 'OK';
  74. http_response_code(200);
  75. }
  76. else {
  77. http_response_code(500);
  78. $msg = 'NOK';
  79. }
  80. exit($msg);