Skip to content

Commit

Permalink
Fixes, Update to DLE 14.1
Browse files Browse the repository at this point in the history
- Несколько фиксов (незначительные)
- Добавлена проверка на длину значения для типа string
- для пользователей были измененны методы создания и авторизации: api/v1/users/auth (для авторизации) и api/v1/users/register (для регистрации)
- Скрипт был обновлён для работы с DLE 14.1. На работу для версий постарше это никак не повлияет, поскольку были добавлены новые поля
  • Loading branch information
Gokujo committed Mar 31, 2021
1 parent 6531bdb commit 577e604
Show file tree
Hide file tree
Showing 56 changed files with 1,589 additions and 516 deletions.
132 changes: 68 additions & 64 deletions upload/api/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function checkAPI ($key, $name) {
try {
if (!empty($key) && !empty($name)) {
$keyCheck = $connect->query( "SELECT * FROM {$DLEprefix}_api_keys WHERE api = :key", array( 'key' => $key) );

if(!empty($keyCheck)) {
if($keyCheck[0]['is_admin'] && $keyCheck[0]['active'] === 1) {
$antwort = array(
Expand All @@ -66,8 +66,8 @@ function checkAPI ($key, $name) {
'delete' => true,
);
} else {
$tablesCheck = $connect->query( "SELECT * FROM {$DLEprefix}_api_scope

$tablesCheck = $connect->query( "SELECT * FROM {$DLEprefix}_api_scope
WHERE table = :name and key_id = :api", array( 'name' => $name, 'api' => $keyCheck[0]['api']) );

if ( count( $tablesCheck ) > 0 ) {
Expand Down Expand Up @@ -96,28 +96,32 @@ function checkAPI ($key, $name) {
}

function defType($value, $type = null) {
$output = null;
$output = null;

if ($type === 'integer') $output = intval($value);
elseif ($type === 'boolean') $output = boolval($value);
elseif ($type === 'double') $output = floatval($value);
else $output = "'{$value}'";
if ($type === 'integer') $output = intval($value);
elseif ($type === 'boolean') $output = boolval($value);
elseif ($type === 'double') $output = floatval($value);
else $output = "'{$value}'";

return $output;
}
return $output;
}

function checkLength($text, $max) {
return (strlen() > $max && $max !== 0) ? substr($text, 0, $max) : $text;
}

class CacheSystem {
class CacheSystem {
private $app, $data, $module, $id, $cachePath;

/**
* CacheSystem constructor.
*
* @param $module // Название таблицы
* @param string $id // Идентификационный набор символов
* @param string $data // Передаваемые и сохраняемые данные
* @param string $app // Тип кеша
* @param string $path // Путь кеша
*/
/**
* CacheSystem constructor.
*
* @param $module // Название таблицы
* @param string $id // Идентификационный набор символов
* @param string $data // Передаваемые и сохраняемые данные
* @param string $app // Тип кеша
* @param string $path // Путь кеша
*/
public function __construct ($module, $id = '', $data = '', $app = 'full', $path = ENGINE_DIR . '/cache') {
$this->data = $data;
$this->app = $app;
Expand All @@ -127,54 +131,54 @@ public function __construct ($module, $id = '', $data = '', $app = 'full', $path
$this->setCachePath($path);
}

/**
* @param string $cachePath
*/
public function setCachePath (string $cachePath) {
$this->cachePath = $cachePath;
}

/**
* @return string
*/
public function create() {
$file_name = "{$this->app}_{$this->module}_" . md5($this->id) .'.json';
file_put_contents($this->cachePath .'/'. $file_name, $this->data);

return $this->data;
}

/**
* @return bool|false|string
*/
public function get() {
$file_name = "{$this->app}_{$this->module}_" . md5($this->id) .'.json';
if (file_exists($this->cachePath .'/'. $file_name)) {
return file_get_contents($this->cachePath.'/'.$file_name);
}

return false;
}

/**
* @param string $app
*/
public function clear($app = '') {
$pattern = (empty($app)) ? '*' : $this->app .'_'. $app . '_*';
$pattern .= '.json';
/**
* @param string $cachePath
*/
public function setCachePath (string $cachePath) {
$this->cachePath = $cachePath;
}

/**
* @return string
*/
public function create() {
$file_name = "{$this->app}_{$this->module}_" . md5($this->id) .'.json';
file_put_contents($this->cachePath .'/'. $file_name, $this->data);

return $this->data;
}

/**
* @return bool|false|string
*/
public function get() {
$file_name = "{$this->app}_{$this->module}_" . md5($this->id) .'.json';
if (file_exists($this->cachePath .'/'. $file_name)) {
return file_get_contents($this->cachePath.'/'.$file_name);
}

return false;
}

/**
* @param string $app
*/
public function clear($app = '') {
$pattern = (empty($app)) ? '*' : $this->app .'_'. $app . '_*';
$pattern .= '.json';
try {
foreach (glob($this->cachePath .'/'.$pattern) as $filename) {
unlink($filename);
}
} catch (Exception $e) {
throw new Error($e->getMessage());
}
}

/**
* @param mixed $data
*/
public function setData ($data) {
$this->data = $data;
}
}
}

/**
* @param mixed $data
*/
public function setData ($data) {
$this->data = $data;
}
}
Empty file.
14 changes: 11 additions & 3 deletions upload/api/routes/_sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
// 'type' => "Type of value", // integer, string, boolean, double
// 'required' => true/false, // Обязательное поле?
// 'post' => true/false, // Разрешить использовать при добавлении или редактуре?
// 'length' => 0, // Указывается ограничение для типа string. Содержимое будет обрезаться при нарушении макс. значения
// );
// Оставляем строчку как ориентир
// possibleData Add
Expand Down Expand Up @@ -162,7 +163,7 @@

$names[] = $name;
// проверяем тип данных
$values[] = defType($value, $keyData['type']);
$values[] = defType(checkLength($value, $keyData['length']), $keyData['type']);

}
}
Expand Down Expand Up @@ -226,7 +227,14 @@

foreach ( $body as $name => $value ) {
if ( defType($value) !== null && in_array($name, $possibleData)) {
$values[] = "{$name} = " . defType($value);
$keyNum = array_search($name, array_column($possibleData, 'name'));

if ($keyNum !== false) {
$keyData = $possibleData[$keyNum];

$values[] ="{$name} = " . defType(checkLength($value, $keyData['length']), $keyData['type']);

}
}
}
$values = implode(', ', $values);
Expand Down Expand Up @@ -291,4 +299,4 @@

// Отметка для добавления своих методов и функций
// Own routing Add
});
});
18 changes: 16 additions & 2 deletions upload/api/routes/admin_logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,42 @@
'type' => "integer",
'required' => false,
'post' => false,
'length' => 0
),
array(
'name' => 'name',
'type' => "string",
'required' => false,
'post' => true,
'length' => 40
),
array(
'name' => 'date',
'type' => "integer",
'required' => false,
'post' => true,
'length' => 0
),
array(
'name' => 'ip',
'type' => "string",
'required' => false,
'post' => true,
'length' => 46
),
array(
'name' => 'action',
'type' => "integer",
'required' => false,
'post' => true,
'length' => 0
),
array(
'name' => 'extras',
'type' => "string",
'required' => true,
'post' => true,
'length' => 0
),
);

Expand All @@ -54,6 +60,7 @@
// 'type' => "Type of value",
// 'required' => true/false, // Обязательное поле?
// 'post' => true/false, // Разрешить использовать при добавлении или редактуре?
// 'length' => 0, // Указывается ограничение для типа string. Содержимое будет обрезаться при нарушении макс. значения
// );
// possibleData Add );

Expand Down Expand Up @@ -151,7 +158,7 @@
return $response->withStatus(400)->getBody()->write(json_encode(array('error' => "Требуемая информация отсутствует: {$name}!")));

$names[] = $name;
$values[] = defType($value, $keyData['type']);
$values[] = defType(checkLength($value, $keyData['length']), $keyData['type']);

}
}
Expand Down Expand Up @@ -211,7 +218,14 @@

foreach ( $body as $name => $value ) {
if ( defType($value) !== null && in_array($name, $possibleData)) {
$values[] = "{$name} = " . defType($value);
$keyNum = array_search($name, array_column($possibleData, 'name'));

if ($keyNum !== false) {
$keyData = $possibleData[$keyNum];

$values[] ="{$name} = " . defType(checkLength($value, $keyData['length']), $keyData['type']);

}
}
}
$values = implode(', ', $values);
Expand Down
Loading

0 comments on commit 577e604

Please sign in to comment.