Commit c78835c7 authored by Jasper Knops's avatar Jasper Knops
Browse files

The hunt for hookers has started!

parent 4732a998
Loading
Loading
Loading
Loading

css/hooker_hunter.css

0 → 100644
+3 −0
Original line number Diff line number Diff line
.form-item-hook, .form-item-module {
  display: inline;
}
+3 −0
Original line number Diff line number Diff line
name = Hooker hunter
description = Hunt the hookers by module or hook
core = 7.x
package = Development

hooker_hunter.module

0 → 100644
+198 −0
Original line number Diff line number Diff line
<?php

define('HOOKER_HUNTER_MENU_ITEM', 'admin/config/development/hooker_hunter');

/**
 * Implements hook_menu().
 */
function hooker_hunter_menu() {
  $items = array();

  $items[HOOKER_HUNTER_MENU_ITEM] = array(
    'title' => 'Hooker hunter',
    'page callback' => 'hooker_hunter_hook_page',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items[HOOKER_HUNTER_MENU_ITEM . '/hook'] = array(
    'title' => 'Hook',
    'page callback' => 'hooker_hunter_hook_page',
    'page arguments' => array(5),
    'access arguments' => array('access content'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  $items[HOOKER_HUNTER_MENU_ITEM . '/module'] = array(
    'title' => 'Module',
    'page callback' => 'hooker_hunter_module_page',
    'page arguments' => array(5),
    'access arguments' => array('access content'),
    'type' => MENU_LOCAL_TASK,
  );

  $items['ajax/' . HOOKER_HUNTER_MENU_ITEM . '/hook/autocomplete'] = array(
    'title' => 'Hooker hook autocomplete',
    'page callback' => 'hooker_hunter_hook_autocomplete',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  $items['ajax/' . HOOKER_HUNTER_MENU_ITEM . '/module/autocomplete'] = array(
    'title' => 'Hooker module autocomplete',
    'page callback' => 'hooker_hunter_module_autocomplete',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function hooker_hunter_hook_autocomplete($string = '') {
  $matches = array();
  if ($string) {
    $implementations = cache_get('module_implements', 'cache_bootstrap');
    foreach (array_keys($implementations->data) as $hook) {
      if (count($implementations->data[$hook]) > 0 && strstr($hook, $string)) {
        $matches[$hook] = check_plain($hook);
      }
    }
  }

  drupal_json_output($matches);
}

function hooker_hunter_module_autocomplete($string = '') {
  $matches = array();
  if ($string) {

    $modules = module_list();
    foreach (array_keys($modules) as $module) {
      if (strstr($module, $string)) {
        $matches[$module] = check_plain($module);
      }
    }
  }

  drupal_json_output($matches);
}

function hooker_hunter_hook_page($hook = NULL) {
  $elements = array();
  $elements[] = drupal_get_form('hooker_hunter_hook_hunt_form', $hook);

  if (isset($hook)) {
    // Get all module data
    $module_data = system_rebuild_module_data();
    $implements = module_implements($hook);
    $header = array('Name', 'Module', 'Weight');
    $rows = array();
    if ($implements) {
      foreach ($implements as $module) {
        $rows[] = array($module_data[$module]->info['name'], l($module, HOOKER_HUNTER_MENU_ITEM .'/module/' . $module), $module_data[$module]->weight);
      }
    }
    else {
      $rows[] = array(array('data' => t('No modules found'), 'colspan' => 3));
    }
    $elements[] = array('#theme' => 'table', '#header' => $header, '#rows' => $rows);

  }

  return $elements;
}

function hooker_hunter_module_page($module = NULL) {
  $elements = array();
  $elements[] = drupal_get_form('hooker_hunter_module_hunt_form', $module);

  if (isset($module) && $module) {
    $header = array('Hooker');

    $rows = array();
    $implementations = cache_get('module_implements', 'cache_bootstrap');
    foreach (array_keys($implementations->data) as $hook) {
      if (count($implementations->data[$hook]) > 0) {
        if (function_exists($module . '_' . $hook)) {
          $rows[] = array(l($hook, HOOKER_HUNTER_MENU_ITEM . '/hook/' . $hook));
        }
      }
    }

    if (count($rows) <= 0) {
      $rows[] = array(t('No hooks found'));
    }

    $elements[] = array('#theme' => 'table', '#header' => $header, '#rows' => $rows);
  }

  return $elements;
}

/**
 * Form to lookup a hook
 */
function hooker_hunter_module_hunt_form($form, &$form_state, $module = NULL) {

  $form['#attached']['css'] = array(
    drupal_get_path('module', 'hooker_hunter') . '/css/hooker_hunter.css',
  );

  $form['module'] = array(
    '#type' => 'textfield',
    '#title' => t('Module'),
    '#required' => TRUE,
    '#default_value' => isset($module) ? $module : '',
    '#autocomplete_path' => 'ajax/' . HOOKER_HUNTER_MENU_ITEM . '/module/autocomplete',
  );

  $form['hunt'] = array(
    '#type' => 'submit',
    '#value' => t('Hunt'),
  );

  return $form;
}

/**
 * Submit handler for the hook lookup form
 */
function hooker_hunter_module_hunt_form_submit($form, &$form_state) {
  $module = $form_state['values']['module'];

  $form_state['redirect'] = HOOKER_HUNTER_MENU_ITEM . '/module/' . $module;
}

/**
 * Form to lookup a hook
 */
function hooker_hunter_hook_hunt_form($form, &$form_state, $hook = NULL) {

  $form['#attached']['css'] = array(
    drupal_get_path('module', 'hooker_hunter') . '/css/hooker_hunter.css',
  );

  $form['hook'] = array(
    '#type' => 'textfield',
    '#title' => t('Hook'),
    '#required' => TRUE,
    '#default_value' => isset($hook) ? $hook : '',
    '#autocomplete_path' => 'ajax/' . HOOKER_HUNTER_MENU_ITEM . '/hook/autocomplete',
  );

  $form['hunt'] = array(
    '#type' => 'submit',
    '#value' => t('Hunt'),
  );

  return $form;
}

/**
 * Submit handler for the hook lookup form
 */
function hooker_hunter_hook_hunt_form_submit($form, &$form_state) {
  $hook = $form_state['values']['hook'];

  $form_state['redirect'] = HOOKER_HUNTER_MENU_ITEM . '/hook/' . $hook;
}