[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/drupalforfirebug/ -> drupalforfirebug.module (source)

   1  <?php 
   2  
   3  /**
   4  * Implementation of hook_init() 
   5  */
   6  function drupalforfirebug_exit() {
   7    $dff = TRUE;
   8  
   9    // Try not to break non html pages. [from devel.module]
  10    if (function_exists('drupal_get_headers')) {
  11      $headers = drupal_get_headers();
  12      $formats = array('xml', 'javascript', 'json', 'plain', 'image', 'application', 'csv', 'x-comma-separated-values');
  13      foreach ($formats as $format) {
  14        if (strstr($headers, $format)) {
  15          $dff = FALSE;
  16        }
  17      }
  18    }
  19  
  20    // Check with Devel if Installed
  21    if (module_exists('devel')) {
  22      if (devel_verify_cli()) {
  23        $dff = FALSE;
  24      }
  25    }
  26  
  27  
  28    if ($dff) {
  29      // Load Relevant Files and Register Shutdown
  30      drupal_load('module', 'user');
  31      register_shutdown_function('drupalforfirebug_shutdown');
  32    }
  33  }
  34  
  35  /**
  36  * Implementation of hook_menu()
  37  */
  38  function drupalforfirebug_menu() {
  39    $items['admin/firebug/exec'] = array(
  40      'page callback' => 'drupalforfirebug_get_exec_php_callback',
  41      'access arguments' => array('access content'),
  42      'type' => MENU_CALLBACK,
  43    );
  44    return $items;
  45  }
  46  
  47  /**
  48  *  Implementation of hook_nodeapi() 
  49  */
  50  function drupalforfirebug_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  51    global $dfp_runtime;
  52    if (!user_access('Access Firebug Debug')) {
  53      return;
  54    }
  55    $nid = (isset($node->nid)) ? $node->nid : '*'. t('NEW') . '*';
  56    $data = drupalforfirebug_array_compare((array) $dfp_runtime['drupalforfirebug_nodes']['original'][$node->type][$nid], (array) $node);
  57    $output = drupalforfirebug_field_object('node', $nid, $op, $data);
  58    drupalforfirebug_log($output, 'hook_nodeapi');
  59  }
  60  
  61  /**
  62  * Implementation of hook_views_pre_view();
  63  */
  64  function drupalforfirebug_views_pre_view(&$view, &$display_id) {
  65    global $dfp_runtime;
  66    if (!user_access('Access Firebug Debug')) {
  67      return;
  68    }
  69    $data = drupalforfirebug_array_compare((array) $dfp_runtime['drupalforfirebug_views']['original'][$view->name], (array) $view);
  70    $output = drupalforfirebug_field_object('view', $view->name, NULL, $data);
  71    drupalforfirebug_log($output, 'hook_views');
  72  }
  73  
  74  /**
  75  *  Implementation of hook_form_alter()
  76  */
  77  function drupalforfirebug_form_alter(&$form, $form_state, $form_id) {
  78    global $dfp_runtime;
  79    if (!user_access('Access Firebug Debug')) {
  80      return;
  81    }
  82    if ($form_id != 'drupalforfirebug_execute_form') {
  83      $form_modified = (array) $form;
  84      $data = drupalforfirebug_array_compare($dfp_runtime['drupalforfirebug_forms']['original'][$form_id], $form_modified);
  85      $output = drupalforfirebug_field_object('form', $form_id, NULL, $data);
  86      drupalforfirebug_log($output, 'hook_form_alter');
  87    }
  88  }
  89  
  90  /**
  91  * Implementation of hook_user()
  92  */
  93  function drupalforfirebug_user($op, &$edit, &$account, $category = NULL) {
  94    global $dfp_runtime;
  95    if (!user_access('Access Firebug Debug')) {
  96      return;
  97    }
  98    if (isset($account->uid)) { 
  99      $uid = $account->uid;
 100      $name = $account->name;
 101    } else {
 102      $uid = '*' . t('NEW') . '*';
 103      $name = '*' . t('NEW') . '*';
 104    }
 105    if (is_object($account)) {
 106      $account_clone = drupal_clone($account);
 107      $account_clone->pass = '**' . t('Not shown for security reasons') . '**';
 108      $data = drupalforfirebug_array_compare((array) $account_clone, (array) $account_clone);
 109      $output = drupalforfirebug_field_object('user', $uid, $op, $data);
 110      drupalforfirebug_log($output, 'hook_user');
 111    }
 112  }
 113  
 114  /**
 115  * API Function to Record a Message to the Drupal Firebug Log
 116  */
 117  function drupalforfirebug_log($message, $type = 'general') {
 118    global $dfp_runtime;
 119    $dfp_runtime['firebug_messages'][$type][] = $message; 
 120  }
 121  
 122  /**
 123  * Command Function to Record a Data Element to the Drupal Firebug Log
 124  */
 125  function firep($element, $title = NULL) {
 126    if ($title) {
 127      drupalforfirebug_log('<strong>'.$title.':</strong>');
 128    }
 129    drupalforfirebug_log('<PRE>'. print_r($element, true) . '<br><br></PRE>', 'general');
 130  }
 131  
 132  /**
 133  * Output Function to Return the Results of the Log
 134  */
 135  function drupalforfirebug_get($panetype) {
 136    global $dfp_runtime;
 137    $output = '';
 138    if (isset($dfp_runtime['firebug_messages'][$panetype])) {
 139      foreach($dfp_runtime['firebug_messages'][$panetype] as $message) {
 140        $output .= '<div>'. $message .'</div>';
 141      }
 142      unset($dfp_runtime['firebug_messages'][$panetype]);
 143      return $output;
 144    }
 145  }
 146  
 147  /**
 148  * Output Function to Return the Results of the SQL Log
 149  */
 150  function drupalforfirebug_get_sql_log() {
 151    $output = '<fieldset>';
 152    if (!module_exists('devel')) {
 153      $output .= '<legend>' . t('Devel Module is Not Installed') . '</legend>';
 154      $output .= t('Please install and enable the Devel Module to display the SQL queries.');
 155    } elseif (!variable_get('dev_query', 0)) {
 156      $output .= '<legend>' . t('Query Logging is Not Enabled') . '</legend>';
 157      $output .= t('Please enable "Collect query info" in the Devel Module Settings (admin/settings/devel) to use this feature.');
 158    } else {
 159      global $queries;
 160      list($counts, $query_summary) = devel_query_summary();
 161      $output .= '<legend>' . t('SQL Query Log') . '</legend>';
 162      $output .= $query_summary;
 163      $output .= drupalforfirebug_devel_query_table($queries, $counts);
 164    }
 165    $output .= '</fieldset>'; 
 166    return $output;
 167  }
 168  
 169  /** 
 170  * Generates an Execute PHP Drupal For Firebug Form
 171  **/
 172  function drupalforfirebug_execute_form() {
 173    $form['code'] = array(
 174      '#type' => 'textarea',
 175      '#description' => t('Enter PHP code for execution. Do not use <code>&#60;?php ?&#62;</code> tags.')
 176    );
 177    $form['token'] = array(
 178      '#type' => 'hidden',
 179      '#value' => drupal_get_token(),
 180    );
 181    $form['op'] = array('#type' => 'submit', '#value' => t('Execute'));
 182    $form['#redirect'] = FALSE;
 183    $form['#action'] = url('admin/firebug/exec', array('absolute' => TRUE));
 184    $form['#skip_duplicate_check'] = TRUE;
 185    return $form; 
 186  }
 187  
 188  function drupalforfirebug_get_php_exec_area() {
 189    #$output = '<iframe style="width=100%;frameborder=0;height=100%;margin-bottom:-3px;" src="' . url('admin/firebug/exec', array('absolute' => TRUE)) . '"></iframe>';
 190    $output = '<object style="width:100%;frameborder=0;height=100%;margin-bottom:-3px;" type="text/html" data="' . url('admin/firebug/exec', array('absolute' => TRUE)) . '"></object>';
 191    return $output;
 192  }
 193  
 194  function drupalforfirebug_get_php_exec($code = NULL, $token = NULL) {
 195    $output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
 196    $output .= '<html><head><title>Execute PHP Code</title></head><body>';
 197    $output .= '<fieldset>';
 198    if (!user_access('Execute Firebug PHP') || (!is_null($code) && !drupal_valid_token($token))) {
 199      $output .= '<legend>' . t('Execute Firebug PHP') . '</legend>';
 200      $output .= t('You do not have the proper permissions to use this functionality.');
 201      $output .= '</fieldset></body></html>';
 202      print $output;
 203      exit();
 204    } else {
 205      if (!$code) {
 206        $output .= '<legend>' . t('Execute Firebug PHP') . '</legend>';
 207        $output .= drupal_get_form('drupalforfirebug_execute_form');
 208        $output .= '</fieldset></body></html>';
 209        print $output;
 210        exit();
 211      } else {
 212        $output = '<legend>' . t('PHP Code Execution Results') . '</legend>';
 213  
 214        // Run the PHP command, get output in variable
 215        ob_start();
 216        eval($code);
 217        $eval_result = ob_get_contents();
 218        ob_end_clean();
 219  
 220        $output .= '<PRE>' . $eval_result . '</PRE>';
 221        $output .= '</fieldset>';
 222        $output .= '<fieldset>';
 223        $output .= '<legend>' . t('Execute Firebug PHP') . '</legend>';
 224        $output .= drupal_get_form('drupalforfirebug_execute_form');
 225        $output .= '</fieldset></body></html>';
 226        print $output;
 227        exit();
 228      }
 229    }
 230  }
 231  
 232  
 233  /**
 234  * Outputs a Execute PHP Form
 235  */
 236  function drupalforfirebug_get_exec_php_callback() {
 237    $code = (isset($_POST['code'])) ? $_POST['code'] : NULL;
 238    $token = (isset($_POST['token'])) ? $_POST['token'] : NULL;
 239    return drupalforfirebug_get_php_exec($code, $token);
 240  }
 241  
 242  /**
 243  * Output Function to Return Hidden Div Containers in Footer
 244  */
 245  function drupalforfirebug_shutdown() {
 246    if (!user_access('Access Firebug Debug')) {
 247      return;
 248    }
 249    $output = '<div style="display: none" id="drupalforfirebug_general">';
 250    $output .= '<fieldset>';
 251    $output .= '<legend>' . t('Drupal for Firebug General Messages') . '</legend>';
 252    $general_messages = drupalforfirebug_get('general');
 253    $output .= $general_messages;
 254    if (!$general_messages) {
 255      $output .= t('There were no messages sent to the general log. Please use "firep($item, $optional_title)" to output messages to this console.');
 256    }
 257    $output .= '</div>';
 258    $output .= '<div style="display: none" id="drupalforfirebug_sql">';
 259    $output .= drupalforfirebug_get_sql_log();
 260    $output .= '</div>';
 261    $output .= '<div style="display: none" id="drupalforfirebug_hook_form_alter">';
 262    $output .= $form_alter_output = drupalforfirebug_get('hook_form_alter');
 263    if (!$form_alter_output) { $output .= t('There was no form altering.'); }
 264    $output .= '</div>';
 265    $output .= '<div style="display: none" id="drupalforfirebug_hook_user">';
 266    $output .= $user_output = drupalforfirebug_get('hook_user');
 267    if (!$user_output) { $output .= t('There was no user processing.'); }
 268    $output .= '</div>';
 269    $output .= '<div style="display: none" id="drupalforfirebug_hook_nodeapi">';
 270    $output .= $node_api_output = drupalforfirebug_get('hook_nodeapi');
 271    if (!$node_api_output) { $output .= t('There was no node processing.'); }
 272    $output .= '</div>';
 273    $output .= '<div style="display: none" id="drupalforfirebug_hook_views">';
 274    if (module_exists('views')) {
 275      $output .= $views_output = drupalforfirebug_get('hook_views');
 276      if (!$views_output) { $output .= t('There was no views processing.'); }
 277    } else {
 278      $output .= t('The views module is not installed.');
 279    }
 280    $output .= '</div>';
 281    $output .= '<div style="display: none" id="drupalforfirebug_php">';
 282    $output .= drupalforfirebug_get_php_exec_area();
 283    $output .= '</div>';
 284    $output .= '<div style="display: none" id="drupalforfirebug_hook_page_alter">';
 285    $output .= t('This feature is only available in Drupal 7.');
 286    $output .= '</div>';
 287    print $output;
 288    unset($GLOBALS['dfp_runtime']);
 289  }
 290  
 291  /**
 292  * Implementation of hook_perm()
 293  */
 294  function drupalforfirebug_perm() {
 295    return array('Access Firebug Debug', 'Execute Firebug PHP');
 296  }
 297  
 298  /**
 299  * Generalized Array Comparision Function
 300  */
 301  function drupalforfirebug_array_compare($a, $b) {
 302    $data = drupalforfirebug_array_compare_code($a, $b);
 303    $style = drupalforfirebug_array_highlight_code($data);
 304    return $style;
 305  }
 306  
 307  /**
 308  * Specialized Function to Return an Array Row
 309  */
 310  function drupalforfirebug_array_row_build($key, $value, $style, $depth) {
 311    $spacing = '';
 312    for ($x = 0; $x <= $depth; $x++) { 
 313      $spacing .= '&nbsp;&nbsp;&nbsp;&nbsp;';
 314    }
 315    switch ($style) {
 316      case 'ADDED':
 317        $color = '<span style="color: green;">';
 318        $colorend = '</span>';
 319        break;
 320      case 'REMOVED':
 321        $color = '<span style="color: red;">';
 322        $colorend = '</span>'; 
 323        break;
 324      case 'SAME':
 325        $color = '<span style="color: black;">';
 326        $colorend = '</span>';
 327        break;
 328      case 'DIFFERENT':
 329        $color = '<span style="color: orange;">';
 330        $colorend = '</span>';
 331        break;
 332      default: // suppose to be for objects
 333        $color = '<span style="color: grey;">'.$style;
 334        $colorend = '</span>';
 335        break;
 336    }
 337  
 338    $output = '';
 339    if (is_array($value) || is_object($value)) {
 340      if ($style == 'DIFFERENT') { // do not highlight if contained item is just changed. 
 341        $color = '';
 342        $colorend = '';
 343      }
 344      if (is_array($value)) {
 345        $output .= "<div>$spacing $color [$key] => array ( $colorend </div>";
 346      } else {
 347        $output .= '<div>$spacing <span style="color: grey;"> [' . $key .'] => stdClass (' . $colorend .'</div>';
 348      }
 349      $output .= drupalforfirebug_array_highlight_code($value, $depth + 1);
 350      $output .= "<div>$spacing $color ) $colorend </div>";
 351    } else {
 352      if (isset($key) || isset($value)) {
 353        if (is_resource($value)) {
 354          $output .= "<div>$spacing $color [$key] => RESOURCE $colorend </div>";
 355        } else {
 356          $output .= "<div>$spacing $color [$key] => [" . check_plain($value) . "] $colorend </div>";
 357        }
 358      }
 359    } 
 360    return $output; 
 361  }
 362  
 363  /**
 364  * Specialized Array Data Style Function
 365  */
 366  function drupalforfirebug_array_highlight_code($data, $depth = 0) {
 367    // Smartly Handling Recursion for Objects
 368    if (is_object($data)) {
 369      $data = (array) $data;
 370      static $refChain = array();
 371      foreach ($refChain as $refVal) {
 372        if ($refVal === $data) {
 373          $data = array('**' . t('Recursion Detected') . '**');
 374        }
 375      }
 376      array_push($refChain, $data);
 377    }
 378  
 379    $output = '';
 380    foreach($data as $key => $value) {
 381      if ((string) $key != '#firebug_style') {
 382        if (isset($data['#firebug_style'])) {
 383          $output .= drupalforfirebug_array_row_build($key, $value, $data['#firebug_style'][$key], $depth);
 384        }
 385      }
 386    } 
 387    return $output;
 388  }
 389  
 390  /**
 391  * Specialized Array Data Comparision Code
 392  */
 393  function drupalforfirebug_array_compare_code($a, $b, $c = array()) {
 394  
 395    // Create the Compared Data Object
 396    $maxcount = count($a) > count($b) ? count($a) : count($b);
 397    $akeys = is_array($a) ? array_keys($a) : array();
 398    $bkeys = is_array($b) ? array_keys($b) : array();
 399    for ($x = 0; $x < $maxcount; $x++) {
 400      // Set the Proper Styling
 401      if (isset($akeys[$x]) && array_key_exists($akeys[$x], array_flip($bkeys))) { // is it in B array?
 402        if ($a[$akeys[$x]] === $b[$akeys[$x]]) {
 403          $c['#firebug_style'][$akeys[$x]] = 'SAME';
 404        } else {
 405          $c['#firebug_style'][$akeys[$x]] = 'DIFFERENT';
 406        }
 407      } else { // not in B array, must be removed
 408        if (isset($akeys[$x])) {
 409          $c['#firebug_style'][$akeys[$x]] = 'REMOVED';
 410        }
 411      }
 412  
 413      // Set the Proper Element
 414      if (isset($akeys[$x]) && is_array($a[$akeys[$x]])) { // is b a valid array
 415        if (isset($c[$akeys[$x]])) {
 416          $c[$akeys[$x]] = drupalforfirebug_array_compare_code($a[$akeys[$x]], $b[$akeys[$x]], $c[$akeys[$x]]);
 417        } else {
 418          $c[$akeys[$x]] = drupalforfirebug_array_compare_code($a[$akeys[$x]], $b[$akeys[$x]], array());
 419        }
 420      } else {
 421        if (isset($akeys[$x]) && array_key_exists($akeys[$x], array_flip($bkeys))) { // is it in B array?
 422          if ($a[$akeys[$x]] === $b[$akeys[$x]]) {
 423            $c[$akeys[$x]] = $a[$akeys[$x]];
 424          } else {
 425            $c[$akeys[$x]] = $b[$akeys[$x]];
 426          }
 427        } else { // not in B array, must be removed
 428          if (isset($akeys[$x])) {
 429            $c[$akeys[$x]] = $a[$akeys[$x]];
 430          }
 431        }
 432      }     
 433      if (isset($bkeys[$x]) && isset($b[$bkeys[$x]])) { // does b have a valid argument
 434        // Set the Proper Styling
 435        if (array_key_exists($bkeys[$x], array_flip($akeys))) { // is it in A array? 
 436          // exists in the A array, already processed
 437        } else {
 438          $c[$bkeys[$x]] = $b[$bkeys[$x]];
 439          $c['#firebug_style'][$bkeys[$x]] = 'ADDED';
 440        }
 441  
 442        // Set the Proper Element
 443        if (isset($b[$bkeys[$x]]) && is_array($b[$bkeys[$x]])) { // is b a valid array
 444          $c[$bkeys[$x]] = drupalforfirebug_array_compare_code($a[$bkeys[$x]], $b[$bkeys[$x]], $c[$bkeys[$x]]);
 445        }
 446      }
 447    }
 448    return $c;
 449  }
 450  
 451  // Array Handling Helper Function
 452  function do_offset($level) {
 453    $offset = "";             // offset for subarry 
 454    for ($i=1; $i<$level;$i++) {
 455      $offset = $offset . "<td></td>";
 456    }
 457    return $offset;
 458  }
 459  
 460  // Array Handling Helper Function
 461  function drupalforfirebug_show_array($array, $level, $sub){
 462    $output = '';
 463    if (is_array($array) == 1){          // check if input is an array
 464      foreach($array as $key_val => $value) {
 465        $offset = "";
 466        if (is_array($value) == 1){   // array is multidimensional
 467          $output .= "<tr>";
 468          $offset = do_offset($level);
 469          $output .= $offset . "<td>" . $key_val . "</td>";
 470          $output .= drupalforfirebug_show_array($value, $level+1, 1);
 471        }
 472        else{                        // (sub)array is not multidim
 473          if ($sub != 1){          // first entry for subarray
 474            $output .= "<tr nosub>";
 475            $offset = do_offset($level);
 476          }
 477          $sub = 0;
 478          $output .= $offset . "<td main ".$sub." width=\"120\">" . $key_val . 
 479            "</td><td width=\"120\">" . $value . "</td>"; 
 480          $output .= "</tr>\n";
 481        }
 482      } //foreach $array
 483    }  
 484    return $output;
 485  }
 486  
 487  // Function to Show an Array
 488  function html_drupalforfirebug_show_array($array){
 489    $output = "<table cellspacing=\"0\" border=\"2\">\n";
 490    $output .= drupalforfirebug_show_array($array, 1, 0);
 491    $output .= "</table>\n";
 492    return $output;
 493  }
 494  
 495  
 496  /**
 497   * Function for fieldsets that wrap object dumps
 498   *
 499   * For efficiency and consistent behavior in firebug window
 500   * Use to be a theme function, but that broke things
 501   */
 502  function drupalforfirebug_field_object($marker, $id, $op = NULL, $data) {
 503    $output = '<fieldset class="toggler">';
 504    $output .= '<legend><strong><a href="#"><em>' . $op . '</em> $'. $marker .'->'. $id . '</a></strong></legend>';
 505    $output .= '<div class="content" style="display: none;">';
 506    $output .= '<div>' . '&nbsp;$'. $marker .' = (' . '</div>';
 507    $output .= $data;
 508    $output .= '<div>' . '&nbsp;);</div>';
 509    $output .= '</div>';
 510    $output .= '</fieldset>';
 511    
 512    return $output;
 513  }
 514  
 515  /**
 516  * Replication of Devel Query Display (but as a table instead of CSS styled div structure)
 517  * This is done to work with the Firefox extension which has a harder time loading CSS
 518  */
 519  function drupalforfirebug_devel_query_table($queries, $counts) {
 520    $header = array ('ms', '#', 'where', 'query');
 521    $i = 0;
 522    foreach ($queries as $query) {
 523      $ar = explode("\n", $query[0]);
 524      $function=array_shift($ar);
 525      $count = isset($counts[$query[0]]) ? $counts[$query[0]] : 0;
 526      $query[0]=join(' ',$ar);
 527  
 528      $diff = round($query[1] * 1000, 2);
 529      if ($diff > variable_get('devel_execution', 5)) {
 530        $cell[$i][] = array ('data' => $diff, 'class' => 'marker');
 531      }
 532      else {
 533        $cell[$i][] = $diff;
 534      }
 535      if ($count > 1) {
 536        $cell[$i][] = array ('data' => $count, 'class' => 'marker');
 537      }
 538      else {
 539        $cell[$i][] = $count;
 540      }
 541      $cell[$i][] = $function;
 542      $pos = strpos($query[0], '*/') + 3;
 543      $cell[$i][] = check_plain(substr($query[0], $pos));
 544      $i++;
 545      unset($diff, $count);
 546    }
 547    if (variable_get('devel_query_sort', DEVEL_QUERY_SORT_BY_SOURCE)) {
 548      usort($cell, '_devel_table_sort');
 549    }
 550    return theme('table', $header, $cell);
 551  }
 552   


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7