From 33f0e2687909bd4a9b85be3ee54ae163a29eb101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaussoin=20Timoth=C3=A9e?= Date: Wed, 3 Dec 2014 18:25:23 +0100 Subject: [PATCH] - Rewrite the WidgetWrapper and clean the old BOSH related code - Rewrite the AccountNext widget and move everything XMPP related to Moxl - New behaviour for the Websocket unregistration --- app/assets/js/movim_websocket.js | 34 +- app/widgets/AccountNext/AccountNext.php | 291 +++++------------- app/widgets/AccountNext/_accountnext_form.tpl | 45 +-- app/widgets/AccountNext/_accountnext_oob.tpl | 10 + .../AccountNext/_accountnext_registered.tpl | 9 + app/widgets/AccountNext/accountnext.js | 12 + app/widgets/AccountNext/accountnext.tpl | 8 +- app/widgets/AccountNext/locales.ini | 6 + .../ContactPubsubSubscription.php | 2 +- app/widgets/Login/Login.php | 7 +- app/widgets/Login/login.js | 2 +- app/widgets/Menu/Menu.php | 1 + app/widgets/Subscribe/subscribe.js | 4 + lib/XMPPtoForm.php | 16 +- linker.php | 16 +- locales/locales.ini | 1 + src/Movim/Daemon/Session.php | 4 +- system/Event.php | 10 +- system/Session.php | 1 - system/widget/WidgetWrapper.php | 254 ++++++--------- themes/movim/css/style.css | 3 +- .../movim/img/icons/placeholder/clipboard.png | Bin 0 -> 8457 bytes 22 files changed, 272 insertions(+), 464 deletions(-) create mode 100644 app/widgets/AccountNext/_accountnext_oob.tpl create mode 100644 app/widgets/AccountNext/_accountnext_registered.tpl create mode 100644 app/widgets/Subscribe/subscribe.js mode change 100755 => 100644 system/widget/WidgetWrapper.php create mode 100644 themes/movim/img/icons/placeholder/clipboard.png diff --git a/app/assets/js/movim_websocket.js b/app/assets/js/movim_websocket.js index 4278cd754..a9505fcb4 100755 --- a/app/assets/js/movim_websocket.js +++ b/app/assets/js/movim_websocket.js @@ -4,14 +4,6 @@ * This file define the websocket behaviour and handle its connection */ -WebSocket.prototype.register = function() { - this.send(JSON.stringify( - { - 'func' : 'register', - 'sid' : localStorage.movimSession, - 'baseuri' : BASE_URI - })); -}; WebSocket.prototype.unregister = function() { this.send(JSON.stringify({'func' : 'unregister'})); }; @@ -28,6 +20,7 @@ WebSocket.prototype.admin = function(key) { var MovimWebsocket = { connection: null, attached: null, + unregistered: false, launchAttached : function() { for(var i = 0; i < this.attached.length; i++) { @@ -49,12 +42,6 @@ var MovimWebsocket = { var obj = JSON.parse(data); - if(obj.id) { - localStorage.movimSession = obj.id; - document.cookie = 'MOVIM_SESSION_ID=' + obj.id; - this.register(); - } - if(obj.func == 'registered') { MovimWebsocket.launchAttached(); } @@ -69,7 +56,12 @@ var MovimWebsocket = { this.connection.onclose = function(e) { console.log("Connection closed by the server or session closed"); if(e.code == 1006 || e.code == 1000) { - movim_disconnect(); + if(MovimWebsocket.unregistered == false) { + movim_disconnect(); + } else { + MovimWebsocket.unregistered = false; + MovimWebsocket.init(); + } } }; @@ -105,7 +97,7 @@ var MovimWebsocket = { if(funcalls != null) { for(h = 0; h < funcalls.length; h++) { var funcall = funcalls[h]; - //console.log(funcall); + console.log(funcall); if(funcall.func != null && (typeof window[funcall.func] == 'function')) { try { window[funcall.func].apply(null, funcall.params); @@ -123,14 +115,20 @@ var MovimWebsocket = { } }, - unregister : function() { + unregister : function(reload) { + if(reload == false) this.unregistered = true; this.connection.unregister(); } } function remoteUnregister() { - MovimWebsocket.unregister(); + MovimWebsocket.unregister(false); +} + +function remoteUnregisterReload() +{ + MovimWebsocket.unregister(true); } window.onbeforeunload = function() { diff --git a/app/widgets/AccountNext/AccountNext.php b/app/widgets/AccountNext/AccountNext.php index b516b378f..d466e02df 100755 --- a/app/widgets/AccountNext/AccountNext.php +++ b/app/widgets/AccountNext/AccountNext.php @@ -17,234 +17,105 @@ * * See COPYING for licensing information. */ - + +use Moxl\Xec\Action\Disco\Request; +use Moxl\Xec\Action\Register\Get; +use Moxl\Xec\Action\Register\Set; + class AccountNext extends WidgetBase { - function load() { - $this->addcss('accountnext.css'); $this->addjs('accountnext.js'); - - $xml = requestURL('http://movim.eu/server-vcards.xml', 1); - if($xml) { - $xml = simplexml_load_string($xml); - $xml = (array)$xml->children(); - - $this->view->assign('servers', $xml['vcard']); - } else { - $this->view->assign('servers', false); - } - - if(isset($_GET['s'])) { - $this->view->assign( - 'getsubscriptionform', - $this->call('ajaxDiscoverServer', "'".$_GET['s']."'") - ); - - $this->view->assign('ndd', $_GET['s']); - } else { - $this->view->assign('getsubscriptionform',''); - $this->view->assign('ndd', ''); - } + $this->registerEvent('register_get_handle', 'onForm'); + $this->registerEvent('register_set_handle', 'onRegistered'); + $this->registerEvent('register_set_errorconflict', 'onRegisterError'); + $this->registerEvent('register_set_errornotacceptable', 'onRegisterNotAcceptable'); + $this->registerEvent('register_get_errorserviceunavailable', 'onServiceUnavailable'); } - function ajaxDiscoverServer($ndd) { - try { - $dns = dns_get_record('_xmpp-client._tcp.'.$ndd); - - if(isset($dns[0]['target']) && $dns[0]['target'] != null) { - $domain = $dns[0]['target']; - } else { - $domain = $ndd['ndd']; - } - - $f = fsockopen($domain, 5222, $errno, $errstr, 10); - - if(!$f ) { - RPC::call('movim_reload', Route::urlize('account', 'xmppconnect')); - RPC::commit(); - exit; - } - - global $language; - - $stream = simplexml_load_string(''); - $stream->addAttribute('to', $ndd); - if (false === fwrite($f, $stream->asXML())) { - \system\Logs\Logger::log('fail write to stream'); - throw new \Exception('fail write to stream'); - } - - unset($stream); - - $response = stream_get_contents($f); - if(!$response) { - RPC::call('movim_reload', Route::urlize('account', 'xmppcomm')); - RPC::commit(); - exit; - } - - $response = simplexml_load_string($response); - - $id = (string)$response->attributes()->id; - - $elements = (array)$response->iq->query; - - // We close properly our first register request - $stream = simplexml_load_string(''); - $stream->addAttribute('to', $ndd); - fwrite($f, $stream->asXML()); - - fclose($f); unset($f); - - if(!empty($elements)) { - $formview = $this->tpl(); - - if($response->iq->query->instructions && $response->iq->query->x) { - $instr = ' -
- '; - if($response->iq->query->x->url) - $instr .= ' - '. - (string)$response->iq->query->x->url.' - '; - $instr .= ' -
'; - - } - $form = new XMPPtoForm(); - if(!empty($response->iq->query->x)){ - $formh = $form->getHTML($response->iq->query->x->asXML()); - } else{// no element in the XML - $formh = $form->getHTML($response->iq->query->asXML()); - } - - if($formh != '') - $instr = ''; - - $formview->assign('instr', $instr); - $formview->assign('formh', $formh); - $formview->assign('id', $id); - $formview->assign('ndd', $ndd); - $formview->assign('domain', $domain); - - $formview->assign( - 'submitdata', - $this->call('ajaxSubmitData', "movim_form_to_json('data')")); - - $html = $formview->draw('_accountnext_form', true); + function display() + { + $host = $_GET['s']; + + $this->view->assign('getsubscriptionform', $this->call('ajaxGetForm', "'".$host."'")); + $this->view->assign('host', $host); + } - /* - if(isset($elements['data'])) { - $html .= ''; - } - */ - } else { - $html = ' -
- '.$this->__('create.notfound').' -
'; + function onForm($package) + { + $form = $package->content; + + $xtf = new \XMPPtoForm(); + if(!empty($form->x)){ + $ns = $form->x->getNamespaces(); + switch($ns['']) { + case 'jabber:x:data' : + $formview = $this->tpl(); + + $formh = $xtf->getHTML($form->x->asXML()); + $formview->assign('submitdata', $this->call('ajaxRegister', "movim_form_to_json('data')")); + + $formview->assign('formh', $formh); + $html = $formview->draw('_accountnext_form', true); + + RPC::call('movim_fill', 'subscription_form', $html); + break; + case 'jabber:x:oob' : + $oobview = $this->tpl(); + $oobview->assign('url', (string)$form->x->url); + + $html = $oobview->draw('_accountnext_oob', true); + + RPC::call('movim_fill', 'subscription_form', $html); + RPC::call('remoteUnregister'); + break; } - - RPC::call('movim_fill', 'subscription_form', $html); - RPC::commit(); - } catch(Exception $e) { - header(sprintf('HTTP/1.1 %d %s', $e->getCode(), $e->getMessage())); - header('Content-Type: text/plain; charset=utf-8'); - \system\Logs\Logger::log($e->getMessage()); + } else{ + $formh = $xtf->getHTML($form->asXML()); } } - function ajaxSubmitData($datas) { - define(XMPP_HOST, $datas->to->value); - define(XMPP_CONN, $datas->ndd->value); + function onRegistered($packet) + { + $data = $packet->content; + + $view = $this->tpl(); + $view->assign('url', (string)$form->x->url); - unset($datas->to); - unset($datas->ndd); + $html = $view->draw('_accountnext_registered', true); - define(XMPP_PORT, 5222); - - try { - // We try to connect to the XMPP Server - $f = fsockopen(XMPP_CONN, XMPP_PORT, $errno, $errstr, 10); - - if(!$f) { - RPC::call('movim_reload', Route::urlize('accountnext', 'xmppconnect')); - RPC::commit(); - exit; - } - - // We create the XML Stanza - $stream = simplexml_load_string(''); - - unset($datas->id); - - $stream->addAttribute('to', XMPP_HOST); - - $xmpp = new FormtoXMPP(); - $stream = $xmpp->getXMPP($stream->asXML(), $datas); - - fwrite($f, $stream->asXML()); - - unset($stream); - - $response = stream_get_contents($f); - - if(!$response) { - RPC::call('movim_reload', Route::urlize('accountnext', array(XMPP_HOST, 'xmppcomm'))); - RPC::commit(); - exit; - } - - fclose($f); unset($f); - - $response = simplexml_load_string($response); - - if(!$response) throw new Exception('The XMPP server sent an invalid response', 500); - - if($stream_error = $response->xpath('/stream:stream/stream:error')) { - list($stream_error) = $stream_error; - list($cond) = $stream_error->children(); - - throw new Exception($stream_error->text ? $stream_error->text : $cond->getName(), 500); - } - - $iq = $response->iq; + RPC::call('movim_fill', 'subscription_form', $html); + RPC::call('setUsername', $data->username->value); + RPC::call('remoteUnregister'); + } - if($iq->error) { - list($cond) = $iq->error->children(); - if($cond->getName() == 'conflict') { - RPC::call('movim_reload', Route::urlize('accountnext', array(XMPP_HOST,'userconflict'))); - RPC::commit(); - exit; - } else if($cond->getName() == 'not-acceptable') { - RPC::call('movim_reload', Route::urlize('accountnext', array(XMPP_HOST,'notacceptable'))); - RPC::commit(); - exit; - } - throw new Exception($iq->error->text ? $iq->error->text : $cond->getName(), 400); - } + function onRegisterError($package) + { + $error = $package->content; + RPC::call('movim_fill', 'subscription_error', '
'.$error.'
'); + } + + function onRegisterNotAcceptable() + { + RPC::call('movim_fill', 'subscription_error', '
'.$this->__('error.not_acceptable').'
'); + } + + function onServiceUnavailable() + { + RPC::call('movim_fill', 'subscription_form', '
'.$this->__('error.service_unavailable').'
'); + RPC::call('remoteUnregister'); + } - if($iq = $response->iq and $iq->attributes()->type == 'result') { - RPC::call('movim_reload', Route::urlize('login', 'acccreated')); - RPC::commit(); - exit; - } else { - RPC::call('movim_reload', Route::urlize('accountnext', array(XMPP_HOST,'unknown'))); - RPC::commit(); - exit; - } - } catch(Exception $e) { - header(sprintf('HTTP/1.1 %d %s', $e->getCode(), $e->getMessage())); - header('Content-Type: text/plain; charset=utf-8'); - \system\Logs\Logger::log($e->getCode().' '.$e->getMessage().' file:'.$e->getFile().' - l.'.$e->getLine()); - } + function ajaxGetForm($host) + { + \Moxl\Stanza\Stream::init($host); } - function flagPath($country) { - return BASE_URI.'themes/movim/img/flags/'.strtolower($country).'.png'; + function ajaxRegister($form) + { + $s = new Set; + $s->setData($form)->request(); } } diff --git a/app/widgets/AccountNext/_accountnext_form.tpl b/app/widgets/AccountNext/_accountnext_form.tpl index 11724d3f8..6a97195bb 100755 --- a/app/widgets/AccountNext/_accountnext_form.tpl +++ b/app/widgets/AccountNext/_accountnext_form.tpl @@ -1,47 +1,14 @@
-
- {$instr} +
-
+
+
{$formh} - - - - - - - - - - {$c->__('button.validate')} + {$c->__('button.validate')}
diff --git a/app/widgets/AccountNext/_accountnext_oob.tpl b/app/widgets/AccountNext/_accountnext_oob.tpl new file mode 100644 index 000000000..451cc1794 --- /dev/null +++ b/app/widgets/AccountNext/_accountnext_oob.tpl @@ -0,0 +1,10 @@ +

+ {$c->__('oob.about')} {$url} +

+ + + {$c->__('page.login')} + diff --git a/app/widgets/AccountNext/_accountnext_registered.tpl b/app/widgets/AccountNext/_accountnext_registered.tpl new file mode 100644 index 000000000..4fdc3add1 --- /dev/null +++ b/app/widgets/AccountNext/_accountnext_registered.tpl @@ -0,0 +1,9 @@ +
+ {$c->__('create.successfull')} + +

username@server.com

+ + + {$c->__('page.login')} + +
diff --git a/app/widgets/AccountNext/accountnext.js b/app/widgets/AccountNext/accountnext.js index 8b1378917..065a09c58 100755 --- a/app/widgets/AccountNext/accountnext.js +++ b/app/widgets/AccountNext/accountnext.js @@ -1 +1,13 @@ +var AccountNext = { + host : '', + setHost : function(host) { + this.host = host; + }, + setUsername : function(user) { + document.querySelector('#username').innerHTML = user + '@' + this.host; + } +} +function setUsername(user) { + AccountNext.setUsername(user); +} diff --git a/app/widgets/AccountNext/accountnext.tpl b/app/widgets/AccountNext/accountnext.tpl index 5316bd71c..447f9b4f6 100755 --- a/app/widgets/AccountNext/accountnext.tpl +++ b/app/widgets/AccountNext/accountnext.tpl @@ -1,12 +1,16 @@
-

{$c->__('create.title')} {$c->__('on')} {$ndd}

+

{$c->__('create.title')} {$c->__('on')} {$host}

{$c->__('loading')}
diff --git a/app/widgets/AccountNext/locales.ini b/app/widgets/AccountNext/locales.ini index c6ac3d769..57f38de23 100755 --- a/app/widgets/AccountNext/locales.ini +++ b/app/widgets/AccountNext/locales.ini @@ -1,4 +1,10 @@ create.title = 'Create a new account' create.notfound = 'No account creation form found on the server' create.on = 'on' +create.successfull = 'Your acccount has been successfully registered' loading = 'Loading' + +error.not_acceptable = 'Not Acceptable' +error.service_unavailable = 'The registration system of this server is currently unavailable' + +oob.about = 'This server use an external system for the registration, please click on the following URL.' diff --git a/app/widgets/ContactPubsubSubscription/ContactPubsubSubscription.php b/app/widgets/ContactPubsubSubscription/ContactPubsubSubscription.php index ee388ac1e..545b36148 100755 --- a/app/widgets/ContactPubsubSubscription/ContactPubsubSubscription.php +++ b/app/widgets/ContactPubsubSubscription/ContactPubsubSubscription.php @@ -41,7 +41,7 @@ class ContactPubsubSubscription extends WidgetBase foreach($list as $item){ if(is_array($item)) { - $html .= '
  • '.$item['title'].'
  • '; + $html .= '
  • '.$item[2].'
  • '; } } diff --git a/app/widgets/Login/Login.php b/app/widgets/Login/Login.php index 1556a0590..354d6961b 100755 --- a/app/widgets/Login/Login.php +++ b/app/widgets/Login/Login.php @@ -138,7 +138,7 @@ class Login extends WidgetBase break; } - RPC::call('remoteUnregister'); + RPC::call('remoteUnregisterReload'); RPC::call('movim_desktop_notification', $title, $warning); } @@ -309,14 +309,13 @@ class Login extends WidgetBase // We create a new session or clear the old one $s = Sessionx::start(); - $s->init($user, $element['pass'], $host, $domain); // We save the loaded widgets list in the database - $wrapper = WidgetWrapper::getInstance(false); + /*$wrapper = WidgetWrapper::getInstance(false); $sess = Session::start(APP_NAME); - $sess->set('registered_events', $wrapper->registerEvents()); + $sess->set('registered_events', $wrapper->registerEvents());*/ \Moxl\Stanza\Stream::init($host); } diff --git a/app/widgets/Login/login.js b/app/widgets/Login/login.js index f184825bf..6d370aa01 100755 --- a/app/widgets/Login/login.js +++ b/app/widgets/Login/login.js @@ -109,7 +109,7 @@ MovimWebsocket.attach(function() } }); -movim_add_onload(function() { +movim_add_onload(function() { /* Dump cache variables in localStorage */ for ( var i = 0, len = localStorage.length; i < len; ++i ) { var cache = localStorage.key(i); diff --git a/app/widgets/Menu/Menu.php b/app/widgets/Menu/Menu.php index 5654aa4e0..8676299f5 100644 --- a/app/widgets/Menu/Menu.php +++ b/app/widgets/Menu/Menu.php @@ -54,6 +54,7 @@ class Menu extends WidgetCommon RPC::call('movim_append', 'menu_widget', $html); } else { RPC::call('movim_fill', 'menu_widget', $html); + RPC::call('movim_posts_unread', 0); } RPC::call('Menu.refresh'); } diff --git a/app/widgets/Subscribe/subscribe.js b/app/widgets/Subscribe/subscribe.js new file mode 100644 index 000000000..82ed3787d --- /dev/null +++ b/app/widgets/Subscribe/subscribe.js @@ -0,0 +1,4 @@ +MovimWebsocket.attach(function() +{ + remoteUnregister(); +}); diff --git a/lib/XMPPtoForm.php b/lib/XMPPtoForm.php index 35e0b9f9e..0e197ef02 100755 --- a/lib/XMPPtoForm.php +++ b/lib/XMPPtoForm.php @@ -139,8 +139,8 @@ class XMPPtoForm{ required; if($s->value == "true" || $s->value == "1") $this->html .= ' checked'; @@ -154,8 +154,8 @@ class XMPPtoForm{