From 0dd410e18424ce264a649467f6dbcaffc468ad22 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 16 Mar 2018 14:20:59 +0100 Subject: [PATCH 01/16] Improve guest handling in comments 1. No contacts menu for guests (can't use it anyway) 2. No contacts menu on guests (no data available) 3. Show "You" as author name above "new" box 4. Do not try to load avatars of guests (results in 404) Signed-off-by: Joas Schilling --- js/views/chatview.js | 52 ++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/js/views/chatview.js b/js/views/chatview.js index f579fd22359..76e7671493b 100644 --- a/js/views/chatview.js +++ b/js/views/chatview.js @@ -35,7 +35,7 @@ ''; var ADD_COMMENT_TEMPLATE = - '
' + + '
' + '
' + '
' + '
{{actorDisplayName}}
' + @@ -50,7 +50,7 @@ var COMMENT_TEMPLATE = '
  • ' + '
    ' + - '
    ' + + '
    ' + '
    {{actorDisplayName}}
    ' + '
    {{date}}
    ' + '
    ' + @@ -83,12 +83,20 @@ if (!this._addCommentTemplate) { this._addCommentTemplate = Handlebars.compile(ADD_COMMENT_TEMPLATE); } - // FIXME handle guest users - var currentUser = OC.getCurrentUser(); + + if (OC.getCurrentUser().uid) { + return this._addCommentTemplate(_.extend({ + actorId: OC.getCurrentUser().uid, + actorDisplayName: OC.getCurrentUser().displayName, + newMessagePlaceholder: t('spreed', 'New message …'), + submitText: t('spreed', 'Send') + }, params)); + } + return this._addCommentTemplate(_.extend({ - actorId: currentUser.uid, - actorDisplayName: currentUser.displayName, - newMessagePlaceholder: t('spreed', 'New message…'), + actorId: '', + actorDisplayName: t('spreed', 'You'), + newMessagePlaceholder: t('spreed', 'New message …'), submitText: t('spreed', 'Send') }, params)); }, @@ -110,8 +118,14 @@ } this.$el.find('.has-tooltip').tooltip({container: this._tooltipContainer}); this.$container = this.$el.find('ul.comments'); - // FIXME handle guest users - this.$el.find('.avatar').avatar(OC.getCurrentUser().uid, 32); + + if (OC.getCurrentUser().uid) { + this.$el.find('.avatar').avatar(OC.getCurrentUser().uid, 32, undefined, false, undefined, OC.getCurrentUser().displayName); + } else { + this.$el.find('.avatar').imageplaceholder('?', undefined, 32); + this.$el.find('.avatar').css('background-color', '#b9b9b9'); + } + this.delegateEvents(); this.$el.find('.message').on('keydown input change', this._onTypeComment); @@ -148,7 +162,7 @@ var actorDisplayName = commentModel.get('actorDisplayName'); if (commentModel.attributes.actorType === 'guests') { // FIXME get guest name from WebRTC or something like that - actorDisplayName = 'Guest'; + actorDisplayName = t('spreed', 'Guest'); } if (actorDisplayName == null) { actorDisplayName = t('spreed', '[Unknown user name]'); @@ -226,7 +240,7 @@ // received. this._lastAddedMessageModel = model; - this._postRenderItem($el); + this._postRenderItem(model, $el); if (scrollToNew) { var newestCommentHiddenHeight = ($newestComment.position().top + $newestComment.outerHeight(true)) - this.$container.outerHeight(true); @@ -298,16 +312,22 @@ return model1.get('date').toDateString() === model2.get('date').toDateString(); }, - _postRenderItem: function($el) { + _postRenderItem: function(model, $el) { $el.find('.has-tooltip').tooltip({container: this._tooltipContainer}); $el.find('.avatar').each(function() { var $this = $(this); - $this.avatar($this.attr('data-username'), 32); + if (model.get('actorType') === 'users') { + $this.avatar($this.data('user-id'), 32, undefined, false, undefined, $this.data('displayname')); + } else { + $this.imageplaceholder('?', undefined, 32); + $this.css('background-color', '#b9b9b9'); + } }); - // FIXME do not show contacts menu for guest users - var username = $el.find('.avatar').data('username'); - if (username !== oc_current_user) { + var username = $el.find('.avatar').data('user-id'); + if (OC.getCurrentUser().uid && + model.get('actorType') === 'users' && + username !== OC.getCurrentUser().uid) { $el.find('.authorRow .avatar, .authorRow .author').contactsMenu( username, 0, $el.find('.authorRow')); } From b5d07bd74043dbd97a3ca31b59336a1775b893ec Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 19 Mar 2018 13:07:11 +0100 Subject: [PATCH 02/16] Store guest names when they leave a comment Signed-off-by: Joas Schilling --- appinfo/info.xml | 2 +- js/views/chatview.js | 22 +++-- lib/Controller/ChatController.php | 31 ++++++- lib/GuestManager.php | 80 +++++++++++++++++++ .../Version3002Date20180319104030.php | 62 ++++++++++++++ 5 files changed, 185 insertions(+), 12 deletions(-) create mode 100644 lib/GuestManager.php create mode 100644 lib/Migration/Version3002Date20180319104030.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 9c95eb4bd3e..a0dc76335f1 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -17,7 +17,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m ]]> - 3.1.0 + 3.1.91 agpl Daniel Calviño Sánchez diff --git a/js/views/chatview.js b/js/views/chatview.js index 76e7671493b..da3eb45ed73 100644 --- a/js/views/chatview.js +++ b/js/views/chatview.js @@ -93,9 +93,10 @@ }, params)); } + var guestNick = OCA.SpreedMe.app._localStorageModel.get('nick'); return this._addCommentTemplate(_.extend({ actorId: '', - actorDisplayName: t('spreed', 'You'), + actorDisplayName: guestNick ? guestNick : t('spreed', 'You'), newMessagePlaceholder: t('spreed', 'New message …'), submitText: t('spreed', 'Send') }, params)); @@ -160,8 +161,8 @@ relativeDate = moment(timestamp, 'x').diff(moment()) > -86400000; var actorDisplayName = commentModel.get('actorDisplayName'); - if (commentModel.attributes.actorType === 'guests') { - // FIXME get guest name from WebRTC or something like that + if (commentModel.get('actorType') === 'guests' && + actorDisplayName === null) { actorDisplayName = t('spreed', 'Guest'); } if (actorDisplayName == null) { @@ -380,7 +381,6 @@ _onSubmitComment: function(e) { var self = this; var $form = $(e.target); - var comment = null; var $submit = $form.find('.submit'); var $loading = $form.find('.submitLoading'); var $commentField = $form.find('.message'); @@ -395,11 +395,19 @@ $loading.removeClass('hidden'); message = this._commentBodyHTML2Plain($commentField); - - comment = new OCA.SpreedMe.Models.ChatMessage({ + var data = { token: this.collection.token, message: message - }); + }; + + if (!OC.getCurrentUser().uid) { + var guestNick = OCA.SpreedMe.app._localStorageModel.get('nick'); + if (guestNick) { + data.actorDisplayName = guestNick; + } + } + + var comment = new OCA.SpreedMe.Models.ChatMessage(data); comment.save({}, { success: function(model) { self._onSubmitSuccess(model, $form); diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index da759204e97..809ca45b958 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -26,6 +26,7 @@ use OCA\Spreed\Chat\ChatManager; use OCA\Spreed\Exceptions\ParticipantNotFoundException; use OCA\Spreed\Exceptions\RoomNotFoundException; +use OCA\Spreed\GuestManager; use OCA\Spreed\Manager; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -53,6 +54,9 @@ class ChatController extends OCSController { /** @var ChatManager */ private $chatManager; + /** @var GuestManager */ + private $guestManager; + /** * @param string $appName * @param string $UserId @@ -61,6 +65,7 @@ class ChatController extends OCSController { * @param ISession $session * @param Manager $manager * @param ChatManager $chatManager + * @param GuestManager $guestManager */ public function __construct($appName, $UserId, @@ -68,7 +73,8 @@ public function __construct($appName, IUserManager $userManager, ISession $session, Manager $manager, - ChatManager $chatManager) { + ChatManager $chatManager, + GuestManager $guestManager) { parent::__construct($appName, $request); $this->userId = $UserId; @@ -76,6 +82,7 @@ public function __construct($appName, $this->session = $session; $this->manager = $manager; $this->chatManager = $chatManager; + $this->guestManager = $guestManager; } /** @@ -121,11 +128,12 @@ private function getRoom($token) { * * @param string $token the room token * @param string $message the message to send + * @param string $actorDisplayName for guests * @return DataResponse the status code is "201 Created" if successful, and * "404 Not found" if the room or session for a guest user was not * found". */ - public function sendMessage($token, $message) { + public function sendMessage($token, $message, $actorDisplayName) { $room = $this->getRoom($token); if ($room === null) { return new DataResponse([], Http::STATUS_NOT_FOUND); @@ -140,6 +148,10 @@ public function sendMessage($token, $message) { // empty in that case but sha1('') would generate a hash too // instead of returning an empty string). $actorId = $actorId ? sha1($actorId) : $actorId; + + if ($actorId && $actorDisplayName) { + $this->guestManager->updateName($actorId, $actorDisplayName); + } } else { $actorType = 'users'; $actorId = $this->userId; @@ -208,11 +220,23 @@ public function receiveMessages($token, $offset = 0, $notOlderThanTimestamp = 0, $comments = $this->chatManager->receiveMessages((string) $room->getId(), $this->userId, $timeout, $offset, $notOlderThan); - return new DataResponse(array_map(function(IComment $comment) use ($token) { + $guestSessions = []; + foreach ($comments as $comment) { + if ($comment->getActorType() !== 'guests') { + continue; + } + + $guestSessions[] = $comment->getActorId(); + } + + $guestNames = !empty($guestSessions) ? $this->guestManager->getNamesBySessionHashes($guestSessions) : []; + return new DataResponse(array_map(function(IComment $comment) use ($token, $guestNames) { $displayName = null; if ($comment->getActorType() === 'users') { $user = $this->userManager->get($comment->getActorId()); $displayName = $user instanceof IUser ? $user->getDisplayName() : null; + } else if ($comment->getActorType() === 'guests' && isset($guestNames[$comment->getActorId()])) { + $displayName = $guestNames[$comment->getActorId()]; } return [ @@ -226,5 +250,4 @@ public function receiveMessages($token, $offset = 0, $notOlderThanTimestamp = 0, ]; }, $comments)); } - } diff --git a/lib/GuestManager.php b/lib/GuestManager.php new file mode 100644 index 00000000000..7eb4afa259f --- /dev/null +++ b/lib/GuestManager.php @@ -0,0 +1,80 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Spreed; + + +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; + +class GuestManager { + + /** @var IDBConnection */ + protected $connection; + + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + } + + /** + * @param string $sessionHash + * @param string $displayName + * @throws \Doctrine\DBAL\DBALException + */ + public function updateName($sessionHash, $displayName) { + $result = $this->connection->insertIfNotExist('*PREFIX*talk_guests', [ + 'session_hash' => $sessionHash, + 'display_name' => $displayName, + ], ['session_hash']); + + if ($result === 1) { + return; + } + + $query = $this->connection->getQueryBuilder(); + $query->update('talk_guests') + ->set('display_name', $query->createNamedParameter($displayName)) + ->where($query->expr()->eq('session_hash', $query->createNamedParameter($sessionHash))); + $query->execute(); + } + + /** + * @param string[] $sessionHashes + * @return string[] + */ + public function getNamesBySessionHashes(array $sessionHashes) { + $query = $this->connection->getQueryBuilder(); + $query->select('*') + ->from('talk_guests') + ->where($query->expr()->in('session_hash', $query->createNamedParameter($sessionHashes, IQueryBuilder::PARAM_STR_ARRAY))); + + $result = $query->execute(); + + $map = []; + + while ($row = $result->fetch()) { + $map[$row['session_hash']] = $row['display_name']; + } + $result->closeCursor(); + + return $map; + } +} diff --git a/lib/Migration/Version3002Date20180319104030.php b/lib/Migration/Version3002Date20180319104030.php new file mode 100644 index 00000000000..f611349ae11 --- /dev/null +++ b/lib/Migration/Version3002Date20180319104030.php @@ -0,0 +1,62 @@ + + * + * @author Joas Schilling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\Spreed\Migration; + +use Doctrine\DBAL\Types\Type; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\SimpleMigrationStep; +use OCP\Migration\IOutput; + +class Version3002Date20180319104030 extends SimpleMigrationStep { + + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + * @since 13.0.0 + */ + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if (!$schema->hasTable('talk_guests')) { + $table = $schema->createTable('talk_guests'); + + $table->addColumn('session_hash', Type::STRING, [ + 'notnull' => false, + 'length' => 64, + ]); + $table->addColumn('display_name', Type::STRING, [ + 'notnull' => false, + 'length' => 64, + 'default' => '', + ]); + + $table->addUniqueIndex(['session_hash'], 'tg_session_hash'); + } + + return $schema; + } +} From 259b98bb69c94e324a865b14c04f6036154c65c5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 19 Mar 2018 13:34:52 +0100 Subject: [PATCH 03/16] Use editabletextlabel view for guest name in chat Signed-off-by: Joas Schilling --- css/style.scss | 7 +++++++ js/app.js | 11 +++++------ js/views/chatview.js | 45 +++++++++++++++++++++++++++++++------------- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/css/style.scss b/css/style.scss index ba362c36423..5bc61a06a1e 100644 --- a/css/style.scss +++ b/css/style.scss @@ -927,26 +927,31 @@ video { } .detailCallInfoContainer h3, +.newCommentRow .guest-name p, .detailCallInfoContainer .guest-name p { display: inline-block; } +.newCommentRow .guest-name p, .detailCallInfoContainer .guest-name p { padding: 9px 0; } +.newCommentRow .editable-text-label .edit-button, .detailCallInfoContainer .editable-text-label .edit-button { display: none; } .detailCallInfoContainer .clipboard-button, .detailCallInfoContainer .password-button, +.newCommentRow .editable-text-label:hover .edit-button, .detailCallInfoContainer .editable-text-label:hover .edit-button { display: inline-block; } .detailCallInfoContainer .clipboard-button .icon, .detailCallInfoContainer .password-button .icon, +.newCommentRow .editable-text-label .edit-button .icon, .detailCallInfoContainer .editable-text-label .edit-button .icon { cursor: pointer; padding: 22px; @@ -955,12 +960,14 @@ video { margin-top: -5px; } +.newCommentRow .editable-text-label .input-wrapper, .detailCallInfoContainer .editable-text-label .input-wrapper, .detailCallInfoContainer .password-option { position: relative; display: inline-block; } +.newCommentRow .editable-text-label input, .detailCallInfoContainer .editable-text-label input, .detailCallInfoContainer .password-input { width: 100%; diff --git a/js/app.js b/js/app.js index 42882cb1881..da68f1957ba 100644 --- a/js/app.js +++ b/js/app.js @@ -507,9 +507,11 @@ this._sidebarView = new OCA.SpreedMe.Views.SidebarView(); $('#app-content').append(this._sidebarView.$el); - if (oc_current_user) { + if (OC.getCurrentUser().uid) { this._rooms = new OCA.SpreedMe.Models.RoomCollection(); this.listenTo(roomChannel, 'active', this._setRoomActive); + } else { + this.initGuestName(); } this._sidebarView.listenTo(roomChannel, 'leaveCurrentCall', function() { @@ -520,7 +522,8 @@ this._chatView = new OCA.SpreedMe.Views.ChatView({ collection: this._messageCollection, id: 'commentsTabView', - oldestOnTopLayout: true + oldestOnTopLayout: true, + guestNameModel: this._localStorageModel }); this._messageCollection.listenTo(roomChannel, 'leaveCurrentCall', function() { @@ -535,10 +538,6 @@ this.connection = new OCA.Talk.Connection(this); this.token = $('#app').attr('data-token'); - if (!OC.getCurrentUser().uid) { - this.initGuestName(); - } - $(window).unload(function () { this.connection.leaveAllCalls(); this.signaling.disconnect(); diff --git a/js/views/chatview.js b/js/views/chatview.js index da3eb45ed73..740200276e5 100644 --- a/js/views/chatview.js +++ b/js/views/chatview.js @@ -38,7 +38,11 @@ '
    ' + '
    ' + '
    ' + - '
    {{actorDisplayName}}
    ' + + ' {{#if actorId}}' + + '
    {{actorDisplayName}}
    ' + + ' {{else}}' + + '
    ' + + ' {{/if}}' + '
    ' + '
    ' + '
    {{message}}
    ' + @@ -63,6 +67,14 @@ return 'chat' + (this._oldestOnTopLayout? ' oldestOnTopLayout': ''); }, + ui: { + 'guestName': 'div.guest-name' + }, + + regions: { + 'guestName': '@ui.guestName' + }, + events: { 'submit .newCommentForm': '_onSubmitComment', }, @@ -72,6 +84,18 @@ this.listenTo(this.collection, 'reset', this.render); this.listenTo(this.collection, 'add', this._onAddModel); + + this._guestNameEditableTextLabel = new OCA.SpreedMe.Views.EditableTextLabel({ + model: this.getOption('guestNameModel'), + modelAttribute: 'nick', + + extraClassNames: 'guest-name', + labelTagName: 'p', + labelPlaceholder: t('spreed', 'You'), + inputMaxLength: '20', + inputPlaceholder: t('spreed', 'Name'), + buttonTitle: t('spreed', 'Rename') + }); }, template: Handlebars.compile(TEMPLATE), @@ -84,19 +108,9 @@ this._addCommentTemplate = Handlebars.compile(ADD_COMMENT_TEMPLATE); } - if (OC.getCurrentUser().uid) { - return this._addCommentTemplate(_.extend({ - actorId: OC.getCurrentUser().uid, - actorDisplayName: OC.getCurrentUser().displayName, - newMessagePlaceholder: t('spreed', 'New message …'), - submitText: t('spreed', 'Send') - }, params)); - } - - var guestNick = OCA.SpreedMe.app._localStorageModel.get('nick'); return this._addCommentTemplate(_.extend({ - actorId: '', - actorDisplayName: guestNick ? guestNick : t('spreed', 'You'), + actorId: OC.getCurrentUser().uid, + actorDisplayName: OC.getCurrentUser().displayName, newMessagePlaceholder: t('spreed', 'New message …'), submitText: t('spreed', 'Send') }, params)); @@ -109,6 +123,10 @@ return this._commentTemplate(params); }, + onBeforeRender: function() { + this.getRegion('guestName').reset({ preventDestroy: true, allowMissingEl: true }); + }, + onRender: function() { delete this._lastAddedMessageModel; @@ -125,6 +143,7 @@ } else { this.$el.find('.avatar').imageplaceholder('?', undefined, 32); this.$el.find('.avatar').css('background-color', '#b9b9b9'); + this.showChildView('guestName', this._guestNameEditableTextLabel, { replaceElement: true, allowMissingEl: true } ); } this.delegateEvents(); From 1546caa4531a2badd01cc36581958d46ae49504f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 19 Mar 2018 13:42:46 +0100 Subject: [PATCH 04/16] Use the first char of the displayname if the guest has a name Signed-off-by: Joas Schilling --- js/views/chatview.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/js/views/chatview.js b/js/views/chatview.js index 740200276e5..19e7fccd14a 100644 --- a/js/views/chatview.js +++ b/js/views/chatview.js @@ -339,7 +339,11 @@ if (model.get('actorType') === 'users') { $this.avatar($this.data('user-id'), 32, undefined, false, undefined, $this.data('displayname')); } else { - $this.imageplaceholder('?', undefined, 32); + if (model.get('actorDisplayName')) { + $this.imageplaceholder(model.get('actorDisplayName')[0], undefined, 32); + } else { + $this.imageplaceholder('?', undefined, 32); + } $this.css('background-color', '#b9b9b9'); } }); From c2636cd6eb6e81557b0194ed60c68931b647b85a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 19 Mar 2018 18:20:26 +0100 Subject: [PATCH 05/16] Update the chat message models when the guest name changes Signed-off-by: Joas Schilling --- bower.json | 3 +- js/models/chatmessage.js | 6 + js/models/chatmessagecollection.js | 4 + js/vendor/jshashes/.bower.json | 40 + js/vendor/jshashes/LICENSE | 25 + js/vendor/jshashes/README.md | 273 +++++ js/vendor/jshashes/bower.json | 17 + js/vendor/jshashes/component.json | 9 + js/vendor/jshashes/hashes.js | 1765 ++++++++++++++++++++++++++++ js/vendor/jshashes/hashes.min.js | 2 + js/views/chatview.js | 2 +- js/webrtc.js | 1 + templates/index-public.php | 1 + templates/index.php | 1 + 14 files changed, 2147 insertions(+), 2 deletions(-) create mode 100644 js/vendor/jshashes/.bower.json create mode 100644 js/vendor/jshashes/LICENSE create mode 100644 js/vendor/jshashes/README.md create mode 100644 js/vendor/jshashes/bower.json create mode 100644 js/vendor/jshashes/component.json create mode 100644 js/vendor/jshashes/hashes.js create mode 100644 js/vendor/jshashes/hashes.min.js diff --git a/bower.json b/bower.json index 034f705dca7..6c52b2c8887 100644 --- a/bower.json +++ b/bower.json @@ -19,6 +19,7 @@ "dependencies": { "backbone": "1.2.3", "backbone.marionette": "3.0.0", - "jquery": "^2.0" + "jquery": "^2.0", + "jshashes": "^1.0" } } diff --git a/js/models/chatmessage.js b/js/models/chatmessage.js index 2449be730b6..04107248357 100644 --- a/js/models/chatmessage.js +++ b/js/models/chatmessage.js @@ -72,6 +72,12 @@ } return Backbone.Model.prototype.sync.call(this, method, model, options); + }, + + updateGuestName: function(data) { + if (this.get('actorType') === 'guests' && this.get('actorId') === data.sessionId && this.get('actorDisplayName') !== data.displayName) { + this.set('actorDisplayName', data.displayName); + } } }); diff --git a/js/models/chatmessagecollection.js b/js/models/chatmessagecollection.js index a56ac7d3b13..1f7ef182fa2 100644 --- a/js/models/chatmessagecollection.js +++ b/js/models/chatmessagecollection.js @@ -137,6 +137,10 @@ this.reset(); }, + updateGuestName: function(sessionId, newDisplayName) { + this.invoke('updateGuestName', {sessionId: sessionId, displayName: newDisplayName}); + }, + receiveMessages: function() { this.receiveMessagesAgain = true; diff --git a/js/vendor/jshashes/.bower.json b/js/vendor/jshashes/.bower.json new file mode 100644 index 00000000000..eb6257009fb --- /dev/null +++ b/js/vendor/jshashes/.bower.json @@ -0,0 +1,40 @@ +{ + "name": "jshashes", + "version": "1.0.7", + "description": "A fast and independent hashing library pure JavaScript implemented (ES3 compliant) for both server and client side (MD5, SHA1, SHA256, SHA512, RIPEMD, HMAC and Base64)", + "keywords": [ + "hash", + "md5", + "sha1", + "sha256", + "hashes", + "sha512", + "RIPEMD", + "base64", + "hmac", + "crc", + "encoding", + "algorithm" + ], + "author": "Tomas Aparicio ", + "main": "hashes.js", + "ignore": [ + "**/.*", + "bin", + "test", + "examples/server", + "package.json", + "Makefile", + "node_modules" + ], + "homepage": "https://github.com/h2non/jshashes", + "_release": "1.0.7", + "_resolution": { + "type": "version", + "tag": "v1.0.7", + "commit": "4cf568ea0bc9bec4f837d5d294932049407c98bb" + }, + "_source": "https://github.com/h2non/jshashes.git", + "_target": "^1.0", + "_originalSource": "jshashes" +} \ No newline at end of file diff --git a/js/vendor/jshashes/LICENSE b/js/vendor/jshashes/LICENSE new file mode 100644 index 00000000000..21236a27b6d --- /dev/null +++ b/js/vendor/jshashes/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) 2012-2017, Tomas Aparicio +Copyright (c) 1999-2012, Paul Johnston, Angel Marin, Jeremy Lin +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 and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the 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 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. diff --git a/js/vendor/jshashes/README.md b/js/vendor/jshashes/README.md new file mode 100644 index 00000000000..2b478971d43 --- /dev/null +++ b/js/vendor/jshashes/README.md @@ -0,0 +1,273 @@ +# jsHashes [![Build Status](https://travis-ci.org/h2non/jshashes.svg)](https://travis-ci.org/h2non/jshashes) [![NPM version](https://img.shields.io/npm/v/jshashes.svg)](https://www.npmjs.com/package/jshashes) + +`jshashes` is lightweight library implementing the most extended [cryptographic hash function](http://en.wikipedia.org/wiki/Cryptographic_hash_function) algorithms in pure JavaScript (ES5 compliant). + +The goal is to provide an dependency-free, fast and reliable solution for hash algorithms for both client-side and server-side JavaScript environments. +The code is fully compatible with the ECMAScript 5 specification and is used in production in browsers and [node.js](http://nodejs.org)/[io.js](http://iojs.org) + +If you are looking for a low-level performance library for the server-side, note that node.js/io.js provides its own native module: [`crypto`](http://nodejs.org/api/crypto.html) + +## Supported hash algorithms + +* `MD5` () +* `SHA1` () +* `SHA256` () +* `SHA512` () +* `HMAC` () +* `RIPEMD-160` () + +**Aditional functionalities** + +* `Base64 encoding/decoding` () +* `CRC-32 calculation` +* `UTF-8 encoding/decoding` + +## Environments + +- Browsers (ES3) +- node.js/io.js (all versions) +- Rhino +- RingoJS + +## Usage + +Each algorithm has its respective own instantiable `object`. Here you can see an example of how to create a new instance for each one: + +```javascript +// new MD5 instance +var MD5 = new Hashes.MD5 +// new SHA1 instance +var SHA1 = new Hashes.SHA1 +// new SHA256 instance +var SHA256 = new Hashes.SHA256 +// new SHA512 instace +var SHA512 = new Hashes.SHA512 +// new RIPEMD-160 instace +var RMD160 = new Hashes.RMD160 +``` + +An example of how to generate an hexadecimal-based hash encoding for each algorithm: + +```javascript +// sample string +var str = 'Sample text!' +// output to console +console.log('MD5: ' + MD5.hex(str)) +console.log('SHA1: ' + SHA1.hex(str)) +console.log('SHA256: ' + SHA256.hex(str)) +console.log('SHA512: ' + SHA512.hex(str)) +console.log('RIPEMD-160: ' + RMD160.hex(str)) +``` + +### Browsers + +This is a simple implementation for a client-side environment: + +```html + + + + + + + + +``` + +### node.js / io.js + +```javascript +// require the module +var Hashes = require('jshashes') +// sample string +var str = 'This is a sample text!' +// new SHA1 instance and base64 string encoding +var SHA1 = new Hashes.SHA1().b64(str) +// output to console +console.log('SHA1: ' + SHA1) +``` + +### Command-line interface + +You can use the simple command-line interface to generate hashes. + +```bash +$ hashes sha1-hex This is a sample string +> b6a8501d8a70e74e1dc12a6082102622fdc719bb + +# or with quotes +$ hashes sha1-hex "This is a sample string" +> b6a8501d8a70e74e1dc12a6082102622fdc719bb +``` + +For more information about the options supported, type: + +```bash +$ hashes -h +``` + +### Installation + +Via [npm](https://npmjs.org) + +``` +$ npm install jshashes +``` + +Via [Bower](http://bower.io/): +``` +$ bower install jshashes +``` + +Via [Component](https://github.com/component/component): +``` +$ component install h2non/jshashes +``` + +Or loading the script directly: +``` +http://cdn.rawgit.com/h2non/jsHashes/master/hashes.js +``` + +## Public methods + +Each algorithm `class` provides the following public methods: + +* `hex(string)` - Hexadecimal hash encoding from string. +* `b64(string)` - Base64 hash encondig from string. +* `any(string,encoding)` - Custom hash algorithm values encoding. +* `hex_hmac(key,string)` - Hexadecimal hash with HMAC salt key. +* `b64_hmac(key,string)` - Base64 hash with HMAC salt key. +* `any_hmac(key,string,encoding)` - Custom hash values encoding with HMAC salt key support. +* `vm_test()` - Simple self-test to see is working. Returns `this` Object. +* `setUpperCase(boolean)` - Enable/disable uppercase hexadecimal returned string. Returns `this` Object. +* `setPad(string)` - Defines a custom base64 pad string. Default is '=' according with the RFC standard. Returns `this` Object. +* `setUTF8(boolean)` - Enable/disable UTF-8 character encoding. Returns `this` Object. + +## Hash encoding formats supported + +* Hexadecimal (most extended) +* Base64 +* Custom hash values `any()` method + +## Benchmark + +Node.js 0.6.18 running on a VPS Intel I7 930 with 512 MB of RAM (see `server/benchmark.js`) + +```javascript +Simple benchmark test generating 10000 hashes for each algorithm. +String: "A0gTtNtKh3RaduBfIo59ZdfTc5pTdOQrkxdZ5EeVOIZh1cXxqPyexKZBg6VlE1KzIz6pd6r1LLIpT5B8THRfcGvbJElwhWBi9ZAE" + +* MD5 +** Done in: 205 miliseconds +* SHA1 +** Done in: 277 miliseconds +* SHA256 +** Done in: 525 miliseconds +* SHA512 +** Done in: 593 miliseconds +* RMD160 +** Done in: 383 miliseconds +``` + +See `client/benchmark.html` for client-side. + +## Notes + +* Don't support checksum hash for files on the server-side, only strings-based inputs are supported. +* It has not been planned to include support for more hash algorithms. +* The goal is to provide the same JavaScript code in both server and client side, so it isn't planned to improve it in other ways. +* Only Node.js server-side was tested, so with minimal changes, you can setup `jsHashes` in other server-side JS environment. + +## Changelog + +* `1.0.7` + - Merge #37: fix terminator statement token. +* `1.0.6` + - Fix #34: options `pad` typo. +* `1.0.4` + - Fix CLI script call error when use it from Bash + - Added CLI usage example +* `1.0.3` + - Important bugfixes to UTF-8 encoding (broken in 1.0.2) and the RIPEMD-160 hash (broken in 1.0.1). (gh #6) + - New test suite for hashes, CRC32, and hmac; run with 'npm test' in node. + - Fixed global variable leaks. (gh #13) + - CRC32 will now always return positive values. (gh #11) + - Added package version property to the exposed Hashes Object + - Updated CLI script utility supporting all algorithms (see bin/hashes) + - Fixed UTF-8 encoding/decoding error (if input parameter is undefined or invalid) +* `1.0.2` + - Performance improvements and minimal refactor (length property caching, literal notation) + - Available from Bower package manager +* `1.0.1` + - Refactoring (hoisting, coercion, removed redundant functions, scoping, restructure...) + - Performance improves + - JSLint validation (except bitwise operators) + - Now the library can be used like a AMD CommonJS module + - Updated documentation + - New folders structure + - Added closure compiled and minimized library version + - Available from Jam package manager +* `0.1.5b` + - Added index.js for easy call the module in Node.js + - Updated documentation +* `0.1.4b` + - Now declaring objects using Literal Notation. + - Solved sintax errors on minimized version (jshashes.min.js) + - Added benchmark test and sample +* `0.1.3b` + - Starting non-redundancy code refactorization + - Added `Helpers` Object with some global functions + - Added native support for Base64 provided as `class` + - Added CRC-32 calculation support + - Added URL encode/decode helpers functions +* `0.1.2b` + - SHA1 error fixed. + - General code changes (renaming classes, private methods, new methods...). + - Changing library namespace to 'Hashes'. + - Starting code documentation. + - Added new examples of how to use. +* `0.1.1b` + - Minimal library improvements. + - There has been added some samples, like how to use it and support for NPM package. +* `0.1.0b` + - First release: the code is stable, but the library is still beta and must be improved and documented. + +## TODO + +* Performance benchmarking + +## Authors + +### Library author + +* [Tomas Aparicio](https://github.com/h2non/) + +### Original algorithm authors + +* [Paul Johnston](http://pajhome.org.uk/crypt/md5/) +* Angel Marin (SHA256) +* Jeremy Lin (RIPEMD-160) + +### Other contributors + +* [C. Scott Ananian](https://github.com/cscott) +* Greg Holt +* Andrew Kepert +* Ydnar +* Lostinet + +## License + +jsHashes is released under `New BSD` license. See `LICENSE` file. + +## Issues + +Feel free to report any issue you experiment via Github . diff --git a/js/vendor/jshashes/bower.json b/js/vendor/jshashes/bower.json new file mode 100644 index 00000000000..ee95fa8da08 --- /dev/null +++ b/js/vendor/jshashes/bower.json @@ -0,0 +1,17 @@ +{ + "name": "jshashes", + "version": "1.0.7", + "description": "A fast and independent hashing library pure JavaScript implemented (ES3 compliant) for both server and client side (MD5, SHA1, SHA256, SHA512, RIPEMD, HMAC and Base64)", + "keywords": ["hash", "md5", "sha1", "sha256", "hashes", "sha512", "RIPEMD", "base64", "hmac", "crc", "encoding", "algorithm"], + "author": "Tomas Aparicio ", + "main": "hashes.js", + "ignore": [ + "**/.*", + "bin", + "test", + "examples/server", + "package.json", + "Makefile", + "node_modules" + ] +} diff --git a/js/vendor/jshashes/component.json b/js/vendor/jshashes/component.json new file mode 100644 index 00000000000..a5e9a6ee97e --- /dev/null +++ b/js/vendor/jshashes/component.json @@ -0,0 +1,9 @@ +{ + "name": "jshashes", + "main": "hashes.js", + "version": "1.0.5", + "scripts": [ + "hashes.js", + "hashes.min.js" + ] +} diff --git a/js/vendor/jshashes/hashes.js b/js/vendor/jshashes/hashes.js new file mode 100644 index 00000000000..9e62254ded2 --- /dev/null +++ b/js/vendor/jshashes/hashes.js @@ -0,0 +1,1765 @@ +/** + * jshashes - https://github.com/h2non/jshashes + * Released under the "New BSD" license + * + * Algorithms specification: + * + * MD5 - http://www.ietf.org/rfc/rfc1321.txt + * RIPEMD-160 - http://homes.esat.kuleuven.be/~bosselae/ripemd160.html + * SHA1 - http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf + * SHA256 - http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf + * SHA512 - http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf + * HMAC - http://www.ietf.org/rfc/rfc2104.txt + */ +(function() { + var Hashes; + + function utf8Encode(str) { + var x, y, output = '', + i = -1, + l; + + if (str && str.length) { + l = str.length; + while ((i += 1) < l) { + /* Decode utf-16 surrogate pairs */ + x = str.charCodeAt(i); + y = i + 1 < l ? str.charCodeAt(i + 1) : 0; + if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) { + x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF); + i += 1; + } + /* Encode output as utf-8 */ + if (x <= 0x7F) { + output += String.fromCharCode(x); + } else if (x <= 0x7FF) { + output += String.fromCharCode(0xC0 | ((x >>> 6) & 0x1F), + 0x80 | (x & 0x3F)); + } else if (x <= 0xFFFF) { + output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F), + 0x80 | ((x >>> 6) & 0x3F), + 0x80 | (x & 0x3F)); + } else if (x <= 0x1FFFFF) { + output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07), + 0x80 | ((x >>> 12) & 0x3F), + 0x80 | ((x >>> 6) & 0x3F), + 0x80 | (x & 0x3F)); + } + } + } + return output; + } + + function utf8Decode(str) { + var i, ac, c1, c2, c3, arr = [], + l; + i = ac = c1 = c2 = c3 = 0; + + if (str && str.length) { + l = str.length; + str += ''; + + while (i < l) { + c1 = str.charCodeAt(i); + ac += 1; + if (c1 < 128) { + arr[ac] = String.fromCharCode(c1); + i += 1; + } else if (c1 > 191 && c1 < 224) { + c2 = str.charCodeAt(i + 1); + arr[ac] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63)); + i += 2; + } else { + c2 = str.charCodeAt(i + 1); + c3 = str.charCodeAt(i + 2); + arr[ac] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); + i += 3; + } + } + } + return arr.join(''); + } + + /** + * Add integers, wrapping at 2^32. This uses 16-bit operations internally + * to work around bugs in some JS interpreters. + */ + + function safe_add(x, y) { + var lsw = (x & 0xFFFF) + (y & 0xFFFF), + msw = (x >> 16) + (y >> 16) + (lsw >> 16); + return (msw << 16) | (lsw & 0xFFFF); + } + + /** + * Bitwise rotate a 32-bit number to the left. + */ + + function bit_rol(num, cnt) { + return (num << cnt) | (num >>> (32 - cnt)); + } + + /** + * Convert a raw string to a hex string + */ + + function rstr2hex(input, hexcase) { + var hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef', + output = '', + x, i = 0, + l = input.length; + for (; i < l; i += 1) { + x = input.charCodeAt(i); + output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt(x & 0x0F); + } + return output; + } + + /** + * Encode a string as utf-16 + */ + + function str2rstr_utf16le(input) { + var i, l = input.length, + output = ''; + for (i = 0; i < l; i += 1) { + output += String.fromCharCode(input.charCodeAt(i) & 0xFF, (input.charCodeAt(i) >>> 8) & 0xFF); + } + return output; + } + + function str2rstr_utf16be(input) { + var i, l = input.length, + output = ''; + for (i = 0; i < l; i += 1) { + output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF, input.charCodeAt(i) & 0xFF); + } + return output; + } + + /** + * Convert an array of big-endian words to a string + */ + + function binb2rstr(input) { + var i, l = input.length * 32, + output = ''; + for (i = 0; i < l; i += 8) { + output += String.fromCharCode((input[i >> 5] >>> (24 - i % 32)) & 0xFF); + } + return output; + } + + /** + * Convert an array of little-endian words to a string + */ + + function binl2rstr(input) { + var i, l = input.length * 32, + output = ''; + for (i = 0; i < l; i += 8) { + output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF); + } + return output; + } + + /** + * Convert a raw string to an array of little-endian words + * Characters >255 have their high-byte silently ignored. + */ + + function rstr2binl(input) { + var i, l = input.length * 8, + output = Array(input.length >> 2), + lo = output.length; + for (i = 0; i < lo; i += 1) { + output[i] = 0; + } + for (i = 0; i < l; i += 8) { + output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32); + } + return output; + } + + /** + * Convert a raw string to an array of big-endian words + * Characters >255 have their high-byte silently ignored. + */ + + function rstr2binb(input) { + var i, l = input.length * 8, + output = Array(input.length >> 2), + lo = output.length; + for (i = 0; i < lo; i += 1) { + output[i] = 0; + } + for (i = 0; i < l; i += 8) { + output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32); + } + return output; + } + + /** + * Convert a raw string to an arbitrary string encoding + */ + + function rstr2any(input, encoding) { + var divisor = encoding.length, + remainders = Array(), + i, q, x, ld, quotient, dividend, output, full_length; + + /* Convert to an array of 16-bit big-endian values, forming the dividend */ + dividend = Array(Math.ceil(input.length / 2)); + ld = dividend.length; + for (i = 0; i < ld; i += 1) { + dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1); + } + + /** + * Repeatedly perform a long division. The binary array forms the dividend, + * the length of the encoding is the divisor. Once computed, the quotient + * forms the dividend for the next step. We stop when the dividend is zerHashes. + * All remainders are stored for later use. + */ + while (dividend.length > 0) { + quotient = Array(); + x = 0; + for (i = 0; i < dividend.length; i += 1) { + x = (x << 16) + dividend[i]; + q = Math.floor(x / divisor); + x -= q * divisor; + if (quotient.length > 0 || q > 0) { + quotient[quotient.length] = q; + } + } + remainders[remainders.length] = x; + dividend = quotient; + } + + /* Convert the remainders to the output string */ + output = ''; + for (i = remainders.length - 1; i >= 0; i--) { + output += encoding.charAt(remainders[i]); + } + + /* Append leading zero equivalents */ + full_length = Math.ceil(input.length * 8 / (Math.log(encoding.length) / Math.log(2))); + for (i = output.length; i < full_length; i += 1) { + output = encoding[0] + output; + } + return output; + } + + /** + * Convert a raw string to a base-64 string + */ + + function rstr2b64(input, b64pad) { + var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', + output = '', + len = input.length, + i, j, triplet; + b64pad = b64pad || '='; + for (i = 0; i < len; i += 3) { + triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i + 2) : 0); + for (j = 0; j < 4; j += 1) { + if (i * 8 + j * 6 > input.length * 8) { + output += b64pad; + } else { + output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F); + } + } + } + return output; + } + + Hashes = { + /** + * @property {String} version + * @readonly + */ + VERSION: '1.0.6', + /** + * @member Hashes + * @class Base64 + * @constructor + */ + Base64: function() { + // private properties + var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', + pad = '=', // default pad according with the RFC standard + url = false, // URL encoding support @todo + utf8 = true; // by default enable UTF-8 support encoding + + // public method for encoding + this.encode = function(input) { + var i, j, triplet, + output = '', + len = input.length; + + pad = pad || '='; + input = (utf8) ? utf8Encode(input) : input; + + for (i = 0; i < len; i += 3) { + triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i + 2) : 0); + for (j = 0; j < 4; j += 1) { + if (i * 8 + j * 6 > len * 8) { + output += pad; + } else { + output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F); + } + } + } + return output; + }; + + // public method for decoding + this.decode = function(input) { + // var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; + var i, o1, o2, o3, h1, h2, h3, h4, bits, ac, + dec = '', + arr = []; + if (!input) { + return input; + } + + i = ac = 0; + input = input.replace(new RegExp('\\' + pad, 'gi'), ''); // use '=' + //input += ''; + + do { // unpack four hexets into three octets using index points in b64 + h1 = tab.indexOf(input.charAt(i += 1)); + h2 = tab.indexOf(input.charAt(i += 1)); + h3 = tab.indexOf(input.charAt(i += 1)); + h4 = tab.indexOf(input.charAt(i += 1)); + + bits = h1 << 18 | h2 << 12 | h3 << 6 | h4; + + o1 = bits >> 16 & 0xff; + o2 = bits >> 8 & 0xff; + o3 = bits & 0xff; + ac += 1; + + if (h3 === 64) { + arr[ac] = String.fromCharCode(o1); + } else if (h4 === 64) { + arr[ac] = String.fromCharCode(o1, o2); + } else { + arr[ac] = String.fromCharCode(o1, o2, o3); + } + } while (i < input.length); + + dec = arr.join(''); + dec = (utf8) ? utf8Decode(dec) : dec; + + return dec; + }; + + // set custom pad string + this.setPad = function(str) { + pad = str || pad; + return this; + }; + // set custom tab string characters + this.setTab = function(str) { + tab = str || tab; + return this; + }; + this.setUTF8 = function(bool) { + if (typeof bool === 'boolean') { + utf8 = bool; + } + return this; + }; + }, + + /** + * CRC-32 calculation + * @member Hashes + * @method CRC32 + * @static + * @param {String} str Input String + * @return {String} + */ + CRC32: function(str) { + var crc = 0, + x = 0, + y = 0, + table, i, iTop; + str = utf8Encode(str); + + table = [ + '00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 ', + '79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 ', + '84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F ', + '63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD ', + 'A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC ', + '51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 ', + 'B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 ', + '06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 ', + 'E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 ', + '12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 ', + 'D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 ', + '33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 ', + 'CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 ', + '9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E ', + '7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D ', + '806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 ', + '60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA ', + 'AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 ', + '5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 ', + 'B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 ', + '05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 ', + 'F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA ', + '11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 ', + 'D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F ', + '30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E ', + 'C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D' + ].join(''); + + crc = crc ^ (-1); + for (i = 0, iTop = str.length; i < iTop; i += 1) { + y = (crc ^ str.charCodeAt(i)) & 0xFF; + x = '0x' + table.substr(y * 9, 8); + crc = (crc >>> 8) ^ x; + } + // always return a positive number (that's what >>> 0 does) + return (crc ^ (-1)) >>> 0; + }, + /** + * @member Hashes + * @class MD5 + * @constructor + * @param {Object} [config] + * + * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message + * Digest Algorithm, as defined in RFC 1321. + * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009 + * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet + * See for more infHashes. + */ + MD5: function(options) { + /** + * Private config properties. You may need to tweak these to be compatible with + * the server-side, but the defaults work in most cases. + * See {@link Hashes.MD5#method-setUpperCase} and {@link Hashes.SHA1#method-setUpperCase} + */ + var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase + b64pad = (options && typeof options.pad === 'string') ? options.pad : '=', // base-64 pad character. Defaults to '=' for strict RFC compliance + utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true; // enable/disable utf8 encoding + + // privileged (public) methods + this.hex = function(s) { + return rstr2hex(rstr(s, utf8), hexcase); + }; + this.b64 = function(s) { + return rstr2b64(rstr(s), b64pad); + }; + this.any = function(s, e) { + return rstr2any(rstr(s, utf8), e); + }; + this.raw = function(s) { + return rstr(s, utf8); + }; + this.hex_hmac = function(k, d) { + return rstr2hex(rstr_hmac(k, d), hexcase); + }; + this.b64_hmac = function(k, d) { + return rstr2b64(rstr_hmac(k, d), b64pad); + }; + this.any_hmac = function(k, d, e) { + return rstr2any(rstr_hmac(k, d), e); + }; + /** + * Perform a simple self-test to see if the VM is working + * @return {String} Hexadecimal hash sample + */ + this.vm_test = function() { + return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72'; + }; + /** + * Enable/disable uppercase hexadecimal returned string + * @param {Boolean} + * @return {Object} this + */ + this.setUpperCase = function(a) { + if (typeof a === 'boolean') { + hexcase = a; + } + return this; + }; + /** + * Defines a base64 pad string + * @param {String} Pad + * @return {Object} this + */ + this.setPad = function(a) { + b64pad = a || b64pad; + return this; + }; + /** + * Defines a base64 pad string + * @param {Boolean} + * @return {Object} [this] + */ + this.setUTF8 = function(a) { + if (typeof a === 'boolean') { + utf8 = a; + } + return this; + }; + + // private methods + + /** + * Calculate the MD5 of a raw string + */ + + function rstr(s) { + s = (utf8) ? utf8Encode(s) : s; + return binl2rstr(binl(rstr2binl(s), s.length * 8)); + } + + /** + * Calculate the HMAC-MD5, of a key and some data (raw strings) + */ + + function rstr_hmac(key, data) { + var bkey, ipad, opad, hash, i; + + key = (utf8) ? utf8Encode(key) : key; + data = (utf8) ? utf8Encode(data) : data; + bkey = rstr2binl(key); + if (bkey.length > 16) { + bkey = binl(bkey, key.length * 8); + } + + ipad = Array(16), opad = Array(16); + for (i = 0; i < 16; i += 1) { + ipad[i] = bkey[i] ^ 0x36363636; + opad[i] = bkey[i] ^ 0x5C5C5C5C; + } + hash = binl(ipad.concat(rstr2binl(data)), 512 + data.length * 8); + return binl2rstr(binl(opad.concat(hash), 512 + 128)); + } + + /** + * Calculate the MD5 of an array of little-endian words, and a bit length. + */ + + function binl(x, len) { + var i, olda, oldb, oldc, oldd, + a = 1732584193, + b = -271733879, + c = -1732584194, + d = 271733878; + + /* append padding */ + x[len >> 5] |= 0x80 << ((len) % 32); + x[(((len + 64) >>> 9) << 4) + 14] = len; + + for (i = 0; i < x.length; i += 16) { + olda = a; + oldb = b; + oldc = c; + oldd = d; + + a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936); + d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586); + c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819); + b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330); + a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897); + d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426); + c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341); + b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983); + a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416); + d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417); + c = md5_ff(c, d, a, b, x[i + 10], 17, -42063); + b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162); + a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682); + d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101); + c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290); + b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329); + + a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510); + d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632); + c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713); + b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302); + a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691); + d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083); + c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335); + b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848); + a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438); + d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690); + c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961); + b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501); + a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467); + d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784); + c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473); + b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734); + + a = md5_hh(a, b, c, d, x[i + 5], 4, -378558); + d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463); + c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562); + b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556); + a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060); + d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353); + c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632); + b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640); + a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174); + d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222); + c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979); + b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189); + a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487); + d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835); + c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520); + b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651); + + a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844); + d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415); + c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905); + b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055); + a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571); + d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606); + c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523); + b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799); + a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359); + d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744); + c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380); + b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649); + a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070); + d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379); + c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259); + b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551); + + a = safe_add(a, olda); + b = safe_add(b, oldb); + c = safe_add(c, oldc); + d = safe_add(d, oldd); + } + return Array(a, b, c, d); + } + + /** + * These functions implement the four basic operations the algorithm uses. + */ + + function md5_cmn(q, a, b, x, s, t) { + return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b); + } + + function md5_ff(a, b, c, d, x, s, t) { + return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t); + } + + function md5_gg(a, b, c, d, x, s, t) { + return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t); + } + + function md5_hh(a, b, c, d, x, s, t) { + return md5_cmn(b ^ c ^ d, a, b, x, s, t); + } + + function md5_ii(a, b, c, d, x, s, t) { + return md5_cmn(c ^ (b | (~d)), a, b, x, s, t); + } + }, + /** + * @member Hashes + * @class Hashes.SHA1 + * @param {Object} [config] + * @constructor + * + * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined in FIPS 180-1 + * Version 2.2 Copyright Paul Johnston 2000 - 2009. + * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet + * See http://pajhome.org.uk/crypt/md5 for details. + */ + SHA1: function(options) { + /** + * Private config properties. You may need to tweak these to be compatible with + * the server-side, but the defaults work in most cases. + * See {@link Hashes.MD5#method-setUpperCase} and {@link Hashes.SHA1#method-setUpperCase} + */ + var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase + b64pad = (options && typeof options.pad === 'string') ? options.pad : '=', // base-64 pad character. Defaults to '=' for strict RFC compliance + utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true; // enable/disable utf8 encoding + + // public methods + this.hex = function(s) { + return rstr2hex(rstr(s, utf8), hexcase); + }; + this.b64 = function(s) { + return rstr2b64(rstr(s, utf8), b64pad); + }; + this.any = function(s, e) { + return rstr2any(rstr(s, utf8), e); + }; + this.raw = function(s) { + return rstr(s, utf8); + }; + this.hex_hmac = function(k, d) { + return rstr2hex(rstr_hmac(k, d)); + }; + this.b64_hmac = function(k, d) { + return rstr2b64(rstr_hmac(k, d), b64pad); + }; + this.any_hmac = function(k, d, e) { + return rstr2any(rstr_hmac(k, d), e); + }; + /** + * Perform a simple self-test to see if the VM is working + * @return {String} Hexadecimal hash sample + * @public + */ + this.vm_test = function() { + return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72'; + }; + /** + * @description Enable/disable uppercase hexadecimal returned string + * @param {boolean} + * @return {Object} this + * @public + */ + this.setUpperCase = function(a) { + if (typeof a === 'boolean') { + hexcase = a; + } + return this; + }; + /** + * @description Defines a base64 pad string + * @param {string} Pad + * @return {Object} this + * @public + */ + this.setPad = function(a) { + b64pad = a || b64pad; + return this; + }; + /** + * @description Defines a base64 pad string + * @param {boolean} + * @return {Object} this + * @public + */ + this.setUTF8 = function(a) { + if (typeof a === 'boolean') { + utf8 = a; + } + return this; + }; + + // private methods + + /** + * Calculate the SHA-512 of a raw string + */ + + function rstr(s) { + s = (utf8) ? utf8Encode(s) : s; + return binb2rstr(binb(rstr2binb(s), s.length * 8)); + } + + /** + * Calculate the HMAC-SHA1 of a key and some data (raw strings) + */ + + function rstr_hmac(key, data) { + var bkey, ipad, opad, i, hash; + key = (utf8) ? utf8Encode(key) : key; + data = (utf8) ? utf8Encode(data) : data; + bkey = rstr2binb(key); + + if (bkey.length > 16) { + bkey = binb(bkey, key.length * 8); + } + ipad = Array(16), opad = Array(16); + for (i = 0; i < 16; i += 1) { + ipad[i] = bkey[i] ^ 0x36363636; + opad[i] = bkey[i] ^ 0x5C5C5C5C; + } + hash = binb(ipad.concat(rstr2binb(data)), 512 + data.length * 8); + return binb2rstr(binb(opad.concat(hash), 512 + 160)); + } + + /** + * Calculate the SHA-1 of an array of big-endian words, and a bit length + */ + + function binb(x, len) { + var i, j, t, olda, oldb, oldc, oldd, olde, + w = Array(80), + a = 1732584193, + b = -271733879, + c = -1732584194, + d = 271733878, + e = -1009589776; + + /* append padding */ + x[len >> 5] |= 0x80 << (24 - len % 32); + x[((len + 64 >> 9) << 4) + 15] = len; + + for (i = 0; i < x.length; i += 16) { + olda = a; + oldb = b; + oldc = c; + oldd = d; + olde = e; + + for (j = 0; j < 80; j += 1) { + if (j < 16) { + w[j] = x[i + j]; + } else { + w[j] = bit_rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1); + } + t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)), + safe_add(safe_add(e, w[j]), sha1_kt(j))); + e = d; + d = c; + c = bit_rol(b, 30); + b = a; + a = t; + } + + a = safe_add(a, olda); + b = safe_add(b, oldb); + c = safe_add(c, oldc); + d = safe_add(d, oldd); + e = safe_add(e, olde); + } + return Array(a, b, c, d, e); + } + + /** + * Perform the appropriate triplet combination function for the current + * iteration + */ + + function sha1_ft(t, b, c, d) { + if (t < 20) { + return (b & c) | ((~b) & d); + } + if (t < 40) { + return b ^ c ^ d; + } + if (t < 60) { + return (b & c) | (b & d) | (c & d); + } + return b ^ c ^ d; + } + + /** + * Determine the appropriate additive constant for the current iteration + */ + + function sha1_kt(t) { + return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : + (t < 60) ? -1894007588 : -899497514; + } + }, + /** + * @class Hashes.SHA256 + * @param {config} + * + * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined in FIPS 180-2 + * Version 2.2 Copyright Angel Marin, Paul Johnston 2000 - 2009. + * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet + * See http://pajhome.org.uk/crypt/md5 for details. + * Also http://anmar.eu.org/projects/jssha2/ + */ + SHA256: function(options) { + /** + * Private properties configuration variables. You may need to tweak these to be compatible with + * the server-side, but the defaults work in most cases. + * @see this.setUpperCase() method + * @see this.setPad() method + */ + var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase */ + b64pad = (options && typeof options.pad === 'string') ? options.pad : '=', + /* base-64 pad character. Default '=' for strict RFC compliance */ + utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true, + /* enable/disable utf8 encoding */ + sha256_K; + + /* privileged (public) methods */ + this.hex = function(s) { + return rstr2hex(rstr(s, utf8)); + }; + this.b64 = function(s) { + return rstr2b64(rstr(s, utf8), b64pad); + }; + this.any = function(s, e) { + return rstr2any(rstr(s, utf8), e); + }; + this.raw = function(s) { + return rstr(s, utf8); + }; + this.hex_hmac = function(k, d) { + return rstr2hex(rstr_hmac(k, d)); + }; + this.b64_hmac = function(k, d) { + return rstr2b64(rstr_hmac(k, d), b64pad); + }; + this.any_hmac = function(k, d, e) { + return rstr2any(rstr_hmac(k, d), e); + }; + /** + * Perform a simple self-test to see if the VM is working + * @return {String} Hexadecimal hash sample + * @public + */ + this.vm_test = function() { + return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72'; + }; + /** + * Enable/disable uppercase hexadecimal returned string + * @param {boolean} + * @return {Object} this + * @public + */ + this.setUpperCase = function(a) { + if (typeof a === 'boolean') { + hexcase = a; + } + return this; + }; + /** + * @description Defines a base64 pad string + * @param {string} Pad + * @return {Object} this + * @public + */ + this.setPad = function(a) { + b64pad = a || b64pad; + return this; + }; + /** + * Defines a base64 pad string + * @param {boolean} + * @return {Object} this + * @public + */ + this.setUTF8 = function(a) { + if (typeof a === 'boolean') { + utf8 = a; + } + return this; + }; + + // private methods + + /** + * Calculate the SHA-512 of a raw string + */ + + function rstr(s, utf8) { + s = (utf8) ? utf8Encode(s) : s; + return binb2rstr(binb(rstr2binb(s), s.length * 8)); + } + + /** + * Calculate the HMAC-sha256 of a key and some data (raw strings) + */ + + function rstr_hmac(key, data) { + key = (utf8) ? utf8Encode(key) : key; + data = (utf8) ? utf8Encode(data) : data; + var hash, i = 0, + bkey = rstr2binb(key), + ipad = Array(16), + opad = Array(16); + + if (bkey.length > 16) { + bkey = binb(bkey, key.length * 8); + } + + for (; i < 16; i += 1) { + ipad[i] = bkey[i] ^ 0x36363636; + opad[i] = bkey[i] ^ 0x5C5C5C5C; + } + + hash = binb(ipad.concat(rstr2binb(data)), 512 + data.length * 8); + return binb2rstr(binb(opad.concat(hash), 512 + 256)); + } + + /* + * Main sha256 function, with its support functions + */ + + function sha256_S(X, n) { + return (X >>> n) | (X << (32 - n)); + } + + function sha256_R(X, n) { + return (X >>> n); + } + + function sha256_Ch(x, y, z) { + return ((x & y) ^ ((~x) & z)); + } + + function sha256_Maj(x, y, z) { + return ((x & y) ^ (x & z) ^ (y & z)); + } + + function sha256_Sigma0256(x) { + return (sha256_S(x, 2) ^ sha256_S(x, 13) ^ sha256_S(x, 22)); + } + + function sha256_Sigma1256(x) { + return (sha256_S(x, 6) ^ sha256_S(x, 11) ^ sha256_S(x, 25)); + } + + function sha256_Gamma0256(x) { + return (sha256_S(x, 7) ^ sha256_S(x, 18) ^ sha256_R(x, 3)); + } + + function sha256_Gamma1256(x) { + return (sha256_S(x, 17) ^ sha256_S(x, 19) ^ sha256_R(x, 10)); + } + + function sha256_Sigma0512(x) { + return (sha256_S(x, 28) ^ sha256_S(x, 34) ^ sha256_S(x, 39)); + } + + function sha256_Sigma1512(x) { + return (sha256_S(x, 14) ^ sha256_S(x, 18) ^ sha256_S(x, 41)); + } + + function sha256_Gamma0512(x) { + return (sha256_S(x, 1) ^ sha256_S(x, 8) ^ sha256_R(x, 7)); + } + + function sha256_Gamma1512(x) { + return (sha256_S(x, 19) ^ sha256_S(x, 61) ^ sha256_R(x, 6)); + } + + sha256_K = [ + 1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993, -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987, + 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522, + 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585, + 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, + 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885, -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344, + 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, + 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872, -1866530822, -1538233109, -1090935817, -965641998 + ]; + + function binb(m, l) { + var HASH = [1779033703, -1150833019, 1013904242, -1521486534, + 1359893119, -1694144372, 528734635, 1541459225 + ]; + var W = new Array(64); + var a, b, c, d, e, f, g, h; + var i, j, T1, T2; + + /* append padding */ + m[l >> 5] |= 0x80 << (24 - l % 32); + m[((l + 64 >> 9) << 4) + 15] = l; + + for (i = 0; i < m.length; i += 16) { + a = HASH[0]; + b = HASH[1]; + c = HASH[2]; + d = HASH[3]; + e = HASH[4]; + f = HASH[5]; + g = HASH[6]; + h = HASH[7]; + + for (j = 0; j < 64; j += 1) { + if (j < 16) { + W[j] = m[j + i]; + } else { + W[j] = safe_add(safe_add(safe_add(sha256_Gamma1256(W[j - 2]), W[j - 7]), + sha256_Gamma0256(W[j - 15])), W[j - 16]); + } + + T1 = safe_add(safe_add(safe_add(safe_add(h, sha256_Sigma1256(e)), sha256_Ch(e, f, g)), + sha256_K[j]), W[j]); + T2 = safe_add(sha256_Sigma0256(a), sha256_Maj(a, b, c)); + h = g; + g = f; + f = e; + e = safe_add(d, T1); + d = c; + c = b; + b = a; + a = safe_add(T1, T2); + } + + HASH[0] = safe_add(a, HASH[0]); + HASH[1] = safe_add(b, HASH[1]); + HASH[2] = safe_add(c, HASH[2]); + HASH[3] = safe_add(d, HASH[3]); + HASH[4] = safe_add(e, HASH[4]); + HASH[5] = safe_add(f, HASH[5]); + HASH[6] = safe_add(g, HASH[6]); + HASH[7] = safe_add(h, HASH[7]); + } + return HASH; + } + + }, + + /** + * @class Hashes.SHA512 + * @param {config} + * + * A JavaScript implementation of the Secure Hash Algorithm, SHA-512, as defined in FIPS 180-2 + * Version 2.2 Copyright Anonymous Contributor, Paul Johnston 2000 - 2009. + * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet + * See http://pajhome.org.uk/crypt/md5 for details. + */ + SHA512: function(options) { + /** + * Private properties configuration variables. You may need to tweak these to be compatible with + * the server-side, but the defaults work in most cases. + * @see this.setUpperCase() method + * @see this.setPad() method + */ + var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, + /* hexadecimal output case format. false - lowercase; true - uppercase */ + b64pad = (options && typeof options.pad === 'string') ? options.pad : '=', + /* base-64 pad character. Default '=' for strict RFC compliance */ + utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true, + /* enable/disable utf8 encoding */ + sha512_k; + + /* privileged (public) methods */ + this.hex = function(s) { + return rstr2hex(rstr(s)); + }; + this.b64 = function(s) { + return rstr2b64(rstr(s), b64pad); + }; + this.any = function(s, e) { + return rstr2any(rstr(s), e); + }; + this.raw = function(s) { + return rstr(s, utf8); + }; + this.hex_hmac = function(k, d) { + return rstr2hex(rstr_hmac(k, d)); + }; + this.b64_hmac = function(k, d) { + return rstr2b64(rstr_hmac(k, d), b64pad); + }; + this.any_hmac = function(k, d, e) { + return rstr2any(rstr_hmac(k, d), e); + }; + /** + * Perform a simple self-test to see if the VM is working + * @return {String} Hexadecimal hash sample + * @public + */ + this.vm_test = function() { + return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72'; + }; + /** + * @description Enable/disable uppercase hexadecimal returned string + * @param {boolean} + * @return {Object} this + * @public + */ + this.setUpperCase = function(a) { + if (typeof a === 'boolean') { + hexcase = a; + } + return this; + }; + /** + * @description Defines a base64 pad string + * @param {string} Pad + * @return {Object} this + * @public + */ + this.setPad = function(a) { + b64pad = a || b64pad; + return this; + }; + /** + * @description Defines a base64 pad string + * @param {boolean} + * @return {Object} this + * @public + */ + this.setUTF8 = function(a) { + if (typeof a === 'boolean') { + utf8 = a; + } + return this; + }; + + /* private methods */ + + /** + * Calculate the SHA-512 of a raw string + */ + + function rstr(s) { + s = (utf8) ? utf8Encode(s) : s; + return binb2rstr(binb(rstr2binb(s), s.length * 8)); + } + /* + * Calculate the HMAC-SHA-512 of a key and some data (raw strings) + */ + + function rstr_hmac(key, data) { + key = (utf8) ? utf8Encode(key) : key; + data = (utf8) ? utf8Encode(data) : data; + + var hash, i = 0, + bkey = rstr2binb(key), + ipad = Array(32), + opad = Array(32); + + if (bkey.length > 32) { + bkey = binb(bkey, key.length * 8); + } + + for (; i < 32; i += 1) { + ipad[i] = bkey[i] ^ 0x36363636; + opad[i] = bkey[i] ^ 0x5C5C5C5C; + } + + hash = binb(ipad.concat(rstr2binb(data)), 1024 + data.length * 8); + return binb2rstr(binb(opad.concat(hash), 1024 + 512)); + } + + /** + * Calculate the SHA-512 of an array of big-endian dwords, and a bit length + */ + + function binb(x, len) { + var j, i, l, + W = new Array(80), + hash = new Array(16), + //Initial hash values + H = [ + new int64(0x6a09e667, -205731576), + new int64(-1150833019, -2067093701), + new int64(0x3c6ef372, -23791573), + new int64(-1521486534, 0x5f1d36f1), + new int64(0x510e527f, -1377402159), + new int64(-1694144372, 0x2b3e6c1f), + new int64(0x1f83d9ab, -79577749), + new int64(0x5be0cd19, 0x137e2179) + ], + T1 = new int64(0, 0), + T2 = new int64(0, 0), + a = new int64(0, 0), + b = new int64(0, 0), + c = new int64(0, 0), + d = new int64(0, 0), + e = new int64(0, 0), + f = new int64(0, 0), + g = new int64(0, 0), + h = new int64(0, 0), + //Temporary variables not specified by the document + s0 = new int64(0, 0), + s1 = new int64(0, 0), + Ch = new int64(0, 0), + Maj = new int64(0, 0), + r1 = new int64(0, 0), + r2 = new int64(0, 0), + r3 = new int64(0, 0); + + if (sha512_k === undefined) { + //SHA512 constants + sha512_k = [ + new int64(0x428a2f98, -685199838), new int64(0x71374491, 0x23ef65cd), + new int64(-1245643825, -330482897), new int64(-373957723, -2121671748), + new int64(0x3956c25b, -213338824), new int64(0x59f111f1, -1241133031), + new int64(-1841331548, -1357295717), new int64(-1424204075, -630357736), + new int64(-670586216, -1560083902), new int64(0x12835b01, 0x45706fbe), + new int64(0x243185be, 0x4ee4b28c), new int64(0x550c7dc3, -704662302), + new int64(0x72be5d74, -226784913), new int64(-2132889090, 0x3b1696b1), + new int64(-1680079193, 0x25c71235), new int64(-1046744716, -815192428), + new int64(-459576895, -1628353838), new int64(-272742522, 0x384f25e3), + new int64(0xfc19dc6, -1953704523), new int64(0x240ca1cc, 0x77ac9c65), + new int64(0x2de92c6f, 0x592b0275), new int64(0x4a7484aa, 0x6ea6e483), + new int64(0x5cb0a9dc, -1119749164), new int64(0x76f988da, -2096016459), + new int64(-1740746414, -295247957), new int64(-1473132947, 0x2db43210), + new int64(-1341970488, -1728372417), new int64(-1084653625, -1091629340), + new int64(-958395405, 0x3da88fc2), new int64(-710438585, -1828018395), + new int64(0x6ca6351, -536640913), new int64(0x14292967, 0xa0e6e70), + new int64(0x27b70a85, 0x46d22ffc), new int64(0x2e1b2138, 0x5c26c926), + new int64(0x4d2c6dfc, 0x5ac42aed), new int64(0x53380d13, -1651133473), + new int64(0x650a7354, -1951439906), new int64(0x766a0abb, 0x3c77b2a8), + new int64(-2117940946, 0x47edaee6), new int64(-1838011259, 0x1482353b), + new int64(-1564481375, 0x4cf10364), new int64(-1474664885, -1136513023), + new int64(-1035236496, -789014639), new int64(-949202525, 0x654be30), + new int64(-778901479, -688958952), new int64(-694614492, 0x5565a910), + new int64(-200395387, 0x5771202a), new int64(0x106aa070, 0x32bbd1b8), + new int64(0x19a4c116, -1194143544), new int64(0x1e376c08, 0x5141ab53), + new int64(0x2748774c, -544281703), new int64(0x34b0bcb5, -509917016), + new int64(0x391c0cb3, -976659869), new int64(0x4ed8aa4a, -482243893), + new int64(0x5b9cca4f, 0x7763e373), new int64(0x682e6ff3, -692930397), + new int64(0x748f82ee, 0x5defb2fc), new int64(0x78a5636f, 0x43172f60), + new int64(-2067236844, -1578062990), new int64(-1933114872, 0x1a6439ec), + new int64(-1866530822, 0x23631e28), new int64(-1538233109, -561857047), + new int64(-1090935817, -1295615723), new int64(-965641998, -479046869), + new int64(-903397682, -366583396), new int64(-779700025, 0x21c0c207), + new int64(-354779690, -840897762), new int64(-176337025, -294727304), + new int64(0x6f067aa, 0x72176fba), new int64(0xa637dc5, -1563912026), + new int64(0x113f9804, -1090974290), new int64(0x1b710b35, 0x131c471b), + new int64(0x28db77f5, 0x23047d84), new int64(0x32caab7b, 0x40c72493), + new int64(0x3c9ebe0a, 0x15c9bebc), new int64(0x431d67c4, -1676669620), + new int64(0x4cc5d4be, -885112138), new int64(0x597f299c, -60457430), + new int64(0x5fcb6fab, 0x3ad6faec), new int64(0x6c44198c, 0x4a475817) + ]; + } + + for (i = 0; i < 80; i += 1) { + W[i] = new int64(0, 0); + } + + // append padding to the source string. The format is described in the FIPS. + x[len >> 5] |= 0x80 << (24 - (len & 0x1f)); + x[((len + 128 >> 10) << 5) + 31] = len; + l = x.length; + for (i = 0; i < l; i += 32) { //32 dwords is the block size + int64copy(a, H[0]); + int64copy(b, H[1]); + int64copy(c, H[2]); + int64copy(d, H[3]); + int64copy(e, H[4]); + int64copy(f, H[5]); + int64copy(g, H[6]); + int64copy(h, H[7]); + + for (j = 0; j < 16; j += 1) { + W[j].h = x[i + 2 * j]; + W[j].l = x[i + 2 * j + 1]; + } + + for (j = 16; j < 80; j += 1) { + //sigma1 + int64rrot(r1, W[j - 2], 19); + int64revrrot(r2, W[j - 2], 29); + int64shr(r3, W[j - 2], 6); + s1.l = r1.l ^ r2.l ^ r3.l; + s1.h = r1.h ^ r2.h ^ r3.h; + //sigma0 + int64rrot(r1, W[j - 15], 1); + int64rrot(r2, W[j - 15], 8); + int64shr(r3, W[j - 15], 7); + s0.l = r1.l ^ r2.l ^ r3.l; + s0.h = r1.h ^ r2.h ^ r3.h; + + int64add4(W[j], s1, W[j - 7], s0, W[j - 16]); + } + + for (j = 0; j < 80; j += 1) { + //Ch + Ch.l = (e.l & f.l) ^ (~e.l & g.l); + Ch.h = (e.h & f.h) ^ (~e.h & g.h); + + //Sigma1 + int64rrot(r1, e, 14); + int64rrot(r2, e, 18); + int64revrrot(r3, e, 9); + s1.l = r1.l ^ r2.l ^ r3.l; + s1.h = r1.h ^ r2.h ^ r3.h; + + //Sigma0 + int64rrot(r1, a, 28); + int64revrrot(r2, a, 2); + int64revrrot(r3, a, 7); + s0.l = r1.l ^ r2.l ^ r3.l; + s0.h = r1.h ^ r2.h ^ r3.h; + + //Maj + Maj.l = (a.l & b.l) ^ (a.l & c.l) ^ (b.l & c.l); + Maj.h = (a.h & b.h) ^ (a.h & c.h) ^ (b.h & c.h); + + int64add5(T1, h, s1, Ch, sha512_k[j], W[j]); + int64add(T2, s0, Maj); + + int64copy(h, g); + int64copy(g, f); + int64copy(f, e); + int64add(e, d, T1); + int64copy(d, c); + int64copy(c, b); + int64copy(b, a); + int64add(a, T1, T2); + } + int64add(H[0], H[0], a); + int64add(H[1], H[1], b); + int64add(H[2], H[2], c); + int64add(H[3], H[3], d); + int64add(H[4], H[4], e); + int64add(H[5], H[5], f); + int64add(H[6], H[6], g); + int64add(H[7], H[7], h); + } + + //represent the hash as an array of 32-bit dwords + for (i = 0; i < 8; i += 1) { + hash[2 * i] = H[i].h; + hash[2 * i + 1] = H[i].l; + } + return hash; + } + + //A constructor for 64-bit numbers + + function int64(h, l) { + this.h = h; + this.l = l; + //this.toString = int64toString; + } + + //Copies src into dst, assuming both are 64-bit numbers + + function int64copy(dst, src) { + dst.h = src.h; + dst.l = src.l; + } + + //Right-rotates a 64-bit number by shift + //Won't handle cases of shift>=32 + //The function revrrot() is for that + + function int64rrot(dst, x, shift) { + dst.l = (x.l >>> shift) | (x.h << (32 - shift)); + dst.h = (x.h >>> shift) | (x.l << (32 - shift)); + } + + //Reverses the dwords of the source and then rotates right by shift. + //This is equivalent to rotation by 32+shift + + function int64revrrot(dst, x, shift) { + dst.l = (x.h >>> shift) | (x.l << (32 - shift)); + dst.h = (x.l >>> shift) | (x.h << (32 - shift)); + } + + //Bitwise-shifts right a 64-bit number by shift + //Won't handle shift>=32, but it's never needed in SHA512 + + function int64shr(dst, x, shift) { + dst.l = (x.l >>> shift) | (x.h << (32 - shift)); + dst.h = (x.h >>> shift); + } + + //Adds two 64-bit numbers + //Like the original implementation, does not rely on 32-bit operations + + function int64add(dst, x, y) { + var w0 = (x.l & 0xffff) + (y.l & 0xffff); + var w1 = (x.l >>> 16) + (y.l >>> 16) + (w0 >>> 16); + var w2 = (x.h & 0xffff) + (y.h & 0xffff) + (w1 >>> 16); + var w3 = (x.h >>> 16) + (y.h >>> 16) + (w2 >>> 16); + dst.l = (w0 & 0xffff) | (w1 << 16); + dst.h = (w2 & 0xffff) | (w3 << 16); + } + + //Same, except with 4 addends. Works faster than adding them one by one. + + function int64add4(dst, a, b, c, d) { + var w0 = (a.l & 0xffff) + (b.l & 0xffff) + (c.l & 0xffff) + (d.l & 0xffff); + var w1 = (a.l >>> 16) + (b.l >>> 16) + (c.l >>> 16) + (d.l >>> 16) + (w0 >>> 16); + var w2 = (a.h & 0xffff) + (b.h & 0xffff) + (c.h & 0xffff) + (d.h & 0xffff) + (w1 >>> 16); + var w3 = (a.h >>> 16) + (b.h >>> 16) + (c.h >>> 16) + (d.h >>> 16) + (w2 >>> 16); + dst.l = (w0 & 0xffff) | (w1 << 16); + dst.h = (w2 & 0xffff) | (w3 << 16); + } + + //Same, except with 5 addends + + function int64add5(dst, a, b, c, d, e) { + var w0 = (a.l & 0xffff) + (b.l & 0xffff) + (c.l & 0xffff) + (d.l & 0xffff) + (e.l & 0xffff), + w1 = (a.l >>> 16) + (b.l >>> 16) + (c.l >>> 16) + (d.l >>> 16) + (e.l >>> 16) + (w0 >>> 16), + w2 = (a.h & 0xffff) + (b.h & 0xffff) + (c.h & 0xffff) + (d.h & 0xffff) + (e.h & 0xffff) + (w1 >>> 16), + w3 = (a.h >>> 16) + (b.h >>> 16) + (c.h >>> 16) + (d.h >>> 16) + (e.h >>> 16) + (w2 >>> 16); + dst.l = (w0 & 0xffff) | (w1 << 16); + dst.h = (w2 & 0xffff) | (w3 << 16); + } + }, + /** + * @class Hashes.RMD160 + * @constructor + * @param {Object} [config] + * + * A JavaScript implementation of the RIPEMD-160 Algorithm + * Version 2.2 Copyright Jeremy Lin, Paul Johnston 2000 - 2009. + * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet + * See http://pajhome.org.uk/crypt/md5 for details. + * Also http://www.ocf.berkeley.edu/~jjlin/jsotp/ + */ + RMD160: function(options) { + /** + * Private properties configuration variables. You may need to tweak these to be compatible with + * the server-side, but the defaults work in most cases. + * @see this.setUpperCase() method + * @see this.setPad() method + */ + var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, + /* hexadecimal output case format. false - lowercase; true - uppercase */ + b64pad = (options && typeof options.pad === 'string') ? options.pa : '=', + /* base-64 pad character. Default '=' for strict RFC compliance */ + utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true, + /* enable/disable utf8 encoding */ + rmd160_r1 = [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, + 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, + 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, + 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13 + ], + rmd160_r2 = [ + 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, + 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, + 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, + 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, + 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11 + ], + rmd160_s1 = [ + 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, + 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, + 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, + 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, + 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 + ], + rmd160_s2 = [ + 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, + 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, + 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, + 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, + 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 + ]; + + /* privileged (public) methods */ + this.hex = function(s) { + return rstr2hex(rstr(s, utf8)); + }; + this.b64 = function(s) { + return rstr2b64(rstr(s, utf8), b64pad); + }; + this.any = function(s, e) { + return rstr2any(rstr(s, utf8), e); + }; + this.raw = function(s) { + return rstr(s, utf8); + }; + this.hex_hmac = function(k, d) { + return rstr2hex(rstr_hmac(k, d)); + }; + this.b64_hmac = function(k, d) { + return rstr2b64(rstr_hmac(k, d), b64pad); + }; + this.any_hmac = function(k, d, e) { + return rstr2any(rstr_hmac(k, d), e); + }; + /** + * Perform a simple self-test to see if the VM is working + * @return {String} Hexadecimal hash sample + * @public + */ + this.vm_test = function() { + return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72'; + }; + /** + * @description Enable/disable uppercase hexadecimal returned string + * @param {boolean} + * @return {Object} this + * @public + */ + this.setUpperCase = function(a) { + if (typeof a === 'boolean') { + hexcase = a; + } + return this; + }; + /** + * @description Defines a base64 pad string + * @param {string} Pad + * @return {Object} this + * @public + */ + this.setPad = function(a) { + if (typeof a !== 'undefined') { + b64pad = a; + } + return this; + }; + /** + * @description Defines a base64 pad string + * @param {boolean} + * @return {Object} this + * @public + */ + this.setUTF8 = function(a) { + if (typeof a === 'boolean') { + utf8 = a; + } + return this; + }; + + /* private methods */ + + /** + * Calculate the rmd160 of a raw string + */ + + function rstr(s) { + s = (utf8) ? utf8Encode(s) : s; + return binl2rstr(binl(rstr2binl(s), s.length * 8)); + } + + /** + * Calculate the HMAC-rmd160 of a key and some data (raw strings) + */ + + function rstr_hmac(key, data) { + key = (utf8) ? utf8Encode(key) : key; + data = (utf8) ? utf8Encode(data) : data; + var i, hash, + bkey = rstr2binl(key), + ipad = Array(16), + opad = Array(16); + + if (bkey.length > 16) { + bkey = binl(bkey, key.length * 8); + } + + for (i = 0; i < 16; i += 1) { + ipad[i] = bkey[i] ^ 0x36363636; + opad[i] = bkey[i] ^ 0x5C5C5C5C; + } + hash = binl(ipad.concat(rstr2binl(data)), 512 + data.length * 8); + return binl2rstr(binl(opad.concat(hash), 512 + 160)); + } + + /** + * Convert an array of little-endian words to a string + */ + + function binl2rstr(input) { + var i, output = '', + l = input.length * 32; + for (i = 0; i < l; i += 8) { + output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF); + } + return output; + } + + /** + * Calculate the RIPE-MD160 of an array of little-endian words, and a bit length. + */ + + function binl(x, len) { + var T, j, i, l, + h0 = 0x67452301, + h1 = 0xefcdab89, + h2 = 0x98badcfe, + h3 = 0x10325476, + h4 = 0xc3d2e1f0, + A1, B1, C1, D1, E1, + A2, B2, C2, D2, E2; + + /* append padding */ + x[len >> 5] |= 0x80 << (len % 32); + x[(((len + 64) >>> 9) << 4) + 14] = len; + l = x.length; + + for (i = 0; i < l; i += 16) { + A1 = A2 = h0; + B1 = B2 = h1; + C1 = C2 = h2; + D1 = D2 = h3; + E1 = E2 = h4; + for (j = 0; j <= 79; j += 1) { + T = safe_add(A1, rmd160_f(j, B1, C1, D1)); + T = safe_add(T, x[i + rmd160_r1[j]]); + T = safe_add(T, rmd160_K1(j)); + T = safe_add(bit_rol(T, rmd160_s1[j]), E1); + A1 = E1; + E1 = D1; + D1 = bit_rol(C1, 10); + C1 = B1; + B1 = T; + T = safe_add(A2, rmd160_f(79 - j, B2, C2, D2)); + T = safe_add(T, x[i + rmd160_r2[j]]); + T = safe_add(T, rmd160_K2(j)); + T = safe_add(bit_rol(T, rmd160_s2[j]), E2); + A2 = E2; + E2 = D2; + D2 = bit_rol(C2, 10); + C2 = B2; + B2 = T; + } + + T = safe_add(h1, safe_add(C1, D2)); + h1 = safe_add(h2, safe_add(D1, E2)); + h2 = safe_add(h3, safe_add(E1, A2)); + h3 = safe_add(h4, safe_add(A1, B2)); + h4 = safe_add(h0, safe_add(B1, C2)); + h0 = T; + } + return [h0, h1, h2, h3, h4]; + } + + // specific algorithm methods + + function rmd160_f(j, x, y, z) { + return (0 <= j && j <= 15) ? (x ^ y ^ z) : + (16 <= j && j <= 31) ? (x & y) | (~x & z) : + (32 <= j && j <= 47) ? (x | ~y) ^ z : + (48 <= j && j <= 63) ? (x & z) | (y & ~z) : + (64 <= j && j <= 79) ? x ^ (y | ~z) : + 'rmd160_f: j out of range'; + } + + function rmd160_K1(j) { + return (0 <= j && j <= 15) ? 0x00000000 : + (16 <= j && j <= 31) ? 0x5a827999 : + (32 <= j && j <= 47) ? 0x6ed9eba1 : + (48 <= j && j <= 63) ? 0x8f1bbcdc : + (64 <= j && j <= 79) ? 0xa953fd4e : + 'rmd160_K1: j out of range'; + } + + function rmd160_K2(j) { + return (0 <= j && j <= 15) ? 0x50a28be6 : + (16 <= j && j <= 31) ? 0x5c4dd124 : + (32 <= j && j <= 47) ? 0x6d703ef3 : + (48 <= j && j <= 63) ? 0x7a6d76e9 : + (64 <= j && j <= 79) ? 0x00000000 : + 'rmd160_K2: j out of range'; + } + } + }; + + // exposes Hashes + (function(window, undefined) { + var freeExports = false; + if (typeof exports === 'object') { + freeExports = exports; + if (exports && typeof global === 'object' && global && global === global.global) { + window = global; + } + } + + if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) { + // define as an anonymous module, so, through path mapping, it can be aliased + define(function() { + return Hashes; + }); + } else if (freeExports) { + // in Node.js or RingoJS v0.8.0+ + if (typeof module === 'object' && module && module.exports === freeExports) { + module.exports = Hashes; + } + // in Narwhal or RingoJS v0.7.0- + else { + freeExports.Hashes = Hashes; + } + } else { + // in a browser or Rhino + window.Hashes = Hashes; + } + }(this)); +}()); // IIFE diff --git a/js/vendor/jshashes/hashes.min.js b/js/vendor/jshashes/hashes.min.js new file mode 100644 index 00000000000..5118a877096 --- /dev/null +++ b/js/vendor/jshashes/hashes.min.js @@ -0,0 +1,2 @@ +/*! jshashes - New BSD License - https://github.com/h2non/jshashes */ +(function(){var n;function e(n){var e,t,r="",o=-1,f;if(n&&n.length){f=n.length;while((o+=1)>>6&31,128|e&63)}else if(e<=65535){r+=String.fromCharCode(224|e>>>12&15,128|e>>>6&63,128|e&63)}else if(e<=2097151){r+=String.fromCharCode(240|e>>>18&7,128|e>>>12&63,128|e>>>6&63,128|e&63)}}}return r}function t(n){var e,t,r,o,f,i=[],h;e=t=r=o=f=0;if(n&&n.length){h=n.length;n+="";while(e191&&r<224){o=n.charCodeAt(e+1);i[t]=String.fromCharCode((r&31)<<6|o&63);e+=2}else{o=n.charCodeAt(e+1);f=n.charCodeAt(e+2);i[t]=String.fromCharCode((r&15)<<12|(o&63)<<6|f&63);e+=3}}}return i.join("")}function r(n,e){var t=(n&65535)+(e&65535),r=(n>>16)+(e>>16)+(t>>16);return r<<16|t&65535}function o(n,e){return n<>>32-e}function f(n,e){var t=e?"0123456789ABCDEF":"0123456789abcdef",r="",o,f=0,i=n.length;for(;f>>4&15)+t.charAt(o&15)}return r}function i(n){var e,t=n.length,r="";for(e=0;e>>8&255)}return r}function h(n){var e,t=n.length,r="";for(e=0;e>>8&255,n.charCodeAt(e)&255)}return r}function u(n){var e,t=n.length*32,r="";for(e=0;e>5]>>>24-e%32&255)}return r}function a(n){var e,t=n.length*32,r="";for(e=0;e>5]>>>e%32&255)}return r}function c(n){var e,t=n.length*8,r=Array(n.length>>2),o=r.length;for(e=0;e>5]|=(n.charCodeAt(e/8)&255)<>2),o=r.length;for(e=0;e>5]|=(n.charCodeAt(e/8)&255)<<24-e%32}return r}function D(n,e){var t=e.length,r=Array(),o,f,i,h,u,a,c,l;a=Array(Math.ceil(n.length/2));h=a.length;for(o=0;o0){u=Array();i=0;for(o=0;o0||f>0){u[u.length]=f}}r[r.length]=i;a=u}c="";for(o=r.length-1;o>=0;o--){c+=e.charAt(r[o])}l=Math.ceil(n.length*8/(Math.log(e.length)/Math.log(2)));for(o=c.length;on.length*8){r+=e}else{r+=t.charAt(h>>>6*(3-i)&63)}}}return r}n={VERSION:"1.0.6",Base64:function(){var n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",r="=",o=false,f=true;this.encode=function(t){var o,i,h,u="",a=t.length;r=r||"=";t=f?e(t):t;for(o=0;oa*8){u+=r}else{u+=n.charAt(h>>>6*(3-i)&63)}}}return u};this.decode=function(e){var o,i,h,u,a,c,l,D,B,C,A="",s=[];if(!e){return e}o=C=0;e=e.replace(new RegExp("\\"+r,"gi"),"");do{a=n.indexOf(e.charAt(o+=1));c=n.indexOf(e.charAt(o+=1));l=n.indexOf(e.charAt(o+=1));D=n.indexOf(e.charAt(o+=1));B=a<<18|c<<12|l<<6|D;i=B>>16&255;h=B>>8&255;u=B&255;C+=1;if(l===64){s[C]=String.fromCharCode(i)}else if(D===64){s[C]=String.fromCharCode(i,h)}else{s[C]=String.fromCharCode(i,h,u)}}while(o>>8^r}return(t^-1)>>>0},MD5:function(n){var t=n&&typeof n.uppercase==="boolean"?n.uppercase:false,i=n&&typeof n.pad==="string"?n.pad:"=",h=n&&typeof n.utf8==="boolean"?n.utf8:true;this.hex=function(n){return f(u(n,h),t)};this.b64=function(n){return B(u(n),i)};this.any=function(n,e){return D(u(n,h),e)};this.raw=function(n){return u(n,h)};this.hex_hmac=function(n,e){return f(l(n,e),t)};this.b64_hmac=function(n,e){return B(l(n,e),i)};this.any_hmac=function(n,e,t){return D(l(n,e),t)};this.vm_test=function(){return hex("abc").toLowerCase()==="900150983cd24fb0d6963f7d28e17f72"};this.setUpperCase=function(n){if(typeof n==="boolean"){t=n}return this};this.setPad=function(n){i=n||i;return this};this.setUTF8=function(n){if(typeof n==="boolean"){h=n}return this};function u(n){n=h?e(n):n;return a(C(c(n),n.length*8))}function l(n,t){var r,o,f,i,u;n=h?e(n):n;t=h?e(t):t;r=c(n);if(r.length>16){r=C(r,n.length*8)}o=Array(16),f=Array(16);for(u=0;u<16;u+=1){o[u]=r[u]^909522486;f[u]=r[u]^1549556828}i=C(o.concat(c(t)),512+t.length*8);return a(C(f.concat(i),512+128))}function C(n,e){var t,o,f,i,h,u=1732584193,a=-271733879,c=-1732584194,l=271733878;n[e>>5]|=128<>>9<<4)+14]=e;for(t=0;t16){r=C(r,n.length*8)}o=Array(16),f=Array(16);for(i=0;i<16;i+=1){o[i]=r[i]^909522486;f[i]=r[i]^1549556828}a=C(o.concat(l(t)),512+t.length*8);return u(C(f.concat(a),512+160))}function C(n,e){var t,f,i,h,u,a,c,l,D=Array(80),B=1732584193,C=-271733879,w=-1732584194,F=271733878,E=-1009589776;n[e>>5]|=128<<24-e%32;n[(e+64>>9<<4)+15]=e;for(t=0;t16){f=m(f,n.length*8)}for(;o<16;o+=1){h[o]=f[o]^909522486;a[o]=f[o]^1549556828}r=m(h.concat(l(t)),512+t.length*8);return u(m(a.concat(r),512+256))}function C(n,e){return n>>>e|n<<32-e}function A(n,e){return n>>>e}function s(n,e,t){return n&e^~n&t}function w(n,e,t){return n&e^n&t^e&t}function F(n){return C(n,2)^C(n,13)^C(n,22)}function E(n){return C(n,6)^C(n,11)^C(n,25)}function d(n){return C(n,7)^C(n,18)^A(n,3)}function g(n){return C(n,17)^C(n,19)^A(n,10)}function p(n){return C(n,28)^C(n,34)^C(n,39)}function y(n){return C(n,14)^C(n,18)^C(n,41)}function b(n){return C(n,1)^C(n,8)^A(n,7)}function v(n){return C(n,19)^C(n,61)^A(n,6)}h=[1116352408,1899447441,-1245643825,-373957723,961987163,1508970993,-1841331548,-1424204075,-670586216,310598401,607225278,1426881987,1925078388,-2132889090,-1680079193,-1046744716,-459576895,-272742522,264347078,604807628,770255983,1249150122,1555081692,1996064986,-1740746414,-1473132947,-1341970488,-1084653625,-958395405,-710438585,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,-2117940946,-1838011259,-1564481375,-1474664885,-1035236496,-949202525,-778901479,-694614492,-200395387,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,-2067236844,-1933114872,-1866530822,-1538233109,-1090935817,-965641998];function m(n,e){var t=[1779033703,-1150833019,1013904242,-1521486534,1359893119,-1694144372,528734635,1541459225];var o=new Array(64);var f,i,u,a,c,l,D,B;var C,A,p,y;n[e>>5]|=128<<24-e%32;n[(e+64>>9<<4)+15]=e;for(C=0;C32){i=c(i,n.length*8)}for(;f<32;f+=1){h[f]=i[f]^909522486;a[f]=i[f]^1549556828}r=c(h.concat(l(t)),1024+t.length*8);return u(c(a.concat(r),1024+512))}function c(n,e){var t,r,o,f=new Array(80),h=new Array(16),u=[new C(1779033703,-205731576),new C(-1150833019,-2067093701),new C(1013904242,-23791573),new C(-1521486534,1595750129),new C(1359893119,-1377402159),new C(-1694144372,725511199),new C(528734635,-79577749),new C(1541459225,327033209)],a=new C(0,0),c=new C(0,0),l=new C(0,0),D=new C(0,0),B=new C(0,0),p=new C(0,0),y=new C(0,0),b=new C(0,0),v=new C(0,0),m=new C(0,0),x=new C(0,0),_=new C(0,0),S=new C(0,0),U=new C(0,0),j=new C(0,0),M=new C(0,0),T=new C(0,0);if(i===undefined){i=[new C(1116352408,-685199838),new C(1899447441,602891725),new C(-1245643825,-330482897),new C(-373957723,-2121671748),new C(961987163,-213338824),new C(1508970993,-1241133031),new C(-1841331548,-1357295717),new C(-1424204075,-630357736),new C(-670586216,-1560083902),new C(310598401,1164996542),new C(607225278,1323610764),new C(1426881987,-704662302),new C(1925078388,-226784913),new C(-2132889090,991336113),new C(-1680079193,633803317),new C(-1046744716,-815192428),new C(-459576895,-1628353838),new C(-272742522,944711139),new C(264347078,-1953704523),new C(604807628,2007800933),new C(770255983,1495990901),new C(1249150122,1856431235),new C(1555081692,-1119749164),new C(1996064986,-2096016459),new C(-1740746414,-295247957),new C(-1473132947,766784016),new C(-1341970488,-1728372417),new C(-1084653625,-1091629340),new C(-958395405,1034457026),new C(-710438585,-1828018395),new C(113926993,-536640913),new C(338241895,168717936),new C(666307205,1188179964),new C(773529912,1546045734),new C(1294757372,1522805485),new C(1396182291,-1651133473),new C(1695183700,-1951439906),new C(1986661051,1014477480),new C(-2117940946,1206759142),new C(-1838011259,344077627),new C(-1564481375,1290863460),new C(-1474664885,-1136513023),new C(-1035236496,-789014639),new C(-949202525,106217008),new C(-778901479,-688958952),new C(-694614492,1432725776),new C(-200395387,1467031594),new C(275423344,851169720),new C(430227734,-1194143544),new C(506948616,1363258195),new C(659060556,-544281703),new C(883997877,-509917016),new C(958139571,-976659869),new C(1322822218,-482243893),new C(1537002063,2003034995),new C(1747873779,-692930397),new C(1955562222,1575990012),new C(2024104815,1125592928),new C(-2067236844,-1578062990),new C(-1933114872,442776044),new C(-1866530822,593698344),new C(-1538233109,-561857047),new C(-1090935817,-1295615723),new C(-965641998,-479046869),new C(-903397682,-366583396),new C(-779700025,566280711),new C(-354779690,-840897762),new C(-176337025,-294727304),new C(116418474,1914138554),new C(174292421,-1563912026),new C(289380356,-1090974290),new C(460393269,320620315),new C(685471733,587496836),new C(852142971,1086792851),new C(1017036298,365543100),new C(1126000580,-1676669620),new C(1288033470,-885112138),new C(1501505948,-60457430),new C(1607167915,987167468),new C(1816402316,1246189591)]}for(r=0;r<80;r+=1){f[r]=new C(0,0)}n[e>>5]|=128<<24-(e&31);n[(e+128>>10<<5)+31]=e;o=n.length;for(r=0;r>>t|e.h<<32-t;n.h=e.h>>>t|e.l<<32-t}function w(n,e,t){n.l=e.h>>>t|e.l<<32-t;n.h=e.l>>>t|e.h<<32-t}function F(n,e,t){n.l=e.l>>>t|e.h<<32-t;n.h=e.h>>>t}function E(n,e,t){var r=(e.l&65535)+(t.l&65535);var o=(e.l>>>16)+(t.l>>>16)+(r>>>16);var f=(e.h&65535)+(t.h&65535)+(o>>>16);var i=(e.h>>>16)+(t.h>>>16)+(f>>>16);n.l=r&65535|o<<16;n.h=f&65535|i<<16}function d(n,e,t,r,o){var f=(e.l&65535)+(t.l&65535)+(r.l&65535)+(o.l&65535);var i=(e.l>>>16)+(t.l>>>16)+(r.l>>>16)+(o.l>>>16)+(f>>>16);var h=(e.h&65535)+(t.h&65535)+(r.h&65535)+(o.h&65535)+(i>>>16);var u=(e.h>>>16)+(t.h>>>16)+(r.h>>>16)+(o.h>>>16)+(h>>>16);n.l=f&65535|i<<16;n.h=h&65535|u<<16}function g(n,e,t,r,o,f){var i=(e.l&65535)+(t.l&65535)+(r.l&65535)+(o.l&65535)+(f.l&65535),h=(e.l>>>16)+(t.l>>>16)+(r.l>>>16)+(o.l>>>16)+(f.l>>>16)+(i>>>16),u=(e.h&65535)+(t.h&65535)+(r.h&65535)+(o.h&65535)+(f.h&65535)+(h>>>16),a=(e.h>>>16)+(t.h>>>16)+(r.h>>>16)+(o.h>>>16)+(f.h>>>16)+(u>>>16);n.l=i&65535|h<<16;n.h=u&65535|a<<16}},RMD160:function(n){var t=n&&typeof n.uppercase==="boolean"?n.uppercase:false,i=n&&typeof n.pad==="string"?n.pa:"=",h=n&&typeof n.utf8==="boolean"?n.utf8:true,u=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8,3,10,14,4,9,15,8,1,2,7,0,6,13,11,5,12,1,9,11,10,0,8,12,4,13,3,7,15,14,5,6,2,4,0,5,9,7,12,2,10,14,1,3,8,11,6,15,13],a=[5,14,7,0,9,2,11,4,13,6,15,8,1,10,3,12,6,11,3,7,0,13,5,10,14,15,8,12,4,9,1,2,15,5,1,3,7,14,6,9,11,8,12,2,10,0,4,13,8,6,4,1,3,11,15,0,5,12,2,13,9,7,10,14,12,15,10,4,1,5,8,7,6,2,13,14,0,3,9,11],l=[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8,7,6,8,13,11,9,7,15,7,12,15,9,11,7,13,12,11,13,6,7,14,9,13,15,14,8,13,6,5,12,7,5,11,12,14,15,14,15,9,8,9,14,5,6,8,6,5,12,9,15,5,11,6,8,13,12,5,12,13,14,11,8,5,6],C=[8,9,9,11,13,15,15,5,7,7,8,11,14,14,12,6,9,13,15,7,12,8,9,11,7,7,12,7,6,15,13,11,9,7,15,11,8,6,6,14,12,13,5,14,13,13,7,5,15,5,8,11,14,14,6,14,6,9,12,9,12,5,15,8,8,5,12,9,12,5,14,6,8,13,6,5,15,13,11,11];this.hex=function(n){return f(A(n,h))};this.b64=function(n){return B(A(n,h),i)};this.any=function(n,e){return D(A(n,h),e)};this.raw=function(n){return A(n,h)};this.hex_hmac=function(n,e){return f(s(n,e))};this.b64_hmac=function(n,e){return B(s(n,e),i)};this.any_hmac=function(n,e,t){return D(s(n,e),t)};this.vm_test=function(){return hex("abc").toLowerCase()==="900150983cd24fb0d6963f7d28e17f72"};this.setUpperCase=function(n){if(typeof n==="boolean"){t=n}return this};this.setPad=function(n){if(typeof n!=="undefined"){i=n}return this};this.setUTF8=function(n){if(typeof n==="boolean"){h=n}return this};function A(n){n=h?e(n):n;return w(F(c(n),n.length*8))}function s(n,t){n=h?e(n):n;t=h?e(t):t;var r,o,f=c(n),i=Array(16),u=Array(16);if(f.length>16){f=F(f,n.length*8)}for(r=0;r<16;r+=1){i[r]=f[r]^909522486;u[r]=f[r]^1549556828}o=F(i.concat(c(t)),512+t.length*8);return w(F(u.concat(o),512+160))}function w(n){var e,t="",r=n.length*32;for(e=0;e>5]>>>e%32&255)}return t}function F(n,e){var t,f,i,h,c=1732584193,D=4023233417,B=2562383102,A=271733878,s=3285377520,w,F,p,y,b,v,m,x,_,S;n[e>>5]|=128<>>9<<4)+14]=e;h=n.length;for(i=0;i' + - '
    ' + + '
    ' + '
    {{actorDisplayName}}
    ' + '
    {{date}}
    ' + '
    ' + diff --git a/js/webrtc.js b/js/webrtc.js index a4a77cbab84..1de3cc0807e 100644 --- a/js/webrtc.js +++ b/js/webrtc.js @@ -714,6 +714,7 @@ var spreedPeerConnectionTable = []; OCA.SpreedMe.webrtc.emit('mute', {id: peer.id, name:'video'}); } else if (data.type === 'nickChanged') { OCA.SpreedMe.webrtc.emit('nick', {id: peer.id, name:data.payload}); + app._messageCollection.updateGuestName(new Hashes.SHA1().hex(peer.id), data.payload); } } else if (label === 'hark') { // Ignore messages from hark datachannel diff --git a/templates/index-public.php b/templates/index-public.php index 780a7b5e8f8..631cd8e6169 100644 --- a/templates/index-public.php +++ b/templates/index-public.php @@ -13,6 +13,7 @@ 'vendor/backbone/backbone-min', 'vendor/backbone.radio/build/backbone.radio.min', 'vendor/backbone.marionette/lib/backbone.marionette.min', + 'vendor/jshashes/hashes.min', 'models/chatmessage', 'models/chatmessagecollection', 'models/localstoragemodel', diff --git a/templates/index.php b/templates/index.php index c3e81edf6b2..3d8dd5f2f71 100644 --- a/templates/index.php +++ b/templates/index.php @@ -13,6 +13,7 @@ 'vendor/backbone/backbone-min', 'vendor/backbone.radio/build/backbone.radio.min', 'vendor/backbone.marionette/lib/backbone.marionette.min', + 'vendor/jshashes/hashes.min', 'models/chatmessage', 'models/chatmessagecollection', 'models/room', From 8db6adfea87c37ceac1f57e08db2fab6354d78ec Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 20 Mar 2018 10:30:22 +0100 Subject: [PATCH 06/16] Send a request to the server when a guest renames Signed-off-by: Joas Schilling --- appinfo/routes.php | 8 +++ docs/api-v1.md | 19 ++++++ js/app.js | 41 ++++++++----- lib/Controller/CallController.php | 1 - lib/Controller/GuestController.php | 92 ++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 14 deletions(-) create mode 100644 lib/Controller/GuestController.php diff --git a/appinfo/routes.php b/appinfo/routes.php index 36fb9190668..f7b0aacd77f 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -265,6 +265,14 @@ 'token' => '^[a-z0-9]{4,30}$', ], ], + [ + 'name' => 'Guest#setDisplayName', + 'url' => '/api/{apiVersion}/guest/name', + 'verb' => 'POST', + 'requirements' => [ + 'apiVersion' => 'v1', + ], + ], ], ]; diff --git a/docs/api-v1.md b/docs/api-v1.md index 36cfa3d47f0..3d43da9c79e 100644 --- a/docs/api-v1.md +++ b/docs/api-v1.md @@ -459,7 +459,26 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1` - Header: + `201 Created` + `404 Not Found` When the room could not be found for the participant + +## Guests +### Set display name + +* Method: `POST` +* Endpoint: `/geust/name` +* Data: + + field | type | Description + ------|------|------------ + `displayName` | string | The new display name + +* Response: + - Header: + + `200 OK` + + `404 Not Found` When the guest is not found or has no session + + `403 Forbidden` When the user is logged in + + ## Signaling See the [Draft](https://github.com/nextcloud/spreed/wiki/Signaling-API) in the wiki… diff --git a/js/app.js b/js/app.js index da68f1957ba..e104ff46e77 100644 --- a/js/app.js +++ b/js/app.js @@ -677,23 +677,38 @@ }, initGuestName: function() { this._localStorageModel = new OCA.SpreedMe.Models.LocalStorageModel({ nick: '' }); - this._localStorageModel.on('change:nick', function(model, value) { - var avatar = $('#localVideoContainer').find('.avatar'); - - if (value) { - avatar.imageplaceholder(value, undefined, 128); - } else { - avatar.imageplaceholder('?', undefined, 128); - avatar.css('background-color', '#b9b9b9'); - } - - if (OCA.SpreedMe.webrtc) { - OCA.SpreedMe.webrtc.sendDirectlyToAll('status', 'nickChanged', value); - } + this._localStorageModel.on('change:nick', function(model, newDisplayName) { + $.ajax({ + url: OC.linkToOCS('apps/spreed/api/v1/guest', 2) + 'name', + type: 'POST', + data: { + displayName: newDisplayName + }, + beforeSend: function (request) { + request.setRequestHeader('Accept', 'application/json'); + }, + success: function() { + this._onChangeGuestName(newDisplayName); + }.bind(this) + }); }); this._localStorageModel.fetch(); }, + _onChangeGuestName: function(newDisplayName) { + var avatar = $('#localVideoContainer').find('.avatar'); + + if (newDisplayName) { + avatar.imageplaceholder(value, undefined, 128); + } else { + avatar.imageplaceholder('?', undefined, 128); + avatar.css('background-color', '#b9b9b9'); + } + + if (this.webrtc) { + this.webrtc.sendDirectlyToAll('status', 'nickChanged', newDisplayName); + } + }, initShareRoomClipboard: function () { $('body').find('.shareRoomClipboard').tooltip({ placement: 'bottom', diff --git a/lib/Controller/CallController.php b/lib/Controller/CallController.php index 0a0ba3f68b2..0b810838a28 100644 --- a/lib/Controller/CallController.php +++ b/lib/Controller/CallController.php @@ -25,7 +25,6 @@ namespace OCA\Spreed\Controller; -use OCA\Spreed\Exceptions\InvalidPasswordException; use OCA\Spreed\Exceptions\ParticipantNotFoundException; use OCA\Spreed\Exceptions\RoomNotFoundException; use OCA\Spreed\Manager; diff --git a/lib/Controller/GuestController.php b/lib/Controller/GuestController.php new file mode 100644 index 00000000000..981856ec51a --- /dev/null +++ b/lib/Controller/GuestController.php @@ -0,0 +1,92 @@ +. + * + */ + +namespace OCA\Spreed\Controller; + +use Doctrine\DBAL\DBALException; +use OCA\Spreed\GuestManager; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCSController; +use OCP\IRequest; +use OCP\ISession; + +class GuestController extends OCSController { + + /** @var string */ + private $userId; + + /** @var ISession */ + private $session; + + /** @var GuestManager */ + private $guestManager; + + /** + * @param string $appName + * @param string $UserId + * @param IRequest $request + * @param ISession $session + * @param GuestManager $guestManager + */ + public function __construct($appName, + $UserId, + IRequest $request, + ISession $session, + GuestManager $guestManager) { + parent::__construct($appName, $request); + + $this->userId = $UserId; + $this->session = $session; + $this->guestManager = $guestManager; + } + + /** + * @PublicPage + * + * + * @param string $displayName + * @return DataResponse + */ + public function setDisplayName($displayName) { + if ($this->userId) { + return new DataResponse([], Http::STATUS_FORBIDDEN); + } + + $sessionId = $this->session->get('spreed-session'); + $sessionId = $sessionId ? sha1($sessionId) : $sessionId; + + if (!$sessionId) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + + try { + $this->guestManager->updateName($sessionId, $displayName); + } catch (DBALException $e) { + return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + } + + return new DataResponse(); + } + +} From a491212d8ace00bbb08facdc271d61e3fe294d28 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 20 Mar 2018 10:33:09 +0100 Subject: [PATCH 07/16] Update docs for earlier changes Signed-off-by: Joas Schilling --- docs/api-v1.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/api-v1.md b/docs/api-v1.md index 3d43da9c79e..c11a5aa9325 100644 --- a/docs/api-v1.md +++ b/docs/api-v1.md @@ -454,6 +454,7 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1` field | type | Description ------|------|------------ `message` | string | The message the user wants to say + `actorDisplayName` | string | Guest display name (ignored for logged in users) * Response: - Header: From 7434a57e5676ddf572fa09d54c0c20f2d9b040bb Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 12 Mar 2018 15:24:23 +0100 Subject: [PATCH 08/16] Make sure users with numerical user ids/display names still show as users Signed-off-by: Joas Schilling --- js/views/participantlistview.js | 6 ++++-- lib/Controller/CallController.php | 2 +- lib/Controller/RoomController.php | 8 ++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/js/views/participantlistview.js b/js/views/participantlistview.js index 66543aa2efc..8ceb395a534 100644 --- a/js/views/participantlistview.js +++ b/js/views/participantlistview.js @@ -131,8 +131,10 @@ }, onRender: function() { this.$el.find('.avatar').each(function() { - var element = $(this); - if (element.data('displayname').length) { + var element = $(this), + userId = '' + element.data('user-id'); // Make sure it's a string + + if (userId.length) { element.avatar(element.data('user-id'), 32, undefined, false, undefined, element.data('displayname')); } else { element.imageplaceholder('?', undefined, 32); diff --git a/lib/Controller/CallController.php b/lib/Controller/CallController.php index 0b810838a28..531b51bf496 100644 --- a/lib/Controller/CallController.php +++ b/lib/Controller/CallController.php @@ -111,7 +111,7 @@ public function getPeersForCall($token) { } $result[] = [ - 'userId' => $participant, + 'userId' => (string) $participant, 'token' => $token, 'lastPing' => $data['lastPing'], 'sessionId' => $data['sessionId'], diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php index 21a97bea975..09934fdd43f 100644 --- a/lib/Controller/RoomController.php +++ b/lib/Controller/RoomController.php @@ -199,7 +199,7 @@ protected function formatRoom(Room $room, Participant $participant = null) { foreach ($participants['users'] as $userId => $data) { $user = $this->userManager->get($userId); if ($user instanceof IUser) { - $participantList[$user->getUID()] = [ + $participantList[(string) $user->getUID()] = [ 'name' => $user->getDisplayName(), 'type' => $data['participantType'], 'call' => $data['inCall'], @@ -508,14 +508,14 @@ public function getParticipants($token) { $results = []; foreach ($participants['users'] as $userId => $participant) { - $user = $this->userManager->get($userId); + $user = $this->userManager->get((string) $userId); if (!$user instanceof IUser) { continue; } $results[] = array_merge($participant, [ - 'userId' => $userId, - 'displayName' => $user->getDisplayName(), + 'userId' => (string) $userId, + 'displayName' => (string) $user->getDisplayName(), ]); } From e23f35fc825868ad09f0c881f2ca9dc6c01bd1ba Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 20 Mar 2018 10:57:19 +0100 Subject: [PATCH 09/16] Show guest names in the participant list Signed-off-by: Joas Schilling --- js/views/participantlistview.js | 30 +++++++++++++++++++++--------- lib/Controller/RoomController.php | 15 ++++++++++++++- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/js/views/participantlistview.js b/js/views/participantlistview.js index 8ceb395a534..a58d5f55ec6 100644 --- a/js/views/participantlistview.js +++ b/js/views/participantlistview.js @@ -31,7 +31,7 @@ var ITEM_TEMPLATE = '' + '' + - '
    ' + + '
    ' + ' {{name}}' + '{{#if participantIsOwner}}(' + t('spreed', 'moderator') + '){{/if}}' + '{{#if participantIsModerator}}(' + t('spreed', 'moderator') + '){{/if}}' + @@ -119,26 +119,38 @@ var canModerate = this.model.get('participantType') !== OCA.SpreedMe.app.OWNER && // can not moderate owners this.model.get('userId') !== oc_current_user && // can not moderate yourself (OCA.SpreedMe.app.activeRoom.get('participantType') === OCA.SpreedMe.app.OWNER || // current user must be owner - OCA.SpreedMe.app.activeRoom.get('participantType') === OCA.SpreedMe.app.MODERATOR); // or moderator. + OCA.SpreedMe.app.activeRoom.get('participantType') === OCA.SpreedMe.app.MODERATOR), // or moderator. + name = ''; + + + if (this.model.get('userId').length || this.model.get('displayName').length) { + name = this.model.get('displayName'); + } else { + name = t('spreed', 'Guest'); + } return { canModerate: canModerate, - name: this.model.get('userId').length ? this.model.get('displayName') : t('spreed', 'Guest'), + name: name, participantIsUser: this.model.get('participantType') === OCA.SpreedMe.app.USER, participantIsModerator: this.model.get('participantType') === OCA.SpreedMe.app.MODERATOR, participantIsOwner: this.model.get('participantType') === OCA.SpreedMe.app.OWNER }; }, onRender: function() { + var model = this.model; this.$el.find('.avatar').each(function() { - var element = $(this), - userId = '' + element.data('user-id'); // Make sure it's a string + var $element = $(this); - if (userId.length) { - element.avatar(element.data('user-id'), 32, undefined, false, undefined, element.data('displayname')); + if (model.get('participantType') !== OCA.SpreedMe.app.GUEST) { + $element.avatar(model.get('userId'), 32, undefined, false, undefined, model.get('displayName')); } else { - element.imageplaceholder('?', undefined, 32); - element.css('background-color', '#b9b9b9'); + if (model.get('displayName')) { + $element.imageplaceholder(model.get('displayName')[0], undefined, 32); + } else { + $element.imageplaceholder('?', undefined, 32); + } + $element.css('background-color', '#b9b9b9'); } }); diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php index 09934fdd43f..0630338e890 100644 --- a/lib/Controller/RoomController.php +++ b/lib/Controller/RoomController.php @@ -28,6 +28,7 @@ use OCA\Spreed\Exceptions\InvalidPasswordException; use OCA\Spreed\Exceptions\ParticipantNotFoundException; use OCA\Spreed\Exceptions\RoomNotFoundException; +use OCA\Spreed\GuestManager; use OCA\Spreed\Manager; use OCA\Spreed\Participant; use OCA\Spreed\Room; @@ -59,6 +60,8 @@ class RoomController extends OCSController { private $manager; /** @var Messages */ private $messages; + /** @var GuestManager */ + private $guestManager; /** @var IL10N */ private $l10n; @@ -72,6 +75,7 @@ class RoomController extends OCSController { * @param ILogger $logger * @param Manager $manager * @param Messages $messages + * @param GuestManager $guestManager * @param IL10N $l10n */ public function __construct($appName, @@ -83,6 +87,7 @@ public function __construct($appName, ILogger $logger, Manager $manager, Messages $messages, + GuestManager $guestManager, IL10N $l10n) { parent::__construct($appName, $request); $this->session = $session; @@ -92,6 +97,7 @@ public function __construct($appName, $this->logger = $logger; $this->manager = $manager; $this->messages = $messages; + $this->guestManager = $guestManager; $this->l10n = $l10n; } @@ -519,11 +525,18 @@ public function getParticipants($token) { ]); } + $guestSessions = []; foreach ($participants['guests'] as $participant) { + $guestSessions[] = sha1($participant['sessionId']); + } + $guestNames = $this->guestManager->getNamesBySessionHashes($guestSessions); + + foreach ($participants['guests'] as $participant) { + $sessionHash = sha1($participant['sessionId']); $results[] = array_merge($participant, [ 'participantType' => Participant::GUEST, 'userId' => '', - 'displayName' => '', + 'displayName' => isset($guestNames[$sessionHash]) ? $guestNames[$sessionHash] : '', ]); } From a74866ad1a52e6cba5a66f8bf33eea1ab322c32d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 20 Mar 2018 10:57:32 +0100 Subject: [PATCH 10/16] Fix guest avatar above chat input Signed-off-by: Joas Schilling --- js/views/chatview.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/js/views/chatview.js b/js/views/chatview.js index a741e7326b1..dcd66da0dc0 100644 --- a/js/views/chatview.js +++ b/js/views/chatview.js @@ -141,7 +141,11 @@ if (OC.getCurrentUser().uid) { this.$el.find('.avatar').avatar(OC.getCurrentUser().uid, 32, undefined, false, undefined, OC.getCurrentUser().displayName); } else { - this.$el.find('.avatar').imageplaceholder('?', undefined, 32); + if (this.getOption('guestNameModel').get('nick')) { + this.$el.find('.avatar').imageplaceholder(this.getOption('guestNameModel').get('nick')[0], undefined, 32); + } else { + this.$el.find('.avatar').imageplaceholder('?', undefined, 32); + } this.$el.find('.avatar').css('background-color', '#b9b9b9'); this.showChildView('guestName', this._guestNameEditableTextLabel, { replaceElement: true, allowMissingEl: true } ); } From 1ff8ffad72a5bf7258e7c028e1dafe740bb9d6df Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 20 Mar 2018 11:06:43 +0100 Subject: [PATCH 11/16] Don't use deprecated oc_current_user Signed-off-by: Joas Schilling --- js/app.js | 12 ++++++------ js/signaling.js | 2 +- js/views/participantlistview.js | 2 +- js/views/roomlistview.js | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/js/app.js b/js/app.js index e104ff46e77..74fe3613446 100644 --- a/js/app.js +++ b/js/app.js @@ -86,7 +86,7 @@ var results = []; $.each(response.ocs.data.exact.users, function(id, user) { - if (oc_current_user === user.value.shareWith) { + if (OC.getCurrentUser().uid === user.value.shareWith) { return; } results.push({ id: user.value.shareWith, displayName: user.label, type: "user"}); @@ -95,7 +95,7 @@ results.push({ id: group.value.shareWith, displayName: group.label + ' ' + t('spreed', '(group)'), type: "group"}); }); $.each(response.ocs.data.users, function(id, user) { - if (oc_current_user === user.value.shareWith) { + if (OC.getCurrentUser().uid === user.value.shareWith) { return; } results.push({ id: user.value.shareWith, displayName: user.label, type: "user"}); @@ -346,7 +346,7 @@ * @param {string} token */ _setRoomActive: function(token) { - if (oc_current_user) { + if (OC.getCurrentUser().uid) { this._rooms.forEach(function(room) { room.set('active', room.get('token') === token); }); @@ -368,7 +368,7 @@ .then(function() { self.stopListening(self.activeRoom, 'change:participantInCall'); - if (oc_current_user) { + if (OC.getCurrentUser().uid) { roomChannel.trigger('active', token); self._rooms.forEach(function(room) { @@ -655,8 +655,8 @@ var avatar = avatarContainer.find('.avatar'); var guestName = localStorage.getItem("nick"); - if (oc_current_user) { - avatar.avatar(OC.currentUser, 128); + if (OC.getCurrentUser().uid) { + avatar.avatar(OC.getCurrentUser().uid, 128); } else if (guestName) { avatar.imageplaceholder(guestName, undefined, 128); } else if (this.displayedGuestNameHint === false) { diff --git a/js/signaling.js b/js/signaling.js index 39612e45f9c..cb71d1dc335 100644 --- a/js/signaling.js +++ b/js/signaling.js @@ -141,7 +141,7 @@ OCA.Talk.Signaling.Base.prototype.syncRooms = function() { var defer = $.Deferred(); - if (this.roomCollection && oc_current_user) { + if (this.roomCollection && OC.getCurrentUser().uid) { this.roomCollection.fetch({ success: function(data) { defer.resolve(data); diff --git a/js/views/participantlistview.js b/js/views/participantlistview.js index a58d5f55ec6..bcfecad8c4d 100644 --- a/js/views/participantlistview.js +++ b/js/views/participantlistview.js @@ -117,7 +117,7 @@ }, templateContext: function() { var canModerate = this.model.get('participantType') !== OCA.SpreedMe.app.OWNER && // can not moderate owners - this.model.get('userId') !== oc_current_user && // can not moderate yourself + this.model.get('userId') !== OC.getCurrentUser().uid && // can not moderate yourself (OCA.SpreedMe.app.activeRoom.get('participantType') === OCA.SpreedMe.app.OWNER || // current user must be owner OCA.SpreedMe.app.activeRoom.get('participantType') === OCA.SpreedMe.app.MODERATOR), // or moderator. name = ''; diff --git a/js/views/roomlistview.js b/js/views/roomlistview.js index 26c44995459..6bf71d21404 100644 --- a/js/views/roomlistview.js +++ b/js/views/roomlistview.js @@ -367,7 +367,7 @@ addTooltip: function () { var participants = []; $.each(this.model.get('participants'), function(participantId, data) { - if (participantId !== oc_current_user) { + if (participantId !== OC.getCurrentUser().uid) { participants.push(escapeHTML(data.name)); } }); From 160f431bc289f936b7b4cba960849c403c57ffd5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 20 Mar 2018 11:33:39 +0100 Subject: [PATCH 12/16] Simplify guest avatar handling Signed-off-by: Joas Schilling --- js/app.js | 33 +++++++++++++++++---------------- js/views/chatview.js | 12 ++---------- js/views/participantlistview.js | 6 +----- js/webrtc.js | 18 +++++------------- 4 files changed, 25 insertions(+), 44 deletions(-) diff --git a/js/app.js b/js/app.js index 74fe3613446..37eba3380dc 100644 --- a/js/app.js +++ b/js/app.js @@ -657,13 +657,13 @@ var guestName = localStorage.getItem("nick"); if (OC.getCurrentUser().uid) { avatar.avatar(OC.getCurrentUser().uid, 128); - } else if (guestName) { - avatar.imageplaceholder(guestName, undefined, 128); - } else if (this.displayedGuestNameHint === false) { - avatar.imageplaceholder('?', undefined, 128); + } else { + avatar.imageplaceholder('?', guestName, 128); avatar.css('background-color', '#b9b9b9'); - OC.Notification.showTemporary(t('spreed', 'You can set your name on the right sidebar so other participants can identify you better.')); - this.displayedGuestNameHint = true; + if (this.displayedGuestNameHint === false) { + OC.Notification.showTemporary(t('spreed', 'You can set your name on the right sidebar so other participants can identify you better.')); + this.displayedGuestNameHint = true; + } } avatarContainer.removeClass('hidden'); @@ -676,6 +676,7 @@ this.videoDisabled = true; }, initGuestName: function() { + var self = this; this._localStorageModel = new OCA.SpreedMe.Models.LocalStorageModel({ nick: '' }); this._localStorageModel.on('change:nick', function(model, newDisplayName) { $.ajax({ @@ -688,8 +689,8 @@ request.setRequestHeader('Accept', 'application/json'); }, success: function() { - this._onChangeGuestName(newDisplayName); - }.bind(this) + self._onChangeGuestName(newDisplayName); + } }); }); @@ -698,15 +699,15 @@ _onChangeGuestName: function(newDisplayName) { var avatar = $('#localVideoContainer').find('.avatar'); - if (newDisplayName) { - avatar.imageplaceholder(value, undefined, 128); - } else { - avatar.imageplaceholder('?', undefined, 128); - avatar.css('background-color', '#b9b9b9'); - } + avatar.imageplaceholder('?', newDisplayName, 128); + avatar.css('background-color', '#b9b9b9'); - if (this.webrtc) { - this.webrtc.sendDirectlyToAll('status', 'nickChanged', newDisplayName); + console.log('_onChangeGuestName'); + console.log(this); + console.log(this.webrtc); + if (OCA.SpreedMe.webrtc) { + console.log('_onChangeGuestName.webrtc'); + OCA.SpreedMe.webrtc.sendDirectlyToAll('status', 'nickChanged', newDisplayName); } }, initShareRoomClipboard: function () { diff --git a/js/views/chatview.js b/js/views/chatview.js index dcd66da0dc0..020198705f1 100644 --- a/js/views/chatview.js +++ b/js/views/chatview.js @@ -141,11 +141,7 @@ if (OC.getCurrentUser().uid) { this.$el.find('.avatar').avatar(OC.getCurrentUser().uid, 32, undefined, false, undefined, OC.getCurrentUser().displayName); } else { - if (this.getOption('guestNameModel').get('nick')) { - this.$el.find('.avatar').imageplaceholder(this.getOption('guestNameModel').get('nick')[0], undefined, 32); - } else { - this.$el.find('.avatar').imageplaceholder('?', undefined, 32); - } + this.$el.find('.avatar').imageplaceholder('?', this.getOption('guestNameModel').get('nick'), 128); this.$el.find('.avatar').css('background-color', '#b9b9b9'); this.showChildView('guestName', this._guestNameEditableTextLabel, { replaceElement: true, allowMissingEl: true } ); } @@ -343,11 +339,7 @@ if (model.get('actorType') === 'users') { $this.avatar($this.data('user-id'), 32, undefined, false, undefined, $this.data('displayname')); } else { - if (model.get('actorDisplayName')) { - $this.imageplaceholder(model.get('actorDisplayName')[0], undefined, 32); - } else { - $this.imageplaceholder('?', undefined, 32); - } + $this.imageplaceholder('?', model.get('actorDisplayName'), 32); $this.css('background-color', '#b9b9b9'); } }); diff --git a/js/views/participantlistview.js b/js/views/participantlistview.js index bcfecad8c4d..0843552577f 100644 --- a/js/views/participantlistview.js +++ b/js/views/participantlistview.js @@ -145,11 +145,7 @@ if (model.get('participantType') !== OCA.SpreedMe.app.GUEST) { $element.avatar(model.get('userId'), 32, undefined, false, undefined, model.get('displayName')); } else { - if (model.get('displayName')) { - $element.imageplaceholder(model.get('displayName')[0], undefined, 32); - } else { - $element.imageplaceholder('?', undefined, 32); - } + $element.imageplaceholder('?', model.get('displayName'), 32); $element.css('background-color', '#b9b9b9'); } }); diff --git a/js/webrtc.js b/js/webrtc.js index 1de3cc0807e..af591c46717 100644 --- a/js/webrtc.js +++ b/js/webrtc.js @@ -740,16 +740,10 @@ var spreedPeerConnectionTable = []; if (userId && userId.length) { avatar.avatar(userId, 128); nameIndicator.text(peer.nick); - } else if (peer.nick) { - avatar.imageplaceholder(peer.nick, undefined, 128); - nameIndicator.text(peer.nick); - } else if (guestName && guestName.length > 0) { - avatar.imageplaceholder(guestName, undefined, 128); - nameIndicator.text(guestName); } else { - avatar.imageplaceholder('?', undefined, 128); + avatar.imageplaceholder('?', peer.nick || guestName, 128); avatar.css('background-color', '#b9b9b9'); - nameIndicator.text(t('spreed', 'Guest')); + nameIndicator.text(peer.nick || guestName || t('spreed', 'Guest')); } $(videoContainer).prepend(video); @@ -914,16 +908,14 @@ var spreedPeerConnectionTable = []; var screenNameIndicator = $(screen).find('.nameIndicator'); if (!data.name) { - videoNameIndicator.text(t('spreed', 'Guest')); - videoAvatar.imageplaceholder('?', undefined, 128); - videoAvatar.css('background-color', '#b9b9b9'); screenNameIndicator.text(t('spreed', "Guest's screen")); } else { - videoNameIndicator.text(data.name); - videoAvatar.imageplaceholder(data.name, undefined, 128); screenNameIndicator.text(t('spreed', "{participantName}'s screen", {participantName: data.name})); guestNamesTable[data.id] = data.name; } + videoNameIndicator.text(data.name || t('spreed', 'Guest')); + videoAvatar.imageplaceholder('?', data.name, 128); + videoAvatar.css('background-color', '#b9b9b9'); if (latestSpeakerId === data.id) { OCA.SpreedMe.speakers.updateVideoContainerDummy(data.id); From 2c9422cfc92f096087bee212c4c88f48732cbe6b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 20 Mar 2018 11:50:49 +0100 Subject: [PATCH 13/16] Fix name positioning for guests Signed-off-by: Joas Schilling --- css/style.scss | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/css/style.scss b/css/style.scss index 5bc61a06a1e..7c0bc4ff4ae 100644 --- a/css/style.scss +++ b/css/style.scss @@ -973,6 +973,10 @@ video { width: 100%; } +.newCommentRow .editable-text-label .label { + margin-left: 5px; +} + .detailCallInfoContainer #link-checkbox+label { display: inline-block; padding: 12px 0; From c84f46a486230b860064404626e30d6df4b7463d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 20 Mar 2018 13:52:20 +0100 Subject: [PATCH 14/16] Fix eslint Signed-off-by: Joas Schilling --- .eslintrc.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index f29f3378c19..ca36a414f9f 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -15,9 +15,9 @@ globals: t: false Backbone: false _: false - oc_current_user: false Clipboard: false oc_defaults: false + Hashes: false rules: curly: error From 1b1436b8ac0d6049cf1e06431b6ccb7fc104ab71 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 20 Mar 2018 13:55:14 +0100 Subject: [PATCH 15/16] Fix unit tests Signed-off-by: Joas Schilling --- lib/Controller/ChatController.php | 2 +- tests/php/Controller/ChatControllerTest.php | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 809ca45b958..0089b7b31ab 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -133,7 +133,7 @@ private function getRoom($token) { * "404 Not found" if the room or session for a guest user was not * found". */ - public function sendMessage($token, $message, $actorDisplayName) { + public function sendMessage($token, $message, $actorDisplayName = '') { $room = $this->getRoom($token); if ($room === null) { return new DataResponse([], Http::STATUS_NOT_FOUND); diff --git a/tests/php/Controller/ChatControllerTest.php b/tests/php/Controller/ChatControllerTest.php index 827f4ac2c8b..9f3d5040600 100644 --- a/tests/php/Controller/ChatControllerTest.php +++ b/tests/php/Controller/ChatControllerTest.php @@ -27,6 +27,7 @@ use OCA\Spreed\Controller\ChatController; use OCA\Spreed\Exceptions\ParticipantNotFoundException; use OCA\Spreed\Exceptions\RoomNotFoundException; +use OCA\Spreed\GuestManager; use OCA\Spreed\Manager; use OCA\Spreed\Room; use OCP\AppFramework\Http; @@ -41,19 +42,22 @@ class ChatControllerTest extends \Test\TestCase { /** @var string */ private $userId; - /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ protected $userManager; - /** @var \OCP\ISession|\PHPUnit_Framework_MockObject_MockObject */ + /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */ private $session; - /** @var \OCA\Spreed\Manager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */ protected $manager; - /** @var \OCA\Spreed\Chat\ChatManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var ChatManager|\PHPUnit_Framework_MockObject_MockObject */ protected $chatManager; - /** @var \OCA\Spreed\Room|\PHPUnit_Framework_MockObject_MockObject */ + /** @var GuestManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $guestManager; + + /** @var Room|\PHPUnit_Framework_MockObject_MockObject */ protected $room; /** @var \OCA\Spreed\Controller\ChatController */ @@ -70,6 +74,7 @@ public function setUp() { $this->session = $this->createMock(ISession::class); $this->manager = $this->createMock(Manager::class); $this->chatManager = $this->createMock(ChatManager::class); + $this->guestManager = $this->createMock(GuestManager::class); $this->room = $this->createMock(Room::class); @@ -91,7 +96,8 @@ private function recreateChatController() { $this->userManager, $this->session, $this->manager, - $this->chatManager + $this->chatManager, + $this->guestManager ); } From e1923b03f6383563878e4dce0befd7f7484135d7 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 21 Mar 2018 14:20:04 +0100 Subject: [PATCH 16/16] Update participant list also when not in call Signed-off-by: Joas Schilling --- appinfo/routes.php | 3 ++- docs/api-v1.md | 4 ++-- js/app.js | 12 +++++----- lib/AppInfo/Application.php | 2 ++ lib/Controller/ChatController.php | 6 ++--- lib/Controller/GuestController.php | 18 +++++++++++---- lib/Controller/SignalingController.php | 2 ++ lib/GuestManager.php | 31 +++++++++++++++++--------- 8 files changed, 53 insertions(+), 25 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index f7b0aacd77f..5a19618a059 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -267,10 +267,11 @@ ], [ 'name' => 'Guest#setDisplayName', - 'url' => '/api/{apiVersion}/guest/name', + 'url' => '/api/{apiVersion}/guest/{token}/name', 'verb' => 'POST', 'requirements' => [ 'apiVersion' => 'v1', + 'token' => '^[a-z0-9]{4,30}$', ], ], ], diff --git a/docs/api-v1.md b/docs/api-v1.md index c11a5aa9325..e00074dc825 100644 --- a/docs/api-v1.md +++ b/docs/api-v1.md @@ -466,7 +466,7 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1` ### Set display name * Method: `POST` -* Endpoint: `/geust/name` +* Endpoint: `/guest/{token}/name` * Data: field | type | Description @@ -476,7 +476,7 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1` * Response: - Header: + `200 OK` - + `404 Not Found` When the guest is not found or has no session + + `404 Not Found` When the room is not found or the session does not exist in the room + `403 Forbidden` When the user is logged in diff --git a/js/app.js b/js/app.js index 37eba3380dc..abaac0f9fb6 100644 --- a/js/app.js +++ b/js/app.js @@ -334,6 +334,11 @@ id: 'participantsTabView' }); + this.signaling.on('usersInRoom', function() { + // Also refresh the participant list when the users change + this._participants.fetch(); + }.bind(this)); + this._participantsView.listenTo(this._rooms, 'change:active', function(model, active) { if (active) { this.setRoom(model); @@ -680,7 +685,7 @@ this._localStorageModel = new OCA.SpreedMe.Models.LocalStorageModel({ nick: '' }); this._localStorageModel.on('change:nick', function(model, newDisplayName) { $.ajax({ - url: OC.linkToOCS('apps/spreed/api/v1/guest', 2) + 'name', + url: OC.linkToOCS('apps/spreed/api/v1/guest', 2) + this.token + '/name', type: 'POST', data: { displayName: newDisplayName @@ -692,7 +697,7 @@ self._onChangeGuestName(newDisplayName); } }); - }); + }.bind(this)); this._localStorageModel.fetch(); }, @@ -702,9 +707,6 @@ avatar.imageplaceholder('?', newDisplayName, 128); avatar.css('background-color', '#b9b9b9'); - console.log('_onChangeGuestName'); - console.log(this); - console.log(this.webrtc); if (OCA.SpreedMe.webrtc) { console.log('_onChangeGuestName.webrtc'); OCA.SpreedMe.webrtc.sendDirectlyToAll('status', 'nickChanged', newDisplayName); diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 731626330a8..980abc915cc 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -24,6 +24,7 @@ use OCA\Spreed\Activity\Hooks; use OCA\Spreed\Capabilities; use OCA\Spreed\Chat\ChatManager; +use OCA\Spreed\GuestManager; use OCA\Spreed\HookListener; use OCA\Spreed\Notification\Notifier; use OCA\Spreed\Room; @@ -92,6 +93,7 @@ protected function registerInternalSignalingHooks(EventDispatcherInterface $disp $dispatcher->addListener(Room::class . '::postUserDisconnectRoom', $listener); $dispatcher->addListener(Room::class . '::postSessionJoinCall', $listener); $dispatcher->addListener(Room::class . '::postSessionLeaveCall', $listener); + $dispatcher->addListener(GuestManager::class . '::updateName', $listener); } protected function getBackendNotifier() { diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 0089b7b31ab..e40143e9043 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -141,16 +141,16 @@ public function sendMessage($token, $message, $actorDisplayName = '') { if ($this->userId === null) { $actorType = 'guests'; - $actorId = $this->session->get('spreed-session'); + $sessionId = $this->session->get('spreed-session'); // The character limit for actorId is 64, but the spreed-session is // 256 characters long, so it has to be hashed to get an ID that // fits (except if there is no session, as the actorId should be // empty in that case but sha1('') would generate a hash too // instead of returning an empty string). - $actorId = $actorId ? sha1($actorId) : $actorId; + $actorId = $sessionId ? sha1($sessionId) : ''; if ($actorId && $actorDisplayName) { - $this->guestManager->updateName($actorId, $actorDisplayName); + $this->guestManager->updateName($room, $sessionId, $actorDisplayName); } } else { $actorType = 'users'; diff --git a/lib/Controller/GuestController.php b/lib/Controller/GuestController.php index 981856ec51a..67bfd457aad 100644 --- a/lib/Controller/GuestController.php +++ b/lib/Controller/GuestController.php @@ -24,7 +24,9 @@ namespace OCA\Spreed\Controller; use Doctrine\DBAL\DBALException; +use OCA\Spreed\Exceptions\RoomNotFoundException; use OCA\Spreed\GuestManager; +use OCA\Spreed\Manager; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; @@ -39,6 +41,9 @@ class GuestController extends OCSController { /** @var ISession */ private $session; + /** @var Manager */ + private $roomManager; + /** @var GuestManager */ private $guestManager; @@ -47,17 +52,20 @@ class GuestController extends OCSController { * @param string $UserId * @param IRequest $request * @param ISession $session + * @param Manager $roomManager * @param GuestManager $guestManager */ public function __construct($appName, $UserId, IRequest $request, ISession $session, + Manager $roomManager, GuestManager $guestManager) { parent::__construct($appName, $request); $this->userId = $UserId; $this->session = $session; + $this->roomManager = $roomManager; $this->guestManager = $guestManager; } @@ -65,23 +73,25 @@ public function __construct($appName, * @PublicPage * * + * @param string $token * @param string $displayName * @return DataResponse */ - public function setDisplayName($displayName) { + public function setDisplayName($token, $displayName) { if ($this->userId) { return new DataResponse([], Http::STATUS_FORBIDDEN); } $sessionId = $this->session->get('spreed-session'); - $sessionId = $sessionId ? sha1($sessionId) : $sessionId; - if (!$sessionId) { + try { + $room = $this->roomManager->getRoomForSession($this->userId, $this->session->get('spreed-session')); + } catch (RoomNotFoundException $exception) { return new DataResponse([], Http::STATUS_NOT_FOUND); } try { - $this->guestManager->updateName($sessionId, $displayName); + $this->guestManager->updateName($room, $sessionId, $displayName); } catch (DBALException $e) { return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } diff --git a/lib/Controller/SignalingController.php b/lib/Controller/SignalingController.php index c0dc40974a4..7b915912dab 100644 --- a/lib/Controller/SignalingController.php +++ b/lib/Controller/SignalingController.php @@ -26,6 +26,7 @@ use OCA\Spreed\Config; use OCA\Spreed\Exceptions\RoomNotFoundException; use OCA\Spreed\Exceptions\ParticipantNotFoundException; +use OCA\Spreed\GuestManager; use OCA\Spreed\Manager; use OCA\Spreed\Participant; use OCA\Spreed\Room; @@ -63,6 +64,7 @@ class SignalingController extends OCSController { * @param Manager $manager * @param IDBConnection $connection * @param Messages $messages + * @param IUserManager $userManager * @param string $UserId */ public function __construct($appName, diff --git a/lib/GuestManager.php b/lib/GuestManager.php index 7eb4afa259f..70c769c1126 100644 --- a/lib/GuestManager.php +++ b/lib/GuestManager.php @@ -24,36 +24,47 @@ use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\GenericEvent; class GuestManager { /** @var IDBConnection */ protected $connection; - public function __construct(IDBConnection $connection) { + /** @var EventDispatcherInterface */ + protected $dispatcher; + + public function __construct(IDBConnection $connection, EventDispatcherInterface $dispatcher) { $this->connection = $connection; + $this->dispatcher = $dispatcher; } /** - * @param string $sessionHash + * @param Room $room + * @param string $sessionId * @param string $displayName * @throws \Doctrine\DBAL\DBALException */ - public function updateName($sessionHash, $displayName) { + public function updateName(Room $room, $sessionId, $displayName) { + $sessionHash = sha1($sessionId); $result = $this->connection->insertIfNotExist('*PREFIX*talk_guests', [ 'session_hash' => $sessionHash, 'display_name' => $displayName, ], ['session_hash']); - if ($result === 1) { - return; + if ($result === 0) { + $query = $this->connection->getQueryBuilder(); + $query->update('talk_guests') + ->set('display_name', $query->createNamedParameter($displayName)) + ->where($query->expr()->eq('session_hash', $query->createNamedParameter($sessionHash))); + $query->execute(); } - $query = $this->connection->getQueryBuilder(); - $query->update('talk_guests') - ->set('display_name', $query->createNamedParameter($displayName)) - ->where($query->expr()->eq('session_hash', $query->createNamedParameter($sessionHash))); - $query->execute(); + $this->dispatcher->dispatch(self::class . '::updateName', new GenericEvent($room, [ + 'sessionId' => $sessionId, + 'newName' => $displayName, + ])); } /**