Updated copyright:
[core.git] / inc / classes / main / file_directories / io_stream / class_FileIoStream.php
1 <?php
2 /**
3  * An universal class for file input/output streams.
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, FileOutputStreamer {
25         /**
26          * File header indicator
27          */
28         const FILE_IO_FILE_HEADER_ID = '@head';
29
30         /**
31          * Data block indicator
32          */
33         const FILE_IO_DATA_BLOCK_ID = '@data';
34
35         /**
36          * Separator #1
37          */
38         const FILE_IO_CHUNKER = ':';
39
40         /**
41          * Separator #2
42          */
43         const FILE_IO_SEPARATOR = '^';
44
45         /**
46          * Protected constructor
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51         }
52
53         /**
54          * Create a file IO stream. This is a class for performing all actions
55          * on files like creating, deleting and loading them.
56          *
57          * @return      $ioInstance     An instance of FileIoStream
58          */
59         public static final function createFileIoStream () {
60                 // Create new instance
61                 $ioInstance = new FileIoStream();
62
63                 // Return the instance
64                 return $ioInstance;
65         }
66
67         /**
68          * Saves data to a given local file and create missing directory structures
69          *
70          * @param       $fileName       The file name for the to be saved file
71          * @param       $dataArray      The data we shall store to the file
72          * @return      void
73          * @see         FileOutputStreamer
74          * @todo        This method needs heavy rewrite
75          */
76         public final function saveFile ($fileName, array $dataArray) {
77                 // Try it five times
78                 $dirName = '';
79                 $fileInstance = NULL;
80
81                 for ($idx = 0; $idx < 5; $idx++) {
82                         // Get a file output pointer
83                         try {
84                                 $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_output_class', array($fileName, 'wb'));
85                         } catch (FileIoException $e) {
86                                 // Create missing directory
87                                 $dirName = dirname($fileName);
88                                 for ($idx2 = 0; $idx2 < (2 - $idx); $idx2++) {
89                                         $dirName = dirname($dirName);
90                                 } // END - for
91
92                                 // Try to create it
93                                 @mkdir($dirName);
94                         }
95                 } // END - for
96
97                 // Write a header information for validation purposes
98                 $fileInstance->writeToFile(sprintf('%s%s%s%s%s%s%s%s%s' . PHP_EOL,
99                         self::FILE_IO_FILE_HEADER_ID,
100                         self::FILE_IO_SEPARATOR,
101                         $dataArray[0],
102                         self::FILE_IO_CHUNKER,
103                         time(),
104                         self::FILE_IO_CHUNKER,
105                         strlen($dataArray[1]),
106                         self::FILE_IO_CHUNKER,
107                         md5($dataArray[1])
108                 ));
109
110                 // Encode the (maybe) binary stream with Base64
111                 $b64Stream = base64_encode($dataArray[1]);
112
113                 // write the data line by line
114                 $line = str_repeat(' ', 50); $idx = 0;
115                 while (strlen($line) == 50) {
116                         // Get 50 chars or less
117                         $line = substr($b64Stream, $idx, 50);
118
119                         // Save it to the stream
120                         $fileInstance->writeToFile(sprintf('%s%s%s%s%s' . PHP_EOL,
121                                 self::FILE_IO_DATA_BLOCK_ID,
122                                 self::FILE_IO_SEPARATOR,
123                                 $line,
124                                 self::FILE_IO_CHUNKER,
125                                 md5($line)
126                         ));
127
128                         // Advance to the next 50-chars block
129                         $idx += 50;
130                 } // END - while
131
132                 // Close the file
133                 $fileInstance->closeFile();
134         }
135
136         /**
137          * Reads from a local file
138          *
139          * @param       $fqfn   The full-qualified file-name which we shall load
140          * @return      $array  An array with the element 'header' and 'data'
141          * @see         FileInputStreamer
142          */
143         public final function loadFileContents ($fqfn) {
144                 // Initialize some variables and arrays
145                 $inputBuffer = '';
146                 $lastBuffer = '';
147                 $header = array();
148                 $data = array();
149                 $readData = ''; // This will contain our read data
150
151                 // Get a file input handler
152                 $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_class', array($fqfn));
153
154                 // Read all it's contents (we very and transparently decompress it below)
155                 while ($readRawLine = $fileInstance->readFromFile()) {
156                         // Add the read line to the buffer
157                         $inputBuffer .= $readRawLine;
158
159                         // Break infinite loop maybe caused by the input handler
160                         if ($lastBuffer == $inputBuffer) {
161                                 break;
162                         } // END - if
163
164                         // Remember last read line for avoiding possible infinite loops
165                         $lastBuffer = $inputBuffer;
166                 } // END - while
167
168                 // Close directory handle
169                 $fileInstance->closeFile();
170
171                 // Convert it into an array
172                 $inputBuffer = explode(chr(10), $inputBuffer);
173
174                 // Now process the read lines and verify it's content
175                 foreach ($inputBuffer as $rawLine) {
176                         // Trim it a little but not the leading spaces/tab-stops
177                         $rawLine = rtrim($rawLine);
178
179                         // Analyze this line
180                         if (substr($rawLine, 0, 5) == self::FILE_IO_FILE_HEADER_ID) {
181                                 // Header found, so let's extract it
182                                 $header = explode(self::FILE_IO_SEPARATOR, $rawLine);
183                                 $header = trim($header[1]);
184
185                                 // Now we must convert it again into an array
186                                 $header = explode(self::FILE_IO_CHUNKER, $header);
187
188                                 // Is the header (maybe) valid?
189                                 if (count($header) != 4) {
190                                         // Throw an exception
191                                         throw new InvalidArrayCountException(array($this, 'header', count($header), 4), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
192                                 } // END - if
193                         } elseif (substr($rawLine, 0, 5) == self::FILE_IO_DATA_BLOCK_ID) {
194                                 // Is a data line!
195                                 $data = explode(self::FILE_IO_SEPARATOR, $rawLine);
196                                 $data = $data[1];
197
198                                 // First element is the data, second the MD5 checksum
199                                 $data = explode(self::FILE_IO_CHUNKER, $data);
200
201                                 // Validate the read line
202                                 if (count($data) == 2) {
203                                         if (md5($data[0]) != $data[1]) {
204                                                 // MD5 hash did not match!
205                                                 throw new InvalidMD5ChecksumException(array($this, md5($data[0]), $data[1]), self::EXCEPTION_MD5_CHECKSUMS_MISMATCH);
206                                         } // END - if
207                                 } else {
208                                         // Invalid count!
209                                         throw new InvalidArrayCountException(array($this, 'data', count($data), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
210                                 }
211
212                                 // Add this to the readData string
213                                 $readData .= $data[0];
214                         } else {
215                                 // Other raw lines than header/data tagged lines and re-add the new-line char
216                                 $readData .= $rawLine . PHP_EOL;
217                         }
218                 } // END - foreach
219
220                 // Was raw lines read and no header/data?
221                 if ((!empty($readData)) && (count($header) == 0) && (count($data) == 0)) {
222                         // Return raw lines back
223                         return $readData;
224                 } // END - if
225
226                 // Was a header found?
227                 if (count($header) != 4) {
228                         // Throw an exception
229                         throw new InvalidArrayCountException(array($this, 'header', count($header), 4), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
230                 } // END - if
231
232                 // Decode all from Base64
233                 $readData = @base64_decode($readData);
234
235                 // Does the size match?
236                 if (strlen($readData) != $header[2]) {
237                         // Size did not match
238                         throw new InvalidDataLengthException(array($this, strlen($readData), $header[2]), self::EXCEPTION_UNEXPECTED_STRING_SIZE);
239                 } // END - if
240
241                 // Validate the decoded data with the final MD5 hash
242                 if (md5($readData) != $header[3]) {
243                         // MD5 hash did not match!
244                         throw new InvalidMD5ChecksumException(array($this, md5($readData), $header[3]), self::EXCEPTION_MD5_CHECKSUMS_MISMATCH);
245                 } // END - if
246
247                 // Return all in an array
248                 return array(
249                         'header' => $header,
250                         'data'   => $readData
251                 );
252         }
253
254         /**
255          * Streams the data and maybe does something to it
256          *
257          * @param       $data   The data (string mostly) to "stream"
258          * @return      $data   The data (string mostly) to "stream"
259          * @throws      UnsupportedOperationException   If this method is called
260          */
261         public function streamData ($data) {
262                 self::createDebugInstance(__CLASS__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
263                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
264         }
265
266         /**
267          * Determines seek position
268          *
269          * @return      $seekPosition   Current seek position
270          * @todo        0% done
271          */
272         public function determineSeekPosition () {
273                 $this->partialStub();
274         }
275
276         /**
277          * Seek to given offset (default) or other possibilities as fseek() gives.
278          *
279          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
280          * @param       $whence         Added to offset (default: only use offset to seek to)
281          * @return      $status         Status of file seek: 0 = success, -1 = failed
282          */
283         public function seek ($offset, $whence = SEEK_SET) {
284                 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
285         }
286
287         /**
288          * Size of file stack
289          *
290          * @return      $size   Size (in bytes) of file
291          */
292         public function size () {
293                 $this->partialStub();
294         }
295 }
296
297 // [EOF]
298 ?>