noalyss Version-9
extension.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * NOALYSS is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * NOALYSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with NOALYSS; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21// Copyright Author Dany De Bontridder danydb@aevalys.eu
22
23/*!\file
24 * \brief the extension class manages the plugins for the security, the access
25 * the inclusion...
26 */
27/*!
28 *
29 * \class Extension
30 *
31 * \brief manage the extension, it involves the table extension
32 *
33 * Data member
34 * - $cn database connection
35 * - $variable :
36 * - id (extension.ex_id)
37 * - name (extension.ex_name)
38 * - plugin_code (extension.ex_code)
39 * - desc (extension.ex_desc)
40 * - enable (extension.ex_enable)
41 * - filepath (extension.ex_file)
42 */
43require_once NOALYSS_INCLUDE.'/database/menu_ref_sql.class.php';
44require_once NOALYSS_INCLUDE.'/database/profile_sql.class.php';
45
47{
48 // code of the standard plugin (from noalyss-plugins)
50 array('AMORTIS','BACKNOADM','COPRO','IMPCARD',
51 'IMPORTBANK','INVOICING','LISTING',
52 'MODOP','RAPAV','SAV',
53 'TOOLPCMN','TOOLS','TRANSFORM',
54 'TVA');
55
56 public function verify()
57 {
58 // Verify that the elt we want to add is correct
59 if (trim($this->me_code)=="")
60 throw new Exception('Le code ne peut pas être vide');
61 if (trim($this->me_menu)=="")
62 throw new Exception('Le nom ne peut pas être vide');
63 if (trim($this->me_file)=="")
64 throw new Exception('Chemin incorrect');
65 if (file_exists(NOALYSS_PLUGIN.'/'.$this->me_file)==false)
66 throw new Exception("$this->me_code $this->me_file".'Extension non trouvée, le chemin est-il correct?');
67 }
68
69 /*!@brief search a extension, the what is the column (extends_code */
70
71 function search($p_what)
72 {
73 $this->me_code=strtoupper($p_what);
74 if ($this->load()==false)
75 return null;
76 return 1;
77 }
78
79 /*!\brief return 1 if the user given in parameter can access this extension
80 * otherwise returns 0
81 * \param $p_login the user login
82 * \return 1 has access, 0 has no access
83 */
84
85 function can_request($p_login)
86 {
87 $cnt=$this->cn->get_value("select count(*) from menu_ref
88 join profile_menu using (me_code)
89 join profile_user using (p_id)
90 where
91 me_code=$1
92 and user_name=$2", array($this->me_code, $p_login));
93 if ($cnt>0)
94 return 1;
95 return 0;
96 }
97
98 /*!@brief make an array of the available plugin for the current user
99 * @return an array
100 * @see ISelect
101 */
102
103 static function make_array($cn)
104 {
105 $sql="select DISTINCT me_code as value, me_menu as label from ".
106 " menu_ref join profile_menu using (me_code)
107 join profile_user using (p_id) where ".
108 " user_name=$1 and me_type='PL' ORDER BY ME_MENU";
109 $a=$cn->get_array($sql, array($_SESSION[SESSION_KEY.'g_user']));
110 return $a;
111 }
112 /**
113 * @brief check the version of the plugin , null stands for one of the standard plugins, it means
114 * self::aStandard_plugin
115 * @global type $version_noalyss
116 * @param type $i
117 * @param type $p_plugin_code
118 * @return type
119 */
120 static function check_version($i,$p_plugin_code=null)
121 {
122 global $version_noalyss;
124 {
125 alert(_('Cette extension ne fonctionne pas sur cette version de NOALYSS'.
126 ' Veuillez mettre votre programme a jour. Version minimum ').$i);
127 return;
128 }
129 Extension::check_plugin_version($p_plugin_code);
130 }
131
132 /**
133 * @brief insert into the table profile_menu for the given profile id and depending
134 * of the module $p_module
135 * @remark type $cn
136 * @param type $p_id profile.p_id
137 * @throws Exception 10 : profile absent , 20 module absent , 30 No parent menu
138 */
140 {
141 global $cn;
142 // Module for the plugin
143 $p_module=$this->depend;
144 //profile exists ?
146 if ($profile->p_id!=$p_id)
147 {
148 throw new Exception(_('Profil inexistant'), 10);
149 }
150 // Menu exists
151 \Noalyss\Dbg::echo_var(1,__FILE__.__LINE__. "p_module to find $p_module");
152 $module=new Menu_Ref($cn, $p_module);
153 if ($module->me_code==null)
154 {
155 throw new Exception(_('Module inexistant'), 20);
156 }
157 // Dependency
158 $dep_id=$cn->get_array('select pm_id from profile_menu
159 where
160 p_id=$1
161 and me_code = $2 ', array($p_id, $p_module));
162 // throw an exception if there is no dependency
163 if (empty($dep_id))
164 {
165 throw new Exception(_('Pas de menu ').$p_module, 30);
166 }
167 $nb_dep=count($dep_id);
168
169 // insert at the right location
170 for ($i=0; $i<$nb_dep; $i++)
171 {
172 $profil_menu=new Profile_Menu($cn);
173 $profil_menu->me_code=$this->me_code;
174 $profil_menu->me_code_dep=$p_module;
175 $profil_menu->p_type_display='S';
176 $profil_menu->p_id=$p_id;
177 $profil_menu->pm_id_dep=$dep_id[$i]['pm_id'];
178 $profil_menu->pm_default=0;
179 $profil_menu->p_order=$this->order;
180
181 $cnt=$profil_menu->count(' where pm_id_dep=$3 and p_id=$1 and me_code = $2',
182 array($p_id, $this->me_code, $dep_id[$i]['pm_id']));
183 if ($cnt==0)
184 {
185 $profil_menu->insert();
186 }
187 }
188 }
189
191 {
192 global $cn;
193
194 $cn->exec_sql('delete from profile_menu where (me_code = $1 or me_code in (select me_code from menu_ref where me_file=$2)) and p_id=$3',
195 array($this->me_code, $this->me_file, $p_id));
196 }
197
198 /**
199 * Insert a plugin into the given profile, by default always insert into EXT
200 *
201 * @param type $p_id profile.p_id
202 * @throws Exception if duplicate or error db
203 */
204 function insert_plugin()
205 {
206 try
207 {
208 $this->cn->start();
209 $this->verify();
210 // check if duplicate
211 $this->me_code=strtoupper($this->me_code);
212 $count=$this->cn->get_value("select count(*) from menu_ref where me_code=$1", array($this->me_code));
213 if ($count!=0)
214 throw new Exception("Doublon");
215 $this->me_type='PL';
216 $this->insert();
217 $this->cn->commit();
218 }
219 catch (Exception $exc)
220 {
221 echo alert($exc->getMessage());
222 }
223 }
224
225 function update_plugin()
226 {
227 try
228 {
229 $this->cn->start();
230 $this->verify();
231 $this->me_type='PL';
232 $this->update();
233 $this->cn->commit();
234 }
235 catch (Exception $exc)
236 {
237 echo alert($exc->getMessage());
238 }
239 }
240
241 function remove_plugin()
242 {
243 try
244 {
245 $this->cn->start();
246 $this->delete();
247 $this->cn->commit();
248 }
249 catch (Exception $exc)
250 {
251 echo alert($exc->getMessage());
252 }
253 }
254
255 /**
256 * @brief remove all the standard plugins schema
257 * @param Database $p_cn
258 */
259 static function clean(Database $p_cn)
260 {
261 $a_ext=array("tva_belge", "amortissement", "impdol", "coprop", "importbank");
262 for ($i=0; $i<count($a_ext); $i++)
263 {
264 if ($p_cn->exist_schema($a_ext[$i]))
265 {
266 $p_cn->exec_sql("drop schema ".$a_ext[$i]." cascade");
267 }
268 }
269 }
270
271 /**
272 * @brief compare the version of the plugin and the last version , propose to update it if a new version exists
273 * @todo add a mechanism to check once a day
274 * @global User $g_user
275 * @global number $version_plugin
276 */
277 static function check_plugin_version($p_plugin_code)
278 {
279 global $g_user, $version_plugin;
280 if ($g_user->Admin()==1)
281 {
282 if ( in_array($p_plugin_code, self::aStandard_plugin) && SITE_UPDATE_PLUGIN!="")
283 {
284 $update=@file_get_contents(SITE_UPDATE_PLUGIN);
285 if ($update>$version_plugin)
286 {
287 echo '<div id="version_plugin_div_id" class="inner_box" style="position:absolute;zindex:2;top:5px;left:37.5%;width:25%">';
288 echo '<p class="notice">';
289 echo "Mise à jour disponible des plugins pour NOALYSS, version actuelle : $update votre version $version_plugin";
290 echo '</p>';
291 echo '<p style="text-align:center">'.
292 '<a id="version_plugin_button" class="button" onclick="$(\'version_plugin_div_id\').remove()">'.
293 _('Fermer').
294 "</a></p>";
295 echo '</div>';
296 }
297 }
298 }
299 }
300
301 /**
302 * Check that the xml contains all the needed information to change them into
303 * a extension, the exception code is 0 if the element is optional
304 * @brief Check XML.
305 * @param SimpleXMLElement $xml
306 * @throws Exception
307 */
308 function check_xml(SimpleXMLElement $xml)
309 {
310 try
311 {
312 if (!isset($xml->plugin))
313 throw new Exception(_('Manque plugin'), 1);
314 $nb_plugin=count($xml->plugin);
315
316 for ($i=0; $i<$nb_plugin; $i++)
317 {
318 if (!isset($xml->plugin[$i]->name))
319 throw new Exception(_('Manque nom'), 1);
320 if (!isset($xml->plugin[$i]->description))
321 throw new Exception(_('Manque description'), 0);
322 if (!isset($xml->plugin[$i]->code))
323 throw new Exception(_('Manque code'), 1);
324 if (!isset($xml->plugin[$i]->author))
325 throw new Exception(_('Manque auteur'), 0);
326 if (!isset($xml->plugin[$i]->root))
327 throw new Exception(_('Manque répertoire racine'), 1);
328 if (!isset($xml->plugin[$i]->file))
329 throw new Exception(_('Manque fichier à inclure'), 1);
330 if (!isset($xml->plugin[$i]->version))
331 throw new Exception(_("Manque version de l'extension"), 1);
332
333 if (!isset($xml->plugin[$i]->depend))
334 $xml->plugin[$i]->depend="EXT";
335 if (!isset($xml->plugin[$i]->order))
336 $xml->plugin[$i]->order=9000;
337 }
338 }
339 catch (Exception $ex)
340 {
341 throw $ex;
342 }
343 }
344
345 /**
346 * @brief Parse a XML file to complete an array of extension objects, in the plugin.xml file , you can find
347 * several plugins sharing some parts.
348 *
349 * @param string $p_file filename
350 * @return array array of Extension
351 */
352 static function read_definition($p_file)
353 {
354 global $cn;
355 $dom=new DomDocument('1.0');
356 $dom->load($p_file);
357 $xml=simplexml_import_dom($dom);
358 $nb_plugin=count($xml->plugin);
359 $a_extension=array();
360 for ($i=0; $i<$nb_plugin; $i++)
361 {
362
363 $extension=new Extension($cn);
364 try
365 {
366 $extension->check_xml($xml);
367 }
368 catch (Exception $ex)
369 {
370 echo_warning($ex->getMessage());
371 if ($ex->getCode()==1)
372 {
373 continue;
374 }
375 }
376 $extension->me_file=trim($xml->plugin[$i]->root).'/'.trim($xml->plugin[$i]->file);
377 $extension->me_code=trim($xml->plugin[$i]->code);
378 $extension->me_description=(isset($xml->plugin[$i]->description))?trim($xml->plugin[$i]->description):"";
379 $extension->me_description_etendue=(trim($xml->plugin[$i]->author))?trim($xml->plugin[$i]->author):"";
380 $extension->me_type='PL';
381 $extension->me_menu=trim($xml->plugin[$i]->name);
382 $extension->me_parameter='plugin_code='.trim($xml->plugin[$i]->code);
383 $extension->depend=(isset($xml->plugin[$i]->depend))?trim($xml->plugin[$i]->depend):"EXT";
384 $extension->order=(isset($xml->plugin[$i]->order))?trim($xml->plugin[$i]->order):9000;
385 $extension->version=trim($xml->plugin[$i]->version);
386 $extension->noalyss_version=(isset($xml->plugin[$i]->noalyss_version))?trim($xml->plugin[$i]->noalyss_version):8000;
387 $a_extension[]=clone $extension;
388 }
389 return $a_extension;
390 }
391
392 /**
393 * @brief find the extension with the me_code = last part of access_code
394 * @param $a_extension
395 * @param $access_code find the ME_CODE (normally last part )
396 * @return the extension or null
397 */
398 public static function find_extension_code($a_extension,$access_code):Extension|null
399 {
400 $a_me_code=explode("/", $access_code);
401 if (empty($a_me_code ) ) return null;
402 $nb_me_code=count($a_me_code);
403 $me_code=$a_me_code[$nb_me_code-1];
404 foreach ($a_extension as $extension) {
405 if ($extension->me_code==$me_code) return $extension;
406 }
407 return null;
408 }
409
410 public function __toString(): string
411 {
412 $r = "";
413 $r .= " me_code " . $this->me_code.PHP_EOL;
414 $r .= " me_menu.".$this->me_menu.PHP_EOL;
415 $r .= " version".$this->version.PHP_EOL;
416 $r .= " noalyss_version".$this->noalyss_version.PHP_EOL;
417 $r .= " me_file" . $this->me_file.PHP_EOL;
418 $r .= " me_url" . $this->me_url.PHP_EOL;
419 $r .= " me_description" . $this->me_description.PHP_EOL;
420 $r .= " me_parameter" . $this->me_parameter.PHP_EOL;
421 $r .= " me_javascript" . $this->me_javascript.PHP_EOL;
422 $r .= " me_type" . $this->me_type.PHP_EOL;
423 $r .= " me_descrition_etendue" . $this->me_description_etendue.PHP_EOL;
424 return "Extension $r";
425 }
426
427 /**
428 * @brief retrieve the version of the current plugin
429 * @param $xml_file always __DIR__."/plugin.xml"
430 * @param $plugin_code the plugin or $_REQUEST['ac']
431 * @return int version or -1 if not found
432 */
433 public static function get_version($xml_file,$plugin_code):int
434 {
435 $aExtension=\Extension::read_definition($xml_file);
436 $extension=self::find_extension_code($aExtension, $plugin_code);
437 if ( empty($plugin_code)) return -1;
438 return $extension->version;
439 }
440
441
442}
echo_warning($p_string)
warns
Definition: ac_common.php:589
alert($p_msg, $buffer=false)
alert in javascript
Definition: ac_common.php:738
global $g_user
if no group available , then stop
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
switch($op2) $xml
Definition: ajax_card.php:806
$input_from cn
Definition: balance.inc.php:66
$ex
Definition: balance.inc.php:45
for($e=0;$e< $nb_dirscan;$e++) $nb_plugin
exec_sql($p_string, $p_array=null)
send a sql string to the database
contains the class for connecting to Noalyss
manage the extension, it involves the table extension
const aStandard_plugin
static check_plugin_version($p_plugin_code)
compare the version of the plugin and the last version , propose to update it if a new version exists
remove_from_profile_menu($p_id)
static clean(Database $p_cn)
remove all the standard plugins schema
static make_array($cn)
make an array of the available plugin for the current user
check_xml(SimpleXMLElement $xml)
Check that the xml contains all the needed information to change them into a extension,...
insert_profile_menu($p_id=1)
insert into the table profile_menu for the given profile id and depending of the module $p_module
insert_plugin()
Insert a plugin into the given profile, by default always insert into EXT.
can_request($p_login)
return 1 if the user given in parameter can access this extension otherwise returns 0
static find_extension_code($a_extension, $access_code)
find the extension with the me_code = last part of access_code
search($p_what)
search a extension, the what is the column (extends_code
static get_version($xml_file, $plugin_code)
retrieve the version of the current plugin
static read_definition($p_file)
Parse a XML file to complete an array of extension objects, in the plugin.xml file ,...
static check_version($i, $p_plugin_code=null)
check the version of the plugin , null stands for one of the standard plugins, it means self::aStanda...
Menu_Ref let you manage the available menu.
static echo_var($n_level, $msg, $print=true)
Display the value of a var if DEBUGNOALYSS is greater than $n_level, the debugging info has a certain...
Definition: dbg.php:45
Manage the menu of a profile.
Manage the table public.profile.
global $version_noalyss
Definition: constant.php:26
$count
foreach(array('magic_quotes_gpc', 'magic_quotes_runtime') as $a) $module
Definition: install.php:410
$dom
Definition: xml.php:15