noalyss Version-9
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
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 /**************************************************************************
57 * Variables from plugin.xml
58 *************************************************************************/
59 var $version; //<! version of the plugin in the XML
60 var $noalyss_version; //!< minimum version of NOALYSS for this plugin
61 var $order ; //!< $order in the menu (can be override by config menu)
62 var $depend; //!< default depending menu (can be override by config menu)
63
64 public function verify()
65 {
66 // Verify that the elt we want to add is correct
67 if (trim($this->me_code)=="")
68 throw new Exception('Le code ne peut pas être vide');
69 if (trim($this->me_menu)=="")
70 throw new Exception('Le nom ne peut pas être vide');
71 if (trim($this->me_file)=="")
72 throw new Exception('Chemin incorrect');
73 if (file_exists(NOALYSS_PLUGIN.'/'.$this->me_file)==false)
74 throw new Exception("$this->me_code $this->me_file".'Extension non trouvée, le chemin est-il correct?');
75 }
76
77 /*!@brief search a extension, the what is the column (extends_code */
78
79 function search($p_what)
80 {
81 $this->me_code=strtoupper($p_what);
82 if ($this->load()==false)
83 return null;
84 return 1;
85 }
86
87 /*!\brief return 1 if the user given in parameter can access this extension
88 * otherwise returns 0
89 * \param $p_login the user login
90 * \return 1 has access, 0 has no access
91 */
92
93 function can_request($p_login)
94 {
95 $cnt=$this->cn->get_value("select count(*) from menu_ref
96 join profile_menu using (me_code)
97 join profile_user using (p_id)
98 where
99 me_code=$1
100 and user_name=$2", array($this->me_code, $p_login));
101 if ($cnt>0)
102 return 1;
103 return 0;
104 }
105
106 /*!@brief make an array of the available plugin for the current user
107 * @return an array
108 * @see ISelect
109 */
110
111 static function make_array($cn)
112 {
113 $sql="select DISTINCT me_code as value, me_menu as label from ".
114 " menu_ref join profile_menu using (me_code)
115 join profile_user using (p_id) where ".
116 " user_name=$1 and me_type='PL' ORDER BY ME_MENU";
117 $a=$cn->get_array($sql, array($_SESSION[SESSION_KEY.'g_user']));
118 return $a;
119 }
120 /**
121 * @brief check the version of the plugin , null stands for one of the standard plugins, it means
122 * self::aStandard_plugin
123 * @global type $version_noalyss
124 * @param type $i
125 * @param type $p_plugin_code
126 * @return type
127 */
128 static function check_version($i,$p_plugin_code=null)
129 {
130 global $version_noalyss;
132 {
133 alert(_('Cette extension ne fonctionne pas sur cette version de NOALYSS'.
134 ' Veuillez mettre votre programme a jour. Version minimum ').$i);
135 return;
136 }
137 Extension::check_plugin_version($p_plugin_code);
138 }
139
140 /**
141 * @brief insert into the table profile_menu for the given profile id and depending
142 * of the module $p_module
143 * @remark type $cn
144 * @param type $p_id profile.p_id
145 * @throws Exception 10 : profile absent , 20 module absent , 30 No parent menu
146 */
148 {
149 global $cn;
150 // Module for the plugin
151 $p_module=$this->depend;
152 //profile exists ?
154 if ($profile->p_id!=$p_id)
155 {
156 throw new Exception(_('Profil inexistant'), 10);
157 }
158 // Menu exists
159
160 $module=new Menu_Ref($cn, $p_module);
161 if ($module->me_code==null)
162 {
163 throw new Exception(_('Module inexistant'), 20);
164 }
165 // Dependency
166 $dep_id=$cn->get_array('select pm_id from profile_menu
167 where
168 p_id=$1
169 and me_code = $2 ', array($p_id, $p_module));
170 // throw an exception if there is no dependency
171 if (empty($dep_id))
172 {
173 $msg = sprintf(_("Le menu %s dont dépend %s doit être crée ou %s doit être ajouté depuis le menu C0PROFL"),
174 $p_module,$this->me_code,$this->me_code);
175 throw new Exception($msg, 30);
176 }
177 $nb_dep=count($dep_id);
178
179 // insert at the right location
180 for ($i=0; $i<$nb_dep; $i++)
181 {
182 $profil_menu=new Profile_Menu($cn);
183 $profil_menu->me_code=$this->me_code;
184 $profil_menu->me_code_dep=$p_module;
185 $profil_menu->p_type_display='S';
186 $profil_menu->p_id=$p_id;
187 $profil_menu->pm_id_dep=$dep_id[$i]['pm_id'];
188 $profil_menu->pm_default=0;
189 $profil_menu->p_order=$this->order;
190
191 $cnt=$profil_menu->count(' where pm_id_dep=$3 and p_id=$1 and me_code = $2',
192 array($p_id, $this->me_code, $dep_id[$i]['pm_id']));
193 if ($cnt==0)
194 {
195 $profil_menu->insert();
196 }
197 }
198 }
199
201 {
202 global $cn;
203
204 $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',
205 array($this->me_code, $this->me_file, $p_id));
206 }
207
208 /**
209 * @brief save a plugin into MENU_REF , calls insert_plugin or update_plugin if it already exists
210 * @return void
211 */
212 function save_plugin()
213 {
214 if ( $this->cn->get_value("select count(*) from menu_ref where me_code=$1",[$this->me_code]) > 0) {
215 $this->update_plugin();
216 } else {
217 $this->insert_plugin();
218
219 }
220 }
221 /**
222 * @brief Insert a plugin into the given profile, by default always insert into EXT
223 *
224 * @param type $p_id profile.p_id
225 * @throws Exception if duplicate or error db
226 */
227 function insert_plugin()
228 {
229 try
230 {
231 $this->cn->start();
232 $this->verify();
233 // check if duplicate
234 $this->me_code=strtoupper($this->me_code);
235 $count=$this->cn->get_value("select count(*) from menu_ref where me_code=$1", array($this->me_code));
236 if ($count!=0)
237 throw new Exception("Doublon");
238 $this->me_type='PL';
239 $this->insert();
240 $this->cn->commit();
241 }
242 catch (Exception $exc)
243 {
244 echo alert($exc->getMessage());
245 }
246 }
247
248 function update_plugin()
249 {
250 try
251 {
252 $this->cn->start();
253 $this->verify();
254 $this->me_type='PL';
255 $this->update();
256 $this->cn->commit();
257 }
258 catch (Exception $exc)
259 {
260 echo alert($exc->getMessage());
261 }
262 }
263
264 function remove_plugin()
265 {
266 try
267 {
268 $this->cn->start();
269 $this->delete();
270 $this->cn->commit();
271 }
272 catch (Exception $exc)
273 {
274 echo alert($exc->getMessage());
275 }
276 }
277
278 /**
279 * @brief remove all the standard plugins schema
280 * @param Database $p_cn
281 */
282 static function clean(Database $p_cn)
283 {
284 $a_ext=array("tva_belge", "amortissement", "impdol", "coprop", "importbank");
285 for ($i=0; $i<count($a_ext); $i++)
286 {
287 if ($p_cn->exist_schema($a_ext[$i]))
288 {
289 $p_cn->exec_sql("drop schema ".$a_ext[$i]." cascade");
290 }
291 }
292 }
293
294 /**
295 * @brief compare the version of the plugin and the last version , propose to update it if a new version exists
296 * @todo add a mechanism to check once a day
297 * @global User $g_user
298 * @global number $version_plugin
299 */
300 static function check_plugin_version($p_plugin_code)
301 {
302 global $g_user, $version_plugin;
303 if ($g_user->Admin()==1)
304 {
305 if ( in_array($p_plugin_code, self::aStandard_plugin) && SITE_UPDATE_PLUGIN!="")
306 {
307 $update=@file_get_contents(SITE_UPDATE_PLUGIN);
308 if ($update>$version_plugin)
309 {
310 echo '<div id="version_plugin_div_id" class="inner_box" style="position:absolute;zindex:2;top:5px;left:37.5%;width:25%">';
311 echo '<p class="notice">';
312 echo "Mise à jour disponible des plugins pour NOALYSS, version actuelle : $update votre version $version_plugin";
313 echo '</p>';
314 echo '<p style="text-align:center">'.
315 '<a id="version_plugin_button" class="button" onclick="$(\'version_plugin_div_id\').remove()">'.
316 _('Fermer').
317 "</a></p>";
318 echo '</div>';
319 }
320 }
321 }
322 }
323
324 /**
325 * Check that the xml contains all the needed information to change them into
326 * a extension, the exception code is 0 if the element is optional
327 * @brief Check XML.
328 * @param SimpleXMLElement $xml
329 * @throws Exception
330 */
331 function check_xml(SimpleXMLElement $xml)
332 {
333 try
334 {
335 if (!isset($xml->plugin))
336 throw new Exception(_('Manque plugin'), 1);
337 $nb_plugin=count($xml->plugin);
338
339 for ($i=0; $i<$nb_plugin; $i++)
340 {
341 if (!isset($xml->plugin[$i]->name))
342 throw new Exception(_('Manque nom'), 1);
343 if (!isset($xml->plugin[$i]->description))
344 throw new Exception(_('Manque description'), 0);
345 if (!isset($xml->plugin[$i]->code))
346 throw new Exception(_('Manque code'), 1);
347 if (!isset($xml->plugin[$i]->author))
348 throw new Exception(_('Manque auteur'), 0);
349 if (!isset($xml->plugin[$i]->root))
350 throw new Exception(_('Manque répertoire racine'), 1);
351 if (!isset($xml->plugin[$i]->file))
352 throw new Exception(_('Manque fichier à inclure'), 1);
353 if (!isset($xml->plugin[$i]->version))
354 throw new Exception(_("Manque version de l'extension"), 1);
355
356 if (!isset($xml->plugin[$i]->depend))
357 $xml->plugin[$i]->depend="EXT";
358 if (!isset($xml->plugin[$i]->order))
359 $xml->plugin[$i]->order=9000;
360 }
361 }
362 catch (Exception $ex)
363 {
364 throw $ex;
365 }
366 }
367
368 /**
369 * @brief Parse a XML file to complete an array of extension objects, in the plugin.xml file , you can find
370 * several plugins sharing some parts.
371 *
372 * @param string $p_file filename
373 * @return array array of Extension
374 */
375 static function read_definition($p_file)
376 {
377 global $cn;
378 $dom=new DomDocument('1.0');
379 $dom->load($p_file);
380 $xml=simplexml_import_dom($dom);
381 $nb_plugin=count($xml->plugin);
382 $a_extension=array();
383 for ($i=0; $i<$nb_plugin; $i++)
384 {
385
387 try
388 {
389 $extension->check_xml($xml);
390 }
391 catch (Exception $ex)
392 {
393 echo_warning($ex->getMessage());
394 if ($ex->getCode()==1)
395 {
396 continue;
397 }
398 }
399 $extension->me_file=trim($xml->plugin[$i]->root).'/'.trim($xml->plugin[$i]->file);
400 $extension->me_code=trim($xml->plugin[$i]->code);
401 $extension->me_description=(isset($xml->plugin[$i]->description))?trim($xml->plugin[$i]->description):"";
402 $extension->me_description_etendue=(trim($xml->plugin[$i]->author))?trim($xml->plugin[$i]->author):"";
403 $extension->me_type='PL';
404 $extension->me_menu=trim($xml->plugin[$i]->name);
405 $extension->me_parameter='plugin_code='.trim($xml->plugin[$i]->code);
406 $extension->depend=(isset($xml->plugin[$i]->depend))?trim($xml->plugin[$i]->depend):"EXT";
407 $extension->order=(isset($xml->plugin[$i]->order))?trim($xml->plugin[$i]->order):9000;
408 $extension->version=trim($xml->plugin[$i]->version);
409 $extension->noalyss_version=(isset($xml->plugin[$i]->noalyss_version))?trim($xml->plugin[$i]->noalyss_version):8000;
410 $a_extension[]=clone $extension;
411 }
412 return $a_extension;
413 }
414
415 /**
416 * @brief find the extension with the me_code = last part of access_code
417 * @param $a_extension
418 * @param $access_code find the ME_CODE (normally last part )
419 * @return the extension or null
420 */
421 public static function find_extension_code($a_extension,$access_code):Extension|null
422 {
423 $a_me_code=explode("/", $access_code);
424 if (empty($a_me_code ) ) return null;
425 $nb_me_code=count($a_me_code);
426 $me_code=$a_me_code[$nb_me_code-1];
427 foreach ($a_extension as $extension) {
428 if ($extension->me_code==$me_code) return $extension;
429 }
430 return null;
431 }
432
433 public function __toString(): string
434 {
435 $r = "";
436 $r .= " me_code " . $this->me_code.PHP_EOL;
437 $r .= " me_menu.".$this->me_menu.PHP_EOL;
438 $r .= " version".$this->version.PHP_EOL;
439 $r .= " noalyss_version".$this->noalyss_version.PHP_EOL;
440 $r .= " me_file" . $this->me_file.PHP_EOL;
441 $r .= " me_url" . $this->me_url.PHP_EOL;
442 $r .= " me_description" . $this->me_description.PHP_EOL;
443 $r .= " me_parameter" . $this->me_parameter.PHP_EOL;
444 $r .= " me_javascript" . $this->me_javascript.PHP_EOL;
445 $r .= " me_type" . $this->me_type.PHP_EOL;
446 $r .= " me_descrition_etendue" . $this->me_description_etendue.PHP_EOL;
447 $r .= " noalyss_version " . $this->noalyss_version.PHP_EOL;
448 $r .= " version " . $this->version.PHP_EOL;
449 $r .= " order " . $this->order.PHP_EOL;
450 $r .= " depend " . $this->depend.PHP_EOL;
451 return "Extension $r";
452 }
453
454 /**
455 * @brief retrieve the version of the current plugin
456 * @param $xml_file always __DIR__."/plugin.xml"
457 * @param $plugin_code the plugin or $_REQUEST['ac']
458 * @return int version or -1 if not found
459 */
460 public static function get_version($xml_file,$plugin_code):int
461 {
462 $aExtension=\Extension::read_definition($xml_file);
463 $extension=self::find_extension_code($aExtension, $plugin_code);
464 if ( empty($plugin_code)) return -1;
465 return $extension->version??0;
466 }
467
468
469}
echo_warning($p_string)
warns
alert($p_msg, $buffer=false)
alert in javascript
global $g_user
if no group available , then stop
switch($op2) $xml
catch(\Exception $e) $extension
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$input_from cn
_("actif, passif,charge,...")
$ex
font $a_plugin[ $e] depend
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)
save_plugin()
save a plugin into MENU_REF , calls insert_plugin or update_plugin if it already exists
static clean(Database $p_cn)
remove all the standard plugins schema
$noalyss_version
minimum version of NOALYSS for this plugin
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
$order
$order in the menu (can be override by config menu)
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 ,...
$depend
default depending menu (can be override by config menu)
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.
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:412