phpDocumentor VFront
Authentication
[ class tree: VFront ] [ index: VFront ] [ all elements ]

Source for file auth.php

Documentation is available at auth.php

  1. <?php
  2. #################################################
  3. #
  4. #     This file is part of VFront.
  5. #
  6. #    VFront 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. #    any later version.
  10. #
  11. #    VFront 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 this program.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19.  
  20.  
  21.  
  22.  
  23. /**
  24.  * Classe generica per l'autenticazione a VFront.
  25.  * L'accesso può essere in 1 o 2 passi
  26.  * In 1 passo se si usa l'autenticazione mediante il database di VFront
  27.  * In 2 mediante strumenti esterni (LDAP, altro DB, ecc) per l'autenticazione
  28.  * e il DB di VFRont per l'accreditamento dei diritti
  29.  * @package VFront
  30.  * @subpackage Authentication
  31.  * @author Mario Marcello Verona <marcelloverona@gmail.com>
  32.  * @copyright 2007 Mario Marcello Verona
  33.  * @version 0.90
  34.  * @license http://www.gnu.org/licenses/gpl.html GNU Public License
  35.  */
  36. class auth{
  37.     
  38.     /**
  39.     * @desc Utente oggetto dell'autenticazione
  40.     * @var string 
  41.     */
  42.     var $user;
  43.     
  44.     /**
  45.     * @desc Password oggetto dell'autenticazione
  46.     * @var string 
  47.     */
  48.     var $passw;
  49.     
  50.     /**
  51.     * Array restituito dalla classe
  52.     * 'response' => true | false
  53.     * 0 => (array) info_account | null
  54.     * @var array 
  55.     */
  56.     var $auth_obj = array('response'=>false);
  57.     
  58.     /**
  59.     * Presenza dell'utente nel frontend
  60.     * @var array 
  61.     */
  62.     var $utente_in_frontend = array('response'=>false);
  63.     
  64.     /**
  65.     * Autenticazione esterna, impostata nel file CONF
  66.     * @var string soap | ldap | db | db_ext | null=dbvfront
  67.     */
  68.     var $tipo_external_auth = '';  // soap | ldap | db | db_ext | null=dbvfront
  69.     
  70.     
  71.     /**
  72.     *    Tipo di autenticazione.
  73.     *     La modalità di accesso può essere in 2 step o 1 step.
  74.     *    Nella modalità 1 step l'autorizzazione e l'accreditamento sono congiunti,
  75.     *    nel 2 step sono separati
  76.     *     Variabile obsoleta, mantenuta per compatibilità
  77.     * @var string 1step | 2step
  78.     */
  79.     var $modalita_auth = ''// 1step | 2step
  80.     
  81.     
  82.     /**
  83.      * Autenticazione ed accreditamento dei diritti
  84.      *
  85.      * @param string $user Email dell'utente
  86.      * @param string $passw Password
  87.      * @return auth 
  88.      */
  89.     function auth($user,$passw)
  90.         
  91.         global $link,$db1,$conf_auth;
  92.         
  93.         
  94.         $this->tipo_external_auth = $conf_auth['tipo_external_auth'];
  95.         
  96.         $this->user = trim($user);
  97.         $this->passw = trim($passw);
  98.         
  99.         
  100.         switch($this->tipo_external_auth){
  101.             
  102.             case 'soap'require_once(FRONT_REALPATH.'/inc/auth.soap.php');
  103.                         $this->auth_obj = auth_soap($this->user $this->passw);
  104.                         $this->modalita_auth='2step';
  105.             break;
  106.             
  107.             case 'db'require_once(FRONT_REALPATH.'/inc/auth.db.php');
  108.                         $this->auth_obj = auth_db($this->user $this->passw);
  109.                         $this->modalita_auth='2step';
  110.             break;
  111.                 
  112.             case 'db_ext'require_once(FRONT_REALPATH.'/inc/auth.db_ext.php');
  113.                         $this->auth_obj = auth_db_ext($this->user $this->passw);
  114.                         $this->modalita_auth='2step';
  115.             break;
  116.             
  117.             case 'ldap'require_once(FRONT_REALPATH.'/inc/auth.ldap.php');
  118.                         $this->auth_obj = auth_ldap($this->user $this->passw);
  119.                         $this->modalita_auth='2step';
  120.             break;
  121.             
  122.             case null$this->modalita_auth='1step';
  123.                        $this->__step_1();
  124.         }
  125.         
  126.         
  127.         // l'uente non esiste, mandalo via
  128.         if($this->auth_obj['response']===false){
  129.             
  130.             
  131.             $this->__respingi_utente(0);
  132.         }
  133.         
  134.         // L'utente esiste in autorizzazione, ora esiste anche in DB? (2 passi)
  135.         else{
  136.             
  137.             if($this->tipo_external_auth!==null){
  138.             
  139.                 $this->__step_2();
  140.             }
  141.         
  142.         
  143.         }
  144.         
  145.     //-- Fine funzione AUTH
  146.     
  147.     
  148.     
  149.     
  150.     
  151.     
  152.     /**
  153.      * @desc Funzione di autenticazione basata sul DB di VFront, tabella utenti
  154.      *
  155.      */
  156.     function __step_1(){
  157.         
  158.         global $link,$db1;
  159.         
  160.                     // Verifico che esista nel db Frontend
  161.                     $this->utente_in_frontend = $this->__frontend_user($this->user $this->passw);
  162.                     
  163.                     // L'utente esiste anche nel DB!
  164.                     // prende i dati, li mette in sessione, lo fa andare avanti
  165.                     if($this->utente_in_frontend['response']){
  166.                         
  167.                         $this->__metti_in_sessione();
  168.                         header("Location: ".FRONT_DOCROOT."/index.php");
  169.                         exit;
  170.                     }
  171.                     else{
  172.                         
  173.                         $this->__respingi_utente(0);
  174.                         
  175.                     }
  176.     }
  177.     
  178.     
  179.     
  180.     
  181.     /**
  182.      * @desc Funzione di accreditamento e inserimento se non esistono i diritti
  183.      *
  184.      */
  185.     function __step_2(){
  186.         
  187.         global $link,$db1,$conf_auth;
  188.         
  189.         
  190.             // Verifico che esista nel db Frontend
  191.                 $this->utente_in_frontend = $this->__frontend_user($this->user $this->passw);
  192.                 
  193.                     // L'utente esiste anche nel DB!
  194.                     // prende i dati, li mette in sessione, lo fa andare avanti
  195.                     if($this->utente_in_frontend['response']){
  196.                         
  197.                         $this->__metti_in_sessione();
  198.                         header("Location: ".FRONT_DOCROOT."/index.php");
  199.                         exit;
  200.                     }
  201.                     
  202.                     // L'utente è autorizzato ma non c'è nel DB.
  203.                     // Prendo i dati a disposizioni e creo un record nel DB con livello di accesso default
  204.                     else{
  205.                         
  206.                             
  207.                             $sql_ins sprintf("INSERT INTO {$db1['frontend']}.utente 
  208.                                                 (nicknomecognomeemailpasswdgidlivellodata_ins)
  209.                                                 VALUES ('%s','%s','%s','%s','%s',%d,%d,'%s')"//,%d)",
  210.                                                 $this->auth_obj[0][$conf_auth['campo_email']],
  211.                                                 $this->auth_obj[0][$conf_auth['campo_nome']],
  212.                                                 $this->auth_obj[0][$conf_auth['campo_cognome']],
  213.                                                 $this->auth_obj[0][$conf_auth['campo_email']],  // email in questo caso = nick
  214.                                                 $this->auth_obj[0][$conf_auth['campo_password']]
  215.                                                 0// Imposta nel gruppo default 0
  216.                                                 1// Imposta il livello di amministrazione a 1 (nessuna amministrazione)
  217.                                                 date("Y-m-d")
  218.                                                 );
  219.                             
  220.                             // INSERISCE NEL DB
  221.                             $q_ins vmsql_query($sql_ins,$link);
  222.                             if(vmsql_affected_rows($link,$q_ins)!=1){
  223.                                 
  224.                                 openErrorGenerico('Errore di gestione autorizzazione',true);
  225.                                 exit;
  226.                             }
  227.                             
  228.                             
  229.                             
  230.                             
  231.                             
  232.                             else{
  233.                                 
  234.                                 $this->utente_in_frontend = $this->__frontend_user($this->user $this->passwtrue);
  235.                                 
  236.                                 $this->__metti_in_sessione();
  237.                                 
  238.                                 header("Location: ".FRONT_DOCROOT."/index.php");
  239.                                 exit;
  240.                             }
  241.                             
  242.                             
  243.                         
  244.                     
  245.         
  246.     }
  247.     
  248.     
  249.     
  250.     
  251.     
  252.     
  253.     /**
  254.      * Funzione di logout
  255.      *
  256.      * @param int $tipo_err 0=errore nell'utente password, 1=livello troppo basso per vedere la pagina
  257.      */
  258.     function __respingi_utente($tipo_err=0){
  259.         
  260.         switch($tipo_err){
  261.             
  262.             case 0// errore nell'utente password
  263.                 unset($_SESSION['user']);
  264.                 unset($_SESSION['gid']);                
  265.                 header("Location: ".FRONT_DOCROOT."/index.php?nolog")exit;
  266.             break;
  267.             
  268.             
  269.             case 1// livello troppo basso per vedere la pagina
  270.                 header("Location: ".FRONT_DOCROOT."/index.php?accesso_vietato=1")exit;
  271.             break;
  272.         }
  273.     }
  274.     
  275.     
  276.     
  277.     
  278.     
  279.     
  280.     /**
  281.      * @desc Cerca l'utente nel DB di VFront
  282.      * @param string $user Nome utente
  283.      * @param string $passw Password
  284.      * @param bool $use_passw Utilizza la password per l'autenticazione (default=true)
  285.      * @return array Array con le informazioni sull'utente
  286.      */
  287.     function __frontend_user($user,$passw='',$use_passw=true){
  288.         
  289.         global $link,$db1;
  290.         
  291.         // cerca l'utente in database
  292.         $sql "SELECT * FROM {$db1['frontend']}.utente 
  293.                 WHERE email='".addslashes($user)."' ";
  294.         
  295.         if($use_passw)    $sql." AND passwd='".md5($passw)."'";
  296.         
  297.         $sql.=" LIMIT 1";
  298.         
  299.         $qvmsql_query($sql,$link);
  300.         
  301.         // L'utente esiste anche nel DB!
  302.         // prende i dati, li mette in sessione, lo fa andare avanti
  303.         if(vmsql_num_rows($q)==1){
  304.             
  305.             $RSvmsql_fetch_assoc($q);
  306.             
  307.             return array('response'=>true,$RS);
  308.         }
  309.         
  310.         // L'utente è autorizzato ma non c'è nel DB.
  311.         else{
  312.             
  313.             return array('response'=>false);
  314.         }
  315.         
  316.     }
  317.     
  318.     
  319.     
  320.     
  321.     
  322.     
  323.     /**
  324.      * @desc Inserisce in sessione le variabili relative all'utente quando autenticato e accreditato
  325.      *
  326.      */
  327.     function __metti_in_sessione(){
  328.         
  329.         $_SESSION['user']=array(
  330.                         'uid'    => $this->utente_in_frontend[0]['id_utente'],
  331.                         'nick'    => $this->utente_in_frontend[0]['nick'],
  332.                         'email'    => $this->utente_in_frontend[0]['email'],
  333.                         'nome'    => $this->utente_in_frontend[0]['nome'],
  334.                         'cognome'=>$this->utente_in_frontend[0]['cognome'],
  335.                         'data_ins'=>$this->utente_in_frontend[0]['data_ins'],
  336.                         'gid'     =>$this->utente_in_frontend[0]['gid'],
  337.                         'livello' =>$this->utente_in_frontend[0]['livello']
  338.                         );
  339.                         
  340.         $_SESSION['gid']=$this->utente_in_frontend[0]['gid'];
  341.     }
  342. }
  343.  
  344.  
  345.  
  346.  
  347. ?>

Documentation generated on Sat, 22 Sep 2007 11:49:51 +0200 by phpDocumentor 1.4.0a2