Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Check both Bearer and Authorization headers for token for GraphQL and Rest API

givanz6e4842b93a6d
diff --git a/admin/controller/content/categories.php b/admin/controller/content/categories.php
index 46c75ac..2173939 100755
--- a/admin/controller/content/categories.php
+++ b/admin/controller/content/categories.php
@@ -25,6 +25,7 @@ namespace Vvveb\Controller\Content;
use \Vvveb\Sql\categorySQL;
use function Vvveb\__;
use Vvveb\Controller\Base;
+use Vvveb\System\Images;
class Categories extends Base {
function delete() {
@@ -61,7 +62,8 @@ class Categories extends Base {
$categories = new categorySQL();
$options = [
- 'taxonomy_item' => $data,
+ 'taxonomy_item_content' => $data['taxonomy_item_content'],
+ 'taxonomy_item' => $data['taxonomy_item'],
] + $this->global;
if (isset($data['taxonomy_item_id']) && $data['taxonomy_item_id']) {
@@ -93,6 +95,11 @@ class Categories extends Base {
$taxonomy_id = $this->request->get['taxonomy_id'] ?? false;
$limit = 1000;
+ $admin_path = \Vvveb\adminPath();
+ $controllerPath = $admin_path . 'index.php?module=media/media';
+ $this->view->scanUrl = "$controllerPath&action=scan";
+ $this->uploadUrl = "$controllerPath&action=upload";
+
if ($taxonomy_id) {
$options = [
'start' => ($page - 1) * $limit,
@@ -110,6 +117,10 @@ class Categories extends Base {
$langs = $taxonomy_item['languages'] ? json_decode($taxonomy_item['languages'], true) : [];
$taxonomy_item['languages'] = [];
+ if (isset($taxonomy_item['image'])) {
+ $taxonomy_item['image_url'] = Images::image($taxonomy_item['image'], 'taxonomy_item');
+ }
+
if ($langs) {
foreach ($langs as $lang) {
$taxonomy_item['languages'][$lang['language_id']] = $lang;
diff --git a/app/component/categories.php b/app/component/categories.php
index 84616b7..04fb1b4 100755
--- a/app/component/categories.php
+++ b/app/component/categories.php
@@ -24,6 +24,7 @@ namespace Vvveb\Component;
use Vvveb\System\Component\ComponentBase;
use Vvveb\System\Event;
+use Vvveb\System\Images;
use function Vvveb\url;
class Categories extends ComponentBase {
@@ -37,7 +38,7 @@ class Categories extends ComponentBase {
'parent_id' => NULL,
'search' => NULL,
'type' => 'categories',
- 'post_type' => 'product',
+ 'post_type' => NULL,
'parents_only' => false,
'parents_children_only' => false,
'parents_without_children' => false,
@@ -56,8 +57,16 @@ class Categories extends ComponentBase {
$category['children'] = 0;
}
+ if (isset($category['post_type'])) {
+ $category['type'] = $category['post_type'];
+ }
+
$category['url'] = url('content/category/index', $category);
+ if (isset($category['image'])) {
+ $category['image_url'] = Images::image($category['image'], 'taxonomy_item');
+ }
+
if ($parent_id > 0 && isset($results['categories'][$parent_id])) {
$parent = &$results['categories'][$parent_id];
diff --git a/config/graphql.php b/config/graphql.php
index 570b833..e71d00f 100644
--- a/config/graphql.php
+++ b/config/graphql.php
@@ -25,7 +25,7 @@
'mode' => 'token', //token = admin added token, http = basic auth using admin user and password
],
'session' => [
- 'driver' => 'request',
+ 'driver' => 'request',//stateless, handle session externally if needed. Change to 'php' for session management if php session cookie can be sent with the request.
],
'email' => [
'driver' => 'mail',
diff --git a/config/rest.php b/config/rest.php
index 53dd576..dd4d67d 100644
--- a/config/rest.php
+++ b/config/rest.php
@@ -25,7 +25,7 @@
'mode' => 'token',//token = admin added token, http = basic auth using admin user and password
],
'session' => [
- 'driver' => 'request',
+ 'driver' => 'request',//stateless, handle session externally if needed. Change to 'php' for session management if php session cookie can be sent with the request.
],
'email' => [
'driver' => 'mail',
diff --git a/graphql/controller/error500.php b/graphql/controller/error500.php
index 5481956..55f9344 100644
--- a/graphql/controller/error500.php
+++ b/graphql/controller/error500.php
@@ -29,13 +29,13 @@ class Error500 extends Base {
if (DEBUG) {
return [
- 'message' => $this->view->message,
- 'file' => $this->view->file,
- 'line_no' => $this->view->line_no,
- 'line' => $this->view->line,
- 'lines' => $this->view->lines,
- 'trace' => $this->view->trace,
- 'codeLines' => $this->view->code,
+ 'message' => $this->view->message ?? '',
+ 'file' => $this->view->file ?? '',
+ 'line_no' => $this->view->line_no ?? '',
+ 'line' => $this->view->line ?? '',
+ 'lines' => $this->view->lines ?? '',
+ 'trace' => $this->view->trace ?? '',
+ 'codeLines' => $this->view->code ?? '',
'code' => 500,
];
} else {
diff --git a/rest/controller/base.php b/rest/controller/base.php
index 1b26e83..beace36 100644
--- a/rest/controller/base.php
+++ b/rest/controller/base.php
@@ -52,16 +52,67 @@ class Base {
$password = $_SERVER['PHP_AUTH_PW'] ?? '';
$loginData = compact('user', 'password');
- if ($userInfo = Admin::login($loginData)) {
+ if ($userInfo = Admin::login($loginData, [], $feedback)) {
} else {
$this->response->addHeader('WWW-Authenticate', 'Basic realm="REST Api"');
$this->response->addHeader('HTTP/1.0 401 Unauthorized');
- FrontController::notFound(false, __('Auth failed!'), 403);
+ $message = __('Auth failed!');
+
+ if (DEBUG) {
+ $message .= ' ' . $feedback['message'] ?? [];
+ $message .= " auth.mode = $authMode";
+ }
+ //FrontController::notFound(false, $message, 403);
+ $this->notFound($message, 403);
}
} else {
$this->response->addHeader('WWW-Authenticate', 'Basic realm="REST Api"');
$this->response->addHeader('HTTP/1.0 401 Unauthorized');
- FrontController::notFound(false, __('Auth failed!'), 403);
+ $message = __('Auth failed!');
+
+ if (DEBUG) {
+ $message .= __('No credentials!');
+ $message .= " auth.mode = $authMode";
+ }
+ //FrontController::notFound(false, $message, 403);
+ $this->notFound($message, 403);
+ }
+ } else {
+ if ($authMode == 'token') {
+ $headers = getallheaders();
+ $token = $headers['Bearer'] ?? $_SERVER['HTTP_BEARER'] ?? null;
+
+ if (! $token) {
+ $token = $headers['Authorization'] ?? $_SERVER['HTTP_AUTHORIZATION'] ?? '';
+ $token = trim(substr($token, 6)) ?: $this->request->get['_token'] ?? false;
+ }
+
+ if ($token) {
+ if ($userInfo = Admin::auth($token, [], $feedback)) {
+ } else {
+ $this->response->addHeader('HTTP/1.0 401 Unauthorized');
+ $message = __('Auth failed!');
+
+ if (DEBUG) {
+ $message .= ' ' . $feedback['message'] ?? [];
+ $message .= " auth.mode = $authMode";
+ $message .= " token = $token";
+ }
+ //FrontController::notFound(false, $message, 403);
+ $this->notFound($message, 403);
+ }
+ } else {
+ $this->response->addHeader('HTTP/1.0 401 Unauthorized');
+ $message = __('Auth failed!');
+
+ if (DEBUG) {
+ $message .= __('No credentials!');
+ $message .= " auth.mode = $authMode";
+ $message .= " token = $token";
+ }
+ //FrontController::notFound(false, $message, 403);
+ $this->notFound($message, 403);
+ }
}
}
}
@@ -80,7 +131,7 @@ class Base {
$this->response->setType('json');
if (! REST) {
- die($this->notFound(__('REST is disabled!'), 404));
+ $this->notFound(__('REST is disabled!'), 404);
}
$this->auth();
@@ -101,14 +152,21 @@ class Base {
list($site) = Event::trigger(__CLASS__, __FUNCTION__, $site);
- $this->global['site_id'] = SITE_ID ?? 1;
- $this->global['language_id'] = $this->session->get('language_id') ?? 1;
- $this->global['limit'] = 10;
- $this->global['start'] = 0;
- $this->global['user_id'] = $user['user_id'] ?? false;
- $this->global['user_group_id'] = $user['user_group_id'] ?? 1;
- $this->global['site'] = &$site;
- $this->global['user'] = $user ?? [];
+ $this->global['site_id'] = SITE_ID ?? 1;
+ $this->global['language_id'] = $this->session->get('language_id') ?? $site['language_id'] ?? 1;
+ $this->global['default_language_id'] = $this->session->get('default_language_id') ?? $site['language_id'] ?? 1;
+ $this->global['limit'] = 10;
+ $this->global['start'] = 0;
+ $this->global['user_id'] = $user['user_id'] ?? false;
+ $this->global['user_group_id'] = $user['user_group_id'] ?? 1;
+ $this->global['site'] = &$site;
+ $this->global['user'] = $user ?? [];
+
+ $language_id = $this->global['language_id'];
+
+ if (isset($site['description'][$language_id])) {
+ $site['description'] = $site['description'][$language_id];
+ }
if (isset($site['country_id'])) {
$this->initEcommerce($site['country_id'], $site['region_id']);
@@ -136,6 +194,8 @@ class Base {
$response = Response::getInstance();
http_response_code($statusCode);
- return $response->output(is_array($message) ? $message : ['message' => $message]);
+ $response->output(is_array($message) ? $message : ['message' => $message, 'code' => $statusCode]);
+
+ die();
}
}
diff --git a/rest/controller/crud.php b/rest/controller/crud.php
index 5beb88f..c9e7c5c 100644
--- a/rest/controller/crud.php
+++ b/rest/controller/crud.php
@@ -56,7 +56,7 @@ class Crud extends Base {
function index() {
$data = $this->get();
- if (! $data) {
+ if (! $data || (! isset($data[$this->type . '_id']))) {
return $this->notFound(sprintf(__('%s not found!'), ucfirst($this->type)));
}
diff --git a/rest/controller/error500.php b/rest/controller/error500.php
index 5481956..55f9344 100644
--- a/rest/controller/error500.php
+++ b/rest/controller/error500.php
@@ -29,13 +29,13 @@ class Error500 extends Base {
if (DEBUG) {
return [
- 'message' => $this->view->message,
- 'file' => $this->view->file,
- 'line_no' => $this->view->line_no,
- 'line' => $this->view->line,
- 'lines' => $this->view->lines,
- 'trace' => $this->view->trace,
- 'codeLines' => $this->view->code,
+ 'message' => $this->view->message ?? '',
+ 'file' => $this->view->file ?? '',
+ 'line_no' => $this->view->line_no ?? '',
+ 'line' => $this->view->line ?? '',
+ 'lines' => $this->view->lines ?? '',
+ 'trace' => $this->view->trace ?? '',
+ 'codeLines' => $this->view->code ?? '',
'code' => 500,
];
} else {