/** * Retrieves the container folder and optionally creates a new folder if required. * @param Directory The base path to use * @param string The name of the container. Uses the first CONTAINER_NAME_USAGE_LENGTH characters of the string. If the string is smaller, Its get zero-padded. * @param bool True to create the folder if it does not exists, false otherwise. * @return Directory Returns the requested path with the container components */ protected static function getContainerFolder(\System\IO\Directory $baseFolder, $container, $createIfNotExists) { $containerNameUsage = self::getContainerNameParts($container); $containerNameParts = str_split($containerNameUsage); $containerPath = implode(\System\IO\Directory::getSeparator(), $containerNameParts); $basePath = new \System\IO\Directory($baseFolder->getCurrentPath() . $containerPath, false); if (!$basePath->exists() && $createIfNotExists) { $oldMask = umask(0); mkdir($basePath->getCurrentPath(), \System\IO\Directory::FULL_ACCESS, true); umask($oldMask); } if (!$basePath->exists()) { throw new \System\Error\Exception\FileNotFoundException('Could not create the given folder: ' . $basePath->getCurrentPath()); } return $basePath; }
/** * Returns the hashes for the given files in the given folder. * This function gives the hash per file in the folder. * @param \System\IO\Directory the directory to get the hashes from * @return \System\Collection\Map A map with hashes per file */ public static final function getFileFingerprint(\System\IO\Directory $directory) { $fileHashes = new \System\Collection\Map(); //new \System\Collection\Vector('php') $files = $directory->getFiles(); foreach ($files as $file) { /** @var \System\IO\File */ $file = $file; $descriptors = new \System\Collection\Map(); $descriptors->hash = $file->getHash(); $descriptors->modifiedTime = $file->getModifiedTime(); $descriptors->filesizeBytes = $file->getFileSizeInBytes(); $descriptors->filesizeKBytes = $file->getFileSizeInKiloBytes(); $fileHashes->set($file->getFilename(), $descriptors); } return $fileHashes; }
public function __construct_5($code, $fontFolder, $minFontSize, $maxFontSize, $maxRotation) { if (!function_exists('imagettftext')) { throw new \System\Error\Exception\SystemException('Requires FreeType support. Please enable PHP FreeType'); } $this->fontFolder = $fontFolder; $this->ttfFiles = \System\IO\Directory::walkDir($fontFolder, new \System\Collection\Vector('ttf')); $this->width = (strlen($code) + 1) * intval(($minFontSize + $maxFontSize) / self::CAPTCHA_WIDTH_FACTOR); $this->height = intval(self::CAPTCHA_HEIGHT_FACTOR * $maxFontSize); $this->minFontSize = $minFontSize; $this->maxFontSize = $maxFontSize; $this->maxRotation = $maxRotation; $this->image = imagecreatetruecolor($this->width, $this->height); $bgcolor = imagecolorallocate($this->image, mt_rand(224, 255), mt_rand(224, 255), mt_rand(224, 255)); imagefill($this->image, 0, 0, $bgcolor); $this->createBackground(); $chars = str_split($code); $xPos = mt_rand($minFontSize, $maxFontSize); foreach ($chars as $char) { $size = mt_rand($minFontSize, $maxFontSize); $rotation = mt_rand($maxRotation * -1, $maxRotation); $yPos = mt_rand(intval($size * self::CAPTCHA_WIDTH_FACTOR), intval($this->height - $size / 4)); $r = mt_rand(0, 127); $g = mt_rand(0, 127); $b = mt_rand(0, 127); $color = imagecolorallocate($this->image, $r, $g, $b); $shadow = imagecolorallocate($this->image, $r + 127, $g + 127, $b + 127); $ttfFile = $this->getNextTTFFile(); imagettftext($this->image, $size, $rotation, $xPos + mt_rand(1, 5), $yPos + mt_rand(0, 5), $shadow, $ttfFile, $char); imagettftext($this->image, $size, $rotation, $xPos, $yPos, $color, $ttfFile, $char); $xPos += intval($size + $minFontSize / 3); } $watermarkImage = imagecreatefromstring(base64_decode($this->refreshIcon)); $origSX = imagesx($this->image); $origSY = imagesy($this->image); $waterSX = imagesx($watermarkImage); $waterSY = imagesy($watermarkImage); $destX = $origSX - $waterSX - 5; $destY = $origSY - $waterSY - 5; imagecopy($this->image, $watermarkImage, $destX, $destY, 0, 0, $waterSX, $waterSY); imagedestroy($watermarkImage); }
/** * This function only needs the be called once every run to load and to parse the XML file * This call is made automatically. Successive calls will instantly return. */ public static final function prepareObject() { //initialize the xmltree map for the first time in the system //all the objects in the system use the same map as it is static //we do this inline so we can check for the existance of the entry itself instead of using validateMap() if (self::$xmlTree == null) { self::$xmlTree = new \System\Collection\Map(); } $key = get_called_class(); if (!isset(self::$xmlTree[$key])) { //we get the xml file using late static binding $xmlFile = \System\IO\Directory::getPath(static::getXMLSourceFile()); //parse the xml file and create an xml tree $xml = self::parseXML($xmlFile); //create the fields self::createFields($xml); //create the virtuals self::createVirtuals($xml); //create the queries and the conditions self::createQueries($xml); //store the xml and mark the class as completed by doing so self::$xmlTree[$key] = $xml; } }
/** * Transfer the file to its new location by copying it. * @param \System\IO\File The sourcefile. * @return bool True on success, false otherwise */ public final function transferFile(\System\IO\File $file) { $target = \System\IO\Directory::getPath($this->targetFolder . $file->getFilename()); return \System\IO\File::writeContents($target, $file->getContents()) instanceof \System\IO\File; }
/** * Creates a fingerprint of the entire system and returns it as an encoded json string. This fingerprint only gives information about the system version. * By using this fingerprint, we can identify the versions used in the currently deployed build * Note that the used encryption is not a secure one. * @param string The key used to encode the resultset and generate the hashes. This key is required for decryption. * @return string The encoded fingerprint */ public static final function getSystemFingerprint($encodeKey = \System\Version::FINGERPRINT_KEY) { $baseDir = PATH_SYSTEM; $files = \System\IO\Directory::walkDir($baseDir, new \System\Collection\Vector('php')); $map = new \System\Collection\Map(); foreach ($files as $file) { $hash = new \System\Security\Hash(\System\Security\Hash::HASH_SHA512); $hash->addFile($file); $map[\System\Security\Base64Encoding::Base64Encode($file->stripBase(PATH_SYSTEM), $encodeKey)] = $hash->getHash(); } $jsonObject = json_encode($map->getArrayCopy()); $encoded = \System\Security\XOREncoding::XOREncrypt($jsonObject, $encodeKey); return $encoded; }
/** * Process the file upload and publish the posted data to the given targets. * Do note: this function returns True if all placements in the targets are succesfull. * @return bool True on success, false otherwise. */ public final function process() { if (isset($_FILES[$this->formFieldname])) { if (is_uploaded_file($_FILES[$this->formFieldname]['tmp_name'])) { //the file is uploaded succesfully //check for filesize if ($_FILES[$this->formFieldname]['size'] <= $this->maxFileSize) { //check for fileextension if (in_array(strtolower(pathinfo($_FILES[$this->formFieldname]['name'], PATHINFO_EXTENSION)), $this->allowedExtensions)) { //no uploaderrors if ($_FILES[$this->formFieldname]['error'] == UPLOAD_ERR_OK) { $fullPath = \System\IO\Directory::getPath(PATH_TEMP . $this->targetFilename); if (@move_uploaded_file($_FILES[$this->formFieldname]['tmp_name'], $fullPath)) { $file = new \System\IO\File($fullPath); //cycle through all the targets and place them where desired $success = true; foreach ($this->targets as $target) { if (!$target->transferFile($file)) { $success = false; } } //remove the temporary file again $file->delete(); return $success; } } } } } } return false; }
/** * Combines the given blocks, and produces a Renderer to be added to a RenderSurface. * The Render is ready to be added to the surface. * @return \System\Output\Renderer The renderer to use. */ public final function getRenderer() { $xml = $this->generateXML(); switch ($this->getOutputRenderer()) { case self::OUTPUT_RENDERER_XML: $renderer = new \System\Output\Renderer\XMLRenderer(); $renderer->render($xml); break; case self::OUTPUT_RENDERER_XSL: //we raise the event so the system can perform any checks on the page before actually rendering $event = new Event\OnBeforeRenderEvent(); $event->setPage($this); $event->setXmlTree($xml); $event->raise($this); $renderer = new \System\Output\Renderer\XSLTRenderer(); $renderer->render($xml, \System\IO\Directory::getPath($this->getXSL()), $this->getPageOptions()); break; default: throw new \InvalidArgumentException('The given output renderer is invalid: ' . $this->getOutputRenderer()); } return $renderer; }
/** * This function is identical to the getFullPath() function, except that it strips the given * $base variable from the beginning of the file. If the given base is not found, it returns the regular getFullPath() result. * The base variable gets normalised before checking. * @param string The base to remove from the beginning * @return string The remaining fullpath string */ public final function stripBase($base) { $path = $this->getFullPath(); $base = \System\IO\Directory::normalize($base) ?: \System\IO\Directory::getPath($base); $startPos = stripos($path, $base); if ($startPos !== false && $startPos == 0) { return substr($path, strlen($base)); } return $path; }
/** * Stores the given codeblock to its appropriate file. It appends the block to the end of the file. * @param \System\IO\Directory The target base folder * @param \ReflectionClass The class whoms definition to store * @param string The code to place in the sourcefile. */ private static final function storeToFile(\System\IO\Directory $targetFolder, \ReflectionClass $class, $code) { //store the contents to a file $targetFolderStr = \System\IO\Directory::getPath($targetFolder->getCurrentPath(true) . strtolower($class->getNamespaceName()) . \System\IO\Directory::getSeparator()); if (!file_exists($targetFolderStr)) { mkdir($targetFolderStr, 0777, true); } $targetFileName = $targetFolderStr . basename($class->getFileName()); file_put_contents($targetFileName, $code, LOCK_EX | FILE_APPEND); }
/** * Listens to the EVENT_FILE_FINGERPRINT event and returns the fingerprint of the given folders * @param OnInteractionEvent The event to listen to */ public static final function fileFingerprint(\System\System\Interaction\Event\OnInteractionEvent $event) { $msg = $event->getMessage(); if ($msg->getType() == \System\System\Interaction\MessageType::TYPE_SYSTEM && $msg->getMessage() == SystemInteractionEventEvent::EVENT_FILE_FINGERPRINT) { $params = $msg->getParams(); if (isset($params['folders'])) { $folders = $params['folders']; $map = new \System\Collection\Map(); foreach ($folders as $folder) { $directory = new \System\IO\Directory($folder, false); if ($directory) { $content = \System\System\Introspection\IO::getFileFingerprint($directory); $subFolders = new \System\Collection\Vector(); foreach ($directory->getDirectories() as $subFolder) { /** @var \System\IO\Directory */ $subFolder = $subFolder; $subFolders[] = \System\IO\Directory::getPath($subFolder->getCurrentPath(true), '/'); } $content->set('folders', $subFolders); $map->set($folder, $content); } } $response = new \System\System\Interaction\Response($msg, $map); $event->addResponse($response); } } }