Elgg  Version 4.x
elgglib.js
Go to the documentation of this file.
1 
4 var elgg = elgg || {};
5 
9 elgg.assertTypeOf = function(type, val) {
10  if (typeof val !== type) {
11  throw new TypeError("Expecting param of " +
12  arguments.caller + "to be a(n) " + type + "." +
13  " Was actually a(n) " + typeof val + ".");
14  }
15 };
16 
30 elgg.normalize_url = function(url) {
31  url = url || '';
32  elgg.assertTypeOf('string', url);
33 
34  function validate(url) {
35  url = elgg.parse_url(url);
36  if (url.scheme) {
37  url.scheme = url.scheme.toLowerCase();
38  }
39  if (url.scheme == 'http' || url.scheme == 'https') {
40  if (!url.host) {
41  return false;
42  }
43  /* hostname labels may contain only alphanumeric characters, dots and hypens. */
44  if (!(new RegExp("^([a-zA-Z0-9][a-zA-Z0-9\\-\\.]*)$", "i")).test(url.host) || url.host.charAt(-1) == '.') {
45  return false;
46  }
47  }
48  /* some schemas allow the host to be empty */
49  if (!url.scheme || !url.host && url.scheme != 'mailto' && url.scheme != 'news' && url.scheme != 'file') {
50  return false;
51  }
52  return true;
53  };
54 
55  // ignore anything with a recognized scheme
56  if (url.indexOf('http:') === 0 || url.indexOf('https:') === 0 || url.indexOf('javascript:') === 0 || url.indexOf('mailto:') === 0 ) {
57  return url;
58  } else if (validate(url)) {
59  // all normal URLs including mailto:
60  return url;
61  } else if ((new RegExp("^(\\#|\\?|//)", "i")).test(url)) {
62  // '//example.com' (Shortcut for protocol.)
63  // '?query=test', #target
64  return url;
65  } else if ((new RegExp("^[^\/]*\\.php(\\?.*)?$", "i")).test(url)) {
66  // watch those double escapes in JS.
67  // 'install.php', 'install.php?step=step'
68  if (url.indexOf('/') === 0) {
69  url = url.substring(1);
70  }
71 
72  return elgg.config.wwwroot + url;
73  } else if ((new RegExp("^[^/\\?\\#]*\\.", "i")).test(url)) {
74  // 'example.com', 'example.com/subpage'
75  return 'http://' + url;
76  } else {
77  // 'page/handler', 'mod/plugin/file.php'
78  // trim off any leading / because the site URL is stored
79  // with a trailing /
80  if (url.indexOf('/') === 0) {
81  url = url.substring(1);
82  }
83 
84  return elgg.config.wwwroot + url;
85  }
86 };
87 
95 elgg.deprecated_notice = function(msg, dep_version) {
96  if (elgg.is_admin_logged_in()) {
97  msg = "Deprecated in Elgg " + dep_version + ": " + msg;
98  if (typeof console !== "undefined") {
99  console.info(msg);
100  }
101  }
102 };
103 
110 elgg.forward = function(url) {
111  var dest = elgg.normalize_url(url);
112 
113  if (dest == location.href) {
114  location.reload();
115  }
116 
117  // in case the href set below just changes the hash, we want to reload. There's sadly
118  // no way to force a reload and set a different hash at the same time.
119  $(window).on('hashchange', function () {
120  location.reload();
121  });
122 
123  location.href = dest;
124 };
125 
135 elgg.parse_url = function(url, component, expand) {
136  // Adapted from http://blog.stevenlevithan.com/archives/parseuri
137  // which was release under the MIT
138  // It was modified to fix mailto: and javascript: support.
139  expand = expand || false;
140  component = component || false;
141 
142  var re_str =
143  // scheme (and user@ testing)
144  '^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?://)?'
145  // possibly a user[:password]@
146  + '((?:(([^:@]*)(?::([^:@]*))?)?@)?'
147  // host and port
148  + '([^:/?#]*)(?::(\\d*))?)'
149  // path
150  + '(((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))'
151  // query string
152  + '(?:\\?([^#]*))?'
153  // fragment
154  + '(?:#(.*))?)';
155  var keys = {
156  1: "scheme",
157  4: "user",
158  5: "pass",
159  6: "host",
160  7: "port",
161  9: "path",
162  12: "query",
163  13: "fragment"
164  };
165  var results = {};
166 
167  if (url.indexOf('mailto:') === 0) {
168  results['scheme'] = 'mailto';
169  results['path'] = url.replace('mailto:', '');
170  return results;
171  }
172 
173  if (url.indexOf('javascript:') === 0) {
174  results['scheme'] = 'javascript';
175  results['path'] = url.replace('javascript:', '');
176  return results;
177  }
178 
179  var re = new RegExp(re_str);
180  var matches = re.exec(url);
181 
182  for (var i in keys) {
183  if (matches[i]) {
184  results[keys[i]] = matches[i];
185  }
186  }
187 
188  if (expand && typeof(results['query']) != 'undefined') {
189  results['query'] = elgg.parse_str(results['query']);
190  }
191 
192  if (component) {
193  if (typeof(results[component]) != 'undefined') {
194  return results[component];
195  } else {
196  return false;
197  }
198  }
199  return results;
200 };
201 
208 elgg.parse_str = function(string) {
209  var params = {},
210  result,
211  key,
212  value,
213  re = /([^&=]+)=?([^&]*)/g,
214  re2 = /\[\]$/;
215 
216  // assignment intentional
217  while (result = re.exec(string)) {
218  key = decodeURIComponent(result[1].replace(/\+/g, ' '));
219  value = decodeURIComponent(result[2].replace(/\+/g, ' '));
220 
221  if (re2.test(key)) {
222  key = key.replace(re2, '');
223  if (!params[key]) {
224  params[key] = [];
225  }
226  params[key].push(value);
227  } else {
228  params[key] = value;
229  }
230  }
231 
232  return params;
233 };
234 
247 elgg.getSelectorFromUrlFragment = function(url) {
248  var fragment = url.split('#')[1];
249 
250  if (fragment) {
251  // this is a .class or a tag.class
252  if (fragment.indexOf('.') > -1) {
253  return fragment;
254  } else {
255  // this is an id
256  return '#' + fragment;
257  }
258  }
259  return '';
260 };
261 
267 elgg.get_logged_in_user_guid = function() {
268  return elgg.user ? elgg.user.guid : 0;
269 };
270 
276 elgg.is_logged_in = function() {
277  return elgg.get_logged_in_user_guid > 0;
278 };
279 
285 elgg.is_admin_logged_in = function() {
286  return elgg.user ? elgg.user.admin : false;
287 };
288 
294 elgg.get_site_url = function() {
295  return elgg.config.wwwroot;
296 };
297 
305 elgg.get_simplecache_url = function(view, subview) {
306  elgg.assertTypeOf('string', view);
307 
308  var lastcache, path;
309 
310  if (elgg.config.simplecache_enabled) {
311  lastcache = elgg.config.lastcache;
312  } else {
313  lastcache = 0;
314  }
315 
316  if (!subview) {
317  path = '/cache/' + lastcache + '/' + elgg.config.viewtype + '/' + view;
318  } else {
319  elgg.assertTypeOf('string', subview);
320 
321  if ((view === 'js' || view === 'css') && 0 === subview.indexOf(view + '/')) {
322  subview = subview.substr(view.length + 1);
323  }
324  path = '/cache/' + lastcache + '/' + elgg.config.viewtype + '/' + view + '/' + subview;
325  }
326 
327  return elgg.normalize_url(path);
328 };
$data value
Definition: default.php:27
Bundled plugins(the contents of the"/mod"directory) are available only under the GPLv2 license.The remainder of the project is available under either MIT or GPLv2.Both licenses can be found below.More info and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed on
Definition: LICENSE.txt:96
var elgg
Definition: elgglib.js:4