Added hexval() which really stupid regex
[core.git] / inc / classes / main / class_BaseFrameworkSystem.php
index 370e2ff0f4baa6de2ebb75aa63686c53bf449234..4b2812dc41adfc530e6fbbb9fe106b0d69f71dc4 100644 (file)
@@ -5,7 +5,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -1004,6 +1004,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @return      void
         */
        public final function setSocketResource ($socketResource) {
+               //* NOISY-DEBUG: */ $this->debugOutput($this->__toString() . '::' . __FUNCTION__ . ': socketResource=' . $socketResource . ',previous[' . gettype($this->socketResource) . ']=' . $this->socketResource);
                $this->socketResource = $socketResource;
        }
 
@@ -1012,7 +1013,8 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         *
         * @return      $socketResource         A valid socket resource
         */
-       public function getSocketResource () {
+       public final function getSocketResource () {
+               //* NOISY-DEBUG: */ $this->debugOutput($this->__toString() . '::' . __FUNCTION__ . ': socketResource[' . gettype($this->socketResource) . ']=' . $this->socketResource);
                return $this->socketResource;
        }
 
@@ -1346,7 +1348,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                } // END - if
 
                // Construct the full message
-               $stubMessage = sprintf("[%s:] Partial stub!",
+               $stubMessage = sprintf('[%s:] Partial stub!',
                        $methodName
                );
 
@@ -1377,7 +1379,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                // Sorry, there is no other way getting this nice backtrace
                if (!empty($message)) {
                        // Output message
-                       printf("Message: %s<br />" . chr(10), $message);
+                       printf('Message: %s<br />' . chr(10), $message);
                } // END - if
 
                print('<pre>');
@@ -1398,8 +1400,16 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @return      void
         */
        public function debugOutput ($message, $doPrint = true) {
-               // Get debug instance
-               $debugInstance = $this->getDebugInstance();
+               // Set debug instance to NULL
+               $debugInstance = NULL;
+
+               // Try it:
+               try {
+                       // Get debug instance
+                       $debugInstance = $this->getDebugInstance();
+               } catch (NullPointerException $e) {
+                       // The debug instance is not set (yet)
+               }
 
                // Is the debug instance there?
                if (is_object($debugInstance)) {
@@ -1422,7 +1432,10 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                                // Print message
                                print($message . chr(10));
                        } else {
-                               // DO NOT REWRITE THIS TO app_die() !!!
+                               /*
+                                * BIG FAT NOTE: Do NEVER rewrite this to app_die(), this will
+                                * cause an endless loop.
+                                */
                                die($message);
                        }
                }
@@ -1625,11 +1638,17 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                $fieldArray = $resultInstance->current();
                //* DEBUG: */ $this->debugOutput($fieldName.':<pre>'.print_r($fieldArray, true).'</pre>');
 
+               // Convert dashes to underscore
+               $fieldName = $this->convertDashesToUnderscores($fieldName);
+
                // Does the field exist?
                if (isset($fieldArray[$fieldName])) {
                        // Get it
                        $fieldValue = $fieldArray[$fieldName];
-               } // END - if
+               } else {
+                       // Missing field entry, may require debugging
+                       $this->debugOutput($this->__toString() . ':fieldname=' . $fieldName . ' not found!');
+               }
 
                // Return it
                return $fieldValue;
@@ -2025,6 +2044,27 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                // Return result
                return $ret;
        }
+
+       /**
+        * Checks whether the given hexadecimal number is really a hex-number (only chars 0-9,a-f).
+        *
+        * @param       $num    A string consisting only chars between 0 and 9
+        * @param       $assertMismatch         Whether to assert mismatches
+        * @return      $ret    The (hopefully) secured hext-numbered value
+        */
+       public function hexval ($num, $assertMismatch = false) {
+               // Filter all numbers out
+               $ret = preg_replace('/[^0123456789abcdefABCDEF]/', '', $num);
+
+               // Assert only if requested
+               if ($assertMismatch === true) {
+                       // Has the whole value changed?
+                       assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
+               } // END - if
+
+               // Return result
+               return $ret;
+       }
 }
 
 // [EOF]