1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008 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 | /* |
---|
25 | Plugin Name: Event tracer |
---|
26 | Version: 2.0 |
---|
27 | Description: For developers. Shows all calls to trigger_event. |
---|
28 | Plugin URI: http://piwigo.org |
---|
29 | Author: Piwigo team |
---|
30 | Author URI: http://piwigo.org |
---|
31 | */ |
---|
32 | |
---|
33 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
34 | |
---|
35 | class 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 $conf['local_data_dir'].'/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_args'] = false; |
---|
69 | $this->my_config['show_registered'] = true; |
---|
70 | $this->save_config(); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | function save_config() |
---|
75 | { |
---|
76 | $dir = $this->get_config_file_dir(); |
---|
77 | @mkgetdir($dir); |
---|
78 | $file = fopen( $dir.$this->get_config_file_name(), 'w' ); |
---|
79 | fwrite($file, serialize($this->my_config) ); |
---|
80 | fclose( $file ); |
---|
81 | } |
---|
82 | |
---|
83 | function on_pre_trigger_event($event_info) |
---|
84 | { |
---|
85 | @$this->trigger_counts[$event_info['event']]++; |
---|
86 | $this->dump('pre_trigger_event', $event_info); |
---|
87 | } |
---|
88 | function on_post_trigger_event($event_info) |
---|
89 | { |
---|
90 | $this->dump('post_trigger_event', $event_info); |
---|
91 | } |
---|
92 | |
---|
93 | function on_trigger_action($event_info) |
---|
94 | { |
---|
95 | @$this->trigger_counts[$event_info['event']]++; |
---|
96 | $this->dump('trigger_action', $event_info); |
---|
97 | } |
---|
98 | |
---|
99 | function on_page_tail() |
---|
100 | { |
---|
101 | if (1 || @$this->my_config['show_registered']) |
---|
102 | { |
---|
103 | global $debug, $pwg_event_handlers; |
---|
104 | $out = ''; |
---|
105 | foreach ($pwg_event_handlers as $event => $prio_array) |
---|
106 | { |
---|
107 | $out .= $event.' '.intval(@$this->trigger_counts[$event])." calls\n"; |
---|
108 | foreach ($prio_array as $prio => $handlers) |
---|
109 | { |
---|
110 | foreach ($handlers as $handler) |
---|
111 | { |
---|
112 | $out .= "\t$prio "; |
---|
113 | if ( is_array($handler['function']) ) |
---|
114 | { |
---|
115 | if ( is_string($handler['function'][0]) ) |
---|
116 | $out .= $handler['function'][0].'::'; |
---|
117 | else |
---|
118 | $out .= @get_class($handler['function'][0]).'->'; |
---|
119 | $out .= $handler['function'][1]; |
---|
120 | } |
---|
121 | else |
---|
122 | $out .= $handler['function']; |
---|
123 | $out .= "\n"; |
---|
124 | } |
---|
125 | } |
---|
126 | $out .= "\n"; |
---|
127 | } |
---|
128 | $debug .= '<pre>'.$out.'</pre>'; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | function dump($event, $event_info) |
---|
133 | { |
---|
134 | foreach( $this->my_config['filters'] as $filter) |
---|
135 | { |
---|
136 | if ( preg_match( '/'.$filter.'/', $event_info['event'] ) ) |
---|
137 | { |
---|
138 | if ($this->my_config['show_args']) |
---|
139 | { |
---|
140 | $s = '<pre>'; |
---|
141 | $s .= htmlspecialchars( var_export( $event_info['data'], true ) ); |
---|
142 | $s .= '</pre>'; |
---|
143 | } |
---|
144 | else |
---|
145 | $s = ''; |
---|
146 | pwg_debug($event.' "'.$event_info['event'].'" '.($this->trigger_counts[$event_info['event']]).' calls '.($s) ); |
---|
147 | break; |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | function plugin_admin_menu($menu) |
---|
153 | { |
---|
154 | array_push($menu, |
---|
155 | array( |
---|
156 | 'NAME' => 'Event Tracer', |
---|
157 | 'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/tracer_admin.php') |
---|
158 | ) |
---|
159 | ); |
---|
160 | return $menu; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | $obj = new EventTracer(); |
---|
165 | $obj->load_config(); |
---|
166 | |
---|
167 | add_event_handler('get_admin_plugin_menu_links', array(&$obj, 'plugin_admin_menu') ); |
---|
168 | add_event_handler('pre_trigger_event', array(&$obj, 'on_pre_trigger_event') ); |
---|
169 | add_event_handler('post_trigger_event', array(&$obj, 'on_post_trigger_event') ); |
---|
170 | add_event_handler('loc_begin_page_tail', array(&$obj, 'on_page_tail') ); |
---|
171 | add_event_handler('trigger_action', array(&$obj, 'on_trigger_action') ); |
---|
172 | set_plugin_data($plugin['id'], $obj); |
---|
173 | ?> |
---|