Used exit() (also app_die()->app_exit()) because die() is an alias of exit nowadays
[core.git] / inc / classes / main / class_BaseFrameworkSystem.php
index 370e2ff0f4baa6de2ebb75aa63686c53bf449234..da54f83abacc82a7339912022a82e635adaaf7d9 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
  *
@@ -328,7 +328,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         *
         * @return      void
         */
-       public function __destruct() {
+       public function __destruct () {
                // Flush any updated entries to the database
                $this->flushPendingUpdates();
 
@@ -338,7 +338,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                        $this->setRealClass('DestructedObject');
                } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) {
                        // Already destructed object
-                       $this->debugOutput(sprintf("[%s:] The object <span class=\"object_name\">%s</span> is already destroyed.",
+                       self::createDebugInstance(__CLASS__)->debugOutput(sprintf("[%s:] The object <span class=\"object_name\">%s</span> is already destroyed.",
                                __CLASS__,
                                $this->__toString()
                        ));
@@ -353,6 +353,17 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @return      void
         */
        public final function __call ($methodName, $args) {
+               return self::__callStatic($methodName, $args);
+       }
+
+       /**
+        * The __callStatic() method where all non-implemented static methods end up
+        *
+        * @param       $methodName             Name of the missing method
+        * @args        $args                   Arguments passed to the method
+        * @return      void
+        */
+       public static final function __callStatic ($methodName, $args) {
                // Implode all given arguments
                $argsString = '';
                if (empty($args)) {
@@ -373,10 +384,10 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
 
                                if (is_string($arg)) {
                                        // Add length for strings
-                                       $argsString .= ', '.strlen($arg);
+                                       $argsString .= ', ' . strlen($arg);
                                } elseif (is_array($arg)) {
                                        // .. or size if array
-                                       $argsString .= ', '.count($arg);
+                                       $argsString .= ', ' . count($arg);
                                } elseif ($arg === true) {
                                        // ... is boolean 'true'
                                        $argsString .= ', true';
@@ -399,8 +410,8 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                }
 
                // Output stub message
-               $this->debugOutput(sprintf("[%s-&gt;%s] Stub! Args: %s",
-                       $this->__toString(),
+               // @TODO __CLASS__ does always return BaseFrameworkSystem but not the extending (=child) class
+               self::createDebugInstance(__CLASS__)->debugOutput(sprintf("[unknown::%s:] Stub! Args: %s",
                        $methodName,
                        $argsString
                ));
@@ -1004,6 +1015,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @return      void
         */
        public final function setSocketResource ($socketResource) {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . '::' . __FUNCTION__ . ': socketResource=' . $socketResource . ',previous[' . gettype($this->socketResource) . ']=' . $this->socketResource);
                $this->socketResource = $socketResource;
        }
 
@@ -1012,7 +1024,8 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         *
         * @return      $socketResource         A valid socket resource
         */
-       public function getSocketResource () {
+       public final function getSocketResource () {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . '::' . __FUNCTION__ . ': socketResource[' . gettype($this->socketResource) . ']=' . $this->socketResource);
                return $this->socketResource;
        }
 
@@ -1303,7 +1316,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                );
 
                // Output it
-               ApplicationEntryPoint::app_die(sprintf("<div class=\"debug_header\">%s debug output:</div><div class=\"debug_content\">%s</div>\nLoaded includes: <div class=\"debug_include_list\">%s</div>",
+               ApplicationEntryPoint::app_exit(sprintf("<div class=\"debug_header\">%s debug output:</div><div class=\"debug_content\">%s</div>\nLoaded includes: <div class=\"debug_include_list\">%s</div>",
                        $this->__toString(),
                        $content,
                        ClassLoader::getSelfInstance()->getPrintableIncludeList()
@@ -1346,23 +1359,23 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                } // END - if
 
                // Construct the full message
-               $stubMessage = sprintf("[%s:] Partial stub!",
+               $stubMessage = sprintf('[%s:] Partial stub!',
                        $methodName
                );
 
                // Is the extra message given?
                if (!empty($message)) {
                        // Then add it as well
-                       $stubMessage .= sprintf(' Message: <span id="stub_message">%s</span>', $message);
+                       $stubMessage .= ' Message: ' . $message;
                } // END - if
 
                // Debug instance is there?
                if (!is_null($this->getDebugInstance())) {
                        // Output stub message
-                       $this->debugOutput($stubMessage);
+                       self::createDebugInstance(__CLASS__)->debugOutput($stubMessage);
                } else {
                        // Trigger an error
-                       trigger_error($stubMessage . '<br />' . chr(10));
+                       trigger_error($stubMessage);
                }
        }
 
@@ -1377,7 +1390,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>');
@@ -1391,39 +1404,76 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        }
 
        /**
-        * Outputs a debug message whether to debug instance (should be set!) or dies with or pints the message
+        * Creates an instance of a debugger instance
+        *
+        * @param       $className              Name of the class (currently unsupported)
+        * @return      $debugInstance  An instance of a debugger class
+        */
+       public final static function createDebugInstance ($className) {
+               // Init debug instance
+               $debugInstance = NULL;
+
+               // Try it
+               try {
+                       // Get a debugger instance
+                       $debugInstance = DebugMiddleware::createDebugMiddleware(FrameworkConfiguration::getSelfInstance()->getConfigEntry('debug_class'));
+               } catch (NullPointerException $e) {
+                       // Didn't work, no instance there
+                       exit('Cannot create debugInstance! Exception=' . $e->__toString() . ', message=' . $e->getMessage());
+               }
+
+               // Empty string should be ignored and used for testing the middleware
+               DebugMiddleware::getSelfInstance()->output('');
+
+               // Return it
+               return $debugInstance;
+       }
+
+       /**
+        * Outputs a debug message whether to debug instance (should be set!) or
+        * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
+        * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
         *
         * @param       $message        Message we shall send out...
-        * @param       $doPrint        Whether we shall print or die here which first is the default
+        * @param       $doPrint        Whether print or die here (default: print)
+        * @paran       $stripTags      Whether to strip tags (default: false)
         * @return      void
         */
-       public function debugOutput ($message, $doPrint = true) {
-               // Get debug instance
-               $debugInstance = $this->getDebugInstance();
+       public function debugOutput ($message, $doPrint = true, $stripTags = false) {
+               // 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)) {
                        // Use debug output handler
-                       $debugInstance->output($message);
+                       $debugInstance->output($message, $stripTags);
 
                        if ($doPrint === false) {
                                // Die here if not printed
-                               die();
+                               exit();
                        } // END - if
                } else {
+                       // Are debug times enabled?
+                       if ($this->getConfigInstance()->getConfigEntry('debug_output_timings') == 'Y') {
+                               // Prepent it
+                               $message = $this->getPrintableExecutionTime() . $message;
+                       } // END - if
+
                        // Put directly out
                        if ($doPrint === true) {
-                               // Are debug times enabled?
-                               if ($this->getConfigInstance()->getConfigEntry('debug_output_timings') == 'Y') {
-                                       // Output it first
-                                       print($this->getPrintableExecutionTime());
-                               } // END - if
-
                                // Print message
                                print($message . chr(10));
                        } else {
-                               // DO NOT REWRITE THIS TO app_die() !!!
-                               die($message);
+                               // Die here
+                               exit($message);
                        }
                }
        }
@@ -1623,13 +1673,19 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
 
                // Get current array
                $fieldArray = $resultInstance->current();
-               //* DEBUG: */ $this->debugOutput($fieldName.':<pre>'.print_r($fieldArray, true).'</pre>');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->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
+                       self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . ':fieldname=' . $fieldName . ' not found!');
+               }
 
                // Return it
                return $fieldValue;
@@ -1670,7 +1726,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                        // Debug instance is there?
                        if (!is_null($this->getDebugInstance())) {
                                // Output stub message
-                               $this->debugOutput($message);
+                               self::createDebugInstance(__CLASS__)->debugOutput($message);
                        } else {
                                // Trigger an error
                                trigger_error($message . "<br />\n");
@@ -1855,7 +1911,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        protected function hex2asc ($hex) {
                // Check for length, it must be devideable by 2
-               //* DEBUG: */ $this->debugOutput('hex='.$hex);
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('hex='.$hex);
                assert((strlen($hex) % 2) == 0);
 
                // Walk the string
@@ -1897,7 +1953,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
 
                        // Cut it a little down
                        $prepend = substr($prepend, 0, $diff);
-                       //* DEBUG: */ $this->debugOutput('prepend('.strlen($prepend).')='.$prepend.',diff='.$diff.',length='.$length);
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('prepend('.strlen($prepend).')='.$prepend.',diff='.$diff.',length='.$length);
 
                        // Construct the final prepended string
                        $strFinal = $prepend . $str;
@@ -1955,7 +2011,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                );
 
                // And return it
-               //* NOISY-DEBUG: */ $this->debugOutput($this->__toString() . ': cacheKey=' . $cacheKey);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . ': cacheKey=' . $cacheKey);
                return $cacheKey;
        }
 
@@ -2025,6 +2081,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]