You are here

js.install in JS Callback Handler 7.2

Same filename and directory in other branches
  1. 5.2 js.install
  2. 6 js.install
  3. 7 js.install

Ensures JavaScript callback handler has been setup properly.

File

js.install
View source
<?php

/**
 * @file
 * Ensures JavaScript callback handler has been setup properly.
 */

/**
 * Implements hook_requirements().
 */
function js_requirements($phase) {
  $requirements = array();
  $t = get_t();
  switch ($phase) {
    case 'runtime':
      $title = $t('JavaScript callback handler');

      // Warn about missing js.php in root directory.
      if (!file_exists('js.php')) {
        $requirements['js'] = array(
          'title' => $title,
          'description' => $t('In order for JavaScript callbacks to work correctly, please copy the js.php file to the root of your Drupal installation.'),
          'severity' => REQUIREMENT_ERROR,
          'value' => $t('Please copy js.php'),
        );
      }
      elseif (variable_get('js_server_software', 'apache') == 'apache' && strpos(file_get_contents('.htaccess'), 'js.php') === FALSE) {
        $requirements['js'] = array(
          'title' => $title,
          'description' => $t('Please add the <a href="!url">RewriteRules</a> to the .htaccess file.', array(
            '!url' => url('admin/config/system/js'),
          )),
          'severity' => REQUIREMENT_ERROR,
          'value' => $t('Add Apache RewriteRule to .htaccess'),
        );
      }
      else {
        $requirements['js'] = array(
          'title' => $title,
          'description' => $t('The JavaScript callback handler has been installed correctly.', array(
            '%core' => 'js.php',
          )),
          'severity' => REQUIREMENT_OK,
          'value' => $t('Installed correctly'),
        );
      }
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function js_install() {

  // Move js.php to the root of Drupal.
  $js_dir = realpath(dirname(__FILE__));
  $destination = DRUPAL_ROOT . DIRECTORY_SEPARATOR . 'js.php';
  $source = $js_dir . DIRECTORY_SEPARATOR . 'js.php';
  $t = get_t();
  if (!file_exists($destination) && file_exists($source)) {
    if (!copy($source, $destination)) {
      drupal_set_message($t('Can not copied js.php to installation root folder.'), 'error');
    }
    else {
      drupal_set_message($t('Copied js.php to installation root folder successfully.'));
    }
  }

  // Notify the user that they should add the rewrite rules to their .htaccess file.
  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'),
  )));
}

/**
 * Implements hook_uninstall().
 */
function js_uninstall() {

  // Delete js.php to remove any leftovers from the js module.
  $file = DRUPAL_ROOT . DIRECTORY_SEPARATOR . 'js.php';
  if (file_exists($file)) {
    if (!unlink($file)) {
      drupal_set_message(t('Can not delete js.php'), 'warning');
    }
    else {
      drupal_set_message(t('Deleted js.php successfully'));
    }
  }

  // If the .htaccess contains references to js.php, notify the user they should
  // clean it up.
  if (variable_get('js_server_software', 'apache') == 'apache' && strpos(file_get_contents(DRUPAL_ROOT . DIRECTORY_SEPARATOR . '.htaccess'), 'js.php') !== FALSE) {
    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.'));
  }

  // Delete used variables.
  variable_del('js_server_software');
}

Functions

Namesort descending Description
js_install Implements hook_install().
js_requirements Implements hook_requirements().
js_uninstall Implements hook_uninstall().