Skip to content

Commit

Permalink
Совместимость InstantCMS 2.16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzegit committed Jun 20, 2023
1 parent f79a2e6 commit 13ae80e
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 242 deletions.
2 changes: 1 addition & 1 deletion clientExamples/instantcms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## Использование

Заполнить константы api_key, api_point, api_point_execute своими данными.
Заполнить константы api_key, api_point, api_point_execute своими данными (example.com заменить на свой домен).
Создать директорию /cache/api/. В ней будут кэшироваться ответы.

Вызовы можно осуществлять из любого места кода InstantCMS
Expand Down
27 changes: 14 additions & 13 deletions clientExamples/instantcms/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,27 @@ public static function getMethod($name, $params = [], $cacheable = false, $is_up
$api_point = self::getApiPoint();
}

$cache_file = cmsConfig::get('cache_path') . 'api/' . md5($name . serialize($params) . cmsCore::getLanguageName()) . '.dat';
if ($cacheable) {

$cache_file = cmsConfig::get('cache_path') . 'api/' . md5($name . serialize($params) . cmsCore::getLanguageName()) . '.dat';

if (is_readable($cache_file)) {

if ($cacheable && is_readable($cache_file)) {
$time_diff = (time() - filemtime($cache_file));

$time_diff = (time() - filemtime($cache_file));
if ($time_diff < self::cache_time) {

if ($time_diff < self::cache_time) {
$result = include $cache_file;

$result = include $cache_file;
if ($result) {
return $result;
} else {
unlink($cache_file);
}

if ($result) {
return $result;
} else {
unlink($cache_file);
}

} else {
unlink($cache_file);
}
}

Expand All @@ -123,9 +126,7 @@ public static function getMethod($name, $params = [], $cacheable = false, $is_up

} elseif (cmsUser::isLogged()) {

$user = cmsUser::getInstance();

curl_setopt($curl, CURLOPT_HTTPHEADER, ['Cookie: ' . $user->api_session_name . '=' . $user->api_session_id]);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Cookie: ' . cmsUser::sessionGet('user_session:session_name') . '=' . cmsUser::sessionGet('user_session:session_id')]);

} elseif (cmsUser::isSessionSet('guest_session:session_id')) {

Expand Down
6 changes: 3 additions & 3 deletions manifest.en.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ addon_id = "600"

[version]
major = "2"
minor = "3"
minor = "4"
build = "0"
date = "20200409"
date = "20230620"

[depends]
core = "2.11.0"
core = "2.14.0"

[author]
name = "InstantCMS Team"
Expand Down
6 changes: 3 additions & 3 deletions manifest.ru.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ addon_id = "600"

[version]
major = "2"
minor = "3"
minor = "4"
build = "0"
date = "20200409"
date = "20230620"

[depends]
core = "2.11.0"
core = "2.14.0"

[author]
name = "InstantCMS Team"
Expand Down
88 changes: 44 additions & 44 deletions package/system/controllers/api/actions/method.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/******************************************************************************/
// //
// InstantMedia //
// http://instantmedia.ru/, support@instantmedia.ru //
// written by Fuze //
// http://instantmedia.ru/ //
// written by Fuze //
// //
/******************************************************************************/

class actionApiMethod extends cmsAction {

private $method_params = array();
private $method_params = [];
private $method_controller_name = null;
private $method_action_name = null;

Expand All @@ -18,21 +18,19 @@ class actionApiMethod extends cmsAction {
* @var object
*/
private $method_controller = null;

/**
* Объект класса api метода
* @var object
*/
private $method_action = null;
private $method_action = null;

public function __construct($controller, $params=array()){
public function __construct($controller, $params = []) {

parent::__construct($controller, $params);

$this->loadApiKey();

// для метода after ставим коллбэк, нам не нужен вывод на экран шаблона
$this->setCallback('after', array(array($controller, 'renderJSON')));

}

/**
Expand All @@ -44,7 +42,10 @@ public function __construct($controller, $params=array()){
private function initMethod($method_name) {

$this->method_name = $method_name;
if(empty($this->method_name)){ return $this; }

if (!$this->method_name) {
return $this;
}

$segments = explode('.', $method_name);

Expand All @@ -53,40 +54,37 @@ private function initMethod($method_name) {

$this->method_controller_name = trim($segments[0]);

if ($this->method_controller_name && !preg_match('/^[a-z]{1}[a-z0-9_]*$/', $this->method_controller_name)){
if ($this->method_controller_name && !preg_match('/^[a-z]{1}[a-z0-9_]*$/', $this->method_controller_name)) {
$this->method_controller_name = null;
}

if ($this->method_controller_name && !cmsCore::isControllerExists($this->method_controller_name)) {
$this->method_controller_name = null;
}

if($this->method_controller_name){
if ($this->method_controller_name) {
$this->method_controller = cmsCore::getController($this->method_controller_name, $this->request);
}

}
// действие
if (isset($segments[1])) {

$this->method_action_name = trim($segments[1]);

if ($this->method_action_name && !preg_match('/^[a-z]{1}[a-z0-9_]*$/', $this->method_action_name)){
if ($this->method_action_name && !preg_match('/^[a-z]{1}[a-z0-9_]*$/', $this->method_action_name)) {
$this->method_action_name = null;
}

if($this->method_action_name && $this->method_controller !== null){
$this->method_controller->current_action = 'api_'.$this->method_controller_name.'_'.$this->method_action_name;
if ($this->method_action_name && $this->method_controller !== null) {
$this->method_controller->current_action = 'api_' . $this->method_controller_name . '_' . $this->method_action_name;
}

}
// Параметры действия
if (count($segments) > 2){
if (count($segments) > 2) {
$this->method_params = array_slice($segments, 2);
}

return $this;

}

/**
Expand Down Expand Up @@ -279,7 +277,6 @@ public function run($method_name = null){

// действия после успешного запроса
return $this->afterRequest();

}

/**
Expand All @@ -289,25 +286,25 @@ public function run($method_name = null){
private function afterRequest() {

// записываем в лог, если включено
if(!empty($this->options['log_success'])){
$this->model->log(array(
'request_time' => number_format(cmsCore::getTime(), 4),
'method' => $this->method_name,
'key_id' => $this->key['id']
));
if (!empty($this->options['log_success'])) {

$this->model->log([
'request_time' => number_format((microtime(true) - $this->start_time), 4),
'method' => $this->method_name,
'key_id' => $this->key['id']
]);
}

return true;

}

private function validateMethodParams() {

if(empty($this->method_action->request_params)){
if (empty($this->method_action->request_params)) {
return false;
}

$errors = array();
$errors = [];

// валидация аналогична валидации форм
foreach ($this->method_action->request_params as $param_name => $rules) {
Expand All @@ -320,44 +317,44 @@ private function validateMethodParams() {

$this->request->set($param_name, $value);

} elseif(!is_null($value) && isset($rules['default'])){
} elseif (!is_null($value) && isset($rules['default'])) {

$value = $this->request->get($param_name, $rules['default']);

// для применения типизации переменной
$this->request->set($param_name, $value);

}

if(!empty($rules['rules'])){
if (!empty($rules['rules'])) {
foreach ($rules['rules'] as $rule) {

if (!$rule) { continue; }
if (!$rule) {
continue;
}

$validate_function = "validate_{$rule[0]}";

$rule[] = $value;

unset($rule[0]);

$result = call_user_func_array(array($this, $validate_function), $rule);
$result = call_user_func_array([$this, $validate_function], $rule);

// если получилось false, то дальше не проверяем, т.к.
// ошибка уже найдена
if ($result !== true) {
$errors[$param_name] = $result;
break;
}

}
}

}

if (!sizeof($errors)) { return false; }
if (!sizeof($errors)) {
return false;
}

return $errors;

}

/**
Expand All @@ -367,23 +364,27 @@ private function validateMethodParams() {
public function checkRequest() {

$parent_succes = parent::checkRequest();
if(!$parent_succes){ return false; }

if(empty($this->method_name) ||
if (!$parent_succes) {
return false;
}

if (empty($this->method_name) ||
empty($this->method_controller_name) ||
$this->method_controller === null){
$this->method_controller === null) {

return $this->error(3);
}

if(empty($this->method_action_name)){
if (empty($this->method_action_name)) {
return $this->error(8);
}

if(!$this->method_controller->isEnabled()){
if (!$this->method_controller->isEnabled()) {
return $this->error(23);
}

$check_method_name = $this->method_controller_name.'.'.$this->method_action_name;
$check_method_name = $this->method_controller_name . '.' . $this->method_action_name;

$is_view = !$this->key['key_methods']['allow'] || in_array($check_method_name, $this->key['key_methods']['allow']);
$is_hide = $this->key['key_methods']['disallow'] && in_array($check_method_name, $this->key['key_methods']['disallow']);
Expand All @@ -394,7 +395,6 @@ public function checkRequest() {
}

return true;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function run($ctype_name){
if (empty($this->ctype['options']['list_on'])) { return; }

// параметры
$perpage = (empty($this->ctype['options']['limit']) ? content::perpage : $this->ctype['options']['limit']);
$perpage = (empty($this->ctype['options']['limit']) ? 10 : $this->ctype['options']['limit']);
$page = $this->request->get('page');
$hide_root = !empty($this->ctype['options']['is_empty_root']) && $this->cat['id'] == 1;

Expand Down
Loading

0 comments on commit 13ae80e

Please sign in to comment.