v. 0.6.3 from http://dev.piwik.org/trac/ticket/598
[piwik-CASLogin.git] / CAS / CAS / PGTStorage / pgt-file.php
1 <?php\r
2 /*\r
3  * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative.\r
4  * All rights reserved.\r
5  * \r
6  * Redistribution and use in source and binary forms, with or without\r
7  * modification, are permitted provided that the following conditions are met:\r
8  * \r
9  *     * Redistributions of source code must retain the above copyright notice,\r
10  *       this list of conditions and the following disclaimer.\r
11  *     * Redistributions in binary form must reproduce the above copyright notice,\r
12  *       this list of conditions and the following disclaimer in the documentation\r
13  *       and/or other materials provided with the distribution.\r
14  *     * Neither the name of the ESUP-Portail consortium & the JA-SIG\r
15  *       Collaborative nor the names of its contributors may be used to endorse or\r
16  *       promote products derived from this software without specific prior\r
17  *       written permission.\r
18 \r
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\r
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\r
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
29  */\r
30 /**\r
31  * @file CAS/PGTStorage/pgt-file.php\r
32  * Basic class for PGT file storage\r
33  */\r
34 \r
35 /**\r
36  * @class PGTStorageFile\r
37  * The PGTStorageFile class is a class for PGT file storage. An instance of \r
38  * this class is returned by CASClient::SetPGTStorageFile().\r
39  *\r
40  * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>\r
41  *\r
42  * @ingroup internalPGTStorageFile\r
43  */\r
44 \r
45 class PGTStorageFile extends PGTStorage\r
46 {\r
47   /** \r
48    * @addtogroup internalPGTStorageFile \r
49    * @{ \r
50    */\r
51 \r
52   /**\r
53    * a string telling where PGT's should be stored on the filesystem. Written by\r
54    * PGTStorageFile::PGTStorageFile(), read by getPath().\r
55    *\r
56    * @private\r
57    */\r
58   var $_path;\r
59 \r
60   /**\r
61    * This method returns the name of the directory where PGT's should be stored \r
62    * on the filesystem.\r
63    *\r
64    * @return the name of a directory (with leading and trailing '/')\r
65    *\r
66    * @private\r
67    */\r
68   function getPath()\r
69     {\r
70       return $this->_path;\r
71     }\r
72 \r
73   /**\r
74    * a string telling the format to use to store PGT's (plain or xml). Written by\r
75    * PGTStorageFile::PGTStorageFile(), read by getFormat().\r
76    *\r
77    * @private\r
78    */\r
79   var $_format;\r
80 \r
81   /**\r
82    * This method returns the format to use when storing PGT's on the filesystem.\r
83    *\r
84    * @return a string corresponding to the format used (plain or xml).\r
85    *\r
86    * @private\r
87    */\r
88   function getFormat()\r
89     {\r
90       return $this->_format;\r
91     }\r
92 \r
93   // ########################################################################\r
94   //  DEBUGGING\r
95   // ########################################################################\r
96   \r
97   /**\r
98    * This method returns an informational string giving the type of storage\r
99    * used by the object (used for debugging purposes).\r
100    *\r
101    * @return an informational string.\r
102    * @public\r
103    */\r
104   function getStorageType()\r
105     {\r
106       return "file";\r
107     }\r
108 \r
109   /**\r
110    * This method returns an informational string giving informations on the\r
111    * parameters of the storage.(used for debugging purposes).\r
112    *\r
113    * @return an informational string.\r
114    * @public\r
115    */\r
116   function getStorageInfo()\r
117     {\r
118       return 'path=`'.$this->getPath().'\', format=`'.$this->getFormat().'\'';\r
119     }\r
120 \r
121   // ########################################################################\r
122   //  CONSTRUCTOR\r
123   // ########################################################################\r
124   \r
125   /**\r
126    * The class constructor, called by CASClient::SetPGTStorageFile().\r
127    *\r
128    * @param $cas_parent the CASClient instance that creates the object.\r
129    * @param $format the format used to store the PGT's (`plain' and `xml' allowed).\r
130    * @param $path the path where the PGT's should be stored\r
131    *\r
132    * @public\r
133    */\r
134   function PGTStorageFile($cas_parent,$format,$path)\r
135     {\r
136       phpCAS::traceBegin();\r
137       // call the ancestor's constructor\r
138       $this->PGTStorage($cas_parent);\r
139 \r
140       if (empty($format) ) $format = CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT;\r
141       if (empty($path) ) $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH;\r
142 \r
143       // check that the path is an absolute path\r
144       if (getenv("OS")=="Windows_NT"){\r
145         \r
146          if (!preg_match('`^[a-zA-Z]:`', $path)) {\r
147                 phpCAS::error('an absolute path is needed for PGT storage to file');\r
148         }\r
149         \r
150       }\r
151       else\r
152       {\r
153       \r
154         if ( $path[0] != '/' ) {\r
155                         phpCAS::error('an absolute path is needed for PGT storage to file');\r
156         }\r
157 \r
158         // store the path (with a leading and trailing '/')      \r
159         $path = preg_replace('|[/]*$|','/',$path);\r
160         $path = preg_replace('|^[/]*|','/',$path);\r
161       }\r
162       \r
163       $this->_path = $path;\r
164       // check the format and store it\r
165       switch ($format) {\r
166       case CAS_PGT_STORAGE_FILE_FORMAT_PLAIN:\r
167       case CAS_PGT_STORAGE_FILE_FORMAT_XML:\r
168         $this->_format = $format;\r
169         break;\r
170       default:\r
171         phpCAS::error('unknown PGT file storage format (`'.CAS_PGT_STORAGE_FILE_FORMAT_PLAIN.'\' and `'.CAS_PGT_STORAGE_FILE_FORMAT_XML.'\' allowed)');\r
172       }\r
173       phpCAS::traceEnd();      \r
174     }\r
175 \r
176   // ########################################################################\r
177   //  INITIALIZATION\r
178   // ########################################################################\r
179   \r
180   /**\r
181    * This method is used to initialize the storage. Halts on error.\r
182    *\r
183    * @public\r
184    */\r
185   function init()\r
186     {\r
187       phpCAS::traceBegin();\r
188       // if the storage has already been initialized, return immediatly\r
189       if ( $this->isInitialized() )\r
190         return;\r
191       // call the ancestor's method (mark as initialized)\r
192       parent::init();\r
193       phpCAS::traceEnd();      \r
194     }\r
195 \r
196   // ########################################################################\r
197   //  PGT I/O\r
198   // ########################################################################\r
199 \r
200   /**\r
201    * This method returns the filename corresponding to a PGT Iou.\r
202    *\r
203    * @param $pgt_iou the PGT iou.\r
204    *\r
205    * @return a filename\r
206    * @private\r
207    */\r
208   function getPGTIouFilename($pgt_iou)\r
209     {\r
210       phpCAS::traceBegin();\r
211       $filename = $this->getPath().$pgt_iou.'.'.$this->getFormat();\r
212       phpCAS::traceEnd($filename);\r
213       return $filename;\r
214     }\r
215   \r
216   /**\r
217    * This method stores a PGT and its corresponding PGT Iou into a file. Echoes a\r
218    * warning on error.\r
219    *\r
220    * @param $pgt the PGT\r
221    * @param $pgt_iou the PGT iou\r
222    *\r
223    * @public\r
224    */\r
225   function write($pgt,$pgt_iou)\r
226           {\r
227           phpCAS::traceBegin();\r
228           $fname = $this->getPGTIouFilename($pgt_iou);\r
229           if(!file_exists($fname)){\r
230                   if ( $f=fopen($fname,"w") ) {\r
231                           if ( fputs($f,$pgt) === FALSE ) {\r
232                                   phpCAS::error('could not write PGT to `'.$fname.'\'');\r
233                           }\r
234                           fclose($f);\r
235                   } else {\r
236                           phpCAS::error('could not open `'.$fname.'\'');\r
237                   }\r
238           }else{\r
239                   phpCAS::error('File exists: `'.$fname.'\'');\r
240           }\r
241           phpCAS::traceEnd();      \r
242           }\r
243 \r
244   /**\r
245    * This method reads a PGT corresponding to a PGT Iou and deletes the \r
246    * corresponding file.\r
247    *\r
248    * @param $pgt_iou the PGT iou\r
249    *\r
250    * @return the corresponding PGT, or FALSE on error\r
251    *\r
252    * @public\r
253    */\r
254   function read($pgt_iou)\r
255           {\r
256           phpCAS::traceBegin();\r
257           $pgt = FALSE;\r
258           $fname = $this->getPGTIouFilename($pgt_iou);\r
259           if (file_exists($fname)){\r
260                   if ( !($f=fopen($fname,"r")) ) {\r
261                           phpCAS::trace('could not open `'.$fname.'\'');\r
262                   } else {\r
263                           if ( ($pgt=fgets($f)) === FALSE ) {\r
264                                   phpCAS::trace('could not read PGT from `'.$fname.'\'');\r
265                           } \r
266                           fclose($f);\r
267                   }\r
268                   \r
269                   // delete the PGT file\r
270                   @unlink($fname);\r
271           }else{\r
272                   phpCAS::trace('No such file `'.$fname.'\'');\r
273           }\r
274           phpCAS::traceEnd($pgt);\r
275           return $pgt;\r
276           }\r
277   \r
278   /** @} */\r
279   \r
280 }\r
281 \r
282   \r
283 ?>