3 namespace CoreFramework\Object;
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Controller\Controller;
8 use CoreFramework\Criteria\Criteria;
9 use CoreFramework\Criteria\Local\LocalSearchCriteria;
10 use CoreFramework\Factory\Database\Wrapper\DatabaseWrapperFactory;
11 use CoreFramework\Factory\ObjectFactory;
12 use CoreFramework\Generic\FrameworkInterface;
13 use CoreFramework\Generic\NullPointerException;
14 use CoreFramework\Handler\Stream\IoHandler;
15 use CoreFramework\Loader\ClassLoader;
16 use CoreFramework\Manager\ManageableApplication;
17 use CoreFramework\Middleware\Debug\DebugMiddleware;
18 use CoreFramework\Registry\Register;
19 use CoreFramework\Registry\Registry;
20 use CoreFramework\Request\Requestable;
21 use CoreFramework\Resolver\Resolver;
22 use CoreFramework\Response\Responseable;
23 use CoreFramework\Stream\Output\OutputStreamer;
24 use CoreFramework\Template\CompileableTemplate;
30 * The simulator system class is the super class of all other classes. This
31 * class handles saving of games etc.
33 * @author Roland Haeder <webmaster@shipsimu.org>
35 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
36 * @license GNU GPL 3.0 or any newer version
37 * @link http://www.shipsimu.org
39 * This program is free software: you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation, either version 3 of the License, or
42 * (at your option) any later version.
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
49 * You should have received a copy of the GNU General Public License
50 * along with this program. If not, see <http://www.gnu.org/licenses/>.
52 class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
54 * Length of output from hash()
56 private static $hashLength = NULL;
61 private $realClass = 'BaseFrameworkSystem';
64 * Instance of a Requestable class
66 private $requestInstance = NULL;
69 * Instance of a Responseable class
71 private $responseInstance = NULL;
74 * Search criteria instance
76 private $searchInstance = NULL;
79 * Update criteria instance
81 private $updateInstance = NULL;
84 * The file I/O instance for the template loader
86 private $fileIoInstance = NULL;
91 private $resolverInstance = NULL;
94 * Template engine instance
96 private $templateInstance = NULL;
99 * Database result instance
101 private $resultInstance = NULL;
104 * Instance for user class
106 private $userInstance = NULL;
109 * A controller instance
111 private $controllerInstance = NULL;
116 private $rngInstance = NULL;
119 * Instance of a crypto helper
121 private $cryptoInstance = NULL;
124 * Instance of an Iterator class
126 private $iteratorInstance = NULL;
129 * Instance of the list
131 private $listInstance = NULL;
136 private $menuInstance = NULL;
139 * Instance of the image
141 private $imageInstance = NULL;
144 * Instance of the stacker
146 private $stackInstance = NULL;
149 * A Compressor instance
151 private $compressorInstance = NULL;
154 * A Parseable instance
156 private $parserInstance = NULL;
159 * A HandleableProtocol instance
161 private $protocolInstance = NULL;
164 * A database wrapper instance
166 private $databaseInstance = NULL;
169 * A helper instance for the form
171 private $helperInstance = NULL;
174 * An instance of a Source class
176 private $sourceInstance = NULL;
179 * An instance of a UrlSource class
181 private $urlSourceInstance = NULL;
184 * An instance of a InputStream class
186 private $inputStreamInstance = NULL;
189 * An instance of a OutputStream class
191 private $outputStreamInstance = NULL;
194 * Networkable handler instance
196 private $handlerInstance = NULL;
199 * Visitor handler instance
201 private $visitorInstance = NULL;
206 private $dhtInstance = NULL;
209 * An instance of a database wrapper class
211 private $wrapperInstance = NULL;
214 * An instance of a file I/O pointer class (not handler)
216 private $pointerInstance = NULL;
219 * An instance of an Indexable class
221 private $indexInstance = NULL;
224 * An instance of a Block class
226 private $blockInstance = NULL;
231 private $minableInstance = NULL;
234 * A FrameworkDirectory instance
236 private $directoryInstance = NULL;
241 private $listenerInstance = NULL;
244 * An instance of a communicator
246 private $communicatorInstance = NULL;
249 * The concrete output instance
251 private $outputInstance = NULL;
256 private $stateInstance = NULL;
259 * Thousands separator
261 private $thousands = '.'; // German
266 private $decimals = ','; // German
271 private $socketResource = FALSE;
274 * Regular expression to use for validation
276 private $regularExpression = '';
281 private $packageData = array();
286 private $genericArray = array();
291 private $commandName = '';
296 private $controllerName = '';
299 * Name of used protocol
301 private $protocolName = 'invalid';
304 * Array with bitmasks and such for pack/unpack methods to support both
305 * 32-bit and 64-bit systems
307 private $packingData = array(
310 'left' => 0xffff0000,
311 'right' => 0x0000ffff,
317 'left' => 0xffffffff00000000,
318 'right' => 0x00000000ffffffff,
325 * Simple 64-bit check, thanks to "Salman A" from stackoverflow.com:
327 * The integer size is 4 bytes on 32-bit and 8 bytes on a 64-bit system.
329 private $archArrayElement = FALSE;
331 /***********************
332 * Exception codes.... *
333 ***********************/
335 // @todo Try to clean these constants up
336 const EXCEPTION_IS_NULL_POINTER = 0x001;
337 const EXCEPTION_IS_NO_OBJECT = 0x002;
338 const EXCEPTION_IS_NO_ARRAY = 0x003;
339 const EXCEPTION_MISSING_METHOD = 0x004;
340 const EXCEPTION_CLASSES_NOT_MATCHING = 0x005;
341 const EXCEPTION_INDEX_OUT_OF_BOUNDS = 0x006;
342 const EXCEPTION_DIMENSION_ARRAY_INVALID = 0x007;
343 const EXCEPTION_ITEM_NOT_TRADEABLE = 0x008;
344 const EXCEPTION_ITEM_NOT_IN_PRICE_LIST = 0x009;
345 const EXCEPTION_GENDER_IS_WRONG = 0x00a;
346 const EXCEPTION_BIRTH_DATE_IS_INVALID = 0x00b;
347 const EXCEPTION_EMPTY_STRUCTURES_ARRAY = 0x00c;
348 const EXCEPTION_HAS_ALREADY_PERSONELL_LIST = 0x00d;
349 const EXCEPTION_NOT_ENOUGTH_UNEMPLOYEES = 0x00e;
350 const EXCEPTION_TOTAL_PRICE_NOT_CALCULATED = 0x00f;
351 const EXCEPTION_HARBOR_HAS_NO_SHIPYARDS = 0x010;
352 const EXCEPTION_CONTRACT_PARTNER_INVALID = 0x011;
353 const EXCEPTION_CONTRACT_PARTNER_MISMATCH = 0x012;
354 const EXCEPTION_CONTRACT_ALREADY_SIGNED = 0x013;
355 const EXCEPTION_UNEXPECTED_EMPTY_STRING = 0x014;
356 const EXCEPTION_PATH_NOT_FOUND = 0x015;
357 const EXCEPTION_INVALID_PATH_NAME = 0x016;
358 const EXCEPTION_READ_PROTECED_PATH = 0x017;
359 const EXCEPTION_WRITE_PROTECED_PATH = 0x018;
360 const EXCEPTION_DIR_POINTER_INVALID = 0x019;
361 const EXCEPTION_FILE_POINTER_INVALID = 0x01a;
362 const EXCEPTION_INVALID_RESOURCE = 0x01b;
363 const EXCEPTION_UNEXPECTED_OBJECT = 0x01c;
364 const EXCEPTION_LIMIT_ELEMENT_IS_UNSUPPORTED = 0x01d;
365 const EXCEPTION_GETTER_IS_MISSING = 0x01e;
366 const EXCEPTION_ARRAY_EXPECTED = 0x01f;
367 const EXCEPTION_ARRAY_HAS_INVALID_COUNT = 0x020;
368 const EXCEPTION_ID_IS_INVALID_FORMAT = 0x021;
369 const EXCEPTION_MD5_CHECKSUMS_MISMATCH = 0x022;
370 const EXCEPTION_UNEXPECTED_STRING_SIZE = 0x023;
371 const EXCEPTION_SIMULATOR_ID_INVALID = 0x024;
372 const EXCEPTION_MISMATCHING_COMPRESSORS = 0x025;
373 const EXCEPTION_CONTAINER_ITEM_IS_NULL = 0x026;
374 const EXCEPTION_ITEM_IS_NO_ARRAY = 0x027;
375 const EXCEPTION_CONTAINER_MAYBE_DAMAGED = 0x028;
376 const EXCEPTION_INVALID_STRING = 0x029;
377 const EXCEPTION_VARIABLE_NOT_SET = 0x02a;
378 const EXCEPTION_ATTRIBUTES_ARE_MISSING = 0x02b;
379 const EXCEPTION_ARRAY_ELEMENTS_MISSING = 0x02c;
380 const EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED = 0x02d;
381 const EXCEPTION_UNSPPORTED_OPERATION = 0x02e;
382 const EXCEPTION_FACTORY_REQUIRE_PARAMETER = 0x02f;
383 const EXCEPTION_MISSING_ELEMENT = 0x030;
384 const EXCEPTION_HEADERS_ALREADY_SENT = 0x031;
385 const EXCEPTION_DEFAULT_CONTROLLER_GONE = 0x032;
386 const EXCEPTION_CLASS_NOT_FOUND = 0x033;
387 const EXCEPTION_REQUIRED_INTERFACE_MISSING = 0x034;
388 const EXCEPTION_FATAL_ERROR = 0x035;
389 const EXCEPTION_FILE_NOT_FOUND = 0x036;
390 const EXCEPTION_ASSERTION_FAILED = 0x037;
391 const EXCEPTION_FILE_NOT_REACHABLE = 0x038;
392 const EXCEPTION_FILE_CANNOT_BE_READ = 0x039;
393 const EXCEPTION_FILE_CANNOT_BE_WRITTEN = 0x03a;
394 const EXCEPTION_PATH_CANNOT_BE_WRITTEN = 0x03b;
395 const EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED = 0x03c;
396 const EXCEPTION_FILTER_CHAIN_INTERCEPTED = 0x03d;
399 * Hexadecimal->Decimal translation array
401 private static $hexdec = array(
421 * Decimal->hexadecimal translation array
423 private static $dechex = array(
443 * Startup time in miliseconds
445 private static $startupTime = 0;
448 * Protected super constructor
450 * @param $className Name of the class
453 protected function __construct ($className) {
455 $this->setRealClass($className);
457 // Set configuration instance if no registry ...
458 if (!$this instanceof Register) {
459 // ... because registries doesn't need to be configured
460 $this->setConfigInstance(FrameworkConfiguration::getSelfInstance());
463 // Is the startup time set? (0 cannot be TRUE anymore)
464 if (self::$startupTime == 0) {
466 self::$startupTime = microtime(TRUE);
470 $this->archArrayElement = (PHP_INT_SIZE === 8 ? 64 : 32);
474 * Destructor for all classes. You should not call this method on your own.
478 public function __destruct () {
479 // Flush any updated entries to the database
480 $this->flushPendingUpdates();
482 // Is this object already destroyed?
483 if ($this->__toString() != 'DestructedObject') {
484 // Destroy all informations about this class but keep some text about it alive
485 $this->setRealClass('DestructedObject');
486 } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) {
487 // Already destructed object
488 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] The object <span class="object_name">%s</span> is already destroyed.',
493 // Do not call this twice
494 trigger_error(__METHOD__ . ': Called twice.');
500 * The __call() method where all non-implemented methods end up
502 * @param $methodName Name of the missing method
503 * @args $args Arguments passed to the method
506 public final function __call ($methodName, $args) {
507 return self::__callStatic($methodName, $args);
511 * The __callStatic() method where all non-implemented static methods end up
513 * @param $methodName Name of the missing method
514 * @args $args Arguments passed to the method
517 public static final function __callStatic ($methodName, $args) {
519 //* PRINT-DEBUG: */ printf('[%s:%d]: methodName=%s,args[]=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $methodName, gettype($args));
521 // Init argument string
524 // Is it NULL, empty or an array?
525 if (is_null($args)) {
527 $argsString = 'NULL';
528 } elseif (is_array($args)) {
532 // Some arguments are there
533 foreach ($args as $arg) {
534 // Add data about the argument
535 $argsString .= gettype($arg) . ':';
538 // Found a NULL argument
539 $argsString .= 'NULL';
540 } elseif (is_string($arg)) {
541 // Add length for strings
542 $argsString .= strlen($arg);
543 } elseif ((is_int($arg)) || (is_float($arg))) {
546 } elseif (is_array($arg)) {
547 // .. or size if array
548 $argsString .= count($arg);
549 } elseif (is_object($arg)) {
551 $reflection = new ReflectionClass($arg);
553 // Is an other object, maybe no __toString() available
554 $argsString .= $reflection->getName();
555 } elseif ($arg === TRUE) {
556 // ... is boolean 'TRUE'
557 $argsString .= 'TRUE';
558 } elseif ($arg === FALSE) {
559 // ... is boolean 'FALSE'
560 $argsString .= 'FALSE';
563 // Comma for next one
568 if (substr($argsString, -2, 1) == ',') {
569 $argsString = substr($argsString, 0, -2);
575 // Invalid arguments!
576 $argsString = '!INVALID:' . gettype($args) . '!';
579 // Output stub message
580 // @TODO __CLASS__ does always return BaseFrameworkSystem but not the extending (=child) class
581 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[unknown::%s:] Stub! Args: %s',
591 * Getter for $realClass
593 * @return $realClass The name of the real class (not BaseFrameworkSystem)
595 public function __toString () {
596 return $this->realClass;
600 * Magic method to catch setting of missing but set class fields/attributes
602 * @param $name Name of the field/attribute
603 * @param $value Value to store
606 public final function __set ($name, $value) {
607 $this->debugBackTrace(sprintf('Tried to set a missing field. name=%s, value[%s]=%s',
610 print_r($value, TRUE)
615 * Magic method to catch getting of missing fields/attributes
617 * @param $name Name of the field/attribute
620 public final function __get ($name) {
621 $this->debugBackTrace(sprintf('Tried to get a missing field. name=%s',
627 * Magic method to catch unsetting of missing fields/attributes
629 * @param $name Name of the field/attribute
632 public final function __unset ($name) {
633 $this->debugBackTrace(sprintf('Tried to unset a missing field. name=%s',
639 * Magic method to catch object serialization
641 * @return $unsupported Unsupported method
642 * @throws UnsupportedOperationException Objects of this framework cannot be serialized
644 public final function __sleep () {
645 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
649 * Magic method to catch object deserialization
651 * @return $unsupported Unsupported method
652 * @throws UnsupportedOperationException Objects of this framework cannot be serialized
654 public final function __wakeup () {
655 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
659 * Magic method to catch calls when an object instance is called
661 * @return $unsupported Unsupported method
662 * @throws UnsupportedOperationException Objects of this framework cannot be serialized
664 public final function __invoke () {
665 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
669 * Setter for the real class name
671 * @param $realClass Class name (string)
674 public final function setRealClass ($realClass) {
676 $this->realClass = (string) $realClass;
680 * Setter for database result instance
682 * @param $resultInstance An instance of a database result class
684 * @todo SearchableResult and UpdateableResult shall have a super interface to use here
686 protected final function setResultInstance (SearchableResult $resultInstance) {
687 $this->resultInstance = $resultInstance;
691 * Getter for database result instance
693 * @return $resultInstance An instance of a database result class
695 public final function getResultInstance () {
696 return $this->resultInstance;
700 * Setter for template engine instances
702 * @param $templateInstance An instance of a template engine class
705 protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
706 $this->templateInstance = $templateInstance;
710 * Getter for template engine instances
712 * @return $templateInstance An instance of a template engine class
714 protected final function getTemplateInstance () {
715 return $this->templateInstance;
719 * Setter for search instance
721 * @param $searchInstance Searchable criteria instance
724 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
725 $this->searchInstance = $searchInstance;
729 * Getter for search instance
731 * @return $searchInstance Searchable criteria instance
733 public final function getSearchInstance () {
734 return $this->searchInstance;
738 * Setter for update instance
740 * @param $updateInstance Searchable criteria instance
743 public final function setUpdateInstance (LocalUpdateCriteria $updateInstance) {
744 $this->updateInstance = $updateInstance;
748 * Getter for update instance
750 * @return $updateInstance Updateable criteria instance
752 public final function getUpdateInstance () {
753 return $this->updateInstance;
757 * Setter for resolver instance
759 * @param $resolverInstance Instance of a command resolver class
762 public final function setResolverInstance (Resolver $resolverInstance) {
763 $this->resolverInstance = $resolverInstance;
767 * Getter for resolver instance
769 * @return $resolverInstance Instance of a command resolver class
771 public final function getResolverInstance () {
772 return $this->resolverInstance;
776 * Setter for language instance
778 * @param $configInstance The configuration instance which shall
779 * be FrameworkConfiguration
782 public final function setConfigInstance (FrameworkConfiguration $configInstance) {
783 Registry::getRegistry()->addInstance('config', $configInstance);
787 * Getter for configuration instance
789 * @return $configInstance Configuration instance
791 public final function getConfigInstance () {
792 $configInstance = Registry::getRegistry()->getInstance('config');
793 return $configInstance;
797 * Setter for debug instance
799 * @param $debugInstance The instance for debug output class
802 public final function setDebugInstance (DebugMiddleware $debugInstance) {
803 Registry::getRegistry()->addInstance('debug', $debugInstance);
807 * Getter for debug instance
809 * @return $debugInstance Instance to class DebugConsoleOutput or DebugWebOutput
811 public final function getDebugInstance () {
812 // Get debug instance
813 $debugInstance = Registry::getRegistry()->getInstance('debug');
816 return $debugInstance;
820 * Setter for web output instance
822 * @param $webInstance The instance for web output class
825 public final function setWebOutputInstance (OutputStreamer $webInstance) {
826 Registry::getRegistry()->addInstance('web_output', $webInstance);
830 * Getter for web output instance
832 * @return $webOutputInstance - Instance to class WebOutput
834 public final function getWebOutputInstance () {
835 $webOutputInstance = Registry::getRegistry()->getInstance('web_output');
836 return $webOutputInstance;
840 * Setter for database instance
842 * @param $databaseInstance The instance for the database connection (forced DatabaseConnection)
845 public final function setDatabaseInstance (DatabaseConnection $databaseInstance) {
846 Registry::getRegistry()->addInstance('db_instance', $databaseInstance);
850 * Getter for database layer
852 * @return $databaseInstance The database layer instance
854 public final function getDatabaseInstance () {
856 $databaseInstance = Registry::getRegistry()->getInstance('db_instance');
859 return $databaseInstance;
863 * Setter for compressor channel
865 * @param $compressorInstance An instance of CompressorChannel
868 public final function setCompressorChannel (CompressorChannel $compressorInstance) {
869 Registry::getRegistry()->addInstance('compressor', $compressorInstance);
873 * Getter for compressor channel
875 * @return $compressorInstance The compressor channel
877 public final function getCompressorChannel () {
878 $compressorInstance = Registry::getRegistry()->getInstance('compressor');
879 return $compressorInstance;
883 * Protected getter for a manageable application helper class
885 * @return $applicationInstance An instance of a manageable application helper class
887 protected final function getApplicationInstance () {
888 $applicationInstance = Registry::getRegistry()->getInstance('application');
889 return $applicationInstance;
893 * Setter for a manageable application helper class
895 * @param $applicationInstance An instance of a manageable application helper class
898 public final function setApplicationInstance (ManageableApplication $applicationInstance) {
899 Registry::getRegistry()->addInstance('application', $applicationInstance);
903 * Setter for request instance
905 * @param $requestInstance An instance of a Requestable class
908 public final function setRequestInstance (Requestable $requestInstance) {
909 $this->requestInstance = $requestInstance;
913 * Getter for request instance
915 * @return $requestInstance An instance of a Requestable class
917 public final function getRequestInstance () {
918 return $this->requestInstance;
922 * Setter for response instance
924 * @param $responseInstance An instance of a Responseable class
927 public final function setResponseInstance (Responseable $responseInstance) {
928 $this->responseInstance = $responseInstance;
932 * Getter for response instance
934 * @return $responseInstance An instance of a Responseable class
936 public final function getResponseInstance () {
937 return $this->responseInstance;
941 * Private getter for language instance
943 * @return $langInstance An instance to the language sub-system
945 protected final function getLanguageInstance () {
946 $langInstance = Registry::getRegistry()->getInstance('language');
947 return $langInstance;
951 * Setter for language instance
953 * @param $langInstance An instance to the language sub-system
955 * @see LanguageSystem
957 public final function setLanguageInstance (ManageableLanguage $langInstance) {
958 Registry::getRegistry()->addInstance('language', $langInstance);
962 * Private getter for file IO instance
964 * @return $fileIoInstance An instance to the file I/O sub-system
966 protected final function getFileIoInstance () {
967 return $this->fileIoInstance;
971 * Setter for file I/O instance
973 * @param $fileIoInstance An instance to the file I/O sub-system
976 public final function setFileIoInstance (IoHandler $fileIoInstance) {
977 $this->fileIoInstance = $fileIoInstance;
981 * Protected setter for user instance
983 * @param $userInstance An instance of a user class
986 protected final function setUserInstance (ManageableAccount $userInstance) {
987 $this->userInstance = $userInstance;
991 * Getter for user instance
993 * @return $userInstance An instance of a user class
995 public final function getUserInstance () {
996 return $this->userInstance;
1000 * Setter for controller instance (this surely breaks a bit the MVC patterm)
1002 * @param $controllerInstance An instance of the controller
1005 public final function setControllerInstance (Controller $controllerInstance) {
1006 $this->controllerInstance = $controllerInstance;
1010 * Getter for controller instance (this surely breaks a bit the MVC patterm)
1012 * @return $controllerInstance An instance of the controller
1014 public final function getControllerInstance () {
1015 return $this->controllerInstance;
1019 * Setter for RNG instance
1021 * @param $rngInstance An instance of a random number generator (RNG)
1024 protected final function setRngInstance (RandomNumberGenerator $rngInstance) {
1025 $this->rngInstance = $rngInstance;
1029 * Getter for RNG instance
1031 * @return $rngInstance An instance of a random number generator (RNG)
1033 public final function getRngInstance () {
1034 return $this->rngInstance;
1038 * Setter for Cryptable instance
1040 * @param $cryptoInstance An instance of a Cryptable class
1043 protected final function setCryptoInstance (Cryptable $cryptoInstance) {
1044 $this->cryptoInstance = $cryptoInstance;
1048 * Getter for Cryptable instance
1050 * @return $cryptoInstance An instance of a Cryptable class
1052 public final function getCryptoInstance () {
1053 return $this->cryptoInstance;
1057 * Setter for the list instance
1059 * @param $listInstance A list of Listable
1062 protected final function setListInstance (Listable $listInstance) {
1063 $this->listInstance = $listInstance;
1067 * Getter for the list instance
1069 * @return $listInstance A list of Listable
1071 protected final function getListInstance () {
1072 return $this->listInstance;
1076 * Setter for the menu instance
1078 * @param $menuInstance A RenderableMenu instance
1081 protected final function setMenuInstance (RenderableMenu $menuInstance) {
1082 $this->menuInstance = $menuInstance;
1086 * Getter for the menu instance
1088 * @return $menuInstance A RenderableMenu instance
1090 protected final function getMenuInstance () {
1091 return $this->menuInstance;
1095 * Setter for image instance
1097 * @param $imageInstance An instance of an image
1100 public final function setImageInstance (BaseImage $imageInstance) {
1101 $this->imageInstance = $imageInstance;
1105 * Getter for image instance
1107 * @return $imageInstance An instance of an image
1109 public final function getImageInstance () {
1110 return $this->imageInstance;
1114 * Setter for stacker instance
1116 * @param $stackInstance An instance of an stacker
1119 public final function setStackInstance (Stackable $stackInstance) {
1120 $this->stackInstance = $stackInstance;
1124 * Getter for stacker instance
1126 * @return $stackInstance An instance of an stacker
1128 public final function getStackInstance () {
1129 return $this->stackInstance;
1133 * Setter for compressor instance
1135 * @param $compressorInstance An instance of an compressor
1138 public final function setCompressorInstance (Compressor $compressorInstance) {
1139 $this->compressorInstance = $compressorInstance;
1143 * Getter for compressor instance
1145 * @return $compressorInstance An instance of an compressor
1147 public final function getCompressorInstance () {
1148 return $this->compressorInstance;
1152 * Setter for Parseable instance
1154 * @param $parserInstance An instance of an Parseable
1157 public final function setParserInstance (Parseable $parserInstance) {
1158 $this->parserInstance = $parserInstance;
1162 * Getter for Parseable instance
1164 * @return $parserInstance An instance of an Parseable
1166 public final function getParserInstance () {
1167 return $this->parserInstance;
1171 * Setter for HandleableProtocol instance
1173 * @param $protocolInstance An instance of an HandleableProtocol
1176 public final function setProtocolInstance (HandleableProtocol $protocolInstance) {
1177 $this->protocolInstance = $protocolInstance;
1181 * Getter for HandleableProtocol instance
1183 * @return $protocolInstance An instance of an HandleableProtocol
1185 public final function getProtocolInstance () {
1186 return $this->protocolInstance;
1190 * Setter for DatabaseWrapper instance
1192 * @param $wrapperInstance An instance of an DatabaseWrapper
1195 public final function setWrapperInstance (DatabaseWrapper $wrapperInstance) {
1196 $this->wrapperInstance = $wrapperInstance;
1200 * Getter for DatabaseWrapper instance
1202 * @return $wrapperInstance An instance of an DatabaseWrapper
1204 public final function getWrapperInstance () {
1205 return $this->wrapperInstance;
1209 * Setter for socket resource
1211 * @param $socketResource A valid socket resource
1214 public final function setSocketResource ($socketResource) {
1215 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . '::' . __FUNCTION__ . ': socketResource=' . $socketResource . ',previous[' . gettype($this->socketResource) . ']=' . $this->socketResource);
1216 $this->socketResource = $socketResource;
1220 * Getter for socket resource
1222 * @return $socketResource A valid socket resource
1224 public final function getSocketResource () {
1225 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . '::' . __FUNCTION__ . ': socketResource[' . gettype($this->socketResource) . ']=' . $this->socketResource);
1226 return $this->socketResource;
1230 * Setter for regular expression
1232 * @param $regularExpression A valid regular expression
1235 public final function setRegularExpression ($regularExpression) {
1236 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . '::' . __FUNCTION__ . ': regularExpression=' . $regularExpression . ',previous[' . gettype($this->regularExpression) . ']=' . $this->regularExpression);
1237 $this->regularExpression = $regularExpression;
1241 * Getter for regular expression
1243 * @return $regularExpression A valid regular expression
1245 public final function getRegularExpression () {
1246 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . '::' . __FUNCTION__ . ': regularExpression[' . gettype($this->regularExpression) . ']=' . $this->regularExpression);
1247 return $this->regularExpression;
1251 * Setter for helper instance
1253 * @param $helperInstance An instance of a helper class
1256 protected final function setHelperInstance (Helper $helperInstance) {
1257 $this->helperInstance = $helperInstance;
1261 * Getter for helper instance
1263 * @return $helperInstance An instance of a helper class
1265 public final function getHelperInstance () {
1266 return $this->helperInstance;
1270 * Setter for a Source instance
1272 * @param $sourceInstance An instance of a Source class
1275 protected final function setSourceInstance (Source $sourceInstance) {
1276 $this->sourceInstance = $sourceInstance;
1280 * Getter for a Source instance
1282 * @return $sourceInstance An instance of a Source class
1284 protected final function getSourceInstance () {
1285 return $this->sourceInstance;
1289 * Setter for a UrlSource instance
1291 * @param $sourceInstance An instance of a UrlSource class
1294 protected final function setUrlSourceInstance (UrlSource $urlSourceInstance) {
1295 $this->urlSourceInstance = $urlSourceInstance;
1299 * Getter for a UrlSource instance
1301 * @return $urlSourceInstance An instance of a UrlSource class
1303 protected final function getUrlSourceInstance () {
1304 return $this->urlSourceInstance;
1308 * Getter for a InputStream instance
1310 * @param $inputStreamInstance The InputStream instance
1312 protected final function getInputStreamInstance () {
1313 return $this->inputStreamInstance;
1317 * Setter for a InputStream instance
1319 * @param $inputStreamInstance The InputStream instance
1322 protected final function setInputStreamInstance (InputStream $inputStreamInstance) {
1323 $this->inputStreamInstance = $inputStreamInstance;
1327 * Getter for a OutputStream instance
1329 * @param $outputStreamInstance The OutputStream instance
1331 protected final function getOutputStreamInstance () {
1332 return $this->outputStreamInstance;
1336 * Setter for a OutputStream instance
1338 * @param $outputStreamInstance The OutputStream instance
1341 protected final function setOutputStreamInstance (OutputStream $outputStreamInstance) {
1342 $this->outputStreamInstance = $outputStreamInstance;
1346 * Setter for handler instance
1348 * @param $handlerInstance An instance of a Handleable class
1351 protected final function setHandlerInstance (Handleable $handlerInstance) {
1352 $this->handlerInstance = $handlerInstance;
1356 * Getter for handler instance
1358 * @return $handlerInstance A Networkable instance
1360 protected final function getHandlerInstance () {
1361 return $this->handlerInstance;
1365 * Setter for visitor instance
1367 * @param $visitorInstance A Visitor instance
1370 protected final function setVisitorInstance (Visitor $visitorInstance) {
1371 $this->visitorInstance = $visitorInstance;
1375 * Getter for visitor instance
1377 * @return $visitorInstance A Visitor instance
1379 protected final function getVisitorInstance () {
1380 return $this->visitorInstance;
1384 * Setter for DHT instance
1386 * @param $dhtInstance A Distributable instance
1389 protected final function setDhtInstance (Distributable $dhtInstance) {
1390 $this->dhtInstance = $dhtInstance;
1394 * Getter for DHT instance
1396 * @return $dhtInstance A Distributable instance
1398 protected final function getDhtInstance () {
1399 return $this->dhtInstance;
1403 * Setter for raw package Data
1405 * @param $packageData Raw package Data
1408 public final function setPackageData (array $packageData) {
1409 $this->packageData = $packageData;
1413 * Getter for raw package Data
1415 * @return $packageData Raw package Data
1417 public function getPackageData () {
1418 return $this->packageData;
1423 * Setter for Iterator instance
1425 * @param $iteratorInstance An instance of an Iterator
1428 protected final function setIteratorInstance (Iterator $iteratorInstance) {
1429 $this->iteratorInstance = $iteratorInstance;
1433 * Getter for Iterator instance
1435 * @return $iteratorInstance An instance of an Iterator
1437 public final function getIteratorInstance () {
1438 return $this->iteratorInstance;
1442 * Setter for FilePointer instance
1444 * @param $pointerInstance An instance of an FilePointer class
1447 protected final function setPointerInstance (FilePointer $pointerInstance) {
1448 $this->pointerInstance = $pointerInstance;
1452 * Getter for FilePointer instance
1454 * @return $pointerInstance An instance of an FilePointer class
1456 public final function getPointerInstance () {
1457 return $this->pointerInstance;
1461 * Unsets pointer instance which triggers a call of __destruct() if the
1462 * instance is still there. This is surely not fatal on already "closed"
1463 * file pointer instances.
1465 * I don't want to mess around with above setter by giving it a default
1466 * value NULL as setter should always explicitly only set (existing) object
1467 * instances and NULL is NULL.
1471 protected final function unsetPointerInstance () {
1472 // Simply it to NULL
1473 $this->pointerInstance = NULL;
1477 * Setter for Indexable instance
1479 * @param $indexInstance An instance of an Indexable class
1482 protected final function setIndexInstance (Indexable $indexInstance) {
1483 $this->indexInstance = $indexInstance;
1487 * Getter for Indexable instance
1489 * @return $indexInstance An instance of an Indexable class
1491 public final function getIndexInstance () {
1492 return $this->indexInstance;
1496 * Setter for Block instance
1498 * @param $blockInstance An instance of an Block class
1501 protected final function setBlockInstance (Block $blockInstance) {
1502 $this->blockInstance = $blockInstance;
1506 * Getter for Block instance
1508 * @return $blockInstance An instance of an Block class
1510 public final function getBlockInstance () {
1511 return $this->blockInstance;
1515 * Setter for Minable instance
1517 * @param $minableInstance A Minable instance
1520 protected final function setMinableInstance (Minable $minableInstance) {
1521 $this->minableInstance = $minableInstance;
1525 * Getter for minable instance
1527 * @return $minableInstance A Minable instance
1529 protected final function getMinableInstance () {
1530 return $this->minableInstance;
1534 * Setter for FrameworkDirectory instance
1536 * @param $directoryInstance A FrameworkDirectoryPointer instance
1539 protected final function setDirectoryInstance (FrameworkDirectory $directoryInstance) {
1540 $this->directoryInstance = $directoryInstance;
1544 * Getter for FrameworkDirectory instance
1546 * @return $directoryInstance A FrameworkDirectory instance
1548 protected final function getDirectoryInstance () {
1549 return $this->directoryInstance;
1553 * Setter for listener instance
1555 * @param $listenerInstance A Listenable instance
1558 protected final function setListenerInstance (Listenable $listenerInstance) {
1559 $this->listenerInstance = $listenerInstance;
1563 * Getter for listener instance
1565 * @return $listenerInstance A Listenable instance
1567 protected final function getListenerInstance () {
1568 return $this->listenerInstance;
1572 * Getter for communicator instance
1574 * @return $communicatorInstance An instance of a Communicator class
1576 public final function getCommunicatorInstance () {
1577 return $this->communicatorInstance;
1581 * Setter for communicator instance
1583 * @param $communicatorInstance An instance of a Communicator class
1586 protected final function setCommunicatorInstance (Communicator $communicatorInstance) {
1587 $this->communicatorInstance = $communicatorInstance;
1591 * Setter for state instance
1593 * @param $stateInstance A Stateable instance
1596 public final function setStateInstance (Stateable $stateInstance) {
1597 $this->stateInstance = $stateInstance;
1601 * Getter for state instance
1603 * @return $stateInstance A Stateable instance
1605 public final function getStateInstance () {
1606 return $this->stateInstance;
1610 * Setter for output instance
1612 * @param $outputInstance The debug output instance
1615 public final function setOutputInstance (OutputStreamer $outputInstance) {
1616 $this->outputInstance = $outputInstance;
1620 * Getter for output instance
1622 * @return $outputInstance The debug output instance
1624 public final function getOutputInstance () {
1625 return $this->outputInstance;
1629 * Setter for command name
1631 * @param $commandName Last validated command name
1634 protected final function setCommandName ($commandName) {
1635 $this->commandName = $commandName;
1639 * Getter for command name
1641 * @return $commandName Last validated command name
1643 protected final function getCommandName () {
1644 return $this->commandName;
1648 * Setter for controller name
1650 * @param $controllerName Last validated controller name
1653 protected final function setControllerName ($controllerName) {
1654 $this->controllerName = $controllerName;
1658 * Getter for controller name
1660 * @return $controllerName Last validated controller name
1662 protected final function getControllerName () {
1663 return $this->controllerName;
1667 * Getter for protocol name
1669 * @return $protocolName Name of used protocol
1671 public final function getProtocolName () {
1672 return $this->protocolName;
1676 * Setter for protocol name
1678 * @param $protocolName Name of used protocol
1681 protected final function setProtocolName ($protocolName) {
1682 $this->protocolName = $protocolName;
1686 * Checks whether an object equals this object. You should overwrite this
1687 * method to implement own equality checks
1689 * @param $objectInstance An instance of a FrameworkInterface object
1690 * @return $equals Whether both objects equals
1692 public function equals (FrameworkInterface $objectInstance) {
1695 $this->__toString() == $objectInstance->__toString()
1697 $this->hashCode() == $objectInstance->hashCode()
1700 // Return the result
1705 * Generates a generic hash code of this class. You should really overwrite
1706 * this method with your own hash code generator code. But keep KISS in mind.
1708 * @return $hashCode A generic hash code respresenting this whole class
1710 public function hashCode () {
1712 return crc32($this->__toString());
1716 * Formats computer generated price values into human-understandable formats
1717 * with thousand and decimal separators.
1719 * @param $value The in computer format value for a price
1720 * @param $currency The currency symbol (use HTML-valid characters!)
1721 * @param $decNum Number of decimals after commata
1722 * @return $price The for the current language formated price string
1723 * @throws MissingDecimalsThousandsSeparatorException If decimals or
1724 * thousands separator
1727 public function formatCurrency ($value, $currency = '€', $decNum = 2) {
1728 // Are all required attriutes set?
1729 if ((!isset($this->decimals)) || (!isset($this->thousands))) {
1730 // Throw an exception
1731 throw new MissingDecimalsThousandsSeparatorException($this, self::EXCEPTION_ATTRIBUTES_ARE_MISSING);
1735 $value = (float) $value;
1737 // Reformat the US number
1738 $price = number_format($value, $decNum, $this->decimals, $this->thousands) . $currency;
1740 // Return as string...
1745 * Appends a trailing slash to a string
1747 * @param $str A string (maybe) without trailing slash
1748 * @return $str A string with an auto-appended trailing slash
1750 public final function addMissingTrailingSlash ($str) {
1751 // Is there a trailing slash?
1752 if (substr($str, -1, 1) != '/') {
1756 // Return string with trailing slash
1761 * Prepare the template engine (HtmlTemplateEngine by default) for a given
1762 * application helper instance (ApplicationHelper by default).
1764 * @param $applicationInstance An application helper instance or
1765 * null if we shall use the default
1766 * @return $templateInstance The template engine instance
1767 * @throws NullPointerException If the discovered application
1768 * instance is still null
1770 protected function prepareTemplateInstance (ManageableApplication $applicationInstance = NULL) {
1771 // Is the application instance set?
1772 if (is_null($applicationInstance)) {
1773 // Get the current instance
1774 $applicationInstance = $this->getApplicationInstance();
1777 if (is_null($applicationInstance)) {
1778 // Thrown an exception
1779 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1783 // Initialize the template engine
1784 $templateInstance = ObjectFactory::createObjectByConfiguredName('html_template_class');
1786 // Return the prepared instance
1787 return $templateInstance;
1791 * Debugs this instance by putting out it's full content
1793 * @param $message Optional message to show in debug output
1796 public final function debugInstance ($message = '') {
1797 // Restore the error handler to avoid trouble with missing array elements or undeclared variables
1798 restore_error_handler();
1803 // Is a message set?
1804 if (!empty($message)) {
1805 // Construct message
1806 $content = sprintf('<div class="debug_message">Message: %s</div>' . PHP_EOL, $message);
1809 // Generate the output
1810 $content .= sprintf('<pre>%s</pre>',
1813 print_r($this, TRUE)
1819 ApplicationEntryPoint::app_exit(sprintf('<div class="debug_header">%s debug output:</div><div class="debug_content">%s</div>Loaded includes: <div class="debug_include_list">%s</div>',
1820 $this->__toString(),
1822 ClassLoader::getSelfInstance()->getPrintableIncludeList()
1827 * Replaces control characters with printable output
1829 * @param $str String with control characters
1830 * @return $str Replaced string
1832 protected function replaceControlCharacters ($str) {
1835 chr(13), '[r]', str_replace(
1836 chr(10), '[n]', str_replace(
1846 * Output a partial stub message for the caller method
1848 * @param $message An optional message to display
1851 protected function partialStub ($message = '') {
1852 // Get the backtrace
1853 $backtrace = debug_backtrace();
1855 // Generate the class::method string
1856 $methodName = 'UnknownClass->unknownMethod';
1857 if ((isset($backtrace[1]['class'])) && (isset($backtrace[1]['function']))) {
1858 $methodName = $backtrace[1]['class'] . '->' . $backtrace[1]['function'];
1861 // Construct the full message
1862 $stubMessage = sprintf('[%s:] Partial stub!',
1866 // Is the extra message given?
1867 if (!empty($message)) {
1868 // Then add it as well
1869 $stubMessage .= ' Message: ' . $message;
1872 // Debug instance is there?
1873 if (!is_null($this->getDebugInstance())) {
1874 // Output stub message
1875 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($stubMessage);
1878 trigger_error($stubMessage);
1884 * Outputs a debug backtrace and stops further script execution
1886 * @param $message An optional message to output
1887 * @param $doExit Whether exit the program (TRUE is default)
1890 public function debugBackTrace ($message = '', $doExit = TRUE) {
1891 // Sorry, there is no other way getting this nice backtrace
1892 if (!empty($message)) {
1894 printf('Message: %s<br />' . PHP_EOL, $message);
1898 debug_print_backtrace();
1902 if ($doExit === TRUE) {
1908 * Creates an instance of a debugger instance
1910 * @param $className Name of the class (currently unsupported)
1911 * @param $lineNumber Line number where the call was made
1912 * @return $debugInstance An instance of a debugger class
1913 * @deprecated Not fully, as the new Logger facilities are not finished yet.
1915 public final static function createDebugInstance ($className, $lineNumber = NULL) {
1916 // Is the instance set?
1917 if (!Registry::getRegistry()->instanceExists('debug')) {
1918 // Init debug instance
1919 $debugInstance = NULL;
1923 // Get a debugger instance
1924 $debugInstance = DebugMiddleware::createDebugMiddleware(FrameworkConfiguration::getSelfInstance()->getConfigEntry('debug_' . self::getResponseTypeFromSystem() . '_class'), $className);
1925 } catch (NullPointerException $e) {
1926 // Didn't work, no instance there
1927 exit(sprintf('Cannot create debugInstance! Exception=%s,message=%s,className=%s,lineNumber=%d' . PHP_EOL, $e->__toString(), $e->getMessage(), $className, $lineNumber));
1930 // Empty string should be ignored and used for testing the middleware
1931 DebugMiddleware::getSelfInstance()->output('');
1933 // Set it in its own class. This will set it in the registry
1934 $debugInstance->setDebugInstance($debugInstance);
1936 // Get instance from registry
1937 $debugInstance = Registry::getRegistry()->getDebugInstance();
1941 return $debugInstance;
1945 * Simple output of a message with line-break
1947 * @param $message Message to output
1950 public function outputLine ($message) {
1952 print($message . PHP_EOL);
1956 * Outputs a debug message whether to debug instance (should be set!) or
1957 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
1958 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
1960 * @param $message Message we shall send out...
1961 * @param $doPrint Whether print or die here (default: print)
1962 * @paran $stripTags Whether to strip tags (default: FALSE)
1965 public function debugOutput ($message, $doPrint = TRUE, $stripTags = FALSE) {
1966 // Set debug instance to NULL
1967 $debugInstance = NULL;
1971 // Get debug instance
1972 $debugInstance = $this->getDebugInstance();
1973 } catch (NullPointerException $e) {
1974 // The debug instance is not set (yet)
1977 // Is the debug instance there?
1978 if (is_object($debugInstance)) {
1979 // Use debug output handler
1980 $debugInstance->output($message, $stripTags);
1982 if ($doPrint === FALSE) {
1983 // Die here if not printed
1987 // Are debug times enabled?
1988 if ($this->getConfigInstance()->getConfigEntry('debug_' . self::getResponseTypeFromSystem() . '_output_timings') == 'Y') {
1990 $message = $this->getPrintableExecutionTime() . $message;
1994 if ($doPrint === TRUE) {
1996 $this->outputLine($message);
2005 * Converts e.g. a command from URL to a valid class by keeping out bad characters
2007 * @param $str The string, what ever it is needs to be converted
2008 * @return $className Generated class name
2010 public static final function convertToClassName ($str) {
2014 // Convert all dashes in underscores
2015 $str = self::convertDashesToUnderscores($str);
2017 // Now use that underscores to get classname parts for hungarian style
2018 foreach (explode('_', $str) as $strPart) {
2019 // Make the class name part lower case and first upper case
2020 $className .= ucfirst(strtolower($strPart));
2023 // Return class name
2028 * Converts dashes to underscores, e.g. useable for configuration entries
2030 * @param $str The string with maybe dashes inside
2031 * @return $str The converted string with no dashed, but underscores
2033 public static final function convertDashesToUnderscores ($str) {
2035 $str = str_replace('-', '_', $str);
2037 // Return converted string
2042 * Marks up the code by adding e.g. line numbers
2044 * @param $phpCode Unmarked PHP code
2045 * @return $markedCode Marked PHP code
2047 public function markupCode ($phpCode) {
2052 $errorArray = error_get_last();
2054 // Init the code with error message
2055 if (is_array($errorArray)) {
2057 $markedCode = sprintf('<div id="error_header">File: <span id="error_data">%s</span>, Line: <span id="error_data">%s</span>, Message: <span id="error_data">%s</span>, Type: <span id="error_data">%s</span></div>',
2058 basename($errorArray['file']),
2059 $errorArray['line'],
2060 $errorArray['message'],
2065 // Add line number to the code
2066 foreach (explode(chr(10), $phpCode) as $lineNo => $code) {
2068 $markedCode .= sprintf('<span id="code_line">%s</span>: %s' . PHP_EOL,
2070 htmlentities($code, ENT_QUOTES)
2079 * Filter a given GMT timestamp (non Uni* stamp!) to make it look more
2080 * beatiful for web-based front-ends. If null is given a message id
2081 * null_timestamp will be resolved and returned.
2083 * @param $timestamp Timestamp to prepare (filter) for display
2084 * @return $readable A readable timestamp
2086 public function doFilterFormatTimestamp ($timestamp) {
2087 // Default value to return
2090 // Is the timestamp null?
2091 if (is_null($timestamp)) {
2092 // Get a message string
2093 $readable = $this->getLanguageInstance()->getMessage('null_timestamp');
2095 switch ($this->getLanguageInstance()->getLanguageCode()) {
2096 case 'de': // German format is a bit different to default
2097 // Split the GMT stamp up
2098 $dateTime = explode(' ', $timestamp );
2099 $dateArray = explode('-', $dateTime[0]);
2100 $timeArray = explode(':', $dateTime[1]);
2102 // Construct the timestamp
2103 $readable = sprintf($this->getConfigInstance()->getConfigEntry('german_date_time'),
2113 default: // Default is pass-through
2114 $readable = $timestamp;
2124 * Filter a given number into a localized number
2126 * @param $value The raw value from e.g. database
2127 * @return $localized Localized value
2129 public function doFilterFormatNumber ($value) {
2130 // Generate it from config and localize dependencies
2131 switch ($this->getLanguageInstance()->getLanguageCode()) {
2132 case 'de': // German format is a bit different to default
2133 $localized = number_format($value, $this->getConfigInstance()->getConfigEntry('decimals'), ',', '.');
2136 default: // US, etc.
2137 $localized = number_format($value, $this->getConfigInstance()->getConfigEntry('decimals'), '.', ',');
2146 * "Getter" for databse entry
2148 * @return $entry An array with database entries
2149 * @throws NullPointerException If the database result is not found
2150 * @throws InvalidDatabaseResultException If the database result is invalid
2152 protected final function getDatabaseEntry () {
2153 // Is there an instance?
2154 if (!$this->getResultInstance() instanceof SearchableResult) {
2155 // Throw an exception here
2156 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
2160 $this->getResultInstance()->rewind();
2162 // Do we have an entry?
2163 if ($this->getResultInstance()->valid() === FALSE) {
2164 // @TODO Move the constant to e.g. BaseDatabaseResult when there is a non-cached database result available
2165 throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), CachedDatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
2169 $this->getResultInstance()->next();
2172 $entry = $this->getResultInstance()->current();
2179 * Getter for field name
2181 * @param $fieldName Field name which we shall get
2182 * @return $fieldValue Field value from the user
2183 * @throws NullPointerException If the result instance is null
2185 public final function getField ($fieldName) {
2186 // Default field value
2189 // Get result instance
2190 $resultInstance = $this->getResultInstance();
2192 // Is this instance null?
2193 if (is_null($resultInstance)) {
2194 // Then the user instance is no longer valid (expired cookies?)
2195 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
2198 // Get current array
2199 $fieldArray = $resultInstance->current();
2200 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.':<pre>'.print_r($fieldArray, TRUE).'</pre>');
2202 // Convert dashes to underscore
2203 $fieldName2 = self::convertDashesToUnderscores($fieldName);
2205 // Does the field exist?
2206 if ($this->isFieldSet($fieldName)) {
2208 $fieldValue = $fieldArray[$fieldName2];
2209 } elseif (defined('DEVELOPER')) {
2210 // Missing field entry, may require debugging
2211 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:fieldArray<pre>=' . print_r($fieldArray, TRUE) . '</pre>,fieldName=' . $fieldName . ' not found!');
2213 // Missing field entry, may require debugging
2214 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:fieldName=' . $fieldName . ' not found!');
2222 * Checks if given field is set
2224 * @param $fieldName Field name to check
2225 * @return $isSet Whether the given field name is set
2226 * @throws NullPointerException If the result instance is null
2228 public function isFieldSet ($fieldName) {
2229 // Get result instance
2230 $resultInstance = $this->getResultInstance();
2232 // Is this instance null?
2233 if (is_null($resultInstance)) {
2234 // Then the user instance is no longer valid (expired cookies?)
2235 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
2238 // Get current array
2239 $fieldArray = $resultInstance->current();
2240 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . $this->__toString() . ':' . __LINE__ . '] fieldName=' . $fieldName . ',fieldArray=<pre>'.print_r($fieldArray, TRUE).'</pre>');
2242 // Convert dashes to underscore
2243 $fieldName = self::convertDashesToUnderscores($fieldName);
2246 $isSet = isset($fieldArray[$fieldName]);
2253 * Flushs all pending updates to the database layer
2257 public function flushPendingUpdates () {
2258 // Get result instance
2259 $resultInstance = $this->getResultInstance();
2261 // Do we have data to update?
2262 if ((is_object($resultInstance)) && ($resultInstance->ifDataNeedsFlush())) {
2263 // Get wrapper class name config entry
2264 $configEntry = $resultInstance->getUpdateInstance()->getWrapperConfigEntry();
2266 // Create object instance
2267 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName($configEntry);
2269 // Yes, then send the whole result to the database layer
2270 $wrapperInstance->doUpdateByResult($this->getResultInstance());
2275 * Outputs a deprecation warning to the developer.
2277 * @param $message The message we shall output to the developer
2279 * @todo Write a logging mechanism for productive mode
2281 public function deprecationWarning ($message) {
2282 // Is developer mode active?
2283 if (defined('DEVELOPER')) {
2284 // Debug instance is there?
2285 if (!is_null($this->getDebugInstance())) {
2286 // Output stub message
2287 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($message);
2290 trigger_error($message . "<br />\n");
2294 // @TODO Finish this part!
2295 $this->partialStub('Developer mode inactive. Message:' . $message);
2300 * Checks whether the given PHP extension is loaded
2302 * @param $phpExtension The PHP extension we shall check
2303 * @return $isLoaded Whether the PHP extension is loaded
2305 public final function isPhpExtensionLoaded ($phpExtension) {
2307 $isLoaded = in_array($phpExtension, get_loaded_extensions());
2314 * "Getter" as a time() replacement but with milliseconds. You should use this
2315 * method instead of the encapsulated getimeofday() function.
2317 * @return $milliTime Timestamp with milliseconds
2319 public function getMilliTime () {
2320 // Get the time of day as float
2321 $milliTime = gettimeofday(TRUE);
2328 * Idles (sleeps) for given milliseconds
2330 * @return $hasSlept Whether it goes fine
2332 public function idle ($milliSeconds) {
2333 // Sleep is fine by default
2336 // Idle so long with found function
2337 if (function_exists('time_sleep_until')) {
2338 // Get current time and add idle time
2339 $sleepUntil = $this->getMilliTime() + abs($milliSeconds) / 1000;
2341 // New PHP 5.1.0 function found, ignore errors
2342 $hasSlept = @time_sleep_until($sleepUntil);
2345 * My Sun station doesn't have that function even with latest PHP
2348 usleep($milliSeconds * 1000);
2355 * Converts a hexadecimal string, even with negative sign as first string to
2356 * a decimal number using BC functions.
2358 * This work is based on comment #86673 on php.net documentation page at:
2359 * <http://de.php.net/manual/en/function.dechex.php#86673>
2361 * @param $hex Hexadecimal string
2362 * @return $dec Decimal number
2364 protected function hex2dec ($hex) {
2365 // Convert to all lower-case
2366 $hex = strtolower($hex);
2368 // Detect sign (negative/positive numbers)
2370 if (substr($hex, 0, 1) == '-') {
2372 $hex = substr($hex, 1);
2375 // Decode the hexadecimal string into a decimal number
2377 for ($i = strlen($hex) - 1, $e = 1; $i >= 0; $i--, $e = bcmul($e, 16)) {
2378 $factor = self::$hexdec[substr($hex, $i, 1)];
2379 $dec = bcadd($dec, bcmul($factor, $e));
2382 // Return the decimal number
2383 return $sign . $dec;
2387 * Converts even very large decimal numbers, also signed, to a hexadecimal
2390 * This work is based on comment #97756 on php.net documentation page at:
2391 * <http://de.php.net/manual/en/function.hexdec.php#97756>
2393 * @param $dec Decimal number, even with negative sign
2394 * @param $maxLength Optional maximum length of the string
2395 * @return $hex Hexadecimal string
2397 protected function dec2hex ($dec, $maxLength = 0) {
2398 // maxLength can be zero or devideable by 2
2399 assert(($maxLength == 0) || (($maxLength % 2) == 0));
2401 // Detect sign (negative/positive numbers)
2408 // Encode the decimal number into a hexadecimal string
2411 $hex = self::$dechex[($dec % (2 ^ 4))] . $hex;
2413 } while ($dec >= 1);
2416 * Leading zeros are required for hex-decimal "numbers". In some
2417 * situations more leading zeros are wanted, so check for both
2420 if ($maxLength > 0) {
2421 // Prepend more zeros
2422 $hex = str_pad($hex, $maxLength, '0', STR_PAD_LEFT);
2423 } elseif ((strlen($hex) % 2) != 0) {
2424 // Only make string's length dividable by 2
2428 // Return the hexadecimal string
2429 return $sign . $hex;
2433 * Converts a ASCII string (0 to 255) into a decimal number.
2435 * @param $asc The ASCII string to be converted
2436 * @return $dec Decimal number
2438 protected function asc2dec ($asc) {
2439 // Convert it into a hexadecimal number
2440 $hex = bin2hex($asc);
2442 // And back into a decimal number
2443 $dec = $this->hex2dec($hex);
2450 * Converts a decimal number into an ASCII string.
2452 * @param $dec Decimal number
2453 * @return $asc An ASCII string
2455 protected function dec2asc ($dec) {
2456 // First convert the number into a hexadecimal string
2457 $hex = $this->dec2hex($dec);
2459 // Then convert it into the ASCII string
2460 $asc = $this->hex2asc($hex);
2467 * Converts a hexadecimal number into an ASCII string. Negative numbers
2470 * @param $hex Hexadecimal string
2471 * @return $asc An ASCII string
2473 protected function hex2asc ($hex) {
2474 // Check for length, it must be devideable by 2
2475 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('hex='.$hex);
2476 assert((strlen($hex) % 2) == 0);
2480 for ($idx = 0; $idx < strlen($hex); $idx+=2) {
2481 // Get the decimal number of the chunk
2482 $part = hexdec(substr($hex, $idx, 2));
2484 // Add it to the final string
2488 // Return the final string
2493 * Checks whether the given encoded data was encoded with Base64
2495 * @param $encodedData Encoded data we shall check
2496 * @return $isBase64 Whether the encoded data is Base64
2498 protected function isBase64Encoded ($encodedData) {
2500 $isBase64 = (@base64_decode($encodedData, TRUE) !== FALSE);
2507 * "Getter" to get response/request type from analysis of the system.
2509 * @return $responseType Analyzed response type
2511 protected static function getResponseTypeFromSystem () {
2512 // Default is console
2513 $responseType = 'console';
2515 // Is 'HTTP_HOST' set?
2516 if (isset($_SERVER['HTTP_HOST'])) {
2518 * Then it is a HTML response/request as RSS and so on may be
2519 * transfered over HTTP as well.
2521 $responseType = 'html';
2525 return $responseType;
2529 * Gets a cache key from Criteria instance
2531 * @param $criteriaInstance An instance of a Criteria class
2532 * @param $onlyKeys Only use these keys for a cache key
2533 * @return $cacheKey A cache key suitable for lookup/storage purposes
2535 protected function getCacheKeyByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
2537 $cacheKey = sprintf('%s@%s',
2538 $this->__toString(),
2539 $criteriaInstance->getCacheKey($onlyKeys)
2543 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . ': cacheKey=' . $cacheKey);
2548 * Getter for startup time in miliseconds
2550 * @return $startupTime Startup time in miliseconds
2552 protected function getStartupTime () {
2553 return self::$startupTime;
2557 * "Getter" for a printable currently execution time in nice braces
2559 * @return $executionTime Current execution time in nice braces
2561 protected function getPrintableExecutionTime () {
2562 // Caculate the execution time
2563 $executionTime = microtime(TRUE) - $this->getStartupTime();
2565 // Pack it in nice braces
2566 $executionTime = sprintf('[ %01.5f ] ', $executionTime);
2569 return $executionTime;
2573 * Hashes a given string with a simple but stronger hash function (no salt)
2574 * and hex-encode it.
2576 * @param $str The string to be hashed
2577 * @return $hash The hash from string $str
2579 public static final function hash ($str) {
2580 // Hash given string with (better secure) hasher
2581 $hash = bin2hex(mhash(MHASH_SHA256, $str));
2588 * "Getter" for length of hash() output. This will be "cached" to speed up
2591 * @return $length Length of hash() output
2593 public static final function getHashLength () {
2595 if (is_null(self::$hashLength)) {
2596 // No, then hash a string and save its length.
2597 self::$hashLength = strlen(self::hash('abc123'));
2601 return self::$hashLength;
2605 * Checks whether the given number is really a number (only chars 0-9).
2607 * @param $num A string consisting only chars between 0 and 9
2608 * @param $castValue Whether to cast the value to double. Do only use this to secure numbers from Requestable classes.
2609 * @param $assertMismatch Whether to assert mismatches
2610 * @return $ret The (hopefully) secured numbered value
2612 public function bigintval ($num, $castValue = TRUE, $assertMismatch = FALSE) {
2613 // Filter all numbers out
2614 $ret = preg_replace('/[^0123456789]/', '', $num);
2617 if ($castValue === TRUE) {
2618 // Cast to biggest numeric type
2619 $ret = (double) $ret;
2622 // Assert only if requested
2623 if ($assertMismatch === TRUE) {
2624 // Has the whole value changed?
2625 assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
2633 * Checks whether the given hexadecimal number is really a hex-number (only chars 0-9,a-f).
2635 * @param $num A string consisting only chars between 0 and 9
2636 * @param $assertMismatch Whether to assert mismatches
2637 * @return $ret The (hopefully) secured hext-numbered value
2639 public function hexval ($num, $assertMismatch = FALSE) {
2640 // Filter all numbers out
2641 $ret = preg_replace('/[^0123456789abcdefABCDEF]/', '', $num);
2643 // Assert only if requested
2644 if ($assertMismatch === TRUE) {
2645 // Has the whole value changed?
2646 assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
2654 * Checks whether start/end marker are set
2656 * @param $data Data to be checked
2657 * @return $isset Whether start/end marker are set
2659 public final function ifStartEndMarkersSet ($data) {
2661 $isset = ((substr($data, 0, strlen(BaseRawDataHandler::STREAM_START_MARKER)) == BaseRawDataHandler::STREAM_START_MARKER) && (substr($data, -1 * strlen(BaseRawDataHandler::STREAM_END_MARKER), strlen(BaseRawDataHandler::STREAM_END_MARKER)) == BaseRawDataHandler::STREAM_END_MARKER));
2663 // ... and return it
2668 * Determines if an element is set in the generic array
2670 * @param $keyGroup Main group for the key
2671 * @param $subGroup Sub group for the key
2672 * @param $key Key to check
2673 * @param $element Element to check
2674 * @return $isset Whether the given key is set
2676 protected final function isGenericArrayElementSet ($keyGroup, $subGroup, $key, $element) {
2678 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element);
2681 $isset = isset($this->genericArray[$keyGroup][$subGroup][$key][$element]);
2687 * Determines if a key is set in the generic array
2689 * @param $keyGroup Main group for the key
2690 * @param $subGroup Sub group for the key
2691 * @param $key Key to check
2692 * @return $isset Whether the given key is set
2694 protected final function isGenericArrayKeySet ($keyGroup, $subGroup, $key) {
2696 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
2699 $isset = isset($this->genericArray[$keyGroup][$subGroup][$key]);
2707 * Determines if a group is set in the generic array
2709 * @param $keyGroup Main group
2710 * @param $subGroup Sub group
2711 * @return $isset Whether the given group is set
2713 protected final function isGenericArrayGroupSet ($keyGroup, $subGroup) {
2715 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup);
2718 $isset = isset($this->genericArray[$keyGroup][$subGroup]);
2725 * Getter for sub key group
2727 * @param $keyGroup Main key group
2728 * @param $subGroup Sub key group
2729 * @return $array An array with all array elements
2731 protected final function getGenericSubArray ($keyGroup, $subGroup) {
2733 if (!$this->isGenericArrayGroupSet($keyGroup, $subGroup)) {
2734 // No, then abort here
2735 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
2740 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',value=' . print_r($this->genericArray[$keyGroup][$subGroup], TRUE));
2743 return $this->genericArray[$keyGroup][$subGroup];
2747 * Unsets a given key in generic array
2749 * @param $keyGroup Main group for the key
2750 * @param $subGroup Sub group for the key
2751 * @param $key Key to unset
2754 protected final function unsetGenericArrayKey ($keyGroup, $subGroup, $key) {
2756 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
2759 unset($this->genericArray[$keyGroup][$subGroup][$key]);
2763 * Unsets a given element in generic array
2765 * @param $keyGroup Main group for the key
2766 * @param $subGroup Sub group for the key
2767 * @param $key Key to unset
2768 * @param $element Element to unset
2771 protected final function unsetGenericArrayElement ($keyGroup, $subGroup, $key, $element) {
2773 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element);
2776 unset($this->genericArray[$keyGroup][$subGroup][$key][$element]);
2780 * Append a string to a given generic array key
2782 * @param $keyGroup Main group for the key
2783 * @param $subGroup Sub group for the key
2784 * @param $key Key to unset
2785 * @param $value Value to add/append
2788 protected final function appendStringToGenericArrayKey ($keyGroup, $subGroup, $key, $value, $appendGlue = '') {
2790 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',value[' . gettype($value) . ']=' . print_r($value, TRUE) . ',appendGlue=' . $appendGlue);
2792 // Is it already there?
2793 if ($this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
2795 $this->genericArray[$keyGroup][$subGroup][$key] .= $appendGlue . (string) $value;
2798 $this->genericArray[$keyGroup][$subGroup][$key] = (string) $value;
2803 * Append a string to a given generic array element
2805 * @param $keyGroup Main group for the key
2806 * @param $subGroup Sub group for the key
2807 * @param $key Key to unset
2808 * @param $element Element to check
2809 * @param $value Value to add/append
2812 protected final function appendStringToGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value, $appendGlue = '') {
2814 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',value[' . gettype($value) . ']=' . print_r($value, TRUE) . ',appendGlue=' . $appendGlue);
2816 // Is it already there?
2817 if ($this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
2819 $this->genericArray[$keyGroup][$subGroup][$key][$element] .= $appendGlue . (string) $value;
2822 $this->setStringGenericArrayElement($keyGroup, $subGroup, $key, $element, $value);
2827 * Sets a string in a given generic array element
2829 * @param $keyGroup Main group for the key
2830 * @param $subGroup Sub group for the key
2831 * @param $key Key to unset
2832 * @param $element Element to check
2833 * @param $value Value to add/append
2836 protected final function setStringGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value, $appendGlue = '') {
2838 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',value[' . gettype($value) . ']=' . print_r($value, TRUE) . ',appendGlue=' . $appendGlue);
2841 $this->genericArray[$keyGroup][$subGroup][$key][$element] = (string) $value;
2845 * Initializes given generic array group
2847 * @param $keyGroup Main group for the key
2848 * @param $subGroup Sub group for the key
2849 * @param $key Key to use
2850 * @param $forceInit Optionally force initialization
2853 protected final function initGenericArrayGroup ($keyGroup, $subGroup, $forceInit = FALSE) {
2855 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',forceInit=' . intval($forceInit));
2857 // Is it already set?
2858 if (($forceInit === FALSE) && ($this->isGenericArrayGroupSet($keyGroup, $subGroup))) {
2859 // Already initialized
2860 trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' already initialized.');
2865 $this->genericArray[$keyGroup][$subGroup] = array();
2869 * Initializes given generic array key
2871 * @param $keyGroup Main group for the key
2872 * @param $subGroup Sub group for the key
2873 * @param $key Key to use
2874 * @param $forceInit Optionally force initialization
2877 protected final function initGenericArrayKey ($keyGroup, $subGroup, $key, $forceInit = FALSE) {
2879 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',forceInit=' . intval($forceInit));
2881 // Is it already set?
2882 if (($forceInit === FALSE) && ($this->isGenericArrayKeySet($keyGroup, $subGroup, $key))) {
2883 // Already initialized
2884 trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' already initialized.');
2889 $this->genericArray[$keyGroup][$subGroup][$key] = array();
2893 * Initializes given generic array element
2895 * @param $keyGroup Main group for the key
2896 * @param $subGroup Sub group for the key
2897 * @param $key Key to use
2898 * @param $element Element to use
2899 * @param $forceInit Optionally force initialization
2902 protected final function initGenericArrayElement ($keyGroup, $subGroup, $key, $element, $forceInit = FALSE) {
2904 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',forceInit=' . intval($forceInit));
2906 // Is it already set?
2907 if (($forceInit === FALSE) && ($this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element))) {
2908 // Already initialized
2909 trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' already initialized.');
2914 $this->genericArray[$keyGroup][$subGroup][$key][$element] = array();
2918 * Pushes an element to a generic key
2920 * @param $keyGroup Main group for the key
2921 * @param $subGroup Sub group for the key
2922 * @param $key Key to use
2923 * @param $value Value to add/append
2924 * @return $count Number of array elements
2926 protected final function pushValueToGenericArrayKey ($keyGroup, $subGroup, $key, $value) {
2928 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',value[' . gettype($value) . ']=' . print_r($value, TRUE));
2931 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
2933 $this->initGenericArrayKey($keyGroup, $subGroup, $key);
2937 $count = array_push($this->genericArray[$keyGroup][$subGroup][$key], $value);
2940 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], TRUE));
2941 //* DEBUG: */ print(__METHOD__ . ': count=' . $count . PHP_EOL);
2946 * Pushes an element to a generic array element
2948 * @param $keyGroup Main group for the key
2949 * @param $subGroup Sub group for the key
2950 * @param $key Key to use
2951 * @param $element Element to check
2952 * @param $value Value to add/append
2953 * @return $count Number of array elements
2955 protected final function pushValueToGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value) {
2957 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',value[' . gettype($value) . ']=' . print_r($value, TRUE));
2960 if (!$this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
2962 $this->initGenericArrayElement($keyGroup, $subGroup, $key, $element);
2966 $count = array_push($this->genericArray[$keyGroup][$subGroup][$key][$element], $value);
2969 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], TRUE));
2970 //* DEBUG: */ print(__METHOD__ . ': count=' . $count . PHP_EOL);
2975 * Pops an element from a generic group
2977 * @param $keyGroup Main group for the key
2978 * @param $subGroup Sub group for the key
2979 * @param $key Key to unset
2980 * @return $value Last "popped" value
2982 protected final function popGenericArrayElement ($keyGroup, $subGroup, $key) {
2984 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
2987 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
2989 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
2994 $value = array_pop($this->genericArray[$keyGroup][$subGroup][$key]);
2997 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], TRUE));
2998 //* DEBUG: */ print(__METHOD__ . ': value[' . gettype($value) . ']=' . print_r($value, TRUE) . PHP_EOL);
3003 * Shifts an element from a generic group
3005 * @param $keyGroup Main group for the key
3006 * @param $subGroup Sub group for the key
3007 * @param $key Key to unset
3008 * @return $value Last "popped" value
3010 protected final function shiftGenericArrayElement ($keyGroup, $subGroup, $key) {
3012 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
3015 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
3017 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
3022 $value = array_shift($this->genericArray[$keyGroup][$subGroup][$key]);
3025 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], TRUE));
3026 //* DEBUG: */ print(__METHOD__ . ': value[' . gettype($value) . ']=' . print_r($value, TRUE) . PHP_EOL);
3031 * Count generic array group
3033 * @param $keyGroup Main group for the key
3034 * @return $count Count of given group
3036 protected final function countGenericArray ($keyGroup) {
3038 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup);
3041 if (!isset($this->genericArray[$keyGroup])) {
3043 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' not found.');
3048 $count = count($this->genericArray[$keyGroup]);
3051 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',count=' . $count);
3058 * Count generic array sub group
3060 * @param $keyGroup Main group for the key
3061 * @param $subGroup Sub group for the key
3062 * @return $count Count of given group
3064 protected final function countGenericArrayGroup ($keyGroup, $subGroup) {
3066 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup);
3069 if (!$this->isGenericArrayGroupSet($keyGroup, $subGroup)) {
3071 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
3076 $count = count($this->genericArray[$keyGroup][$subGroup]);
3079 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',count=' . $count);
3086 * Count generic array elements
3088 * @param $keyGroup Main group for the key
3089 * @param $subGroup Sub group for the key
3090 * @para $key Key to count
3091 * @return $count Count of given key
3093 protected final function countGenericArrayElements ($keyGroup, $subGroup, $key) {
3095 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
3098 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
3100 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
3102 } elseif (!$this->isValidGenericArrayGroup($keyGroup, $subGroup)) {
3104 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' is not an array.');
3109 $count = count($this->genericArray[$keyGroup][$subGroup][$key]);
3112 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',count=' . $count);
3119 * Getter for whole generic group array
3121 * @param $keyGroup Key group to get
3122 * @return $array Whole generic array group
3124 protected final function getGenericArray ($keyGroup) {
3126 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup);
3129 if (!isset($this->genericArray[$keyGroup])) {
3131 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' does not exist.');
3136 return $this->genericArray[$keyGroup];
3140 * Setter for generic array key
3142 * @param $keyGroup Key group to get
3143 * @param $subGroup Sub group for the key
3144 * @param $key Key to unset
3145 * @param $value Mixed value from generic array element
3148 protected final function setGenericArrayKey ($keyGroup, $subGroup, $key, $value) {
3150 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',value[' . gettype($value) . ']=' . print_r($value, TRUE));
3153 $this->genericArray[$keyGroup][$subGroup][$key] = $value;
3157 * Getter for generic array key
3159 * @param $keyGroup Key group to get
3160 * @param $subGroup Sub group for the key
3161 * @param $key Key to unset
3162 * @return $value Mixed value from generic array element
3164 protected final function getGenericArrayKey ($keyGroup, $subGroup, $key) {
3166 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
3169 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
3171 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' does not exist.');
3176 return $this->genericArray[$keyGroup][$subGroup][$key];
3180 * Sets a value in given generic array key/element
3182 * @param $keyGroup Main group for the key
3183 * @param $subGroup Sub group for the key
3184 * @param $key Key to set
3185 * @param $element Element to set
3186 * @param $value Value to set
3189 protected final function setGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value) {
3191 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',value[' . gettype($value) . ']=' . print_r($value, TRUE));
3194 $this->genericArray[$keyGroup][$subGroup][$key][$element] = $value;
3198 * Getter for generic array element
3200 * @param $keyGroup Key group to get
3201 * @param $subGroup Sub group for the key
3202 * @param $key Key to look for
3203 * @param $element Element to look for
3204 * @return $value Mixed value from generic array element
3206 protected final function getGenericArrayElement ($keyGroup, $subGroup, $key, $element) {
3208 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element);
3211 if (!$this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
3213 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' does not exist.');
3218 return $this->genericArray[$keyGroup][$subGroup][$key][$element];
3222 * Checks if a given sub group is valid (array)
3224 * @param $keyGroup Key group to get
3225 * @param $subGroup Sub group for the key
3226 * @return $isValid Whether given sub group is valid
3228 protected final function isValidGenericArrayGroup ($keyGroup, $subGroup) {
3230 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup);
3233 $isValid = (($this->isGenericArrayGroupSet($keyGroup, $subGroup)) && (is_array($this->getGenericSubArray($keyGroup, $subGroup))));
3240 * Checks if a given key is valid (array)
3242 * @param $keyGroup Key group to get
3243 * @param $subGroup Sub group for the key
3244 * @param $key Key to check
3245 * @return $isValid Whether given sub group is valid
3247 protected final function isValidGenericArrayKey ($keyGroup, $subGroup, $key) {
3249 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
3252 $isValid = (($this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) && (is_array($this->getGenericArrayKey($keyGroup, $subGroup, $key))));
3259 * Initializes the web output instance
3263 protected function initWebOutputInstance () {
3264 // Get application instance
3265 $applicationInstance = Registry::getRegistry()->getInstance('app');
3267 // Is this a response instance?
3268 if ($this instanceof Responseable) {
3269 // Then set it in application instance
3270 $applicationInstance->setResponseInstance($this);
3273 // Init web output instance
3274 $outputInstance = ObjectFactory::createObjectByConfiguredName('output_class', array($applicationInstance));
3277 $this->setWebOutputInstance($outputInstance);
3281 * Translates boolean TRUE to 'Y' and FALSE to 'N'
3283 * @param $boolean Boolean value
3284 * @return $translated Translated boolean value
3286 public static final function translateBooleanToYesNo ($boolean) {
3287 // Make sure it is really boolean
3288 assert(is_bool($boolean));
3291 $translated = ($boolean === TRUE) ? 'Y' : 'N';
3293 // ... and return it
3298 * Encodes raw data (almost any type) by "serializing" it and then pack it
3299 * into a "binary format".
3301 * @param $rawData Raw data (almost any type)
3302 * @return $encoded Encoded data
3304 protected function encodeData ($rawData) {
3305 // Make sure no objects or resources pass through
3306 assert(!is_object($rawData));
3307 assert(!is_resource($rawData));
3309 // First "serialize" it (json_encode() is faster than serialize())
3310 $encoded = $this->packString(json_encode($rawData));
3317 * Pack a string into a "binary format". Please execuse me that this is
3318 * widely undocumented. :-(
3320 * @param $str Unpacked string
3321 * @return $packed Packed string
3322 * @todo Improve documentation
3324 protected function packString ($str) {
3326 //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('str=' . $str . ' - CALLED!');
3328 // First compress the string (gzcompress is okay)
3329 $str = gzcompress($str);
3334 // And start the "encoding" loop
3335 for ($idx = 0; $idx < strlen($str); $idx += $this->packingData[$this->archArrayElement]['step']) {
3337 for ($i = 0; $i < $this->packingData[$this->archArrayElement]['step']; $i++) {
3338 $factor = ($this->packingData[$this->archArrayElement]['step'] - 1 - $i);
3340 if (($idx + $i) <= strlen($str)) {
3341 $ord = ord(substr($str, ($idx + $i), 1));
3343 $add = $ord * pow(256, $factor);
3347 //print 'idx=' . $idx . ',i=' . $i . ',ord=' . $ord . ',factor=' . $factor . ',add=' . $add . ',big=' . $big . PHP_EOL;
3351 $l = ($big & $this->packingData[$this->archArrayElement]['left']) >>$this->packingData[$this->archArrayElement]['factor'];
3352 $r = $big & $this->packingData[$this->archArrayElement]['right'];
3354 $chunk = str_pad(pack($this->packingData[$this->archArrayElement]['format'], $l, $r), 8, '0', STR_PAD_LEFT);
3355 //* NOISY-DEBUG */ print 'big=' . $big . ',chunk('.strlen($chunk) . ')='.md5($chunk).PHP_EOL;
3361 //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('packed=' . $packed . ' - EXIT!');
3366 * Checks whether the given file/path is in open_basedir(). This does not
3367 * gurantee that the file is actually readable and/or writeable. If you need
3368 * such gurantee then please use isReadableFile() instead.
3370 * @param $filePathName Name of the file/path to be checked
3371 * @return $isReachable Whether it is within open_basedir()
3373 protected static function isReachableFilePath ($filePathName) {
3374 // Is not reachable by default
3375 $isReachable = FALSE;
3377 // Get open_basedir parameter
3378 $openBaseDir = ini_get('open_basedir');
3381 if (!empty($openBaseDir)) {
3382 // Check all entries
3383 foreach (explode(PATH_SEPARATOR, $openBaseDir) as $dir) {
3384 // Check on existence
3385 if (substr($filePathName, 0, strlen($dir)) == $dir) {
3387 $isReachable = TRUE;
3391 // If open_basedir is not set, all is allowed
3392 $isReachable = TRUE;
3396 return $isReachable;
3400 * Checks whether the give file is within open_basedir() (done by
3401 * isReachableFilePath()), is actually a file and is readable.
3403 * @param $fileName Name of the file to be checked
3404 * @return $isReadable Whether the file is readable (and therefor exists)
3406 public static function isReadableFile ($fileName) {
3407 // Default is not readable
3408 $isReadable = FALSE;
3410 // Is within parameters, so check if it is a file and readable
3411 $isReadable = ((self::isReachableFilePath($fileName)) && (file_exists($fileName)) && (is_file($fileName)) && (is_readable($fileName)));
3418 * Creates a full-qualified file name (FQFN) for given file name by adding
3419 * a configured temporary file path to it.
3421 * @param $fileName Name for temporary file
3422 * @return $fqfn Full-qualified file name
3423 * @throw PathWriteProtectedException If the path in 'temp_file_path' is write-protected
3424 * @throws FileIoException If the file cannot be written
3426 protected static function createTempPathForFile ($fileName) {
3428 $basePath = FrameworkConfiguration::getSelfInstance()->getConfigEntry('temp_file_path');
3430 // Is the path writeable?
3431 if (!is_writable($basePath)) {
3432 // Path is write-protected
3433 throw new PathWriteProtectedException($fileName, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN);
3437 $fqfn = $basePath . '/' . $fileName;
3440 if (!self::isReachableFilePath($fqfn)) {
3442 throw new FileIoException($fqfn, self::EXCEPTION_FILE_NOT_REACHABLE);
3450 * "Getter" for a printable state name
3452 * @return $stateName Name of the node's state in a printable format
3454 public final function getPrintableState () {
3455 // Default is 'null'
3456 $stateName = 'null';
3458 // Get the state instance
3459 $stateInstance = $this->getStateInstance();
3461 // Is it an instance of Stateable?
3462 if ($stateInstance instanceof Stateable) {
3463 // Then use that state name
3464 $stateName = $stateInstance->getStateName();
3472 * Handles socket error for given socket resource and peer data. This method
3473 * validates $socketResource if it is a valid resource (see is_resource())
3474 * but assumes valid data in array $recipientData, except that
3475 * count($recipientData) is always 2.
3477 * @param $method Value of __METHOD__ from calling method
3478 * @param $line Value of __LINE__ from calling method
3479 * @param $socketResource A valid socket resource
3480 * @param $socketData A valid socket data array (0 = IP/file name, 1 = port)
3482 * @throws InvalidSocketException If $socketResource is no socket resource
3483 * @throws NoSocketErrorDetectedException If socket_last_error() gives zero back
3484 * @todo Move all this socket-related stuff into own class, most of it resides in BaseListener
3486 protected final function handleSocketError ($method, $line, $socketResource, array $socketData) {
3487 // This method handles only socket resources
3488 if (!is_resource($socketResource)) {
3489 // No resource, abort here
3490 throw new InvalidSocketException(array($this, $socketResource), BaseListener::EXCEPTION_INVALID_SOCKET);
3493 // Check socket array, 1st element is mostly IP address (or file name), 2nd is port number
3494 //* DEBUG-DIE: */ die(__METHOD__ . ':socketData=' . print_r($socketData, TRUE));
3495 assert(isset($socketData[0]));
3496 assert(isset($socketData[1]));
3498 // Get error code for first validation (0 is not an error)
3499 $errorCode = socket_last_error($socketResource);
3501 // If the error code is zero, someone called this method without an error
3502 if ($errorCode == 0) {
3503 // No error detected (or previously cleared outside this method)
3504 throw new NoSocketErrorDetectedException(array($this, $socketResource), BaseListener::EXCEPTION_NO_SOCKET_ERROR);
3507 // Get handler (method) name
3508 $handlerName = $this->getSocketErrorHandlerFromCode($errorCode);
3510 // Call-back the error handler method
3511 call_user_func_array(array($this, $handlerName), array($socketResource, $socketData));
3513 // Finally clear the error because it has been handled
3514 socket_clear_error($socketResource);