Apache Zeta Components Manual :: File Source for openid_file_store.php

Source for file openid_file_store.php

Documentation is available at openid_file_store.php

  1. <?php
  2. /**
  3.  * File containing the ezcAuthenticationOpenidFileStore class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23.  * @filesource
  24.  * @package Authentication
  25.  * @version //autogen//
  26.  */
  27.  
  28. /**
  29.  * Class providing file storage for OpenID authentication.
  30.  *
  31.  * Example of use:
  32.  * <code>
  33.  * // create an OpenID options object
  34.  * $options = new ezcAuthenticationOpenidOptions();
  35.  * $options->mode = ezcAuthenticationOpenidFilter::MODE_SMART;
  36.  *
  37.  * // define a file store
  38.  * $options->store = new ezcAuthenticationOpenidFileStore( '/tmp/store' );
  39.  *
  40.  * // create an OpenID filter based on the options object
  41.  * $filter = new ezcAuthenticationOpenidFilter( $options );
  42.  * </code>
  43.  *
  44.  * @property string $path 
  45.  *            The path where the files will be kept. It must exist and it must
  46.  *            be writable.
  47.  *
  48.  * @package Authentication
  49.  * @version //autogen//
  50.  */
  51. {
  52.     /**
  53.      * Holds the properties of this class.
  54.      *
  55.      * @var array(string=>mixed) 
  56.      */
  57.     private $properties array();
  58.  
  59.     /**
  60.      * Creates a new object of this class.
  61.      *
  62.      * @throws ezcBaseFileNotFoundException
  63.      *          if $path does not exist
  64.      * @throws ezcBaseFilePermissionException
  65.      *          if $path cannot be opened for reading and writing
  66.      * @param string $path The path where to save the nonces
  67.      * @param ezcAuthenticationOpenidFileStoreOptions $options Options for this class
  68.      */
  69.     public function __construct$pathezcAuthenticationOpenidFileStoreOptions $options null )
  70.     {
  71.         $this->path $path;
  72.         $this->options = $options === null new ezcAuthenticationOpenidFileStoreOptions($options;
  73.     }
  74.  
  75.     /**
  76.      * Sets the property $name to $value.
  77.      *
  78.      * @throws ezcBasePropertyNotFoundException
  79.      *          if the property $name does not exist
  80.      * @throws ezcBaseValueException
  81.      *          if $value is not correct for the property $name
  82.      * @throws ezcBaseFileNotFoundException
  83.      *          if the $value file does not exist
  84.      * @throws ezcBaseFilePermissionException
  85.      *          if the $value file cannot be opened for reading and writing
  86.      * @param string $name The name of the property to set
  87.      * @param mixed $value The new value of the property
  88.      * @ignore
  89.      */
  90.     public function __set$name$value )
  91.     {
  92.         switch $name )
  93.         {
  94.             case 'path':
  95.                 if !is_string$value ) )
  96.                 {
  97.                     throw new ezcBaseValueException$name$value'string' );
  98.                 }
  99.  
  100.                 if !is_dir$value ) )
  101.                 {
  102.                     throw new ezcBaseFileNotFoundException$value );
  103.                 }
  104.  
  105.                 if !is_readable$value ) )
  106.                 {
  107.                     throw new ezcBaseFilePermissionException$valueezcBaseFileException::READ );
  108.                 }
  109.  
  110.                 if !is_writable$value ) )
  111.                 {
  112.                     throw new ezcBaseFilePermissionException$valueezcBaseFileException::WRITE );
  113.                 }
  114.  
  115.                 $this->properties[$name$value;
  116.                 break;
  117.  
  118.             default:
  119.                 throw new ezcBasePropertyNotFoundException$name );
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * Returns the value of the property $name.
  125.      *
  126.      * @throws ezcBasePropertyNotFoundException
  127.      *          if the property $name does not exist
  128.      * @param string $name The name of the property for which to return the value
  129.      * @return mixed 
  130.      * @ignore
  131.      */
  132.     public function __get$name )
  133.     {
  134.         switch $name )
  135.         {
  136.             case 'path':
  137.                 return $this->properties[$name];
  138.  
  139.             default:
  140.                 throw new ezcBasePropertyNotFoundException$name );
  141.         }
  142.     }
  143.  
  144.     /**
  145.      * Returns true if the property $name is set, otherwise false.
  146.      *
  147.      * @param string $name The name of the property to test if it is set
  148.      * @return bool 
  149.      * @ignore
  150.      */
  151.     public function __isset$name )
  152.     {
  153.         switch $name )
  154.         {
  155.             case 'path':
  156.                 return isset$this->properties[$name);
  157.  
  158.             default:
  159.                 return false;
  160.         }
  161.     }
  162.  
  163.     /**
  164.      * Stores the nonce in the store.
  165.      *
  166.      * Returns true if the nonce was stored successfully, and false otherwise.
  167.      *
  168.      * @throws ezcBaseFilePermissionException
  169.      *          if the nonce cannot be written in the store
  170.      * @param string $nonce The nonce value to store
  171.      * @return bool 
  172.      */
  173.     public function storeNonce$nonce )
  174.     {
  175.         $file $this->path DIRECTORY_SEPARATOR $nonce;
  176.  
  177.         // suppress warnings caused by fopen() if $file could not be opened
  178.         $fh @fopen$file'w' );
  179.  
  180.         if $fh === false )
  181.         {
  182.             throw new ezcBaseFilePermissionException$fileezcBaseFileException::WRITE );
  183.         }
  184.  
  185.         fclose$fh );
  186.  
  187.         return true;
  188.     }
  189.  
  190.     /**
  191.      * Checks if the nonce exists and afterwards deletes it.
  192.      *
  193.      * Returns the timestamp of the nonce if it exists, and false otherwise.
  194.      *
  195.      * @param string $nonce The nonce value to check and delete
  196.      * @return bool|int
  197.      */
  198.     public function useNonce$nonce )
  199.     {
  200.         $file $this->path DIRECTORY_SEPARATOR $nonce;
  201.  
  202.         if !file_exists$file ) )
  203.         {
  204.             return false;
  205.         }
  206.  
  207.         $lastModified filemtime$file );
  208.         unlink$file );
  209.  
  210.         return $lastModified;
  211.     }
  212.  
  213.     /**
  214.      * Stores an association in the store linked to the OpenID provider URL.
  215.      *
  216.      * Returns true if the association was stored successfully, and false
  217.      * otherwise.
  218.      *
  219.      * @throws ezcBaseFilePermissionException
  220.      *          if the nonce cannot be written in the store
  221.      * @param string $url The URL of the OpenID provider
  222.      * @param ezcAuthenticationOpenidAssociation $association The association value to store
  223.      * @return bool 
  224.      */
  225.     public function storeAssociation$url$association )
  226.     {
  227.         $file $this->path DIRECTORY_SEPARATOR $this->convertToFilename$url );
  228.  
  229.         // suppress warnings caused by fopen() if $file could not be opened
  230.         $fh @fopen$file'w' );
  231.  
  232.         if $fh === false )
  233.         {
  234.             throw new ezcBaseFilePermissionException$fileezcBaseFileException::WRITE );
  235.         }
  236.  
  237.         $data serialize$association );
  238.         fwrite$fh$data );
  239.         fclose$fh );
  240.  
  241.         return true;
  242.     }
  243.  
  244.     /**
  245.      * Returns the unserialized association linked to the OpenID provider URL.
  246.      *
  247.      * Returns false if the association could not be retrieved or if it expired.
  248.      *
  249.      * @param string $url The URL of the OpenID provider
  250.      * @return ezcAuthenticationOpenidAssociation 
  251.      */
  252.     public function getAssociation$url )
  253.     {
  254.         $file $this->path DIRECTORY_SEPARATOR $this->convertToFilename$url );
  255.  
  256.         if !file_exists$file ) )
  257.         {
  258.             return false;
  259.         }
  260.  
  261.         $data unserializefile_get_contents$file ) );
  262.         return $data;
  263.     }
  264.  
  265.     /**
  266.      * Removes the association linked to the OpenID provider URL.
  267.      *
  268.      * Returns true if the association could be removed, and false otherwise.
  269.      *
  270.      * @param string $url The URL of the OpenID provider
  271.      * @return bool 
  272.      */
  273.     public function removeAssociation$url )
  274.     {
  275.         $file $this->path DIRECTORY_SEPARATOR $this->convertToFilename$url );
  276.  
  277.         if !file_exists$file ) )
  278.         {
  279.             return false;
  280.         }
  281.  
  282.         unlink$file );
  283.         return true;
  284.     }
  285.  
  286.     /**
  287.      * Creates a valid filename from the provided string.
  288.      *
  289.      * @param string $value A string which needs to be used as a valid filename
  290.      * @return string 
  291.      */
  292.     protected function convertToFilename$value )
  293.     {
  294.         $result base64_encode$value );
  295.         $result str_replace'/''_'$result );
  296.         $result str_replace'+''-'$result );
  297.         return $result;
  298.     }
  299. }
  300. ?>
Documentation generated by phpDocumentor 1.4.3