Alternc  3.2
Alternc logiel libre pour l'hébergement
 All Data Structures Namespaces Files Functions Variables Pages
m_hooks.php
Go to the documentation of this file.
1 <?php
2 /*
3  ----------------------------------------------------------------------
4  AlternC - Web Hosting System
5  Copyright (C) 2000-2012 by the AlternC Development Team.
6  https://alternc.org/
7  ----------------------------------------------------------------------
8  LICENSE
9 
10  This program is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License (GPL)
12  as published by the Free Software Foundation; either version 2
13  of the License, or (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  To read the license please visit http://www.gnu.org/copyleft/gpl.html
21  ----------------------------------------------------------------------
22  Purpose of file: Manage hook system.
23  ----------------------------------------------------------------------
24 */
25 
26 /**
27  * This class manage hooks.
28  *
29  * @copyright AlternC-Team 2002-2005 http://alternc.org/
30  */
31 class m_hooks {
32 
33  /*---------------------------------------------------------------------------*/
34  /** Constructor
35  * hooks([$mid]) Constructeur de la classe hooks, ne fait rien pour le moment
36  */
37  function m_hooks() {
38  }
39 
40  /*---------------------------------------------------------------------------*/
41  /**
42  * invoke() permet de lancer une fonction donné en parametre dans toute les classes
43  * connues de alternc, avec les parametres donnés.
44  * @param string $hname nom de la fonction "hooks" que l'on cherche dans les classes
45  * @param array $hparam tableau contenant les parametres
46  * @param array|string $hclass tableau contenant les classes spécifique qu'on veux appeler (si on veux pas TOUTE les appeler)
47  * @return array with the return of each classes
48  */
49  function invoke($hname, $hparam = array(), $hclass = null) {
50 
51  // Si $hclass est defini, on veut appeler le hooks QUE pour UNE
52  // classe et pas pour toute.
53  if (is_null($hclass)) {
54  global $classes;
55  } else {
56  if (is_array($hclass)) {
57  $classes = $hclass;
58  } else {
59  $classes = array($hclass);
60  }
61  }
62 
63  // On parcourt les classes, si la fonction qu'on cherche
64  // existe on l'execute et on rajoute ce qu'elle a retourné dans
65  // un tableau
66  $val = array();
67  foreach ($classes as $c) {
68  global $$c;
69  if ( method_exists($$c, $hname) ) {
70  //$val[$$c]=call_user_func_array(array($$c,$hname), $hparam);
71  $val[$c]=call_user_func_array(array($$c,$hname), $hparam);
72  }
73  }
74 
75  // On retourne le tout
76  return $val;
77  }
78 
79  /*---------------------------------------------------------------------------*/
80  /**
81  * invoke each executable script of the directory (or the specified script)
82  * @param string $scripts a script or a directory
83  * @param array $parameters parameters for the scripts
84  * @return boolean TRUE
85  */
86  function invoke_scripts($scripts, $parameters=array()) {
87 
88  // First, build the list of script we want to launch
89  $to_launch=array();
90  if (is_file($scripts)) {
91  if (is_executable($script)) {
92  $to_launch[]=$scripts;
93  }
94  } else if (is_dir($scripts)) {
95  foreach ( scandir($scripts) as $ccc ) {
96  if (is_file($ccc) && is_executable($ccc)) {
97  $to_launch[]=$ccc;
98  }
99  }
100  } else {
101  // not a file nor a directory
102  return false;
103  }
104 
105  // Protect each parameters
106  $parameters = array_map('escapeshellarg', $parameters);
107  $params = implode(" ", $parameters);
108 
109  // Launch !
110  foreach($to_launch as $fi) {
111  system($fi." ".$params);
112  }
113 
114  // TODO: return something more interesting than true
115  return true;
116  }
117 
118 } /* Class hooks */
119