|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DocuSign\Controllers; |
| 4 | + |
| 5 | +use DocuSign\Services\SignatureClientService; |
| 6 | +use DocuSign\Services\RouterService; |
| 7 | +use DocuSign\Services\IRouterService; |
| 8 | +use DocuSign\Services\ApiTypes; |
| 9 | +use DocuSign\Services\ManifestService; |
| 10 | + |
| 11 | +abstract class NotaryApiBaseController extends BaseController |
| 12 | +{ |
| 13 | + private const MINIMUM_BUFFER_MIN = 3; |
| 14 | + protected SignatureClientService $clientService; |
| 15 | + protected IRouterService $routerService; |
| 16 | + protected array $args; |
| 17 | + |
| 18 | + public function __construct() |
| 19 | + { |
| 20 | + $this->args = $this->getTemplateArgs(); |
| 21 | + $this->clientService = new SignatureClientService($this->args); |
| 22 | + $this->routerService = new RouterService(); |
| 23 | + if (defined("static::EG")) { |
| 24 | + $this->checkDsToken(); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + abstract protected function getTemplateArgs(): array; |
| 29 | + |
| 30 | + /** |
| 31 | + * Base controller |
| 32 | + * |
| 33 | + * @param null $eg |
| 34 | + * @return void |
| 35 | + */ |
| 36 | + public function controller($eg = null): void |
| 37 | + { |
| 38 | + if (empty($eg)) { |
| 39 | + $eg = static::EG; |
| 40 | + $this->codeExampleText = $this->getPageText(static::EG); |
| 41 | + } |
| 42 | + |
| 43 | + if ($this->isMethodGet()) { |
| 44 | + $this->getController( |
| 45 | + $eg, |
| 46 | + basename(static::FILE) |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + if ($this->isMethodPost()) { |
| 51 | + $this->createController(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Show the example's form page |
| 57 | + * |
| 58 | + * @param $eg |
| 59 | + * @param $basename string|null |
| 60 | + * @param $brand_languages array|null |
| 61 | + * @param $brands array|null |
| 62 | + * @param $permission_profiles array|null |
| 63 | + * @param $groups array|null |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + protected function getController( |
| 67 | + $eg, |
| 68 | + ?string $basename |
| 69 | + ): void { |
| 70 | + if ($this->isHomePage($eg)) { |
| 71 | + $GLOBALS['twig']->display( |
| 72 | + $eg . '.html', |
| 73 | + [ |
| 74 | + 'title' => $this->homePageTitle($eg), |
| 75 | + 'show_doc' => false, |
| 76 | + 'launcher_texts' => $_SESSION['API_TEXT']['APIs'], |
| 77 | + 'api_texts' => $_SESSION['API_TEXT'], |
| 78 | + 'common_texts' => $this->getCommonText(), |
| 79 | + ] |
| 80 | + ); |
| 81 | + } else { |
| 82 | + $currentAPI = ManifestService::getAPIByLink(static::EG); |
| 83 | + if ($this->routerService->dsTokenOk() && $currentAPI === $_SESSION['api_type']) { |
| 84 | + $displayOptions = [ |
| 85 | + 'title' => $this->routerService->getTitle($eg), |
| 86 | + 'source_file' => $basename, |
| 87 | + 'source_url' => $GLOBALS['DS_CONFIG']['github_example_url'] . "/Notary/". $basename, |
| 88 | + 'documentation' => $GLOBALS['DS_CONFIG']['documentation'] . $eg, |
| 89 | + 'show_doc' => $GLOBALS['DS_CONFIG']['documentation'], |
| 90 | + 'signer_name' => $GLOBALS['DS_CONFIG']['signer_name'], |
| 91 | + 'signer_email' => $GLOBALS['DS_CONFIG']['signer_email'], |
| 92 | + 'code_example_text' => $this->codeExampleText, |
| 93 | + 'common_texts' => $this->getCommonText() |
| 94 | + ]; |
| 95 | + |
| 96 | + $GLOBALS['twig']->display($this->routerService->getTemplate($eg), $displayOptions); |
| 97 | + } else { |
| 98 | + $_SESSION['prefered_api_type'] = ApiTypes::NOTARY; |
| 99 | + $this->saveCurrentUrlToSession($eg); |
| 100 | + header('Location: ' . $GLOBALS['app_url'] . 'index.php?page=' . static::LOGIN_REDIRECT); |
| 101 | + exit; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Declaration for the base controller creator. Each creator should be described in specific Controller |
| 108 | + */ |
| 109 | + abstract protected function createController(): void; |
| 110 | + |
| 111 | + /** |
| 112 | + * Check email input value using regular expression |
| 113 | + * @param $email |
| 114 | + * @return string |
| 115 | + */ |
| 116 | + protected function checkEmailInputValue($email): string |
| 117 | + { |
| 118 | + return preg_replace('/([^\w +\-\@\.\,])+/', '', $email); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Check input values using regular expressions |
| 123 | + * @param $value |
| 124 | + * @return string |
| 125 | + */ |
| 126 | + protected function checkInputValues($value): string |
| 127 | + { |
| 128 | + return preg_replace('/([^\w \-\@\.\,])+/', '', $value); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Check ds |
| 133 | + */ |
| 134 | + protected function checkDsToken(): void |
| 135 | + { |
| 136 | + $currentAPI = ManifestService::getAPIByLink(static::EG); |
| 137 | + |
| 138 | + if (!$this->routerService->dsTokenOk(self::MINIMUM_BUFFER_MIN) || $currentAPI !== $_SESSION['api_type']) { |
| 139 | + $_SESSION['prefered_api_type'] = ApiTypes::NOTARY; |
| 140 | + $this->clientService->needToReAuth(static::EG); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments