/** * hook called on route, all * check if $returnvalue['handler'] to see if we need to replace it * if the handler is an original handler, we want to foward it to the new url * * @param type $hook * @param type $type * @param type $returnvalue * @param type $params * @return array */ function router($hook, $type, $returnvalue, $params) { if (elgg_get_config('pagehandler_hijack')) { return $returnvalue; } $handlers = get_replacement_handlers(); if (in_array($returnvalue['handler'], array_keys($handlers))) { // we have been given an old handler -> we should forward to the replacement // probably from an old link or something $currenturl = current_page_url(); //get everything after the pagehandler $afterhandler = str_replace(elgg_get_site_url() . $returnvalue['handler'], "", $currenturl); $newurl = elgg_get_site_url() . $handlers[$returnvalue['handler']] . $afterhandler; // forward to the new url forward($newurl); } if (in_array($returnvalue['handler'], $handlers)) { // we need to do something about it // get the original handler $original = array_search($returnvalue['handler'], $handlers); if (!empty($original)) { // reset the context for non-hijack aware code elgg_set_context($original); // let the system load content for the original handler $returnvalue['handler'] = $original; $returnvalue['identifier'] = $original; // set a flag so we don't infinite loop ourselves in route hooks elgg_set_config('pagehandler_hijack', true); return elgg_trigger_plugin_hook('route', $original, null, $returnvalue); } } }
function handler_replace($content) { $handlers = get_replacement_handlers(); foreach ($handlers as $original => $replacement) { $search = elgg_get_site_url() . $original . "/"; $replace = elgg_get_site_url() . $replacement . "/"; $content = str_ireplace($search, $replace, $content); } return $content; }
/** * setup our init code */ function init() { elgg_register_library(PLUGIN_ID . ':upgrades', __DIR__ . '/lib/upgrades.php'); // add in our own css $url = elgg_get_simplecache_url('css', 'pagehandler_hijack'); elgg_register_css('pagehandler_hijack', $url); //register action to save our plugin settings elgg_register_action("pagehandler_hijack/settings/save", __DIR__ . "/actions/settings.php", 'admin'); //register plugin hooks // catch-all needed to catch our replacements elgg_register_plugin_hook_handler('route', 'all', __NAMESPACE__ . '\\router', 0); elgg_register_plugin_hook_handler('view', 'all', __NAMESPACE__ . '\\linkfix'); elgg_register_plugin_hook_handler('entity:url', 'all', __NAMESPACE__ . '\\entity_url', 999); elgg_register_plugin_hook_handler('prepare', 'all', __NAMESPACE__ . '\\prepare_notification', 999); elgg_register_plugin_hook_handler('email', 'system', __NAMESPACE__ . '\\prepare_email', 999); $handlers = array_keys(get_replacement_handlers()); foreach ($handlers as $h) { // specific handlers needed to catch route by priority since 'all' happens last regardless elgg_register_plugin_hook_handler('route', $h, __NAMESPACE__ . '\\router', 0); } elgg_register_event_handler('upgrade', 'system', __NAMESPACE__ . '\\upgrades'); }
<?php namespace MBeckett\pagehandler_hijack; elgg_load_css('pagehandler_hijack'); $title = elgg_echo('pagehandler_hijack:settings'); //get current pagehandlers // private API, but no choice, was out-voted $handlers = array_filter(array_keys(_elgg_services()->router->getPageHandlers())); sort($handlers); echo "<div style=\"margin: 15px;\">"; echo "<h1>" . elgg_echo('pagehandler_hijack:settings') . "</h1><br><br>"; echo elgg_echo('pagehandler_hijack:disclaimer') . "<br><br>"; // get an array of our hijacks $hijacks = get_replacement_handlers(); echo "<div class=\"pagehandler_hijack_form_element_wrapper\">"; echo "<table><tr>"; echo "<td class=\"pagehandler_hijack_thead\">" . elgg_echo('pagehandler_hijack:default') . "</td>"; echo "<td class=\"pagehandler_hijack_thead\">" . elgg_echo('pagehandler_hijack:replacement') . "</td>"; echo "</tr>"; $count = 0; foreach ($handlers as $handler) { $count++; if ($count % 2) { $zebra = "phh-odd"; } else { $zebra = "phh-even"; } echo "<tr>"; echo "<td class=\"pagehandler_hijack_element_default {$zebra}\">{$handler}</td>"; echo "<td class=\"pagehandler_hijack_element_replacement {$zebra}\">";