1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8if (basename($_SERVER['SCRIPT_NAME']) === basename(__FILE__)) {
9	die('This script may only be included.');
10}
11
12if (! isset($_REQUEST['mobile_mode']) || $_REQUEST['mobile_mode'] === 'y') {
13	$uagent_info = new uagent_info();
14
15	$supported_device = $uagent_info->DetectIphoneOrIpod() ||
16						$uagent_info->DetectIpad() ||
17						$uagent_info->DetectAndroid() ||
18						$uagent_info->DetectBlackBerry() ||
19						$uagent_info->DetectOperaMobile() ||
20						$uagent_info->DetectPalmWebOS();
21
22	if ((! getCookie('mobile_mode') && $supported_device) || getCookie('mobile_mode') === 'y') {		// supported by jquery.mobile
23		if (! is_array($prefs['mobile_perspectives'])) {
24			$prefs['mobile_perspectives'] = unserialize($prefs['mobile_perspectives']);
25		}
26		if (count($prefs['mobile_perspectives']) > 0) {
27			$persp = $prefs['mobile_perspectives'][0];
28
29			if (Perms::get([ 'type' => 'perspective', 'object' => $persp ])->perspective_view) {
30				$prefs['mobile_mode'] = 'y';
31
32				// pre-tiki14/bootstrap these prefs were disabled by default
33				// they can still be disabled via the mobile perspective if needed
34				/*$prefs['feature_jquery_ui'] = 'n';
35				$prefs['feature_jquery_reflection'] = 'n';
36				$prefs['feature_fullscreen'] = 'n';
37				$prefs['feature_syntax_highlighter'] = 'n';
38				$prefs['feature_layoutshadows'] = 'n';
39				$prefs['feature_wysiwyg'] = 'n';
40				$prefs['ajax_autosave'] = 'n';
41				$prefs['change_theme'] = 'n';
42				$prefs['feature_syntax_highlighter'] = 'n';
43				$prefs['jquery_ui_chosen'] = 'n';
44				$prefs['fgal_show_explorer'] = 'n';
45				$prefs['feature_fixed_width'] = 'n';
46				$prefs['fgal_elfinder_feature'] = 'n';
47				$prefs['wiki_auto_toc'] = 'n';
48				$prefs['feature_smileys'] = 'n';
49				$prefs['calendar_fullcalendar'] = 'n';
50				$prefs['feature_inline_comments'] = 'n';
51				$prefs['feature_jquery_tablesorter'] = 'n';*/
52
53				$headerlib = TikiLib::lib('header');
54
55				if ($prefs['feature_shadowbox'] === 'y') {
56					$headerlib
57						->add_jsfile_external('vendor_bundled/vendor/jquery/photoswipe/lib/klass.min.js', true)
58						->add_jsfile_external('vendor_bundled/vendor/jquery/photoswipe/code.photoswipe.jquery-3.0.5.min.js', true)
59						->add_jq_onready('var $photosToSwipe = $("a[data-box*=\'box\'][data-box*=\'type=img\'], a[data-box*=\'box\'][data-box!=\'type=\']");
60if ($photosToSwipe.length) {$photosToSwipe.photoSwipe();}', 5)
61						->add_cssfile('vendor_bundled/vendor/jquery/photoswipe/photoswipe.css');
62				}
63
64				global $base_url;
65				$perspectivelib = TikiLib::lib('perspective');
66				if (! in_array($perspectivelib->get_current_perspective($prefs), $prefs['mobile_perspectives'])) {	// change perspective
67					$hp = $prefs['wikiHomePage'];							// get default non mobile homepage
68
69					$_SESSION['current_perspective'] = $persp;
70					$_SESSION['current_perspective_name'] = $perspectivelib->get_perspective_name($_SESSION['current_perspective']);
71
72					if ($prefs['tikiIndex'] === 'tiki-index.php' && isset($_REQUEST['page'])) {
73						$pprefs = $perspectivelib->get_preferences($_SESSION['current_perspective']);
74						if (in_array('wikiHomePage', array_keys($pprefs))) {				// mobile persp has home page set (often the case)
75							if ($hp == $_REQUEST['page']) {
76								header('Location: ' . $base_url);							// so redirect to site root and try again
77							}
78						}
79					}
80				}
81			} else {
82				$prefs['mobile_mode'] = 'n';
83				if (! $supported_device) {	// send error only if not on a read mobile device
84					Feedback::error(tra('Mobile mode: Permission denied, please log in.'));
85				}
86			}
87		}
88	} else {
89		$prefs['mobile_mode'] = 'n';
90	}
91} else {
92	$prefs['mobile_mode'] = 'n';
93}
94
95if ($prefs['mobile_mode'] === 'y') {
96	setCookieSection('mobile_mode', $prefs['mobile_mode']);
97}
98