Critical fixes

This commit is contained in:
Dennis Eichhorn 2017-11-11 13:02:38 +01:00
parent 59267c4b08
commit 6987f3adc2
6 changed files with 142 additions and 123 deletions

View File

@ -555,12 +555,18 @@ class Account implements ArrayableInterface, \JsonSerializable
* @param string $password Password
*
* @return void
*
* @throws \Exception
*
* @since 1.0.0
*/
public function generatePassword(string $password) /* : void */
{
$this->password = \password_hash($password, \PASSWORD_DEFAULT);
if($this->password === false) {
throw new \Exception();
}
}
/**

View File

@ -163,8 +163,7 @@ class FileCache implements CacheInterface
return false;
}
$path = File::sanitize($key, self::SANITIZE);
$path = $this->cachePath . '/' . trim($path, '/') . '.cache';
$path = $this->getPath($key);
if (!File::exists($path)) {
File::put($path, $this->build($value, $expire));
@ -276,8 +275,7 @@ class FileCache implements CacheInterface
return null;
}
$name = File::sanitize($key, self::SANITIZE);
$path = $this->cachePath . '/' . trim($name, '/') . '.cache';
$path = $this->getPath($key);
if (!File::exists($path)) {
return null;
@ -303,6 +301,11 @@ class FileCache implements CacheInterface
return null;
}
return $this->parseValue($raw, $expireEnd);
}
private function parseValue(string $raw, int $expireEnd)
{
$value = null;
switch ($type) {
@ -343,8 +346,7 @@ class FileCache implements CacheInterface
return false;
}
$name = File::sanitize($key, self::SANITIZE);
$path = $this->cachePath . '/' . trim($name, '/') . '.cache';
$path = $this->getPath($key);
if ($expire < 0 && File::exists($path)) {
File::delete($path);
@ -406,8 +408,7 @@ class FileCache implements CacheInterface
return false;
}
$name = File::sanitize($key, self::SANITIZE);
$path = $this->cachePath . '/' . trim($path, '/') . '.cache';
$path = $this->getPath($key);
if (File::exists($path)) {
File::put($path, $this->build($value, $expire));
@ -417,4 +418,10 @@ class FileCache implements CacheInterface
return false;
}
private function getPath($key) : string
{
$path = File::sanitize($key, self::SANITIZE);
return $this->cachePath . '/' . trim($path, '/') . '.cache';
}
}

View File

@ -453,24 +453,26 @@ class FileLogger implements LoggerInterface
{
$levels = [];
if (file_exists($this->path)) {
$this->fp = fopen($this->path, 'r');
fseek($this->fp, 0);
if (!file_exists($this->path)) {
return $levels;
}
while (($line = fgetcsv($this->fp, 0, ';')) !== false) {
$line[1] = trim($line[1]);
$this->fp = fopen($this->path, 'r');
fseek($this->fp, 0);
if (!isset($levels[$line[1]])) {
$levels[$line[1]] = 0;
}
while (($line = fgetcsv($this->fp, 0, ';')) !== false) {
$line[1] = trim($line[1]);
$levels[$line[1]]++;
if (!isset($levels[$line[1]])) {
$levels[$line[1]] = 0;
}
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
$levels[$line[1]]++;
}
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
return $levels;
}
@ -485,25 +487,27 @@ class FileLogger implements LoggerInterface
{
$connection = [];
if (file_exists($this->path)) {
$this->fp = fopen($this->path, 'r');
fseek($this->fp, 0);
if (!file_exists($this->path)) {
return 0;
}
while (($line = fgetcsv($this->fp, 0, ';')) !== false) {
$line[2] = trim($line[2]);
$this->fp = fopen($this->path, 'r');
fseek($this->fp, 0);
if (!isset($connection[$line[2]])) {
$connection[$line[2]] = 0;
}
while (($line = fgetcsv($this->fp, 0, ';')) !== false) {
$line[2] = trim($line[2]);
$connection[$line[2]]++;
if (!isset($connection[$line[2]])) {
$connection[$line[2]] = 0;
}
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
asort($connection);
$connection[$line[2]]++;
}
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
asort($connection);
return array_slice($connection, 0, $limit);
}
@ -521,36 +525,37 @@ class FileLogger implements LoggerInterface
$id = 0;
if (file_exists($this->path)) {
$this->fp = fopen($this->path, 'r');
fseek($this->fp, 0);
return $logs;
}
while (($line = fgetcsv($this->fp, 0, ';')) !== false) {
$id++;
$this->fp = fopen($this->path, 'r');
fseek($this->fp, 0);
if ($offset > 0) {
$offset--;
continue;
}
while (($line = fgetcsv($this->fp, 0, ';')) !== false) {
$id++;
if ($limit <= 0) {
reset($logs);
unset($logs[key($logs)]);
}
foreach ($line as &$value) {
$value = trim($value);
}
$logs[$id] = $line;
$limit--;
ksort($logs);
if ($offset > 0) {
$offset--;
continue;
}
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
if ($limit <= 0) {
reset($logs);
unset($logs[key($logs)]);
}
foreach ($line as &$value) {
$value = trim($value);
}
$logs[$id] = $line;
$limit--;
ksort($logs);
}
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
return $logs;
}
@ -567,34 +572,35 @@ class FileLogger implements LoggerInterface
$current = 0;
if (file_exists($this->path)) {
$this->fp = fopen($this->path, 'r');
fseek($this->fp, 0);
return $log;
}
while (($line = fgetcsv($this->fp, 0, ';')) !== false && $current <= $id) {
$current++;
$this->fp = fopen($this->path, 'r');
fseek($this->fp, 0);
if ($current < $id) {
continue;
}
while (($line = fgetcsv($this->fp, 0, ';')) !== false && $current <= $id) {
$current++;
$log['datetime'] = trim($line[0] ?? '');
$log['level'] = trim($line[1] ?? '');
$log['ip'] = trim($line[2] ?? '');
$log['line'] = trim($line[3] ?? '');
$log['version'] = trim($line[4] ?? '');
$log['os'] = trim($line[5] ?? '');
$log['path'] = trim($line[6] ?? '');
$log['message'] = trim($line[7] ?? '');
$log['file'] = trim($line[8] ?? '');
$log['backtrace'] = trim($line[9] ?? '');
break;
if ($current < $id) {
continue;
}
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
$log['datetime'] = trim($line[0] ?? '');
$log['level'] = trim($line[1] ?? '');
$log['ip'] = trim($line[2] ?? '');
$log['line'] = trim($line[3] ?? '');
$log['version'] = trim($line[4] ?? '');
$log['os'] = trim($line[5] ?? '');
$log['path'] = trim($line[6] ?? '');
$log['message'] = trim($line[7] ?? '');
$log['file'] = trim($line[8] ?? '');
$log['backtrace'] = trim($line[9] ?? '');
break;
}
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
return $log;
}

View File

@ -472,13 +472,8 @@ class ModuleManager
return true;
} catch (PathException $e) {
// todo: handle module doesn't exist or files are missing
//echo $e->getMessage();
return false;
} catch (\Exception $e) {
//echo $e->getMessage();
return false;
}
}

View File

@ -165,29 +165,27 @@ class File extends FileAbstract implements FileInterface
*/
public static function created(string $path) : \DateTime
{
if (!file_exists($path)) {
throw new PathException($path);
}
$created = new \DateTime();
$created->setTimestamp(filemtime($path));
return $created;
return $this->createFileTime($path, filemtime($path));
}
/**
* {@inheritdoc}
*/
public static function changed(string $path) : \DateTime
{
return $this->createFileTime($path, filectime($path));
}
private static function createFileTime(string $path, int $time)
{
if (!file_exists($path)) {
throw new PathException($path);
}
$changed = new \DateTime();
$changed->setTimestamp(filectime($path));
$fileTime = new \DateTime();
$fileTime->setTimestamp($time);
return $changed;
return $fileTime;
}
/**
@ -285,25 +283,13 @@ class File extends FileAbstract implements FileInterface
*/
public static function move(string $from, string $to, bool $overwrite = false) : bool
{
if (!is_file($from)) {
throw new PathException($from);
$result = self::copy($from, $to, $overwrite);
if(!$result) {
return false;
}
if ($overwrite || !file_exists($to)) {
if (!Directory::exists(dirname($to))) {
Directory::create(dirname($to), 0755, true);
}
if ($overwrite && file_exists($to)) {
unlink($to);
}
rename($from, $to);
return true;
}
return false;
return self::delete($from);
}
/**

View File

@ -220,23 +220,24 @@ class Repository
* Create repository
*
* @param string $source Create repository from source (optional, can be remote)
* @param bool $bare Bare repository
*
* @return string
*
* @throws \Exception
*
* @since 1.0.0
*/
public function create(string $source = null, bool $bare = false)
public function create(string $source = null)
{
if (!is_dir($this->path) || file_exists($this->path . '/.git')) {
throw new \Exception('Already repository');
}
if (isset($source)) {
$this->clone($source);
} else {
$this->init($bare);
return stripos($haystack, '//') ? $this->cloneRemote($source) : $this->cloneFrom($source);
}
return $this->run('init');
}
/**
@ -264,11 +265,7 @@ class Repository
*/
public function add($files = '*') : string
{
if (is_array($files)) {
$files = '"' . implode('" "', $files) . '"';
} elseif (!is_string($files)) {
throw new \Exception('Wrong type');
}
$files = $this->parseFileList($files);
return implode("\n", $this->run('add ' . $files . ' -v'));
}
@ -281,19 +278,33 @@ class Repository
*
* @return string
*
* @throws \InvalidArgumentException
*
* @since 1.0.0
*/
public function rm($files = '*', bool $cached = false) : string
{
$files = $this->parseFileList($files);
return implode("\n", $this->run('rm ' . ($cached ? '--cached ' : '') . $files));
}
/**
* Remove file(s) from repository
*
* @param string|array $files Files to remove
*
* @throws \InvalidArgumentException
*
* @since 1.0.0
*/
private function parseFileList($files) : string
{
if (is_array($files)) {
$files = '"' . implode('" "', $files) . '"';
return '"' . implode('" "', $files) . '"';
} elseif (!is_string($files)) {
throw new \InvalidArgumentException('Wrong type for $files.');
}
return implode("\n", $this->run('rm ' . ($cached ? '--cached ' : '') . $files));
return $files;
}
/**
@ -734,6 +745,14 @@ class Repository
*/
public function getAdditionsRemovalsByContributor(Author $author, \DateTime $start = null, \DateTime $end = null) : array
{
if(!isset($start)) {
$start = new \DateTime('1900-01-01');
}
if(!isset($end)) {
$end = new \DateTime('now');
}
$addremove = ['added' => 0, 'removed' => 0];
$lines = $this->run('log --author=' . escapeshellarg($author->getName()) . ' --since="' . $start->format('Y-m-d') . '" --before="' . $end->format('Y-m-d') . '" --pretty=tformat: --numstat');
@ -756,7 +775,7 @@ class Repository
*/
public function getRemote() : string
{
return $this->run('config --get remote.origin.url');
return implode("\n", $this->run('config --get remote.origin.url'));
}
/**