Skip to content

Commit

Permalink
Add authentification (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
remdex authored Apr 21, 2019
1 parent fe93fa5 commit 2416bfe
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 84 deletions.
22 changes: 19 additions & 3 deletions nodejshelper/bootstrap/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public function messageReceivedAdmin($params) {
if (isset($params['ou']) && $params['ou'] instanceof erLhcoreClassModelChatOnlineUser && $params['chat']->user_status == erLhcoreClassModelChat::USER_STATUS_PENDING_REOPEN) {
erLhcoreClassNodeJSRedis::instance()->publish('uo_' . $params['ou']->vid,'o:' . json_encode(array('op' => 'check_message')));
} else {
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
if(erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')){
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . erLhcoreClassInstance::getInstance()->id . '_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
} else{
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
}
}
}

Expand All @@ -76,6 +80,10 @@ public function getSettingVariable($var) {
return $this->settings['public_settings']['secure'];
break;

case 'automated_hosting':
return $this->settings['automated_hosting'];
break;

default:
return null;
;
Expand Down Expand Up @@ -113,12 +121,20 @@ public function proactiveInvitationSend($params)

public function messageReceived($params)
{
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
if(erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')){
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . erLhcoreClassInstance::getInstance()->id . '_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
} else{
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
}
}

public function statusChange($params)
{
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'schange')));
if(erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')){
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . erLhcoreClassInstance::getInstance()->id . '_' . $params['chat']->id,'o:' . json_encode(array('op' => 'schange')));
} else{
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'schange')));
}
}


Expand Down
42 changes: 35 additions & 7 deletions nodejshelper/design/nodejshelpertheme/js/customjs-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ var channelList = [];
socketOptions.secure = true;
}

var chanelName;

if (lh.nodejsHelperOptions.instance_id > 0) {
chanelName = ('chat_'+lh.nodejsHelperOptions.instance_id+'_'+lhinst.chat_id);
} else {
chanelName = ('chat_'+lhinst.chat_id);
}

// Initiate the connection to the server
var socket = socketCluster.connect(socketOptions);

Expand All @@ -26,7 +34,11 @@ var channelList = [];
try {
if (typeof channelList[chat_id] === 'undefined')
{
channelList[chat_id] = socket.subscribe('chat_' + chat_id);
if (lh.nodejsHelperOptions.instance_id > 0) {
channelList[chat_id] = socket.subscribe('chat_'+lh.nodejsHelperOptions.instance_id+'_'+ chat_id);
} else {
channelList[chat_id] = socket.subscribe('chat_' + chat_id);
}

channelList[chat_id].on('subscribeFail', function (err) {
console.error('Failed to subscribe to the sample channel due to error: ' + err);
Expand All @@ -52,7 +64,11 @@ var channelList = [];
function operatorTypingListener(data) {
data.ttx = lh.nodejsHelperOptions.typer;
ee.emitEvent('nodeJsTypingOperator', [data]);
socket.publish('chat_'+data.chat_id,{'op':'ot','data':data}); // Operator typing
if (lh.nodejsHelperOptions.instance_id > 0) {
socket.publish('chat_'+lh.nodejsHelperOptions.instance_id+'_'+data.chat_id,{'op':'ot','data':data}); // Operator typing
} else{
socket.publish('chat_'+data.chat_id,{'op':'ot','data':data}); // Operator typing
}
}

function removeSynchroChatListener(chat_id) {
Expand All @@ -68,7 +84,6 @@ var channelList = [];

socket.on('close', function() {
try {

lhinst.nodeJsMode = false;
channelList.forEach(function(channel){
if (typeof channel !== 'undefined') {
Expand All @@ -89,23 +104,36 @@ var channelList = [];
}
});

socket.on('connect', function () {

function connectAdmin(){
try {
lhinst.nodeJsMode = true;

lhinst.chatsSynchronising.forEach(function (chat_id) {
addChatToNodeJS(chat_id);
});

ee.addListener('chatTabLoaded', addChatToNodeJS);
ee.addListener('operatorTyping', operatorTypingListener);
ee.addListener('removeSynchroChat', removeSynchroChatListener);

confLH.chat_message_sinterval = 15000;

} catch (e) {
console.log(e);
}
}

socket.on('connect', function (status) {
if (status.isAuthenticated) {
connectAdmin();
} else {
socket.emit('login', {hash:lh.nodejsHelperOptions.hash, chanelName: chanelName}, function (err) {
if (err) {
console.log(err);
} else {
connectAdmin();
}
});
}
});

$(window).on('beforeunload', function () {
Expand Down
95 changes: 63 additions & 32 deletions nodejshelper/design/nodejshelpertheme/js/customjs-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ setTimeout(function() {
socketOptions.secure = true;
}

var chanelName;

if (lh.nodejsHelperOptions.instance_id > 0) {
chanelName = ('chat_'+lh.nodejsHelperOptions.instance_id+'_'+lhinst.chat_id);
} else{
chanelName = ('chat_'+lhinst.chat_id);
}

// Initiate the connection to the server
var socket = socketCluster.connect(socketOptions);

Expand All @@ -24,12 +32,20 @@ setTimeout(function() {

function visitorTypingListener(data)
{
socket.publish('chat_'+lhinst.chat_id,{'op':'vt','msg':data.msg});
if (lh.nodejsHelperOptions.instance_id > 0) {
socket.publish('chat_'+lh.nodejsHelperOptions.instance_id+'_'+lhinst.chat_id,{'op':'vt','msg':data.msg});
} else {
socket.publish('chat_'+lhinst.chat_id,{'op':'vt','msg':data.msg});
}
}

function visitorTypingStoppedListener()
{
socket.publish('chat_'+lhinst.chat_id,{'op':'vts'});
if (lh.nodejsHelperOptions.instance_id > 0) {
socket.publish('chat_'+lh.nodejsHelperOptions.instance_id+'_'+lhinst.chat_id,{'op':'vts'});
} else {
socket.publish('chat_'+lhinst.chat_id,{'op':'vts'});
}
}

socket.on('close', function(){
Expand All @@ -45,43 +61,58 @@ setTimeout(function() {
confLH.chat_message_sinterval = confLH.defaut_chat_message_sinterval;
});

socket.on('connect', function () {

if (lhinst.chat_id > 0) {
function connectVisitor(){
if (lh.nodejsHelperOptions.instance_id > 0) {
sampleChannel = socket.subscribe('chat_'+lh.nodejsHelperOptions.instance_id+'_'+lhinst.chat_id);
} else {
sampleChannel = socket.subscribe('chat_' + lhinst.chat_id);
}

sampleChannel.on('subscribeFail', function (err) {
console.error('Failed to subscribe to the sample channel due to error: ' + err);
});

sampleChannel.watch(function (op) {
if (op.op == 'ot') { // Operator Typing Message
var instStatus = $('#id-operator-typing');
if (op.data.status == true) {
instStatus.text(op.data.ttx);
instStatus.css('visibility','visible');
} else {
instStatus.css('visibility','hidden');
}
} else if (op.op == 'cmsg') {
lhinst.syncusercall();
} else if (op.op == 'schange') {
lhinst.chatsyncuserpending();
lhinst.syncusercall();
sampleChannel.on('subscribeFail', function (err) {
console.error('Failed to subscribe to the sample channel due to error: ' + err);
});

sampleChannel.watch(function (op) {
if (op.op == 'ot') { // Operator Typing Message
var instStatus = $('#id-operator-typing');
if (op.data.status == true) {
instStatus.text(op.data.ttx);
instStatus.css('visibility','visible');
} else {
instStatus.css('visibility','hidden');
}
});
} else if (op.op == 'cmsg') {
lhinst.syncusercall();
} else if (op.op == 'schange') {
lhinst.chatsyncuserpending();
lhinst.syncusercall();
}
});

// Disable default method
LHCCallbacks.initTypingMonitoringUserInform = true;
// Disable default method
LHCCallbacks.initTypingMonitoringUserInform = true;

ee.addListener('visitorTyping', visitorTypingListener);
ee.addListener('visitorTypingStopped', visitorTypingStoppedListener);
ee.addListener('visitorTyping', visitorTypingListener);
ee.addListener('visitorTypingStopped', visitorTypingStoppedListener);

// Make larger sync interval
confLH.chat_message_sinterval = 10000;
// Make larger sync interval
confLH.chat_message_sinterval = 10000;

// Force one time check
lhinst.syncusercall();
// Force one time check
lhinst.syncusercall();
}

socket.on('connect', function (status) {
if (status.isAuthenticated && lhinst.chat_id > 0) {
connectVisitor();
} else {
socket.emit('login', {hash:lh.nodejsHelperOptions.hash, chanelName: chanelName}, function (err) {
if (err) {
console.log(err);
} else {
connectVisitor();
}
});
}
});

Expand Down
49 changes: 32 additions & 17 deletions nodejshelper/design/nodejshelpertheme/js/customjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,41 @@
socketOptions.secure = true;
}

// Initiate the connection to the server
var socket = socketCluster.connect(socketOptions);
// Initiate the connection to the server
var socket = socketCluster.connect(socketOptions);

socket.on('error', function (err) {
console.error(err);
});
socket.on('error', function (err) {
console.error(err);
});

socket.on('connect', function () {
//console.log('Socket is connected');
});
function connectSiteVisitor(){
var sampleChannel = socket.subscribe('uo_' + lh_inst.cookieDataPers.vid);

var sampleChannel = socket.subscribe('uo_' + lh_inst.cookieDataPers.vid);
sampleChannel.on('subscribeFail', function (err) {
console.error('Failed to subscribe to the sample channel due to error: ' + err);
});

sampleChannel.on('subscribeFail', function (err) {
console.error('Failed to subscribe to the sample channel due to error: ' + err);
});
sampleChannel.watch(function (op) {
if (op.op == 'check_message') {
lh_inst.startNewMessageCheckSingle();
}
});
}

var chanelName = ('chat_'+'uo_' + lh_inst.cookieDataPers.vid);

socket.on('connect', function (status) {
if (status.isAuthenticated) {
connectSiteVisitor();
} else {
socket.emit('login', {hash:lh_inst.nodejsHelperOptions.hash, chanelName: chanelName}, function (err) {
if (err) {
console.log(err);
} else {
connectSiteVisitor();
}
});
}
});

sampleChannel.watch(function (op) {
if (op.op == 'check_message') {
lh_inst.startNewMessageCheckSingle();
}
});
})();

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions nodejshelper/design/nodejshelpertheme/js/nodejshelper.min.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
'path':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('path')?>',
'port':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('port')?>',
'secure':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('secure')?>',
'instance_id':<?php if (erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')) : ?><?php echo erLhcoreClassInstance::getInstance()->id?><?php else : ?>0<?php endif; ?>,
'hash': '<?php $date = time(); echo sha1($date . 'Operator' . erConfigClassLhConfig::getInstance()->getSetting('site','secrethash')) . '.' . $date;?>'
};

var thnjs = document.getElementsByTagName('head')[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0') === false &&
strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0') === false
) : ?>

<script>
lh.nodejsHelperOptions = {
'hostname':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('hostname')?>',
'path':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('path')?>',
'port':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('port')?>',
'secure':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('secure')?>'
'secure':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('secure')?>',
'hash': '<?php $date = time(); echo sha1($date . 'Operator' . erConfigClassLhConfig::getInstance()->getSetting('site','secrethash')) . '.' . $date;?>',
'instance_id':<?php if (erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')) : ?><?php echo erLhcoreClassInstance::getInstance()->id?><?php else : ?>0<?php endif; ?>
};
confLH.defaut_chat_message_sinterval = confLH.chat_message_sinterval;
<?php if (erLhcoreClassSystem::instance()->SiteAccess == 'site_admin' && erLhcoreClassUser::instance()->isLogged()) :
$currentUser = erLhcoreClassUser::instance();
$userData = $currentUser->getUserData(true); ?>
lh.nodejsHelperOptions.typer = typeof lh.nodejsHelperOptions.typer !== 'undefined' ? lh.nodejsHelperOptions.typer : '<?php echo htmlspecialchars($userData->name_support,ENT_QUOTES);?> <?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/chat','is typing now...')?>';
<?php endif;?>

</script>
<script src="<?php echo erLhcoreClassDesign::designJS('js/nodejshelper.admin.min.js');?>"></script>
<?php endif; ?>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
'path':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('path')?>',
'port':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('port')?>',
'secure':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('secure')?>',
'hash': '<?php $date = time(); echo sha1($date . 'Visitor' . erConfigClassLhConfig::getInstance()->getSetting('site','secrethash')) . '.' . $date; ?>',
'instance_id':<?php if (erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')) : ?><?php echo erLhcoreClassInstance::getInstance()->id?><?php else : ?>0<?php endif; ?>
};
confLH.defaut_chat_message_sinterval = confLH.chat_message_sinterval;
</script>
Expand Down
3 changes: 2 additions & 1 deletion nodejshelper/serversc/lhc/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ var options = {
brokerOptions: {
host: '127.0.0.1',
port: 6379
}
},
secretHash: '<use_your_secret_hash>', //This secrethash of lhc settings, need for user authenticated
};

var bootTimeout = Number(process.env.SOCKETCLUSTER_CONTROLLER_BOOT_TIMEOUT) || 10000;
Expand Down
Loading

0 comments on commit 2416bfe

Please sign in to comment.