eZ Publish  [4.0]
ezredirectmanager.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZRedirectManager class
00004 //
00005 // Created on: <24-Nov-2004 15:03:51 jb>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezredirectmanager.php
00032 */
00033 
00034 /*!
00035   \class eZRedirectManager ezredirectmanager.php
00036   \brief Handles generation of redirection URIs and redirection
00037 
00038 */
00039 
00040 class eZRedirectManager
00041 {
00042 
00043     /*!
00044      Generates a URI which can be used to redirect with, the uri is based on:
00045      - The last accessed view/non-view page if any (see \a $view parameter)
00046      - The uri is not the currently running module, if so use default
00047      - The default uri \a $default
00048 
00049      \return The new URI string or \c false if no uri could be made.
00050 
00051      \param $module The current module object
00052      \param $default The default URI to redirect to if all else fails.
00053                      If set to \c false then it will return false.
00054      \param $view If true it will try to redirect to last accessed view URI.
00055      \param $disallowed An array with urls not allowed to redirect to or \c false to allow all
00056      \param $preferredURI An URI that is preferred for the caller. If that URI is valid, it's returned.
00057 
00058      \note All URLs must start with a slash \c /
00059 
00060      \sa redirectTo()
00061     */
00062     static function redirectURI( $module, $default, $view = true, $disallowed = false, $preferredURI = false )
00063     {
00064         $uri = false;
00065         $http = eZHTTPTool::instance();
00066 
00067         if ( $preferredURI ) // check if $preferredURI is a valid URI
00068             return $preferredURI;
00069 
00070         if ( $view )
00071         {
00072             if ( $http->hasSessionVariable( "LastAccessesURI" ) )
00073             {
00074                 $uri = $http->sessionVariable( "LastAccessesURI" );
00075             }
00076         }
00077         else
00078         {
00079             if ( $http->hasSessionVariable( "LastAccessedModifyingURI" ) )
00080             {
00081                 $uri = $http->sessionVariable( "LastAccessedModifyingURI" );
00082             }
00083         }
00084 
00085         if ( $uri !== false )
00086         {
00087             $moduleURI = $module->functionURI( $module->currentView() );
00088             // Check for correct module/view
00089             if ( substr( $uri, 0, strlen( $moduleURI ) ) == $moduleURI )
00090             {
00091                 // Check parameters
00092                 $moduleURI = $module->currentRedirectionURI();
00093                 if ( $moduleURI == $uri )
00094                     $uri = false;
00095             }
00096         }
00097 
00098         // Check for disallowed urls
00099         if ( $uri !== false and
00100              is_array( $disallowed ) )
00101         {
00102             if ( in_array( $uri, $disallowed ) )
00103                 $uri = false;
00104         }
00105 
00106         if ( $uri === false )
00107         {
00108             // If no default is set we should return false.
00109             if ( $default === false )
00110                 return false;
00111             $uri = $default;
00112         }
00113 
00114         return $uri;
00115     }
00116 
00117     /*!
00118      Generates a URI which can be used to redirect with, the uri is based on:
00119      - The last accessed view/non-view page if any (see \a $view parameter)
00120      - The uri is not the currently running module, if so use default
00121      - The default uri \a $default
00122 
00123      \param $module The current module object
00124      \param $default The default URI to redirect to if all else fails.
00125                      If set to \c false then it will not redirect if there is no url found
00126                      but instead it will return false.
00127      \param $view If true it will try to redirect to last accessed view URI.
00128      \param $disallowed An array with urls not allowed to redirect to or \c false to allow all
00129      \param $preferredURI An URI that is preferred for the caller.
00130             We redirect to that URI if it's specified and is valid.
00131 
00132      \return \c true if the module was redirected or \c false if not.
00133 
00134      \note All URLs must start with a slash \c /
00135      \sa redirectURI()
00136     */
00137     static function redirectTo( $module, $default, $view = true, $disallowed = false, $preferredURI = false )
00138     {
00139         $uri = eZRedirectManager::redirectURI( $module, $default, $view, $disallowed, $preferredURI );
00140         if ( $uri === false )
00141             return false;
00142         $module->redirectTo( $uri );
00143         return true;
00144     }
00145 }
00146 
00147 ?>