Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   1  <?php
   2  /**
   3   * Copyright 2011-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @category  Horde
   9   * @copyright 2011-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * ACL rights for a mailbox (see RFC 2086/4314).
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2011-2017 Horde LLC
  20   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  21   * @package   Imap_Client
  22   */
  23  class Horde_Imap_Client_Data_Acl extends Horde_Imap_Client_Data_AclCommon implements ArrayAccess, IteratorAggregate, Serializable
  24  {
  25      /**
  26       * ACL rights.
  27       *
  28       * @var array
  29       */
  30      protected $_rights;
  31  
  32      /**
  33       * Constructor.
  34       *
  35       * @param string $rights  The rights (see RFC 4314 [2.1]).
  36       */
  37      public function __construct($rights = '')
  38      {
  39          $this->_rights = str_split($rights);
  40          $this->_normalize();
  41      }
  42  
  43      /**
  44       * String representation of the ACL.
  45       *
  46       * @return string  String representation (RFC 4314 compliant).
  47       */
  48      public function __toString()
  49      {
  50          return implode('', $this->_rights);
  51      }
  52  
  53      /**
  54       * Computes the difference to another rights string.
  55       * Virtual rights are ignored.
  56       *
  57       * @param string $rights  The rights to compute against.
  58       *
  59       * @return array  Two element array: added and removed.
  60       */
  61      public function diff($rights)
  62      {
  63          $rlist = array_diff(str_split($rights), array_keys($this->_virtual));
  64  
  65          return array(
  66              'added' => implode('', array_diff($rlist, $this->_rights)),
  67              'removed' => implode('', array_diff($this->_rights, $rlist))
  68          );
  69      }
  70  
  71      /**
  72       * Normalize virtual rights (see RFC 4314 [2.1.1]).
  73       */
  74      protected function _normalize()
  75      {
  76          /* Clients conforming to RFC 4314 MUST ignore the virtual ACL_CREATE
  77           * and ACL_DELETE rights. See RFC 4314 [2.1]. However, we still need
  78           * to handle these rights when dealing with RFC 2086 servers since
  79           * we are abstracting out use of ACL_CREATE/ACL_DELETE to their
  80           * component RFC 4314 rights. */
  81          foreach ($this->_virtual as $key => $val) {
  82              foreach ($val as $right) {
  83                  if ($this[$right]) {
  84                      foreach (array_keys($this->_virtual) as $virtual) {
  85                          unset($this[$virtual]);
  86                      }
  87                      return;
  88                  }
  89              }
  90          }
  91          foreach ($this->_virtual as $key => $val) {
  92              if ($this[$key]) {
  93                  unset($this[$key]);
  94                  $this->_rights = array_unique(array_merge($this->_rights, $val));
  95              }
  96          }
  97      }
  98  
  99      /* ArrayAccess methods. */
 100  
 101      /**
 102       */
 103      #[ReturnTypeWillChange]
 104      public function offsetExists($offset)
 105      {
 106          return $this[$offset];
 107      }
 108  
 109      /**
 110       */
 111      #[ReturnTypeWillChange]
 112      public function offsetGet($offset)
 113      {
 114          return in_array($offset, $this->_rights);
 115      }
 116  
 117      /**
 118       */
 119      #[ReturnTypeWillChange]
 120      public function offsetSet($offset, $value)
 121      {
 122          if ($value) {
 123              if (!$this[$offset]) {
 124                  $this->_rights[] = $offset;
 125                  $this->_normalize();
 126              }
 127          } elseif ($this[$offset]) {
 128              if (isset($this->_virtual[$offset])) {
 129                  foreach ($this->_virtual[$offset] as $val) {
 130                      unset($this[$val]);
 131                  }
 132              }
 133              unset($this[$offset]);
 134          }
 135      }
 136  
 137      /**
 138       */
 139      #[ReturnTypeWillChange]
 140      public function offsetUnset($offset)
 141      {
 142          $this->_rights = array_values(array_diff($this->_rights, array($offset)));
 143      }
 144  
 145      /* IteratorAggregate method. */
 146  
 147      #[ReturnTypeWillChange]
 148      public function getIterator()
 149      {
 150          return new ArrayIterator($this->_rights);
 151      }
 152  
 153      /* Serializable methods. */
 154  
 155      /**
 156       */
 157      public function serialize()
 158      {
 159          return serialize($this->__serialize());
 160      }
 161  
 162      /**
 163       */
 164      public function unserialize($data)
 165      {
 166          $data = @unserialize($data);
 167          if (!is_array($data)) {
 168              throw new Exception('Cache version changed.');
 169          }
 170          $this->__unserialize($data);
 171      }
 172  
 173      /**
 174       * @return array
 175       */
 176      public function __serialize()
 177      {
 178          return array(
 179              'rights' => $this->_rights
 180          );
 181      }
 182  
 183      public function __unserialize(array $data)
 184      {
 185          $this->_rights = $data['rights'];
 186      }
 187  
 188  }