Example #1
0
 /**
  * Sends a message to the given remote host via a HTTP POST request. The remote host must have the System module activated in order to process the request.
  * Optional listeners should be attached on the remote host for the OnInteractionEvent event.
  * @param string The remote root url. This should only be the root, ending with a '/'
  * @param \System\Collection\Vector A vector with \System\System\Interaction\Message obejcts
  * @param string The custom encryptionkey. This is used over the systems default key
  * @return \System\Collection\Vector A Vector with \System\System\Interaction\Response objects, or false on error or no reply
  */
 public static final function sendMessage($rootUrl, \System\Collection\Vector $messages, $customEncryptionKey = '')
 {
     $targetUrl = $rootUrl . self::INTERACTION_CONTROLLER_CALL;
     $encodedMessages = self::encode($messages, $customEncryptionKey);
     $paramMap = new \System\Collection\Map();
     $paramMap['request'] = $encodedMessages;
     $request = \System\HTTP\Request\Request::getRequest();
     $a1 = array();
     $a2 = array();
     $response = \System\HTTP\Request\Call::httpPageRequest($targetUrl, $paramMap, self::INTERACTION_USERAGENT, $request, '', $a1, $a2, self::INTERACTION_CALL_TIMEOUT);
     if ($response) {
         $decodedResponse = self::decode($response, $customEncryptionKey);
         return $decodedResponse;
     }
     return false;
 }
Example #2
0
 /**
  * This function is the initiator of the controller call chain. It is called
  * automatically by the bootloader and spawns the proper controller child, based on the request.
  * When there is no request specified, the default controller will be loaded.
  * The BeforeControllerLoadEvent is called prior to the loading of the controller.
  * When the requested controller cannot be loaded, an OnControllerNotFoundEvent event will be fired and the optional replacement controller will be loaded.
  * @return \System\Web\Controller The current selected controller
  */
 public static final function callController()
 {
     $options = getopt('', array('controller::', 'action::', 'mod::'));
     $get = new \System\HTTP\Request\Get();
     //we default to the given config namespace and the config controller.
     $targetNamespace = SITE_NAMESPACE;
     $targetController = DEFAULT_CONTROLLER;
     $targetAction = 'defaultAction';
     //The default controller and the default namespace can be overridden by parameters
     if (isset($get->controller)) {
         $targetController = ucfirst($get->controller);
     } else {
         if (isset($options['controller'])) {
             $targetController = ucfirst($options['controller']);
         }
     }
     if (isset($get->action)) {
         $targetAction = ucfirst($get->action);
     } else {
         if (isset($options['action'])) {
             $targetAction = ucfirst($options['action']);
         }
     }
     /*
     for security reasons, we only allow the overridden namespace to be executed from within the global modules.
     Effectively, this means that a mod override parameter is effectively transferring control to a global module
     instead of de default controllers. This way, we support controllers within global modules.
     */
     if (isset($get->mod)) {
         $targetNamespace = (ucfirst($get->mod) == 'System' ? '\\System\\' : '\\Module\\') . ucfirst($get->mod);
     } else {
         if (isset($options['mod'])) {
             $targetNamespace = (ucfirst($options['mod']) == 'System' ? '\\System\\' : '\\Module\\') . ucfirst($options['mod']);
         }
     }
     //create the controller and run it
     $controllerName = $targetNamespace . '\\' . $targetController;
     $controller = null;
     if (class_exists($controllerName, true) && is_subclass_of($controllerName, '\\System\\Web\\Controller')) {
         $controller = new $controllerName();
     } else {
         //try to load a replacement controller using eventcatching
         if (\System\Event\EventHandler::hasListeners('\\System\\Event\\Event\\OnControllerNotFoundEvent')) {
             $event = new \System\Event\Event\OnControllerNotFoundEvent();
             $event->setControllerRequest($controllerName);
             $event->setControllerReplacement($controllerName);
             $event->raise();
             $controllerReplacement = $event->getControllerReplacement();
             if ($controllerReplacement != null && class_exists($controllerReplacement, true) && is_subclass_of($controllerReplacement, '\\System\\Web\\Controller')) {
                 $controller = new $controllerReplacement();
             }
         }
     }
     //check if the controller actually is a valid controller
     if ($controller instanceof \System\Web\Controller) {
         $requestMethod = \System\HTTP\Request\Request::getMethod();
         //throws before load events and checks if the controller needs to be replaced
         $controller = self::checkControllerReplacement($controller, $targetNamespace, $targetAction, $requestMethod);
         //process sapi specific calls
         self::proccessSAPICalls($controller);
         //process the services
         self::processServices($controller);
         //call the proper entry action, but also allow specific method based versions of that call
         //example: defaultAction_GET, defaultAction_POST, etc. This falls back to defaultAction
         switch (true) {
             case self::attemptControllerAction($controller, $targetAction . '_' . $requestMethod):
                 $action = $targetAction . '_' . $requestMethod;
                 break;
             case self::attemptControllerAction($controller, $targetAction):
                 $action = $targetAction;
                 break;
             case self::attemptControllerAction($controller, 'defaultAction_' . $requestMethod):
                 $action = 'defaultAction_' . $requestMethod;
                 break;
             default:
                 $action = 'defaultAction';
                 break;
         }
         $controller->{$action}();
         return $controller;
     }
     throw new \System\Error\Exception\InvalidControllerException('The given controller is invalid or does not exist: ' . $targetController . ' in ' . $targetNamespace);
 }
Example #3
0
 /**
  * Outputs the error and logs it to a file.
  * Note that this function effectively halts the execution of our script.
  * @param int The errorlevel of the error.
  * @param string The message
  * @param string The file the error occured in
  * @param int The line number of the error
  * @param string The stacktrace as a string
  */
 protected static final function outputError($errorLevel, $errorNotice, $errorFile, $errorLine, $stackTrace)
 {
     //logging of the error goes here. This is done using classic SQL queries and manual connecting to the db,
     //because we dont want to be dependant of the system
     $errorPage = self::ERRORPAGE;
     if (defined('ERRORPAGE_CUSTOM')) {
         $errorPage = constant('ERRORPAGE_CUSTOM');
     }
     if (defined('DATABASE_HOST') && defined('DATABASE_USER') && defined('DATABASE_PASS') && defined('DATABASE_PORT') && defined('DATABASE_NAME')) {
         $link = @mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME, DATABASE_PORT);
         if ($link) {
             $ip = @mysqli_real_escape_string($link, \System\HTTP\Visitor\IP::getClientIP());
             $serverIp = @mysqli_real_escape_string($link, \System\HTTP\Request\Request::getServerAddress());
             $query = @mysqli_real_escape_string($link, \System\HTTP\Request\Request::getQuery());
             $referer = @mysqli_real_escape_string($link, \System\HTTP\Request\Request::getReferer());
             $request = @mysqli_real_escape_string($link, \System\HTTP\Request\Request::getRequest());
             $errorNumber = @mysqli_real_escape_string($link, $errorLevel);
             $errorString = @mysqli_real_escape_string($link, $errorNotice);
             $escapedError = @mysqli_real_escape_string($link, $errorFile);
             $errorLine = @mysqli_real_escape_string($link, $errorLine);
             $escapedTrace = @mysqli_real_escape_string($link, $stackTrace);
             $post = new \System\HTTP\Request\Post();
             $postData = $post->serialize();
             $get = new \System\HTTP\Request\Get();
             $getData = $get->serialize();
             @mysqli_query($link, "INSERT INTO syserror (syserror_code, syserror_string, syserror_file, syserror_line, syserror_timestamp, syserror_server_ip, syserror_ip, syserror_query, syserror_referer, syserror_request, syserror_stacktrace, syserror_post, syserror_get)\n\t                VALUES ('{$errorNumber}', '{$errorString}', '{$escapedError}', '{$errorLine}', NOW(), '{$serverIp}', '{$ip}', '{$query}', '{$referer}', '{$request}', '{$escapedTrace}', '{$postData}', '{$getData}')");
             @mysqli_close($link);
             //remove extended notices for cli mode
             if (\System\Server\SAPI::getSAPI() == \System\Server\SAPI::SAPI_CLI) {
                 $errorPage = '{ERROR}';
             }
             if (!defined('DEBUG')) {
                 $errorPage = str_replace('{ERROR}', "<p><b>Details:</b></p><p>The page you requested could not be found. Please contact the webmaster.</p>", $errorPage);
             } else {
                 $errorPage = str_ireplace("{ERROR}", "<p><b>Details:</b></p><p><b>Errorcode</b>: " . $errorNumber . " - " . self::translateErrorNumber($errorNumber) . "</p><p><b>Error message</b>: " . $errorNotice . "</p><p><b>Errorneous file</b>: " . $errorFile . "</p><p><b>On line</b>: " . $errorLine . "</p><p><b>Stacktrace</b>:<br />" . nl2br(strip_tags($stackTrace)) . "</p>", $errorPage);
             }
             //remove tags for cli mode
             if (\System\Server\SAPI::getSAPI() == \System\Server\SAPI::SAPI_CLI) {
                 $errorPage = strip_tags(str_ireplace(array('<br />', '<br>', '<br/>', '</p><p>'), PHP_EOL, $errorPage));
             }
         }
     }
     //terminate; in debug mode we output potentially harmfull info without escaping!
     $errorPage = str_replace('{ERROR}', "<p><b>Details:</b></p><p>(ERR: 1001) The page you requested could not be found. Please contact the webmaster.</p>" . (defined('DEBUG') ? '<p>' . $errorNotice . '</p>' : ''), $errorPage);
     echo $errorPage;
 }