Upgrade phpCAS
[piwik-CASLogin.git] / CAS / CAS / PGTStorage / File.php
1 <?php
2
3 /**
4  * Licensed to Jasig under one or more contributor license
5  * agreements. See the NOTICE file distributed with this work for
6  * additional information regarding copyright ownership.
7  *
8  * Jasig licenses this file to you under the Apache License,
9  * Version 2.0 (the "License"); you may not use this file except in
10  * compliance with the License. You may obtain a copy of the License at:
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * PHP Version 5
21  *
22  * @file     CAS/PGTStorage/AbstractStorage.php
23  * @category Authentication
24  * @package  PhpCAS
25  * @author   Pascal Aubry <pascal.aubry@univ-rennes1.fr>
26  * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
27  * @link     https://wiki.jasig.org/display/CASC/phpCAS
28  */
29
30 /**
31  * The CAS_PGTStorage_File class is a class for PGT file storage. An instance of
32  * this class is returned by CAS_Client::SetPGTStorageFile().
33  *
34  * @class    CAS_PGTStorage_File
35  * @category Authentication
36  * @package  PhpCAS
37  * @author   Pascal Aubry <pascal.aubry@univ-rennes1.fr>
38  * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
39  * @link     https://wiki.jasig.org/display/CASC/phpCAS
40  *
41  *
42  * @ingroup internalPGTStorageFile
43  */
44
45 class CAS_PGTStorage_File extends CAS_PGTStorage_AbstractStorage
46 {
47     /**
48      * @addtogroup internalPGTStorageFile
49      * @{
50      */
51
52     /**
53      * a string telling where PGT's should be stored on the filesystem. Written by
54      * PGTStorageFile::PGTStorageFile(), read by getPath().
55      *
56      * @private
57      */
58     var $_path;
59
60     /**
61      * This method returns the name of the directory where PGT's should be stored
62      * on the filesystem.
63      *
64      * @return the name of a directory (with leading and trailing '/')
65      *
66      * @private
67      */
68     function getPath()
69     {
70         return $this->_path;
71     }
72
73     // ########################################################################
74     //  DEBUGGING
75     // ########################################################################
76
77     /**
78      * This method returns an informational string giving the type of storage
79      * used by the object (used for debugging purposes).
80      *
81      * @return an informational string.
82      * @public
83      */
84     function getStorageType()
85     {
86         return "file";
87     }
88
89     /**
90      * This method returns an informational string giving informations on the
91      * parameters of the storage.(used for debugging purposes).
92      *
93      * @return an informational string.
94      * @public
95      */
96     function getStorageInfo()
97     {
98         return 'path=`'.$this->getPath().'\'';
99     }
100
101     // ########################################################################
102     //  CONSTRUCTOR
103     // ########################################################################
104
105     /**
106      * The class constructor, called by CAS_Client::SetPGTStorageFile().
107      *
108      * @param CAS_Client $cas_parent the CAS_Client instance that creates the object.
109      * @param string     $path       the path where the PGT's should be stored
110      *
111      * @return void
112      *
113      * @public
114      */
115     function __construct($cas_parent,$path)
116     {
117         phpCAS::traceBegin();
118         // call the ancestor's constructor
119         parent::__construct($cas_parent);
120
121         if (empty($path)) {
122             $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH;
123         }
124         // check that the path is an absolute path
125         if (getenv("OS")=="Windows_NT") {
126
127             if (!preg_match('`^[a-zA-Z]:`', $path)) {
128                 phpCAS::error('an absolute path is needed for PGT storage to file');
129             }
130
131         } else {
132
133             if ( $path[0] != '/' ) {
134                 phpCAS::error('an absolute path is needed for PGT storage to file');
135             }
136
137             // store the path (with a leading and trailing '/')
138             $path = preg_replace('|[/]*$|', '/', $path);
139             $path = preg_replace('|^[/]*|', '/', $path);
140         }
141
142         $this->_path = $path;
143         phpCAS::traceEnd();
144     }
145
146     // ########################################################################
147     //  INITIALIZATION
148     // ########################################################################
149
150     /**
151      * This method is used to initialize the storage. Halts on error.
152      *
153      * @return void
154      * @public
155      */
156     function init()
157     {
158         phpCAS::traceBegin();
159         // if the storage has already been initialized, return immediatly
160         if ($this->isInitialized()) {
161             return;
162         }
163         // call the ancestor's method (mark as initialized)
164         parent::init();
165         phpCAS::traceEnd();
166     }
167
168     // ########################################################################
169     //  PGT I/O
170     // ########################################################################
171
172     /**
173      * This method returns the filename corresponding to a PGT Iou.
174      *
175      * @param string $pgt_iou the PGT iou.
176      *
177      * @return a filename
178      * @private
179      */
180     function getPGTIouFilename($pgt_iou)
181     {
182         phpCAS::traceBegin();
183         $filename = $this->getPath().$pgt_iou.'.plain';
184         phpCAS::traceEnd($filename);
185         return $filename;
186     }
187
188     /**
189      * This method stores a PGT and its corresponding PGT Iou into a file. Echoes a
190      * warning on error.
191      *
192      * @param string $pgt     the PGT
193      * @param string $pgt_iou the PGT iou
194      *
195      * @return void
196      *
197      * @public
198      */
199     function write($pgt,$pgt_iou)
200     {
201         phpCAS::traceBegin();
202         $fname = $this->getPGTIouFilename($pgt_iou);
203         if (!file_exists($fname)) {
204             touch($fname);
205             // Chmod will fail on windows
206             @chmod($fname, 0600);
207             if ($f=fopen($fname, "w")) {
208                 if (fputs($f, $pgt) === false) {
209                     phpCAS::error('could not write PGT to `'.$fname.'\'');
210                 }
211                 phpCAS::trace('Successful write of PGT to `'.$fname.'\'');
212                 fclose($f);
213             } else {
214                 phpCAS::error('could not open `'.$fname.'\'');
215             }
216         } else {
217             phpCAS::error('File exists: `'.$fname.'\'');
218         }
219         phpCAS::traceEnd();
220     }
221
222     /**
223      * This method reads a PGT corresponding to a PGT Iou and deletes the
224      * corresponding file.
225      *
226      * @param string $pgt_iou the PGT iou
227      *
228      * @return the corresponding PGT, or FALSE on error
229      *
230      * @public
231      */
232     function read($pgt_iou)
233     {
234         phpCAS::traceBegin();
235         $pgt = false;
236         $fname = $this->getPGTIouFilename($pgt_iou);
237         if (file_exists($fname)) {
238             if (!($f=fopen($fname, "r"))) {
239                 phpCAS::error('could not open `'.$fname.'\'');
240             } else {
241                 if (($pgt=fgets($f)) === false) {
242                     phpCAS::error('could not read PGT from `'.$fname.'\'');
243                 }
244                 phpCAS::trace('Successful read of PGT to `'.$fname.'\'');
245                 fclose($f);
246             }
247             // delete the PGT file
248             @unlink($fname);
249         } else {
250             phpCAS::error('No such file `'.$fname.'\'');
251         }
252         phpCAS::traceEnd($pgt);
253         return $pgt;
254     }
255
256     /** @} */
257
258 }
259 ?>