diff --git a/composer.json b/composer.json index 005bb7c9..b2e48943 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,6 @@ } }, "require": { - "daita/my-small-php-tools": "dev-master", "gumlet/php-image-resize": "2.0.*", "friendica/json-ld": "^1.0" }, diff --git a/lib/AP.php b/lib/AP.php index bad3fdba..23f89a30 100644 --- a/lib/AP.php +++ b/lib/AP.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; use OCA\Social\Exceptions\SocialAppConfigException; @@ -174,7 +174,7 @@ class AP { AP::$activityPub = Server::get(AP::class); } catch (QueryException $e) { Server::get(LoggerInterface::class) - ->errir($e->getMessage(), ['exception' => $e]); + ->error($e->getMessage(), ['exception' => $e]); } } diff --git a/lib/Command/CheckInstall.php b/lib/Command/CheckInstall.php index 575a6fa4..e0a3712d 100644 --- a/lib/Command/CheckInstall.php +++ b/lib/Command/CheckInstall.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Command; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OC\Core\Command\Base; use OCA\Social\Db\StreamDestRequest; diff --git a/lib/Command/ExtendedBase.php b/lib/Command/ExtendedBase.php index c940cdc8..c7feb0a7 100644 --- a/lib/Command/ExtendedBase.php +++ b/lib/Command/ExtendedBase.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Command; -use daita\MySmallPhpTools\Exceptions\CacheItemNotFoundException; +use OCA\Social\Tools\Exceptions\CacheItemNotFoundException; use OC\Core\Command\Base; use OCA\Social\AP; use OCA\Social\Exceptions\ItemUnknownException; diff --git a/lib/Command/MigrateAlpha3.php b/lib/Command/MigrateAlpha3.php index cd7fc93f..8a9ee936 100644 --- a/lib/Command/MigrateAlpha3.php +++ b/lib/Command/MigrateAlpha3.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Command; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OC\Core\Command\Base; use OCA\Social\Db\CoreRequestBuilder; diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php index 945c8803..61a400df 100644 --- a/lib/Controller/ActivityPubController.php +++ b/lib/Controller/ActivityPubController.php @@ -30,10 +30,10 @@ declare(strict_types=1); namespace OCA\Social\Controller; -use daita\MySmallPhpTools\Traits\Nextcloud\nc20\TNC20Logger; -use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; -use daita\MySmallPhpTools\Traits\TAsync; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TNCLogger; +use OCA\Social\Tools\Traits\TNCDataResponse; +use OCA\Social\Tools\Traits\TAsync; +use OCA\Social\Tools\Traits\TStringTools; use Exception; use OCP\AppFramework\Http; use OCA\Social\AppInfo\Application; @@ -63,7 +63,7 @@ class ActivityPubController extends Controller { use TNCDataResponse; use TStringTools; use TAsync; - use TNC20Logger; + use TNCLogger; private SocialPubController $socialPubController; private FediverseService $fediverseService; diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 75df3637..a6dc9fba 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Controller; -use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; +use OCA\Social\Tools\Traits\TNCDataResponse; use Exception; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\AccountDoesNotExistException; diff --git a/lib/Controller/ConfigController.php b/lib/Controller/ConfigController.php index e0d9acd4..82110048 100644 --- a/lib/Controller/ConfigController.php +++ b/lib/Controller/ConfigController.php @@ -26,8 +26,8 @@ declare(strict_types=1); namespace OCA\Social\Controller; -use daita\MySmallPhpTools\Model\SimpleDataStore; -use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; +use OCA\Social\Tools\Model\SimpleDataStore; +use OCA\Social\Tools\Traits\TNCDataResponse; use Exception; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Service\ConfigService; diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index 0311eebf..20c64b79 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Controller; -use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TNCDataResponse; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\AccountDoesNotExistException; diff --git a/lib/Controller/NavigationController.php b/lib/Controller/NavigationController.php index 910ff0a0..1d4bbbca 100644 --- a/lib/Controller/NavigationController.php +++ b/lib/Controller/NavigationController.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Controller; -use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TNCDataResponse; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCP\AppFramework\Http; use OC\User\NoUserException; diff --git a/lib/Controller/OAuthController.php b/lib/Controller/OAuthController.php index b9be8560..b87bb966 100644 --- a/lib/Controller/OAuthController.php +++ b/lib/Controller/OAuthController.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Controller; -use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; +use OCA\Social\Tools\Traits\TNCDataResponse; use Exception; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\ClientException; diff --git a/lib/Controller/OStatusController.php b/lib/Controller/OStatusController.php index 0854df9c..c57cc8b3 100644 --- a/lib/Controller/OStatusController.php +++ b/lib/Controller/OStatusController.php @@ -31,9 +31,9 @@ declare(strict_types=1); namespace OCA\Social\Controller; -use daita\MySmallPhpTools\Exceptions\ArrayNotFoundException; -use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\ArrayNotFoundException; +use OCA\Social\Tools\Traits\TNCDataResponse; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\InvalidResourceException; diff --git a/lib/Controller/QueueController.php b/lib/Controller/QueueController.php index e18463c6..3cf05d1b 100644 --- a/lib/Controller/QueueController.php +++ b/lib/Controller/QueueController.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Controller; -use daita\MySmallPhpTools\Traits\TAsync; +use OCA\Social\Tools\Traits\TAsync; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Model\RequestQueue; diff --git a/lib/Controller/SocialPubController.php b/lib/Controller/SocialPubController.php index 1495689c..41b66b7c 100644 --- a/lib/Controller/SocialPubController.php +++ b/lib/Controller/SocialPubController.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Controller; -use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; +use OCA\Social\Tools\Traits\TNCDataResponse; use Exception; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\AccountDoesNotExistException; diff --git a/lib/Db/ActionsRequest.php b/lib/Db/ActionsRequest.php index 59ef8f4c..977b7f90 100644 --- a/lib/Db/ActionsRequest.php +++ b/lib/Db/ActionsRequest.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use DateTime; use Exception; use OCA\Social\Exceptions\ActionDoesNotExistException; diff --git a/lib/Db/ActionsRequestBuilder.php b/lib/Db/ActionsRequestBuilder.php index 39d60bdc..bf139db9 100644 --- a/lib/Db/ActionsRequestBuilder.php +++ b/lib/Db/ActionsRequestBuilder.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Exceptions\RowNotFoundException; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\RowNotFoundException; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Exceptions\ActionDoesNotExistException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Model\ActivityPub\ACore; diff --git a/lib/Db/ActorsRequestBuilder.php b/lib/Db/ActorsRequestBuilder.php index 7db688f0..42e3df21 100644 --- a/lib/Db/ActorsRequestBuilder.php +++ b/lib/Db/ActorsRequestBuilder.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Model\ActivityPub\Actor\Person; use OCP\DB\QueryBuilder\IQueryBuilder; diff --git a/lib/Db/CacheActorsRequestBuilder.php b/lib/Db/CacheActorsRequestBuilder.php index 55b24d6f..779c21bf 100644 --- a/lib/Db/CacheActorsRequestBuilder.php +++ b/lib/Db/CacheActorsRequestBuilder.php @@ -30,8 +30,8 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Exceptions\RowNotFoundException; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\RowNotFoundException; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Exceptions\CacheActorDoesNotExistException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Model\ActivityPub\Actor\Person; diff --git a/lib/Db/CacheDocumentsRequestBuilder.php b/lib/Db/CacheDocumentsRequestBuilder.php index 803f90fb..74b53269 100644 --- a/lib/Db/CacheDocumentsRequestBuilder.php +++ b/lib/Db/CacheDocumentsRequestBuilder.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Model\ActivityPub\Object\Document; use OCP\DB\QueryBuilder\IQueryBuilder; diff --git a/lib/Db/ClientRequest.php b/lib/Db/ClientRequest.php index 52725dee..ad8b0a12 100644 --- a/lib/Db/ClientRequest.php +++ b/lib/Db/ClientRequest.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use DateTime; use Exception; use OCA\Social\Exceptions\ClientNotFoundException; diff --git a/lib/Db/ClientRequestBuilder.php b/lib/Db/ClientRequestBuilder.php index 96f0453d..ec8b32b9 100644 --- a/lib/Db/ClientRequestBuilder.php +++ b/lib/Db/ClientRequestBuilder.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Exceptions\RowNotFoundException; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\RowNotFoundException; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCA\Social\Exceptions\ClientNotFoundException; use OCA\Social\Model\Client\SocialClient; diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index 69a5fab9..687c045d 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Exceptions\DateTimeException; +use OCA\Social\Tools\Exceptions\DateTimeException; use DateInterval; use DateTime; use Doctrine\DBAL\Query\QueryBuilder; @@ -46,6 +46,7 @@ use OCA\Social\Model\StreamAction; use OCA\Social\Service\ConfigService; use OCA\Social\Service\MiscService; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\DB\QueryBuilder\IQueryFunction; use OCP\IDBConnection; use OCP\IURLGenerator; use Psr\Log\LoggerInterface; @@ -573,14 +574,6 @@ class CoreRequestBuilder { $qb->andWhere($expr); } - - /** - * @param IQueryBuilder $qb - * @param string $field - * @param string $value - * @param bool $cs - case sensitive - * @param string $alias - */ protected function filterDBField( IQueryBuilder &$qb, string $field, string $value, bool $cs = true, string $alias = '' ) { @@ -588,21 +581,10 @@ class CoreRequestBuilder { $qb->andWhere($expr); } - - /** - * @param IQueryBuilder $qb - * @param string $field - * @param string $value - * @param bool $eq - true = limit, false = filter - * @param bool $cs - * @param string $alias - * - * @return string - */ protected function exprLimitToDBField( IQueryBuilder &$qb, string $field, string $value, bool $eq = true, bool $cs = true, string $alias = '' - ): string { + ): IQueryFunction { $expr = $qb->expr(); $pf = ''; @@ -627,32 +609,16 @@ class CoreRequestBuilder { } } - - /** - * @param IQueryBuilder $qb - * @param string $field - * @param int $value - * @param string $alias - */ protected function limitToDBFieldInt( IQueryBuilder &$qb, string $field, int $value, string $alias = '' - ) { + ): void { $expr = $this->exprLimitToDBFieldInt($qb, $field, $value, $alias); $qb->andWhere($expr); } - - /** - * @param IQueryBuilder $qb - * @param string $field - * @param int $value - * @param string $alias - * - * @return string - */ protected function exprLimitToDBFieldInt( IQueryBuilder &$qb, string $field, int $value, string $alias = '' - ): string { + ): IQueryFunction { $expr = $qb->expr(); $pf = ''; diff --git a/lib/Db/FollowsRequest.php b/lib/Db/FollowsRequest.php index 89f47d07..4a7ea538 100644 --- a/lib/Db/FollowsRequest.php +++ b/lib/Db/FollowsRequest.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use DateTime; use Exception; use OCA\Social\Exceptions\FollowNotFoundException; diff --git a/lib/Db/FollowsRequestBuilder.php b/lib/Db/FollowsRequestBuilder.php index d6b15e8b..a2d2b82e 100644 --- a/lib/Db/FollowsRequestBuilder.php +++ b/lib/Db/FollowsRequestBuilder.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Exceptions\RowNotFoundException; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\RowNotFoundException; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Exceptions\FollowNotFoundException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Model\ActivityPub\Object\Follow; diff --git a/lib/Db/HashtagsRequest.php b/lib/Db/HashtagsRequest.php index 07dfb8bd..06cb3aae 100644 --- a/lib/Db/HashtagsRequest.php +++ b/lib/Db/HashtagsRequest.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Exceptions\HashtagDoesNotExistException; /** diff --git a/lib/Db/HashtagsRequestBuilder.php b/lib/Db/HashtagsRequestBuilder.php index e75b4562..7b9e5a3c 100644 --- a/lib/Db/HashtagsRequestBuilder.php +++ b/lib/Db/HashtagsRequestBuilder.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCP\DB\QueryBuilder\IQueryBuilder; /** diff --git a/lib/Db/InstancesRequest.php b/lib/Db/InstancesRequest.php index 6cd5e386..b46bccbe 100644 --- a/lib/Db/InstancesRequest.php +++ b/lib/Db/InstancesRequest.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Exceptions\InstanceDoesNotExistException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\Instance; diff --git a/lib/Db/InstancesRequestBuilder.php b/lib/Db/InstancesRequestBuilder.php index ac59ca10..9b76be5d 100644 --- a/lib/Db/InstancesRequestBuilder.php +++ b/lib/Db/InstancesRequestBuilder.php @@ -30,8 +30,8 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Exceptions\RowNotFoundException; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\RowNotFoundException; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Exceptions\InstanceDoesNotExistException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Model\ActivityPub\ACore; diff --git a/lib/Db/RequestQueueRequestBuilder.php b/lib/Db/RequestQueueRequestBuilder.php index dd1089ba..aa53b66d 100644 --- a/lib/Db/RequestQueueRequestBuilder.php +++ b/lib/Db/RequestQueueRequestBuilder.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Model\RequestQueue; use OCP\DB\QueryBuilder\IQueryBuilder; diff --git a/lib/Db/SocialCoreQueryBuilder.php b/lib/Db/SocialCoreQueryBuilder.php index 30b66ffd..fc8f9644 100644 --- a/lib/Db/SocialCoreQueryBuilder.php +++ b/lib/Db/SocialCoreQueryBuilder.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Db\ExtendedQueryBuilder; +use OCA\Social\Tools\Db\ExtendedQueryBuilder; use OC\SystemConfig; use OCA\Social\Model\ActivityPub\Actor\Person; use OCP\DB\QueryBuilder\ICompositeExpression; diff --git a/lib/Db/SocialLimitsQueryBuilder.php b/lib/Db/SocialLimitsQueryBuilder.php index 213af905..218b3775 100644 --- a/lib/Db/SocialLimitsQueryBuilder.php +++ b/lib/Db/SocialLimitsQueryBuilder.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Exceptions\DateTimeException; +use OCA\Social\Tools\Exceptions\DateTimeException; use DateInterval; use DateTime; use Exception; diff --git a/lib/Db/StreamActionsRequestBuilder.php b/lib/Db/StreamActionsRequestBuilder.php index f7c69616..b4fd27df 100644 --- a/lib/Db/StreamActionsRequestBuilder.php +++ b/lib/Db/StreamActionsRequestBuilder.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Model\StreamAction; use OCP\DB\QueryBuilder\IQueryBuilder; diff --git a/lib/Db/StreamDestRequest.php b/lib/Db/StreamDestRequest.php index be0f318d..95f23758 100644 --- a/lib/Db/StreamDestRequest.php +++ b/lib/Db/StreamDestRequest.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TStringTools; use Exception; use OCP\DB\Exception as DBException; use OCA\Social\Model\ActivityPub\Internal\SocialAppNotification; diff --git a/lib/Db/StreamDestRequestBuilder.php b/lib/Db/StreamDestRequestBuilder.php index 239f0f40..61bac94e 100644 --- a/lib/Db/StreamDestRequestBuilder.php +++ b/lib/Db/StreamDestRequestBuilder.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCP\DB\QueryBuilder\IQueryBuilder; /** diff --git a/lib/Db/StreamQueueRequestBuilder.php b/lib/Db/StreamQueueRequestBuilder.php index 40c3819a..2f6f4b35 100644 --- a/lib/Db/StreamQueueRequestBuilder.php +++ b/lib/Db/StreamQueueRequestBuilder.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Model\StreamQueue; use OCP\DB\QueryBuilder\IQueryBuilder; diff --git a/lib/Db/StreamRequest.php b/lib/Db/StreamRequest.php index c67a990e..821c3616 100644 --- a/lib/Db/StreamRequest.php +++ b/lib/Db/StreamRequest.php @@ -30,8 +30,8 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Exceptions\DateTimeException; -use daita\MySmallPhpTools\Model\Cache; +use OCA\Social\Tools\Exceptions\DateTimeException; +use OCA\Social\Tools\Model\Cache; use DateTime; use Exception; use OCP\DB\Exception as DBException; diff --git a/lib/Db/StreamRequestBuilder.php b/lib/Db/StreamRequestBuilder.php index fa9d8198..ba3323de 100644 --- a/lib/Db/StreamRequestBuilder.php +++ b/lib/Db/StreamRequestBuilder.php @@ -31,9 +31,9 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Exceptions\CacheItemNotFoundException; -use daita\MySmallPhpTools\Exceptions\RowNotFoundException; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\CacheItemNotFoundException; +use OCA\Social\Tools\Exceptions\RowNotFoundException; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\AP; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemUnknownException; diff --git a/lib/Db/StreamTagsRequest.php b/lib/Db/StreamTagsRequest.php index 1a4f0bce..24697aed 100644 --- a/lib/Db/StreamTagsRequest.php +++ b/lib/Db/StreamTagsRequest.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TStringTools; use OCA\Social\Model\ActivityPub\Object\Note; use OCA\Social\Model\ActivityPub\Stream; use OCP\Server; diff --git a/lib/Db/StreamTagsRequestBuilder.php b/lib/Db/StreamTagsRequestBuilder.php index 4f6f70f9..cd85a676 100644 --- a/lib/Db/StreamTagsRequestBuilder.php +++ b/lib/Db/StreamTagsRequestBuilder.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Db; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCP\DB\QueryBuilder\IQueryBuilder; /** diff --git a/lib/Interfaces/Actor/PersonInterface.php b/lib/Interfaces/Actor/PersonInterface.php index d189ca61..5a5c1c63 100644 --- a/lib/Interfaces/Actor/PersonInterface.php +++ b/lib/Interfaces/Actor/PersonInterface.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Interfaces\Actor; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Db\CacheActorsRequest; use OCA\Social\Db\FollowsRequest; use OCA\Social\Db\StreamRequest; diff --git a/lib/Interfaces/Object/AnnounceInterface.php b/lib/Interfaces/Object/AnnounceInterface.php index 9a0dff28..c5642391 100644 --- a/lib/Interfaces/Object/AnnounceInterface.php +++ b/lib/Interfaces/Object/AnnounceInterface.php @@ -31,9 +31,9 @@ declare(strict_types=1); namespace OCA\Social\Interfaces\Object; -use daita\MySmallPhpTools\Exceptions\CacheItemNotFoundException; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\CacheItemNotFoundException; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCA\Social\AP; use OCA\Social\Db\ActionsRequest; @@ -44,11 +44,11 @@ use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Exceptions\UnauthorizedFediverseException; diff --git a/lib/Interfaces/Object/FollowInterface.php b/lib/Interfaces/Object/FollowInterface.php index 20b5ea9d..725a9417 100644 --- a/lib/Interfaces/Object/FollowInterface.php +++ b/lib/Interfaces/Object/FollowInterface.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Interfaces\Object; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Exceptions\MalformedArrayException; use Exception; use OCA\Social\AP; use OCA\Social\Db\FollowsRequest; @@ -41,11 +41,11 @@ use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Interfaces\Activity\AbstractActivityPubInterface; use OCA\Social\Interfaces\IActivityPubInterface; diff --git a/lib/Model/ActivityPub/ACore.php b/lib/Model/ActivityPub/ACore.php index 2c2f47e6..00feb456 100644 --- a/lib/Model/ActivityPub/ACore.php +++ b/lib/Model/ActivityPub/ACore.php @@ -30,9 +30,9 @@ declare(strict_types=1); namespace OCA\Social\Model\ActivityPub; -use daita\MySmallPhpTools\Traits\TArrayTools; -use daita\MySmallPhpTools\Traits\TPathTools; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TPathTools; +use OCA\Social\Tools\Traits\TStringTools; use JsonSerializable; use OCA\Social\Exceptions\ActivityCantBeVerifiedException; use OCA\Social\Exceptions\InvalidOriginException; diff --git a/lib/Model/ActivityPub/Actor/Person.php b/lib/Model/ActivityPub/Actor/Person.php index 608be84e..a5db5244 100644 --- a/lib/Model/ActivityPub/Actor/Person.php +++ b/lib/Model/ActivityPub/Actor/Person.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Model\ActivityPub\Actor; -use daita\MySmallPhpTools\IQueryRow; +use OCA\Social\Tools\IQueryRow; use DateTime; use Exception; use JsonSerializable; diff --git a/lib/Model/ActivityPub/Item.php b/lib/Model/ActivityPub/Item.php index f9a2baf8..38483c5e 100644 --- a/lib/Model/ActivityPub/Item.php +++ b/lib/Model/ActivityPub/Item.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Model\ActivityPub; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\InstancePath; diff --git a/lib/Model/ActivityPub/Object/Follow.php b/lib/Model/ActivityPub/Object/Follow.php index e390aed4..3b779311 100644 --- a/lib/Model/ActivityPub/Object/Follow.php +++ b/lib/Model/ActivityPub/Object/Follow.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Model\ActivityPub\Object; -use daita\MySmallPhpTools\IQueryRow; +use OCA\Social\Tools\IQueryRow; use JsonSerializable; use OCA\Social\Model\ActivityPub\ACore; diff --git a/lib/Model/ActivityPub/Stream.php b/lib/Model/ActivityPub/Stream.php index ac25c53b..61ca03b3 100644 --- a/lib/Model/ActivityPub/Stream.php +++ b/lib/Model/ActivityPub/Stream.php @@ -30,9 +30,9 @@ declare(strict_types=1); namespace OCA\Social\Model\ActivityPub; -use daita\MySmallPhpTools\IQueryRow; -use daita\MySmallPhpTools\Model\Cache; -use daita\MySmallPhpTools\Model\CacheItem; +use OCA\Social\Tools\IQueryRow; +use OCA\Social\Tools\Model\Cache; +use OCA\Social\Tools\Model\CacheItem; use DateTime; use Exception; use JsonSerializable; diff --git a/lib/Model/Client/Options/TimelineOptions.php b/lib/Model/Client/Options/TimelineOptions.php index 36709b2f..5bef3831 100644 --- a/lib/Model/Client/Options/TimelineOptions.php +++ b/lib/Model/Client/Options/TimelineOptions.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Model\Client\Options; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use JsonSerializable; use OCP\IRequest; diff --git a/lib/Model/Client/SocialClient.php b/lib/Model/Client/SocialClient.php index 0d51237e..3a4d733b 100644 --- a/lib/Model/Client/SocialClient.php +++ b/lib/Model/Client/SocialClient.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Model\Client; -use daita\MySmallPhpTools\IQueryRow; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\IQueryRow; +use OCA\Social\Tools\Traits\TArrayTools; use DateTime; use Exception; use JsonSerializable; diff --git a/lib/Model/Instance.php b/lib/Model/Instance.php index ec1a4c26..19603060 100644 --- a/lib/Model/Instance.php +++ b/lib/Model/Instance.php @@ -30,8 +30,8 @@ declare(strict_types=1); namespace OCA\Social\Model; -use daita\MySmallPhpTools\IQueryRow; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\IQueryRow; +use OCA\Social\Tools\Traits\TArrayTools; use JsonSerializable; use OCA\Social\Model\ActivityPub\Actor\Person; diff --git a/lib/Model/InstancePath.php b/lib/Model/InstancePath.php index d9347514..37c393be 100644 --- a/lib/Model/InstancePath.php +++ b/lib/Model/InstancePath.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Model; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use JsonSerializable; /** diff --git a/lib/Model/LinkedDataSignature.php b/lib/Model/LinkedDataSignature.php index b4022ed2..d0dc7c48 100644 --- a/lib/Model/LinkedDataSignature.php +++ b/lib/Model/LinkedDataSignature.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Model; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use JsonSerializable; use OCA\Social\Exceptions\LinkedDataSignatureMissingException; use OCA\Social\Service\SignatureService; diff --git a/lib/Model/Post.php b/lib/Model/Post.php index b45dfba7..3152995a 100644 --- a/lib/Model/Post.php +++ b/lib/Model/Post.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Model; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use JsonSerializable; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Object\Document; diff --git a/lib/Model/RequestQueue.php b/lib/Model/RequestQueue.php index c2cd5ca8..be650004 100644 --- a/lib/Model/RequestQueue.php +++ b/lib/Model/RequestQueue.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Model; -use daita\MySmallPhpTools\Traits\TArrayTools; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TStringTools; use DateTime; use Exception; use JsonSerializable; diff --git a/lib/Model/StreamAction.php b/lib/Model/StreamAction.php index 455511c2..1b276dba 100644 --- a/lib/Model/StreamAction.php +++ b/lib/Model/StreamAction.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Model; -use daita\MySmallPhpTools\Traits\TArrayTools; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TStringTools; use JsonSerializable; /** diff --git a/lib/Model/StreamDetails.php b/lib/Model/StreamDetails.php index 79adf472..78777b20 100644 --- a/lib/Model/StreamDetails.php +++ b/lib/Model/StreamDetails.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Model; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use JsonSerializable; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Stream; diff --git a/lib/Model/StreamQueue.php b/lib/Model/StreamQueue.php index 7077dc2b..91072ea1 100644 --- a/lib/Model/StreamQueue.php +++ b/lib/Model/StreamQueue.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Model; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use DateTime; use Exception; use JsonSerializable; diff --git a/lib/Model/Test.php b/lib/Model/Test.php index 468e1cbb..1f331a02 100644 --- a/lib/Model/Test.php +++ b/lib/Model/Test.php @@ -30,8 +30,8 @@ declare(strict_types=1); namespace OCA\Social\Model; -use daita\MySmallPhpTools\Model\SimpleDataStore; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Model\SimpleDataStore; +use OCA\Social\Tools\Traits\TArrayTools; use JsonSerializable; /** diff --git a/lib/Search/UnifiedSearchProvider.php b/lib/Search/UnifiedSearchProvider.php index 9a1259fb..37824c59 100644 --- a/lib/Search/UnifiedSearchProvider.php +++ b/lib/Search/UnifiedSearchProvider.php @@ -30,8 +30,8 @@ declare(strict_types=1); namespace OCA\Social\Search; -use daita\MySmallPhpTools\Traits\Nextcloud\nc20\TNC20Logger; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TNCLogger; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCA\Social\Exceptions\AccountDoesNotExistException; use OCA\Social\Model\ActivityPub\Actor\Person; @@ -60,7 +60,7 @@ class UnifiedSearchProvider implements IProvider { use TArrayTools; - use TNC20Logger; + use TNCLogger; private IL10N $l10n; diff --git a/lib/Service/AccountService.php b/lib/Service/AccountService.php index 30fc6e7c..97c77032 100644 --- a/lib/Service/AccountService.php +++ b/lib/Service/AccountService.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OC\User\NoUserException; use OCA\Social\Db\ActorsRequest; diff --git a/lib/Service/ActivityService.php b/lib/Service/ActivityService.php index f1c67501..dd514a9d 100644 --- a/lib/Service/ActivityService.php +++ b/lib/Service/ActivityService.php @@ -30,14 +30,14 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; -use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; -use daita\MySmallPhpTools\Model\Request; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; +use OCA\Social\Tools\Model\NCRequest; +use OCA\Social\Tools\Model\Request; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCA\Social\AP; use OCA\Social\Db\FollowsRequest; @@ -349,13 +349,7 @@ class ActivityService { return $instancePaths; } - - /** - * @param RequestQueue $queue - * - * @return NC20Request - */ - private function generateRequestFromQueue(RequestQueue $queue): NC20Request { + private function generateRequestFromQueue(RequestQueue $queue): NCRequest { $path = $queue->getInstance(); $requestType = Request::TYPE_GET; @@ -365,7 +359,7 @@ class ActivityService { $requestType = Request::TYPE_POST; } - $request = new NC20Request($path->getPath(), $requestType); + $request = new NCRequest($path->getPath(), $requestType); $request->setTimeout($queue->getTimeout()); $request->setDataJson($queue->getActivity()); $request->setHost($path->getAddress()); diff --git a/lib/Service/ActorService.php b/lib/Service/ActorService.php index 7b77b031..4123eeaf 100644 --- a/lib/Service/ActorService.php +++ b/lib/Service/ActorService.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\AP; use OCA\Social\Db\CacheActorsRequest; use OCA\Social\Db\CacheDocumentsRequest; diff --git a/lib/Service/BoostService.php b/lib/Service/BoostService.php index b96cd611..224c4b7a 100644 --- a/lib/Service/BoostService.php +++ b/lib/Service/BoostService.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TStringTools; use Exception; use OCA\Social\AP; use OCA\Social\Db\StreamRequest; diff --git a/lib/Service/CacheActorService.php b/lib/Service/CacheActorService.php index 3a81fbdf..f68d0d59 100644 --- a/lib/Service/CacheActorService.php +++ b/lib/Service/CacheActorService.php @@ -30,14 +30,14 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; -use daita\MySmallPhpTools\Traits\Nextcloud\nc20\TNC20Logger; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; +use OCA\Social\Tools\Traits\TNCLogger; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCA\Social\AP; use OCA\Social\Db\CacheActorsRequest; @@ -60,31 +60,17 @@ use OCP\IURLGenerator; */ class CacheActorService { use TArrayTools; - use TNC20Logger; - + use TNCLogger; private \OCP\IURLGenerator $urlGenerator; - private CacheActorsRequest $cacheActorsRequest; - private CurlService $curlService; - private FediverseService $fediverseService; - private ConfigService $configService; - private MiscService $miscService; - /** * CacheService constructor. - * - * @param IUrlGenerator $urlGenerator - * @param CacheActorsRequest $cacheActorsRequest - * @param CurlService $curlService - * @param FediverseService $fediverseService - * @param ConfigService $configService - * @param MiscService $miscService */ public function __construct( IUrlGenerator $urlGenerator, CacheActorsRequest $cacheActorsRequest, CurlService $curlService, diff --git a/lib/Service/CacheDocumentService.php b/lib/Service/CacheDocumentService.php index e77b4240..c3b5e130 100644 --- a/lib/Service/CacheDocumentService.php +++ b/lib/Service/CacheDocumentService.php @@ -2,7 +2,6 @@ declare(strict_types=1); - /** * Nextcloud - Social Support * @@ -30,15 +29,15 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; -use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; -use daita\MySmallPhpTools\Model\Request; -use daita\MySmallPhpTools\Traits\TArrayTools; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; +use OCA\Social\Tools\Model\NCRequest; +use OCA\Social\Tools\Model\Request; +use OCA\Social\Tools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TStringTools; use Exception; use Gumlet\ImageResize; use Gumlet\ImageResizeException; @@ -57,27 +56,14 @@ class CacheDocumentService { use TArrayTools; use TStringTools; - public const RESIZED_WIDTH = 280; public const RESIZED_HEIGHT = 180; private IAppData $appData; - private CurlService $curlService; - private ConfigService $configService; - private MiscService $miscService; - - /** - * CacheService constructor. - * - * @param IAppData $appData - * @param CurlService $curlService - * @param ConfigService $configService - * @param MiscService $miscService - */ public function __construct( IAppData $appData, CurlService $curlService, ConfigService $configService, MiscService $miscService @@ -90,9 +76,7 @@ class CacheDocumentService { /** - * @param Document $document - * @param string $uploaded - * @param string $mime + * @brief Save the local upload to the cache * * @throws CacheContentMimeTypeException * @throws NotFoundException @@ -265,7 +249,7 @@ class CacheDocumentService { public function retrieveContent(string $url): string { $url = parse_url($url); $this->mustContains(['path', 'host', 'scheme'], $url); - $request = new NC20Request($url['path'], Request::TYPE_GET, true); + $request = new NCRequest($url['path'], Request::TYPE_GET, true); $request->setHost($url['host']); $request->setProtocol($url['scheme']); diff --git a/lib/Service/CheckService.php b/lib/Service/CheckService.php index 08b6420a..763d4eb5 100644 --- a/lib/Service/CheckService.php +++ b/lib/Service/CheckService.php @@ -23,8 +23,8 @@ namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\TArrayTools; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TStringTools; use Exception; use GuzzleHttp\Exception\ClientException; use OC\User\NoUserException; diff --git a/lib/Service/ClientService.php b/lib/Service/ClientService.php index dad26271..e181cb41 100644 --- a/lib/Service/ClientService.php +++ b/lib/Service/ClientService.php @@ -30,7 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TStringTools; use Exception; use OCA\Social\Db\ClientRequest; use OCA\Social\Exceptions\ClientException; diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index 2d36345f..1c2a852f 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -31,10 +31,10 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; -use daita\MySmallPhpTools\Model\Request; -use daita\MySmallPhpTools\Traits\TArrayTools; -use daita\MySmallPhpTools\Traits\TPathTools; +use OCA\Social\Tools\Model\NCRequest; +use OCA\Social\Tools\Model\Request; +use OCA\Social\Tools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TPathTools; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\SocialAppConfigException; use OCP\IConfig; @@ -412,10 +412,7 @@ class ConfigService { } - /** - * @param NC20Request $request - */ - public function configureRequest(NC20Request $request) { + public function configureRequest(NCRequest $request): void { $request->setVerifyPeer($this->getAppValue(ConfigService::SOCIAL_SELF_SIGNED) !== '1'); if ($request->getType() === Request::TYPE_GET) { diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php index c9005149..cddb6994 100644 --- a/lib/Service/CurlService.php +++ b/lib/Service/CurlService.php @@ -30,18 +30,20 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\ArrayNotFoundException; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; -use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; -use daita\MySmallPhpTools\Model\Request; -use daita\MySmallPhpTools\Traits\Nextcloud\nc20\TNC20Request; -use daita\MySmallPhpTools\Traits\TArrayTools; -use daita\MySmallPhpTools\Traits\TPathTools; +use OCA\Social\Tools\Exceptions\ArrayNotFoundException; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; +use OCA\Social\Tools\Model\NCRequest; +use OCA\Social\Tools\Model\Request; +use OCA\Social\Tools\Traits\TNCRequest; +use OCA\Social\Tools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TPathTools; +use OCA\Social\Tools\Traits\TNCSetup; +use OCA\Social\Tools\Traits\TNCLogger; use Exception; use OCA\Social\AP; use OCA\Social\Exceptions\HostMetaException; @@ -56,8 +58,10 @@ use OCA\Social\Model\ActivityPub\Actor\Person; class CurlService { use TArrayTools; + use TNCSetup; + use TNCLogger; use TPathTools; - use TNC20Request { + use TNCRequest { retrieveJson as retrieveJsonOrig; doRequest as doRequestOrig; } @@ -126,7 +130,7 @@ class CurlService { $path = '/.well-known/webfinger'; } - $request = new NC20Request($path); + $request = new NCRequest($path); $request->addParam('resource', 'acct:' . $account); $request->setHost($host); $request->setProtocols($protocols); @@ -152,7 +156,7 @@ class CurlService { * @throws HostMetaException */ public function hostMeta(string &$host, array &$protocols): string { - $request = new NC20Request('/.well-known/host-meta'); + $request = new NCRequest('/.well-known/host-meta'); $request->setHost($host); $request->setProtocols($protocols); @@ -243,7 +247,7 @@ class CurlService { $this->debug('retrieveObject', ['id' => $id]); $url = parse_url($id); $this->mustContains(['path', 'host', 'scheme'], $url); - $request = new NC20Request($url['path'], Request::TYPE_GET); + $request = new NCRequest($url['path'], Request::TYPE_GET); $request->setHost($url['host']); $request->setProtocol($url['scheme']); @@ -261,13 +265,13 @@ class CurlService { /** - * @param NC20Request $request + * @param NCRequest $request * * @return array * @throws RequestContentException * @throws RequestNetworkException */ - public function retrieveJson(NC20Request $request): array { + public function retrieveJson(NCRequest $request): array { try { return $this->retrieveJsonOrig($request); } catch (RequestNetworkException | RequestContentException $e) { @@ -278,9 +282,8 @@ class CurlService { /** - * @param NC20Request $request + * @param NCRequest $request * - * @return mixed * @throws SocialAppConfigException * @throws UnauthorizedFediverseException * @throws RequestContentException @@ -289,7 +292,7 @@ class CurlService { * @throws RequestServerException */ // migration ? - public function doRequest(NC20Request $request) { + public function doRequest(NCRequest $request): string { $this->fediverseService->authorized($request->getAddress()); $this->configService->configureRequest($request); $this->assignUserAgent($request); @@ -299,9 +302,9 @@ class CurlService { /** - * @param NC20Request $request + * @param NCRequest $request */ - public function assignUserAgent(NC20Request $request) { + public function assignUserAgent(NCRequest $request): void { $request->setUserAgent( self::USER_AGENT . ' ' . $this->configService->getAppValue('installed_version') ); @@ -320,7 +323,7 @@ class CurlService { $path .= $this->withoutBeginSlash(self::ASYNC_REQUEST_TOKEN); $path = str_replace('{token}', $token, $path); - $request = new NC20Request($path, Request::TYPE_POST); + $request = new NCRequest($path, Request::TYPE_POST); $request->setHost($this->configService->getCloudHost()); $request->setProtocol(parse_url($address, PHP_URL_SCHEME)); diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index 013f1c99..cee4bc7a 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -31,12 +31,12 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; use Exception; use OCA\Social\AP; use OCA\Social\Db\ActorsRequest; diff --git a/lib/Service/FollowService.php b/lib/Service/FollowService.php index df62a295..47e1ce59 100644 --- a/lib/Service/FollowService.php +++ b/lib/Service/FollowService.php @@ -30,8 +30,8 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\AP; use OCA\Social\Db\FollowsRequest; use OCA\Social\Exceptions\CacheActorDoesNotExistException; @@ -41,11 +41,11 @@ use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; use OCA\Social\Exceptions\RetrieveAccountFormatException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; diff --git a/lib/Service/HashtagService.php b/lib/Service/HashtagService.php index 3988eaad..3b8ae8ab 100644 --- a/lib/Service/HashtagService.php +++ b/lib/Service/HashtagService.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\DateTimeException; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\DateTimeException; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Db\HashtagsRequest; use OCA\Social\Db\StreamRequest; use OCA\Social\Exceptions\HashtagDoesNotExistException; diff --git a/lib/Service/ImportService.php b/lib/Service/ImportService.php index 00bcd41c..ada48b27 100644 --- a/lib/Service/ImportService.php +++ b/lib/Service/ImportService.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\TArrayTools; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TStringTools; use Exception; use OCA\Social\AP; use OCA\Social\Exceptions\ActivityPubFormatException; diff --git a/lib/Service/InstanceService.php b/lib/Service/InstanceService.php index a7c7db66..55305934 100644 --- a/lib/Service/InstanceService.php +++ b/lib/Service/InstanceService.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\AppInfo\Application; use OCA\Social\Db\InstancesRequest; use OCA\Social\Exceptions\InstanceDoesNotExistException; diff --git a/lib/Service/LikeService.php b/lib/Service/LikeService.php index a7d4c48e..b70e4bde 100644 --- a/lib/Service/LikeService.php +++ b/lib/Service/LikeService.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\TStringTools; +use OCA\Social\Tools\Traits\TStringTools; use Exception; use OCA\Social\AP; use OCA\Social\Db\StreamRequest; diff --git a/lib/Service/PostService.php b/lib/Service/PostService.php index 41db71e8..c10145d6 100644 --- a/lib/Service/PostService.php +++ b/lib/Service/PostService.php @@ -30,12 +30,12 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; use Exception; use OCA\Social\AP; use OCA\Social\Exceptions\CacheContentMimeTypeException; diff --git a/lib/Service/PushService.php b/lib/Service/PushService.php index 59f703b9..78274d5a 100644 --- a/lib/Service/PushService.php +++ b/lib/Service/PushService.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\TAsync; +use OCA\Social\Tools\Traits\TAsync; use OC; //use OC\Push\Model\Helper\PushCallback; use OCA\Social\Exceptions\SocialAppConfigException; diff --git a/lib/Service/RequestQueueService.php b/lib/Service/RequestQueueService.php index 6b074ccc..c4d32003 100644 --- a/lib/Service/RequestQueueService.php +++ b/lib/Service/RequestQueueService.php @@ -31,7 +31,7 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TArrayTools; use OCA\Social\Db\RequestQueueRequest; use OCA\Social\Exceptions\EmptyQueueException; use OCA\Social\Exceptions\NoHighPriorityRequestException; diff --git a/lib/Service/SearchService.php b/lib/Service/SearchService.php index 2e8b9ab2..91867895 100644 --- a/lib/Service/SearchService.php +++ b/lib/Service/SearchService.php @@ -31,8 +31,8 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Traits\Nextcloud\nc20\TNC20Logger; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Traits\TNCLogger; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCA\Social\Model\ActivityPub\Actor\Person; @@ -43,7 +43,7 @@ use OCA\Social\Model\ActivityPub\Actor\Person; */ class SearchService { use TArrayTools; - use TNC20Logger; + use TNCLogger; public const SEARCH_ACCOUNTS = 1; diff --git a/lib/Service/SignatureService.php b/lib/Service/SignatureService.php index eaddfa3f..021f4e06 100644 --- a/lib/Service/SignatureService.php +++ b/lib/Service/SignatureService.php @@ -30,15 +30,15 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\DateTimeException; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; -use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\DateTimeException; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; +use OCA\Social\Tools\Model\NCRequest; +use OCA\Social\Tools\Traits\TArrayTools; use DateTime; use Exception; use JsonLdException; @@ -119,13 +119,13 @@ class SignatureService { /** - * @param NC20Request $request + * @param NCRequest $request * @param RequestQueue $queue * * @throws ActorDoesNotExistException - * @throws SocialAppConfigException // TODO: implement in TNC20Request ? + * @throws SocialAppConfigException // TODO: implement in TNCRequest ? */ - public function signRequest(NC20Request $request, RequestQueue $queue) { + public function signRequest(NCRequest $request, RequestQueue $queue): void { $date = gmdate(self::DATE_HEADER); $path = $queue->getInstance(); @@ -153,11 +153,11 @@ class SignatureService { /** * @param array $elements * @param array $data - * @param NC20Request $request + * @param NCRequest $request * * @return string */ - private function generateHeaders(array $elements, array $data, NC20Request $request): string { + private function generateHeaders(array $elements, array $data, NCRequest $request): string { $signingElements = []; foreach ($elements as $element) { $signingElements[] = $element . ': ' . $data[$element]; diff --git a/lib/Service/StreamQueueService.php b/lib/Service/StreamQueueService.php index 168b6000..5fb17eeb 100644 --- a/lib/Service/StreamQueueService.php +++ b/lib/Service/StreamQueueService.php @@ -30,9 +30,9 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Model\Cache; -use daita\MySmallPhpTools\Model\CacheItem; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Model\Cache; +use OCA\Social\Tools\Model\CacheItem; use OCA\Social\AP; use OCA\Social\Db\StreamRequest; use OCA\Social\Db\StreamQueueRequest; @@ -42,11 +42,11 @@ use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Exceptions\QueueStatusException; use OCA\Social\Exceptions\RedundancyLimitException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; use OCA\Social\Model\ActivityPub\Object\Note; diff --git a/lib/Service/StreamService.php b/lib/Service/StreamService.php index 49c4576b..c2604db5 100644 --- a/lib/Service/StreamService.php +++ b/lib/Service/StreamService.php @@ -30,13 +30,13 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\DateTimeException; -use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; +use OCA\Social\Tools\Exceptions\DateTimeException; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; use Exception; use OCA\Social\Db\StreamRequest; use OCA\Social\Exceptions\InvalidOriginException; diff --git a/lib/Service/TestService.php b/lib/Service/TestService.php index 4f096665..c099f76a 100644 --- a/lib/Service/TestService.php +++ b/lib/Service/TestService.php @@ -30,10 +30,10 @@ declare(strict_types=1); namespace OCA\Social\Service; -use daita\MySmallPhpTools\Exceptions\ArrayNotFoundException; -use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; -use daita\MySmallPhpTools\Model\SimpleDataStore; -use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Tools\Exceptions\ArrayNotFoundException; +use OCA\Social\Tools\Model\NCRequest; +use OCA\Social\Tools\Model\SimpleDataStore; +use OCA\Social\Tools\Traits\TArrayTools; use Exception; use OCA\Social\AP; use OCA\Social\Exceptions\HostMetaException; @@ -102,7 +102,7 @@ class TestService { $tests->aObj('tests', $testHostMeta); - $request = new NC20Request($path); + $request = new NCRequest($path); $request->addParam('resource', 'acct:' . $account); $request->setHost($host); $request->setProtocols($protocols); diff --git a/lib/Tools/Db/ExtendedQueryBuilder.php b/lib/Tools/Db/ExtendedQueryBuilder.php new file mode 100644 index 00000000..83b88a92 --- /dev/null +++ b/lib/Tools/Db/ExtendedQueryBuilder.php @@ -0,0 +1,527 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Db; + +use OCA\Social\Tools\Exceptions\DateTimeException; +use OCA\Social\Tools\Exceptions\RowNotFoundException; +use OCA\Social\Tools\IExtendedQueryBuilder; +use OCA\Social\Tools\IQueryRow; +use DateInterval; +use DateTime; +use Doctrine\DBAL\Query\QueryBuilder as DBALQueryBuilder; +use Exception; +use OC\DB\QueryBuilder\QueryBuilder; +use OCP\DB\QueryBuilder\ICompositeExpression; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\DB\QueryBuilder\IQueryFunction; + +/** + * Class ExtendedQueryBuilder + * + * @package OCA\Social\Tools\Db + */ +class ExtendedQueryBuilder extends QueryBuilder implements IExtendedQueryBuilder { + + + /** @var string */ + private $defaultSelectAlias = ''; + + + /** + * @param string $alias + * + * @return IExtendedQueryBuilder + */ + public function setDefaultSelectAlias(string $alias): IExtendedQueryBuilder { + $this->defaultSelectAlias = $alias; + + return $this; + } + + /** + * @return string + */ + public function getDefaultSelectAlias(): string { + return $this->defaultSelectAlias; + } + + + /** + * Limit the request to the Id + * + * @param int $id + * + * @return ExtendedQueryBuilder + */ + public function limitToId(int $id): IExtendedQueryBuilder { + $this->limitToDBFieldInt('id', $id); + + return $this; + } + + + /** + * @param array $ids + * + * @return IExtendedQueryBuilder + */ + public function limitToIds(array $ids): IExtendedQueryBuilder { + $this->limitToDBFieldArray('id', $ids); + + return $this; + } + + + /** + * Limit the request to the Id (string) + * + * @param string $id + * + * @return ExtendedQueryBuilder + */ + public function limitToIdString(string $id): IExtendedQueryBuilder { + $this->limitToDBField('id', $id, false); + + return $this; + } + + + /** + * Limit the request to the UserId + * + * @param string $userId + * + * @return ExtendedQueryBuilder + */ + public function limitToUserId(string $userId): IExtendedQueryBuilder { + $this->limitToDBField('user_id', $userId, false); + + return $this; + } + + + /** + * Limit the request to the creation + * + * @param int $delay + * + * @return ExtendedQueryBuilder + * @throws Exception + */ + public function limitToCreation(int $delay = 0): IExtendedQueryBuilder { + $date = new DateTime('now'); + $date->sub(new DateInterval('PT' . $delay . 'M')); + + $this->limitToDBFieldDateTime('creation', $date, true); + + return $this; + } + + + /** + * @param string $field + * @param string $value + * @param bool $cs - case sensitive + * @param string $alias + */ + public function limitToDBField(string $field, string $value, bool $cs = true, string $alias = '') { + $expr = $this->exprLimitToDBField($field, $value, true, $cs, $alias); + + $this->andWhere($expr); + } + + + /** + * @param string $field + * @param string $value + * @param bool $cs - case sensitive + * @param string $alias + */ + public function filterDBField(string $field, string $value, bool $cs = true, string $alias = '' + ) { + $expr = $this->exprLimitToDBField($field, $value, false, $cs, $alias); + $this->andWhere($expr); + } + + public function exprLimitToDBField( + string $field, string $value, bool $eq = true, bool $cs = true, string $alias = '' + ): IQueryFunction { + $expr = $this->expr(); + + $pf = ''; + if ($this->getType() === DBALQueryBuilder::SELECT) { + $pf = (($alias === '') ? $this->getDefaultSelectAlias() : $alias) . '.'; + } + $field = $pf . $field; + + $comp = 'eq'; + if ($eq === false) { + $comp = 'neq'; + } + + if ($cs) { + return $expr->$comp($field, $this->createNamedParameter($value)); + } else { + $func = $this->func(); + + return $expr->$comp( + $func->lower($field), $func->lower($this->createNamedParameter($value)) + ); + } + } + + + /** + * @param string $field + * @param array $values + * @param bool $cs - case sensitive + * @param string $alias + */ + public function limitToDBFieldArray( + string $field, array $values, bool $cs = true, string $alias = '' + ) { + $expr = $this->exprLimitToDBFieldArray($field, $values, true, $cs, $alias); + $this->andWhere($expr); + } + + + /** + * @param string $field + * @param string $value + * @param bool $cs - case sensitive + * @param string $alias + */ + public function filterDBFieldArray( + string $field, string $value, bool $cs = true, string $alias = '' + ) { + $expr = $this->exprLimitToDBField($field, $value, false, $cs, $alias); + $this->andWhere($expr); + } + + + /** + * @param string $field + * @param array $values + * @param bool $eq + * @param bool $cs + * @param string $alias + * + * @return ICompositeExpression + */ + public function exprLimitToDBFieldArray( + string $field, array $values, bool $eq = true, bool $cs = true, string $alias = '' + ): ICompositeExpression { + $expr = $this->expr(); + + $pf = ''; + if ($this->getType() === DBALQueryBuilder::SELECT) { + $pf = (($alias === '') ? $this->getDefaultSelectAlias() : $alias) . '.'; + } + $field = $pf . $field; + + $func = $this->func(); + if ($eq === false) { + $comp = 'neq'; + $junc = $expr->andX(); + } else { + $comp = 'eq'; + $junc = $expr->orX(); + } + + foreach ($values as $value) { + if ($cs) { + $junc->add($expr->$comp($field, $this->createNamedParameter($value))); + } else { + $junc->add( + $expr->$comp( + $func->lower($field), $func->lower($this->createNamedParameter($value)) + ) + ); + } + } + + return $junc; + } + + + /** + * @param string $field + * @param int $value + * @param string $alias + */ + public function limitToDBFieldInt(string $field, int $value, string $alias = '') { + $expr = $this->exprLimitToDBFieldInt($field, $value, $alias, true); + $this->andWhere($expr); + } + + + /** + * @param string $field + * @param int $value + * @param string $alias + */ + public function filterDBFieldInt(string $field, int $value, string $alias = '') { + $expr = $this->exprLimitToDBFieldInt($field, $value, $alias, false); + $this->andWhere($expr); + } + + public function exprLimitToDBFieldInt(string $field, int $value, string $alias = '', bool $eq = true + ): IQueryFunction { + $expr = $this->expr(); + + $pf = ''; + if ($this->getType() === DBALQueryBuilder::SELECT) { + $pf = (($alias === '') ? $this->getDefaultSelectAlias() : $alias) . '.'; + } + $field = $pf . $field; + + $comp = 'eq'; + if ($eq === false) { + $comp = 'neq'; + } + + return $expr->$comp($field, $this->createNamedParameter($value, IQueryBuilder::PARAM_INT)); + } + + + /** + * @param string $field + */ + public function limitToDBFieldEmpty(string $field) { + $expr = $this->expr(); + $pf = + ($this->getType() === DBALQueryBuilder::SELECT) ? $this->getDefaultSelectAlias() + . '.' : ''; + $field = $pf . $field; + + $this->andWhere($expr->eq($field, $this->createNamedParameter(''))); + } + + + /** + * @param string $field + */ + public function filterDBFieldEmpty(string $field) { + $expr = $this->expr(); + $pf = + ($this->getType() === DBALQueryBuilder::SELECT) ? $this->getDefaultSelectAlias() + . '.' : ''; + $field = $pf . $field; + + $this->andWhere($expr->neq($field, $this->createNamedParameter(''))); + } + + + /** + * @param string $field + * @param DateTime $date + * @param bool $orNull + */ + public function limitToDBFieldDateTime(string $field, DateTime $date, bool $orNull = false) { + $expr = $this->expr(); + $pf = + ($this->getType() === DBALQueryBuilder::SELECT) ? $this->getDefaultSelectAlias() + . '.' : ''; + $field = $pf . $field; + + $orX = $expr->orX(); + $orX->add( + $expr->lte($field, $this->createNamedParameter($date, IQueryBuilder::PARAM_DATE)) + ); + + if ($orNull === true) { + $orX->add($expr->isNull($field)); + } + + $this->andWhere($orX); + } + + + /** + * @param int $timestamp + * @param string $field + * + * @throws DateTimeException + */ + public function limitToSince(int $timestamp, string $field) { + try { + $dTime = new DateTime(); + $dTime->setTimestamp($timestamp); + } catch (Exception $e) { + throw new DateTimeException($e->getMessage()); + } + + $expr = $this->expr(); + $pf = ($this->getType() === DBALQueryBuilder::SELECT) ? $this->getDefaultSelectAlias() . '.' : ''; + $field = $pf . $field; + + $orX = $expr->orX(); + $orX->add( + $expr->gte($field, $this->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE)) + ); + + $this->andWhere($orX); + } + + + /** + * @param string $field + * @param string $value + */ + public function searchInDBField(string $field, string $value) { + $expr = $this->expr(); + + $pf = ($this->getType() === DBALQueryBuilder::SELECT) ? $this->getDefaultSelectAlias() + . '.' : ''; + $field = $pf . $field; + + $this->andWhere($expr->iLike($field, $this->createNamedParameter($value))); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param string $fieldRight + * @param string $alias + * + * @return string + */ + public function exprFieldWithinJsonFormat( + IQueryBuilder $qb, string $field, string $fieldRight, string $alias = '' + ) { + $func = $qb->func(); + $expr = $qb->expr(); + + if ($alias === '') { + $alias = $this->defaultSelectAlias; + } + + $concat = $func->concat( + $qb->createNamedParameter('%"'), + $func->concat($fieldRight, $qb->createNamedParameter('"%')) + ); + + return $expr->iLike($alias . '.' . $field, $concat); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param string $value + * @param bool $eq (eq, not eq) + * @param bool $cs (case sensitive, or not) + * + * @return string + */ + public function exprValueWithinJsonFormat( + IQueryBuilder $qb, string $field, string $value, bool $eq = true, bool $cs = true + ): string { + $dbConn = $this->getConnection(); + $expr = $qb->expr(); + $func = $qb->func(); + + $value = $dbConn->escapeLikeParameter($value); + if ($cs) { + $field = $func->lower($field); + $value = $func->lower($value); + } + + $comp = 'iLike'; + if ($eq) { + $comp = 'notLike'; + } + + return $expr->$comp($field, $qb->createNamedParameter('%"' . $value . '"%')); + } + +// +// /** +// * @param IQueryBuilder $qb +// * @param string $field +// * @param string $value +// * +// * @return string +// */ +// public function exprValueNotWithinJsonFormat(IQueryBuilder $qb, string $field, string $value): string { +// $dbConn = $this->getConnection(); +// $expr = $qb->expr(); +// $func = $qb->func(); +// +// +// return $expr->notLike( +// $func->lower($field), +// $qb->createNamedParameter( +// '%"' . $func->lower($dbConn->escapeLikeParameter($value)) . '"%' +// ) +// ); +// } + + + /** + * @param callable $method + * + * @return IQueryRow + * @throws RowNotFoundException + */ + public function getRow(callable $method): IQueryRow { + $cursor = $this->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + + if ($data === false) { + throw new RowNotFoundException(); + } + + return $method($data, $this); + } + + /** + * @param callable $method + * + * @return IQueryRow[] + */ + public function getRows(callable $method): array { + $rows = []; + $cursor = $this->execute(); + while ($data = $cursor->fetch()) { + try { + $rows[] = $method($data, $this); + } catch (Exception $e) { + } + } + $cursor->closeCursor(); + + return $rows; + } +} diff --git a/lib/Tools/Db/RequestBuilder.php b/lib/Tools/Db/RequestBuilder.php new file mode 100644 index 00000000..dc2b4757 --- /dev/null +++ b/lib/Tools/Db/RequestBuilder.php @@ -0,0 +1,331 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Db; + +use DateInterval; +use DateTime; +use Doctrine\DBAL\Query\QueryBuilder; +use Exception; +use OCP\DB\QueryBuilder\IQueryBuilder; + +/** + * Class RequestBuilder + * @deprecated - 19 + * @package OCA\Social\Tools\Db + */ +class RequestBuilder { + + + /** @var string */ + protected $defaultSelectAlias; + + + /** + * Limit the request to the Id + * + * @param IQueryBuilder $qb + * @param int $id + */ + protected function limitToId(IQueryBuilder $qb, int $id) { + $this->limitToDBFieldInt($qb, 'id', $id); + } + + + /** + * Limit the request to the Id (string) + * + * @param IQueryBuilder $qb + * @param string $id + */ + protected function limitToIdString(IQueryBuilder $qb, string $id) { + $this->limitToDBField($qb, 'id', $id, false); + } + + + /** + * Limit the request to the UserId + * + * @param IQueryBuilder $qb + * @param string $userId + */ + protected function limitToUserId(IQueryBuilder $qb, string $userId) { + $this->limitToDBField($qb, 'user_id', $userId, false); + } + + + /** + * Limit the request to the creation + * + * @param IQueryBuilder $qb + * @param int $delay + * + * @throws Exception + */ + protected function limitToCreation(IQueryBuilder $qb, int $delay = 0) { + $date = new DateTime('now'); + $date->sub(new DateInterval('PT' . $delay . 'M')); + + $this->limitToDBFieldDateTime($qb, 'creation', $date, true); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param string $value + * @param bool $cs - case sensitive + * @param string $alias + */ + protected function limitToDBField( + IQueryBuilder $qb, string $field, string $value, bool $cs = true, string $alias = '' + ) { + $expr = $this->exprLimitToDBField($qb, $field, $value, true, $cs, $alias); + $qb->andWhere($expr); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param string $value + * @param bool $cs - case sensitive + * @param string $alias + */ + protected function filterDBField( + IQueryBuilder $qb, string $field, string $value, bool $cs = true, string $alias = '' + ) { + $expr = $this->exprLimitToDBField($qb, $field, $value, false, $cs, $alias); + $qb->andWhere($expr); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param string $value + * @param bool $eq + * @param bool $cs + * @param string $alias + * + * @return string + */ + protected function exprLimitToDBField( + IQueryBuilder $qb, string $field, string $value, bool $eq = true, bool $cs = true, string $alias = '' + ): string { + $expr = $qb->expr(); + + $pf = ''; + if ($qb->getType() === QueryBuilder::SELECT) { + $pf = (($alias === '') ? $this->defaultSelectAlias : $alias) . '.'; + } + $field = $pf . $field; + + $comp = 'eq'; + if ($eq === false) { + $comp = 'neq'; + } + + if ($cs) { + return $expr->$comp($field, $qb->createNamedParameter($value)); + } else { + $func = $qb->func(); + + return $expr->$comp( + $func->lower($field), $func->lower($qb->createNamedParameter($value)) + ); + } + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param int $value + * @param string $alias + */ + protected function limitToDBFieldInt( + IQueryBuilder $qb, string $field, int $value, string $alias = '' + ) { + $expr = $this->exprLimitToDBFieldInt($qb, $field, $value, $alias, true); + $qb->andWhere($expr); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param int $value + * @param string $alias + */ + protected function filterDBFieldInt( + IQueryBuilder $qb, string $field, int $value, string $alias = '' + ) { + $expr = $this->exprLimitToDBFieldInt($qb, $field, $value, $alias, false); + $qb->andWhere($expr); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param int $value + * @param string $alias + * + * @param bool $eq + * + * @return string + */ + protected function exprLimitToDBFieldInt( + IQueryBuilder $qb, string $field, int $value, string $alias = '', bool $eq = true + ): string { + $expr = $qb->expr(); + + $pf = ''; + if ($qb->getType() === QueryBuilder::SELECT) { + $pf = (($alias === '') ? $this->defaultSelectAlias : $alias) . '.'; + } + $field = $pf . $field; + + $comp = 'eq'; + if ($eq === false) { + $comp = 'neq'; + } + + return $expr->$comp($field, $qb->createNamedParameter($value)); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + */ + protected function limitToDBFieldEmpty(IQueryBuilder $qb, string $field) { + $expr = $qb->expr(); + $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : ''; + $field = $pf . $field; + + $qb->andWhere($expr->eq($field, $qb->createNamedParameter(''))); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + */ + protected function filterDBFieldEmpty(IQueryBuilder $qb, string $field) { + $expr = $qb->expr(); + $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : ''; + $field = $pf . $field; + + $qb->andWhere($expr->neq($field, $qb->createNamedParameter(''))); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param DateTime $date + * @param bool $orNull + */ + protected function limitToDBFieldDateTime( + IQueryBuilder $qb, string $field, DateTime $date, bool $orNull = false + ) { + $expr = $qb->expr(); + $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : ''; + $field = $pf . $field; + + $orX = $expr->orX(); + $orX->add($expr->lte($field, $qb->createNamedParameter($date, IQueryBuilder::PARAM_DATE))); + + if ($orNull === true) { + $orX->add($expr->isNull($field)); + } + + $qb->andWhere($orX); + } + + + /** + * @param IQueryBuilder $qb + * @param int $timestamp + * @param string $field + * + * @throws Exception + */ + protected function limitToSince(IQueryBuilder $qb, int $timestamp, string $field) { + $dTime = new DateTime(); + $dTime->setTimestamp($timestamp); + + $expr = $qb->expr(); + $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : ''; + $field = $pf . $field; + + $orX = $expr->orX(); + $orX->add($expr->gte($field, $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE))); + + $qb->andWhere($orX); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param array $values + */ + protected function limitToDBFieldArray(IQueryBuilder $qb, string $field, array $values): void { + $expr = $qb->expr(); + $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : ''; + $field = $pf . $field; + + $orX = $expr->orX(); + foreach ($values as $value) { + $orX->add($expr->eq($field, $qb->createNamedParameter($value))); + } + + $qb->andWhere($orX); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param string $value + */ + protected function searchInDBField(IQueryBuilder $qb, string $field, string $value) { + $expr = $qb->expr(); + + $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : ''; + $field = $pf . $field; + + $qb->andWhere($expr->iLike($field, $qb->createNamedParameter($value))); + } +} diff --git a/lib/Tools/Exceptions/ArrayNotFoundException.php b/lib/Tools/Exceptions/ArrayNotFoundException.php new file mode 100644 index 00000000..f34dd4d7 --- /dev/null +++ b/lib/Tools/Exceptions/ArrayNotFoundException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ArrayNotFoundException + * + * @package OCA\Social\Tools\Exceptions + */ +class ArrayNotFoundException extends Exception { +} diff --git a/lib/Tools/Exceptions/CacheItemNotFoundException.php b/lib/Tools/Exceptions/CacheItemNotFoundException.php new file mode 100644 index 00000000..8ede4a49 --- /dev/null +++ b/lib/Tools/Exceptions/CacheItemNotFoundException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class CacheItemNotFoundException + * + * @package OCA\Social\Tools\Exceptions + */ +class CacheItemNotFoundException extends Exception { +} diff --git a/lib/Tools/Exceptions/DateTimeException.php b/lib/Tools/Exceptions/DateTimeException.php new file mode 100644 index 00000000..a150d8e6 --- /dev/null +++ b/lib/Tools/Exceptions/DateTimeException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class RequestContentException + * + * @package OCA\Social\Tools\Exceptions + */ +class DateTimeException extends Exception { +} diff --git a/lib/Tools/Exceptions/DependencyInjectionException.php b/lib/Tools/Exceptions/DependencyInjectionException.php new file mode 100644 index 00000000..a0d56a89 --- /dev/null +++ b/lib/Tools/Exceptions/DependencyInjectionException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ArrayNotFoundException + * + * @package OCA\Social\Tools\Exceptions + */ +class DependencyInjectionException extends Exception { +} diff --git a/lib/Tools/Exceptions/InvalidItemException.php b/lib/Tools/Exceptions/InvalidItemException.php new file mode 100644 index 00000000..37cffc00 --- /dev/null +++ b/lib/Tools/Exceptions/InvalidItemException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ArrayNotFoundException + * + * @package OCA\Social\Tools\Exceptions + */ +class InvalidItemException extends Exception { +} diff --git a/lib/Tools/Exceptions/InvalidOriginException.php b/lib/Tools/Exceptions/InvalidOriginException.php new file mode 100644 index 00000000..c85f18aa --- /dev/null +++ b/lib/Tools/Exceptions/InvalidOriginException.php @@ -0,0 +1,40 @@ + + * @copyright 2020, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class SignatureException + * + * @package OCA\Social\Tools\Exceptions + */ +class InvalidOriginException extends Exception { +} diff --git a/lib/Tools/Exceptions/ItemNotFoundException.php b/lib/Tools/Exceptions/ItemNotFoundException.php new file mode 100644 index 00000000..51010cb5 --- /dev/null +++ b/lib/Tools/Exceptions/ItemNotFoundException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ArrayNotFoundException + * + * @package OCA\Social\Tools\Exceptions + */ +class ItemNotFoundException extends Exception { +} diff --git a/lib/Tools/Exceptions/JsonNotRequestedException.php b/lib/Tools/Exceptions/JsonNotRequestedException.php new file mode 100644 index 00000000..69981657 --- /dev/null +++ b/lib/Tools/Exceptions/JsonNotRequestedException.php @@ -0,0 +1,41 @@ + + * @copyright 2020, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class JsonNotRequestedException + * + * + * @package OCA\Social\Tools\Exceptions + */ +class JsonNotRequestedException extends Exception { +} diff --git a/lib/Tools/Exceptions/MalformedArrayException.php b/lib/Tools/Exceptions/MalformedArrayException.php new file mode 100644 index 00000000..9fe452f0 --- /dev/null +++ b/lib/Tools/Exceptions/MalformedArrayException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ArrayNotFoundException + * + * @package OCA\Social\Tools\Exceptions + */ +class MalformedArrayException extends Exception { +} diff --git a/lib/Tools/Exceptions/RequestContentException.php b/lib/Tools/Exceptions/RequestContentException.php new file mode 100644 index 00000000..6e83e1af --- /dev/null +++ b/lib/Tools/Exceptions/RequestContentException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class RequestContentException + * + * @package OCA\Social\Tools\Exceptions + */ +class RequestContentException extends Exception { +} diff --git a/lib/Tools/Exceptions/RequestNetworkException.php b/lib/Tools/Exceptions/RequestNetworkException.php new file mode 100644 index 00000000..4aaff0cb --- /dev/null +++ b/lib/Tools/Exceptions/RequestNetworkException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class RequestNetworkException + * + * @package OCA\Social\Tools\Exceptions + */ +class RequestNetworkException extends Exception { +} diff --git a/lib/Tools/Exceptions/RequestResultNotJsonException.php b/lib/Tools/Exceptions/RequestResultNotJsonException.php new file mode 100644 index 00000000..f4e6e94d --- /dev/null +++ b/lib/Tools/Exceptions/RequestResultNotJsonException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class RequestResultNotJsonException + * + * @package OCA\Social\Tools\Exceptions + */ +class RequestResultNotJsonException extends Exception { +} diff --git a/lib/Tools/Exceptions/RequestResultSizeException.php b/lib/Tools/Exceptions/RequestResultSizeException.php new file mode 100644 index 00000000..9f862aee --- /dev/null +++ b/lib/Tools/Exceptions/RequestResultSizeException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class RequestResultSizeException + * + * @package OCA\Social\Tools\Exceptions + */ +class RequestResultSizeException extends Exception { +} diff --git a/lib/Tools/Exceptions/RequestServerException.php b/lib/Tools/Exceptions/RequestServerException.php new file mode 100644 index 00000000..23a287b6 --- /dev/null +++ b/lib/Tools/Exceptions/RequestServerException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class RequestServerException + * + * @package OCA\Social\Tools\Exceptions + */ +class RequestServerException extends Exception { +} diff --git a/lib/Tools/Exceptions/RowNotFoundException.php b/lib/Tools/Exceptions/RowNotFoundException.php new file mode 100644 index 00000000..149a2142 --- /dev/null +++ b/lib/Tools/Exceptions/RowNotFoundException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class RowNotFoundException + * + * @package OCA\Social\Tools\Exceptions + */ +class RowNotFoundException extends Exception { +} diff --git a/lib/Tools/Exceptions/ShellConfirmationException.php b/lib/Tools/Exceptions/ShellConfirmationException.php new file mode 100644 index 00000000..f39fd10d --- /dev/null +++ b/lib/Tools/Exceptions/ShellConfirmationException.php @@ -0,0 +1,42 @@ + + * @copyright 2021, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ShellConfirmationException + * + * @package OCA\Social\Tools\Exceptions + */ +class ShellConfirmationException extends Exception { +} diff --git a/lib/Tools/Exceptions/ShellMissingCommandException.php b/lib/Tools/Exceptions/ShellMissingCommandException.php new file mode 100644 index 00000000..66c051ac --- /dev/null +++ b/lib/Tools/Exceptions/ShellMissingCommandException.php @@ -0,0 +1,42 @@ + + * @copyright 2021, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ShellMissingItemException + * + * @package OCA\Social\Tools\Exceptions + */ +class ShellMissingCommandException extends Exception { +} diff --git a/lib/Tools/Exceptions/ShellMissingItemException.php b/lib/Tools/Exceptions/ShellMissingItemException.php new file mode 100644 index 00000000..96d20989 --- /dev/null +++ b/lib/Tools/Exceptions/ShellMissingItemException.php @@ -0,0 +1,42 @@ + + * @copyright 2021, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ShellMissingItemException + * + * @package OCA\Social\Tools\Exceptions + */ +class ShellMissingItemException extends Exception { +} diff --git a/lib/Tools/Exceptions/ShellUnknownCommandException.php b/lib/Tools/Exceptions/ShellUnknownCommandException.php new file mode 100644 index 00000000..1abc8d0d --- /dev/null +++ b/lib/Tools/Exceptions/ShellUnknownCommandException.php @@ -0,0 +1,42 @@ + + * @copyright 2021, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ShellUnknownCommandException + * + * @package OCA\Social\Tools\Exceptions + */ +class ShellUnknownCommandException extends Exception { +} diff --git a/lib/Tools/Exceptions/ShellUnknownItemException.php b/lib/Tools/Exceptions/ShellUnknownItemException.php new file mode 100644 index 00000000..e174fc2f --- /dev/null +++ b/lib/Tools/Exceptions/ShellUnknownItemException.php @@ -0,0 +1,42 @@ + + * @copyright 2021, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ShellUnknownItemException + * + * @package OCA\Social\Tools\Exceptions + */ +class ShellUnknownItemException extends Exception { +} diff --git a/lib/Tools/Exceptions/SignatoryException.php b/lib/Tools/Exceptions/SignatoryException.php new file mode 100644 index 00000000..00bea117 --- /dev/null +++ b/lib/Tools/Exceptions/SignatoryException.php @@ -0,0 +1,40 @@ + + * @copyright 2020, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class SignatoryException + * + * @package OCA\Social\Tools\Exceptions + */ +class SignatoryException extends Exception { +} diff --git a/lib/Tools/Exceptions/SignatureException.php b/lib/Tools/Exceptions/SignatureException.php new file mode 100644 index 00000000..c799c957 --- /dev/null +++ b/lib/Tools/Exceptions/SignatureException.php @@ -0,0 +1,40 @@ + + * @copyright 2020, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class SignatureException + * + * @package OCA\Social\Tools\Exceptions + */ +class SignatureException extends Exception { +} diff --git a/lib/Tools/Exceptions/UnknownTypeException.php b/lib/Tools/Exceptions/UnknownTypeException.php new file mode 100644 index 00000000..100427f6 --- /dev/null +++ b/lib/Tools/Exceptions/UnknownTypeException.php @@ -0,0 +1,40 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class ArrayNotFoundException + * + * @package OCA\Social\Tools\Exceptions + */ +class UnknownTypeException extends Exception { +} diff --git a/lib/Tools/Exceptions/WellKnownLinkNotFoundException.php b/lib/Tools/Exceptions/WellKnownLinkNotFoundException.php new file mode 100644 index 00000000..705e21d4 --- /dev/null +++ b/lib/Tools/Exceptions/WellKnownLinkNotFoundException.php @@ -0,0 +1,40 @@ + + * @copyright 2020, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Exceptions; + +use Exception; + +/** + * Class CacheItemNotFoundException + * + * @package OCA\Social\Tools\Exceptions + */ +class WellKnownLinkNotFoundException extends Exception { +} diff --git a/lib/Tools/IDeserializable.php b/lib/Tools/IDeserializable.php new file mode 100644 index 00000000..d779dc9f --- /dev/null +++ b/lib/Tools/IDeserializable.php @@ -0,0 +1,42 @@ + + * @copyright 2020, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools; + +interface IDeserializable { + + /** + * @param array $data + * + * @return self + */ + public function import(array $data): self; +} diff --git a/lib/Tools/IExtendedQueryBuilder.php b/lib/Tools/IExtendedQueryBuilder.php new file mode 100644 index 00000000..e072a94c --- /dev/null +++ b/lib/Tools/IExtendedQueryBuilder.php @@ -0,0 +1,234 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools; + +use DateTime; +use Exception; +use OCP\DB\QueryBuilder\ICompositeExpression; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\DB\QueryBuilder\IQueryFunction; + +/** + * Interface IExtendedQueryBuilder + * + * @deprecated + * @package OCA\Social\Tools + */ +interface IExtendedQueryBuilder extends IQueryBuilder { + + + /** + * @param string $alias + * + * @return IExtendedQueryBuilder + */ + public function setDefaultSelectAlias(string $alias): IExtendedQueryBuilder; + + + /** + * @return string + */ + public function getDefaultSelectAlias(): string; + + + /** + * Limit the request to the Id + * + * @param int $id + * + * @return IExtendedQueryBuilder + */ + public function limitToId(int $id): IExtendedQueryBuilder; + + + /** + * Limit the request to Ids + * + * @param list $ids + * + * @return IExtendedQueryBuilder + */ + public function limitToIds(array $ids): IExtendedQueryBuilder; + + + /** + * Limit the request to the Id (string) + * + * @param string $id + * + * @return IExtendedQueryBuilder + */ + public function limitToIdString(string $id): IExtendedQueryBuilder; + + + /** + * Limit the request to the UserId + * + * @param string $userId + * + * @return IExtendedQueryBuilder + */ + public function limitToUserId(string $userId): IExtendedQueryBuilder; + + + /** + * Limit the request to the creation + * + * @param int $delay + * + * @return IExtendedQueryBuilder + * @throws Exception + */ + public function limitToCreation(int $delay = 0): IExtendedQueryBuilder; + + + /** + * @param string $field + * @param string $value + * @param bool $cs + * @param string $alias + */ + public function limitToDBField(string $field, string $value, bool $cs = true, string $alias = '' + ); + + + /** + * @param string $field + * @param string $value + * @param bool $cs + * @param string $alias + * + * @return mixed + */ + public function filterDBField(string $field, string $value, bool $cs = true, string $alias = '' + ); + + public function exprLimitToDBField( + string $field, string $value, bool $eq = true, bool $cs = true, string $alias = '' + ): IQueryFunction; + + public function limitToDBFieldArray( + string $field, array $values, bool $cs = true, string $alias = '' + ); + + + /** + * @param string $field + * @param string $value + * @param bool $cs + * @param string $alias + * + * @return mixed + */ + public function filterDBFieldArray( + string $field, string $value, bool $cs = true, string $alias = '' + ); + + + /** + * @param string $field + * @param array $values + * @param bool $eq + * @param bool $cs + * @param string $alias + * + * @return ICompositeExpression + */ + public function exprLimitToDBFieldArray( + string $field, array $values, bool $eq = true, bool $cs = true, string $alias = '' + ): ICompositeExpression; + + + /** + * @param string $field + * @param int $value + * @param string $alias + */ + public function limitToDBFieldInt(string $field, int $value, string $alias = ''); + + + /** + * @param string $field + * @param int $value + * @param string $alias + * + * @return mixed + */ + public function filterDBFieldInt(string $field, int $value, string $alias = ''); + + + /** + * @param string $field + * @param int $value + * @param string $alias + */ + public function exprLimitToDBFieldInt(string $field, int $value, string $alias = ''): IQueryFunction; + + + /** + * @param string $field + */ + public function limitToDBFieldEmpty(string $field); + + + /** + * @param string $field + * + * @return mixed + */ + public function filterDBFieldEmpty(string $field); + + + /** + * @param string $field + * @param DateTime $date + * @param bool $orNull + */ + public function limitToDBFieldDateTime(string $field, DateTime $date, bool $orNull = false + ); + + + /** + * @param int $timestamp + * @param string $field + */ + public function limitToSince(int $timestamp, string $field); + + + /** + * @param string $field + * @param string $value + * + * @return mixed + */ + public function searchInDBField(string $field, string $value); +} diff --git a/lib/Tools/IInteractiveShellClient.php b/lib/Tools/IInteractiveShellClient.php new file mode 100644 index 00000000..076c2388 --- /dev/null +++ b/lib/Tools/IInteractiveShellClient.php @@ -0,0 +1,65 @@ + + * @copyright 2021, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools; + +use OCA\Social\Tools\Exceptions\ShellMissingCommandException; +use OCA\Social\Tools\Exceptions\ShellMissingItemException; +use OCA\Social\Tools\Exceptions\ShellUnknownCommandException; +use OCA\Social\Tools\Exceptions\ShellUnknownItemException; + +/** + * Interface IInteractiveShellClient + * + * @package OCA\Social\Tools + */ +interface IInteractiveShellClient { + + + /** + * @param string $source + * @param string $field + * + * @return array + */ + public function fillCommandList(string $source, string $field): array; + + + /** + * @param string $command + * + * @throws ShellMissingItemException + * @throws ShellUnknownCommandException + * @throws ShellUnknownItemException + * @throws ShellMissingCommandException + */ + public function manageCommand(string $command): void; +} diff --git a/lib/Tools/IQueryRow.php b/lib/Tools/IQueryRow.php new file mode 100644 index 00000000..92108399 --- /dev/null +++ b/lib/Tools/IQueryRow.php @@ -0,0 +1,48 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools; + +/** + * Interface IQueryRow + * + * @deprecated + * @package OCA\Social\Tools + */ +interface IQueryRow { + + /** + * import data to feed the model. + * + * @param array $data + */ + public function importFromDatabase(array $data); +} diff --git a/lib/Tools/Model/Cache.php b/lib/Tools/Model/Cache.php new file mode 100644 index 00000000..2a43a928 --- /dev/null +++ b/lib/Tools/Model/Cache.php @@ -0,0 +1,219 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Social\Tools\Model; + +use OCA\Social\Tools\Exceptions\CacheItemNotFoundException; +use OCA\Social\Tools\Traits\TArrayTools; +use JsonSerializable; + +/** + * Class Cache + * + * @package OCA\Social\Tools\Model + */ +class Cache implements JsonSerializable { + use TArrayTools; + + + /** @var CacheItem[] */ + private $items = []; + + + public function __construct() { + } + + + /** + * @return bool + */ + public function hasItems(): bool { + return !empty($this->items); + } + + /** + * @return CacheItem[] + */ + public function getItems(): array { + return $this->items; + } + + /** + * @param CacheItem[] $items + * + * @return Cache + */ + public function setItems(array $items): Cache { + $this->items = $items; + + return $this; + } + + /** + * @param CacheItem $item + * + * @return Cache + */ + public function addItem(CacheItem $item): Cache { + if ($item->getUrl() === '' || $this->hasItem($item->getUrl())) { + return $this; + } + + $this->items[] = $item; + + return $this; + } + + + /** + * @param string $url + * + * @return CacheItem + * @throws CacheItemNotFoundException + */ + public function getItem(string $url): CacheItem { + foreach ($this->getItems() as $item) { + if ($item->getUrl() === $url) { + return $item; + } + } + + throw new CacheItemNotFoundException(); + } + + + /** + * @param string $url + * + * @return bool + */ + public function hasItem(string $url): bool { + try { + $this->getItem($url); + + return true; + } catch (CacheItemNotFoundException $e) { + return false; + } + } + + + /** + * @param string $url + * + * @return Cache + */ + public function removeItem(string $url): Cache { + $new = []; + foreach ($this->getItems() as $item) { + if ($item->getUrl() !== $url) { + $new[] = $item; + } + } + + $this->items = $new; + + return $this; + } + + /** + * $create can be false if we don't create item if it is not already in the list. + * + * @param CacheItem $cacheItem + * @param bool $create + * + * @return Cache + */ + public function updateItem(CacheItem $cacheItem, bool $create = true): Cache { + if ($cacheItem->getUrl() === '') { + return $this; + } + + $new = []; + $updated = false; + foreach ($this->getItems() as $item) { + if ($item->getUrl() === $cacheItem->getUrl()) { + $updated = true; + $new[] = $cacheItem; + } else { + $new[] = $item; + } + } + + if (!$updated && !$create) { + $new[] = $cacheItem; + } + + $this->items = $new; + + return $this; + } + + + /** + * @param array $data + */ + public function import(array $data) { + $items = $this->getArray('_items', $data, []); + + foreach ($items as $entry) { + $item = new CacheItem($entry); + if (!array_key_exists($entry, $data)) { + continue; + } + + $item->import($data[$entry]); + $this->addItem($item); + } + } + + + /** + * @return array + */ + public function jsonSerialize(): array { + $ids = array_map( + function (CacheItem $item) { + return $item->getUrl(); + }, $this->getItems() + ); + + $result = [ + '_items' => $ids, + '_count' => count($ids) + ]; + + foreach ($this->getItems() as $item) { + $result[$item->getUrl()] = $item; + } + + return $result; + } +} diff --git a/lib/Tools/Model/CacheItem.php b/lib/Tools/Model/CacheItem.php new file mode 100644 index 00000000..c462b60f --- /dev/null +++ b/lib/Tools/Model/CacheItem.php @@ -0,0 +1,209 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Social\Tools\Model; + +use OCA\Social\Tools\Traits\TArrayTools; +use JsonSerializable; + +/** + * Class CacheItem + * + * @package OCA\Social\Tools\Model + */ +class CacheItem implements JsonSerializable { + use TArrayTools; + + + /** @var string */ + private $url = ''; + + /** @var string */ + private $content = ''; + + /** @var int */ + private $status = 0; + + /** @var int */ + private $error = 0; + + /** @var int */ + private $creation = 0; + + + /** + * CacheItem constructor. + * + * @param string $url + */ + public function __construct(string $url) { + $this->url = $url; + } + + + /** + * @return string + */ + public function getUrl(): string { + return $this->url; + } + + /** + * @param string $url + * + * @return CacheItem + */ + public function setUrl(string $url): CacheItem { + $this->url = $url; + + return $this; + } + + + /** + * @return string + */ + public function getContent(): string { + return $this->content; + } + + /** + * @param string $content + * + * @return CacheItem + */ + public function setContent(string $content): CacheItem { + $this->content = $content; + + return $this; + } + + + /** + * @return array + */ + public function getObject(): array { + $arr = json_decode($this->content, true); + + if (is_array($arr)) { + return $arr; + } + + return []; + } + + + /** + * @return int + */ + public function getStatus(): int { + return $this->status; + } + + /** + * @param int $status + * + * @return CacheItem + */ + public function setStatus(int $status): CacheItem { + $this->status = $status; + + return $this; + } + + /** + * @return int + */ + public function getError(): int { + return $this->error; + } + + /** + * @return CacheItem + */ + public function incrementError(): CacheItem { + $this->error++; + + return $this; + } + + /** + * @param int $error + */ + public function setError(int $error) { + $this->error = $error; + } + + + /** + * @return int + */ + public function getCreation(): int { + return $this->creation; + } + + /** + * @param int $creation + * + * @return CacheItem + */ + public function setCreation(int $creation): CacheItem { + $this->creation = $creation; + + return $this; + } + + + /** + * @param array $data + */ + public function import(array $data) { + $this->setUrl($this->get('url', $data, '')); + $this->setContent($this->get('content', $data, '')); + $this->setStatus($this->getInt('status', $data, 0)); + $this->setError($this->getInt('error', $data, 0)); + $this->setCreation($this->getInt('creation', $data, 0)); + } + + + /** + * @return array + */ + public function jsonSerialize(): array { + return [ + 'url' => $this->getUrl(), + 'content' => $this->getContent(), + 'object' => $this->getObject(), + 'status' => $this->getStatus(), + 'error' => $this->getError(), + 'creation' => $this->getCreation() + ]; + } +} diff --git a/lib/Tools/Model/NCRequest.php b/lib/Tools/Model/NCRequest.php new file mode 100644 index 00000000..b62f84b5 --- /dev/null +++ b/lib/Tools/Model/NCRequest.php @@ -0,0 +1,84 @@ + + * @copyright 2020, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Model; + +use OCP\Http\Client\IClient; + +/** + * Class NCRequest + * + * @package OCA\Social\Tools\Model + */ +class NCRequest extends Request { + private IClient $client; + private array $clientOptions = []; + private bool $localAddressAllowed = false; + + public function setClient(IClient $client): self { + $this->client = $client; + return $this; + } + + public function getClient(): IClient { + return $this->client; + } + + public function getClientOptions(): array { + return $this->clientOptions; + } + + public function setClientOptions(array $clientOptions): self { + $this->clientOptions = $clientOptions; + + return $this; + } + + public function isLocalAddressAllowed(): bool { + return $this->localAddressAllowed; + } + + public function setLocalAddressAllowed(bool $allowed): self { + $this->localAddressAllowed = $allowed; + + return $this; + } + + public function jsonSerialize(): array { + return array_merge( + parent::jsonSerialize(), + [ + 'clientOptions' => $this->getClientOptions(), + 'localAddressAllowed' => $this->isLocalAddressAllowed(), + ] + ); + } +} diff --git a/lib/Tools/Model/Request.php b/lib/Tools/Model/Request.php new file mode 100644 index 00000000..1ee6763f --- /dev/null +++ b/lib/Tools/Model/Request.php @@ -0,0 +1,837 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Model; + +use OCA\Social\Tools\Traits\TArrayTools; +use JsonSerializable; + +/** + * Class Request + * + * @package OCA\Social\Tools\Model + */ +class Request implements JsonSerializable { + use TArrayTools; + + + public const TYPE_GET = 0; + public const TYPE_POST = 1; + public const TYPE_PUT = 2; + public const TYPE_DELETE = 3; + + + public const QS_VAR_DUPLICATE = 1; + public const QS_VAR_ARRAY = 2; + + + /** @var string */ + private $protocol = ''; + + /** @var array */ + private $protocols = ['https']; + + /** @var string */ + private $host = ''; + + /** @var int */ + private $port = 0; + + /** @var string */ + private $url = ''; + + /** @var string */ + private $baseUrl = ''; + + /** @var int */ + private $type = 0; + + /** @var bool */ + private $binary = false; + + /** @var bool */ + private $verifyPeer = true; + + /** @var bool */ + private $httpErrorsAllowed = false; + + /** @var bool */ + private $followLocation = true; + + /** @var array */ + private $headers = []; + + /** @var array */ + private $cookies = []; + + /** @var array */ + private $params = []; + + /** @var array */ + private $data = []; + + /** @var int */ + private $queryStringType = self::QS_VAR_DUPLICATE; + + /** @var int */ + private $timeout = 10; + + /** @var string */ + private $userAgent = ''; + + /** @var int */ + private $resultCode = 0; + + /** @var string */ + private $contentType = ''; + + + /** + * Request constructor. + * + * @param string $url + * @param int $type + * @param bool $binary + */ + public function __construct(string $url = '', int $type = 0, bool $binary = false) { + $this->url = $url; + $this->type = $type; + $this->binary = $binary; + } + + + /** + * @param string $protocol + * + * @return Request + */ + public function setProtocol(string $protocol): Request { + $this->protocols = [$protocol]; + + return $this; + } + + /** + * @param array $protocols + * + * @return Request + */ + public function setProtocols(array $protocols): Request { + $this->protocols = $protocols; + + return $this; + } + + /** + * @return string[] + */ + public function getProtocols(): array { + return $this->protocols; + } + + /** + * @return string + */ + public function getUsedProtocol(): string { + return $this->protocol; + } + + /** + * @param string $protocol + * + * @return Request + */ + public function setUsedProtocol(string $protocol): Request { + $this->protocol = $protocol; + + return $this; + } + + + /** + * @return string + * @deprecated - 19 - use getHost(); + */ + public function getAddress(): string { + return $this->getHost(); + } + + /** + * @param string $address + * + * @return Request + * @deprecated - 19 - use setHost(); + */ + public function setAddress(string $address): Request { + $this->setHost($address); + + return $this; + } + + /** + * @return string + */ + public function getHost(): string { + return $this->host; + } + + /** + * @param string $host + * + * @return Request + */ + public function setHost(string $host): Request { + $this->host = $host; + + return $this; + } + + + /** + * @return int + */ + public function getPort(): int { + return $this->port; + } + + /** + * @param int $port + * + * @return Request + */ + public function setPort(int $port): Request { + $this->port = $port; + + return $this; + } + + + /** + * @param string $instance + * + * @return Request + */ + public function setInstance(string $instance): Request { + if (strpos($instance, ':') === false) { + $this->setHost($instance); + + return $this; + } + + list($host, $port) = explode(':', $instance, 2); + $this->setHost($host); + if ($port !== '') { + $this->setPort((int)$port); + } + + return $this; + } + + + /** + * @return string + */ + public function getInstance(): string { + $instance = $this->getHost(); + if ($this->getPort() > 0) { + $instance .= ':' . $this->getPort(); + } + + return $instance; + } + + + /** + * @param string $url + * + * @deprecated - 19 - use basedOnUrl(); + */ + public function setAddressFromUrl(string $url) { + $this->basedOnUrl($url); + } + + /** + * @param string $url + */ + public function basedOnUrl(string $url) { + $protocol = parse_url($url, PHP_URL_SCHEME); + if ($protocol === null) { + if (strpos($url, '/') > -1) { + list($address, $baseUrl) = explode('/', $url, 2); + $this->setBaseUrl('/' . $baseUrl); + } else { + $address = $url; + } + if (strpos($address, ':') > -1) { + list($address, $port) = explode(':', $address, 2); + $this->setPort((int)$port); + } + $this->setHost($address); + } else { + $this->setProtocols([$protocol]); + $this->setUsedProtocol($protocol); + $this->setHost(parse_url($url, PHP_URL_HOST)); + $this->setBaseUrl(parse_url($url, PHP_URL_PATH)); + if (is_numeric($port = parse_url($url, PHP_URL_PORT))) { + $this->setPort($port); + } + } + } + + /** + * @param string|null $baseUrl + * + * @return Request + */ + public function setBaseUrl(?string $baseUrl): Request { + if ($baseUrl !== null) { + $this->baseUrl = $baseUrl; + } + + return $this; + } + + /** + * @return bool + */ + public function isBinary(): bool { + return $this->binary; + } + + + /** + * @param bool $verifyPeer + * + * @return $this + */ + public function setVerifyPeer(bool $verifyPeer): Request { + $this->verifyPeer = $verifyPeer; + + return $this; + } + + /** + * @return bool + */ + public function isVerifyPeer(): bool { + return $this->verifyPeer; + } + + + /** + * @param bool $httpErrorsAllowed + * + * @return Request + */ + public function setHttpErrorsAllowed(bool $httpErrorsAllowed): Request { + $this->httpErrorsAllowed = $httpErrorsAllowed; + + return $this; + } + + /** + * @return bool + */ + public function isHttpErrorsAllowed(): bool { + return $this->httpErrorsAllowed; + } + + + /** + * @param bool $followLocation + * + * @return $this + */ + public function setFollowLocation(bool $followLocation): Request { + $this->followLocation = $followLocation; + + return $this; + } + + /** + * @return bool + */ + public function isFollowLocation(): bool { + return $this->followLocation; + } + + + /** + * @return string + * @deprecated - 19 - use getParametersUrl() + addParam() + */ + public function getParsedUrl(): string { + $url = $this->getPath(); + $ak = array_keys($this->getData()); + foreach ($ak as $k) { + if (!is_string($this->data[$k])) { + continue; + } + + $url = str_replace(':' . $k, $this->data[$k], $url); + } + + return $url; + } + + /** + * @return string + */ + public function getParametersUrl(): string { + $url = $this->getPath(); + $ak = array_keys($this->getParams()); + foreach ($ak as $k) { + if (!is_string($this->params[$k])) { + continue; + } + + $url = str_replace(':' . $k, $this->params[$k], $url); + } + + return $url; + } + + + /** + * @return string + */ + public function getPath(): string { + return $this->baseUrl . $this->url; + } + + + /** + * @return string + * @deprecated - 19 - use getPath() + */ + public function getUrl(): string { + return $this->getPath(); + } + + + /** + * @return string + */ + public function getCompleteUrl(): string { + $port = ($this->getPort() > 0) ? ':' . $this->getPort() : ''; + + return $this->getUsedProtocol() . '://' . $this->getHost() . $port . $this->getParametersUrl(); + } + + + /** + * @return int + */ + public function getType(): int { + return $this->type; + } + + + public function addHeader($key, $value): Request { + $header = $this->get($key, $this->headers); + if ($header !== '') { + $header .= ', ' . $value; + } else { + $header = $value; + } + + $this->headers[$key] = $header; + + return $this; + } + + /** + * @return array + */ + public function getHeaders(): array { + return array_merge(['User-Agent' => $this->getUserAgent()], $this->headers); + } + + /** + * @param array $headers + * + * @return Request + */ + public function setHeaders(array $headers): Request { + $this->headers = $headers; + + return $this; + } + + + /** + * @return array + */ + public function getCookies(): array { + return $this->cookies; + } + + /** + * @param array $cookies + * + * @return Request + */ + public function setCookies(array $cookies): Request { + $this->cookies = $cookies; + + return $this; + } + + + /** + * @param int $queryStringType + * + * @return Request + */ + public function setQueryStringType(int $queryStringType): self { + $this->queryStringType = $queryStringType; + + return $this; + } + + /** + * @return int + */ + public function getQueryStringType(): int { + return $this->queryStringType; + } + + + /** + * @return array + */ + public function getData(): array { + return $this->data; + } + + + /** + * @param array $data + * + * @return Request + */ + public function setData(array $data): Request { + $this->data = $data; + + return $this; + } + + + /** + * @param string $data + * + * @return Request + */ + public function setDataJson(string $data): Request { + $this->setData(json_decode($data, true)); + + return $this; + } + + + /** + * @param JsonSerializable $data + * + * @return Request + */ + public function setDataSerialize(JsonSerializable $data): Request { + $this->setDataJson(json_encode($data)); + + return $this; + } + + + /** + * @return array + */ + public function getParams(): array { + return $this->params; + } + + /** + * @param array $params + * + * @return Request + */ + public function setParams(array $params): Request { + $this->params = $params; + + return $this; + } + + + /** + * @param string $k + * @param string $v + * + * @return Request + */ + public function addParam(string $k, string $v): Request { + $this->params[$k] = $v; + + return $this; + } + + + /** + * @param string $k + * @param int $v + * + * @return Request + */ + public function addParamInt(string $k, int $v): Request { + $this->params[$k] = $v; + + return $this; + } + + + /** + * @param string $k + * @param string $v + * + * @return Request + */ + public function addData(string $k, string $v): Request { + $this->data[$k] = $v; + + return $this; + } + + + /** + * @param string $k + * @param int $v + * + * @return Request + */ + public function addDataInt(string $k, int $v): Request { + $this->data[$k] = $v; + + return $this; + } + + + /** + * @return string + */ + public function getDataBody(): string { + return json_encode($this->getData()); + } + + /** + * @return string + * @deprecated - 19 - use getUrlParams(); + */ + public function getUrlData(): string { + if ($this->getData() === []) { + return ''; + } + + return preg_replace( + '/([(%5B)]{1})[0-9]+([(%5D)]{1})/', '$1$2', http_build_query($this->getData()) + ); + } + + /** + * @return string + * @deprecated - 21 - use getQueryString(); + */ + public function getUrlParams(): string { + if ($this->getParams() === []) { + return ''; + } + + return preg_replace( + '/([(%5B)]{1})[0-9]+([(%5D)]{1})/', '$1$2', http_build_query($this->getParams()) + ); + } + + + /** + * @param int $type + * + * @return string + */ + public function getQueryString(): string { + if (empty($this->getParams())) { + return ''; + } + + switch ($this->getQueryStringType()) { + case self::QS_VAR_ARRAY: + return '?' . http_build_query($this->getParams()); + + case self::QS_VAR_DUPLICATE: + default: + return '?' . preg_replace( + '/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', http_build_query($this->getParams()) + ); + } + } + + + /** + * @return int + */ + public function getTimeout(): int { + return $this->timeout; + } + + /** + * @param int $timeout + * + * @return Request + */ + public function setTimeout(int $timeout): Request { + $this->timeout = $timeout; + + return $this; + } + + + /** + * @return string + */ + public function getUserAgent(): string { + return $this->userAgent; + } + + /** + * @param string $userAgent + * + * @return Request + */ + public function setUserAgent(string $userAgent): Request { + $this->userAgent = $userAgent; + + return $this; + } + + + /** + * @return int + */ + public function getResultCode(): int { + return $this->resultCode; + } + + /** + * @param int $resultCode + * + * @return Request + */ + public function setResultCode(int $resultCode): Request { + $this->resultCode = $resultCode; + + return $this; + } + + + /** + * @return string + */ + public function getContentType(): string { + return $this->contentType; + } + + /** + * @param string $contentType + * + * @return Request + */ + public function setContentType(string $contentType): Request { + $this->contentType = $contentType; + + return $this; + } + + + /** + * @return array + */ + public function jsonSerialize(): array { + return [ + 'protocols' => $this->getProtocols(), + 'used_protocol' => $this->getUsedProtocol(), + 'port' => $this->getPort(), + 'host' => $this->getHost(), + 'url' => $this->getPath(), + 'timeout' => $this->getTimeout(), + 'type' => $this->getType(), + 'cookies' => $this->getCookies(), + 'headers' => $this->getHeaders(), + 'params' => $this->getParams(), + 'data' => $this->getData(), + 'userAgent' => $this->getUserAgent(), + 'followLocation' => $this->isFollowLocation(), + 'verifyPeer' => $this->isVerifyPeer(), + 'binary' => $this->isBinary() + ]; + } + + + /** + * @param string $type + * + * @return int + */ + public static function type(string $type): int { + switch (strtoupper($type)) { + case 'GET': + return self::TYPE_GET; + case 'POST': + return self::TYPE_POST; + case 'PUT': + return self::TYPE_PUT; + case 'DELETE': + return self::TYPE_DELETE; + } + + return 0; + } + + + public static function method(int $type): string { + switch ($type) { + case self::TYPE_GET: + return 'get'; + case self::TYPE_POST: + return 'post'; + case self::TYPE_PUT: + return 'put'; + case self::TYPE_DELETE: + return 'delete'; + } + + return ''; + } +} diff --git a/lib/Tools/Model/SimpleDataStore.php b/lib/Tools/Model/SimpleDataStore.php new file mode 100644 index 00000000..a929756d --- /dev/null +++ b/lib/Tools/Model/SimpleDataStore.php @@ -0,0 +1,393 @@ + + * @copyright 2020, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Model; + +use OCA\Social\Tools\Exceptions\ItemNotFoundException; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Traits\TArrayTools; +use JsonSerializable; + +/** + * Class SimpleDataStore + * + * @package OCA\Social\Tools\Model + */ +class SimpleDataStore implements JsonSerializable { + use TArrayTools; + + + /** @var array */ + private $data; + + + /** + * SimpleDataStore constructor. + * + * @param array $data + */ + public function __construct(?array $data = []) { + if (!is_array($data)) { + $data = []; + } + + $this->data = $data; + } + + + /** + * @param array $default + */ + public function default(array $default = []) { + $this->data = array_merge($default, $this->data); + } + + + /** + * @param string $key + * @param string $value + * + * @return SimpleDataStore + */ + public function s(string $key, string $value): self { + $this->data[$key] = $value; + + return $this; + } + + /** + * @param string $key + * + * @return string + */ + public function g(string $key): string { + return $this->get($key, $this->data); + } + + /** + * @param string $key + * @param string $value + * + * @return SimpleDataStore + */ + public function a(string $key, string $value): self { + if (!array_key_exists($key, $this->data)) { + $this->data[$key] = []; + } + + $this->data[$key][] = $value; + + return $this; + } + + + /** + * @param string $key + * @param int $value + * + * @return SimpleDataStore + */ + public function sInt(string $key, int $value): self { + $this->data[$key] = $value; + + return $this; + } + + /** + * @param string $key + * + * @return int + */ + public function gInt(string $key): int { + return $this->getInt($key, $this->data); + } + + /** + * @param string $key + * @param int $value + * + * @return SimpleDataStore + */ + public function aInt(string $key, int $value): self { + if (!array_key_exists($key, $this->data)) { + $this->data[$key] = []; + } + + $this->data[$key][] = $value; + + return $this; + } + + + /** + * @param string $key + * @param bool $value + * + * @return SimpleDataStore + */ + public function sBool(string $key, bool $value): self { + $this->data[$key] = $value; + + return $this; + } + + /** + * @param string $key + * + * @return bool + */ + public function gBool(string $key): bool { + return $this->getBool($key, $this->data); + } + + /** + * @param string $key + * @param bool $value + * + * @return SimpleDataStore + */ + public function aBool(string $key, bool $value): self { + if (!array_key_exists($key, $this->data)) { + $this->data[$key] = []; + } + + $this->data[$key][] = $value; + + return $this; + } + + + /** + * @param string $key + * @param array $values + * + * @return SimpleDataStore + */ + public function sArray(string $key, array $values): self { + $this->data[$key] = $values; + + return $this; + } + + /** + * @param string $key + * + * @return array + */ + public function gArray(string $key): array { + return $this->getArray($key, $this->data); + } + + /** + * @param string $key + * @param array $values + * + * @return SimpleDataStore + */ + public function aArray(string $key, array $values): self { + if (!array_key_exists($key, $this->data)) { + $this->data[$key] = []; + } + + $this->data[$key] = array_merge($this->data[$key], $values); + + return $this; + } + + + /** + * @param string $key + * @param JsonSerializable $value + * + * @return SimpleDataStore + */ + public function sObj(string $key, JsonSerializable $value): self { + $this->data[$key] = $value; + + return $this; + } + + /** + * @param string $key + * @param JsonSerializable $value + * + * @return SimpleDataStore + */ + public function aObj(string $key, JsonSerializable $value): self { + if (!array_key_exists($key, $this->data)) { + $this->data[$key] = []; + } + + $this->data[$key][] = $value; + + return $this; + } + + + /** + * @param string $key + * @param SimpleDataStore $data + * + * @return $this + */ + public function sData(string $key, SimpleDataStore $data): self { + $this->data[$key] = $data->gAll(); + + return $this; + } + + /** + * @param string $key + * @param SimpleDataStore $data + * + * @return $this + */ + public function aData(string $key, SimpleDataStore $data): self { + if (!array_key_exists($key, $this->data) || !is_array($this->data[$key])) { + $this->data[$key] = []; + } + + $this->data[$key][] = $data->gAll(); + + return $this; + } + + /** + * @param string $key + * + * @return SimpleDataStore + */ + public function gData(string $key): SimpleDataStore { + return new SimpleDataStore($this->getArray($key, $this->data)); + } + + + /** + * @param string $key + * + * @return mixed + * @throws ItemNotFoundException + */ + public function gItem(string $key) { + if (!array_key_exists($key, $this->data)) { + throw new ItemNotFoundException(); + } + + return $this->data[$key]; + } + + + /** + * @return array + */ + public function gAll(): array { + return $this->data; + } + + /** + * @param array $data + * + * @return SimpleDataStore + */ + public function sAll(array $data): self { + $this->data = $data; + + return $this; + } + + + public function keys(): array { + return array_keys($this->data); + } + + /** + * @param string $key + * + * @return bool + */ + public function hasKey(string $key): bool { + return (array_key_exists($key, $this->data)); + } + + + /** + * @param array $keys + * + * @param bool $must + * + * @return bool + * @throws MalformedArrayException + */ + public function hasKeys(array $keys, bool $must = false): bool { + foreach ($keys as $key) { + if (!$this->haveKey($key)) { + if ($must) { + throw new MalformedArrayException($key . ' missing in ' . json_encode($this->keys())); + } + + return false; + } + } + + return true; + } + + + /** + * @param array $keys + * @param bool $must + * + * @return bool + * @throws MalformedArrayException + * @deprecated + */ + public function haveKeys(array $keys, bool $must = false): bool { + return $this->hasKeys($keys, $must); + } + + /** + * @param string $key + * + * @return bool + * @deprecated + */ + public function haveKey(string $key): bool { + return $this->hasKey($key); + } + + + /** + * @return array + */ + public function jsonSerialize(): array { + return $this->data; + } +} diff --git a/lib/Tools/Traits/TArrayTools.php b/lib/Tools/Traits/TArrayTools.php new file mode 100644 index 00000000..e5e29419 --- /dev/null +++ b/lib/Tools/Traits/TArrayTools.php @@ -0,0 +1,382 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Traits; + +use OCA\Social\Tools\Exceptions\ArrayNotFoundException; +use OCA\Social\Tools\Exceptions\ItemNotFoundException; +use OCA\Social\Tools\Exceptions\MalformedArrayException; +use OCA\Social\Tools\Exceptions\UnknownTypeException; +use Exception; +use JsonSerializable; + +/** + * Trait TArrayTools + * + * @package OCA\Social\Tools\Traits + */ +trait TArrayTools { + public static $TYPE_NULL = 'Null'; + public static $TYPE_STRING = 'String'; + public static $TYPE_ARRAY = 'Array'; + public static $TYPE_BOOLEAN = 'Boolean'; + public static $TYPE_INTEGER = 'Integer'; + public static $TYPE_SERIALIZABLE = 'Serializable'; + + protected function get(string $k, array $arr, string $default = ''): string { + if (!array_key_exists($k, $arr)) { + $subs = explode('.', $k, 2); + if (sizeof($subs) > 1) { + if (!array_key_exists($subs[0], $arr)) { + return $default; + } + + $r = $arr[$subs[0]]; + if (!is_array($r)) { + return $default; + } + + return $this->get($subs[1], $r, $default); + } else { + return $default; + } + } + + if ($arr[$k] === null || !is_string($arr[$k]) && (!is_int($arr[$k]))) { + return $default; + } + + return (string)$arr[$k]; + } + + protected function getInt(string $k, array $arr, int $default = 0): int { + if (!array_key_exists($k, $arr)) { + $subs = explode('.', $k, 2); + if (sizeof($subs) > 1) { + if (!array_key_exists($subs[0], $arr)) { + return $default; + } + + $r = $arr[$subs[0]]; + if (!is_array($r)) { + return $default; + } + + return $this->getInt($subs[1], $r, $default); + } else { + return $default; + } + } + + if ($arr[$k] === null) { + return $default; + } + + return intval($arr[$k]); + } + + protected function getFloat(string $k, array $arr, float $default = 0): float { + if (!array_key_exists($k, $arr)) { + $subs = explode('.', $k, 2); + if (sizeof($subs) > 1) { + if (!array_key_exists($subs[0], $arr)) { + return $default; + } + + $r = $arr[$subs[0]]; + if (!is_array($r)) { + return $default; + } + + return $this->getFloat($subs[1], $r, $default); + } else { + return $default; + } + } + + if ($arr[$k] === null) { + return $default; + } + + return intval($arr[$k]); + } + + protected function getBool(string $k, array $arr, bool $default = false): bool { + if (!array_key_exists($k, $arr)) { + $subs = explode('.', $k, 2); + if (sizeof($subs) > 1) { + if (!array_key_exists($subs[0], $arr)) { + return $default; + } + + return $this->getBool($subs[1], $arr[$subs[0]], $default); + } else { + return $default; + } + } + + if ($arr[$k] === null) { + return $default; + } + + if (is_bool($arr[$k])) { + return $arr[$k]; + } + + $sk = (string)$arr[$k]; + if ($sk === '1' || strtolower($sk) === 'true') { + return true; + } + + if ($sk === '0' || strtolower($sk) === 'false') { + return false; + } + + return $default; + } + + protected function getObj(string $k, array $arr, ?JsonSerializable $default = null): ?JsonSerializable { + if (!array_key_exists($k, $arr)) { + $subs = explode('.', $k, 2); + if (sizeof($subs) > 1) { + if (!array_key_exists($subs[0], $arr)) { + return $default; + } + + return $this->getObj($subs[1], $arr[$subs[0]], $default); + } else { + return $default; + } + } + + return $arr[$k]; + } + + protected function getArray(string $k, array $arr, array $default = []): array { + if (!array_key_exists($k, $arr)) { + $subs = explode('.', $k, 2); + if (sizeof($subs) > 1) { + if (!array_key_exists($subs[0], $arr)) { + return $default; + } + + $r = $arr[$subs[0]]; + if (!is_array($r)) { + return $default; + } + + return $this->getArray($subs[1], $r, $default); + } else { + return $default; + } + } + + $r = $arr[$k]; + if ($r === null || (!is_array($r) && !is_string($r))) { + return $default; + } + + if (is_string($r)) { + $r = json_decode($r, true); + } + + if (!is_array($r)) { + return $default; + } + + return $r; + } + + public function validKey(string $k, array $arr): bool { + if (array_key_exists($k, $arr)) { + return true; + } + + $subs = explode('.', $k, 2); + if (sizeof($subs) > 1) { + if (!array_key_exists($subs[0], $arr)) { + return false; + } + + $r = $arr[$subs[0]]; + if (!is_array($r)) { + return false; + } + + return $this->validKey($subs[1], $r); + } + + return false; + } + + + /** + * @param string $k + * @param array $arr + * @param array $import + * @param array $default + * + * @return array + */ + protected function getList(string $k, array $arr, array $import, array $default = []): array { + $list = $this->getArray($k, $arr, $default); + + $r = []; + list($obj, $method) = $import; + foreach ($list as $item) { + try { + $o = new $obj(); + $o->$method($item); + + $r[] = $o; + } catch (Exception $e) { + } + } + + return $r; + } + + + /** + * @param string $k + * @param string $value + * @param array $list + * + * @return mixed + * @throws ArrayNotFoundException + */ + protected function extractArray(string $k, string $value, array $list) { + foreach ($list as $arr) { + if (!array_key_exists($k, $arr)) { + continue; + } + + if ($arr[$k] === $value) { + return $arr; + } + } + + throw new ArrayNotFoundException(); + } + + + /** + * @param string $key + * @param array $arr + * @param bool $root + * + * @return string + * @throws ItemNotFoundException + * @throws UnknownTypeException + */ + public function typeOf(string $key, array $arr, bool $root = true): string { + if (array_key_exists($key, $arr)) { + $item = $arr[$key]; + + if (is_null($item)) { + return self::$TYPE_NULL; + } + + if (is_string($item)) { + return self::$TYPE_STRING; + } + + if (is_array($item)) { + return self::$TYPE_ARRAY; + } + + if (is_bool($item)) { + return self::$TYPE_BOOLEAN; + } + + if (is_int($item)) { + return self::$TYPE_INTEGER; + } + + if ($item instanceof JsonSerializable) { + return self::$TYPE_SERIALIZABLE; + } + + throw new UnknownTypeException(); + } + + $subs = explode('.', $key, 2); + if (sizeof($subs) > 1) { + if (!array_key_exists($subs[0], $arr)) { + throw new ItemNotFoundException(); + } + + $r = $arr[$subs[0]]; + if (is_array($r)) { + return $this->typeOf($subs[1], $r); + } + } + + throw new ItemNotFoundException(); + } + + + /** + * @param array $keys + * @param array $arr + * + * @throws MalformedArrayException + */ + protected function mustContains(array $keys, array $arr) { + foreach ($keys as $key) { + if (!array_key_exists($key, $arr)) { + throw new MalformedArrayException( + 'source: ' . json_encode($arr) . ' - missing key: ' . $key + ); + } + } + } + + + /** + * @param array $arr + */ + protected function cleanArray(array &$arr) { + $arr = array_filter( + $arr, + function ($v) { + if (is_string($v)) { + return ($v !== ''); + } + if (is_array($v)) { + return !empty($v); + } + + return true; + } + ); + } +} diff --git a/lib/Tools/Traits/TAsync.php b/lib/Tools/Traits/TAsync.php new file mode 100644 index 00000000..e025f079 --- /dev/null +++ b/lib/Tools/Traits/TAsync.php @@ -0,0 +1,72 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Traits; + +use JsonSerializable; + +/** + * @deprecated + * Trait TAsync + * + * @package OCA\Social\Tools\Traits + */ +trait TAsync { + + + /** + * Hacky way to async the rest of the process without keeping client on hold. + * + * @param string $result + */ + public function async(string $result = ''): void { + if (ob_get_contents() !== false) { + ob_end_clean(); + } + + header('Connection: close'); + header('Content-Encoding: none'); + ignore_user_abort(); + ob_start(); + echo($result); + $size = ob_get_length(); + header('Content-Length: ' . $size); + ob_end_flush(); + flush(); + } + + /** + * @param JsonSerializable $obj + */ + public function asyncObj(JsonSerializable $obj): void { + $this->async(json_encode($obj)); + } +} diff --git a/lib/Tools/Traits/TFileTools.php b/lib/Tools/Traits/TFileTools.php new file mode 100644 index 00000000..218e505b --- /dev/null +++ b/lib/Tools/Traits/TFileTools.php @@ -0,0 +1,52 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Traits; + +/** + * Trait TFileTools + * + * @package OCA\Social\Tools\Traits + */ +trait TFileTools { + + /** + * @param $stream + * + * @return string + */ + protected function getChecksumFromStream($stream): string { + $ctx = hash_init('md5'); + hash_update_stream($ctx, $stream); + + return hash_final($ctx); + } +} diff --git a/lib/Tools/Traits/TNCDataResponse.php b/lib/Tools/Traits/TNCDataResponse.php new file mode 100644 index 00000000..36c393b7 --- /dev/null +++ b/lib/Tools/Traits/TNCDataResponse.php @@ -0,0 +1,92 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Traits; + +use Exception; +use JsonSerializable; +use OC; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; + +/** + * Trait TNCDataResponse + * + * @deprecated - 19 + * @package OCA\Social\Tools\Traits + */ +trait TNCDataResponse { + /** + * @return DataResponse + */ + protected function fail( + Exception $e, array $more = [], int $status = Http::STATUS_INTERNAL_SERVER_ERROR, + bool $log = true + ): DataResponse { + $data = array_merge( + $more, + [ + 'status' => -1, + 'exception' => get_class($e), + 'message' => $e->getMessage() + ] + ); + + if ($log) { + OC::$server->getLogger() + ->log(2, $status . ' - ' . json_encode($data)); + } + + return new DataResponse($data, $status); + } + + + /** + * @param array $result + * @param array $more + * + * @return DataResponse + */ + protected function success(array $result = [], array $more = []): DataResponse { + $data = array_merge( + $more, + [ + 'result' => $result, + 'status' => 1 + ] + ); + + return new DataResponse($data, Http::STATUS_OK); + } + protected function directSuccess(JsonSerializable $result): DataResponse { + return new DataResponse($result, Http::STATUS_OK); + } +} diff --git a/lib/Tools/Traits/TNCLogger.php b/lib/Tools/Traits/TNCLogger.php new file mode 100644 index 00000000..57bec246 --- /dev/null +++ b/lib/Tools/Traits/TNCLogger.php @@ -0,0 +1,200 @@ + + * @copyright 2020, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Traits; + +use Exception; +use OC\HintException; +use OCP\Server; +use Psr\Log\LoggerInterface; +use Throwable; + +trait TNCLogger { + use TNCSetup; + + public static int $EMERGENCY = 4; + public static int $ALERT = 3; + public static int $CRITICAL = 3; + public static int $ERROR = 3; + public static int $WARNING = 2; + public static int $NOTICE = 1; + public static int $INFO = 1; + public static int $DEBUG = 0; + + + /** + * @param Throwable $t + * @param array $serializable + */ + public function t(Throwable $t, array $serializable = []): void { + $this->throwable($t, self::$ERROR, $serializable); + } + + /** + * @param Throwable $t + * @param int $level + * @param array $serializable + */ + public function throwable(Throwable $t, int $level = 3, array $serializable = []): void { + $message = ''; + if (!empty($serializable)) { + $message = json_encode($serializable); + } + + $this->logger() + ->log( + $level, + $message, + [ + 'app' => $this->setup('app'), + 'exception' => $t + ] + ); + } + + + /** + * @param Exception $e + * @param array $serializable + */ + public function e(Exception $e, array $serializable = []): void { + $this->exception($e, self::$ERROR, $serializable); + } + + /** + * @param Exception $e + * @param int|array $level + * @param array $serializable + */ + public function exception(Exception $e, $level = 3, array $serializable = []): void { + if (is_array($level) && empty($serializable)) { + $serializable = $level; + $level = 3; + } + + $message = ''; + if (!empty($serializable)) { + $message = json_encode($serializable); + } + + if ($level === self::$DEBUG) { + $level = (int)$this->appConfig('debug_level'); + } + + $this->logger() + ->log( + $level, + $message, + [ + 'app' => $this->setup('app'), + 'exception' => $e + ] + ); + } + + + /** + * @param string $message + * @param bool $trace + * @param array $serializable + */ + public function emergency(string $message, bool $trace = false, array $serializable = []): void { + $this->log(self::$EMERGENCY, '[emergency] ' . $message, $trace, $serializable); + } + + /** + * @param string $message + * @param bool $trace + * @param array $serializable + */ + public function alert(string $message, bool $trace = false, array $serializable = []): void { + $this->log(self::$ALERT, '[alert] ' . $message, $trace, $serializable); + } + + /** + * @param string $message + * @param bool $trace + * @param array $serializable + */ + public function warning(string $message, bool $trace = false, array $serializable = []): void { + $this->log(self::$WARNING, '[warning] ' . $message, $trace, $serializable); + } + + /** + * @param string $message + * @param bool $trace + * @param array $serializable + */ + public function notice(string $message, bool $trace = false, array $serializable = []): void { + $this->log(self::$NOTICE, '[notice] ' . $message, $trace, $serializable); + } + + /** + * @param string $message + * @param array $serializable + */ + public function debug(string $message, array $serializable = []): void { + $message = '[debug] ' . $message; + $debugLevel = (int)$this->appConfig('debug_level'); + $this->log($debugLevel, $message, ($this->appConfig('debug_trace') === '1'), $serializable); + } + + + /** + * @param int $level + * @param string $message + * @param bool $trace + * @param array $serializable + */ + public function log(int $level, string $message, bool $trace = false, array $serializable = []): void { + $opts = ['app' => $this->setup('app')]; + if ($trace) { + $opts['exception'] = new HintException($message, json_encode($serializable)); + } elseif (!empty($serializable)) { + $message .= ' -- ' . json_encode($serializable); + } + + $this->logger() + ->log($level, $message, $opts); + } + + + /** + * @return LoggerInterface + */ + public function logger(): LoggerInterface { + if (isset($this->logger) && $this->logger instanceof LoggerInterface) { + return $this->logger; + } else { + return Server::get(LoggerInterface::class); + } + } +} diff --git a/lib/Tools/Traits/TNCRequest.php b/lib/Tools/Traits/TNCRequest.php new file mode 100644 index 00000000..68f28a9c --- /dev/null +++ b/lib/Tools/Traits/TNCRequest.php @@ -0,0 +1,340 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Traits; + +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; +use OCA\Social\Tools\Model\Request; + +/** + * Trait TNCRequest + * + * @package OCA\Social\Tools\Traits + */ +trait TNCRequest { + + + /** @var int */ + private $maxDownloadSize = 100; + + /** @var bool */ + private $maxDownloadSizeReached = false; + + + /** + * @param int $size + */ + public function setMaxDownloadSize(int $size) { + $this->maxDownloadSize = $size; + } + + + /** + * @param Request $request + * + * @return array + * @throws RequestContentException + * @throws RequestNetworkException + * @throws RequestResultNotJsonException + * @throws RequestResultSizeException + * @throws RequestServerException + */ + public function retrieveJson(Request $request): array { + $result = $this->doRequest($request); + + if (strpos($request->getContentType(), 'application/xrd') === 0) { + $xml = simplexml_load_string($result); + $result = json_encode($xml, JSON_UNESCAPED_SLASHES); + } + + $result = json_decode((string)$result, true); + if (is_array($result)) { + return $result; + } + + throw new RequestResultNotJsonException(); + } + + + /** + * @throws RequestContentException + * @throws RequestNetworkException + * @throws RequestResultSizeException + * @throws RequestServerException + */ + public function doRequest(Request $request): string { + $this->maxDownloadSizeReached = false; + + $ignoreProtocolOnErrors = [7]; + $result = ''; + foreach ($request->getProtocols() as $protocol) { + $request->setUsedProtocol($protocol); + + $curl = $this->initRequest($request); + + $this->initRequestGet($request); + $this->initRequestPost($curl, $request); + $this->initRequestPut($curl, $request); + $this->initRequestDelete($curl, $request); + + $this->initRequestHeaders($curl, $request); + + $result = curl_exec($curl); + + if (in_array(curl_errno($curl), $ignoreProtocolOnErrors)) { + continue; + } + + if ($this->maxDownloadSizeReached === true) { + throw new RequestResultSizeException(); + } + + $this->parseRequestResult($curl, $request); + break; + } + + return $result; + } + + + /** + * @param Request $request + * + * @return resource + */ + private function initRequest(Request $request) { + $curl = $this->generateCurlRequest($request); + + curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $request->getTimeout()); + curl_setopt($curl, CURLOPT_TIMEOUT, $request->getTimeout()); + + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_BINARYTRANSFER, $request->isBinary()); + + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $request->isVerifyPeer()); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $request->isFollowLocation()); + + curl_setopt($curl, CURLOPT_BUFFERSIZE, 128); + curl_setopt($curl, CURLOPT_NOPROGRESS, false); + curl_setopt( + /** + * @param $downloadSize + * @param int $downloaded + * @param $uploadSize + * @param int $uploaded + * + * @return int + */ + $curl, CURLOPT_PROGRESSFUNCTION, + function ($downloadSize, int $downloaded, $uploadSize, int $uploaded) { + if ($downloaded > ($this->maxDownloadSize * (1024 * 1024))) { + $this->maxDownloadSizeReached = true; + + return 1; + } + + return 0; + } + ); + + return $curl; + } + + + /** + * @param Request $request + * + * @return resource + */ + private function generateCurlRequest(Request $request) { + $url = $request->getUsedProtocol() . '://' . $request->getHost() . $request->getParsedUrl(); + if ($request->getType() !== Request::TYPE_GET) { + $curl = curl_init($url); + } else { + $curl = curl_init($url . '?' . $request->getUrlData()); + } + + return $curl; + } + + + /** + * @param Request $request + */ + private function initRequestGet(Request $request) { + if ($request->getType() !== Request::TYPE_GET) { + return; + } + } + + + /** + * @param resource $curl + * @param Request $request + */ + private function initRequestPost($curl, Request $request) { + if ($request->getType() !== Request::TYPE_POST) { + return; + } + + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody()); + } + + + /** + * @param resource $curl + * @param Request $request + */ + private function initRequestPut($curl, Request $request) { + if ($request->getType() !== Request::TYPE_PUT) { + return; + } + + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); + curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody()); + } + + + /** + * @param resource $curl + * @param Request $request + */ + private function initRequestDelete($curl, Request $request) { + if ($request->getType() !== Request::TYPE_DELETE) { + return; + } + + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); + curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody()); + } + + + /** + * @param resource $curl + * @param Request $request + */ + private function initRequestHeaders($curl, Request $request) { + $headers = $request->getHeaders(); + + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + } + + + /** + * @param resource $curl + * @param Request $request + * + * @throws RequestContentException + * @throws RequestServerException + * @throws RequestNetworkException + */ + private function parseRequestResult($curl, Request $request) { + $this->parseRequestResultCurl($curl, $request); + + $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); + $request->setContentType((!is_string($contentType)) ? '' : $contentType); + $request->setResultCode($code); + + $this->parseRequestResultCode301($code, $request); + $this->parseRequestResultCode4xx($code, $request); + $this->parseRequestResultCode5xx($code, $request); + } + + + /** + * @param resource $curl + * @param Request $request + * + * @throws RequestNetworkException + */ + private function parseRequestResultCurl($curl, Request $request) { + $errno = curl_errno($curl); + if ($errno > 0) { + throw new RequestNetworkException( + $errno . ' - ' . curl_error($curl) . ' - ' . json_encode( + $request, JSON_UNESCAPED_SLASHES + ), $errno + ); + } + } + + + /** + * @param int $code + * @param Request $request + * + * @throws RequestContentException + */ + private function parseRequestResultCode301(int $code, Request $request) { + if ($code === 301) { + throw new RequestContentException( + '301 - ' . json_encode($request, JSON_UNESCAPED_SLASHES) + ); + } + } + + + /** + * @param int $code + * @param Request $request + * + * @throws RequestContentException + */ + private function parseRequestResultCode4xx(int $code, Request $request) { + if ($code === 404 || $code === 410) { + throw new RequestContentException( + $code . ' - ' . json_encode($request, JSON_UNESCAPED_SLASHES) + ); + } + } + + + /** + * @param int $code + * @param Request $request + * + * @throws RequestServerException + */ + private function parseRequestResultCode5xx(int $code, Request $request) { + if ($code === 500) { + throw new RequestServerException( + $code . ' - ' . json_encode($request, JSON_UNESCAPED_SLASHES) + ); + } + } +} diff --git a/lib/Tools/Traits/TNCSetup.php b/lib/Tools/Traits/TNCSetup.php new file mode 100644 index 00000000..22c62c1b --- /dev/null +++ b/lib/Tools/Traits/TNCSetup.php @@ -0,0 +1,78 @@ + + * @copyright 2020, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Traits; + +use OCP\IConfig; +use OCP\Server; + +/** + * Trait TNCSetup + */ +trait TNCSetup { + use TArrayTools; + + private array $_setup = []; + + public function setup(string $key, string $value = '', string $default = ''): string { + if ($value !== '') { + $this->_setup[$key] = $value; + } + + return $this->get($key, $this->_setup, $default); + } + + public function setupArray(string $key, array $value = [], array $default = []): array { + if (!empty($value)) { + $this->_setup[$key] = $value; + } + + return $this->getArray($key, $this->_setup, $default); + } + + public function setupInt(string $key, int $value = -999, int $default = 0): int { + if ($value !== -999) { + $this->_setup[$key] = $value; + } + + return $this->getInt($key, $this->_setup, $default); + } + + public function appConfig(string $key): string { + $app = $this->setup('app'); + if ($app === '') { + return ''; + } + + $config = Server::get(IConfig::class); + return $config->getAppValue($app, $key, ''); + } +} diff --git a/lib/Tools/Traits/TPathTools.php b/lib/Tools/Traits/TPathTools.php new file mode 100644 index 00000000..4ac7c10f --- /dev/null +++ b/lib/Tools/Traits/TPathTools.php @@ -0,0 +1,125 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Traits; + +/** + * Trait TPathTools + * + * @deprecated - 19 + * @package OCA\Social\Tools\Traits + */ +trait TPathTools { + + /** + * @param string $path + * + * @return string + */ + protected function withEndSlash(string $path): string { + $path .= '/'; + $path = str_replace('//', '/', $path); + + return trim($path); + } + + + /** + * @param string $path + * @param bool $force + * @param bool $clean + * + * @return string + */ + protected function withoutEndSlash(string $path, bool $force = false, bool $clean = true + ): string { + if ($clean) { + $path = str_replace('//', '/', $path); + } + + if ($path === '/' && !$force) { + return $path; + } + + $path = rtrim($path, '/'); + + return trim($path); + } + + + /** + * @param string $path + * + * @return string + */ + protected function withBeginSlash(string $path): string { + $path = '/' . $path; + $path = str_replace('//', '/', $path); + + return trim($path); + } + + + /** + * @param string $path + * @param bool $force + * @param bool $clean + * + * @return string + */ + protected function withoutBeginSlash(string $path, bool $force = false, bool $clean = true) { + if ($clean) { + $path = str_replace('//', '/', $path); + } + + if ($path === '/' && !$force) { + return $path; + } + + $path = ltrim($path, '/'); + + return trim($path); + } + + + /** + * @param string $path + * @param bool $force + * @param bool $clean + * + * @return string + */ + protected function withoutBeginAt(string $path) { + $path = ltrim($path, '@'); + + return trim($path); + } +} diff --git a/lib/Tools/Traits/TRequest.php b/lib/Tools/Traits/TRequest.php new file mode 100644 index 00000000..5694420b --- /dev/null +++ b/lib/Tools/Traits/TRequest.php @@ -0,0 +1,330 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Traits; + +use OCA\Social\Tools\Exceptions\RequestContentException; +use OCA\Social\Tools\Exceptions\RequestNetworkException; +use OCA\Social\Tools\Exceptions\RequestResultNotJsonException; +use OCA\Social\Tools\Exceptions\RequestResultSizeException; +use OCA\Social\Tools\Exceptions\RequestServerException; +use OCA\Social\Tools\Model\Request; + +/** + * Trait TRequest + * + * @package OCA\Social\Tools\Traits + */ +trait TRequest { + private int $maxDownloadSize = 100; + private bool $maxDownloadSizeReached = false; + + public function setMaxDownloadSize(int $size) { + $this->maxDownloadSize = $size; + } + + + /** + * @param Request $request + * + * @return array + * @throws RequestContentException + * @throws RequestNetworkException + * @throws RequestResultNotJsonException + * @throws RequestResultSizeException + * @throws RequestServerException + */ + public function retrieveJson(Request $request): array { + $result = $this->doRequest($request); + + if (strpos($request->getContentType(), 'application/xrd') === 0) { + $xml = simplexml_load_string($result); + $result = json_encode($xml, JSON_UNESCAPED_SLASHES); + } + + $result = json_decode((string)$result, true); + if (is_array($result)) { + return $result; + } + + throw new RequestResultNotJsonException(); + } + + + /** + * @param Request $request + * + * @return mixed + * @throws RequestContentException + * @throws RequestNetworkException + * @throws RequestResultSizeException + * @throws RequestServerException + */ + public function doRequest(Request $request) { + $this->maxDownloadSizeReached = false; + + $ignoreProtocolOnErrors = [7]; + $result = ''; + foreach ($request->getProtocols() as $protocol) { + $request->setUsedProtocol($protocol); + + $curl = $this->initRequest($request); + + $this->initRequestGet($request); + $this->initRequestPost($curl, $request); + $this->initRequestPut($curl, $request); + $this->initRequestDelete($curl, $request); + + $this->initRequestHeaders($curl, $request); + + $result = curl_exec($curl); + + if (in_array(curl_errno($curl), $ignoreProtocolOnErrors)) { + continue; + } + + if ($this->maxDownloadSizeReached === true) { + throw new RequestResultSizeException(); + } + + $this->parseRequestResult($curl, $request); + break; + } + + return $result; + } + + + /** + * @param Request $request + * + * @return resource + */ + private function initRequest(Request $request) { + $curl = $this->generateCurlRequest($request); + + curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $request->getTimeout()); + curl_setopt($curl, CURLOPT_TIMEOUT, $request->getTimeout()); + + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_BINARYTRANSFER, $request->isBinary()); + + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $request->isVerifyPeer()); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $request->isFollowLocation()); + + curl_setopt($curl, CURLOPT_BUFFERSIZE, 128); + curl_setopt($curl, CURLOPT_NOPROGRESS, false); + curl_setopt( + /** + * @param $downloadSize + * @param int $downloaded + * @param $uploadSize + * @param int $uploaded + * + * @return int + */ + $curl, CURLOPT_PROGRESSFUNCTION, + function ($downloadSize, int $downloaded, $uploadSize, int $uploaded) { + if ($downloaded > ($this->maxDownloadSize * (1024 * 1024))) { + $this->maxDownloadSizeReached = true; + + return 1; + } + + return 0; + } + ); + + return $curl; + } + + + /** + * @param Request $request + * + * @return resource + */ + private function generateCurlRequest(Request $request) { + $port = ($request->getPort() > 0) ? ':' . $request->getPort() : ''; + $url = + $request->getUsedProtocol() . '://' . $request->getHost() . $port . $request->getParsedUrl(); + if ($request->getType() !== Request::TYPE_GET) { + $curl = curl_init($url); + } else { + $curl = curl_init($url . '?' . $request->getUrlData()); + } + + return $curl; + } + + + /** + * @param Request $request + */ + private function initRequestGet(Request $request) { + if ($request->getType() !== Request::TYPE_GET) { + return; + } + } + + + /** + * @param resource $curl + * @param Request $request + */ + private function initRequestPost($curl, Request $request) { + if ($request->getType() !== Request::TYPE_POST) { + return; + } + + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody()); + } + + /** + * @param resource $curl + */ + private function initRequestPut($curl, Request $request) { + if ($request->getType() !== Request::TYPE_PUT) { + return; + } + + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); + curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody()); + } + + /** + * @param resource $curl + */ + private function initRequestDelete($curl, Request $request) { + if ($request->getType() !== Request::TYPE_DELETE) { + return; + } + + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); + curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody()); + } + + /** + * @param resource $curl + */ + private function initRequestHeaders($curl, Request $request) { + $headers = $request->getHeaders(); + + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + } + + + /** + * @param resource $curl + * @param Request $request + * + * @throws RequestContentException + * @throws RequestServerException + * @throws RequestNetworkException + */ + private function parseRequestResult($curl, Request $request) { + $this->parseRequestResultCurl($curl, $request); + + $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); + $request->setContentType((!is_string($contentType)) ? '' : $contentType); + $request->setResultCode($code); + + $this->parseRequestResultCode301($code, $request); + $this->parseRequestResultCode4xx($code, $request); + $this->parseRequestResultCode5xx($code, $request); + } + + + /** + * @param resource $curl + * @param Request $request + * + * @throws RequestNetworkException + */ + private function parseRequestResultCurl($curl, Request $request) { + $errno = curl_errno($curl); + if ($errno > 0) { + throw new RequestNetworkException( + $errno . ' - ' . curl_error($curl) . ' - ' . json_encode( + $request, JSON_UNESCAPED_SLASHES + ), $errno + ); + } + } + + + /** + * @param int $code + * @param Request $request + * + * @throws RequestContentException + */ + private function parseRequestResultCode301(int $code, Request $request) { + if ($code === 301) { + throw new RequestContentException( + '301 - ' . json_encode($request, JSON_UNESCAPED_SLASHES) + ); + } + } + + + /** + * @param int $code + * @param Request $request + * + * @throws RequestContentException + */ + private function parseRequestResultCode4xx(int $code, Request $request) { + if ($code === 404 || $code === 410) { + throw new RequestContentException( + $code . ' - ' . json_encode($request, JSON_UNESCAPED_SLASHES) + ); + } + } + + + /** + * @param int $code + * @param Request $request + * + * @throws RequestServerException + */ + private function parseRequestResultCode5xx(int $code, Request $request) { + if ($code === 500) { + throw new RequestServerException( + $code . ' - ' . json_encode($request, JSON_UNESCAPED_SLASHES) + ); + } + } +} diff --git a/lib/Tools/Traits/TStringTools.php b/lib/Tools/Traits/TStringTools.php new file mode 100644 index 00000000..cb5514cc --- /dev/null +++ b/lib/Tools/Traits/TStringTools.php @@ -0,0 +1,165 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Tools\Traits; + +use Exception; + +/** + * Trait TStringTools + * + * @package OCA\Social\Tools\Traits + */ +trait TStringTools { + + + /** + * @param int $length + * + * @return string + */ + protected function token(int $length = 15): string { + $chars = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890'; + + $str = ''; + $max = strlen($chars); + for ($i = 0; $i < $length; $i++) { + try { + $str .= $chars[random_int(0, $max - 2)]; + } catch (Exception $e) { + } + } + + return $str; + } + + + /** + * Generate uuid: 2b5a7a87-8db1-445f-a17b-405790f91c80 + * + * @param int $length + * + * @return string + */ + protected function uuid(int $length = 0): string { + $uuid = sprintf( + '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), + mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) + ); + + if ($length > 0) { + if ($length <= 16) { + $uuid = str_replace('-', '', $uuid); + } + + $uuid = substr($uuid, 0, $length); + } + + return $uuid; + } + + + /** + * @param string $str1 + * @param string $str2 + * @param bool $cs case sensitive ? + * + * @return string + */ + protected function commonPart(string $str1, string $str2, bool $cs = true): string { + for ($i = 0; $i < strlen($str1) && $i < strlen($str2); $i++) { + $chr1 = $str1[$i]; + $chr2 = $str2[$i]; + + if (!$cs) { + $chr1 = strtolower($chr1); + $chr2 = strtolower($chr2); + } + + if ($chr1 !== $chr2) { + break; + } + } + + return substr($str1, 0, $i); + } + + + /** + * @param string $line + * @param array $params + * + * @return string + */ + protected function feedStringWithParams(string $line, array $params): string { + $ak = array_keys($params); + foreach ($ak as $k) { + $line = str_replace('{' . $k . '}', $params[$k], $line); + } + + return $line; + } + + + /** + * @param int $words + * + * @return string + */ + public function generateRandomSentence(int $words = 5): string { + $sentence = []; + for ($i = 0; $i < $words; $i++) { + $sentence[] = $this->generateRandomWord(rand(2, 12)); + } + + return implode(' ', $sentence); + } + + + /** + * @param int $length + * + * @return string + */ + public function generateRandomWord(int $length = 8): string { + $c = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v']; + $v = ['a', 'e', 'i', 'o', 'u', 'y']; + + $word = []; + for ($i = 0; $i <= ($length / 2); $i++) { + $word[] = $c[array_rand($c)]; + $word[] = $v[array_rand($v)]; + } + + return implode('', $word); + } +} diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index a64f984a..93b22f91 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -5,11 +5,6 @@ $level - - - $schema - - DBALException @@ -58,12 +53,10 @@ - + Person - string - - $this->accountPrim + $this->contactAccount @@ -138,7 +131,7 @@ $varr[0] !== null - IFactory + IAppDataFactory @@ -153,4 +146,9 @@ $username === null + + + HintException + +