[BUGFIX] Fix T3editor after PSR-7 change
[Packages/TYPO3.CMS.git] / typo3 / sysext / openid / Classes / Wizard.php
1 <?php
2 namespace FoT3\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 {
62 $this->processWizard();
63 $content = $this->renderContent();
64
65 $response->getBody()->write($content);
66 return $response;
67 }
68
69 /**
70 * Run the wizard and output HTML.
71 *
72 * @return void
73 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use mainAction() instead
74 */
75 public function main()
76 {
77 GeneralUtility::logDeprecatedFunction();
78 $this->processWizard();
79 $this->renderHtml();
80 }
81
82 /**
83 * Run the wizard
84 */
85 protected function processWizard()
86 {
87 $p = GeneralUtility::_GP('P');
88 if (isset($p['itemName'])) {
89 $this->parentFormItemName = $p['itemName'];
90 }
91 if (isset($p['fieldChangeFunc']['TBE_EDITOR_fieldChanged'])) {
92 $this->parentFormFieldChangeFunc = $p['fieldChangeFunc']['TBE_EDITOR_fieldChanged'];
93 }
94
95 if (GeneralUtility::_GP('tx_openid_mode') === 'finish' && $this->openIDResponse === null) {
96 $this->includePHPOpenIDLibrary();
97 $openIdConsumer = $this->getOpenIDConsumer();
98 $this->openIDResponse = $openIdConsumer->complete($this->getReturnUrl());
99 $this->handleResponse();
100 } elseif (GeneralUtility::_POST('openid_url') != '') {
101 $openIDIdentifier = GeneralUtility::_POST('openid_url');
102 $this->sendOpenIDRequest($openIDIdentifier);
103
104 // When sendOpenIDRequest() returns, there was an error
105 $flashMessageService = GeneralUtility::makeInstance(
106 FlashMessageService::class
107 );
108 $flashMessage = GeneralUtility::makeInstance(
109 FlashMessage::class,
110 sprintf(
111 $this->getLanguageService()->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:error.setup'),
112 htmlspecialchars($openIDIdentifier)
113 ),
114 $this->getLanguageService()->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:title.error'),
115 FlashMessage::ERROR
116 );
117 $flashMessageService->getMessageQueueByIdentifier()->enqueue($flashMessage);
118 }
119 }
120
121 /**
122 * Return URL that shall be called by the OpenID server
123 *
124 * @return string Full URL with protocol and hostname
125 */
126 protected function getReturnUrl()
127 {
128 $parameters = [
129 'tx_openid_mode' => 'finish',
130 'P[itemName]' => $this->parentFormItemName,
131 'P[fieldChangeFunc][TBE_EDITOR_fieldChanged]' => $this->parentFormFieldChangeFunc
132 ];
133 return BackendUtility::getModuleUrl('wizard_openid', $parameters, false, true);
134 }
135
136 /**
137 * Check OpenID response and set flash messages depending on its state
138 *
139 * @return void
140 */
141 protected function handleResponse()
142 {
143 /** @var $flashMessageService FlashMessageService */
144 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
145 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
146
147 $lang = $this->getLanguageService();
148 if (!$this->openIDResponse instanceof \Auth_OpenID_ConsumerResponse) {
149 $flashMessage = GeneralUtility::makeInstance(
150 FlashMessage::class,
151 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:error.no-response'),
152 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:title.error'),
153 FlashMessage::ERROR
154 );
155 } elseif ($this->openIDResponse->status == Auth_OpenID_SUCCESS) {
156 // all fine
157 $this->claimedId = $this->getSignedParameter('openid_claimed_id');
158 $flashMessage = GeneralUtility::makeInstance(
159 FlashMessage::class,
160 sprintf(
161 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:youropenid'),
162 htmlspecialchars($this->claimedId)
163 ),
164 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:title.success'),
165 FlashMessage::OK
166 );
167 } elseif ($this->openIDResponse->status == Auth_OpenID_CANCEL) {
168 $flashMessage = GeneralUtility::makeInstance(
169 FlashMessage::class,
170 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:error.cancelled'),
171 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:title.error'),
172 FlashMessage::ERROR
173 );
174 } else {
175 // another failure. show error message and form again
176 $flashMessage = GeneralUtility::makeInstance(
177 FlashMessage::class,
178 sprintf(
179 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:error.general'),
180 htmlspecialchars($this->openIDResponse->status),
181 ''
182 ),
183 $lang->sL('LLL:EXT:openid/Resources/Private/Language/locallang.xlf:title.error'),
184 FlashMessage::ERROR
185 );
186 }
187
188 $defaultFlashMessageQueue->enqueue($flashMessage);
189 }
190
191 /**
192 * Render HTML with message and OpenID form
193 *
194 * @return string
195 */
196 protected function renderContent()
197 {
198 // use FLUID standalone view for wizard content
199 $view = GeneralUtility::makeInstance(StandaloneView::class);
200 $view->getRequest()->setControllerExtensionName('openid');
201 $view->setTemplatePathAndFilename(
202 ExtensionManagementUtility::extPath('openid') .
203 'Resources/Private/Templates/Wizard/Content.html'
204 );
205
206 /** @var $flashMessageService FlashMessageService */
207 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
208 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
209
210 $messages = array();
211 foreach ($defaultFlashMessageQueue->getAllMessagesAndFlush() as $message) {
212 $messages[] = $message->render();
213 }
214 $view->assign('messages', $messages);
215 $view->assign('formAction', BackendUtility::getModuleUrl('wizard_openid', [], false, true));
216 $view->assign('claimedId', $this->claimedId);
217 $view->assign('parentFormItemName', $this->parentFormItemName);
218 $view->assign('parentFormItemNameNoHr', strtr($this->parentFormItemName, array('_hr' => '')));
219 $view->assign('parentFormFieldChangeFunc', $this->parentFormFieldChangeFunc);
220 $view->assign('showForm', true);
221 if (isset($_REQUEST['openid_url'])) {
222 $view->assign('openid_url', $_REQUEST['openid_url']);
223 }
224
225 return $view->render();
226 }
227
228 /**
229 * Render HTML and output it
230 *
231 * @return void
232 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use mainAction() instead
233 */
234 protected function renderHtml()
235 {
236 GeneralUtility::logDeprecatedFunction();
237 header('HTTP/1.0 200 OK');
238 echo $this->renderContent();
239 }
240
241 /**
242 * @return LanguageService
243 */
244 protected function getLanguageService()
245 {
246 return $GLOBALS['LANG'];
247 }
248 }