Skip to content

Commit

Permalink
Merge branch 'release/v1.3.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schendel committed Aug 3, 2024
2 parents eb099b3 + 1115807 commit 9f50d74
Show file tree
Hide file tree
Showing 108 changed files with 23,765 additions and 563 deletions.
75 changes: 53 additions & 22 deletions AppApi.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function getModuleInfo() {
return [
'title' => 'AppApi',
'summary' => 'Module to create a REST API with ProcessWire',
'version' => '1.3.2',
'version' => '1.3.3',
'author' => 'Sebastian Schendel',
'icon' => 'terminal',
'href' => 'https://modules.processwire.com/modules/app-api/',
Expand Down Expand Up @@ -100,7 +100,7 @@ private function createDBTables() {
`key` varchar(100) NOT NULL,
`version` varchar(100) NOT NULL,
`description` TEXT,
`accessable_until` datetime,
`accessible_until` datetime,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;';

Expand Down Expand Up @@ -163,7 +163,9 @@ public function ___upgrade($fromVersion, $toVersion) {
$application->setTitle('My Rest-Application');
$application->setDescription('Application was automatically generated with information from an older module-version.');
}
} elseif (version_compare($fromVersion, '1.1.0', '<')) {
}

if (version_compare($fromVersion, '1.1.0', '<')) {
// Add default_application column to application
try {
$alterStatement = '
Expand All @@ -177,7 +179,9 @@ public function ___upgrade($fromVersion, $toVersion) {
} catch (\Exception $e) {
$this->error('Error altering db-tables: ' . $e->getMessage());
}
} elseif (version_compare($fromVersion, '1.1.0', '==') && version_compare($toVersion, '1.1.1', '==')) {
}

if (version_compare($fromVersion, '1.1.0', '==') && version_compare($toVersion, '1.1.1', '==')) {
// Add default_application column to application
try {
$alterStatement = '
Expand All @@ -191,7 +195,9 @@ public function ___upgrade($fromVersion, $toVersion) {
} catch (\Exception $e) {
$this->error('Error altering db-tables: ' . $e->getMessage());
}
} elseif (version_compare($fromVersion, '1.2.7', '<') && version_compare($toVersion, '1.2.6', '>')) {
}

if (version_compare($fromVersion, '1.2.7', '<') && version_compare($toVersion, '1.2.6', '>')) {
// Add default_application column to application
try {
$alterStatement = '
Expand All @@ -206,6 +212,22 @@ public function ___upgrade($fromVersion, $toVersion) {
$this->error('Error altering db-tables: ' . $e->getMessage());
}
}

if (version_compare($fromVersion, '1.3.3', '<') && version_compare($toVersion, '1.3.3', '>=')) {
// Rename accessable_until column to accessible_until
try {
$alterStatement = '
ALTER TABLE `' . self::tableApplications . '` RENAME COLUMN `accessable_until` TO `accessible_until`;
';

$datenbank = wire('database');
$datenbank->exec($alterStatement);

$this->notices->add(new NoticeMessage('Successfully Altered Database-Scheme.'));
} catch (\Exception $e) {
$this->error('Error altering db-tables: ' . $e->getMessage());
}
}
}

public function ___execute() {
Expand Down Expand Up @@ -787,26 +809,35 @@ public static function getAjaxOf($content) {
'filesize' => $content->filesize,
'filesizeStr' => $content->filesizeStr,
'page_id' => $content->page->id,
'ext' => $content->ext
'ext' => $content->ext,
'http_url' => $content->httpUrl
];

if ($content instanceof PageImage) {
$output['basename_mini'] = $content->size(600, 0)->basename;
$output['width'] = $content->width;
$output['height'] = $content->height;
$output['dimension_ratio'] = round($content->width / $content->height, 2);

if ($content->original) {
$output['original'] = [
'basename' => $content->original->basename,
'name' => $content->original->name,
'filesize' => $content->original->filesize,
'filesizeStr' => $content->original->filesizeStr,
'ext' => $content->original->ext,
'width' => $content->original->width,
'height' => $content->original->height,
'dimension_ratio' => round($content->original->width / $content->original->height, 2)
];
try {
$output['basename_mini'] = $content->size(600, 0)->basename;
$output['width'] = @$content->width;
$output['height'] = $content->height;
if (is_numeric($content->width) && !empty($content->width) && is_numeric($content->height) && !empty($content->height)) {
$output['dimension_ratio'] = round($content->width / $content->height, 2);
}

if ($content->original) {
$output['original'] = [
'basename' => $content->original->basename,
'name' => $content->original->name,
'filesize' => $content->original->filesize,
'filesizeStr' => $content->original->filesizeStr,
'ext' => $content->original->ext,
'width' => $content->original->width,
'height' => $content->original->height
];

if (is_numeric($content->original->width) && !empty($content->original->width) && is_numeric($content->original->height) && !empty($content->original->height)) {
$output['original']['dimension_ratio'] = round($content->original->width / $content->original->height, 2);
}
}
} catch (\Exception $e) {
}
}

Expand Down
74 changes: 52 additions & 22 deletions classes/Apikey.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace ProcessWire;

class Apikey extends WireData {
Expand All @@ -13,7 +12,7 @@ class Apikey extends WireData {
protected $key;
protected $version;
protected $description;
protected $accessableUntil;
protected $accessibleUntil;

public function __construct($import = []) {
$this->id = null;
Expand All @@ -25,7 +24,7 @@ public function __construct($import = []) {
$this->key = '';
$this->version = '';
$this->description = '';
$this->accessableUntil = null;
$this->accessibleUntil = null;

if (is_array($import) && wireCount($import) > 0) {
$this->import($import);
Expand Down Expand Up @@ -87,8 +86,11 @@ protected function import(array $values) {
$this->___setDescription($values['description']);
}

if (isset($values['accessable_until'])) {
$this->___setAccessableUntil($values['accessable_until']);
if (isset($values['accessible_until'])) {
$this->___setAccessibleUntil($values['accessible_until']);
} else if (isset($values['accessable_until'])) {
// Deprecated fallback:
$this->___setAccessibleUntil($values['accessible_until']);
}
}

Expand All @@ -100,11 +102,18 @@ public function ___isSaveable() {
}

public function ___isValid() {
return $this->isApplicationIDValid() && $this->isIDValid() && $this->isCreatedValid() && $this->isCreatedUserValid() && $this->isModifiedValid() && $this->isModifiedUserValid() && $this->isKeyValid() && $this->isVersionValid() && $this->isDescriptionValid() && $this->isAccessableUntilValid();
return $this->isApplicationIDValid() && $this->isIDValid() && $this->isCreatedValid() && $this->isCreatedUserValid() && $this->isModifiedValid() && $this->isModifiedUserValid() && $this->isKeyValid() && $this->isVersionValid() && $this->isDescriptionValid() && $this->isAccessibleUntilValid();
}

public function ___isAccessible() {
return $this->isValid() && ($this->getAccessibleUntil() === null || $this->getAccessibleUntil() > time());
}

/**
* @deprecated
*/
public function ___isAccessable() {
return $this->isValid() && ($this->getAccessableUntil() === null || $this->getAccessableUntil() > time());
return $this->isAccessible();
}

public function ___isNew() {
Expand Down Expand Up @@ -349,36 +358,57 @@ public function getDescription() {
return $this->description;
}

public function ___setAccessableUntil($accessableUntil) {
if (is_string($accessableUntil)) {
$accessableUntil = strtotime($accessableUntil);
public function ___setAccessibleUntil($accessibleUntil) {
if (is_string($accessibleUntil)) {
$accessibleUntil = strtotime($accessibleUntil);
}

if (!$accessableUntil || !is_integer($accessableUntil) || $accessableUntil <= 0) {
$accessableUntil = null;
if (!$accessibleUntil || !is_integer($accessibleUntil) || $accessibleUntil <= 0) {
$accessibleUntil = null;
}

if (!$this->isAccessableUntilValid($accessableUntil)) {
throw new ApikeyException('No valid accessable-until date');
if (!$this->isAccessibleUntilValid($accessibleUntil)) {
throw new ApikeyException('No valid accessible-until date');
}

$this->accessableUntil = $accessableUntil;
$this->accessibleUntil = $accessibleUntil;
if ($this->initiated) {
$this->modified = time();
$this->modifiedUser = $this->wire('user');
}
return $this->accessableUntil;
return $this->accessibleUntil;
}

public function isAccessableUntilValid($value = false) {
/**
* @deprecated
*/
public function ___setAccessableUntil($accessibleUntil) {
return $this->___setAccessibleUntil($accessibleUntil);
}

public function isAccessibleUntilValid($value = false) {
if ($value === false) {
$value = $this->accessableUntil;
$value = $this->accessibleUntil;
}
return $value === null || (is_integer($value) && $value > 0);
}

/**
* @deprecated
*/
public function isAccessableUntilValid($value = false) {
return $this->isAccessibleUntilValid($value);
}

public function ___getAccessibleUntil() {
return $this->accessibleUntil;
}

/**
* @deprecated
*/
public function ___getAccessableUntil() {
return $this->accessableUntil;
return $this->___getAccessibleUntil();
}

public function ___delete() {
Expand Down Expand Up @@ -417,7 +447,7 @@ public function ___save() {
':key' => $this->getKey(),
':version' => $this->getVersion(),
':description' => $this->getDescription(),
':accessable_until' => $this->getAccessableUntil() === null ? null : date('Y-m-d G:i:s', $this->getAccessableUntil())
':accessible_until' => $this->getAccessibleUntil() === null ? null : date('Y-m-d G:i:s', $this->getAccessibleUntil())
];

if (!$this->isNew()) {
Expand All @@ -426,7 +456,7 @@ public function ___save() {
$queryVars[':id'] = $this->getID();

try {
$query = $db->prepare('UPDATE `' . AppApi::tableApikeys . '` SET `application_id`=:application_id, `created_user_id`=:created_user_id, `created`=:created, `modified_user_id`=:modified_user_id, `modified`=:modified, `key`=:key, `version`=:version, `description`=:description, `accessable_until`=:accessable_until WHERE `id`=:id;');
$query = $db->prepare('UPDATE `' . AppApi::tableApikeys . '` SET `application_id`=:application_id, `created_user_id`=:created_user_id, `created`=:created, `modified_user_id`=:modified_user_id, `modified`=:modified, `key`=:key, `version`=:version, `description`=:description, `accessible_until`=:accessible_until WHERE `id`=:id;');
$query->closeCursor();
$query->execute($queryVars);
} catch (\Exception $e) {
Expand All @@ -439,7 +469,7 @@ public function ___save() {

// New apikey should be saved into db:
try {
$query = $db->prepare('INSERT INTO `' . AppApi::tableApikeys . '` (`application_id`,`id`, `created_user_id`, `created`,`modified_user_id`, `modified`, `key`, `version`, `description`, `accessable_until`) VALUES (:application_id, NULL, :created_user_id, :created, :modified_user_id, :modified, :key, :version, :description, :accessable_until);');
$query = $db->prepare('INSERT INTO `' . AppApi::tableApikeys . '` (`application_id`,`id`, `created_user_id`, `created`,`modified_user_id`, `modified`, `key`, `version`, `description`, `accessible_until`) VALUES (:application_id, NULL, :created_user_id, :created, :modified_user_id, :modified, :key, :version, :description, :accessible_until);');
$query->closeCursor();
$query->execute($queryVars);
$this->id = $db->lastInsertId();
Expand Down
3 changes: 1 addition & 2 deletions classes/AppApiHelper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace ProcessWire;

class AppApiHelper {
Expand Down Expand Up @@ -31,7 +30,7 @@ public static function checkAndSanitizeRequiredParameters($data, $params) {
$sanitizer = $sanitizer[1];
}

if (!method_exists(wire('sanitizer'), $sanitizer)) {
if (!method_exists(wire('sanitizer'), $sanitizer) && !method_exists(wire('sanitizer'), '___' . $sanitizer)) {
throw new AppApiException("Sanitizer: '$sanitizer' is no valid sanitizer", 400);
}

Expand Down
9 changes: 8 additions & 1 deletion classes/Apptoken.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,17 @@ public function ___isValid() {
return $this->isApplicationIDValid() && $this->isIDValid() && $this->isCreatedValid() && $this->isCreatedUserValid() && $this->isModifiedValid() && $this->isModifiedUserValid() && $this->isTokenIDValid() && $this->isUserValid() && $this->isLastUsedValid() && $this->isExpirationTimeValid() && $this->isNotBeforeTimeValid();
}

public function ___isAccessable() {
public function ___isAccessible() {
return $this->isValid() && $this->getNotBeforeTime() <= time() && ($this->getExpirationTime() === null || $this->getExpirationTime() > time());
}

/**
* @deprecated
*/
public function ___isAccessable() {
return $this->___isAccessible();
}

public function isNew() {
return empty($this->id);
}
Expand Down
13 changes: 7 additions & 6 deletions classes/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_once __DIR__ . '/AppApiHelper.php';

use \Firebase\JWT\JWT;
use Firebase\JWT\Key;

class Auth extends WireData {
protected $apikey = false;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function getApplication() {
}

public function isApikeyValid() {
return ($this->apikey instanceof Apikey && $this->apikey->isAccessable() && $this->application instanceof Application) || ($this->apikey === false && $this->application instanceof Application);
return ($this->apikey instanceof Apikey && $this->apikey->isAccessible() && $this->application instanceof Application) || ($this->apikey === false && $this->application instanceof Application);
}

public function getApikeyLog() {
Expand Down Expand Up @@ -217,7 +218,7 @@ public function ___getAccessToken() {
}

// throws exception if token is invalid:
$token = JWT::decode($tokenString, $this->application->getTokenSecret(), ['HS256']);
$token = JWT::decode($tokenString, new Key($this->application->getTokenSecret(), 'HS256'));
if (!is_object($token)) {
throw new AuthException('Invalid Token', 400);
}
Expand All @@ -236,7 +237,7 @@ public function ___getAccessToken() {
throw new AuthException('Invalid User', 400);
}

if (!$refreshtokenFromDB->isAccessable()) {
if (!$refreshtokenFromDB->isAccessible()) {
throw new RefreshtokenExpiredException();
}

Expand Down Expand Up @@ -302,7 +303,7 @@ public function ___doLogout() {
try {
$secret = $this->application->getAccesstokenSecret();

$token = JWT::decode($tokenString, $secret, ['HS256']);
$token = JWT::decode($tokenString, new Key($secret, 'HS256'));
} catch (\Firebase\JWT\ExpiredException $e) {
throw new AccesstokenExpiredException();
} catch (\Firebase\JWT\BeforeValidException $e) {
Expand Down Expand Up @@ -468,7 +469,7 @@ protected function ___handleToken($singleJwt = false) {
if (!$singleJwt) {
$secret = $this->application->getAccesstokenSecret();
}
$token = JWT::decode($tokenString, $secret, ['HS256']);
$token = JWT::decode($tokenString, new Key($secret, 'HS256'));
} catch (\Firebase\JWT\ExpiredException $e) {
throw new AccesstokenExpiredException();
} catch (\Firebase\JWT\BeforeValidException $e) {
Expand Down Expand Up @@ -502,7 +503,7 @@ protected function ___handleToken($singleJwt = false) {
throw new AccesstokenInvalidException();
}

if (!$refreshtokenFromDB->isAccessable()) {
if (!$refreshtokenFromDB->isAccessible()) {
throw new RefreshtokenExpiredException();
}

Expand Down
Loading

0 comments on commit 9f50d74

Please sign in to comment.