Exemple #1
0
/**
 * Redirect the requestor to the new URL
 * Checks the plugin setting to determine the course of action:
 * a) Displays an error page with the new URL
 * b) Forwards to the new URL and displays an error message
 * c) Silently forwards to the new URL
 *
 * @param string $url Relative or absolute URL
 * @return mixed
 */
function legacy_urls_redirect($url)
{
    $method = elgg_get_plugin_setting('redirect_method', 'legacy_urls');
    // we only show landing page or queue warning if html generating page
    $viewtype = elgg_get_viewtype();
    if ($viewtype != 'default' && !elgg_does_viewtype_fallback($viewtype)) {
        $method = 'immediate';
    }
    switch ($method) {
        case 'landing':
            $content = elgg_view('legacy_urls/message', array('url' => $url));
            $body = elgg_view_layout('error', array('content' => $content));
            echo elgg_view_page('', $body, 'error');
            return true;
            break;
        case 'immediate_error':
            // drop through after setting error message
            register_error(elgg_echo('changebookmark'));
        case 'immediate':
        default:
            $url = elgg_normalize_url($url);
            header("HTTP/1.1 301 Moved Permanently");
            header("Location: {$url}");
            exit;
            break;
    }
}
Exemple #2
0
/**
 * Return a parsed view.
 *
 * Views are rendered by a template handler and returned as strings.
 *
 * Views are called with a special $vars variable set,
 * which includes any variables passed as the second parameter.
 * For backward compatbility, the following variables are also set but we
 * recommend that you do not use them:
 *  - $vars['config'] The $CONFIG global. (Use {@link elgg_get_config()} instead).
 *  - $vars['url'] The site URL. (use {@link elgg_get_site_url()} instead).
 *  - $vars['user'] The logged in user. (use {@link elgg_get_logged_in_user_entity()} instead).
 *
 * Custom template handlers can be set with {@link set_template_handler()}.
 *
 * The output of views can be intercepted by registering for the
 * view, $view_name plugin hook.
 *
 * @warning Any variables in $_SESSION will override passed vars
 * upon name collision.  See {@trac #2124}.
 *
 * @param string  $view     The name and location of the view to use
 * @param array   $vars     Variables to pass to the view.
 * @param boolean $bypass   If set to true, elgg_view will bypass any specified
 *                          alternative template handler; by default, it will
 *                          hand off to this if requested (see set_template_handler)
 * @param boolean $debug    If set to true, the viewer will complain if it can't find a view
 * @param string  $viewtype If set, forces the viewtype for the elgg_view call to be
 *                          this value (default: standard detection)
 *
 * @return string The parsed view
 * @see set_template_handler()
 * @example views/elgg_view.php
 * @link http://docs.elgg.org/View
 * @todo $debug isn't used.
 * @todo $usercache is redundant.
 */
function elgg_view($view, $vars = array(), $bypass = false, $debug = false, $viewtype = '')
{
    global $CONFIG;
    static $usercache;
    $view = (string) $view;
    // basic checking for bad paths
    if (strpos($view, '..') !== false) {
        return false;
    }
    $view_orig = $view;
    // Trigger the pagesetup event
    if (!isset($CONFIG->pagesetupdone)) {
        elgg_trigger_event('pagesetup', 'system');
        $CONFIG->pagesetupdone = true;
    }
    if (!is_array($usercache)) {
        $usercache = array();
    }
    if (!is_array($vars)) {
        elgg_log("Vars in views must be an array: {$view}", 'ERROR');
        $vars = array();
    }
    if (empty($vars)) {
        $vars = array();
    }
    // @warning - plugin authors: do not expect user, config, and url to be
    // set by elgg_view() in the future. Instead, use elgg_get_logged_in_user_entity(),
    // elgg_get_config(), and elgg_get_site_url() in your views.
    if (!isset($vars['user'])) {
        $vars['user'] = elgg_get_logged_in_user_entity();
    }
    if (!isset($vars['config'])) {
        $vars['config'] = $CONFIG;
    }
    if (!isset($vars['url'])) {
        $vars['url'] = elgg_get_site_url();
    }
    // full_view is the new preferred key for full view on entities @see elgg_view_entity()
    if (isset($vars['full'])) {
        elgg_deprecated_notice("Use \$vars['full_view'] instead of \$vars['full']", 1.8);
        $vars['full_view'] = $vars['full'];
    }
    if (isset($vars['full_view'])) {
        $vars['full'] = $vars['full_view'];
    }
    // internalname => name (1.8)
    if (isset($vars['internalname'])) {
        elgg_deprecated_notice('You should pass $vars[\'name\'] now instead of $vars[\'internalname\']', 1.8);
        $vars['name'] = $vars['internalname'];
    } elseif (isset($vars['name'])) {
        $vars['internalname'] = $vars['name'];
    }
    // internalid => id (1.8)
    if (isset($vars['internalid'])) {
        elgg_deprecated_notice('You should pass $vars[\'id\'] now instead of $vars[\'internalid\']', 1.8);
        $vars['id'] = $vars['internalid'];
    } elseif (isset($vars['id'])) {
        $vars['internalid'] = $vars['id'];
    }
    // If it's been requested, pass off to a template handler instead
    if ($bypass == false && isset($CONFIG->template_handler) && !empty($CONFIG->template_handler)) {
        $template_handler = $CONFIG->template_handler;
        if (is_callable($template_handler)) {
            return call_user_func($template_handler, $view, $vars);
        }
    }
    // Get the current viewtype
    if (empty($viewtype)) {
        $viewtype = elgg_get_viewtype();
    }
    // Viewtypes can only be alphanumeric
    if (preg_match('[\\W]', $viewtype)) {
        return '';
    }
    // Set up any extensions to the requested view
    if (isset($CONFIG->views->extensions[$view])) {
        $viewlist = $CONFIG->views->extensions[$view];
    } else {
        $viewlist = array(500 => $view);
    }
    // Start the output buffer, find the requested view file, and execute it
    ob_start();
    foreach ($viewlist as $priority => $view) {
        $view_location = elgg_get_view_location($view, $viewtype);
        $view_file = "{$view_location}{$viewtype}/{$view}.php";
        $default_location = elgg_get_view_location($view, 'default');
        $default_view_file = "{$default_location}default/{$view}.php";
        // try to include view
        if (!file_exists($view_file) || !(include $view_file)) {
            // requested view does not exist
            $error = "{$viewtype}/{$view} view does not exist.";
            // attempt to load default view
            if ($viewtype != 'default' && elgg_does_viewtype_fallback($viewtype)) {
                if (file_exists($default_view_file) && (include $default_view_file)) {
                    // default view found
                    $error .= " Using default/{$view} instead.";
                } else {
                    // no view found at all
                    $error = "Neither {$viewtype}/{$view} nor default/{$view} view exists.";
                }
            }
            // log warning
            elgg_log($error, 'NOTICE');
        }
    }
    // Save the output buffer into the $content variable
    $content = ob_get_clean();
    // Plugin hook
    $params = array('view' => $view_orig, 'vars' => $vars, 'viewtype' => $viewtype);
    $content = elgg_trigger_plugin_hook('view', $view_orig, $params, $content);
    // backward compatibility with less granular hook will be gone in 2.0
    $content_tmp = elgg_trigger_plugin_hook('display', 'view', $params, $content);
    if ($content_tmp != $content) {
        $content = $content_tmp;
        elgg_deprecated_notice('The display:view plugin hook is deprecated by view:view_name', 1.8);
    }
    return $content;
}