Skip to content

Commit 2416bfe

Browse files
authored
Add authentification (#5)
1 parent fe93fa5 commit 2416bfe

File tree

12 files changed

+214
-84
lines changed

12 files changed

+214
-84
lines changed

nodejshelper/bootstrap/bootstrap.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public function messageReceivedAdmin($params) {
5252
if (isset($params['ou']) && $params['ou'] instanceof erLhcoreClassModelChatOnlineUser && $params['chat']->user_status == erLhcoreClassModelChat::USER_STATUS_PENDING_REOPEN) {
5353
erLhcoreClassNodeJSRedis::instance()->publish('uo_' . $params['ou']->vid,'o:' . json_encode(array('op' => 'check_message')));
5454
} else {
55-
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
55+
if(erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')){
56+
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . erLhcoreClassInstance::getInstance()->id . '_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
57+
} else{
58+
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
59+
}
5660
}
5761
}
5862

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

83+
case 'automated_hosting':
84+
return $this->settings['automated_hosting'];
85+
break;
86+
7987
default:
8088
return null;
8189
;
@@ -113,12 +121,20 @@ public function proactiveInvitationSend($params)
113121

114122
public function messageReceived($params)
115123
{
116-
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
124+
if(erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')){
125+
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . erLhcoreClassInstance::getInstance()->id . '_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
126+
} else{
127+
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'cmsg')));
128+
}
117129
}
118130

119131
public function statusChange($params)
120132
{
121-
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'schange')));
133+
if(erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')){
134+
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . erLhcoreClassInstance::getInstance()->id . '_' . $params['chat']->id,'o:' . json_encode(array('op' => 'schange')));
135+
} else{
136+
erLhcoreClassNodeJSRedis::instance()->publish('chat_' . $params['chat']->id,'o:' . json_encode(array('op' => 'schange')));
137+
}
122138
}
123139

124140

nodejshelper/design/nodejshelpertheme/js/customjs-admin.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ var channelList = [];
1515
socketOptions.secure = true;
1616
}
1717

18+
var chanelName;
19+
20+
if (lh.nodejsHelperOptions.instance_id > 0) {
21+
chanelName = ('chat_'+lh.nodejsHelperOptions.instance_id+'_'+lhinst.chat_id);
22+
} else {
23+
chanelName = ('chat_'+lhinst.chat_id);
24+
}
25+
1826
// Initiate the connection to the server
1927
var socket = socketCluster.connect(socketOptions);
2028

@@ -26,7 +34,11 @@ var channelList = [];
2634
try {
2735
if (typeof channelList[chat_id] === 'undefined')
2836
{
29-
channelList[chat_id] = socket.subscribe('chat_' + chat_id);
37+
if (lh.nodejsHelperOptions.instance_id > 0) {
38+
channelList[chat_id] = socket.subscribe('chat_'+lh.nodejsHelperOptions.instance_id+'_'+ chat_id);
39+
} else {
40+
channelList[chat_id] = socket.subscribe('chat_' + chat_id);
41+
}
3042

3143
channelList[chat_id].on('subscribeFail', function (err) {
3244
console.error('Failed to subscribe to the sample channel due to error: ' + err);
@@ -52,7 +64,11 @@ var channelList = [];
5264
function operatorTypingListener(data) {
5365
data.ttx = lh.nodejsHelperOptions.typer;
5466
ee.emitEvent('nodeJsTypingOperator', [data]);
55-
socket.publish('chat_'+data.chat_id,{'op':'ot','data':data}); // Operator typing
67+
if (lh.nodejsHelperOptions.instance_id > 0) {
68+
socket.publish('chat_'+lh.nodejsHelperOptions.instance_id+'_'+data.chat_id,{'op':'ot','data':data}); // Operator typing
69+
} else{
70+
socket.publish('chat_'+data.chat_id,{'op':'ot','data':data}); // Operator typing
71+
}
5672
}
5773

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

6985
socket.on('close', function() {
7086
try {
71-
7287
lhinst.nodeJsMode = false;
7388
channelList.forEach(function(channel){
7489
if (typeof channel !== 'undefined') {
@@ -89,23 +104,36 @@ var channelList = [];
89104
}
90105
});
91106

92-
socket.on('connect', function () {
93-
107+
function connectAdmin(){
94108
try {
95109
lhinst.nodeJsMode = true;
96-
97110
lhinst.chatsSynchronising.forEach(function (chat_id) {
98111
addChatToNodeJS(chat_id);
99112
});
100113

101114
ee.addListener('chatTabLoaded', addChatToNodeJS);
102115
ee.addListener('operatorTyping', operatorTypingListener);
103116
ee.addListener('removeSynchroChat', removeSynchroChatListener);
104-
117+
105118
confLH.chat_message_sinterval = 15000;
119+
106120
} catch (e) {
107121
console.log(e);
108122
}
123+
}
124+
125+
socket.on('connect', function (status) {
126+
if (status.isAuthenticated) {
127+
connectAdmin();
128+
} else {
129+
socket.emit('login', {hash:lh.nodejsHelperOptions.hash, chanelName: chanelName}, function (err) {
130+
if (err) {
131+
console.log(err);
132+
} else {
133+
connectAdmin();
134+
}
135+
});
136+
}
109137
});
110138

111139
$(window).on('beforeunload', function () {

nodejshelper/design/nodejshelpertheme/js/customjs-widget.js

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ setTimeout(function() {
1313
socketOptions.secure = true;
1414
}
1515

16+
var chanelName;
17+
18+
if (lh.nodejsHelperOptions.instance_id > 0) {
19+
chanelName = ('chat_'+lh.nodejsHelperOptions.instance_id+'_'+lhinst.chat_id);
20+
} else{
21+
chanelName = ('chat_'+lhinst.chat_id);
22+
}
23+
1624
// Initiate the connection to the server
1725
var socket = socketCluster.connect(socketOptions);
1826

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

2533
function visitorTypingListener(data)
2634
{
27-
socket.publish('chat_'+lhinst.chat_id,{'op':'vt','msg':data.msg});
35+
if (lh.nodejsHelperOptions.instance_id > 0) {
36+
socket.publish('chat_'+lh.nodejsHelperOptions.instance_id+'_'+lhinst.chat_id,{'op':'vt','msg':data.msg});
37+
} else {
38+
socket.publish('chat_'+lhinst.chat_id,{'op':'vt','msg':data.msg});
39+
}
2840
}
2941

3042
function visitorTypingStoppedListener()
3143
{
32-
socket.publish('chat_'+lhinst.chat_id,{'op':'vts'});
44+
if (lh.nodejsHelperOptions.instance_id > 0) {
45+
socket.publish('chat_'+lh.nodejsHelperOptions.instance_id+'_'+lhinst.chat_id,{'op':'vts'});
46+
} else {
47+
socket.publish('chat_'+lhinst.chat_id,{'op':'vts'});
48+
}
3349
}
3450

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

48-
socket.on('connect', function () {
49-
50-
if (lhinst.chat_id > 0) {
64+
function connectVisitor(){
65+
if (lh.nodejsHelperOptions.instance_id > 0) {
66+
sampleChannel = socket.subscribe('chat_'+lh.nodejsHelperOptions.instance_id+'_'+lhinst.chat_id);
67+
} else {
5168
sampleChannel = socket.subscribe('chat_' + lhinst.chat_id);
69+
}
5270

53-
sampleChannel.on('subscribeFail', function (err) {
54-
console.error('Failed to subscribe to the sample channel due to error: ' + err);
55-
});
56-
57-
sampleChannel.watch(function (op) {
58-
if (op.op == 'ot') { // Operator Typing Message
59-
var instStatus = $('#id-operator-typing');
60-
if (op.data.status == true) {
61-
instStatus.text(op.data.ttx);
62-
instStatus.css('visibility','visible');
63-
} else {
64-
instStatus.css('visibility','hidden');
65-
}
66-
} else if (op.op == 'cmsg') {
67-
lhinst.syncusercall();
68-
} else if (op.op == 'schange') {
69-
lhinst.chatsyncuserpending();
70-
lhinst.syncusercall();
71+
sampleChannel.on('subscribeFail', function (err) {
72+
console.error('Failed to subscribe to the sample channel due to error: ' + err);
73+
});
74+
75+
sampleChannel.watch(function (op) {
76+
if (op.op == 'ot') { // Operator Typing Message
77+
var instStatus = $('#id-operator-typing');
78+
if (op.data.status == true) {
79+
instStatus.text(op.data.ttx);
80+
instStatus.css('visibility','visible');
81+
} else {
82+
instStatus.css('visibility','hidden');
7183
}
72-
});
84+
} else if (op.op == 'cmsg') {
85+
lhinst.syncusercall();
86+
} else if (op.op == 'schange') {
87+
lhinst.chatsyncuserpending();
88+
lhinst.syncusercall();
89+
}
90+
});
7391

74-
// Disable default method
75-
LHCCallbacks.initTypingMonitoringUserInform = true;
92+
// Disable default method
93+
LHCCallbacks.initTypingMonitoringUserInform = true;
7694

77-
ee.addListener('visitorTyping', visitorTypingListener);
78-
ee.addListener('visitorTypingStopped', visitorTypingStoppedListener);
95+
ee.addListener('visitorTyping', visitorTypingListener);
96+
ee.addListener('visitorTypingStopped', visitorTypingStoppedListener);
7997

80-
// Make larger sync interval
81-
confLH.chat_message_sinterval = 10000;
98+
// Make larger sync interval
99+
confLH.chat_message_sinterval = 10000;
82100

83-
// Force one time check
84-
lhinst.syncusercall();
101+
// Force one time check
102+
lhinst.syncusercall();
103+
}
104+
105+
socket.on('connect', function (status) {
106+
if (status.isAuthenticated && lhinst.chat_id > 0) {
107+
connectVisitor();
108+
} else {
109+
socket.emit('login', {hash:lh.nodejsHelperOptions.hash, chanelName: chanelName}, function (err) {
110+
if (err) {
111+
console.log(err);
112+
} else {
113+
connectVisitor();
114+
}
115+
});
85116
}
86117
});
87118

nodejshelper/design/nodejshelpertheme/js/customjs.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,41 @@
1313
socketOptions.secure = true;
1414
}
1515

16-
// Initiate the connection to the server
17-
var socket = socketCluster.connect(socketOptions);
16+
// Initiate the connection to the server
17+
var socket = socketCluster.connect(socketOptions);
1818

19-
socket.on('error', function (err) {
20-
console.error(err);
21-
});
19+
socket.on('error', function (err) {
20+
console.error(err);
21+
});
2222

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

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

29-
sampleChannel.on('subscribeFail', function (err) {
30-
console.error('Failed to subscribe to the sample channel due to error: ' + err);
31-
});
30+
sampleChannel.watch(function (op) {
31+
if (op.op == 'check_message') {
32+
lh_inst.startNewMessageCheckSingle();
33+
}
34+
});
35+
}
36+
37+
var chanelName = ('chat_'+'uo_' + lh_inst.cookieDataPers.vid);
38+
39+
socket.on('connect', function (status) {
40+
if (status.isAuthenticated) {
41+
connectSiteVisitor();
42+
} else {
43+
socket.emit('login', {hash:lh_inst.nodejsHelperOptions.hash, chanelName: chanelName}, function (err) {
44+
if (err) {
45+
console.log(err);
46+
} else {
47+
connectSiteVisitor();
48+
}
49+
});
50+
}
51+
});
3252

33-
sampleChannel.watch(function (op) {
34-
if (op.op == 'check_message') {
35-
lh_inst.startNewMessageCheckSingle();
36-
}
37-
});
3853
})();

nodejshelper/design/nodejshelpertheme/js/nodejshelper.admin.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nodejshelper/design/nodejshelpertheme/js/nodejshelper.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nodejshelper/design/nodejshelpertheme/js/nodejshelper.widget.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nodejshelper/design/nodejshelpertheme/tpl/lhchat/getstatus/lhc_chat_after_cookie_multiinclude.tpl.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
'path':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('path')?>',
1111
'port':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('port')?>',
1212
'secure':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('secure')?>',
13+
'instance_id':<?php if (erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')) : ?><?php echo erLhcoreClassInstance::getInstance()->id?><?php else : ?>0<?php endif; ?>,
14+
'hash': '<?php $date = time(); echo sha1($date . 'Operator' . erConfigClassLhConfig::getInstance()->getSetting('site','secrethash')) . '.' . $date;?>'
1315
};
1416

1517
var thnjs = document.getElementsByTagName('head')[0];

nodejshelper/design/nodejshelpertheme/tpl/pagelayouts/parts/page_head_js_extension_multiinclude.tpl.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0') === false &&
55
strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0') === false
66
) : ?>
7-
87
<script>
98
lh.nodejsHelperOptions = {
109
'hostname':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('hostname')?>',
1110
'path':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('path')?>',
1211
'port':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('port')?>',
13-
'secure':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('secure')?>'
12+
'secure':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('secure')?>',
13+
'hash': '<?php $date = time(); echo sha1($date . 'Operator' . erConfigClassLhConfig::getInstance()->getSetting('site','secrethash')) . '.' . $date;?>',
14+
'instance_id':<?php if (erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')) : ?><?php echo erLhcoreClassInstance::getInstance()->id?><?php else : ?>0<?php endif; ?>
1415
};
1516
confLH.defaut_chat_message_sinterval = confLH.chat_message_sinterval;
1617
<?php if (erLhcoreClassSystem::instance()->SiteAccess == 'site_admin' && erLhcoreClassUser::instance()->isLogged()) :
1718
$currentUser = erLhcoreClassUser::instance();
1819
$userData = $currentUser->getUserData(true); ?>
1920
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...')?>';
2021
<?php endif;?>
21-
2222
</script>
2323
<script src="<?php echo erLhcoreClassDesign::designJS('js/nodejshelper.admin.min.js');?>"></script>
2424
<?php endif; ?>

nodejshelper/design/nodejshelpertheme/tpl/pagelayouts/parts/page_head_js_user_extension_multiinclude.tpl.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
'path':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('path')?>',
1414
'port':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('port')?>',
1515
'secure':'<?php echo erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('secure')?>',
16+
'hash': '<?php $date = time(); echo sha1($date . 'Visitor' . erConfigClassLhConfig::getInstance()->getSetting('site','secrethash')) . '.' . $date; ?>',
17+
'instance_id':<?php if (erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionNodejshelper')->getSettingVariable('automated_hosting')) : ?><?php echo erLhcoreClassInstance::getInstance()->id?><?php else : ?>0<?php endif; ?>
1618
};
1719
confLH.defaut_chat_message_sinterval = confLH.chat_message_sinterval;
1820
</script>

0 commit comments

Comments
 (0)