/modules/radioactivity/radioactivity-bootstrap.inc

https://bitbucket.org/hfccwebdev/sites_mirrornews · Pascal · 127 lines · 53 code · 13 blank · 61 comment · 5 complexity · 5972586aa1a83f47952b31a581ebe0b1 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is used both in drupal and in callback files.
  4. */
  5. // prepare variables
  6. if (!defined("DRUPAL_ROOT")) {
  7. // We're not in drupal but we need 'some' functionality so...
  8. _radioactivity_light_initialization();
  9. }
  10. else {
  11. // We're in drupal, setup necessary vars
  12. define("VAR_RADIOACTIVITY_CHECKSUM_SALT", variable_get("radioactivity_checksum_salt", "undefined"));
  13. define("VAR_RADIOACTIVITY_TEMP_DIR", variable_get('radioactivity_temp_dir', sys_get_temp_dir()));
  14. if (class_exists('Memcache') || class_exists("Memcached")) {
  15. define("VAR_RADIOACTIVITY_MEMCACHED_HOST", variable_get("radioactivity_memcached_host", "localhost"));
  16. define("VAR_RADIOACTIVITY_MEMCACHED_PORT", variable_get("radioactivity_memcached_port", "11211"));
  17. }
  18. }
  19. /**
  20. * Get the config file path of R
  21. */
  22. function _radioactivity_get_config_file_path() {
  23. $dir = dirname(__FILE__);
  24. $config_file = $dir . "/radioactivity-bootstrap.cfg.inc";
  25. return $config_file;
  26. }
  27. /**
  28. * Do a light system initialization
  29. */
  30. function _radioactivity_light_initialization() {
  31. if (!defined("RADIOACTIVITY_BOOTSTRAPPED")) {
  32. $config_file = _radioactivity_get_config_file_path();
  33. if (file_exists($config_file)) {
  34. include ($config_file);
  35. }
  36. else {
  37. _radioactivity_require_bootstrapping();
  38. // Grab the checksum variable
  39. $var = variable_get('radioactivity_checksum_salt', 'undefined');
  40. define("VAR_RADIOACTIVITY_CHECKSUM_SALT", $var);
  41. define("VAR_RADIOACTIVITY_TEMP_DIR", variable_get('radioactivity_temp_dir', sys_get_temp_dir()));
  42. variable_set("radioactivity_config_warning", TRUE);
  43. if (class_exists('Memcache') || class_exists("Memcached")) {
  44. define("VAR_RADIOACTIVITY_MEMCACHED_HOST", variable_get("radioactivity_memcached_host", "localhost"));
  45. define("VAR_RADIOACTIVITY_MEMCACHED_PORT", variable_get("radioactivity_memcached_port", "11211"));
  46. }
  47. }
  48. define("RADIOACTIVITY_BOOTSTRAPPED", TRUE);
  49. }
  50. }
  51. /**
  52. * Do a light boostrap
  53. */
  54. function _radioactivity_require_bootstrapping() {
  55. // use the VERSION to figure out if we've done bootstrapping already
  56. if (!defined("VERSION")) {
  57. include "radioactivity.module";
  58. if (!defined("VAR_RADIOACTIVITY_DRUPAL_ROOT")) {
  59. define("VAR_RADIOACTIVITY_DRUPAL_ROOT", $_SERVER['DOCUMENT_ROOT']);
  60. }
  61. define('DRUPAL_ROOT', VAR_RADIOACTIVITY_DRUPAL_ROOT);
  62. chdir(DRUPAL_ROOT);
  63. if (!file_exists('./includes/bootstrap.inc')) {
  64. die("Unable to figure out bootstrapping directory!");
  65. }
  66. require_once './includes/bootstrap.inc';
  67. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  68. variable_set("radioactivity_bootstrap_warning", TRUE);
  69. }
  70. }
  71. /**
  72. * Internal function to filter nested arrays().
  73. * @param type $array
  74. */
  75. function _radioactivity_filter_nested_array($array) {
  76. return !is_array($array) ? TRUE : FALSE;
  77. }
  78. /**
  79. * Generate a checksum for given data
  80. */
  81. function _radioactivity_checksum_generate($data) {
  82. $temp = array_filter($data, "_radioactivity_filter_nested_array");
  83. unset($temp['checksum']);
  84. ksort($temp);
  85. $temp = join(',', $temp);
  86. return md5(VAR_RADIOACTIVITY_CHECKSUM_SALT . $temp);
  87. }
  88. /**
  89. * Validate checksum against given data
  90. */
  91. function _radioactivity_checksum_validate($data, $checksum) {
  92. return strcmp(_radioactivity_checksum_generate($data), $checksum) === 0;
  93. }
  94. /**
  95. * Validate (and filter) a payload
  96. */
  97. function _radioactivity_validate_incident($incident) {
  98. return _radioactivity_checksum_validate($incident, $incident['checksum']);
  99. }
  100. /**
  101. * Prepare payload
  102. */
  103. function _radioactivity_prepare_incident($incident) {
  104. ksort($incident);
  105. $incident['checksum'] = _radioactivity_checksum_generate($incident);
  106. return $incident;
  107. }