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