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