// Place Contents Here // Place Contents Here var wtbx = {}; wtbx.webAPISSO = { forumAddess : 'https://spi-forum.discussion.community', // Required chatRoomAddess : 'CHAT-ROOM-ADDRESS', // Optional (Only needed if chat room is enable) forumApiKey : 'xRSzlSY5ZlS9reDAOdVR4At6JJZr4vRx', // First part of API forumUsername : 'spi-forum', // Required webApiKey : '', id : '{{user_id}}', // Used logged-in user id from the Weebly Website. user : '{{email}}', // Added email get from weebly email : '{{email}}', // Added email get from weebly fullName : '', // Added name get from weebly jaxRequestSSO : 0, setCookie: function(cname, cvalue, exdays, set_domain) { var expires = ""; if (exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); expires = "expires="+d.toUTCString(); } var domain; if(set_domain) { domain = window.location.host; // Remove www. from dmoain name if exist. domain = domain.replace(/^www./i,''); domain = "domain=."+domain; } document.cookie = cname+"="+escape(cvalue)+expires+"; "+ domain +"; path=/"; }, getCookie: function(c_name) { var i,x,y,ARRcookies=document.cookie.split(";"); for (i=0;i"); wtbx.webAPISSO.setCookie("wtbLogoutToken",authtoken,0,1); wtbx.webAPISSO.setCookie("wtbToken",authtoken,0,1); wtbx.webAPISSO.setCookie("wtbLoginToken",'','',1); // SSO login for the chat room. var chatRoomAccessToken = wtbx.webAPISSO.getCookie('wtbChatRoomLoginToken'); if(chatRoomAccessToken) { var chatRoomLoginPath = wtbx.webAPISSO.chatRoomAddess+'/sso/token/login?access_token='+chatRoomAccessToken; $("#wtbImage").append(""); wtbx.webAPISSO.setCookie("wtbChatRoomLogoutToken",chatRoomAccessToken,0,1); wtbx.webAPISSO.setCookie("wtbChatRoomLoginToken",'','',1); } } }, setLoginAuthtoken: function(webApiKey, loginUserEmail, loginUserName){ if((typeof(wtbx.webAPISSO.getCookie('wtbLogoutToken')) === "undefined" || wtbx.webAPISSO.getCookie('wtbLogoutToken') == "")) { wtbx.webAPISSO.user = loginUserEmail; wtbx.webAPISSO.email = loginUserEmail; wtbx.webAPISSO.fullName = loginUserName; $.ajax({ type : "POST", url : wtbx.webAPISSO.forumAddess+'/register/setauthtoken', dataType : "jsonp", jsonp : 'callback', crossDomain : true, contentType : 'application/x-www-form-urlencoded; charset=UTF-8', data: { 'username' : wtbx.webAPISSO.forumUsername, 'type' : 'json', 'webAPIKey' : webApiKey, 'user' : wtbx.webAPISSO.user, 'email' : wtbx.webAPISSO.email, 'name' : wtbx.webAPISSO.fullName, 'apiToken' : $("#apitoken").attr("data-apitoken"), }, success: function(response){ if(response.authtoken) { var wtbAuthToken = response.authtoken; var wtbForumToken = ''; var wtbChatRoomToken = ''; if (wtbAuthToken.indexOf("-") >= 0) { var token = wtbAuthToken.split("-"); wtbForumToken = token[0]; wtbChatRoomToken = token[1]; } else { wtbForumToken = wtbAuthToken; } wtbx.webAPISSO.setCookie("wtbLoginToken",wtbForumToken,0,1); wtbx.webAPISSO.setCookie("wtbToken",wtbForumToken,0,1); if(wtbChatRoomToken) { wtbx.webAPISSO.chatRoomAddess = ''; wtbx.webAPISSO.setCookie("wtbChatRoomLoginToken",wtbChatRoomToken,0,1); } wtbx.webAPISSO.ssoLogin(); } else { console.log(response.message); } }, error : function(xhr, textStatus, errorThrown) { console.log("=xhr="+xhr+"=textStatus="+textStatus+"=errorThrown="+errorThrown+"=end="); } }); } }, getLogoutParameterByName: function (name, url) { if (!url) url = window.location; var match = RegExp('[?&]' + name + '=([^&]*)') .exec(url); return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); }, generateWebApiKey : function(loginUserEmail, loginUserName) { var wtbapikeyArray = wtbx.webAPISSO.forumApiKey.split(''); var webAPIKeyFirst = ''; var webAPIKeyLast = ''; for (i=0; i < wtbapikeyArray.length; ++i) { if(i <= 7) { webAPIKeyFirst = webAPIKeyFirst + wtbapikeyArray[i]; } else { webAPIKeyLast = webAPIKeyLast + wtbapikeyArray[i]; } } var forumApiKeyArr = wtbx.webAPISSO.forumApiKey.split('').reverse().join(''); var wtbkeyArray = forumApiKeyArr.split(''); var forumUname = wtbx.webAPISSO.forumUsername.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); var loginUsername = $("#"+$("#apitoken").attr("data-apitoken")).attr("data-"+$("#apitoken").attr("data-apitoken")); loginUsername = loginUsername.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); var forumUsernameArray = forumUname.split(''); var loginUsernameArray = loginUsername.split(''); var webApiKey = ''; for (j=0; j < wtbkeyArray.length; ++j) { webApiKey += wtbkeyArray[j]; if(typeof forumUsernameArray[j] != 'undefined') { webApiKey += forumUsernameArray[j]; } if(typeof loginUsernameArray[j] != 'undefined') { webApiKey += loginUsernameArray[j]; } } webApiKey = webApiKey + $("#apitoken").attr("data-apitoken"); webApiKey = webApiKey.replace(/A/g,'C').replace(/P/g,'N').replace(/B/g,'O').replace(/D/g,'T').replace(/E/g,'S').replace(/F/g,'R').replace(/G/g,'Q').replace(/a/g,'c').replace(/p/g,'n').replace(/b/g,'o').replace(/d/g,'t').replace(/e/g,'s').replace(/f/g,'r').replace(/g/g,'q'); webApiKey = webAPIKeyFirst + webApiKey + webAPIKeyLast; wtbx.webAPISSO.setLoginAuthtoken(webApiKey, loginUserEmail, loginUserName); }, ssoLogout: function() { if(wtbx.webAPISSO.getCookie('wtbLogoutToken')) { var loginImgPath = wtbx.webAPISSO.forumAddess+'/register/logout?authtoken=' + wtbx.webAPISSO.getCookie('wtbLogoutToken'); $("#wtbImage").append(""); if(wtbx.webAPISSO.getCookie('wtbChatRoomLogoutToken')) { var chatRoomLogoutImgPath = wtbx.webAPISSO.chatRoomAddess+'/sso/token/logout?access_token=' + wtbx.webAPISSO.getCookie('wtbChatRoomLogoutToken'); $("#wtbImage").append(""); wtbx.webAPISSO.setCookie("wtbChatRoomLogoutToken",'','',1); } wtbx.webAPISSO.setCookie("wtbLogoutToken",'','',1); wtbx.webAPISSO.setCookie("wtbLoginToken",'','',1); wtbx.webAPISSO.setCookie("wtbToken",'','',1); } }, isiPhoneiPadiPod : function() { return navigator.userAgent.match(/(iPod|iPhone|iPad)/); } }; $(document).on('click', '.logout-link', function(evt) { wtbx.webAPISSO.setCookie("wtbLogout",'1',0,1); }); // Get login UER detail from the weebly website. if(typeof(wtbx.webAPISSO.getCookie('wtbLogoutToken')) === "undefined" || wtbx.webAPISSO.getCookie('wtbLogoutToken') == "") { $.ajax({ url : "https://www.spi-researchcentre.com/ajax/api/JsonRPC/Membership/?Membership[Member::get_account_information]", data : JSON.stringify({jsonrpc:'2.0', method:'Member::get_account_information', params:[],id:"0"} ), type : "POST", dataType : "json", success: function (data) { if(data.result.data !== null) { var loginUserEmail = data.result.data.member.email; if(data.result.data.member.name !== null) { var loginUserName = data.result.data.member.name; } else { var loginUserName = data.result.data.member.email; } // Set email in HTML element as data attribute. if(typeof loginUserEmail != "undefined" && loginUserEmail) { $("#"+$("#apitoken").attr("data-apitoken")).attr("data-"+$("#apitoken").attr("data-apitoken"), loginUserEmail); } if(typeof loginUserEmail != "undefined" && loginUserEmail && typeof(wtbx.webAPISSO.getCookie('wtbLogoutToken')) === "undefined" || wtbx.webAPISSO.getCookie('wtbLogoutToken') == "") { wtbx.webAPISSO.generateWebApiKey(loginUserEmail, loginUserName); } } }, error: function (err) { console.log("Error"); } }); } $( document ).ready(function() { // Logout from the forum setTimeout(function(){ // Execute this function once user login on the Weebly website. if(( typeof(wtbx.webAPISSO.getCookie('wtbLogout')) !== "undefined" && wtbx.webAPISSO.getCookie('wtbLogout') == '1') || (wtbx.webAPISSO.getLogoutParameterByName('weeblylogout', window.document.location.href) || $.trim($("#member-login #wsite-nav-login-a").text()) == "Log In") ) { wtbx.webAPISSO.setCookie("wtbLogout",'','',1); wtbx.webAPISSO.ssoLogout(); } }, 1000); });