summaryrefslogtreecommitdiffstats
path: root/lib/Service/SignatureService.php
diff options
context:
space:
mode:
authorvioloncello.ch <22591354+violoncelloCH@users.noreply.github.com>2019-02-27 17:33:46 +0100
committerGitHub <noreply@github.com>2019-02-27 17:33:46 +0100
commit50af205239ef3d66de06b76b98f8b3bdbf0c844c (patch)
tree27885bda6a4758b3653be68a0152974bfdc6cbea /lib/Service/SignatureService.php
parentcbdb8a3bda1770a924e6a3629a8bf42302a2075d (diff)
parent07543b01b8d81c65c93e3802d6c8ff89f6a6aa4d (diff)
Merge pull request #444 from nextcloud/backport/433/alpha1alpha1
[alpha1] create object even if it can't be signed
Diffstat (limited to 'lib/Service/SignatureService.php')
-rw-r--r--lib/Service/SignatureService.php119
1 files changed, 117 insertions, 2 deletions
diff --git a/lib/Service/SignatureService.php b/lib/Service/SignatureService.php
index 45da8ad4..fff546f6 100644
--- a/lib/Service/SignatureService.php
+++ b/lib/Service/SignatureService.php
@@ -35,10 +35,13 @@ use daita\MySmallPhpTools\Model\Request;
use daita\MySmallPhpTools\Traits\TArrayTools;
use DateTime;
use Exception;
+use JsonLdException;
+use OCA\Social\AppInfo\Application;
use OCA\Social\Db\ActorsRequest;
use OCA\Social\Exceptions\ActorDoesNotExistException;
use OCA\Social\Exceptions\InvalidOriginException;
use OCA\Social\Exceptions\InvalidResourceException;
+use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Exceptions\LinkedDataSignatureMissingException;
use OCA\Social\Exceptions\RedundancyLimitException;
use OCA\Social\Exceptions\RequestContentException;
@@ -48,12 +51,16 @@ use OCA\Social\Exceptions\RequestServerException;
use OCA\Social\Exceptions\SignatureException;
use OCA\Social\Exceptions\SignatureIsGoneException;
use OCA\Social\Exceptions\SocialAppConfigException;
-use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Model\LinkedDataSignature;
use OCA\Social\Model\RequestQueue;
+use OCP\Files\NotFoundException;
+use OCP\Files\NotPermittedException;
+use OCP\Files\SimpleFS\ISimpleFile;
+use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IRequest;
+use stdClass;
class SignatureService {
@@ -256,7 +263,7 @@ class SignatureService {
try {
$signature->sign();
$object->setSignature($signature);
- } catch (LinkedDataSignatureMissingException $e) {
+ } catch (Exception $e) {
}
}
@@ -412,5 +419,113 @@ class SignatureService {
}
}
+
+ /**
+ * @param string $url
+ *
+ * @return stdClass
+ * @throws NotPermittedException
+ * @throws JsonLdException
+ */
+ public static function documentLoader($url): stdClass {
+ $recursion = 0;
+ $x = debug_backtrace();
+ if ($x) {
+ foreach ($x as $n) {
+ if ($n['function'] === __FUNCTION__) {
+ $recursion++;
+ }
+ }
+ }
+
+ if ($recursion > 5) {
+ exit();
+ }
+
+ $folder = self::getContextCacheFolder();
+ $filename = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
+ $filename = str_replace('/', '.', $filename) . '.json';
+
+ try {
+ $cache = $folder->getFile($filename);
+ self::updateContextCacheDocument($cache, $url);
+
+ $data = json_decode($cache->getContent());
+ } catch (NotFoundException $e) {
+ $data = self::generateContextCacheDocument($folder, $filename, $url);
+ }
+
+ return $data;
+ }
+
+
+ /**
+ * @return ISimpleFolder
+ * @throws NotPermittedException
+ */
+ private static function getContextCacheFolder(): ISimpleFolder {
+ $path = 'context';
+
+ $appData = \OC::$server->getAppDataDir(Application::APP_NAME);
+ try {
+ $folder = $appData->getFolder($path);
+ } catch (NotFoundException $e) {
+ $folder = $appData->newFolder($path);
+ }
+
+ return $folder;
+ }
+
+
+ /**
+ * @param ISimpleFolder $folder
+ * @param string $filename
+ *
+ * @param string $url
+ *
+ * @return stdClass
+ * @throws JsonLdException
+ * @throws NotPermittedException
+ */
+ private static function generateContextCacheDocument(
+ ISimpleFolder $folder, string $filename, string $url
+ ): stdClass {
+
+ try {
+ $data = jsonld_default_document_loader($url);
+ $content = json_encode($data);
+ } catch (JsonLdException $e) {
+ $context = file_get_contents(__DIR__ . '/../../context/' . $filename);
+ if (is_bool($context)) {
+ throw $e;
+ }
+
+ $content = $context;
+ $data = json_decode($context);
+ }
+
+ $cache = $folder->newFile($filename);
+ $cache->putContent($content);
+
+ return $data;
+ }
+
+
+ /**
+ * @param ISimpleFile $cache
+ * @param string $url
+ *
+ * @throws NotPermittedException
+ */
+ private static function updateContextCacheDocument(ISimpleFile $cache, string $url) {
+ if ($cache->getMTime() < (time() - 98765)) {
+ try {
+ $data = jsonld_default_document_loader($url);
+ $cache->putContent(json_encode($data));
+ } catch (JsonLdException $e) {
+ }
+ }
+ }
+
}