start.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /**
  3. * Provides support for URLs no longer used in Elgg for those who bookmarked or
  4. * linked to them
  5. */
  6. elgg_register_event_handler('init', 'system', 'legacy_urls_init');
  7. /**
  8. * Initialize the plugin
  9. * @return void
  10. */
  11. function legacy_urls_init() {
  12. elgg_register_page_handler('tag', 'legacy_urls_tag_handler');
  13. elgg_register_page_handler('pg', 'legacy_urls_pg_handler');
  14. elgg_register_plugin_hook_handler('route', 'blog', 'legacy_urls_blog_forward');
  15. elgg_register_plugin_hook_handler('route', 'bookmarks', 'legacy_urls_bookmarks_forward');
  16. elgg_register_plugin_hook_handler('route', 'file', 'legacy_urls_file_forward');
  17. elgg_register_plugin_hook_handler('route', 'groups', 'legacy_urls_groups_forward');
  18. elgg_register_plugin_hook_handler('route', 'settings', 'legacy_urls_settings_forward');
  19. elgg_register_page_handler('forum', 'legacy_urls_forum_handler');
  20. elgg_register_plugin_hook_handler('route', 'messageboard', 'legacy_urls_messageboard_forward');
  21. }
  22. /**
  23. * Redirect the requestor to the new URL
  24. * Checks the plugin setting to determine the course of action:
  25. * a) Displays an error page with the new URL
  26. * b) Forwards to the new URL and displays an error message
  27. * c) Silently forwards to the new URL
  28. *
  29. * @param string $url Relative or absolute URL
  30. * @return mixed
  31. */
  32. function legacy_urls_redirect($url) {
  33. $method = elgg_get_plugin_setting('redirect_method', 'legacy_urls');
  34. // we only show landing page or queue warning if html generating page
  35. $viewtype = elgg_get_viewtype();
  36. if ($viewtype != 'default' && !elgg_does_viewtype_fallback($viewtype)) {
  37. $method = 'immediate';
  38. }
  39. switch ($method) {
  40. case 'landing':
  41. $content = elgg_view('legacy_urls/message', array('url' => $url));
  42. $body = elgg_view_layout('error', array('content' => $content));
  43. echo elgg_view_page('', $body, 'error');
  44. return true;
  45. break;
  46. case 'immediate_error':
  47. // drop through after setting error message
  48. register_error(elgg_echo('changebookmark'));
  49. case 'immediate':
  50. default:
  51. $url = elgg_normalize_url($url);
  52. header("HTTP/1.1 301 Moved Permanently");
  53. header("Location: $url");
  54. exit;
  55. break;
  56. }
  57. }
  58. /**
  59. * Adds query parameters to URL for redirect
  60. *
  61. * @param string $url The URL
  62. * @param array $query_vars Additional query parameters in associate array
  63. * @return string
  64. */
  65. function legacy_urls_prepare_url($url, array $query_vars = array()) {
  66. $params = array();
  67. // Elgg munges the request in htaccess rules so cannot use $_GET
  68. $query = parse_url(_elgg_services()->request->server->get('REQUEST_URI'), PHP_URL_QUERY);
  69. if ($query) {
  70. parse_str($query, $params);
  71. }
  72. $params = array_merge($params, $query_vars);
  73. if ($params) {
  74. if (!empty($params['__elgg_uri'])) {
  75. // on multiple redirects, __elgg_uri is appended to the URL causing infinite loops #8494
  76. unset($params['__elgg_uri']);
  77. }
  78. return elgg_http_add_url_query_elements($url, $params);
  79. } else {
  80. return $url;
  81. }
  82. }
  83. /**
  84. * Handle requests for /tag/<tag string>
  85. *
  86. * @param array $segments URL segments
  87. * @return mixed
  88. */
  89. function legacy_urls_tag_handler($segments) {
  90. $tag = $segments[0];
  91. $url = legacy_urls_prepare_url('search', array('q' => $tag));
  92. return legacy_urls_redirect($url);
  93. }
  94. /**
  95. * Handle requests for URLs that start with /pg/
  96. *
  97. * @param array $segments URL segments
  98. * @return mixed
  99. */
  100. function legacy_urls_pg_handler($segments) {
  101. $url = implode('/', $segments);
  102. return legacy_urls_redirect(legacy_urls_prepare_url($url));
  103. }
  104. /**
  105. * Blog forwarder
  106. *
  107. * 1.0-1.7.5
  108. * Group blogs page: /blog/group:<container_guid>/
  109. * Group blog view: /blog/group:<container_guid>/read/<guid>/<title>
  110. * 1.7.5-pre 1.8
  111. * Group blogs page: /blog/owner/group:<container_guid>/
  112. * Group blog view: /blog/read/<guid>
  113. *
  114. * @param $hook string "route"
  115. * @param $type string "blog"
  116. * @param $result mixed Old identifier and segments
  117. * @return mixed
  118. */
  119. function legacy_urls_blog_forward($hook, $type, $result) {
  120. $page = $result['segments'];
  121. // easier to work with and no notices
  122. $page = array_pad($page, 4, "");
  123. // group usernames
  124. if (preg_match('~/group\:([0-9]+)/~', "/{$page[0]}/{$page[1]}/", $matches)) {
  125. $guid = $matches[1];
  126. $entity = get_entity($guid);
  127. if (elgg_instanceof($entity, 'group')) {
  128. if (!empty($page[2])) {
  129. $url = "blog/view/$page[2]/";
  130. } else {
  131. $url = "blog/group/$guid/all";
  132. }
  133. // we drop query params because the old group URLs were invalid
  134. legacy_urls_redirect(legacy_urls_prepare_url($url));
  135. return false;
  136. }
  137. }
  138. if (empty($page[0])) {
  139. return;
  140. }
  141. if ($page[0] == "read") {
  142. $url = "blog/view/{$page[1]}/";
  143. legacy_urls_redirect(legacy_urls_prepare_url($url));
  144. return false;
  145. }
  146. // user usernames
  147. $user = get_user_by_username($page[0]);
  148. if (!$user) {
  149. return;
  150. }
  151. if (empty($page[1])) {
  152. $page[1] = 'owner';
  153. }
  154. switch ($page[1]) {
  155. case "read":
  156. $url = "blog/view/{$page[2]}/{$page[3]}";
  157. break;
  158. case "archive":
  159. $url = "blog/archive/{$page[0]}/{$page[2]}/{$page[3]}";
  160. break;
  161. case "friends":
  162. $url = "blog/friends/{$page[0]}";
  163. break;
  164. case "new":
  165. $url = "blog/add/$user->guid";
  166. break;
  167. case "owner":
  168. $url = "blog/owner/{$page[0]}";
  169. break;
  170. }
  171. if (isset($url)) {
  172. legacy_urls_redirect(legacy_urls_prepare_url($url));
  173. return false;
  174. }
  175. }
  176. /**
  177. * Bookmarks forwarder
  178. * /bookmarks/group:<group_guid>/
  179. * /bookmarks/gorup:<group_guid>/read/<guid>/
  180. * /bookmarks/read/<guid>
  181. * /bookmarks/<username>[/(items|read|inbox|friends|add|bookmarklet)/<guid>]
  182. *
  183. * @param $hook string "route"
  184. * @param $type string "bookmarks"
  185. * @param $result mixed Old identifier and segments
  186. * @return mixed
  187. */
  188. function legacy_urls_bookmarks_forward($hook, $type, $result) {
  189. $page = $result['segments'];
  190. // easier to work with and no notices
  191. $page = array_pad($page, 4, "");
  192. // old group usernames
  193. if (substr_count($page[0], 'group:')) {
  194. preg_match('/group\:([0-9]+)/i', $page[0], $matches);
  195. $guid = $matches[1];
  196. $entity = get_entity($guid);
  197. if (elgg_instanceof($entity, 'group')) {
  198. if (!empty($page[2])) {
  199. $url = "bookmarks/view/$page[2]/";
  200. } else {
  201. $url = "bookmarks/group/$guid/all";
  202. }
  203. // we drop query params because the old group URLs were invalid
  204. legacy_urls_redirect(legacy_urls_prepare_url($url));
  205. }
  206. }
  207. if ($page[0] == "read") {
  208. $url = "bookmarks/view/{$page[1]}/";
  209. legacy_urls_redirect(legacy_urls_prepare_url($url));
  210. return false;
  211. }
  212. $user = get_user_by_username($page[0]);
  213. if (!$user) {
  214. return;
  215. }
  216. if (empty($page[1])) {
  217. $page[1] = 'items';
  218. }
  219. switch ($page[1]) {
  220. case "read":
  221. $url = "bookmarks/view/{$page[2]}/{$page[3]}";
  222. break;
  223. case "inbox":
  224. $url = "bookmarks/inbox/{$page[0]}";
  225. break;
  226. case "friends":
  227. $url = "bookmarks/friends/{$page[0]}";
  228. break;
  229. case "add":
  230. $url = "bookmarks/add/{$page[0]}";
  231. break;
  232. case "items":
  233. $url = "bookmarks/owner/{$page[0]}";
  234. break;
  235. case "bookmarklet":
  236. $url = "bookmarks/bookmarklet/{$page[0]}";
  237. break;
  238. }
  239. if (isset($url)) {
  240. legacy_urls_redirect(legacy_urls_prepare_url($url));
  241. return false;
  242. }
  243. }
  244. /**
  245. * File forwarder
  246. * /file/read/<guid>
  247. *
  248. * @param $hook string "route"
  249. * @param $type string "file"
  250. * @param $result mixed Old identifier and segments
  251. * @return mixed
  252. */
  253. function legacy_urls_file_forward($hook, $type, $result) {
  254. $page = $result['segments'];
  255. // easier to work with and no notices
  256. $page = array_pad($page, 4, "");
  257. if ($page[0] == 'read') {
  258. $url = "file/view/{$page[1]}";
  259. legacy_urls_redirect(legacy_urls_prepare_url($url));
  260. return false;
  261. }
  262. }
  263. /**
  264. * Groups forwarder
  265. * /groups/<guid>
  266. * /groups/forum/<guid>
  267. *
  268. * @param $hook string "route"
  269. * @param $type string "groups"
  270. * @param $result mixed Old identifier and segments
  271. * @return mixed
  272. */
  273. function legacy_urls_groups_forward($hook, $type, $result) {
  274. $page = $result['segments'];
  275. // easier to work with and no notices
  276. $page = array_pad($page, 4, "");
  277. if (is_numeric($page[0])) {
  278. $group = get_entity($page[0]);
  279. if (elgg_instanceof($group, 'group', '', 'ElggGroup')) {
  280. legacy_urls_redirect(legacy_urls_prepare_url($group->getURL()));
  281. return false;
  282. }
  283. }
  284. if ($page[0] == 'forum') {
  285. $url = "discussion/owner/{$page[1]}";
  286. legacy_urls_redirect(legacy_urls_prepare_url($url));
  287. return false;
  288. }
  289. }
  290. /**
  291. * User settings forwarder
  292. * /settings/plugins/
  293. *
  294. * @param $hook string "route"
  295. * @param $type string "settings"
  296. * @param $result mixed Old identifier and segments
  297. * @return mixed
  298. */
  299. function legacy_urls_settings_forward($hook, $type, $result) {
  300. $page = $result['segments'];
  301. // easier to work with and no notices
  302. $page = array_pad($page, 4, "");
  303. if ($page[0] == "plugins") {
  304. if (empty($page[2])) {
  305. $url = "settings";
  306. if (!empty($page[1])) {
  307. $url .= "/user/" . $page[1];
  308. }
  309. legacy_urls_redirect(legacy_urls_prepare_url($url));
  310. return false;
  311. }
  312. }
  313. }
  314. /**
  315. * Group forum forwarder
  316. * /forum/.*
  317. *
  318. * @param array $page URL segments
  319. * @return mixed
  320. */
  321. function legacy_urls_forum_handler($page) {
  322. switch ($page[0]) {
  323. case 'topic':
  324. $url = "discussion/view/{$page[1]}/{$page[2]}";
  325. legacy_urls_redirect(legacy_urls_prepare_url($url));
  326. return true;
  327. default:
  328. return false;
  329. }
  330. }
  331. /**
  332. * Messageboard forwarder
  333. * /messageboard/!(owner|add|group)
  334. *
  335. * @param $hook string "route"
  336. * @param $type string "messageboard"
  337. * @param $result mixed Old identifier and segments
  338. * @return mixed
  339. */
  340. function legacy_urls_messageboard_forward($hook, $type, $result) {
  341. $page = $result['segments'];
  342. // easier to work with and no notices
  343. $page = array_pad($page, 4, "");
  344. // if the first part is a username, forward to new format
  345. $new_section_one = array('owner', 'add', 'group');
  346. if (isset($page[0]) && !in_array($page[0], $new_section_one) && get_user_by_username($page[0])) {
  347. $url = "messageboard/owner/{$page[0]}";
  348. legacy_urls_redirect(legacy_urls_prepare_url($url));
  349. return false;
  350. }
  351. }