Removed timezone + API improvements

This commit is contained in:
Roman Hergenreder 2020-06-24 16:09:04 +02:00
parent 73d20b4b5c
commit 9904be687f
11 changed files with 48 additions and 44 deletions

@ -177,8 +177,11 @@ namespace Api\Groups {
} }
$id = $this->getParam("uid"); $id = $this->getParam("uid");
$sql = $this->user->getSQL(); if (in_array($id, DEFAULT_GROUPS)) {
return $this->createError("You cannot delete a default group.");
}
$sql = $this->user->getSQL();
$res = $sql->select($sql->count()) $res = $sql->select($sql->count())
->from("Group") ->from("Group")
->where(new Compare("uid", $id)) ->where(new Compare("uid", $id))

@ -26,7 +26,7 @@ namespace Api\Routes {
use Api\RoutesAPI; use Api\RoutesAPI;
use Driver\SQL\Column\Column; use Driver\SQL\Column\Column;
use Driver\SQL\Condition\CondBool; use Driver\SQL\Condition\CondBool;
use Driver\SQL\Condition\Regex; use Driver\SQL\Condition\CondRegex;
class Fetch extends RoutesAPI { class Fetch extends RoutesAPI {
@ -99,7 +99,7 @@ namespace Api\Routes {
->select("uid", "request", "action", "target", "extra") ->select("uid", "request", "action", "target", "extra")
->from("Route") ->from("Route")
->where(new CondBool("active")) ->where(new CondBool("active"))
->where(new Regex($request, new Column("request"))) ->where(new CondRegex($request, new Column("request")))
->limit(1) ->limit(1)
->execute(); ->execute();

@ -2,8 +2,6 @@
namespace Driver\SQL\Condition; namespace Driver\SQL\Condition;
use Driver\SQL\Column\Column;
class CondIn extends Condition { class CondIn extends Condition {
private string $column; private string $column;

@ -2,16 +2,19 @@
namespace Driver\SQL\Condition; namespace Driver\SQL\Condition;
class Regex extends Condition { abstract class CondKeyword extends Condition {
private $leftExpression; private $leftExpression;
private $rightExpression; private $rightExpression;
private string $keyword;
public function __construct($leftExpression, $rightExpression) { public function __construct($keyword, $leftExpression, $rightExpression) {
$this->leftExpression = $leftExpression; $this->leftExpression = $leftExpression;
$this->rightExpression = $rightExpression; $this->rightExpression = $rightExpression;
$this->keyword = $keyword;
} }
public function getLeftExp() { return $this->leftExpression; } public function getLeftExp() { return $this->leftExpression; }
public function getRightExp() { return $this->rightExpression; } public function getRightExp() { return $this->rightExpression; }
public function getKeyword() { return $this->keyword; }
} }

@ -0,0 +1,10 @@
<?php
namespace Driver\SQL\Condition;
class CondLike extends CondKeyword {
public function __construct($leftExpression, $rightExpression) {
parent::__construct("LIKE", $leftExpression, $rightExpression);
}
}

@ -0,0 +1,11 @@
<?php
namespace Driver\SQL\Condition;
class CondRegex extends CondKeyword {
public function __construct($leftExpression, $rightExpression) {
parent::__construct("REGEXP", $leftExpression, $rightExpression);
}
}

@ -13,7 +13,7 @@ use \Driver\SQL\Column\DateTimeColumn;
use Driver\SQL\Column\BoolColumn; use Driver\SQL\Column\BoolColumn;
use Driver\SQL\Column\JsonColumn; use Driver\SQL\Column\JsonColumn;
use Driver\SQL\Condition\Regex; use Driver\SQL\Condition\CondRegex;
use Driver\SQL\Expression\Add; use Driver\SQL\Expression\Add;
use Driver\SQL\Strategy\Strategy; use Driver\SQL\Strategy\Strategy;
use \Driver\SQL\Strategy\UpdateStrategy; use \Driver\SQL\Strategy\UpdateStrategy;
@ -304,16 +304,4 @@ class MySQL extends SQL {
public function getStatus() { public function getStatus() {
return mysqli_stat($this->connection); return mysqli_stat($this->connection);
} }
protected function buildCondition($condition, &$params) {
if($condition instanceof Regex) {
$left = $condition->getLeftExp();
$right = $condition->getRightExp();
$left = ($left instanceof Column) ? $this->columnName($left->getName()) : $this->addValue($left, $params);
$right = ($right instanceof Column) ? $this->columnName($right->getName()) : $this->addValue($right, $params);
return $left . " REGEXP " . $right;
} else {
return parent::buildCondition($condition, $params);
}
}
} }

@ -13,7 +13,7 @@ use \Driver\SQL\Column\DateTimeColumn;
use Driver\SQL\Column\BoolColumn; use Driver\SQL\Column\BoolColumn;
use Driver\SQL\Column\JsonColumn; use Driver\SQL\Column\JsonColumn;
use Driver\SQL\Condition\Regex; use Driver\SQL\Condition\CondRegex;
use Driver\SQL\Expression\Add; use Driver\SQL\Expression\Add;
use Driver\SQL\Strategy\Strategy; use Driver\SQL\Strategy\Strategy;
use Driver\SQL\Strategy\UpdateStrategy; use Driver\SQL\Strategy\UpdateStrategy;
@ -304,7 +304,7 @@ class PostgreSQL extends SQL {
} }
protected function buildCondition($condition, &$params) { protected function buildCondition($condition, &$params) {
if($condition instanceof Regex) { if($condition instanceof CondRegex) {
$left = $condition->getLeftExp(); $left = $condition->getLeftExp();
$right = $condition->getRightExp(); $right = $condition->getRightExp();
$left = ($left instanceof Column) ? $this->columnName($left->getName()) : $this->addValue($left, $params); $left = ($left instanceof Column) ? $this->columnName($left->getName()) : $this->addValue($left, $params);

@ -6,8 +6,9 @@ use Driver\SQL\Column\Column;
use Driver\SQL\Condition\Compare; use Driver\SQL\Condition\Compare;
use Driver\SQL\Condition\CondBool; use Driver\SQL\Condition\CondBool;
use Driver\SQL\Condition\CondIn; use Driver\SQL\Condition\CondIn;
use Driver\SQL\Condition\CondKeyword;
use Driver\SQL\Condition\CondOr; use Driver\SQL\Condition\CondOr;
use Driver\SQL\Condition\Regex; use Driver\SQL\Condition\CondRegex;
use Driver\SQL\Constraint\Constraint; use Driver\SQL\Constraint\Constraint;
use \Driver\SQL\Constraint\Unique; use \Driver\SQL\Constraint\Unique;
use \Driver\SQL\Constraint\PrimaryKey; use \Driver\SQL\Constraint\PrimaryKey;
@ -343,6 +344,13 @@ abstract class SQL {
$values = implode(",", $values); $values = implode(",", $values);
return $this->columnName($condition->getColumn()) . " IN ($values)"; return $this->columnName($condition->getColumn()) . " IN ($values)";
} else if($condition instanceof CondKeyword) {
$left = $condition->getLeftExp();
$right = $condition->getRightExp();
$keyword = $condition->getKeyword();
$left = ($left instanceof Column) ? $this->columnName($left->getName()) : $this->addValue($left, $params);
$right = ($right instanceof Column) ? $this->columnName($right->getName()) : $this->addValue($right, $params);
return "$left $keyword $right ";
} else { } else {
$this->lastError = "Unsupported condition type: " . get_class($condition); $this->lastError = "Unsupported condition type: " . get_class($condition);
return false; return false;

@ -7,6 +7,10 @@ const USER_GROUP_SUPPORT_NAME = "Support";
const USER_GROUP_ADMIN = 3; const USER_GROUP_ADMIN = 3;
const USER_GROUP_ADMIN_NAME = "Administrator"; const USER_GROUP_ADMIN_NAME = "Administrator";
const DEFAULT_GROUPS = array(
USER_GROUP_MODERATOR, USER_GROUP_SUPPORT, USER_GROUP_ADMIN
);
function GroupName($index) { function GroupName($index) {
$groupNames = array( $groupNames = array(
USER_GROUP_MODERATOR => USER_GROUP_MODERATOR_NAME, USER_GROUP_MODERATOR => USER_GROUP_MODERATOR_NAME,

@ -1,26 +1,5 @@
<?php <?php
function setTimezone($default) {
$timezone = "";
if (is_link("/etc/localtime")) {
$filename = readlink("/etc/localtime");
$pos = strpos($filename, "zoneinfo");
if ($pos) {
$timezone = substr($filename, $pos + strlen("zoneinfo/"));
} else {
$timezone = $default;
}
} else {
$timezone = file_get_contents("/etc/timezone");
if (!strlen($timezone)) {
$timezone = $default;
}
}
date_default_timezone_set($timezone);
}
setTimezone("UTC");
function getFirstWeekDayOfMonth($d = NULL) { function getFirstWeekDayOfMonth($d = NULL) {
if(is_null($d)) $d = date('Y-m-d H:i:s'); if(is_null($d)) $d = date('Y-m-d H:i:s');
$dt = new DateTime($d); $dt = new DateTime($d);