splashify.display.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /**
  3. * @file
  4. * Handles displaying the splash page.
  5. *
  6. * Either creates the JS for handling the splash page (redirect, lightbox or
  7. * popup) or displays the What: Content full screen or in the site design.
  8. */
  9. /**
  10. * Implements hook_init().
  11. *
  12. * Generates the JS for the redirect, lightbox or popup window.
  13. */
  14. function splashify_init() {
  15. global $user;
  16. global $base_url;
  17. // Load the jStorage library.
  18. $library = libraries_load('jstorage', 'minified');
  19. // If this is the cron script or drush, do not run the splashify code.
  20. $drush_check = (function_exists('drush_verify_cli') && call_user_func('drush_verify_cli')) ? TRUE : FALSE;
  21. $cron_check = (strpos($_SERVER['PHP_SELF'], 'cron.php') !== FALSE) ? TRUE : FALSE;
  22. if ($drush_check || $cron_check) {
  23. return;
  24. }
  25. /*
  26. * Step #1: Retrieve the admin settings.
  27. */
  28. $config = _splashify_get_config();
  29. $only_anonymous = $config['only_anonymous'];
  30. $when_frequency = $config['when_frequency'];
  31. $where_page = $config['where_page'];
  32. $where_listpages = $config['where_listpages'];
  33. $where_opposite = $config['where_opposite'];
  34. $what_mode = $config['what_mode'];
  35. $what_content_value = $config['what_content_value'];
  36. $how_mode = $config['how_mode'];
  37. $how_size = $config['how_size'];
  38. /*
  39. * Step #2: Should we display the splash page?
  40. */
  41. if ($only_anonymous && $user->uid > 0) {
  42. return;
  43. }
  44. if ($when_frequency == 'never') {
  45. return;
  46. }
  47. // Set variable whether to show page, default is true
  48. $is_role = TRUE;
  49. // Check whether use role setting is checked
  50. if($config['when_roles']){
  51. // If use role setting is checked set variable not to show.
  52. $is_role = FALSE;
  53. // Check if any roles match and then sat show to true.
  54. foreach($user->roles as $v) {
  55. if(in_array($v, $config['when_roles_options'])) {
  56. $is_role = TRUE;
  57. }
  58. }
  59. }
  60. // Check the outcome of above and return if no roles match
  61. if (!$is_role) {
  62. return;
  63. }
  64. // Default to not showing the splash page.
  65. $splash_display = FALSE;
  66. $splash_get_var = '';
  67. if (isset($_GET['splash'])) {
  68. $splash_get_var = $_GET['splash'];
  69. }
  70. $splash_correct_page = FALSE;
  71. switch ($where_page) {
  72. case 'all':
  73. // Display on all pages.
  74. $splash_correct_page = TRUE;
  75. break;
  76. case 'home':
  77. $home_page = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
  78. if ($where_opposite) {
  79. if ($_GET['q'] != $home_page) {
  80. // Display on every page except the home page.
  81. $splash_correct_page = TRUE;
  82. }
  83. }
  84. else {
  85. if ($_GET['q'] == $home_page) {
  86. // Display on the home page.
  87. $splash_correct_page = TRUE;
  88. }
  89. }
  90. break;
  91. case 'list':
  92. $list_paths = drupal_strtolower($where_listpages);
  93. $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
  94. $splash_correct_page = drupal_match_path($path, $list_paths);
  95. if ($path != $_GET['q']){
  96. $splash_correct_page = $splash_correct_page || drupal_match_path($_GET['q'], $list_paths);
  97. }
  98. $splash_correct_page = ($splash_correct_page xor $where_opposite);
  99. break;
  100. }
  101. /*
  102. * Goes through the main server side checks to see if we should show the
  103. * splash page.
  104. */
  105. if (empty($what_content_value)) {
  106. // The What: Content field is not specified.
  107. $splash_display = FALSE;
  108. }
  109. elseif ($splash_get_var == 'off') {
  110. // Special case for preventing the splash page from showing up.
  111. $splash_display = FALSE;
  112. }
  113. elseif ($splash_get_var == 'on') {
  114. // Special case for forcing the splash page to show up.
  115. $splash_display = TRUE;
  116. // Modify this value to force the splash to show up in this case.
  117. $when_frequency = 'always';
  118. }
  119. elseif ($splash_correct_page) {
  120. // We need to show the splash page on this page. Do one last check.
  121. $parsed_url = parse_url($base_url);
  122. // We passed all of the tests...display the splash page!
  123. $splash_display = TRUE;
  124. }
  125. // If we shouldn't display the splash, end the code here.
  126. if (!$splash_display) {
  127. return;
  128. }
  129. /*
  130. * Step #3: Display the Splash Page
  131. *
  132. * At this point, we know that we should display the splash page.
  133. */
  134. // Controls when the jStorage variable should expire next. In other words,
  135. // when should the splash page show up again?
  136. $js_expiretime = '';
  137. // Since the time is based on server time, we need to pass this value to
  138. // the JS.
  139. $js_nowtime = time();
  140. // This variable is a way to always show the splash in the JS.
  141. $js_splash_always = FALSE;
  142. switch ($when_frequency) {
  143. case 'once':
  144. // Set to expire in one year.
  145. $js_expiretime = time() + (365 * 24 * 60 * 60);
  146. break;
  147. case 'daily':
  148. // Set to expire in 24 hours.
  149. $js_expiretime = time() + (24 * 60 * 60);
  150. break;
  151. case 'weekly':
  152. // Set to expire in 7 days.
  153. $js_expiretime = time() + (7 * 24 * 60 * 60);
  154. break;
  155. case 'always':
  156. // This should make the splash always show up on the next page load.
  157. $js_expiretime = time();
  158. $js_splash_always = TRUE;
  159. break;
  160. }
  161. // Deal with the mode settings.
  162. $js_mode_settings = array();
  163. $js_mode_settings['mode'] = $what_mode;
  164. $js_mode_settings['system_splash'] = '';
  165. $js_mode_settings['urls'] = '';
  166. if ($what_mode == 'template' || $what_mode == 'fullscreen') {
  167. // We need to redirect to the System generated splash page. Define the
  168. // action url. This variable tells the JS that we only are dealing with one
  169. // url.
  170. $js_mode_settings['system_splash'] = url('splashify-splash');
  171. }
  172. elseif ($what_mode == 'sequence' || $what_mode == 'random') {
  173. // Split up the textarea field by lines.
  174. $what_paths = preg_split('/[\n\r]+/', $what_content_value);
  175. // This holds all of the url values in the entered order.
  176. $js_mode_settings['urls'] = array();
  177. $js_mode_settings['total_urls'] = count($what_paths);
  178. foreach ($what_paths as $path) {
  179. $js_mode_settings['urls'][] = url(trim($path));
  180. }
  181. }
  182. // Define the last remaining JS variables we need to send.
  183. $size_action = $how_size ? explode('x', $how_size) : FALSE;
  184. $js_mode = '';
  185. switch ($how_mode) {
  186. case 'redirect':
  187. // Redirect to a different url.
  188. $js_mode = 'redirect';
  189. break;
  190. case 'window':
  191. // Open up a popup window.
  192. $js_mode = 'window';
  193. $js_mode_settings['size'] = $size_action ? 'width=' . $size_action[0] . ',height=' . $size_action[1] : '';
  194. break;
  195. case 'lightbox':
  196. if (module_exists('colorbox')) {
  197. // Display a ColorBox.
  198. $js_mode = 'colorbox';
  199. // Get the size of the lightbox.
  200. if (count($size_action) > 1) {
  201. $colorbox_width = $size_action[0];
  202. $colorbox_height = $size_action[1];
  203. }
  204. else {
  205. // Default size settings for the colorbox.
  206. $colorbox_width = 800;
  207. $colorbox_height = 600;
  208. }
  209. $js_mode_settings['size_width'] = $colorbox_width;
  210. $js_mode_settings['size_height'] = $colorbox_height;
  211. }
  212. else {
  213. return;
  214. }
  215. break;
  216. default:
  217. // Do nothing! This is a fail safe.
  218. return;
  219. }
  220. /*
  221. * Finally: Include the JS that puts it all together!
  222. *
  223. * At this point the page passed all of the server side checks. We now
  224. * implement JS code that checks if the splash page should show up, based
  225. * on when it last showed up. It then executes the JS action code, based
  226. * on the specified settings.
  227. */
  228. // Make our splash settings variables available to our JavaScript.
  229. $js_settings = array(
  230. 'js_splash_always' => $js_splash_always ? '1' : '0',
  231. 'js_expire_after' => $js_expiretime - $js_nowtime,
  232. 'js_mode' => $js_mode,
  233. 'js_mode_settings' => $js_mode_settings,
  234. 'js_disable_referrer_check' => variable_get('disable_referrer_check', 0),
  235. );
  236. drupal_add_js(array('splashify' => $js_settings), array(
  237. 'type' => 'setting',
  238. 'cache' => TRUE,
  239. 'weight' => -100,
  240. 'every_page' => TRUE,
  241. ));
  242. // Include the main JS file that does the heavy lifting.
  243. $js_splashify_int = drupal_get_path('module', 'splashify') . '/js/splashify_init_min.js';
  244. drupal_add_js($js_splashify_int, array(
  245. 'type' => 'file',
  246. 'scope' => 'header',
  247. 'group' => JS_THEME,
  248. 'cache' => TRUE,
  249. 'weight' => -99,
  250. 'every_page' => TRUE,
  251. ));
  252. }
  253. /**
  254. * Display the What: Content either in the site template or full screen.
  255. *
  256. * @return string
  257. * If the $what_mode equals 'template', returns the string entered into
  258. * $what_content_value. If $what_mode equals 'fullscreen', it displays
  259. * the text right there and exits the request.
  260. */
  261. function splashify_display_splashtext() {
  262. $config = _splashify_get_config();
  263. $what_mode = $config['what_mode'];
  264. $what_content_value = $config['what_content_value'];
  265. $what_content_title = $config['what_content_title'];
  266. if ($what_mode == 'template') {
  267. // Display what they entered in the field in the site template.
  268. if (!empty($what_content_title)) {
  269. drupal_set_title($what_content_title);
  270. }
  271. return $what_content_value;
  272. }
  273. elseif ($what_mode == 'fullscreen') {
  274. // Display what they entered in the field as a full page.
  275. echo $what_content_value;
  276. drupal_exit();
  277. }
  278. }
  279. /**
  280. * Retrieves the admin settings.
  281. *
  282. * Will either retrieve the desktop or mobile settings, depending whether this
  283. * is a mobile request or not.
  284. *
  285. * @return array
  286. * Returns an associate array with the following keys: when_frequency,
  287. * when_mobile_test, where_page, where_listpages, what_mode, what_content,
  288. * what_content_value, how_mode, and how_size.
  289. */
  290. function _splashify_get_config() {
  291. // Control whether user roles are being used or not
  292. $when_roles = variable_get('splashify_when_roles', '');
  293. // If roles are enabled get the options
  294. if($when_roles){
  295. $when_roles_options = variable_get('splashify_when_roles_options', '');
  296. foreach($when_roles_options as $k => $v) {
  297. if ($v === 0) {
  298. unset($when_roles_options[$k]);
  299. }
  300. }
  301. }
  302. else{
  303. $when_roles_options = 0;
  304. }
  305. // This controls whether mobile is enabled or not.
  306. $is_mobile_enabled = variable_get('splashify_when_mobile', 0);
  307. if ($is_mobile_enabled) {
  308. // Verify the Mobile Detect library is installed.
  309. $mobile_detect = libraries_load('Mobile_Detect');
  310. if (!$mobile_detect['installed'] || !file_exists(DRUPAL_ROOT . '/' . $mobile_detect['library path'] . '/Mobile_Detect.php')) {
  311. $is_mobile_enabled = 0;
  312. }
  313. }
  314. // This controls whether or not we should use the mobile settings for this
  315. // request.
  316. $do_mobile = FALSE;
  317. // If this admin setting is true, force the system to use the mobile
  318. // settings.
  319. $force_mobile = variable_get('splashify_when_mobile_test', FALSE);
  320. if (!empty($is_mobile_enabled)) {
  321. // Is this a mobile request?
  322. $detect = new Mobile_Detect();
  323. if ($detect->isMobile() || $force_mobile) {
  324. // This is a mobile request!
  325. $do_mobile = TRUE;
  326. // Get the mobile admin settings.
  327. $when_frequency = variable_get('splashify_when_mobile_frequency', 'never');
  328. $where_page = variable_get('splashify_where_mobile_page', 'home');
  329. $where_listpages = variable_get('splashify_where_mobile_listpages', '');
  330. $where_opposite = variable_get('splashify_where_mobile_opposite', FALSE);
  331. $what_mode = variable_get('splashify_what_mobile_mode', 'random');
  332. $what_content = variable_get('splashify_what_mobile_content', '');
  333. $what_content_title = variable_get('splashify_what_mobile_pagetitle', '');
  334. $how_mode = variable_get('splashify_how_mobile_mode', 'redirect');
  335. // This is no longer an option for the mobile settings.
  336. $how_size = '';
  337. }
  338. }
  339. if (!$do_mobile) {
  340. // Get the desktop admin settings.
  341. $when_frequency = variable_get('splashify_when_desktop_frequency', 'never');
  342. $where_page = variable_get('splashify_where_desktop_page', 'home');
  343. $where_listpages = variable_get('splashify_where_desktop_listpages', '');
  344. $where_opposite = variable_get('splashify_where_desktop_opposite', FALSE);
  345. $what_mode = variable_get('splashify_what_desktop_mode', 'random');
  346. $what_content = variable_get('splashify_what_desktop_content', '');
  347. $what_content_title = variable_get('splashify_what_desktop_pagetitle', '');
  348. $how_mode = variable_get('splashify_how_desktop_mode', 'redirect');
  349. $how_size = variable_get('splashify_how_desktop_size', '');
  350. }
  351. $only_anonymous = variable_get('splashify_when_anonymous', FALSE);
  352. // This is a textarea field, so make sure this value is safe.
  353. $where_listpages = check_plain($where_listpages);
  354. if (is_array($what_content)) {
  355. // We are dealing with a text_format field.
  356. $what_content_value = check_markup($what_content['value'], $what_content['format']);
  357. }
  358. elseif (isset($what_content)) {
  359. // We are dealing with a textarea field.
  360. $what_content_value = check_plain($what_content);
  361. }
  362. else {
  363. $what_content_value = '';
  364. }
  365. return array(
  366. 'only_anonymous' => $only_anonymous,
  367. 'when_frequency' => $when_frequency,
  368. 'when_mobile_test' => $force_mobile,
  369. 'when_roles' => $when_roles,
  370. 'when_roles_options' => $when_roles_options,
  371. 'where_page' => $where_page,
  372. 'where_listpages' => html_entity_decode($where_listpages),
  373. 'where_opposite' => $where_opposite,
  374. 'what_mode' => $what_mode,
  375. 'what_content' => $what_content,
  376. 'what_content_value' => $what_content_value,
  377. 'what_content_title' => $what_content_title,
  378. 'how_mode' => $how_mode,
  379. 'how_size' => $how_size,
  380. );
  381. }