<?php
# ************** LICENCE ****************
/*
	Copyright (c) PORTANEO.
*/
# ***************************************
# POSH Admin mobile- change user access to mobile version
#
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
# ***************************************

if (!isset($_POST["action"])) exit();
if ($_POST["action"] == "grant" && !isset($_POST["id"])) exit();

$folder="../includes/plugins/mobile/admin/";
$not_access=1;
$granted="A";
$pagename="../includes/plugins/mobile/admin/scr_update_user_access.php";

//includes
require('includes.php');
require_once('../../../../includes/xml.inc.php');

//control that the current administrator has access to the script
$grantedCalls = array("userstab");
require('../../../../admin/adm_ctrl_access.php');

$usersId="";
if ($_SESSION['user_id'] != 1) {
    $groupList="";
    $DB->getResults($adm_getGroupId,$DB->escape($_SESSION['user_id']));
    while ($row = $DB->fetch(0)) {
        $groupList.="'".$row['group_id']."',";
    }
    $groupList=substr($groupList,0,strlen($groupList)-1);
    $DB->freeResults();

    $DB->getResults($adm_mob_getUserIdByGroupRestricted,$groupList);
    while ($row = $DB->fetch(0)) {
        $usersId.="'".$row["id"]."',";
    }
    $usersId=substr($usersId,0,strlen($usersId)-1);
    $DB->freeResults();
}

function grantUser($id,$access) {
    global $DB;    
    global $users_mob_updateUserAccess;    
    global $users_mob_addUserAccess;    
    if ($access == 0) {
        $DB->execute($users_mob_updateUserAccess,$DB->escape($access),$DB->escape($id));
    }
    else {
        $DB->execute($users_mob_updateUserAccess,$DB->escape($access),$DB->escape($id));
        if ($DB->nbAffected()==0) $DB->execute($users_mob_addUserAccess,$DB->escape($id));
    }    
}

function blockAll() {
    global $DB, 
           $users_mob_removeAllAccess, 
           $adm_getGroupId, 
           $groupList, 
           $usersId, 
           $users_mob_removeAllAccessRestricted;
    if ($_SESSION['user_id']==1) {
        $DB->execute($users_mob_removeAllAccess);
    }   
    else {
        $DB->execute($users_mob_removeAllAccessRestricted,$usersId);
    }
}

function grantAll() {
    global $DB,  
           $users_mob_removeAllAccess, 
           $users_mob_addAllAccess, 
           $groupList, 
           $usersId, 
           $users_mob_removeAllAccessRestricted,
           $users_mob_addAllAccessRestricted;
    if ($_SESSION['user_id']==1) {
        $DB->execute($users_mob_removeAllAccess);
        $DB->execute($users_mob_addAllAccess);
    }
    else {
        $DB->execute($users_mob_removeAllAccessRestricted,$usersId);
        $DB->execute($users_mob_addAllAccessRestricted,$groupList);
    }
}

$file=new xmlFile();

$file->header();

$action = $_POST["action"];

switch ($action) {
    case 'grant':
        $id = $_POST["id"];
        $access = $_POST["access"];
        grantUser($id,$access);
    break;
    case 'blockall':
        blockAll();
    break;
    case 'grantall':  
        grantAll();
    break;
}

$file->status(1);

$file->footer();
?>