ossn.lib.javascripts.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Open Source Social Network
4  *
5  * @package (Informatikon.com).ossn
6  * @author OSSN Core Team <[email protected]>
7  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
8  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
9  * @link http://www.opensource-socialnetwork.org/licence
10  */
11 
12 /**
13  * Initialize the css library
14  *
15  * @return void
16  */
17 function ossn_javascript() {
18  ossn_register_page('js', 'ossn_javascript_pagehandler');
19  ossn_add_hook('js', 'register', 'ossn_js_trigger');
20 
21  ossn_extend_view('ossn/site/head', 'ossn_site_js');
22  ossn_extend_view('ossn/admin/head', 'ossn_admin_js');
23 
24  ossn_extend_view('ossn/site/head', 'ossn_jquery_add');
25  ossn_extend_view('ossn/admin/head', 'ossn_jquery_add');
26 
27  ossn_new_js('opensource.socialnetwork', 'javascripts/libraries/core');
28 
29  ossn_load_js('opensource.socialnetwork');
30  ossn_load_js('opensource.socialnetwork', 'admin');
31 
32  //link chartjs and jquery Ossn v3
33  ossn_new_external_js('chart.js', 'vendors/Chartjs/Chart.min.js');
34  ossn_new_external_js('chart.legend.js', 'vendors/Chartjs/chart.legend.js');
35  ossn_new_external_js('jquery-1.11.1.min.js', 'vendors/jquery/jquery-1.11.1.min.js');
36 
37  ossn_load_external_js('jquery-1.11.1.min.js');
38  ossn_load_external_js('jquery-1.11.1.min.js', 'admin');
39 }
40 
41 /**
42  * Add css page handler
43  *
44  * @return bool
45  */
47  $page = $js[0];
48  if (empty($js[1])) {
49  echo '404 SWITCH ERROR';
50  }
51  if (empty($page)) {
52  $page = 'view';
53  }
54  switch ($page) {
55  case 'view':
56  if (ossn_site_settings('cache') == 1) {
57  return false;
58  }
59  header("Content-type: text/javascript");
60  if (ossn_is_hook('js', "register")) {
61  echo ossn_call_hook('js', "register", $js);
62  }
63  break;
64 
65  default:
66  echo '404 SWITCH ERROR';
67  break;
68 
69  }
70 }
71 
72 /**
73  * Register a new css to system
74  *
75  * @param string $name The name of the css
76  * $file path to css file
77  *
78  * @return void
79  */
80 function ossn_new_js($name, $file) {
81  global $Ossn;
82  $Ossn->js[$name] = $file;
83 }
84 /**
85  * Register a new external js to system
86  *
87  * @param string $name The name of the css
88  * $file complete url path to css file
89  *
90  * @return void
91  */
92 function ossn_new_external_js($name, $file, $type = true) {
93  global $Ossn;
94  if($type){
95  $Ossn->jsExternal[$name] = ossn_site_url($file);
96  } else {
97  $Ossn->jsExternal[$name] = $file;
98  }
99 }
100 /**
101  * Remove a external js from system
102  *
103  * @param string $name The name of the css
104  * $file complete url path to css file
105  *
106  * @return void
107  */
108 function ossn_unlink_external_js($name) {
109  global $Ossn;
110  unset($Ossn->jsExternal[$name]);
111 }
112 /**
113  * Remove a js from system
114  *
115  * This will not remove js file it will just unregister it
116  * @param string $name The name of the js
117  *
118  * @return void
119  */
120 function ossn_unlink_new_js($name) {
121  global $Ossn;
122  if(isset($Ossn->js[$name])){
123  unset($Ossn->js[$name]);
124  }
125 }
126 
127 /**
128  * Get a tag for inserting css
129  *
130  * @params string $args array()
131  *
132  * @return string
133  */
134 function ossn_html_js($args) {
135  if(!is_array($args)){
136  return false;
137  }
138  $default = array(
139  'type' => 'text/javascript',
140  );
141  $args = array_merge($default, $args);
142  $extend = ossn_args($args);
143  return "\r\n<script {$extend}></script>";
144 }
145 
146 /**
147  * Load registered js to system for site
148  *
149  * @return html.tag
150  */
151 function ossn_load_js($name, $type = 'site') {
152  global $Ossn;
153  $Ossn->jshead[$type][] = $name;
154 }
155 /**
156  * Ossn system unloads js from head
157  *
158  * @param string $name The name of the js
159  *
160  * @return void
161  */
162 function ossn_unload_js($name, $type = 'site') {
163  global $Ossn;
164  $js = array_search($name, $Ossn->jshead[$type]);
165  if($js !== false){
166  unset($Ossn->jshead[$type][$js]);
167  }
168 }
169 /**
170  * Load registered js to system for site
171  *
172  * @return html.tag
173  */
174 function ossn_load_external_js($name, $type = 'site') {
175  global $Ossn;
176  $Ossn->jsheadExternal[$type][] = $name;
177 }
178 /**
179  * Ossn system unloads js from head
180  *
181  * @param string $name The name of the js
182  *
183  * @return void
184  */
185 function ossn_unload_external_js($name, $type = 'site') {
186  global $Ossn;
187  $js = array_search($name, $Ossn->jsheadExternal[$type]);
188  if($js !== false){
189  unset($Ossn->jsheadExternal[$type][$js]);
190  }
191 }
192 
193 /**
194  * Load js for frontend
195  *
196  * @return html.tags
197  */
198 function ossn_site_js() {
199  global $Ossn;
200  $url = ossn_site_url();
201 
202  //load external js
203  $external = $Ossn->jsheadExternal['site'];
204  if(!empty($external)){
205  foreach($external as $item){
206  echo ossn_html_js(array('src' => $Ossn->jsExternal[$item]));
207  }
208  }
209 
210  //load internal js
211  if (isset($Ossn->jshead['site'])) {
212  foreach ($Ossn->jshead['site'] as $js) {
213  $src = "{$url}js/view/{$js}.js";
214  if (ossn_site_settings('cache') == 1) {
215  $cache = ossn_site_settings('last_cache');
216  $src = "{$url}cache/js/{$cache}/view/{$js}.js";
217  }
218  echo ossn_html_js(array('src' => $src));
219  }
220  }
221 }
222 /**
223  * Load js to backend
224  *
225  * @return html.tags
226  */
227 function ossn_admin_js() {
228  global $Ossn;
229  $url = ossn_site_url();
230 
231  //load external js
232  $external = $Ossn->jsheadExternal['admin'];
233  if(!empty($external)){
234  foreach($external as $item){
235  echo ossn_html_js(array('src' => $Ossn->jsExternal[$item]));
236  }
237  }
238 
239  //load internal js
240  if (isset($Ossn->jshead['admin'])) {
241  foreach ($Ossn->jshead['admin'] as $js) {
242  $src = "{$url}js/view/{$js}.js";
243  if (ossn_site_settings('cache') == 1) {
244  $cache = ossn_site_settings('last_cache');
245  $src = "{$url}cache/js/{$cache}/view/{$js}.js";
246  }
247  echo ossn_html_js(array('src' => $src));
248  }
249  }
250 }
251 
252 /**
253  * Check if the requested js is registered then load js
254  *
255  * @return bool
256  */
257 function ossn_js_trigger($hook, $type, $value, $params) {
258  global $Ossn;
259  if (isset($params[1]) && substr($params[1], '-3') == '.js') {
260  $params[1] = str_replace('.js', '', $params[1]);
261  if (isset($Ossn->js[$params[1]])) {
262  $file = ossn_plugin_view($Ossn->js[$params[1]]);
263  $extended = ossn_fetch_extend_views("js/{$params[1]}");
264  $data = array(
265  $file,
266  $extended
267  );
268  return implode('', $data);
269  }
270  }
271  return false;
272 }
273 /**
274  * Load jquery framework to system
275  *
276  * @return js.html.tag
277  * @use ossn_new_external_js()
278  */
279 /**
280 function ossn_jquery_add() {
281  echo ossn_html_js(array('src' => ossn_site_url('vendors/jquery/jquery-1.11.1.min.js')));
282 } **/
283 function ossn_languages_js(){
284  $lang = ossn_site_settings('language');
285  $cache = ossn_site_settings('cache');
286  $last_cache = ossn_site_settings('last_cache');
287 
288  if($cache == true){
289  $js = "ossn.{$lang}.language";
290  $url = "cache/js/{$last_cache}/view/{$js}.js";
291  ossn_new_external_js($js, $url);
292 
293  ossn_load_external_js($js, 'site');
294  ossn_load_external_js($js, 'admin');
295  } else {
296 
297  ossn_new_js('ossn.language', 'javascripts/libraries/languages');
298 
299  ossn_load_js('ossn.language');
300  ossn_load_js('ossn.language', 'admin');
301  }
302 }
303 /**
304  * Redirect users to absolute url, if he is on wrong url
305  *
306  * Many users have issue while registeration, this is due to ossn.ajax works on absolute path
307  * Github ticket: https://github.com/opensource-socialnetwork/opensource-socialnetwork/issues/458
308  *
309  * @return void;
310  */
312  $baseurl = ossn_site_url();
313  $parts = parse_url($baseurl);
314  $iswww = preg_match('/www./i', $parts['host']);
315  $host = parse_url($_SERVER['HTTP_HOST']);
316  $redirect = false;
317  $port = "";
318  if(!isset($host['host'])){
319  $host = array();
320  $host['host'] = $_SERVER['HTTP_HOST'];
321  }
322 
323  if(isset($parts['port']) && !empty($parts['port'])){
324  $port = ":{$parts['port']}";
325  if ($parts['port'] == ':80' || $parts['port'] == ':443'){
326  $port = '';
327  }
328  if($parts['port'] !== (int)$_SERVER['SERVER_PORT']){
329  $redirect = true;
330  }
331  }
332  if(empty($parts['port']) && isset($_SERVER['SERVER_PORT']) && !empty($_SERVER['SERVER_PORT'])
333  && $_SERVER['SERVER_PORT'] !== '80' && $_SERVER['SERVER_PORT'] !=='443'){
334  $redirect = true;
335  }
336  if($parts['scheme'] == 'https' && empty($_SERVER["HTTPS"])
337  || (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" && $parts['scheme'] == 'http')) {
338  $redirect = true;
339  }
340 
341  if(($host['host'] !== $parts['host']) || $redirect){
342  header("HTTP/1.1 301 Moved Permanently");
343  $url = "{$parts['scheme']}://{$parts['host']}{$port}{$_SERVER['REQUEST_URI']}";
344  header("Location: {$url}");
345  }
346  }
347 ossn_register_callback('ossn', 'init', 'ossn_languages_js');
348 ossn_register_callback('ossn', 'init', 'ossn_javascript');
349 ossn_register_callback('ossn', 'init', 'ossn_redirect_absolute_url');