Welcome to mirror list, hosted at ThFree Co, Russian Federation.

admin.js « js « build - github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d9c8fa19540a5cdd142a56bd68b6e79341690481 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*!
 * ojsxc v3.1.0-beta - 2017-01-23
 * 
 * Copyright (c) 2017 Klaus Herberth <klaus@jsxc.org> <br>
 * Released under the MIT license
 * 
 * Please see http://www.jsxc.org/
 * 
 * @author Klaus Herberth <klaus@jsxc.org>
 * @version 3.1.0-beta
 * @license MIT
 */

/* global $, OC */

$(document).ready(function() {
   /**
    * Test if bosh server is up and running.
    * 
    * @param  {string}   url    BOSH url
    * @param  {string}   domain host domain for BOSH server
    * @param  {Function} cb     called if test is done
    */
   function testBoshServer(url, domain, cb) {
      var rid = jsxc.storage.getItem('rid') || '123456';

      function fail(m) {
         var msg = 'BOSH server NOT reachable or misconfigured.';

         if (typeof m === 'string') {
            msg += '<br /><br />' + m;
         }

         cb({
            status: 'fail',
            msg: msg
         });
      }

      $.ajax({
         type: 'POST',
         url: url,
         data: "<body rid='" + rid + "' xmlns='http://jabber.org/protocol/httpbind' to='" + domain + "' xml:lang='en' wait='60' hold='1' content='text/xml; charset=utf-8' ver='1.6' xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'/>",
         global: false,
         dataType: 'xml'
      }).done(function(stanza) {
         if (typeof stanza === 'string') {
            // shouldn't be needed anymore, because of dataType
            stanza = $.parseXML(stanza);
         }

         var body = $(stanza).find('body[xmlns="http://jabber.org/protocol/httpbind"]');
         var condition = (body) ? body.attr('condition') : null;
         var type = (body) ? body.attr('type') : null;

         // we got a valid xml response, but we have test for errors

         if (body.length > 0 && type !== 'terminate') {
            cb({
               status: 'success',
               msg: 'BOSH Server reachable.'
            });
         } else {
            if (condition === 'internal-server-error') {
               fail('Internal server error: ' + body.text());
            } else if (condition === 'host-unknown') {
               if (url) {
                  fail('Host unknown: ' + domain + ' is unknown to your XMPP server.');
               } else {
                  fail('Host unknown: Please provide a XMPP domain.');
               }
            } else {
               fail(condition);
            }
         }
      }).fail(function(xhr, textStatus) {
         // no valid xml, not found or csp issue

         var fullurl;
         if (url.match(/^https?:\/\//)) {
            fullurl = url;
         } else {
            fullurl = window.location.protocol + '//' + window.location.host;
            if (url.match(/^\//)) {
               fullurl += url;
            } else {
               fullurl += window.location.pathname.replace(/[^/]+$/, "") + url;
            }
         }

         if(xhr.status === 0) {
            // cross-side
            fail('Cross domain request was not possible. Either your BOSH server does not send any ' +
               'Access-Control-Allow-Origin header or the content-security-policy (CSP) blocks your request. ' +
               'Starting from Owncloud 9.0 your CSP will be updated in any app which uses the appframework (e.g. files) ' +
               'after you save these settings and reload.' +
               'The savest way is still to use Apache ProxyRequest or Nginx proxy_pass.');
         } else if (xhr.status === 404) {
            // not found
            fail('Your server responded with "404 Not Found". Please check if your BOSH server is running and reachable via ' + fullurl + '.');
         } else if (textStatus === 'parsererror') {
            fail('Invalid XML received. Maybe ' + fullurl + ' was redirected. You should use an absolute url.');
         } else {
            fail(xhr.status + ' ' + xhr.statusText);
         }
      });
   }

   $('#ojsxc [name=serverType]').change(function(){
      $('#ojsxc .ojsxc-external, #ojsxc .ojsxc-internal').hide();
      $('#ojsxc .ojsxc-external, #ojsxc .ojsxc-internal').find('.required').removeAttr('required');
      $('#ojsxc .ojsxc-' + $(this).val()).show();
      $('#ojsxc .ojsxc-' + $(this).val()).find('.required').attr('required', 'true');
   });
   $('#ojsxc [name=serverType]:checked').change();

   $('#boshUrl, #xmppDomain').on('input', function(){
      var self = $(this);
      var timeout = self.data('timeout');

      if (timeout) {
         clearTimeout(timeout);
      }

      var url = $('#boshUrl').val();
      var domain = $('#xmppDomain').val();

      if (!url || !domain) {
         // we need url and domain to test BOSH server
         return;
      }

      $('#ojsxc .boshUrl-msg').html('<div></div>');
      var status = $('#ojsxc .boshUrl-msg div');
      status.html('<img src="' + jsxc.options.root + '/img/loading.gif" alt="wait" width="16px" height="16px" /> Testing BOSH Server...');

      // test only every 2 seconds
      timeout = setTimeout(function() {
         testBoshServer(url, domain, function(res) {
            status.addClass('jsxc_' + res.status);
            status.html(res.msg);
         });
      }, 2000);

      self.data('timeout', timeout);
   });

   $('#ojsxc').submit(function(event) {
      event.preventDefault();

      var post = $(this).serialize();

      $('#ojsxc .msg').html('<div>');
      var status = $('#ojsxc .msg div');
      status.html('<img src="' + jsxc.options.root + '/img/loading.gif" alt="wait" width="16px" height="16px" /> Saving...');

      $.post(OC.filePath('ojsxc', 'ajax', 'setAdminSettings.php'), post, function(data) {
         if (data) {
            status.addClass('jsxc_success').text('Settings saved. Please log out and in again.');
         } else {
            status.addClass('jsxc_fail').text('Error!');
         }

         setTimeout(function(){
            status.hide('slow');
         }, 3000);
      });
   });
});