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