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

Last change on this file since 31946 was 30183, checked in by ddtddt, 10 years ago

[extensions] - event_tracer - check 2.7

  • Property svn:eol-style set to LF
File size: 5.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 Piwigo team    http://phpwebgallery.net |
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/*
25Plugin Name: Event tracer
26Version: auto
27Description: For developers. Shows all calls to trigger_change.
28Plugin URI: http://piwigo.org/ext/extension_view.php?eid=288
29Author: Piwigo team
30Author URI: http://piwigo.org
31*/
32
33if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
34
35class EventTracer
36{
37  var $my_config;
38  var $trigger_counts = array();
39
40  function EventTracer()
41  {
42  }
43
44  function get_config_file_dir()
45  {
46    global $conf;
47    return PHPWG_ROOT_PATH.$conf['data_location'].'plugins/';
48  }
49
50  function get_config_file_name()
51  {
52    return basename(dirname(__FILE__)).'.dat';
53  }
54
55  function load_config()
56  {
57    $x = @file_get_contents( $this->get_config_file_dir().$this->get_config_file_name() );
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( '.*' );
68      $this->my_config['show_registered'] = true;
69      $this->save_config();
70    }
71  }
72
73  function save_config()
74  {
75    $dir = $this->get_config_file_dir();
76    @mkgetdir($dir);
77    $file = fopen( $dir.$this->get_config_file_name(), 'w' );
78    fwrite($file, serialize($this->my_config) );
79    fclose( $file );
80  }
81
82  function on_page_tail()
83  {
84    global $debug;
85    if (@$this->my_config['show_registered'])
86    {
87      global $pwg_event_handlers;
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
106            {
107              if ( !is_scalar($handler['function']) && get_class($handler['function'])=='Closure')
108                $out .= 'Closure';
109              else
110                $out .= $handler['function'];
111            }
112            $out .= "\n";
113          }
114        }
115        $out .= "\n";
116      }
117      $debug .= '<pre>'.$out.'</pre>';
118    }
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    }
123  }
124
125  function on_trigger($event_info)
126  {
127    if ($event_info['type']!='post_event')
128      @$this->trigger_counts[$event_info['event']]++;
129
130    foreach( $this->my_config['filters'] as $filter)
131    {
132      if ( preg_match( '/'.$filter.'/', $event_info['event'] ) )
133      {
134        if (@$this->my_config['show_args'])
135        {
136          $s = '<pre>';
137          $s .= htmlspecialchars( var_export( $event_info['data'], true ) );
138          $s .= '</pre>';
139        }
140        else
141          $s = '';
142        pwg_debug($event_info['type'].' "'.$event_info['event'].'" '.($this->trigger_counts[$event_info['event']]).' calls '.($s) );
143        break;
144      }
145    }
146  }
147
148  function plugin_admin_menu($menu)
149  {
150    array_push($menu,
151        array(
152          'NAME' => 'Event Tracer',
153          'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/tracer_admin.php')
154        )
155      );
156    return $menu;
157  }
158}
159
160$obj = new EventTracer();
161$obj->load_config();
162
163add_event_handler('get_admin_plugin_menu_links', array(&$obj, 'plugin_admin_menu') );
164add_event_handler('loc_begin_page_tail', array(&$obj, 'on_page_tail') );
165add_event_handler('trigger', array(&$obj, 'on_trigger') );
166set_plugin_data($plugin['id'], $obj);
167?>
Note: See TracBrowser for help on using the repository browser.