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 * @param string $password Password
* *
* @return void * @return void
*
* @throws \Exception
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function generatePassword(string $password) /* : void */ public function generatePassword(string $password) /* : void */
{ {
$this->password = \password_hash($password, \PASSWORD_DEFAULT); $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; return false;
} }
$path = File::sanitize($key, self::SANITIZE); $path = $this->getPath($key);
$path = $this->cachePath . '/' . trim($path, '/') . '.cache';
if (!File::exists($path)) { if (!File::exists($path)) {
File::put($path, $this->build($value, $expire)); File::put($path, $this->build($value, $expire));
@ -276,8 +275,7 @@ class FileCache implements CacheInterface
return null; return null;
} }
$name = File::sanitize($key, self::SANITIZE); $path = $this->getPath($key);
$path = $this->cachePath . '/' . trim($name, '/') . '.cache';
if (!File::exists($path)) { if (!File::exists($path)) {
return null; return null;
@ -303,6 +301,11 @@ class FileCache implements CacheInterface
return null; return null;
} }
return $this->parseValue($raw, $expireEnd);
}
private function parseValue(string $raw, int $expireEnd)
{
$value = null; $value = null;
switch ($type) { switch ($type) {
@ -343,8 +346,7 @@ class FileCache implements CacheInterface
return false; return false;
} }
$name = File::sanitize($key, self::SANITIZE); $path = $this->getPath($key);
$path = $this->cachePath . '/' . trim($name, '/') . '.cache';
if ($expire < 0 && File::exists($path)) { if ($expire < 0 && File::exists($path)) {
File::delete($path); File::delete($path);
@ -406,8 +408,7 @@ class FileCache implements CacheInterface
return false; return false;
} }
$name = File::sanitize($key, self::SANITIZE); $path = $this->getPath($key);
$path = $this->cachePath . '/' . trim($path, '/') . '.cache';
if (File::exists($path)) { if (File::exists($path)) {
File::put($path, $this->build($value, $expire)); File::put($path, $this->build($value, $expire));
@ -417,4 +418,10 @@ class FileCache implements CacheInterface
return false; 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 = []; $levels = [];
if (file_exists($this->path)) { if (!file_exists($this->path)) {
$this->fp = fopen($this->path, 'r'); return $levels;
fseek($this->fp, 0); }
while (($line = fgetcsv($this->fp, 0, ';')) !== false) { $this->fp = fopen($this->path, 'r');
$line[1] = trim($line[1]); fseek($this->fp, 0);
if (!isset($levels[$line[1]])) { while (($line = fgetcsv($this->fp, 0, ';')) !== false) {
$levels[$line[1]] = 0; $line[1] = trim($line[1]);
}
$levels[$line[1]]++; if (!isset($levels[$line[1]])) {
$levels[$line[1]] = 0;
} }
fseek($this->fp, 0, SEEK_END); $levels[$line[1]]++;
fclose($this->fp);
} }
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
return $levels; return $levels;
} }
@ -485,25 +487,27 @@ class FileLogger implements LoggerInterface
{ {
$connection = []; $connection = [];
if (file_exists($this->path)) { if (!file_exists($this->path)) {
$this->fp = fopen($this->path, 'r'); return 0;
fseek($this->fp, 0); }
while (($line = fgetcsv($this->fp, 0, ';')) !== false) { $this->fp = fopen($this->path, 'r');
$line[2] = trim($line[2]); fseek($this->fp, 0);
if (!isset($connection[$line[2]])) { while (($line = fgetcsv($this->fp, 0, ';')) !== false) {
$connection[$line[2]] = 0; $line[2] = trim($line[2]);
}
$connection[$line[2]]++; if (!isset($connection[$line[2]])) {
$connection[$line[2]] = 0;
} }
fseek($this->fp, 0, SEEK_END); $connection[$line[2]]++;
fclose($this->fp);
asort($connection);
} }
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
asort($connection);
return array_slice($connection, 0, $limit); return array_slice($connection, 0, $limit);
} }
@ -521,36 +525,37 @@ class FileLogger implements LoggerInterface
$id = 0; $id = 0;
if (file_exists($this->path)) { if (file_exists($this->path)) {
$this->fp = fopen($this->path, 'r'); return $logs;
fseek($this->fp, 0); }
while (($line = fgetcsv($this->fp, 0, ';')) !== false) { $this->fp = fopen($this->path, 'r');
$id++; fseek($this->fp, 0);
if ($offset > 0) { while (($line = fgetcsv($this->fp, 0, ';')) !== false) {
$offset--; $id++;
continue;
}
if ($limit <= 0) { if ($offset > 0) {
$offset--;
reset($logs); continue;
unset($logs[key($logs)]);
}
foreach ($line as &$value) {
$value = trim($value);
}
$logs[$id] = $line;
$limit--;
ksort($logs);
} }
fseek($this->fp, 0, SEEK_END); if ($limit <= 0) {
fclose($this->fp); 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; return $logs;
} }
@ -567,34 +572,35 @@ class FileLogger implements LoggerInterface
$current = 0; $current = 0;
if (file_exists($this->path)) { if (file_exists($this->path)) {
$this->fp = fopen($this->path, 'r'); return $log;
fseek($this->fp, 0); }
while (($line = fgetcsv($this->fp, 0, ';')) !== false && $current <= $id) { $this->fp = fopen($this->path, 'r');
$current++; fseek($this->fp, 0);
if ($current < $id) { while (($line = fgetcsv($this->fp, 0, ';')) !== false && $current <= $id) {
continue; $current++;
}
$log['datetime'] = trim($line[0] ?? ''); if ($current < $id) {
$log['level'] = trim($line[1] ?? ''); continue;
$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); $log['datetime'] = trim($line[0] ?? '');
fclose($this->fp); $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; return $log;
} }

View File

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

View File

@ -165,29 +165,27 @@ class File extends FileAbstract implements FileInterface
*/ */
public static function created(string $path) : \DateTime public static function created(string $path) : \DateTime
{ {
if (!file_exists($path)) { return $this->createFileTime($path, filemtime($path));
throw new PathException($path);
}
$created = new \DateTime();
$created->setTimestamp(filemtime($path));
return $created;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function changed(string $path) : \DateTime 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)) { if (!file_exists($path)) {
throw new PathException($path); throw new PathException($path);
} }
$changed = new \DateTime(); $fileTime = new \DateTime();
$changed->setTimestamp(filectime($path)); $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 public static function move(string $from, string $to, bool $overwrite = false) : bool
{ {
if (!is_file($from)) { $result = self::copy($from, $to, $overwrite);
throw new PathException($from);
if(!$result) {
return false;
} }
if ($overwrite || !file_exists($to)) { return self::delete($from);
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;
} }
/** /**

View File

@ -220,23 +220,24 @@ class Repository
* Create repository * Create repository
* *
* @param string $source Create repository from source (optional, can be remote) * @param string $source Create repository from source (optional, can be remote)
* @param bool $bare Bare repository *
* @return string
* *
* @throws \Exception * @throws \Exception
* *
* @since 1.0.0 * @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')) { if (!is_dir($this->path) || file_exists($this->path . '/.git')) {
throw new \Exception('Already repository'); throw new \Exception('Already repository');
} }
if (isset($source)) { if (isset($source)) {
$this->clone($source); return stripos($haystack, '//') ? $this->cloneRemote($source) : $this->cloneFrom($source);
} else {
$this->init($bare);
} }
return $this->run('init');
} }
/** /**
@ -264,11 +265,7 @@ class Repository
*/ */
public function add($files = '*') : string public function add($files = '*') : string
{ {
if (is_array($files)) { $files = $this->parseFileList($files);
$files = '"' . implode('" "', $files) . '"';
} elseif (!is_string($files)) {
throw new \Exception('Wrong type');
}
return implode("\n", $this->run('add ' . $files . ' -v')); return implode("\n", $this->run('add ' . $files . ' -v'));
} }
@ -281,19 +278,33 @@ class Repository
* *
* @return string * @return string
* *
* @throws \InvalidArgumentException
*
* @since 1.0.0 * @since 1.0.0
*/ */
public function rm($files = '*', bool $cached = false) : string 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)) { if (is_array($files)) {
$files = '"' . implode('" "', $files) . '"'; return '"' . implode('" "', $files) . '"';
} elseif (!is_string($files)) { } elseif (!is_string($files)) {
throw new \InvalidArgumentException('Wrong type for $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 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]; $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'); $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 public function getRemote() : string
{ {
return $this->run('config --get remote.origin.url'); return implode("\n", $this->run('config --get remote.origin.url'));
} }
/** /**