Location: PHPKode > projects > cPassMan > cpassman_1.55/sources/main.functions.php
<?php
####################################################################################################
## File : main.functions.php
## Author : Nils Laumaillé
## Description : File contains several needed functions
## 
## DON'T CHANGE !!!
## 
####################################################################################################

# FUNCTION permits to
# crypt a string
#
function encrypt($text, $personal_salt="")
{
    if ( !empty($personal_salt) )
        return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $personal_salt, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
    else
        return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}

# FUNCTION permits to
# decrypt a crypted string
#
function decrypt($text, $personal_salt="")
{
    if ( !empty($personal_salt) )
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $personal_salt, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
    else
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
} 

# FUNCTION permits to
# trim a string depending on a specific string
#
function TrimElement($chaine,$element){
    $chaine = trim($chaine);
    if ( substr($chaine,0,1) == $element ) $chaine = substr($chaine,1);
    if ( substr($chaine,strlen($chaine)-1,1) == $element ) $chaine = substr($chaine,0,strlen($chaine)-1);
    return $chaine;
}

# FUNCTION permits to
# refresh the rights of the actual user
#
function IdentificationDesDroits($groupes_visibles_user,$groupes_interdits_user,$is_admin,$id_fonctions,$refresh){   
    global $server, $user, $pass, $database, $pre;
    
    //include librairies 
    require_once ("../sources/NestedTree.class.php");
    require_once("class.database.php"); 
    $db = new Database($server, $user, $pass, $database, $pre);
    $db->connect(); 
    
    //Check if user is GOD
    if ( $is_admin == 1 ){
        $groupes_visibles = array();
        $_SESSION['groupes_visibles'] = array();
        $_SESSION['groupes_interdits'] = array();
        $_SESSION['personal_visible_groups'] = array();
        $_SESSION['groupes_visibles_list'] = "";
        $rows = $db->fetch_all_array("SELECT id FROM ".$pre."nested_tree WHERE personal_folder = '0'");
        foreach($rows as $record){
            array_push($groupes_visibles,$record['id']);
        }
        $_SESSION['groupes_visibles'] = $groupes_visibles;
        
        //Get ID of personal folder
        $pf = $db->fetch_array("SELECT id FROM ".$pre."nested_tree WHERE title = '".$_SESSION['user_id']."'");
        if ( !empty($pf[0]) ){
            if ( !in_array($pf[0],$_SESSION['groupes_visibles']) ){
                array_push($_SESSION['groupes_visibles'],$pf[0]);
                array_push($_SESSION['personal_visible_groups'],$pf[0]);
                //get all descendants
                $tree = new NestedTree($pre.'nested_tree', 'id', 'parent_id', 'title', 'personal_folder');
                $tree->rebuild();
                $tst = $tree->getDescendants($pf[0]);
                foreach($tst as $t){
                    array_push($_SESSION['groupes_visibles'],$t->id);
                    array_push($_SESSION['personal_visible_groups'],$t->id);
                }
            }
        }
        
        $_SESSION['groupes_visibles_list'] = implode(',',$_SESSION['groupes_visibles']);
        $_SESSION['is_admin'] = $is_admin;
        
        //Check if admin has creating Folders and Roles
        $ret = $db->fetch_row("SELECT COUNT(*) FROM ".$pre."nested_tree");
        $_SESSION['nb_folders'] = $ret[0];
        $ret = $db->fetch_row("SELECT COUNT(*) FROM ".$pre."functions");
        $_SESSION['nb_roles'] = $ret[0];
        
    }else{
        //init
        $_SESSION['groupes_visibles'] = array();
        $_SESSION['groupes_interdits'] = array();
        $_SESSION['personal_visible_groups'] = array();
        $groupes_visibles = array();
        $groupes_interdits = array();
        if ( !empty($groupes_interdits_user) && count($groupes_interdits_user)>0 ) $groupes_interdits = $groupes_interdits_user;
        $_SESSION['is_admin'] = $is_admin;
        $fonctions_associees = explode(';',TrimElement($id_fonctions,";"));
        $new_liste_gp_visibles = array();
        $liste_gp_interdits = array();
        
        //build Tree
        require_once ("NestedTree.class.php");
        $tree = new NestedTree($pre.'nested_tree', 'id', 'parent_id', 'title');    
            
        //rechercher tous les groupes visibles en fonction des fonctions associées à l'utilisateur
        foreach($fonctions_associees as $fonc_id){
            if ( !empty($fonc_id) ){
                $data = $db->query_first("SELECT groupes_visibles,groupes_interdits FROM ".$pre."functions WHERE id=".$fonc_id);
                $gp_visibles_tmp = explode(';',TrimElement($data['groupes_visibles'],";"));
                $gp_interdits_tmp = explode(';',TrimElement($data['groupes_interdits'],";"));
                
                //gérer les groupes visibles
                if (!empty($data['groupes_visibles']) ){
                    foreach($gp_visibles_tmp as $gp_id_visible){    #echo " - id visible : ".$gp_id_visible.";";
                        //récupérer tous les sous groupes
                        $mytree = $tree->getDescendants($gp_id_visible,true);
                        foreach($mytree as $t){
                            if ( !in_array($t->id,$groupes_interdits) && !in_array($t->id,$groupes_visibles) )
                                array_push($groupes_visibles,$t->id); #ne pas rajouter comme visibles si ce groupe est interdit
                        }
                    }
                }
                                
                //gérer les groupes interdits
                if (!empty($data['groupes_interdits']) ){
                    foreach($gp_interdits_tmp as $gp_id_interdit){
                        //supprimer tous les sous groupes
                        $mytree = $tree->getDescendants($gp_id_interdit,true);
                        foreach($mytree as $t){
                             if ( !in_array($t->id,$liste_gp_interdits) )array_push($liste_gp_interdits,$t->id);
                        }
                    }
                }

                //merger les 2 tableaux
                foreach($groupes_visibles as $gpv){
                    if ( !in_array($gpv,$liste_gp_interdits) )array_push($new_liste_gp_visibles,$gpv);
                }

                //ajouter les groupes spécifiques à l'utilisateurs
                $groupes_visibles_by_user = explode(';',$groupes_visibles_user);
                foreach($groupes_visibles_by_user as $id_visible){
                    if ( !in_array($id_visible,$new_liste_gp_visibles) ) array_push($new_liste_gp_visibles,$id_visible);
                }      
            }
        }
        //Clean array
        $array = array_unique($new_liste_gp_visibles);
        foreach($array as $key => $value) {
          if($value == "") {
            unset($array[$key]);
          }
        }  
        
        $_SESSION['groupes_visibles'] = array_values($array);                              
        
        //Get ID of personal folder
        if ( isset($_SESSION['settings']['enable_pf_feature']) && $_SESSION['settings']['enable_pf_feature'] == 1 ) {
            $pf = $db->fetch_row("SELECT id FROM ".$pre."nested_tree WHERE title = '".$_SESSION['user_id']."'");
            if ( !empty($pf[0]) ){
                if ( !in_array($pf[0],$_SESSION['groupes_visibles']) ){
                    array_push($_SESSION['groupes_visibles'],$pf[0]);
                    array_push($_SESSION['personal_visible_groups'],$pf[0]);
                    //get all descendants
                    $tree = new NestedTree($pre.'nested_tree', 'id', 'parent_id', 'title', 'personal_folder');
                    $tree->rebuild();
                    $tst = $tree->getDescendants($pf[0]);
                    foreach($tst as $t){
                        array_push($_SESSION['groupes_visibles'],$t->id);
                        array_push($_SESSION['personal_visible_groups'],$t->id);
                    }
                }
            }
        }

        $_SESSION['groupes_visibles_list'] = implode(',',$_SESSION['groupes_visibles']);
    }
}


#################
#### FUNCTION permits to log events into DB
#################
function logEvents($type, $label, $who){
    global $server, $user, $pass, $database, $pre;
    
    //include librairies & connect to DB
    require_once("class.database.php"); 
    $db = new Database($server, $user, $pass, $database, $pre);
    $db->connect(); 
    
    $db->query_insert(
        "log_system",
        array(
            'type' => $type,
            'date' => mktime(date('h'),date('i'),date('s'),date('m'),date('d'),date('y')),
            'label' => $label,
            'qui' => $who
        )
    );
}

#################
#### FUNCTION permits to suppress all "return to line" characters from string
#################
function CleanString($string){
    return str_replace(array(CHR(10),CHR(13)),array(' ',' '),$string);
}

#################
#### FUNCTION permits to update the CACHE table
#################
function UpdateCacheTable($action, $id=""){
    global $db, $server, $user, $pass, $database, $pre;
    //Rebuild full cache table
    if ( $action == "reload"){
        //truncate table
        $db->query("TRUNCATE TABLE ".$pre."cache");

        //reload date
        $sql = "SELECT *
                FROM ".$pre."items
                WHERE inactif=0";
        $rows = $db->fetch_all_array($sql);
        foreach( $rows as $reccord ){
            //Get all TAGS
            $tags = "";
            $item_tags = $db->fetch_all_array("SELECT tag FROM ".$pre."tags WHERE item_id=".$reccord['id']);
            foreach( $item_tags as $item_tag ){
                if ( !empty($item_tag['tag']))
                    $tags .= $item_tag['tag']. " ";
            }
            //store data
            $db->query_insert(
                "cache",
                array(
                    'id'      =>  $reccord['id'],
                    'label'   =>  $reccord['label'],
                    'description'    =>  $reccord['description'],
                    'tags'    =>  $tags,
                    'id_tree' =>  $reccord['id_tree'],
                    'perso' =>  $reccord['perso'],
                    'restricted_to' =>  $reccord['restricted_to'],
                )
            );
        }
    //UPDATE an item
    }else if ( $action == "update_value"){
        //get new value from db
        $sql = "SELECT label, description, id_tree, perso, restricted_to
                FROM ".$pre."items
                WHERE id=".$id;
        $rows = $db->fetch_row($sql);

        //Get all TAGS
        $tags = "";
        $item_tags = $db->fetch_all_array("SELECT tag FROM ".$pre."tags WHERE item_id=".$id);
        foreach( $item_tags as $item_tag ){
            if ( !empty($item_tag['tag']))
                $tags .= $item_tag['tag']. " ";
        }

        //finaly update
        $db->query_update(
                "cache",
                array(
                    'label'   =>  $rows[0],
                    'description'    =>  $rows[1],
                    'tags'    =>  $tags,
                    'id_tree' =>  $rows[2],
                    'perso' =>  $rows[3],
                    'restricted_to' =>  $rows[4],
                ),
                "id='".$id."'"
            );
    //ADD an item
    }else if ( $action == "add_value"){
        //get new value from db
        $sql = "SELECT label, description, id_tree, perso, restricted_to, id
                FROM ".$pre."items
                WHERE id=".$id;
        $rows = $db->fetch_row($sql);

        //Get all TAGS
        $tags = "";
        $item_tags = $db->fetch_all_array("SELECT tag FROM ".$pre."tags WHERE item_id=".$id);
        foreach( $item_tags as $item_tag ){
            if ( !empty($item_tag['tag']))
                $tags .= $item_tag['tag']. " ";
        }

        //finaly update
        $db->query_insert(
            "cache",
            array(
                'id'   =>  $rows[5],
                'label'   =>  $rows[0],
                'description'    =>  $rows[1],
                'tags'    =>  $tags,
                'id_tree' =>  $rows[2],
                'perso' =>  $rows[3],
                'restricted_to' =>  $rows[4],
            )
        );
    //DELETE an item
    }else if ( $action == "delete_value"){
        mysql_query("DELETE FROM ".$pre."items WHERE id = ".$id);
    }
}
?>
Return current item: cPassMan