[FEATURE] Add Dispatcher for Backend Routing
[Packages/TYPO3.CMS.git] / typo3 / sysext / openid / Classes / Wizard.php
1 <?php
2 namespace TYPO3\CMS\Openid;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use Psr\Http\Message\ServerRequestInterface;
17 use Psr\Http\Message\ResponseInterface;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Messaging\FlashMessage;
20 use TYPO3\CMS\Core\Messaging\FlashMessageService;
21 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Fluid\View\StandaloneView;
24 use TYPO3\CMS\Lang\LanguageService;
25
26 /**
27 * OpenID selection wizard for the backend
28 */
29 class Wizard extends OpenidService {
30
31 /**
32 * OpenID of the user after authentication
33 *
34 * @var string
35 */
36 protected $claimedId;
37
38 /**
39 * Name of the form element this wizard should write the OpenID into
40 *
41 * @var string
42 */
43 protected $parentFormItemName;
44
45 /**
46 * Name of the function that needs to be called after setting the value
47 *
48 * @var string
49 */
50 protected $parentFormFieldChangeFunc;
51
52 /**
53 * Injects the request object for the current request or subrequest
54 * Process the wizard and render HTML to response
55 *
56 * @param ServerRequestInterface $request the current request
57 * @param ResponseInterface $response
58 * @return ResponseInterface the response with the content
59 */
60 public function mainAction(ServerRequestInterface $request, ResponseInterface $response) {
61 $this->processWizard();
62 $content = $this->renderContent();
63
64 $response->getBody()->write($content);
65 return $response;
66 }
67
68 /**
69 * Run the wizard and output HTML.
70 *
71 * @return void
72 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use mainAction() instead
73 */
74 public function main() {
75 GeneralUtility::logDeprecatedFunction();
76 $this->processWizard();
77 $this->renderHtml();
78 }
79
80 /**
81 * Run the wizard
82 */
83 protected function processWizard() {
84 $p = GeneralUtility::_GP('P');
85 if (isset($p['itemName'])) {
86 $this->parentFormItemName = $p['itemName'];
87 }
88 if (isset($p['fieldChangeFunc']['TBE_EDITOR_fieldChanged'])) {
89 $this->parentFormFieldChangeFunc = $p['fieldChangeFunc']['TBE_EDITOR_fieldChanged'];
90 }
91
92 if (GeneralUtility::_GP('tx_openid_mode') === 'finish' && $this->openIDResponse === NULL) {
93 $this->includePHPOpenIDLibrary();
94 $openIdConsumer = $this->getOpenIDConsumer();
95 $this->openIDResponse = $openIdConsumer->complete($this->getReturnUrl());
96 $this->handleResponse();
97 } elseif (GeneralUtility::_POST('openid_url') != '') {
98 $openIDIdentifier = GeneralUtility::_POST('openid_url');
99 $this->sendOpenIDRequest($openIDIdentifier);
100
101 // When sendOpenIDRequest() returns, there was an error
102 $flashMessageService = GeneralUtility::makeInstance(
103 FlashMessageService::class
104 );
105 $flashMessage = GeneralUtility::makeInstance(
106 FlashMessage::class,
107 sprintf(
108 $this->getLanguageService()->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:error.setup'),
109 htmlspecialchars($openIDIdentifier)
110 ),
111 $this->getLanguageService()->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:title.error'),
112 FlashMessage::ERROR
113 );
114 $flashMessageService->getMessageQueueByIdentifier()->enqueue($flashMessage);
115 }
116 }
117
118 /**
119 * Return URL that shall be called by the OpenID server
120 *
121 * @return string Full URL with protocol and hostname
122 */
123 protected function getReturnUrl() {
124 $parameters = [
125 'tx_openid_mode' => 'finish',
126 'P[itemName]' => $this->parentFormItemName,
127 'P[fieldChangeFunc][TBE_EDITOR_fieldChanged]' => $this->parentFormFieldChangeFunc
128 ];
129 return BackendUtility::getModuleUrl('wizard_openid', $parameters, FALSE, TRUE);
130 }
131
132 /**
133 * Check OpenID response and set flash messages depending on its state
134 *
135 * @return void
136 */
137 protected function handleResponse() {
138 /** @var $flashMessageService FlashMessageService */
139 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
140 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
141
142 $lang = $this->getLanguageService();
143 if (!$this->openIDResponse instanceof \Auth_OpenID_ConsumerResponse) {
144 $flashMessage = GeneralUtility::makeInstance(
145 FlashMessage::class,
146 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:error.no-response'),
147 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:title.error'),
148 FlashMessage::ERROR
149 );
150 } elseif ($this->openIDResponse->status == Auth_OpenID_SUCCESS) {
151 // all fine
152 $this->claimedId = $this->getSignedParameter('openid_claimed_id');
153 $flashMessage = GeneralUtility::makeInstance(
154 FlashMessage::class,
155 sprintf(
156 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:youropenid'),
157 htmlspecialchars($this->claimedId)
158 ),
159 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:title.success'),
160 FlashMessage::OK
161 );
162 } elseif ($this->openIDResponse->status == Auth_OpenID_CANCEL) {
163 $flashMessage = GeneralUtility::makeInstance(
164 FlashMessage::class,
165 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:error.cancelled'),
166 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:title.error'),
167 FlashMessage::ERROR
168 );
169 } else {
170 // another failure. show error message and form again
171 $flashMessage = GeneralUtility::makeInstance(
172 FlashMessage::class,
173 sprintf(
174 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:error.general'),
175 htmlspecialchars($this->openIDResponse->status),
176 ''
177 ),
178 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:title.error'),
179 FlashMessage::ERROR
180 );
181 }
182
183 $defaultFlashMessageQueue->enqueue($flashMessage);
184 }
185
186 /**
187 * Render HTML with message and OpenID form
188 *
189 * @return string
190 */
191 protected function renderContent() {
192 // use FLUID standalone view for wizard content
193 $view = GeneralUtility::makeInstance(StandaloneView::class);
194 $view->getRequest()->setControllerExtensionName('openid');
195 $view->setTemplatePathAndFilename(
196 ExtensionManagementUtility::extPath('openid') .
197 'Resources/Private/Templates/Wizard/Content.html'
198 );
199
200 /** @var $flashMessageService FlashMessageService */
201 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
202 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
203
204 $messages = array();
205 foreach ($defaultFlashMessageQueue->getAllMessagesAndFlush() as $message) {
206 $messages[] = $message->render();
207 }
208 $view->assign('messages', $messages);
209 $view->assign('formAction', BackendUtility::getModuleUrl('wizard_openid', [], FALSE, TRUE));
210 $view->assign('claimedId', $this->claimedId);
211 $view->assign('parentFormItemName', $this->parentFormItemName);
212 $view->assign('parentFormItemNameNoHr', strtr($this->parentFormItemName, array('_hr' => '')));
213 $view->assign('parentFormFieldChangeFunc', $this->parentFormFieldChangeFunc);
214 $view->assign('showForm', TRUE);
215 if (isset($_REQUEST['openid_url'])) {
216 $view->assign('openid_url', $_REQUEST['openid_url']);
217 }
218
219 return $view->render();
220 }
221
222 /**
223 * Render HTML and output it
224 *
225 * @return void
226 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use mainAction() instead
227 */
228 protected function renderHtml() {
229 GeneralUtility::logDeprecatedFunction();
230 header('HTTP/1.0 200 OK');
231 echo $this->renderContent();
232 }
233
234 /**
235 * @return LanguageService
236 */
237 protected function getLanguageService() {
238 return $GLOBALS['LANG'];
239 }
240
241 }