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