+
+ /**
+ * Checks wether the given PHP extension is loaded
+ *
+ * @param $phpExtension The PHP extension we shall check
+ * @return $isLoaded Wether the PHP extension is loaded
+ */
+ public final function isPhpExtensionLoaded ($phpExtension) {
+ // Is it loaded?
+ $isLoaded = in_array($phpExtension, get_loaded_extensions());
+
+ // Return result
+ return $isLoaded;
+ }
+
+ /**
+ * Setter for RNG instance
+ *
+ * @param $rngInstance An instance of a random number generator (RNG)
+ * @return void
+ */
+ protected final function setRngInstance (RandomNumberGenerator $rngInstance) {
+ $this->rngInstance = $rngInstance;
+ }
+
+ /**
+ * Getter for RNG instance
+ *
+ * @return $rngInstance An instance of a random number generator (RNG)
+ */
+ public final function getRngInstance () {
+ return $this->rngInstance;
+ }
+
+ /**
+ * Setter for Cryptable instance
+ *
+ * @param $cryptoInstance An instance of a Cryptable class
+ * @return void
+ */
+ protected final function setCryptoInstance (Cryptable $cryptoInstance) {
+ $this->cryptoInstance = $cryptoInstance;
+ }
+
+ /**
+ * Getter for Cryptable instance
+ *
+ * @return $cryptoInstance An instance of a Cryptable class
+ */
+ public final function getCryptoInstance () {
+ return $this->cryptoInstance;
+ }
+
+ /**
+ * Setter for Iterator instance
+ *
+ * @param $iteratorInstance An instance of an Iterator
+ * @return void
+ */
+ protected final function setIteratorInstance (Iterator $iteratorInstance) {
+ $this->iteratorInstance = $iteratorInstance;
+ }
+
+ /**
+ * Getter for Iterator instance
+ *
+ * @return $iteratorInstance An instance of an Iterator
+ */
+ public final function getIteratorInstance () {
+ return $this->iteratorInstance;
+ }
+
+ /**
+ * "Getter" as a time() replacement but with milliseconds. You should use this
+ * method instead of the encapsulated getimeofday() function.
+ *
+ * @return $milliTime Timestamp with milliseconds
+ */
+ public function getMilliTime () {
+ // Get the time of day as float
+ $milliTime = gettimeofday(true);
+
+ // Return it
+ return $milliTime;
+ }
+
+ /**
+ * Idles (sleeps) for given milliseconds
+ *
+ * @return $hasSlept Wether it goes fine
+ */
+ public function idle ($milliSeconds) {
+ // Sleep is fine by default
+ $hasSlept = true;
+
+ // Idle so long with found function
+ if (function_exists('time_sleep_until')) {
+ // Get current time and add idle time
+ $sleepUntil = $this->getMilliTime() + abs($milliSeconds) / 1000;
+
+ // New PHP 5.1.0 function found, ignore errors
+ $hasSlept = @time_sleep_until($sleepUntil);
+ } else {
+ // My Sun Station doesn't have that function even with latest PHP
+ // package. :(
+ usleep($milliSeconds * 1000);
+ }
+
+ // Return result
+ return $hasSlept;
+ }
+
+ /**
+ * Setter for the list instance
+ *
+ * @param $listInstance A list of Listable
+ * @return void
+ */
+ protected final function setListInstance (Listable $listInstance) {
+ $this->listInstance = $listInstance;
+ }
+
+ /**
+ * Getter for the list instance
+ *
+ * @return $listInstance A list of Listable
+ */
+ protected final function getListInstance () {
+ return $this->listInstance;
+ }
+
+ /**
+ * Setter for the menu instance
+ *
+ * @param $menuInstance A RenderableMenu instance
+ * @return void
+ */
+ protected final function setMenuInstance (RenderableMenu $menuInstance) {
+ $this->menuInstance = $menuInstance;
+ }
+
+ /**
+ * Getter for the menu instance
+ *
+ * @return $menuInstance A RenderableMenu instance
+ */
+ protected final function getMenuInstance () {
+ return $this->menuInstance;
+ }
+
+ /**
+ * Setter for image instance
+ *
+ * @param $imageInstance An instance of an image
+ * @return void
+ */
+ public final function setImageInstance (BaseImage $imageInstance) {
+ $this->imageInstance = $imageInstance;
+ }
+
+ /**
+ * Getter for image instance
+ *
+ * @return $imageInstance An instance of an image
+ */
+ public final function getImageInstance () {
+ return $this->imageInstance;
+ }
+
+ /**
+ * Setter for stacker instance
+ *
+ * @param $stackerInstance An instance of an stacker
+ * @return void
+ */
+ public final function setStackerInstance (Stackable $stackerInstance) {
+ $this->stackerInstance = $stackerInstance;
+ }
+
+ /**
+ * Getter for stacker instance
+ *
+ * @return $stackerInstance An instance of an stacker
+ */
+ public final function getStackerInstance () {
+ return $this->stackerInstance;
+ }
+
+ /**
+ * Setter for compressor instance
+ *
+ * @param $compressorInstance An instance of an compressor
+ * @return void
+ */
+ public final function setCompressorInstance (Compressor $compressorInstance) {
+ $this->compressorInstance = $compressorInstance;
+ }
+
+ /**
+ * Getter for compressor instance
+ *
+ * @return $compressorInstance An instance of an compressor
+ */
+ public final function getCompressorInstance () {
+ return $this->compressorInstance;
+ }
+
+ /**
+ * Setter for Parseable instance
+ *
+ * @param $parserInstance An instance of an Parseable
+ * @return void
+ */
+ public final function setParserInstance (Parseable $parserInstance) {
+ $this->parserInstance = $parserInstance;
+ }
+
+ /**
+ * Getter for Parseable instance
+ *
+ * @return $parserInstance An instance of an Parseable
+ */
+ public final function getParserInstance () {
+ return $this->parserInstance;
+ }
+
+ /**
+ * Setter for ProtocolHandler instance
+ *
+ * @param $protocolInstance An instance of an ProtocolHandler
+ * @return void
+ */
+ public final function setProtocolInstance (ProtocolHandler $protocolInstance = NULL) {
+ $this->protocolInstance = $protocolInstance;
+ }
+
+ /**
+ * Getter for ProtocolHandler instance
+ *
+ * @return $protocolInstance An instance of an ProtocolHandler
+ */
+ public final function getProtocolInstance () {
+ return $this->protocolInstance;
+ }
+
+ /**
+ * Setter for BaseDatabaseWrapper instance
+ *
+ * @param $wrapperInstance An instance of an BaseDatabaseWrapper
+ * @return void
+ */
+ public final function setWrapperInstance (BaseDatabaseWrapper $wrapperInstance) {
+ $this->wrapperInstance = $wrapperInstance;
+ }
+
+ /**
+ * Getter for BaseDatabaseWrapper instance
+ *
+ * @return $wrapperInstance An instance of an BaseDatabaseWrapper
+ */
+ public final function getWrapperInstance () {
+ return $this->wrapperInstance;
+ }
+
+ /**
+ * Setter for socket resource
+ *
+ * @param $socketResource A valid socket resource
+ * @return void
+ */
+ public final function setSocketResource ($socketResource) {
+ $this->socketResource = $socketResource;
+ }
+
+ /**
+ * Getter for socket resource
+ *
+ * @return $socketResource A valid socket resource
+ */
+ public function getSocketResource () {
+ return $this->socketResource;
+ }
+
+ /**
+ * Setter for helper instance
+ *
+ * @param $helperInstance An instance of a helper class
+ * @return void
+ */
+ protected final function setHelperInstance (Helper $helperInstance) {
+ $this->helperInstance = $helperInstance;
+ }
+
+ /**
+ * Getter for helper instance
+ *
+ * @return $helperInstance An instance of a helper class
+ */
+ public final function getHelperInstance () {
+ return $this->helperInstance;
+ }
+
+ /**
+ * Setter for a Sourceable instance
+ *
+ * @param $sourceInstance The Sourceable instance
+ * @return void
+ */
+ protected final function setSourceInstance (Sourceable $sourceInstance) {
+ $this->sourceInstance = $sourceInstance;
+ }
+
+ /**
+ * Getter for a Sourceable instance
+ *
+ * @return $sourceInstance The Sourceable instance
+ */
+ protected final function getSourceInstance () {
+ return $this->sourceInstance;
+ }
+
+ /**
+ * Getter for a InputStreamable instance
+ *
+ * @param $inputStreamInstance The InputStreamable instance
+ */
+ protected final function getInputStreamInstance () {
+ return $this->inputStreamInstance;
+ }
+
+ /**
+ * Setter for a InputStreamable instance
+ *
+ * @param $inputStreamInstance The InputStreamable instance
+ * @return void
+ */
+ protected final function setInputStreamInstance (InputStreamable $inputStreamInstance) {
+ $this->inputStreamInstance = $inputStreamInstance;
+ }
+
+ /**
+ * Getter for a OutputStreamable instance
+ *
+ * @param $outputStreamInstance The OutputStreamable instance
+ */
+ protected final function getOutputStreamInstance () {
+ return $this->outputStreamInstance;
+ }
+
+ /**
+ * Setter for a OutputStreamable instance
+ *
+ * @param $outputStreamInstance The OutputStreamable instance
+ * @return void
+ */
+ protected final function setOutputStreamInstance (OutputStreamable $outputStreamInstance) {
+ $this->outputStreamInstance = $outputStreamInstance;
+ }
+
+ /**
+ * Setter for handler instance
+ *
+ * @param $handlerInstance A Networkable instance
+ * @return void
+ */
+ protected final function setHandlerInstance (Networkable $handlerInstance) {
+ $this->handlerInstance = $handlerInstance;
+ }
+
+ /**
+ * Getter for handler instance
+ *
+ * @return $handlerInstance A Networkable instance
+ */
+ protected final function getHandlerInstance () {
+ return $this->handlerInstance;
+ }
+
+ /**
+ * Setter for visitor instance
+ *
+ * @param $visitorInstance A Visitor instance
+ * @return void
+ */
+ protected final function setVisitorInstance (Visitor $visitorInstance) {
+ $this->visitorInstance = $visitorInstance;
+ }
+
+ /**
+ * Getter for visitor instance
+ *
+ * @return $visitorInstance A Visitor instance
+ */
+ protected final function getVisitorInstance () {
+ return $this->visitorInstance;
+ }
+
+ /**
+ * Setter for raw package Data
+ *
+ * @param $packageData Raw package Data
+ * @return void
+ */
+ public final function setPackageData (array $packageData) {
+ $this->packageData = $packageData;
+ }
+
+ /**
+ * Getter for raw package Data
+ *
+ * @return $packageData Raw package Data
+ */
+ public function getPackageData () {
+ return $this->packageData;
+ }
+
+ /**
+ * Converts a hexadecimal string, even with negative sign as first string to
+ * a decimal number using BC functions.
+ *
+ * This work is based on comment #86673 on php.net documentation page at:
+ * <http://de.php.net/manual/en/function.dechex.php#86673>
+ *
+ * @param $hex Hexadecimal string
+ * @return $dec Decimal number
+ */
+ protected function hex2dec ($hex) {
+ // Convert to all lower-case
+ $hex = strtolower($hex);
+
+ // Detect sign (negative/positive numbers)
+ $sign = '';
+ if (substr($hex, 0, 1) == '-') {
+ $sign = '-';
+ $hex = substr($hex, 1);
+ } // END - if
+
+ // Decode the hexadecimal string into a decimal number
+ $dec = 0;
+ for ($i = strlen($hex) - 1, $e = 1; $i >= 0; $i--, $e = bcmul($e, 16)) {
+ $factor = self::$hexdec[substr($hex, $i, 1)];
+ $dec = bcadd($dec, bcmul($factor, $e));
+ } // END - for
+
+ // Return the decimal number
+ return $sign . $dec;
+ }
+
+ /**
+ * Converts even very large decimal numbers, also with negative sign, to a
+ * hexadecimal string.
+ *
+ * This work is based on comment #97756 on php.net documentation page at:
+ * <http://de.php.net/manual/en/function.hexdec.php#97756>
+ *
+ * @param $dec Decimal number, even with negative sign
+ * @param $maxLength Optional maximum length of the string
+ * @return $hex Hexadecimal string
+ */
+ protected function dec2hex ($dec, $maxLength = 0) {
+ // maxLength can be zero or devideable by 2
+ assert(($maxLength == 0) || (($maxLength % 2) == 0));
+
+ // Detect sign (negative/positive numbers)
+ $sign = '';
+ if ($dec < 0) {
+ $sign = '-';
+ $dec = abs($dec);
+ } // END - if
+
+ // Encode the decimal number into a hexadecimal string
+ $hex = '';
+ do {
+ $hex = self::$dechex[($dec % 16)] . $hex;
+ $dec /= 16;
+ } while ($dec >= 1);
+
+ /*
+ * We need hexadecimal strings with leading zeros if the length cannot
+ * be divided by 2
+ */
+ if ($maxLength > 0) {
+ // Prepend more zeros
+ $hex = $this->prependStringToString($hex, '0', $maxLength);
+ } elseif ((strlen($hex) % 2) != 0) {
+ $hex = '0' . $hex;
+ }
+
+ // Return the hexadecimal string
+ return $sign . $hex;
+ }
+
+ /**
+ * Converts a ASCII string (0 to 255) into a decimal number.
+ *
+ * @param $asc The ASCII string to be converted
+ * @return $dec Decimal number
+ */
+ protected function asc2dec ($asc) {
+ // Convert it into a hexadecimal number
+ $hex = bin2hex($asc);
+
+ // And back into a decimal number
+ $dec = $this->hex2dec($hex);
+
+ // Return it
+ return $dec;
+ }
+
+ /**
+ * Converts a decimal number into an ASCII string.
+ *
+ * @param $dec Decimal number
+ * @return $asc An ASCII string
+ */
+ protected function dec2asc ($dec) {
+ // First convert the number into a hexadecimal string
+ $hex = $this->dec2hex($dec);
+
+ // Then convert it into the ASCII string
+ $asc = $this->hex2asc($hex);
+
+ // Return it
+ return $asc;
+ }
+
+ /**
+ * Converts a hexadecimal number into an ASCII string. Negative numbers
+ * are not allowed.
+ *
+ * @param $hex Hexadecimal string
+ * @return $asc An ASCII string
+ */
+ protected function hex2asc ($hex) {
+ // Check for length, it must be devideable by 2
+ //* DEBUG: */ $this->debugOutput('hex='.$hex);
+ assert((strlen($hex) % 2) == 0);
+
+ // Walk the string
+ $asc = '';
+ for ($idx = 0; $idx < strlen($hex); $idx+=2) {
+ // Get the decimal number of the chunk
+ $part = hexdec(substr($hex, $idx, 2));
+
+ // Add it to the final string
+ $asc .= chr($part);
+ } // END - for
+
+ // Return the final string
+ return $asc;
+ }
+
+ /**
+ * Prepends a given string $prepend to $str with a given total length
+ *
+ * @param $str Given original string which should be prepended
+ * @param $prepend The string to prepend
+ * @param $length Total length of the final string
+ * @return $strFinal Final prepended string
+ */
+ protected function prependStringToString ($str, $prepend, $length) {
+ // Set final string to original string by default
+ $strFinal = $str;
+
+ // Can it devided
+ if (strlen($str) < $length) {
+ // Difference between total length and length of original string
+ $diff = $length - strlen($str);
+
+ // Prepend the string
+ $prepend = str_repeat($prepend, ($diff / strlen($prepend) + 1));
+
+ // Make sure it will definedly fit
+ assert(strlen($prepend) >= $diff);
+
+ // Cut it a little down
+ $prepend = substr($prepend, 0, $diff);
+ //* DEBUG: */ $this->debugOutput('prepend('.strlen($prepend).')='.$prepend.',diff='.$diff.',length='.$length);
+
+ // Construct the final prepended string
+ $strFinal = $prepend . $str;
+ } // END - if
+
+ // Return it
+ return $strFinal;
+ }
+
+ /**
+ * Checks wether the given encoded data was encoded with Base64
+ *
+ * @param $encodedData Encoded data we shall check
+ * @return $isBase64 Wether the encoded data is Base64
+ */
+ protected function isBase64Encoded ($encodedData) {
+ // Determine it
+ $isBase64 = (@base64_decode($encodedData, true) !== false);
+
+ // Return it
+ return $isBase64;
+ }