You are here

class secureCookieTreeTwo in Secure Cookie Data 7.2

@file secure_cookie_data_tree_two.class.inc @author António P. P. Almeida <appa@perusio.net> @date Mon Dec 2 11:59:02 2013

@brief Extends the secureCookieBasic class for handling two level deep trees.

Hierarchy

Expanded class hierarchy of secureCookieTreeTwo

File

modules/secure_cookie_data_tree_two.class.inc, line 12
secure_cookie_data_tree_two.class.inc @author António P. P. Almeida <appa@perusio.net> @date Mon Dec 2 11:59:02 2013

View source
class secureCookieTreeTwo extends secureCookieBasic {

  // The class specific secret.
  public static $__secret = 'afe76e816d2486f2ec796ec199af1a613202a24a';

  // The cookie name.
  public static $__cookie_name = 'SecureDataCookie32';

  /**
   * Setter for the class.
   *
   * @param object|array $data
   *   The data to be stored in the cookie in the form of an object or a keyed
   *   array.
   * @return boolen
   *   TRUE if the cookie was modified/set, FALSE if not.
   */
  public static function set_tree($data) {

    // If given an object then set the cookie immediately.
    if (is_object($data)) {

      // Set the cookie.
      return self::set($data);
    }

    // Check if the cookie exists. If so then recover the existing value.
    if (isset($_COOKIE[static::$__cookie_name])) {
      $previous_data = parent::decode($_COOKIE[static::$__cookie_name]);

      // Unset the hmac.
      if (isset($previous_data->hmac)) {
        unset($previous_data->hmac);
      }
    }

    // If there's no previous data, meaning that no cookie exists then set it
    // now.
    if (!isset($previous_data)) {
      $previous_data = new stdClass();
      $previous_data->data = new stdClass();
    }

    // See if we have already set or not the child object. Just in case.
    if (!isset($previous_data->data)) {
      $previous_data->data = new stdClass();
    }

    // Setting the values for the tree node (level 0 and 1).
    if (!isset($previous_data->data->{$data}[$data['label_level0']])) {

      // Go one level down by creating a child element.
      $previous_data->data->{$data}[$data['label_level0']] = new stdClass();
    }

    // Handle the values list. Either is an array or is brand new. If an array
    // check that the value isn't already there.
    if (is_array($previous_data->data->{$data}[$data['label_level0']]->{$data}['label_level1'])) {

      // We have to copy the array to another variable. Yes, PHP sucks!
      $values = $previous_data->data->{$data}[$data['label_level0']]->{$data}['label_level1'];

      // Check to see if the value is different.
      if (!in_array($data['value'], $values)) {
        $values[] = $data['value'];
        $previous_data->data->{$data}[$data['label_level0']]->{$data}['label_level1'] = $values;
      }
    }
    else {
      $previous_data->data->{$data}[$data['label_level0']]->{$data}['label_level1'] = array(
        $data['value'],
      );
    }

    // Set the cookie.
    self::set($previous_data);
  }

  /**
   * Removes a certain node from the tree stored as cookie data.
   *
   * @param array $element
   *   Specification of the element to delete.
   * @param object $data
   *   The data tree as an object.
   * @return nothing
   *   Side effects only.
   */
  public static function delete_node($element, $data = NULL) {

    // Get the cookie data if not given.
    if (empty($data)) {
      $cookie_data = self::get();
    }
    else {
      $cookie_data = $data;
    }

    // Get the element description to know at which depth a tree branch should
    // be pruned.
    $keys = array_keys($element);

    // Check to see if we are at the first or at the second level and proceed
    // accordingly.
    if (in_array('label_level1', $keys)) {
      if (isset($cookie_data->data->{$element}[$element['label_level0']]->{$element}['label_level1'])) {
        unset($cookie_data->data->{$element}[$element['label_level0']]->{$element}['label_level1']);
      }
    }
    elseif (in_array('label_level0', $keys)) {
      if (isset($cookie_data->data->{$element}[$element['label_level0']])) {
        unset($cookie_data->data->{$element}[$element['label_level0']]);
      }
    }
    elseif (count($keys) == 1 && $keys[0] == 'ALL' && $element['ALL']) {
      if (isset($cookie_data)) {
        self::delete();
      }
    }
    else {
      return FALSE;
    }

    // Set the cookie without the deleted node.
    self::set_tree($cookie_data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
secureCookieBasic::$__algorithm protected static property
secureCookieBasic::$__cookie_duration public static property
secureCookieBasic::$__cookie_path public static property
secureCookieBasic::decode public static function Decodes the data as stored in the cookie.
secureCookieBasic::delete public static function Destroyer for the class. Deletes the data cookie if it exists.
secureCookieBasic::encode public static function Encode the data as a safe string to be in a cookie.
secureCookieBasic::get public static function Getter for the class.
secureCookieBasic::get_domain public static function Get the current cookie domain.
secureCookieBasic::get_secret public static function Returns the secret that is used as a key for the HMAC.
secureCookieBasic::secure_session_p public static function Predicate to check if the current session is secure or not.
secureCookieBasic::set public static function Setter for the class.
secureCookieBasic::set_hmac public static function Generates the ciphered hash for the given data, algorithm and secret key.
secureCookieBasic::validate public static function Verifies the message digest for the given data, algorithm and secret key.
secureCookieTreeTwo::$__cookie_name public static property Overrides secureCookieBasic::$__cookie_name
secureCookieTreeTwo::$__secret public static property Overrides secureCookieBasic::$__secret
secureCookieTreeTwo::delete_node public static function Removes a certain node from the tree stored as cookie data.
secureCookieTreeTwo::set_tree public static function Setter for the class.