/*******************************************************************************
* Copyright (c) 2015, 2017 Christopher W. Olsen <cwolsen@SpherePBX.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions, the following disclaimer and the list of authors.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions, the following disclaimer and the list of authors in
* the documentation and/or other materials provided with the distribution.
* Neither the name of the uBix Cube Project nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id: common.js 955 2017-09-08 19:56:02Z reddawg $
*
******************************************************************************/
// MrOlsen (2015-10-09) NOTE: In the future I need to switch to language files
// if i want this framework to be more universal.
var system_login;
var page_token;
var last_uri;
var aL_body = $("body");
var aL = 0;
/*
* MrOlsen (2015-10-02) NOTE: Auth Codes - 0 - Not Logged In 1 - Authenticated 2 -
* Session Timed Out 3 - Invalid User/Pass Combo 4 - Access Denied 5 - Logged
* Out
*/
$(document).on({
ajaxStart : function() {
if (aL == 0) {
$aL = 1;
aL_body.addClass("loading");
}
},
ajaxStop : function() {
aL_body.removeClass("loading");
aL = 0;
}
});
$(document).tooltip();
$(function() {
$("#auth_form").submit(function(event) {
event.preventDefault();
});
$("#auth_form_login").click(function() {
systemLogin()
});
function systemLogout() {
$.ajax({
type : "GET",
url : "/common/auth_json.php?logout",
dataType : 'json',
success : function(dJSON) {
system_login.dialog('open');
$(".validateTips").text(dJSON.message);
lastURI = dJSON.last_uri;
},
error : function() {
alert('There was problem communicating with the server, please reload your browser');
}
});
}
function systemLogin() {
$.ajax({
type : "POST",
url : "/common/auth_json.php",
dataType : "json",
data : $('#auth_form').serialize(),
success : function(dJSON) {
lastURI = dJSON.last_uri;
if (dJSON.authenticated) {
switch (dJSON.authenticated) {
case 0:
case '0':
case 3:
case '3':
$(".validateTips").text(dJSON.message);
break;
case 1:
case '1':
if (dJSON.return_uri != '') {
window.location.href = dJSON.return_uri;
} else {
if (dJSON.user_menu != '')
$("#_USER_MENU").html(dJSON.user_menu);
if (dJSON.admin_menu != '')
$("#_ADMIN_MENU").html(dJSON.admin_menu);
if (dJSON.user_info != '')
$("#_USER_INFO").html(dJSON.user_info);
page_token = dJSON.page_token;
system_login.dialog("close");
if (typeof _start === 'function')
_start();
}
break;
case 2:
case '2':
$(".validateTips").text(dJSON.message);
break;
case 4:
case '4':
$(".validateTips").text(dJSON.message);
break;
default:
alert('There was a problem communicating, please reload your browser');
break;
}
}
},
error : function() {
alert('There was a problem communicating, please reload your browser');
}
});
}
/* Construct Login Modal Element */
system_login = $("#login_modal").dialog({
autoOpen : false,
height : 325,
width : 550,
modal : true,
buttons : {
"Login" : systemLogin,
"Fogot Password" : function() {
system_login.dialog("close");
},
"Go Back" : function() {
$(location).attr('href', lastURI);
}
},
open : function() { /* Hide The Close Button So You Have To Login */
$(this).parent().find(".ui-dialog-titlebar-close").hide();
}
});
/* Check Login */
function checkLogin() {
$.ajax({
type : "GET",
url : "/common/auth_json.php",
dataType : 'json',
success : function(dJSON) {
if (dJSON.authenticated && dJSON.authenticated == 1) {
page_token = dJSON.page_token;
if (dJSON.user_menu != '')
$("#_USER_MENU").html(dJSON.user_menu);
if (dJSON.admin_menu != '')
$("#_ADMIN_MENU").html(dJSON.admin_menu);
if (dJSON.user_info != '')
$("#_USER_INFO").html(dJSON.user_info);
if (typeof _start === 'function')
_start();
} else {
system_login.dialog('open');
$(".validateTips").text(dJSON.message);
lastURI = dJSON.last_uri;
}
},
error : function() {
alert('There was problem communicating with the server, please reload your browser');
}
});
}
checkLogin();
});
function _AJAX(post_url, post_data, success_func, error_func) {
var rData = true;
error_func = error_func || function() {
alert('There was a problem communicating, please reload your browser');
}
if (!post_data || post_data == '')
post_data = 'data[page_token]=' + page_token;
else
post_data = post_data + '&data[page_token]=' + page_token;
if (!success_func)
cB = false;
else
cB = true;
$.ajax({
type : "POST",
url : post_url,
data : post_data,
async : cB,
dataType : 'json',
success : function(dJSON) {
if (dJSON.authenticated && dJSON.authenticated != 1) {
system_login.dialog('open');
$(".validateTips").text(dJSON.message);
lastURI = dJSON.last_uri;
} else {
if (success_func)
success_func(dJSON.data, dJSON.code, dJSON.message);
else
rData = dJSON.data;
}
},
error : function() {
error_func();
}
});
return (rData);
}
function _AJAX_UPLOAD(post_url, post_data, success_func, error_func) {
var rData = true;
error_func = error_func || function() {
alert('There was a problem communicating, please reload your browser');
}
if (!post_data || post_data == '') {
post_data = new FormData();
post_data.append('data[page_token]' + page_token);
} else
post_data.append('data[page_token]', page_token);
if (!success_func)
cB = false;
else
cB = true;
$.ajax({
type : "POST",
url : post_url,
data : post_data,
async : cB,
processData : false,
contentType : false,
dataType : 'json',
success : function(dJSON) {
if (dJSON.authenticated && dJSON.authenticated != 1) {
system_login.dialog('open');
$(".validateTips").text(dJSON.message);
lastURI = dJSON.last_uri;
} else {
if (success_func)
success_func(dJSON.data, dJSON.code, dJSON.message);
else
rData = dJSON.data;
}
},
error : function() {
error_func();
}
});
return (rData);
}