js.install 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @file
  4. * Ensures JavaScript callback handler has been setup properly.
  5. */
  6. /**
  7. * Implements hook_requirements().
  8. */
  9. function js_requirements($phase) {
  10. $requirements = array();
  11. $t = get_t();
  12. switch ($phase) {
  13. case 'runtime':
  14. $title = $t('JavaScript callback handler');
  15. // Warn about missing js.php in root directory.
  16. if (!file_exists('js.php')) {
  17. $requirements['js'] = array(
  18. 'title' => $title,
  19. 'description' => $t('In order for JavaScript callbacks to work correctly, please copy the js.php file to the root of your Drupal installation.'),
  20. 'severity' => REQUIREMENT_ERROR,
  21. 'value' => $t('Please copy js.php'),
  22. );
  23. }
  24. // Warn about missing RewriteRule in .htaccess.
  25. elseif (variable_get('js_server_software', 'apache') == 'apache' && strpos(file_get_contents('.htaccess'), 'js.php') === FALSE) {
  26. $requirements['js'] = array(
  27. 'title' => $title,
  28. 'description' => t('Please add the <a href="!url">RewriteRules</a> to the .htaccess file.', array('!url' => url('admin/config/system/js'))),
  29. 'severity' => REQUIREMENT_ERROR,
  30. 'value' => $t('Add Apache RewriteRule to .htaccess'),
  31. );
  32. }
  33. // Return positive status message.
  34. else {
  35. $requirements['js'] = array(
  36. 'title' => $title,
  37. 'description' => t('The JavaScript callback handler has been installed correctly.',
  38. array('%core' => 'js.php')
  39. ),
  40. 'severity' => REQUIREMENT_OK,
  41. 'value' => $t('Installed correctly'),
  42. );
  43. }
  44. }
  45. return $requirements;
  46. }
  47. /**
  48. * Implements hook_install().
  49. */
  50. function js_install() {
  51. // Move js.php to the root of Drupal.
  52. $js_dir = realpath(dirname(__FILE__));
  53. $destination = DRUPAL_ROOT . DIRECTORY_SEPARATOR . 'js.php';
  54. $source = $js_dir . DIRECTORY_SEPARATOR . 'js.php';
  55. if (!file_exists($destination) && file_exists($source)) {
  56. if (!copy($source, $destination)) {
  57. drupal_set_message(t('Can not copied js.php to installation root folder.'), 'error');
  58. }
  59. else {
  60. drupal_set_message(t('Copied js.php to installation root folder successfully.'));
  61. }
  62. }
  63. // Notify the user that they should add the rewrite rules to their .htaccess
  64. // file.
  65. drupal_set_message(t('To complete the installation: please add the <a href="!url">RewriteRules</a> to the .htaccess file.', array('!url' => url('admin/config/system/js'))));
  66. }
  67. /**
  68. * Implements hook_uninstall().
  69. */
  70. function js_uninstall() {
  71. // Delete js.php to remove any leftovers from the js module.
  72. $file = DRUPAL_ROOT . DIRECTORY_SEPARATOR . 'js.php';
  73. if (file_exists($file)) {
  74. if (!unlink($file)) {
  75. drupal_set_message(t('Can not delete js.php'), 'warning');
  76. }
  77. else {
  78. drupal_set_message(t('Deleted js.php successfully'));
  79. }
  80. }
  81. // If the .htaccess contains references to js.php, notify the user they should
  82. // clean it up.
  83. if (variable_get('js_server_software', 'apache') == 'apache' && strpos(file_get_contents(DRUPAL_ROOT . DIRECTORY_SEPARATOR . '.htaccess'), 'js.php') !== FALSE) {
  84. drupal_set_message(t('The js.php file is still referenced in the .htaccess file. Please make sure to remove the .htaccess handling for js.php for a full uninstall.'));
  85. }
  86. // Delete used variables.
  87. variable_del('js_server_software');
  88. }