]> git.mxchange.org Git - hub.git/blob - application/hub/classes/streams/raw_data/input/class_RawDataInputStream.php
Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / classes / streams / raw_data / input / class_RawDataInputStream.php
1 <?php
2 /**
3  * A class for handling incoming (encoded) raw data with start and end markers.
4  * The "stream" is being verified by its length (if modulo 4 of it is always
5  * zero) and if the "stream" contains all valid characters (the BASE64
6  * "alphabet").
7  *
8  * Since the latest refacturing this class works "handler-less".
9  *
10  * @author              Roland Haeder <webmaster@shipsimu.org>
11  * @version             0.0.0
12  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009  Developer Team
13  * @license             GNU GPL 3.0 or any newer version
14  * @link                http://www.shipsimu.org
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29 class RawDataInputStream extends BaseStream implements InputStream {
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Creates an instance of this node class
42          *
43          * @return      $streamInstance         An instance of this node class
44          */
45         public final static function createRawDataInputStream () {
46                 // Get a new instance
47                 $streamInstance = new RawDataInputStream();
48
49                 // Return the instance
50                 return $streamInstance;
51         }
52
53         /**
54          * Streams the data and maybe does something to it
55          *
56          * @param       $data   The data (string mostly) to "stream"
57          * @return      $data   The data (string mostly) to "stream"
58          * @todo        Do we need to do something more here?
59          * @throws      Base64EncodingModuloException   If the data's length modulo 4 is not zero
60          * @throws      Base64EncodingBadException              If the data contains characters which are not in the "alphabet" of BASE64 messages.
61          * @throws      MultipleMessageSentException    If the sender has sent two messages and both end up here
62          */
63         public function streamData ($data) {
64                 // Debug message
65                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RAW-INPUT-STREAM[' . __METHOD__ . ':' . __LINE__ . ': data(' . strlen($data) . ')=' . $data);
66
67                 // Do we have start and end marker again?
68                 assert($this->ifStartEndMarkersSet($data));
69
70                 // Count of start and end markers must be the same
71                 assert(substr_count($data, BaseRawDataHandler::STREAM_START_MARKER) == substr_count($data, BaseRawDataHandler::STREAM_END_MARKER));
72
73                 // Check if more than two start markers exist and if so, split it.
74                 if (substr_count($data, BaseRawDataHandler::STREAM_START_MARKER) > 1) {
75                         // Please do it outside this method
76                         throw new MultipleMessageSentException(array($this, $data), BaseHubSystem::EXCEPTION_MULTIPLE_MESSAGE_SENT);
77                 } // END - if
78
79                 // Remove both
80                 $data = substr($data, strlen(BaseRawDataHandler::STREAM_START_MARKER), -1 * strlen(BaseRawDataHandler::STREAM_END_MARKER));
81
82                 // Can it be validated?
83                 if ((strlen($data) % 4) != 0) {
84                         // Length modulo 4 must be zero, else it is an invalid Base64 message
85                         throw new Base64EncodingModuloException(array($this, $data), BaseHubSystem::EXCEPTION_BASE64_ENCODING_NOT_MODULO_4);
86                 } elseif (!$this->isBase64Encoded($data)) {
87                         // Is not a valid Base64-encoded message
88                         throw new Base64EncodingBadException(array($this, $data), BaseHubSystem::EXCEPTION_BASE64_BAD_ENCODING);
89                 } else {
90                         // Decode the data with BASE64-encoding
91                         $data = base64_decode($data);
92                 }
93
94                 // Debug message
95                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('RAW-INPUT-STREAM: Length of data is now ' . strlen($data) . ' Bytes.');
96
97                 // Return it
98                 return $data;
99         }
100 }
101
102 // [EOF]
103 ?>