PageRenderTime 3374ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/plugins/google_seo/404.php

https://gitlab.com/iulian/immigration-australia-forum
PHP | 251 lines | 144 code | 45 blank | 62 comment | 30 complexity | 7aed9d8e76f313d79cfece59e548f327 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of Google SEO plugin for MyBB.
  4. * Copyright (C) 2008-2011 Andreas Klauer <Andreas.Klauer@metamorpher.de>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. // Disallow direct access to this file for security reasons
  21. if(!defined("IN_MYBB"))
  22. {
  23. die("Direct initialization of this file is not allowed.<br /><br />
  24. Please make sure IN_MYBB is defined.");
  25. }
  26. /**
  27. * Hooks are called too late, so this is done before hooks:
  28. *
  29. * Define NO_ONLINE if it looks like we're going to show an 404 error,
  30. * unless the user explicitely wants these errors to show in the WOL.
  31. */
  32. global $mybb;
  33. if(THIS_SCRIPT == "misc.php"
  34. && $mybb->input['google_seo_error'] == "404")
  35. {
  36. if($mybb->settings['google_seo_404_wol_show'])
  37. {
  38. // Set the 404 error location
  39. $location = 'misc.php?google_seo_error=404';
  40. if($mybb->settings['google_seo_404_wol_show'] == 2)
  41. {
  42. // Include the URI for debugging purposes
  43. $location .= '&amp;uri='.urlencode($_SERVER['REQUEST_URI']);
  44. }
  45. $location = substr($location, 0, 150);
  46. global $google_seo_location;
  47. $google_seo_location = $location;
  48. }
  49. else
  50. {
  51. @define("NO_ONLINE", 1);
  52. }
  53. }
  54. /* --- Hooks: --- */
  55. // Set HTTP 404 status code, add widget to error pages:
  56. $plugins->add_hook("error", "google_seo_404");
  57. $plugins->add_hook("no_permission", "google_seo_404_no_permission");
  58. // Custom 404 error page:
  59. $plugins->add_hook("misc_start", "google_seo_404_page");
  60. // WOL extension for custom page:
  61. $plugins->add_hook("build_friendly_wol_location_end", "google_seo_404_wol");
  62. /* --- 404 helpers: --- */
  63. /**
  64. * Sort function: longest strings first, patterns last
  65. */
  66. function google_seo_404_status_cmp($a, $b)
  67. {
  68. return strlen($b) - strlen($a);
  69. }
  70. /**
  71. * Parse the 404 status setting
  72. */
  73. function google_seo_404_status($label)
  74. {
  75. global $settings;
  76. $patterns = array('*' => '404 Not Found');
  77. if(is_string($settings['google_seo_404_status']))
  78. {
  79. $lines = explode("\n", $settings['google_seo_404_status']);
  80. foreach($lines as $line)
  81. {
  82. $fields = explode("=", $line);
  83. if(count($fields) == 2)
  84. {
  85. $status = trim($fields[0]);
  86. $values = explode(",", $fields[1]);
  87. if(strpos($status, (int)$status.' ') === 0)
  88. {
  89. foreach($values as $value)
  90. {
  91. $value = trim($value);
  92. if($value === $label)
  93. {
  94. return $status;
  95. }
  96. else if(strpos($value, '*') !== false)
  97. {
  98. $patterns[$value] = $status;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. // Sort patterns by length.
  106. uksort($patterns, 'google_seo_404_status_cmp');
  107. // Find the first matching pattern
  108. foreach($patterns as $pattern => $status)
  109. {
  110. $pattern = preg_quote($pattern, '#');
  111. $pattern = str_replace('\\*', '.*', $pattern);
  112. if(preg_match("#^{$pattern}\$#", $label))
  113. {
  114. return $status;
  115. }
  116. }
  117. }
  118. /* --- 404 error handling: --- */
  119. /**
  120. * Set the 404 header, add Google 404 widget, if enabled.
  121. *
  122. * @param string Error message, which may be modified.
  123. */
  124. function google_seo_404(&$error)
  125. {
  126. global $mybb, $lang, $settings;
  127. global $google_seo_404_label;
  128. if($mybb->input['ajax'])
  129. {
  130. // don't mess with ajax
  131. return;
  132. }
  133. // Reverse language lookup hack. Might fail for dynamic {1} elements.
  134. // Thanks to Cayo / HTTP Status plugin for this idea.
  135. $keys = array_keys((array)$lang, $error);
  136. if(count($keys) > 0)
  137. {
  138. sort($keys); // try to be deterministic in case of dupes
  139. $label = $keys[0];
  140. // Most labels start with error_, shorten it.
  141. if(strpos($label, "error_") === 0)
  142. {
  143. $label = substr($label, 6);
  144. }
  145. }
  146. else if($google_seo_404_label)
  147. {
  148. $label = $google_seo_404_label;
  149. }
  150. else
  151. {
  152. $label = 'NULL';
  153. }
  154. if($label && $settings['google_seo_404_debug'])
  155. {
  156. $label = htmlspecialchars($label);
  157. $error .= "<p>(Error label: '<b>{$label}</b>')</p>";
  158. }
  159. $status = google_seo_404_status($label);
  160. if($status)
  161. {
  162. @header("HTTP/1.1 {$status}");
  163. if($status[0] == '4' && $settings['google_seo_404_widget'])
  164. {
  165. $error .= $lang->sprintf($lang->googleseo_404_widget,
  166. $settings['bburl']);
  167. }
  168. }
  169. }
  170. function google_seo_404_no_permission()
  171. {
  172. global $google_seo_404_label;
  173. $google_seo_404_label = "no_permission";
  174. }
  175. /* --- Custom 404 error page --- */
  176. /**
  177. * Create a custom 404 error page for errors that occur outside of MyBB.
  178. *
  179. */
  180. function google_seo_404_page()
  181. {
  182. global $mybb, $lang;
  183. if($mybb->input['google_seo_error'] == 404)
  184. {
  185. error($lang->googleseo_404_notfound);
  186. }
  187. }
  188. /* --- WOL --- */
  189. /**
  190. * Extend WOL for users on the 404 page.
  191. *
  192. */
  193. function google_seo_404_wol(&$p)
  194. {
  195. global $lang, $user, $settings;
  196. // Check if this user is on a 404 page.
  197. if(strpos($p['user_activity']['location'], "misc.php?google_seo_error=404") === 0)
  198. {
  199. $p['user_activity']['activity'] = 'google_seo_404';
  200. $location = $p['user_activity']['location'];
  201. $p['location_name'] = $lang->sprintf($lang->googleseo_404_wol,
  202. $location);
  203. }
  204. }
  205. /* --- End of file. --- */
  206. ?>