Example #1
0
 /**
  * Returns the method used.
  * A valid value from \System\HTTP\Request\Method must be used, or METHOD_GET will be returned
  * @return string The method used.
  */
 public static final function getMethod()
 {
     $handle = self::getServerHandle();
     $method = $handle['REQUEST_METHOD'];
     $val = new \System\Security\Validate();
     if ($val->inStruct($method, 'method', new \System\HTTP\Request\Method(), true) == \System\Security\ValidateResult::VALIDATE_OK) {
         return $method;
     }
     return \System\HTTP\Request\Method::METHOD_GET;
 }
 /**
  * Converts a RGB code to hex value, preceded by #
  * @param integer The red component
  * @param integer The green component
  * @param integer The blue component
  * @param string The converted hex value
  * @return boolean True on succes, false otherwise
  */
 public static final function RGBToHex($r, $g, $b, &$hex = '')
 {
     $val = new \System\Security\Validate();
     $val->isInt($r, 'red', 0, 255, true);
     $val->isInt($g, 'green', 0, 255, true);
     $val->isInt($b, 'blue', 0, 255, true);
     if ($val->isInputOk()) {
         $hex = '#';
         $hex .= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
         $hex .= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
         $hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
         return true;
     }
     return false;
 }
 /**
  * Applies the image effect on a GD resource. This function should be overridden by the effect.
  * @param resource The image resource to work with
  * @return resource The resource to use
  */
 protected final function executeFilterGD($imageData)
 {
     $width = imagesx($imageData);
     $height = imagesy($imageData);
     //we make sure we cannot go out of bounds
     if ($this->reflectionHeight > $height) {
         $this->reflectionHeight = $height;
     }
     //create a new image with alpha channels
     $image = imagecreatetruecolor($width, $height + $this->reflectionHeight + $this->gradientDistance - 1);
     imagesavealpha($image, true);
     //use the proper color for the background and fill
     if (!empty($this->reflectionBackgroundColor)) {
         $val = new \System\Security\Validate();
         if ($val->isHexColor($this->reflectionBackgroundColor, 'color', true) == \System\Security\ValidateResult::VALIDATE_OK) {
             $r = 0;
             $g = 0;
             $b = 0;
             \System\Image\ColorConversion::hexToRGB($this->reflectionBackgroundColor, $r, $g, $b);
             $transparentColor = imagecolorallocatealpha($image, $r, $g, $b, 0);
         }
     } else {
         $transparentColor = imagecolorallocatealpha($image, 0, 0, 0, 127);
     }
     imagefill($image, 0, 0, $transparentColor);
     //copy the original image into the newimage
     imagecopy($image, $imageData, 0, 0, 0, 0, $width, $height);
     $transparencyStep = 127 / $this->reflectionHeight;
     //copy the bottom $reflectionHeight pixels of the image and make them transparent
     for ($y = $height - 1; $y >= $height - $this->reflectionHeight; $y--) {
         for ($x = 0; $x < $width; $x++) {
             $colors = imagecolorsforindex($imageData, imagecolorat($imageData, $x, $y));
             $alpha = intval(round($transparencyStep * ($height - $y)));
             if ($colors['alpha'] > $alpha) {
                 $alpha = $colors['alpha'];
             }
             $newColor = imagecolorallocatealpha($image, $colors['red'], $colors['green'], $colors['blue'], $alpha);
             imagesetpixel($image, $x, $height + ($height - $y) + $this->gradientDistance - 1, $newColor);
         }
     }
     imagedestroy($imageData);
     return $image;
 }
Example #4
0
 /**
  * Outputs the given image object as a PNG image. The output of this renderer can be written to any RenderSurface.
  * The image will be rendered with no filters applied.
  * @param \System\Image\Image The image to render as a PNG image.
  * @param int The amount of compression used. Default is 0. Range is 0..9
  */
 public final function render()
 {
     $args = func_get_args();
     if (count($args) != 1 && count($args) != 2 || !$args[0] instanceof \System\Image\Image) {
         throw new \InvalidArgumentException('Invalid amount of arguments given.');
     }
     $output = '';
     $compression = 0;
     if (count($args) == 2) {
         $compression = $args[1];
     }
     $val = new \System\Security\Validate();
     if ($val->isInt($compression, 'compressionlevel', 0, 9, true) != \System\Security\ValidateResult::VALIDATE_OK) {
         throw new \InvalidArgumentException('Second parameter should be between 0..9');
     }
     ob_start();
     imagepng($args[0]->getImageData(), null, $compression);
     $output = ob_get_contents();
     ob_end_clean();
     $this->addToBuffer($output);
 }
 public static final function call(\System\Collection\Map $serviceResult, \System\Db\Database $defaultDb)
 {
     self::validateHandles();
     $val = new \System\Security\Validate();
     $val->checkTextLength(self::$get->token, 'token', 10, true);
     $val->checkTextLength(self::$get->value, 'value', 1, true);
     if ($val->isInputOk()) {
         $vec = new \System\Collection\Vector();
         $vec[] = self::$get->token;
         $vec[] = self::$get->value;
         $onetimecall = \Module\Onetimecall\Onetimecall::load($defaultDb, 'token_value', $vec, false);
         if ($onetimecall) {
             $serviceResult->onetimeCall = $onetimecall;
             $onetimecall->delete($defaultDb);
             return true;
         }
         $event = new \Module\Onetimecall\Event\OnOnetimecallFailEvent();
         $event->raise();
     }
     return false;
 }
Example #6
0
 /**
  * Dynamically loads objects from the relational database into xml based objects.
  * The object used must inherit from the \System\Base\DynamicBaseObj class.
  * @param \System\Db\Database The database object to query
  * @param string The full classname of the class to use as the base object.
  * @param string The condition string as described in the objects xml file
  * @param \System\Collection\Vector The parameters to be used in the condition. If an item in the vector is a vector itself, its items are imploded by ', ' and treated as a \System\Db\QueryType::TYPE_QUERY parameter
  * @param bool When true, the result will always be wrapped in a \System\Db\DatabaseResult vector, even if there is only one result.
  * @param bool When true, the secondary db connection pipe is used to read the data from.
  * @return mixed If there is one result, then only an instance of the requested object is returned; when there are multiple results a \System\Db\DatabaseResult vector is returned, also see the $alwaysUseContainer parameter. If there are no results, null is returned
  */
 public static final function load(\System\Db\Database $db, $className, $condition, \System\Collection\Vector $parameters = null, $alwaysUseContainer = false, $useSecondaryDatabasePipe = true)
 {
     $event = new \System\Event\Event\OnBeforeDynamicObjectLoadEvent();
     $event->setDatabase($db)->setDynamicClassName($className)->setCondition($condition)->setParameters($parameters ?: new \System\Collection\Vector())->setAlwaysUseContainer($alwaysUseContainer)->setUseSecondaryDatabasePipe($useSecondaryDatabasePipe);
     $event->raise();
     //if the event has listeners that want to override the call, we redirect it and call that instead, this will logically re-fire the event!
     if ($event->hasListeners('\\System\\Event\\Event\\OnBeforeDynamicObjectLoadEvent') && ($event->getDatabase() != $db || $event->getDynamicClassName() != $className || $event->getCondition() != $condition || !(!$parameters && $event->getParameters()->count() == 0 || $parameters == $event->getParameters()) || $event->getAlwaysUseContainer() != $alwaysUseContainer || $event->getUseSecondaryDatabasePipe() != $useSecondaryDatabasePipe)) {
         return self::load($event->getDatabase(), $event->getDynamicClassName(), $event->getCondition(), $event->getParameters(), $event->getAlwaysUseContainer(), $event->getUseSecondaryDatabasePipe());
     }
     if (!class_exists($className) || !is_subclass_of($className, '\\System\\Base\\DynamicBaseObj')) {
         throw new \System\Error\Exception\ObjectLoaderSourceException('The given class ' . $className . ' does not appear to be a valid child of \\System\\Base\\DynamicBaseObj or does not exist (is the Module loaded?).');
     }
     call_user_func(array($className, 'prepareObject'));
     $queryString = call_user_func(array($className, 'queryForCondition'), $condition);
     $query = new \System\Db\Query($db, $queryString);
     $query->setResultType($className);
     //Use or dont use the secondary connection pipe
     $query->setUseSecondaryPipe($useSecondaryDatabasePipe);
     /*
     we need the validator to check the type of the value.
     this is needed, because an integer can also be string containing numbers.
     */
     $val = new \System\Security\Validate();
     if ($parameters) {
         foreach ($parameters as $index => $param) {
             //we need to decide the type of the parameter. Currently we only support integers and strings and Vectors.
             $type = \System\Db\QueryType::TYPE_INTEGER;
             //if the item is a Vector, we implode the vector and add it as a \System\Db\QueryType::TYPE_QUERY type parameter.
             if ($param instanceof \System\Collection\Vector) {
                 $type = \System\Db\QueryType::TYPE_QUERY;
                 $param = $param->convertToString();
             } else {
                 if ($val->isInt($param, $index, null, null, true) == \System\Security\ValidateResult::VALIDATE_INVALIDVALUE) {
                     $type = \System\Db\QueryType::TYPE_STRING;
                 }
             }
             $query->bind($param, $type);
         }
     }
     //actually execute the query
     $results = $db->query($query);
     //if there is only 1 result, then we just return that instead of the entire Vector
     if ($results->count() == 1 && !$alwaysUseContainer) {
         return $results->current();
     }
     //we return null if there are no results
     if ($results->count() == 0 && !$alwaysUseContainer) {
         return null;
     }
     return $results;
 }
Example #7
0
 /**
  * Gets the internet host name corresponding to a given IP address. This may pose a delay as it does a remoteserver request.
  * On the default IP address, the current client IP is reversed.
  * @param string The ip (V4) address to lookup.
  * @return string The hostname on success, the original ip on failure, or false on invalid input
  */
 public static final function getHostName($ipAddress = self::IP_INVALID)
 {
     if ($ipAddress == self::IP_INVALID) {
         $ipAddress = self::getClientIP();
     }
     $val = new \System\Security\Validate();
     if ($ipAddress == self::IP_INVALID || $val->isIPAddress($ipAddress, 'ip', true, true, false, true, false) != \System\Security\ValidateResult::VALIDATE_OK) {
         return self::IP_INVALID;
     }
     return gethostbyaddr($ipAddress);
 }
Example #8
0
 /**
  * Stores the current object back to the database. This function only does an incremental update, meaning that only the changed fields are updated.
  * The update reflects the changes made to the object, and does not consider updates to the database in the time between retrieval of this object
  * and the calling of this function.
  * This update is executed without the use of transactions.
  * @param string The condition to use for the update
  * @param \System\Collection\Vector The parameters for the condition
  * @return integer The amount of affected rows
  */
 public function store($condition, \System\Collection\Vector $parameters)
 {
     $fieldMap = self::validateMap(self::$fieldMap);
     $conditionMap = self::validateMap(self::$conditionMap);
     $modifications = $this->validateInstanceMap($this->modifications);
     $virtualModifications = $this->validateInstanceMap($this->virtualModifications);
     $dataMap = $this->validateInstanceMap($this->data);
     //we  dont do anything if we dont have anything to do, no modifications
     if (!$modifications->hasItems() && !$virtualModifications->hasItems()) {
         return 0;
     }
     if (!$conditionMap->keyExists($condition)) {
         throw new \System\Error\Exception\ObjectLoaderSourceException('Invalid condition given. Condition is not defined in ' . $this->getClassName() . '.');
     }
     //create the query
     $tuples = new \System\Collection\Vector();
     foreach ($modifications as $modification) {
         $dataField = $fieldMap->{$modification};
         $tuples[] = "`" . mb_strtolower((string) $dataField['dbkey']) . "` = %?%";
     }
     //iterate over all the virtual modifications as we do want to reflect those
     foreach ($virtualModifications as $virtualModification) {
         $tuples[] = "`" . mb_strtolower($virtualModification) . "` = %?%";
     }
     //we get the table definitions again. could have saved this at load, but now we reduce memory footprint at the cost of neglectable slower saving.
     $querySources = self::createQuerySources(self::$xmlTree[get_class($this)]);
     $conditionString = $conditionMap->{$condition};
     $sql = 'UPDATE ' . $querySources . ' SET ' . $tuples->convertToString() . ' ' . $conditionString;
     $query = new \System\Db\Query($this->getDatabase(), $sql);
     //bind the values to the query for setting the new values
     foreach ($modifications as $modification) {
         $fieldData = $fieldMap->{$modification};
         $modification = mb_strtolower($modification);
         //get the type of the parameter. only integers and strings are supported
         switch (mb_strtolower((string) $fieldData['type'])) {
             case \System\Type::TYPE_BOOL:
             case \System\Type::TYPE_BOOLEAN:
             case \System\Type::TYPE_INT:
             case \System\Type::TYPE_INTEGER:
                 $type = \System\Db\QueryType::TYPE_INTEGER;
                 break;
             default:
                 $type = \System\Db\QueryType::TYPE_STRING;
         }
         //we first check if the new value is set to nullify, if so, we actually store NULL
         if (isset($fieldData['nullify']) && (string) $fieldData['nullify'] == $dataMap->{$modification}) {
             $query->bind('NULL', \System\Db\QueryType::TYPE_QUERY);
         } elseif (isset($fieldData['encoding'])) {
             switch (mb_strtolower((string) $fieldData['encoding'])) {
                 case self::ENCRYPTION_BASE64:
                     $type = \System\Db\QueryType::TYPE_STRING;
                     $query->bind(self::encodeBase64($dataMap->{$modification}), $type);
                     break;
                 case self::ENCRYPTION_XOR:
                     $type = \System\Db\QueryType::TYPE_STRING;
                     $query->bind(self::encodeXOR($dataMap->{$modification}), $type);
                     break;
                 case self::ENCRYPTION_AES:
                     $type = \System\Db\QueryType::TYPE_STRING;
                     $query->bind(self::encodeAES($dataMap->{$modification}), $type);
                     break;
                 default:
                     throw new \System\Error\Exception\ObjectLoaderSourceException('The given encryptionmethod is not supported.');
             }
         } else {
             $query->bind($dataMap->{$modification}, $type);
         }
     }
     //bind the values to the query for setting the new values
     foreach ($virtualModifications as $virtualModification) {
         $type = \System\Db\QueryType::TYPE_STRING;
         $virtualField = 'virtual_' . $virtualModification;
         $value = $dataMap->{$virtualField};
         $query->bind((string) $value, $type);
     }
     //bind the condition values to the query
     $val = new \System\Security\Validate();
     foreach ($parameters as $index => $param) {
         //we need to decide the type of the parameter. Currently we only support integers and strings.
         $type = \System\Db\QueryType::TYPE_INTEGER;
         if ($val->isInt($param, $index, null, null, true) == \System\Security\ValidateResult::VALIDATE_INVALIDVALUE) {
             $type = \System\Db\QueryType::TYPE_STRING;
         }
         $query->bind($param, $type);
     }
     //execute the query
     $this->getDatabase()->query($query);
     //reset the modified list, because we already stored this. No need to store the same things at successive calls.
     $modifications->clear();
     return $this->getDatabase()->getAffectedRows();
 }
Example #9
0
 /**
  * Sets the index of the resultset to a given index.
  * Note: this invalidates iterators and advanced the key to the next item
  * @param integer The index to point at.
  */
 public function pointTo($point)
 {
     $val = new \System\Security\Validate();
     if ($this->count() > 0 && $val->isInt($point, 'point', 0, null, true) == \System\Security\ValidateResult::VALIDATE_OK && $this->count() > $point) {
         $this->key = $point;
         $this->results->data_seek($point);
         $this->next();
     }
 }