/** * 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; }
/** * 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; }
/** * Executes a query on the database system and tries to return it as a map * The result will be converted to a \System\Collection\Map * The first field will be used as the key while the second field will be the value * If the number of fields retrieved from the query does not equal 2 an exception will be thrown * @param Query The query to be executed * @return \System\Collection\Map A map */ public final function queryMap(\System\Db\Query $query) { $results = $this->query($query); $map = new \System\Collection\Map(); if (count($results->getFields()) == 2) { $fields = $results->getFields(); foreach ($results as $result) { $key = $fields[0]->name; $value = $fields[1]->name; $map->set($result->{$key}, $result->{$value}); } } else { throw new \System\Error\Exception\DatabaseQueryException('Given query should have 2 fields but has: ' . $results->getFields()); } return $map; }
/** * 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); } } }