<?php
/**
 *    This file is part of Mobile Assistant Connector.
 *
 *   Mobile Assistant Connector is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Mobile Assistant Connector is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Mobile Assistant Connector.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @author    eMagicOne <contact@emagicone.com>
 *  @copyright 2014-2017 eMagicOne
 *  @license   http://www.gnu.org/licenses   GNU General Public License
 */

//require_once '../../../config/config.inc.php';
//include_once 'functions.php';
//require_once 'for_ajax.php';

$key              = Tools::getValue('auth_key');
$function         = Tools::getValue('call_function');
$push_ids         = Tools::getValue('push_ids');
$push_id          = (int)Tools::getValue('push_id');
$value            = Tools::getValue('value');
$user_id          = (int)Tools::getValue('user_id');
$account_id       = (int)Tools::getValue('account_id');
$user_ids         = Tools::getValue('user_ids');
$selected_actions = (string)Tools::getValue('selected_actions');
$actions_codes    = (string)Tools::getValue('actions_codes');
$is_new_user      = Tools::getValue('is_new_user');
$login            = Tools::getValue('login');
$password         = Tools::getValue('password');

if (!isAuthenticated($key)) {
    die(Tools::jsonEncode('Authentication error'));
}

if ($function && function_exists($function)) {
    if ($function == 'changeStatus') {
        echo Tools::jsonEncode(changeStatus($account_id, $value));
    } elseif ($function == 'deletePush') {
        echo Tools::jsonEncode(deletePush($push_id));
    } elseif ($function == 'getUserPushes') {
        echo Tools::jsonEncode(getUserPushes($user_id));
    } elseif ($function == 'getActions') {
        echo Tools::jsonEncode(getActions($user_id));
    } elseif ($function == 'saveUserActions') {
        echo Tools::jsonEncode(saveUserActions($user_id, $selected_actions));
    } elseif ($function == 'getUserActions') {
        echo Tools::jsonEncode(getUserActions($user_id));
    } elseif ($function == 'getActionsByCodes') {
        echo Tools::jsonEncode(getActionsByCodes($actions_codes));
    } elseif ($function == 'changeStatusForUser') {
        echo Tools::jsonEncode(changeStatusForUser($user_ids, $value));
    } elseif ($function == 'deleteUser') {
        echo Tools::jsonEncode(deleteUser($user_ids));
    } elseif ($function == 'checkUserData') {
        echo Tools::jsonEncode(checkUserData($login, $user_id));
    } elseif ($function == 'saveUser') {
        echo Tools::jsonEncode(saveUser($user_id, $login, $password, $actions_codes, $value, $is_new_user));
    } elseif ($function == 'getUserByUsername') {
        echo Tools::jsonEncode(getUserByUsername($login));
    } elseif ($function == 'regenerateQrCodeHash') {
        echo Tools::jsonEncode(regenerateQrCodeHash($user_id));
    } else {
        echo Tools::jsonEncode(call_user_func($function));
    }
} else {
    die(Tools::jsonEncode('error'));
}
