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