noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_extension.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   NOALYSS is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00021 
00022 /*!\file
00023  * \brief the extension class manages the plugins for the security, the access
00024  * the inclusion...
00025  */
00026 /*!\brief manage the extension, it involves the table extension
00027  *
00028  * Data member
00029  * - $cn database connection
00030  * - $variable :
00031  *    -  id (extension.ex_id)
00032  *    -  name (extension.ex_name)
00033  *    - plugin_code (extension.ex_code)
00034  *    - desc (extension.ex_desc)
00035  *    - enable (extension.ex_enable)
00036  *    - filepath (extension.ex_file)
00037  */
00038 require_once 'class_menu_ref_sql.php';
00039 include_once 'class_profile_sql.php';
00040 require_once 'class_menu_ref.php';
00041 require_once 'class_profile_menu.php';
00042 
00043 class Extension extends Menu_Ref_sql
00044 {
00045     public function verify()
00046     {
00047         // Verify that the elt we want to add is correct
00048         if (trim($this->me_code)=="") throw new Exception('Le code ne peut pas être vide');
00049         if (trim($this->me_menu)=="") throw new Exception('Le nom ne peut pas être vide');
00050         if (trim($this->me_file)=="") throw new Exception('Chemin incorrect');
00051         if (file_exists('..'.DIRECTORY_SEPARATOR.'include'.DIRECTORY_SEPARATOR.'ext'.DIRECTORY_SEPARATOR.$this->me_file) == false)
00052             throw new Exception ('Extension non trouvée, le chemin est-il correct?');
00053     }
00054     /*!@brief search a extension, the what is the column (extends_code */
00055     function search($p_what)
00056     {
00057                 $this->me_code=strtoupper($p_what);
00058                 if ( $this->load() == -1) return null;
00059                 return 1;
00060     }
00061     /*!\brief return 1 if the user given in parameter can access this extension
00062      * otherwise returns 0
00063      *\param $p_login the user login
00064      *\return 1 has access, 0 has no access
00065      */
00066     function can_request($p_login)
00067     {
00068                 $cnt=$this->cn->get_value("select count(*) from menu_ref
00069                                                                                 join profile_menu using (me_code)
00070                                                                                 join profile_user using (p_id)
00071                                                                                 where
00072                                                                                 me_code=$1
00073                                                                                 and user_name=$2",
00074                                                                 array($this->me_code,$p_login));
00075                 if ( $cnt > 0)        return 1;
00076                 return 0;
00077     }
00078     /*!@brief make an array of the available plugin for the current user
00079      * @return  an array
00080      *@see ISelect
00081      */
00082     static function make_array($cn)
00083     {
00084         $sql="select DISTINCT me_code as value, me_menu as label from ".
00085              " menu_ref join profile_menu using (me_code)
00086                                  join profile_user using (p_id) where ".
00087              " user_name=$1 and me_type='PL' ORDER BY ME_MENU";
00088         $a=$cn->get_array($sql,array($_SESSION['g_user']));
00089         return $a;
00090     }
00091     static function check_version($i)
00092     {
00093         global $version_noalyss;
00094         if ( ! isset ($version_noalyss) || $version_noalyss < $i )
00095         {
00096             alert('Cette extension ne fonctionne pas sur cette version de NOALYSS'.
00097                   ' Veuillez mettre votre programme a jour. Version minimum '.$i);
00098             return;
00099         }
00100         Extension::check_plugin_version();
00101     }
00102     /**
00103      * insert into the table profile_menu for the given profile id and depending
00104      * of the module $p_module
00105      * @global type $cn
00106      * @param type $p_id profile.p_id
00107      * @param type $p_module menu_ref.me_code
00108      * @throws Exception 10 : profile absent 20 module absent
00109      */
00110     function insert_profile_menu($p_id=1,$p_module='EXT')
00111     {
00112         global $cn;
00113         //profile exists ?
00114         $profile=new Profile_sql($cn,$p_id);
00115         if ( $profile->p_id != $p_id) {
00116                 throw new Exception(_('Profil inexistant'),10);
00117         }
00118         // Menu exists
00119         $module=new Menu_Ref($cn,$p_module);
00120         if ($module->me_code==null) {
00121                 throw new Exception(_('Module inexistant'),20);
00122         }
00123         
00124         $profil_menu=new Profile_Menu($cn);
00125         $profil_menu->me_code=$this->me_code;
00126         $profil_menu->me_code_dep=$p_module;
00127         $profil_menu->p_type_display='S';
00128         $profil_menu->p_id=$p_id;
00129         $cnt=$profil_menu->count(' where p_id=$1 and me_code = $2',array($p_id,$this->me_code));
00130         if ( $cnt==0) {
00131             $profil_menu->insert();
00132         }
00133 
00134         
00135     }
00136     function remove_from_profile_menu($p_id)
00137     {
00138         global $cn;
00139        
00140          $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',array($this->me_code,$this->me_file,$p_id));
00141         
00142     }
00143     /**
00144      * Insert a plugin into the given profile, by default always insert into EXT
00145      * 
00146      * @param type $p_id profile.p_id
00147      * @throws Exception if duplicate or error db
00148      */
00149         function insert_plugin()
00150         {
00151                 try
00152                 {
00153                         $this->cn->start();
00154                         $this->verify();
00155                         // check if duplicate
00156                         $this->me_code = strtoupper($this->me_code);
00157                         $count = $this->cn->get_value("select count(*) from menu_ref where me_code=$1", array($this->me_code));
00158                         if ($count != 0)
00159                                 throw new Exception("Doublon");
00160                         $this->me_type = 'PL';
00161                         $this->insert();
00162                         $this->cn->commit();
00163                 }
00164                 catch (Exception $exc)
00165                 {
00166                         echo alert($exc->getMessage());
00167                 }
00168         }
00169         function update_plugin()
00170         {
00171                 try
00172                 {
00173                         $this->cn->start();
00174                         $this->verify();
00175                         $this->me_type = 'PL';
00176                         $this->update();
00177                         $this->cn->commit();
00178                 }
00179                 catch (Exception $exc)
00180                 {
00181                         echo alert($exc->getMessage());
00182                 }
00183         }
00184         function remove_plugin()
00185         {
00186                 try
00187                 {
00188                         $this->cn->start();
00189                         $this->delete();
00190                         $this->cn->commit();
00191                 }
00192                 catch (Exception $exc)
00193                 {
00194                         echo alert($exc->getMessage());
00195                 }
00196         }
00197         /**
00198          *remove all the schema from the plugins
00199          * @param Database $p_cn
00200          */
00201         static function clean(Database $p_cn)
00202         {
00203                 $a_ext=array("tva_belge","amortissement","impdol","coprop","importbank");
00204                 for($i=0;$i<count($a_ext);$i++){
00205                         if ($p_cn->exist_schema($a_ext[$i])) {
00206                                 $p_cn->exec_sql("drop schema ".$a_ext[$i]." cascade");
00207                         }
00208                 }
00209         }
00210         static function check_plugin_version()
00211         {
00212             global $g_user,$version_plugin;
00213             if ($g_user->Admin() == 1)
00214             {
00215                 if (SITE_UPDATE_PLUGIN != "")
00216                 {
00217                     $update = @file_get_contents(SITE_UPDATE_PLUGIN);
00218                     if ($update > $version_plugin)
00219                     {
00220                         echo '<div class="inner_box" style="position:absolute;zindex:2;top:5px;left:360px">';
00221                         echo '<p class="notice">';
00222                         echo "Mise à jour disponible des plugins pour NOALYSS, version actuelle : $update votre version $version_plugin";
00223                         echo '</p>';
00224                         echo '</div>';
00225                     }
00226                 }
00227             }
00228         }
00229         /**
00230          * Check that the xml contains all the needed information to change them into
00231          * a extension, the exception code is 0 if the element is optional
00232          * @brief Check XML.
00233          * @param SimpleXMLElement $xml
00234          * @throws Exception
00235          */
00236         function check_xml(SimpleXMLElement $xml)
00237         {
00238             try {
00239                 if ( !isset ($xml->plugin)) throw new Exception(_('Manque plugin'),1);
00240                 $nb_plugin=count($xml->plugin);
00241             
00242                 for ($i=0;$i<$nb_plugin;$i++)
00243                 {
00244                     if ( !isset ($xml->plugin[$i]->name)) throw new Exception(_('Manque nom'),1);
00245                     if ( !isset ($xml->plugin[$i]->description)) throw new Exception(_('Manque description'),0);
00246                     if ( !isset ($xml->plugin[$i]->code)) throw new Exception(_('Manque code'),1);
00247                     if ( !isset ($xml->plugin[$i]->author)) throw new Exception(_('Manque auteur'),0);
00248                     if ( !isset ($xml->plugin[$i]->root)) throw new Exception(_('Manque répertoire racine'),1);
00249                     if ( !isset ($xml->plugin[$i]->file)) throw new Exception(_('Manque fichier à inclure'),1);
00250                 }
00251             } catch (Exception $ex) {
00252                 throw $ex;
00253             }
00254         }
00255          /**
00256          * Parse a XML file to complete an array of extension objects
00257          * @brief Create extension from XML.
00258          * @param type $p_file filename
00259          * @return array of Extension
00260          */
00261         static function read_definition($p_file)
00262         {
00263             global $cn;
00264             $dom=new DomDocument('1.0');
00265             $dom->load($p_file);
00266             $xml=simplexml_import_dom($dom);
00267             $nb_plugin=count($xml->plugin);
00268             $a_extension=array();
00269             for ($i=0;$i<$nb_plugin;$i++)
00270             {
00271                 
00272                 $extension=new Extension($cn);
00273                 try {
00274                         $extension->check_xml($xml);
00275                 } catch (Exception $ex) {
00276                     echo_warning($e->getMessage());
00277                     if ( $ex->getCode()==1) {
00278                         continue;
00279                     }
00280                     
00281                 }
00282                 $extension->me_file=trim($xml->plugin[$i]->root).'/'.trim($xml->plugin[$i]->file);
00283                 $extension->me_code=trim($xml->plugin[$i]->code);
00284                 $extension->me_description=(isset ($xml->plugin[$i]->description))?$xml->plugin[$i]->description:"";
00285                 $extension->me_description_etendue=($xml->plugin[$i]->author)?$xml->plugin[$i]->author:"";
00286                 $extension->me_type='PL';
00287                 $extension->me_menu=$xml->plugin[$i]->name;
00288                 $extension->me_parameter='plugin_code='.trim($xml->plugin[$i]->code);
00289                 $a_extension[]=clone $extension;
00290             }
00291             return $a_extension;
00292         }
00293 }
00294 
 All Data Structures Namespaces Files Functions Variables Enumerations