Rewritten private fields (for seperators) to public class constants.
authorRoland Häder <roland@mxchange.org>
Mon, 18 Apr 2011 20:36:12 +0000 (20:36 +0000)
committerRoland Häder <roland@mxchange.org>
Mon, 18 Apr 2011 20:36:12 +0000 (20:36 +0000)
inc/classes/main/helper/web/links/class_WebLinkHelper.php
inc/classes/main/io/class_FileIoStream.php
inc/classes/main/streams/crypto/class_McryptStream.php

index 8fb87c3..8c4d0aa 100644 (file)
@@ -35,12 +35,12 @@ class WebLinkHelper extends BaseWebHelper implements HelpableTemplate {
        /**
         * First parameter seperator
         */
-       private $firstParameter = '?';
+       const FIRST_PARAMETER_SEPERATOR = '?';
 
        /**
         * Seperator for more paraemters
         */
-       private $parameterSeperator = '&amp;';
+       const EXTRA_PARAMETER_SEPERATOR = '&amp;';
 
        /**
         * Protected constructor
@@ -277,13 +277,13 @@ class WebLinkHelper extends BaseWebHelper implements HelpableTemplate {
                } // END - if
 
                // Default parameter seperator is &amp;
-               $seperator = $this->parameterSeperator;
+               $seperator = self::EXTRA_PARAMETER_SEPERATOR;
 
                // Is there a question mark in?
-               $linkArray = explode($this->firstParameter, $this->getLinkBase());
+               $linkArray = explode(self::FIRST_PARAMETER_SEPERATOR, $this->getLinkBase());
                if (count($linkArray) == 0) {
                        // No question mark
-                       $seperator = $this->firstParameter;
+                       $seperator = self::FIRST_PARAMETER_SEPERATOR;
                } // END - if
 
                // Prepare action
index fb7096d..d95f339 100644 (file)
@@ -25,22 +25,22 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil
        /**
         * File header indicator
         */
-       private $fileHeader = '@head';
+       const FILE_IO_FILE_HEADER_ID = '@head';
 
        /**
         * Data block indicator
         */
-       private $dataBlock = '@data';
+       const FILE_IO_DATA_BLOCK_ID = '@data';
 
        /**
         * Seperator #1
         */
-       private $chunker = ':';
+       const FILE_IO_CHUNKER = ':';
 
        /**
         * Seperator #2
         */
-       private $seperator = '^';
+       const FILE_IO_SEPERATOR = '^';
 
        /**
         * Protected constructor
@@ -94,14 +94,14 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil
 
                // Write a header information for validation purposes
                $fileInstance->writeToFile(sprintf("%s%s%s%s%s%s%s%s%s\n",
-                       $this->fileHeader,
-                       $this->seperator,
+                       self::FILE_IO_FILE_HEADER_ID,
+                       self::FILE_IO_SEPERATOR,
                        $dataArray[0],
-                       $this->chunker,
+                       self::FILE_IO_CHUNKER,
                        time(),
-                       $this->chunker,
+                       self::FILE_IO_CHUNKER,
                        strlen($dataArray[1]),
-                       $this->chunker,
+                       self::FILE_IO_CHUNKER,
                        md5($dataArray[1])
                ));
 
@@ -116,10 +116,10 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil
 
                        // Save it to the stream
                        $fileInstance->writeToFile(sprintf("%s%s%s%s%s\n",
-                               $this->dataBlock,
-                               $this->seperator,
+                               self::FILE_IO_DATA_BLOCK_ID,
+                               self::FILE_IO_SEPERATOR,
                                $line,
-                               $this->chunker,
+                               self::FILE_IO_CHUNKER,
                                md5($line)
                        ));
 
@@ -173,26 +173,26 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil
                        $rawLine = rtrim($rawLine);
 
                        // Analyze this line
-                       if (substr($rawLine, 0, 5) == $this->fileHeader) {
+                       if (substr($rawLine, 0, 5) == self::FILE_IO_FILE_HEADER_ID) {
                                // Header found, so let's extract it
-                               $header = explode($this->seperator, $rawLine);
+                               $header = explode(self::FILE_IO_SEPERATOR, $rawLine);
                                $header = trim($header[1]);
 
                                // Now we must convert it again into an array
-                               $header = explode($this->chunker, $header);
+                               $header = explode(self::FILE_IO_CHUNKER, $header);
 
                                // Is the header (maybe) valid?
                                if (count($header) != 4) {
                                        // Throw an exception
                                        throw new InvalidArrayCountException(array($this, 'header', count($header), 4), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
                                } // END - if
-                       } elseif (substr($rawLine, 0, 5) == $this->dataBlock) {
+                       } elseif (substr($rawLine, 0, 5) == self::FILE_IO_DATA_BLOCK_ID) {
                                // Is a data line!
-                               $data = explode($this->seperator, $rawLine);
+                               $data = explode(self::FILE_IO_SEPERATOR, $rawLine);
                                $data = $data[1];
 
                                // First element is the data, second the MD5 checksum
-                               $data = explode($this->chunker, $data);
+                               $data = explode(self::FILE_IO_CHUNKER, $data);
 
                                // Validate the read line
                                if (count($data) == 2) {
index 829f35b..e5fb3c7 100644 (file)
@@ -25,7 +25,7 @@ class McryptStream extends BaseStream implements EncryptableStream {
        /**
         * Seperator on many places
         */
-       private $seperator = '|';
+       const DATA_PAYLOAD_SEPERATOR = '|';
 
        /**
         * Protected constructor
@@ -72,47 +72,47 @@ class McryptStream extends BaseStream implements EncryptableStream {
                        $key = $this->getRngInstance()->generateKey();
                } // END - if
 
-               // Add some "garbage" to the string
+               // Add some "payload" to the string
                switch ($this->getRngInstance()->randomNumber(0, 8)) {
                        case 0:
-                               $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . crc32($this->getRngInstance()->randomString(20));
                                break;
 
                        case 1:
-                               $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . md5($this->getRngInstance()->randomString(20));
                                break;
 
                        case 2:
-                               $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . sha1($this->getRngInstance()->randomString(20));
                                break;
 
                        case 3:
-                               $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . crc32($this->getRngInstance()->randomString(20));
                                break;
 
                        case 4:
-                               $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . md5($this->getRngInstance()->randomString(20));
                                break;
 
                        case 5:
-                               $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . sha1($this->getRngInstance()->randomString(20));
                                break;
 
                        case 6:
-                               $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . crc32($this->getRngInstance()->randomString(20));
                                break;
 
                        case 7:
-                               $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . md5($this->getRngInstance()->randomString(20));
                                break;
 
                        case 8:
-                               $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . sha1($this->getRngInstance()->randomString(20));
                                break;
                }
 
                // Encrypt the string
-               $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $garbageString, MCRYPT_MODE_ECB, $iv);
+               $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
 
                // Return the string
                return $encrypted;
@@ -122,21 +122,25 @@ class McryptStream extends BaseStream implements EncryptableStream {
         * Decrypt the string with fixed salt
         *
         * @param       $encrypted      Encrypted string
+        * @param       $key            Optional key, if none provided, a random key will be generated
         * @return      $str            The unencrypted string
         */
-       public function decryptStream ($encrypted) {
+       public function decryptStream ($encrypted, $key = null) {
                // Init crypto module
                $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
                $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
 
-               // Generate key
-               $key = $this->getRngInstance()->generateKey();
+               // Shall we use a default key or custom?
+               if (is_null($key) {
+                       // Generate (default) key
+                       $key = $this->getRngInstance()->generateKey();
+               } // END - if
 
                // Decrypt the string
-               $garbageString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
+               $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
 
                // Get the real string out
-               $strArray = explode($this->seperator, $garbageString);
+               $strArray = explode(self::DATA_PAYLOAD_SEPERATOR, $payloadString);
 
                // Does the element count match?
                assert(count($strArray) == 3);