]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-plugins.php
Whitespaces, bitches!! Because, you know, CodingStandards.
[Github/YOURLS.git] / includes / functions-plugins.php
1 <?php\r
2 \r
3 /**\r
4  * The filter/plugin API is located in this file, which allows for creating filters\r
5  * and hooking functions, and methods. The functions or methods will be run when\r
6  * the filter is called.\r
7  *\r
8  * Any of the syntaxes explained in the PHP documentation for the\r
9  * {@link http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback 'callback'}\r
10  * type are valid.\r
11  *\r
12  * This API is heavily inspired by the one I implemented in Zenphoto 1.3, which was heavily inspired by the one used in WordPress.\r
13  *\r
14  * @author Ozh\r
15  * @since 1.5\r
16  */\r
17 \r
18 $yourls_filters = array();\r
19 /* This global var will collect filters with the following structure:\r
20  * $yourls_filters['hook']['array of priorities']['serialized function names']['array of ['array (functions, accepted_args)]']\r
21  */\r
22 \r
23 /**\r
24  * Registers a filtering function\r
25  * \r
26  * Typical use:\r
27  *              yourls_add_filter('some_hook', 'function_handler_for_hook');\r
28  *\r
29  * @global array $yourls_filters Storage for all of the filters\r
30  * @param string $hook the name of the YOURLS element to be filtered or YOURLS action to be triggered\r
31  * @param callback $function_name the name of the function that is to be called.\r
32  * @param integer $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default=10, lower=earlier execution, and functions with the same priority are executed in the order in which they were added to the filter)\r
33  * @param int $accepted_args optional. The number of arguments the function accept (default is the number provided).\r
34  */\r
35 function yourls_add_filter( $hook, $function_name, $priority = 10, $accepted_args = NULL, $type = 'filter' ) {\r
36         global $yourls_filters;\r
37         // At this point, we cannot check if the function exists, as it may well be defined later (which is OK)\r
38         $id = yourls_filter_unique_id( $hook, $function_name, $priority );\r
39         \r
40         $yourls_filters[ $hook ][ $priority ][ $id ] = array(\r
41                 'function'      => $function_name,\r
42                 'accepted_args' => $accepted_args,\r
43                 'type'          => $type,\r
44         );\r
45 }\r
46 \r
47 /**\r
48  * Hooks a function on to a specific action.\r
49  *\r
50  * Actions are the hooks that YOURLS launches at specific points\r
51  * during execution, or when specific events occur. Plugins can specify that\r
52  * one or more of its PHP functions are executed at these points, using the\r
53  * Action API.\r
54  *\r
55  * @param string $hook The name of the action to which the $function_to_add is hooked.\r
56  * @param callback $function_name The name of the function you wish to be called.\r
57  * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.\r
58  * @param int $accepted_args optional. The number of arguments the function accept (default 1).\r
59  */\r
60 function yourls_add_action( $hook, $function_name, $priority = 10, $accepted_args = 1 ) {\r
61         return yourls_add_filter( $hook, $function_name, $priority, $accepted_args, 'action' );\r
62 }\r
63 \r
64 \r
65 \r
66 /**\r
67  * Build Unique ID for storage and retrieval.\r
68  *\r
69  * Simply using a function name is not enough, as several functions can have the same name when they are enclosed in classes.\r
70  *\r
71  * @global array $yourls_filters storage for all of the filters\r
72  * @param string $hook hook to which the function is attached\r
73  * @param string|array $function used for creating unique id\r
74  * @param int|bool $priority used in counting how many hooks were applied.  If === false and $function is an object reference, we return the unique id only if it already has one, false otherwise.\r
75  * @param string $type filter or action\r
76  * @return string unique ID for usage as array key\r
77  */\r
78 function yourls_filter_unique_id( $hook, $function, $priority ) {\r
79         global $yourls_filters;\r
80 \r
81         // If function then just skip all of the tests and not overwrite the following.\r
82         if ( is_string( $function ) )\r
83                 return $function;\r
84         // Object Class Calling\r
85         else if ( is_object( $function[0] ) ) {\r
86                 $obj_idx = get_class( $function[0] ) . $function[1];\r
87                 if ( !isset( $function[0]->_yourls_filters_id ) ) {\r
88                         if ( false === $priority )\r
89                                 return false;\r
90                         $count = isset( $yourls_filters[ $hook ][ $priority ]) ? count( (array)$yourls_filters[ $hook ][ $priority ] ) : 0;\r
91                         $function[0]->_yourls_filters_id = $count;\r
92                         $obj_idx .= $count;\r
93                         unset( $count );\r
94                 } else\r
95                         $obj_idx .= $function[0]->_yourls_filters_id;\r
96                 return $obj_idx;\r
97         }\r
98         // Static Calling\r
99         else if ( is_string( $function[0] ) )\r
100                 return $function[0].$function[1];\r
101 \r
102 }\r
103 \r
104 /**\r
105  * Performs a filtering operation on a YOURLS element or event.\r
106  *\r
107  * Typical use:\r
108  *\r
109  *              1) Modify a variable if a function is attached to hook 'yourls_hook'\r
110  *              $yourls_var = "default value";\r
111  *              $yourls_var = yourls_apply_filter( 'yourls_hook', $yourls_var );\r
112  *\r
113  *              2) Trigger functions is attached to event 'yourls_event'\r
114  *              yourls_apply_filter( 'yourls_event' );\r
115  *      (see yourls_do_action() )\r
116  * \r
117  * Returns an element which may have been filtered by a filter.\r
118  *\r
119  * @global array $yourls_filters storage for all of the filters\r
120  * @param string $hook the name of the YOURLS element or action\r
121  * @param mixed $value the value of the element before filtering\r
122  * @return mixed\r
123  */\r
124 function yourls_apply_filter( $hook, $value = '' ) {\r
125         global $yourls_filters;\r
126         if ( !isset( $yourls_filters[ $hook ] ) )\r
127                 return $value;\r
128         \r
129         $args = func_get_args();\r
130         \r
131         // Sort filters by priority\r
132         ksort( $yourls_filters[ $hook ] );\r
133         \r
134         // Loops through each filter\r
135         reset( $yourls_filters[ $hook ] );\r
136         do {\r
137                 foreach( (array) current( $yourls_filters[ $hook ] ) as $the_ ) {\r
138                         if ( !is_null( $the_['function'] ) ){\r
139                                 $args[1] = $value;\r
140                                 $count = $the_['accepted_args'];\r
141                                 if ( is_null( $count ) ) {\r
142                                         $_value = call_user_func_array( $the_['function'], array_slice( $args, 1 ) );\r
143                                 } else {\r
144                                         $_value = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $count ) );\r
145                                 }\r
146                         }\r
147                         if( $the_['type'] == 'filter' )\r
148                                 $value = $_value;\r
149                 }\r
150 \r
151         } while ( next( $yourls_filters[ $hook ] ) !== false );\r
152         \r
153         if( $the_['type'] == 'filter' )\r
154                 return $value;\r
155 }\r
156 \r
157 function yourls_do_action( $hook, $arg = '' ) {\r
158         $args = array();\r
159         if ( is_array( $arg ) && 1 == count( $arg ) && isset( $arg[0] ) && is_object( $arg[0] ) ) // array(&$this)\r
160                 $args[] =& $arg[0];\r
161         else\r
162                 $args[] = $arg;\r
163         for ( $a = 2; $a < func_num_args(); $a++ )\r
164                 $args[] = func_get_arg( $a );\r
165         \r
166         yourls_apply_filter( $hook, $args );\r
167 }\r
168 \r
169 \r
170 /**\r
171  * Removes a function from a specified filter hook.\r
172  *\r
173  * This function removes a function attached to a specified filter hook. This\r
174  * method can be used to remove default functions attached to a specific filter\r
175  * hook and possibly replace them with a substitute.\r
176  *\r
177  * To remove a hook, the $function_to_remove and $priority arguments must match\r
178  * when the hook was added.\r
179  *\r
180  * @global array $yourls_filters storage for all of the filters\r
181  * @param string $hook The filter hook to which the function to be removed is hooked.\r
182  * @param callback $function_to_remove The name of the function which should be removed.\r
183  * @param int $priority optional. The priority of the function (default: 10).\r
184  * @param int $accepted_args optional. The number of arguments the function accepts (default: 1).\r
185  * @return boolean Whether the function was registered as a filter before it was removed.\r
186  */\r
187 function yourls_remove_filter( $hook, $function_to_remove, $priority = 10, $accepted_args = 1 ) {\r
188         global $yourls_filters;\r
189         \r
190         $function_to_remove = yourls_filter_unique_id( $hook, $function_to_remove, $priority );\r
191 \r
192         $remove = isset( $yourls_filters[ $hook ][ $priority ][ $function_to_remove ] );\r
193 \r
194         if ( $remove === true ) {\r
195                 unset ( $yourls_filters[$hook][$priority][$function_to_remove] );\r
196                 if ( empty( $yourls_filters[$hook][$priority] ) )\r
197                         unset( $yourls_filters[$hook] );\r
198         }\r
199         return $remove;\r
200 }\r
201 \r
202 \r
203 /**\r
204  * Check if any filter has been registered for a hook.\r
205  *\r
206  * @global array $yourls_filters storage for all of the filters\r
207  * @param string $hook The name of the filter hook.\r
208  * @param callback $function_to_check optional.  If specified, return the priority of that function on this hook or false if not attached.\r
209  * @return int|boolean Optionally returns the priority on that hook for the specified function.\r
210  */\r
211 function yourls_has_filter( $hook, $function_to_check = false ) {\r
212         global $yourls_filters;\r
213 \r
214         $has = !empty( $yourls_filters[ $hook ] );\r
215         if ( false === $function_to_check || false == $has ) {\r
216                 return $has;\r
217         }\r
218         if ( !$idx = yourls_filter_unique_id( $hook, $function_to_check, false ) ) {\r
219                 return false;\r
220         }\r
221 \r
222         foreach ( (array) array_keys( $yourls_filters[ $hook ] ) as $priority ) {\r
223                 if ( isset( $yourls_filters[ $hook ][ $priority ][ $idx ] ) )\r
224                         return $priority;\r
225         }\r
226         return false;\r
227 }\r
228 \r
229 function yourls_has_action( $hook, $function_to_check = false ) {\r
230         return yourls_has_filter( $hook, $function_to_check );\r
231 }\r
232 \r
233 /**\r
234  * Return number of active plugins\r
235  *\r
236  * @return integer Number of activated plugins\r
237  */\r
238 function yourls_has_active_plugins( ) {\r
239         global $ydb;\r
240         \r
241         if( !property_exists( $ydb, 'plugins' ) || !$ydb->plugins )\r
242                 $ydb->plugins = array();\r
243                 \r
244         return count( $ydb->plugins );\r
245 }\r
246 \r
247 \r
248 /**\r
249  * List plugins in /user/plugins\r
250  *\r
251  * @global $ydb Storage of mostly everything YOURLS needs to know\r
252  * @return array Array of [/plugindir/plugin.php]=>array('Name'=>'Ozh', 'Title'=>'Hello', )\r
253  */\r
254 function yourls_get_plugins( ) {\r
255         global $ydb;\r
256         \r
257         $plugins = (array) glob( YOURLS_PLUGINDIR .'/*/plugin.php');\r
258         \r
259         if( !$plugins )\r
260                 return array();\r
261         \r
262         foreach( $plugins as $key => $plugin ) {\r
263                 $_plugin = yourls_plugin_basename( $plugin );\r
264                 $plugins[ $_plugin ] = yourls_get_plugin_data( $plugin );\r
265                 unset( $plugins[ $key ] );\r
266         }\r
267         \r
268         return $plugins;\r
269 }\r
270 \r
271 /**\r
272  * Check if a plugin is active\r
273  *\r
274  * @param string $file Physical path to plugin file\r
275  * @return bool\r
276  */\r
277 function yourls_is_active_plugin( $plugin ) {\r
278         if( !yourls_has_active_plugins( ) )\r
279                 return false;\r
280         \r
281         global $ydb;\r
282         $plugin = yourls_plugin_basename( $plugin );\r
283         \r
284         return in_array( $plugin, $ydb->plugins );\r
285 \r
286 }\r
287 \r
288 /**\r
289  * Parse a plugin header\r
290  *\r
291  * @param string $file Physical path to plugin file\r
292  * @return array Array of 'Field'=>'Value' from plugin comment header lines of the form "Field: Value"\r
293  */\r
294 function yourls_get_plugin_data( $file ) {\r
295         $fp = fopen( $file, 'r' ); // assuming $file is readable, since yourls_load_plugins() filters this\r
296         $data = fread( $fp, 8192 ); // get first 8kb\r
297         fclose( $fp );\r
298         \r
299         // Capture all the header within first comment block\r
300         if( !preg_match( '!.*?/\*(.*?)\*/!ms', $data, $matches ) )\r
301                 return array();\r
302         \r
303         // Capture each line with "Something: some text"\r
304         unset( $data );\r
305         $lines = preg_split( "[\n|\r]", $matches[1] );\r
306         unset( $matches );\r
307 \r
308         $plugin_data = array();\r
309         foreach( $lines as $line ) {\r
310                 if( !preg_match( '!(.*?):\s+(.*)!', $line, $matches ) )\r
311                         continue;\r
312                 \r
313                 list( $null, $field, $value ) = array_map( 'trim', $matches);\r
314                 $plugin_data[ $field ] = $value;\r
315         }\r
316         \r
317         return $plugin_data;\r
318 }\r
319 \r
320 // Include active plugins\r
321 function yourls_load_plugins() {\r
322         global $ydb;\r
323         $ydb->plugins = array();\r
324         $active_plugins = yourls_get_option( 'active_plugins' );\r
325         \r
326         // Don't load plugins when installing or updating\r
327         if( !$active_plugins  OR ( defined( 'YOURLS_INSTALLING' ) AND YOURLS_INSTALLING ) OR yourls_upgrade_is_needed() )\r
328                 return;\r
329         \r
330         foreach( (array)$active_plugins as $key=>$plugin ) {\r
331                 if( yourls_validate_plugin_file( YOURLS_PLUGINDIR.'/'.$plugin ) ) {\r
332                         include_once( YOURLS_PLUGINDIR.'/'.$plugin );\r
333                         $ydb->plugins[] = $plugin;\r
334                         unset( $active_plugins[$key] );\r
335                 }\r
336         }\r
337         \r
338         // $active_plugins should be empty now, if not, a plugin could not be find: remove it\r
339         if( count( $active_plugins ) ) {\r
340                 $missing = '<strong>'.join( '</strong>, <strong>', $active_plugins ).'</strong>';\r
341                 yourls_update_option( 'active_plugins', $ydb->plugins );\r
342                 $message = 'Could not find and deactivated '. yourls_plural( 'plugin', count( $active_plugins ) ) .' '. $missing;\r
343                 yourls_add_notice( $message );\r
344         }\r
345 }\r
346 \r
347 /**\r
348  * Check if a file is safe for inclusion (well, "safe", no guarantee)\r
349  *\r
350  * @param string $file Full pathname to a file\r
351  */\r
352 function yourls_validate_plugin_file( $file ) {\r
353         if (\r
354                 false !== strpos( $file, '..' )\r
355                 OR\r
356                 false !== strpos( $file, './' )\r
357                 OR\r
358                 'plugin.php' !== substr( $file, -10 )   // a plugin must be named 'plugin.php'\r
359                 OR\r
360                 !is_readable( $file )\r
361         )\r
362                 return false;\r
363                 \r
364         return true;\r
365 }\r
366 \r
367 /**\r
368  * Activate a plugin\r
369  *\r
370  * @param string $plugin Plugin filename (full or relative to plugins directory)\r
371  * @return mixed string if error or true if success\r
372  */\r
373 function yourls_activate_plugin( $plugin ) {\r
374         // validate file\r
375         $plugin = yourls_plugin_basename( $plugin );\r
376         $plugindir = yourls_sanitize_filename( YOURLS_PLUGINDIR );\r
377         if( !yourls_validate_plugin_file( $plugindir.'/'.$plugin ) )\r
378                 return 'Not a valid plugin file';\r
379                 \r
380         // check not activated already\r
381         global $ydb;\r
382         if( yourls_has_active_plugins() && in_array( $plugin, $ydb->plugins ) )\r
383                 return 'Plugin already activated';\r
384         \r
385         // attempt activation. TODO: uber cool fail proof sandbox like in WP.\r
386         ob_start();\r
387         include( YOURLS_PLUGINDIR.'/'.$plugin );\r
388         if ( ob_get_length() > 0 ) {\r
389                 // there was some output: error\r
390                 $output = ob_get_clean();\r
391                 return 'Plugin generated expected output. Error was: <br/><pre>'.$output.'</pre>';\r
392         }\r
393         \r
394         // so far, so good: update active plugin list\r
395         $ydb->plugins[] = $plugin;\r
396         yourls_update_option( 'active_plugins', $ydb->plugins );\r
397         yourls_do_action( 'activated_plugin', $plugin );\r
398         yourls_do_action( 'activated_' . $plugin );\r
399         \r
400         return true;\r
401 }\r
402 \r
403 /**\r
404  * Dectivate a plugin\r
405  *\r
406  * @param string $plugin Plugin filename (full relative to plugins directory)\r
407  * @return mixed string if error or true if success\r
408  */\r
409 function yourls_deactivate_plugin( $plugin ) {\r
410         $plugin = yourls_plugin_basename( $plugin );\r
411 \r
412         // Check plugin is active\r
413         if( !yourls_is_active_plugin( $plugin ) )\r
414                 return 'Plugin not active';\r
415         \r
416         // Deactivate the plugin\r
417         global $ydb;\r
418         $key = array_search( $plugin, $ydb->plugins );\r
419         if( $key !== false ) {\r
420                 array_splice( $ydb->plugins, $key, 1 );\r
421         }\r
422         \r
423         yourls_update_option( 'active_plugins', $ydb->plugins );\r
424         yourls_do_action( 'deactivated_plugin', $plugin );\r
425         yourls_do_action( 'deactivated_' . $plugin );\r
426         \r
427         return true;\r
428 }\r
429 \r
430 /**\r
431  * Return the path of a plugin file, relative to the plugins directory\r
432  */\r
433 function yourls_plugin_basename( $file ) {\r
434         $file = yourls_sanitize_filename( $file );\r
435         $plugindir = yourls_sanitize_filename( YOURLS_PLUGINDIR );\r
436         $file = str_replace( $plugindir, '', $file );\r
437         return trim( $file, '/' );\r
438 }\r
439 \r
440 /**\r
441  * Return the URL of the directory a plugin\r
442  */\r
443 function yourls_plugin_url( $file ) {\r
444         $url = YOURLS_PLUGINURL . '/' . yourls_plugin_basename( $file );\r
445         if( yourls_is_ssl() or yourls_needs_ssl() )\r
446                 $url = str_replace( 'http://', 'https://', $url );\r
447         return yourls_apply_filter( 'plugin_url', $url, $file );\r
448 }\r
449 \r
450 /**\r
451  * Display list of links to plugin admin pages, if any\r
452  */\r
453 function yourls_list_plugin_admin_pages() {\r
454         global $ydb;\r
455         \r
456         if( !property_exists( $ydb, 'plugin_pages' ) || !$ydb->plugin_pages )\r
457                 return;\r
458         \r
459         $plugin_links = array();\r
460         foreach( (array)$ydb->plugin_pages as $plugin => $page ) {\r
461                 $plugin_links[ $plugin ] = array(\r
462                         'url'    => yourls_admin_url( 'plugins.php?page='.$page['slug'] ),\r
463                         'anchor' => $page['title'],\r
464                 );\r
465         }\r
466         return $plugin_links;\r
467 }\r
468 \r
469 /**\r
470  * Register a plugin administration page\r
471  */\r
472 function yourls_register_plugin_page( $slug, $title, $function ) {\r
473         global $ydb;\r
474         \r
475         if( !property_exists( $ydb, 'plugin_pages' ) || !$ydb->plugin_pages )\r
476                 $ydb->plugin_pages = array();\r
477 \r
478         $ydb->plugin_pages[ $slug ] = array(\r
479                 'slug'     => $slug,\r
480                 'title'    => $title,\r
481                 'function' => $function,\r
482         );\r
483 }\r
484 \r
485 /**\r
486  * Handle plugin administration page\r
487  *\r
488  */\r
489 function yourls_plugin_admin_page( $plugin_page ) {\r
490         global $ydb;\r
491 \r
492         // Check the plugin page is actually registered\r
493         if( !isset( $ydb->plugin_pages[$plugin_page] ) ) {\r
494                 yourls_die( 'This page does not exist. Maybe a plugin you thought was activated is inactive?', 'Invalid link' );\r
495         }\r
496         \r
497         // Draw the page itself\r
498         yourls_do_action( 'load-' . $plugin_page);\r
499         yourls_html_head( 'plugin_page_' . $plugin_page, $ydb->plugin_pages[$plugin_page]['title'] );\r
500         yourls_html_logo();\r
501         yourls_html_menu();\r
502         \r
503         call_user_func( $ydb->plugin_pages[$plugin_page]['function'] );\r
504         \r
505         yourls_html_footer();\r
506         \r
507         die();\r
508 }