-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from humhub/enh/39-well-known-files
Add possibility to profile `.well-known` files
- Loading branch information
Showing
7 changed files
with
197 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\fcmPush\components; | ||
|
||
use humhub\modules\fcmPush\services\WellKnownService; | ||
use yii\base\Component; | ||
use yii\web\UrlRuleInterface; | ||
|
||
class UrlRule extends Component implements UrlRuleInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function createUrl($manager, $route, $params) | ||
{ | ||
if ($route === trim(WellKnownService::URL_ROUTE, '/') && isset($params['file'])) { | ||
return WellKnownService::URL_PREFIX . $params['file']; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function parseRequest($manager, $request) | ||
{ | ||
$path = $request->getPathInfo(); | ||
if (str_starts_with($path, WellKnownService::URL_PREFIX)) { | ||
return WellKnownService::instance($path)->getRuleRoute() ?? false; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\fcmPush\controllers; | ||
|
||
use humhub\components\Controller; | ||
use humhub\modules\fcmPush\services\WellKnownService; | ||
|
||
class WellKnownController extends Controller | ||
{ | ||
public function actionIndex(string $file) | ||
{ | ||
return WellKnownService::instance($file)->renderFile(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\fcmPush\services; | ||
|
||
use Exception; | ||
use humhub\modules\fcmPush\Module; | ||
use Yii; | ||
use yii\helpers\Json; | ||
use yii\web\Response; | ||
|
||
class WellKnownService | ||
{ | ||
public const URL_ROUTE = '/fcm-push/well-known'; | ||
public const URL_PREFIX = '.well-known/'; | ||
public const ALLOWED_FILES = [ | ||
'fileAssetLinks' => 'assetlinks.json', | ||
'fileAppleAssociation' => 'apple-app-site-association', | ||
]; | ||
|
||
private ?string $file; | ||
|
||
public function __construct(string $path) | ||
{ | ||
$this->file = preg_replace('#^' . preg_quote(self::URL_PREFIX) . '#', '', $path); | ||
} | ||
|
||
public static function instance(string $path): self | ||
{ | ||
return new self($path); | ||
} | ||
|
||
public static function getFileName(string $settingName): string | ||
{ | ||
return self::ALLOWED_FILES[$settingName] ?? ''; | ||
} | ||
|
||
public static function getFileRoute(string $settingName): array | ||
{ | ||
return [self::URL_ROUTE, 'file' => self::getFileName($settingName)]; | ||
} | ||
|
||
public function isAllowed(): bool | ||
{ | ||
return in_array($this->file, self::ALLOWED_FILES); | ||
} | ||
|
||
public function getRuleRoute(): ?array | ||
{ | ||
return $this->isAllowed() ? [self::URL_ROUTE, ['file' => $this->file]] : null; | ||
} | ||
|
||
public function getFileContent(): string | ||
{ | ||
$settingName = array_search($this->file, self::ALLOWED_FILES); | ||
if ($settingName === false) { | ||
return ''; | ||
} | ||
|
||
/* @var Module $module */ | ||
$module = Yii::$app->getModule('fcm-push'); | ||
|
||
return $module->settings->get($settingName, ''); | ||
} | ||
|
||
public function renderFile(): Response | ||
{ | ||
Yii::$app->response->format = Response::FORMAT_JSON; | ||
|
||
try { | ||
Yii::$app->response->data = Json::decode($this->getFileContent()); | ||
} catch (Exception $ex) { | ||
Yii::$app->response->data = ''; | ||
Yii::error('Wrong file format "' . $this->file . '". Error: ' . $ex->getMessage(), 'fcm-push'); | ||
} | ||
|
||
return Yii::$app->response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters