[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/ -> access.php (source)

   1  <?php
   2  //
   3  // SOFTWARE NAME: eZ publish
   4  // SOFTWARE RELEASE: 3.9.0
   5  // BUILD VERSION: 17785
   6  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
   7  // SOFTWARE LICENSE: GNU General Public License v2.0
   8  // NOTICE: >
   9  //   This program is free software; you can redistribute it and/or
  10  //   modify it under the terms of version 2.0  of the GNU General
  11  //   Public License as published by the Free Software Foundation.
  12  //
  13  //   This program is distributed in the hope that it will be useful,
  14  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16  //   GNU General Public License for more details.
  17  //
  18  //   You should have received a copy of version 2.0 of the GNU General
  19  //   Public License along with this program; if not, write to the Free
  20  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21  //   MA 02110-1301, USA.
  22  //
  23  //
  24  
  25  define( 'EZ_ACCESS_TYPE_DEFAULT', 1 );
  26  define( 'EZ_ACCESS_TYPE_URI', 2 );
  27  define( 'EZ_ACCESS_TYPE_PORT', 3 );
  28  define( 'EZ_ACCESS_TYPE_HTTP_HOST', 4 );
  29  define( 'EZ_ACCESS_TYPE_INDEX_FILE', 5 );
  30  define( 'EZ_ACCESS_TYPE_STATIC', 6 );
  31  define( 'EZ_ACCESS_TYPE_SERVER_VAR', 7 );
  32  define( 'EZ_ACCESS_TYPE_URL', 8 );
  33  
  34  define( 'EZ_ACCESS_SUBTYPE_PRE', 1 );
  35  define( 'EZ_ACCESS_SUBTYPE_POST', 2 );
  36  
  37  /*!
  38   Goes trough the access matching rules and returns the access match.
  39   The returned match is an associative array with \a name and \c type.
  40  */
  41  function accessType( &$uri, $host, $port, $file )
  42  {
  43      $ini =& eZINI::instance();
  44      if ( $ini->hasVariable( 'SiteAccessSettings', 'StaticMatch' ) )
  45      {
  46          $match = $ini->variable( 'SiteAccessSettings', 'StaticMatch' );
  47          if ( $match != '' )
  48          {
  49              $access = array( 'name' => $match,
  50                               'type' => EZ_ACCESS_TYPE_STATIC );
  51              return $access;
  52          }
  53      }
  54  
  55      list( $siteAccessList, $order ) =
  56          $ini->variableMulti( 'SiteAccessSettings', array( 'AvailableSiteAccessList', 'MatchOrder' ) );
  57      $access = array( 'name' => $ini->variable( 'SiteSettings', 'DefaultAccess' ),
  58                       'type' => EZ_ACCESS_TYPE_DEFAULT );
  59  
  60  
  61      if ( $order == 'none' )
  62          return $access;
  63  
  64      $order = $ini->variableArray( 'SiteAccessSettings', 'MatchOrder' );
  65  
  66      foreach ( $order as $matchprobe )
  67      {
  68          $name = '';
  69          $type = '';
  70          $match_type = '';
  71  
  72          switch( $matchprobe )
  73          {
  74              case 'servervar':
  75              {
  76                  include_once ( 'lib/ezutils/classes/ezsys.php' );
  77                  if ( $serversiteaccess = eZSys::serverVariable( $ini->variable( 'SiteAccessSettings', 'ServerVariableName' ), true ) )
  78                  {
  79                      $access['name'] = $serversiteaccess;
  80                      $access['type'] = EZ_ACCESS_TYPE_SERVER_VAR;
  81                      return $access;
  82                  }
  83                  else
  84                      continue;
  85              } break;
  86              case 'port':
  87              {
  88                  if ( $ini->hasVariable( 'PortAccessSettings', $port ) )
  89                  {
  90                      $access['name'] = $ini->variable( 'PortAccessSettings', $port );
  91                      $access['type'] = EZ_ACCESS_TYPE_PORT;
  92                      return $access;
  93                  }
  94                  else
  95                      continue;
  96              } break;
  97              case 'uri':
  98              {
  99                  $type = EZ_ACCESS_TYPE_URI;
 100                  $match_type = $ini->variable( 'SiteAccessSettings', 'URIMatchType' );
 101  
 102                  if ( $match_type == 'map' )
 103                  {
 104                      if ( $ini->hasVariable( 'SiteAccessSettings', 'URIMatchMapItems' ) )
 105                      {
 106                          $match_item = $uri->element( 0 );
 107                          $matchMapItems = $ini->variableArray( 'SiteAccessSettings', 'URIMatchMapItems' );
 108                          foreach ( $matchMapItems as $matchMapItem )
 109                          {
 110                              $matchMapURI = $matchMapItem[0];
 111                              $matchMapAccess = $matchMapItem[1];
 112                              if ( $matchMapURI == $match_item and in_array( $matchMapAccess, $siteAccessList ) )
 113                              {
 114                                  $uri->increase( 1 );
 115                                  $uri->dropBase();
 116                                  $access['name'] = $matchMapAccess;
 117                                  $access['type'] = $type;
 118                                  return $access;
 119                              }
 120                          }
 121                      }
 122                  }
 123                  else if ( $match_type == 'element' )
 124                  {
 125                      $match_index = $ini->variable( 'SiteAccessSettings', 'URIMatchElement' );
 126                      $elements = $uri->elements( false );
 127                      $elements = array_slice( $elements, 0, $match_index );
 128                      $name = implode( '_', $elements );
 129                  }
 130                  else if ( $match_type == 'text' )
 131                  {
 132                      $match_item = $uri->elements();
 133                      $matcher_pre = $ini->variable( 'SiteAccessSettings', 'URIMatchSubtextPre' );
 134                      $matcher_post = $ini->variable( 'SiteAccessSettings', 'URIMatchSubtextPost' );
 135                  }
 136                  else if ( $match_type == 'regexp' )
 137                  {
 138                      $match_item = $uri->elements();
 139                      $matcher = $ini->variable( 'SiteAccessSettings', 'URIMatchRegexp' );
 140                      $match_num = $ini->variable( 'SiteAccessSettings', 'URIMatchRegexpItem' );
 141                  }
 142                  else
 143                      continue;
 144              } break;
 145              case 'host':
 146              {
 147                  $type = EZ_ACCESS_TYPE_HTTP_HOST;
 148                  $match_type = $ini->variable( 'SiteAccessSettings', 'HostMatchType' );
 149                  $match_item = $host;
 150                  if ( $match_type == 'map' )
 151                  {
 152                      if ( $ini->hasVariable( 'SiteAccessSettings', 'HostMatchMapItems' ) )
 153                      {
 154                          $matchMapItems = $ini->variableArray( 'SiteAccessSettings', 'HostMatchMapItems' );
 155                          foreach ( $matchMapItems as $matchMapItem )
 156                          {
 157                              $matchMapHost = $matchMapItem[0];
 158                              $matchMapAccess = $matchMapItem[1];
 159                              if ( $matchMapHost == $host )
 160                              {
 161                                  $access['name'] = $matchMapAccess;
 162                                  $access['type'] = $type;
 163                                  return $access;
 164                              }
 165                          }
 166                      }
 167                  }
 168                  else if ( $match_type == 'element' )
 169                  {
 170                      $match_index = $ini->variable( 'SiteAccessSettings', 'HostMatchElement' );
 171                      $match_arr = explode( '.', $match_item );
 172                      $name = $match_arr[$match_index];
 173                  }
 174                  else if ( $match_type == 'text' )
 175                  {
 176                      $matcher_pre = $ini->variable( 'SiteAccessSettings', 'HostMatchSubtextPre' );
 177                      $matcher_post = $ini->variable( 'SiteAccessSettings', 'HostMatchSubtextPost' );
 178                  }
 179                  else if ( $match_type == 'regexp' )
 180                  {
 181                      $matcher = $ini->variable( 'SiteAccessSettings', 'HostMatchRegexp' );
 182                      $match_num = $ini->variable( 'SiteAccessSettings', 'HostMatchRegexpItem' );
 183                  }
 184                  else
 185                      continue;
 186              } break;
 187              case 'index':
 188              {
 189                  $type = EZ_ACCESS_TYPE_INDEX_FILE;
 190                  $match_type = $ini->variable( 'SiteAccessSettings', 'IndexMatchType' );
 191                  $match_item = $file;
 192                  if ( $match_type == 'element' )
 193                  {
 194                      $match_index = $ini->variable( 'SiteAccessSettings', 'IndexMatchElement' );
 195                      $match_pos = strpos( $match_item, '.php' );
 196                      if ( $match_pos !== false )
 197                      {
 198                          $match_item = substr( $match_item, 0, $match_pos );
 199                          $match_arr = explode( '_', $match_item );
 200                          $name = $match_arr[$match_index];
 201                      }
 202                  }
 203                  else if ( $match_type == 'text' )
 204                  {
 205                      $matcher_pre = $ini->variable( 'SiteAccessSettings', 'IndexMatchSubtextPre' );
 206                      $matcher_post = $ini->variable( 'SiteAccessSettings', 'IndexMatchSubtextPost' );
 207                  }
 208                  else if ( $match_type == 'regexp' )
 209                  {
 210                      $matcher = $ini->variable( 'SiteAccessSettings', 'IndexMatchRegexp' );
 211                      $match_num = $ini->variable( 'SiteAccessSettings', 'IndexMatchRegexpItem' );
 212                  }
 213                  else
 214                      continue;
 215              } break;
 216              default:
 217              {
 218                  eZDebug::writeError( "Unknown access match: $match", "access" );
 219              } break;
 220          }
 221  
 222          if ( $match_type == 'regexp' )
 223              $name = accessMatchRegexp( $match_item, $matcher, $match_num );
 224          else if ( $match_type == 'text' )
 225              $name = accessMatchText( $match_item, $matcher_pre, $matcher_post );
 226  
 227          if ( ( isset( $name ) && $name != '' ) || $type == EZ_ACCESS_TYPE_URI )
 228          {
 229              $name = preg_replace( array( '/[^a-zA-Z0-9]+/',
 230                                           '/_+/',
 231                                           '/^_/',
 232                                           '/_$/' ),
 233                                    array( '_',
 234                                           '_',
 235                                           '',
 236                                           '' ),
 237                                    $name );
 238  
 239              if ( in_array( $name, $siteAccessList ) )
 240              {
 241                  if ( $type == EZ_ACCESS_TYPE_URI )
 242                  {
 243                      if ( $match_type == 'element' )
 244                      {
 245                          $uri->increase( $match_index );
 246                          $uri->dropBase();
 247                      }
 248                      else if ( $match_type == 'regexp' )
 249                      {
 250                          $uri->setURIString( $match_item );
 251                      }
 252                      else if ( $match_type == 'text' )
 253                      {
 254                          $uri->setURIString( $match_item );
 255                      }
 256                  }
 257                  $access['type'] = $type;
 258                  $access['name'] = $name;
 259                  return $access;
 260              }
 261              else if ( $type == EZ_ACCESS_TYPE_URI )
 262              {
 263                  $access['type'] = $type;
 264                  return $access;
 265              }
 266          }
 267      }
 268      return $access;
 269  }
 270  
 271  /*!
 272   Changes the site access to what's defined in \a $access. It will change the
 273   access path in eZSys and prepend an override dir to eZINI
 274   \param $access An associative array consisting of \c name and \c type.
 275   \return the \a $access parameter.
 276  */
 277  
 278  function changeAccess( $access )
 279  {
 280      $ini =& eZINI::instance();
 281  
 282      $name = $access['name'];
 283      if ( isset( $access['type'] ) &&
 284           $access['type'] == EZ_ACCESS_TYPE_URI )
 285      {
 286          include_once ( 'lib/ezutils/classes/ezsys.php' );
 287          eZSys::addAccessPath( $name );
 288      }
 289  
 290      if ( file_exists( "settings/siteaccess/$name" ) )
 291      {
 292          $ini->prependOverrideDir( "siteaccess/$name", false, 'siteaccess' );
 293      }
 294  
 295      /* Make sure extension siteaccesses are prepended */
 296      include_once ( 'lib/ezutils/classes/ezextension.php' );
 297      eZExtension::prependExtensionSiteAccesses( $name );
 298  
 299      $ini->loadCache();
 300  
 301      eZUpdateDebugSettings();
 302      if ( accessDebugEnabled() )
 303          eZDebug::writeDebug( "Updated settings to use siteaccess '$name'", 'access.php' );
 304  
 305      return $access;
 306  }
 307  
 308  function accessMatchRegexp( &$text, $reg, $num )
 309  {
 310      $reg = preg_replace( "#/#", "\\/", $reg );
 311      if ( preg_match( "/$reg/", $text, $regs ) and $num < count( $regs ) )
 312      {
 313          $text = str_replace( $regs[$num], '', $text );
 314          return $regs[$num];
 315      }
 316      return null;
 317  }
 318  
 319  function accessMatchText( &$text, $match_pre, $match_post )
 320  {
 321      $ret = null;
 322      if ( $match_pre != '' )
 323      {
 324          $pos = strpos( $text, $match_pre );
 325          if ( $pos === false )
 326              return null;
 327  
 328          $ret = substr( $text, $pos + strlen( $match_pre ) );
 329          $text = substr( $text, 0, $pos );
 330      }
 331      if ( $match_post != '' )
 332      {
 333          $pos = strpos( $ret, $match_post );
 334          if ( $pos === false )
 335              return null;
 336  
 337          $text .= substr( $ret, $pos + 1 );
 338          $ret = substr( $ret, 0, $pos );
 339      }
 340      return $ret;
 341  }
 342  
 343  function accessAllowed( $uri )
 344  {
 345      $moduleName = $uri->element();
 346      $viewName = $uri->element( 1 );
 347      $check = array( 'result' => true,
 348                      'module' => $moduleName,
 349                      'view' => $viewName,
 350                      'view-checked' => false );
 351  
 352      $ini =& eZINI::instance();
 353  
 354      $access = true;
 355      $currentAccess = true;
 356      if ( !$ini->hasGroup( 'SiteAccessRules' ) )
 357          return $check;
 358      $items =& $ini->variableArray( 'SiteAccessRules', 'Rules' );
 359      foreach( $items as $item )
 360      {
 361          $name = strtolower( $item[0] );
 362          if ( isset ( $item[1] ) )
 363              $value = $item[1];
 364          else
 365              $value = null;
 366          switch( $name )
 367          {
 368              case 'access':
 369              {
 370                  $currentAccess = ( $value == 'enable' );
 371              } break;
 372              case 'moduleall':
 373              {
 374                  $access = $currentAccess;
 375              } break;
 376              case 'module':
 377              {
 378                  if ( preg_match( "#([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)#", $value, $matches ) )
 379                  {
 380                      if ( $matches[1] == $moduleName and
 381                           $matches[2] == $viewName )
 382                      {
 383                          $check['view_checked'] = true;
 384                          $access = $currentAccess;
 385                      }
 386                  }
 387                  else
 388                  {
 389                      if ( $value == $moduleName )
 390                      {
 391                          $access = $currentAccess;
 392                          $check['view_checked'] = false;
 393                      }
 394                  }
 395              } break;
 396              default:
 397              {
 398                  eZDebug::writeError( "Unknown access rule: $name=$value", 'Access' );
 399              } break;
 400          }
 401      }
 402  
 403      $check['result'] = $access;
 404      return $check;
 405  }
 406  
 407  function precheckAllowed( &$prechecks )
 408  {
 409      $ini =& eZINI::instance();
 410  
 411      $tmp_allow = true;
 412      if ( !$ini->hasGroup( 'SitePrecheckRules' ) )
 413          return true;
 414      $items =& $ini->variableArray( 'SitePrecheckRules', 'Rules' );
 415      foreach( $items as $item )
 416      {
 417          $name = strtolower( $item[0] );
 418          $value = $item[1];
 419          switch( $name )
 420          {
 421              case 'access':
 422              {
 423                  $tmp_allow = ($value == 'enable');
 424              } break;
 425              case 'precheckall':
 426              {
 427                  if ( $value == 'true' )
 428                  {
 429                      foreach( $prechecks as $key => $value )
 430                      {
 431                          $prechecks[$key]['allow'] = $tmp_allow;
 432                      }
 433                  }
 434              } break;
 435              case 'precheck':
 436              {
 437                  if ( isset( $prechecks[$value] ) )
 438                      $prechecks[$value]['allow'] = $tmp_allow;
 439              } break;
 440              default:
 441              {
 442                  eZDebug::writeError( "Unknown precheck rule: $name=$value", 'Access' );
 443              } break;
 444          }
 445      }
 446  }
 447  
 448  function accessDebugEnabled()
 449  {
 450      $ini =& eZINI::instance();
 451      return $ini->variable( 'SiteAccessSettings', 'DebugAccess' ) == 'enabled';
 452  }
 453  
 454  function accessExtraDebugEnabled()
 455  {
 456      $ini =& eZINI::instance();
 457      return $ini->variable( 'SiteAccessSettings', 'DebugExtraAccess' ) == 'enabled';
 458  }
 459  
 460  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7