source: extensions/event_tracer/main.inc.php @ 25013

Last change on this file since 25013 was 22213, checked in by rvelices, 11 years ago

event tracer version update

  • Property svn:eol-style set to LF
File size: 5.3 KB
RevLine 
[2302]1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[5196]5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
[2342]6// | Copyright(C) 2003-2008 Piwigo team    http://phpwebgallery.net |
[2302]7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/*
[1580]25Plugin Name: Event tracer
[22213]26Version: 2.5.0
[1580]27Description: For developers. Shows all calls to trigger_event.
[3275]28Plugin URI: http://piwigo.org/ext/extension_view.php?eid=288
[2342]29Author: Piwigo team
30Author URI: http://piwigo.org
[1580]31*/
[2302]32
[1580]33if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
34
35class EventTracer
36{
37  var $my_config;
[2773]38  var $trigger_counts = array();
[2497]39
[1731]40  function EventTracer()
[1580]41  {
42  }
43
[2254]44  function get_config_file_dir()
45  {
46    global $conf;
[17520]47    return PHPWG_ROOT_PATH.$conf['data_location'].'plugins/';
[2254]48  }
49
50  function get_config_file_name()
51  {
52    return basename(dirname(__FILE__)).'.dat';
53  }
54
[1580]55  function load_config()
56  {
[2254]57    $x = @file_get_contents( $this->get_config_file_dir().$this->get_config_file_name() );
[1580]58    if ($x!==false)
59    {
60      $c = unserialize($x);
61      // do some more tests here
62      $this->my_config = $c;
63    }
64    if ( !isset($this->my_config)
65        or empty($this->my_config['filters']) )
66    {
67      $this->my_config['filters'] = array( '.*' );
[2773]68      $this->my_config['show_registered'] = true;
[1580]69      $this->save_config();
70    }
71  }
72
73  function save_config()
74  {
[2254]75    $dir = $this->get_config_file_dir();
[2497]76    @mkgetdir($dir);
[2254]77    $file = fopen( $dir.$this->get_config_file_name(), 'w' );
[1580]78    fwrite($file, serialize($this->my_config) );
79    fclose( $file );
80  }
81
[2773]82  function on_page_tail()
83  {
[3611]84    global $debug;
85    if (@$this->my_config['show_registered'])
[2773]86    {
[3611]87      global $pwg_event_handlers;
[2773]88      $out = '';
89      foreach ($pwg_event_handlers as $event => $prio_array)
90      {
91        $out .= $event.' '.intval(@$this->trigger_counts[$event])." calls\n";
92        foreach ($prio_array as $prio => $handlers)
93        {
94          foreach ($handlers as $handler)
95          {
96            $out .= "\t$prio ";
97            if ( is_array($handler['function']) )
98            {
99              if ( is_string($handler['function'][0]) )
100                $out .= $handler['function'][0].'::';
101              else
102                $out .= @get_class($handler['function'][0]).'->';
103              $out .= $handler['function'][1];
104            }
105            else
[20598]106            {
107              if ( !is_scalar($handler['function']) && get_class($handler['function'])=='Closure')
108                $out .= 'Closure';
109              else
110                $out .= $handler['function'];
111            }
[2773]112            $out .= "\n";
113          }
114        }
115        $out .= "\n";
116      }
117      $debug .= '<pre>'.$out.'</pre>';
118    }
[3611]119    if (@$this->my_config['show_included_files'])
120    {
121      $debug .= "<pre><em>Included files</em>\n".var_export( get_included_files(), true ).'</pre>';
122    }
[2773]123  }
124
[3136]125  function on_trigger($event_info)
[1590]126  {
[3136]127    if ($event_info['type']!='post_event')
128      @$this->trigger_counts[$event_info['event']]++;
129
[1590]130    foreach( $this->my_config['filters'] as $filter)
[1580]131    {
[1590]132      if ( preg_match( '/'.$filter.'/', $event_info['event'] ) )
[1580]133      {
[3611]134        if (@$this->my_config['show_args'])
[1580]135        {
[1590]136          $s = '<pre>';
137          $s .= htmlspecialchars( var_export( $event_info['data'], true ) );
138          $s .= '</pre>';
[1580]139        }
[1590]140        else
141          $s = '';
[3136]142        pwg_debug($event_info['type'].' "'.$event_info['event'].'" '.($this->trigger_counts[$event_info['event']]).' calls '.($s) );
[1590]143        break;
[1580]144      }
145    }
146  }
147
[1705]148  function plugin_admin_menu($menu)
[1580]149  {
[1705]150    array_push($menu,
151        array(
152          'NAME' => 'Event Tracer',
[1731]153          'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/tracer_admin.php')
[1705]154        )
155      );
156    return $menu;
[1580]157  }
158}
159
[1731]160$obj = new EventTracer();
[1705]161$obj->load_config();
[1580]162
[1705]163add_event_handler('get_admin_plugin_menu_links', array(&$obj, 'plugin_admin_menu') );
[2773]164add_event_handler('loc_begin_page_tail', array(&$obj, 'on_page_tail') );
[3136]165add_event_handler('trigger', array(&$obj, 'on_trigger') );
[1705]166set_plugin_data($plugin['id'], $obj);
[2254]167?>
Note: See TracBrowser for help on using the repository browser.