Move mod/viewsrc to src/Module/ItemBody
authorPhilipp Holzer <admin@philipp.info>
Sun, 5 May 2019 17:15:33 +0000 (19:15 +0200)
committerPhilipp Holzer <admin@philipp.info>
Sun, 5 May 2019 17:15:33 +0000 (19:15 +0200)
mod/viewsrc.php [deleted file]
src/App/Router.php
src/Module/ItemBody.php [new file with mode: 0644]

diff --git a/mod/viewsrc.php b/mod/viewsrc.php
deleted file mode 100644 (file)
index 55eb0b9..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-<?php
-/**
- * @file mod/viewsrc.php
- */
-use Friendica\App;
-use Friendica\Core\L10n;
-use Friendica\Database\DBA;
-use Friendica\Model\Item;
-
-function viewsrc_content(App $a)
-{
-       if (!local_user()) {
-               notice(L10n::t('Access denied.') . EOL);
-               return;
-       }
-
-       $o = '';
-       $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
-
-       if (!$item_id) {
-               throw new \Friendica\Network\HTTPException\NotFoundException(L10n::t('Item not found.'));
-       }
-
-       $item = Item::selectFirst(['body'], ['uid' => local_user(), 'id' => $item_id]);
-
-       if (DBA::isResult($item)) {
-               if ($a->isAjax()) {
-                       echo str_replace("\n", '<br />', $item['body']);
-                       exit();
-               } else {
-                       $o .= str_replace("\n", '<br />', $item['body']);
-               }
-       }
-       return $o;
-}
index 1dffb6b..f1d5c09 100644 (file)
@@ -167,6 +167,7 @@ class Router
                $this->routeCollector->addRoute(['GET'],         '/rsd.xml',             Module\ReallySimpleDiscovery::class);
                $this->routeCollector->addRoute(['GET'],         '/statistics.json',     Module\Statistics::class);
                $this->routeCollector->addRoute(['GET'],         '/tos',                 Module\Tos::class);
+               $this->routeCollector->addRoute(['GET'],         '/viewsrc/{item:\d+}',  Module\ItemBody::class);
                $this->routeCollector->addRoute(['GET'],         '/webfinger',           Module\WebFinger::class);
                $this->routeCollector->addRoute(['GET'],         '/xrd',                 Module\Xrd::class);
        }
diff --git a/src/Module/ItemBody.php b/src/Module/ItemBody.php
new file mode 100644 (file)
index 0000000..ee50b52
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+namespace Friendica\Module;
+
+use Friendica\BaseModule;
+use Friendica\Core\L10n;
+use Friendica\Model\Item;
+use Friendica\Network\HTTPException;
+
+/**
+ * Print the body of an Item
+ */
+class ItemBody extends BaseModule
+{
+       public static function content()
+       {
+               if (!local_user()) {
+                       throw new HTTPException\UnauthorizedException(L10n::t('Access denied.'));
+               }
+
+               $app = self::getApp();
+
+               // @TODO: Replace with parameter from router
+               $itemId = (($app->argc > 1) ? intval($app->argv[1]) : 0);
+
+               if (!$itemId) {
+                       throw new HTTPException\NotFoundException(L10n::t('Item not found.'));
+               }
+
+               $item = Item::selectFirst(['body'], ['uid' => local_user(), 'id' => $itemId]);
+
+               if (!empty($item)) {
+                       if ($app->isAjax()) {
+                               echo str_replace("\n", '<br />', $item['body']);
+                               exit();
+                       } else {
+                               return str_replace("\n", '<br />', $item['body']);
+                       }
+               } else {
+                       throw new HTTPException\NotFoundException(L10n::t('Item not found.'));
+               }
+       }
+}