[!!!][TASK] Remove module URI storage in global variable
[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 GeneralUtility::getIndpEnv('TYPO3_SITE_URL')
101 . TYPO3_mainDir
102 . BackendUtility::getModuleUrl('wizard_openid');
103 }
104
105 /**
106 * Return URL that shall be called by the OpenID server
107 *
108 * @return string Full URL with protocol and hostname
109 */
110 protected function getReturnUrl() {
111 return $this->getSelfURL() .
112 '&tx_openid_mode=finish' .
113 '&P[itemName]=' . urlencode($this->parentFormItemName) .
114 '&P[fieldChangeFunc][TBE_EDITOR_fieldChanged]]=' . urlencode($this->parentFormFieldChangeFunc);
115 }
116
117 /**
118 * Check OpenID response and set flash messages depending on its state
119 *
120 * @return void
121 *
122 * @uses $openIDResponse
123 */
124 protected function handleResponse() {
125 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
126 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
127 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
128
129 if (!$this->openIDResponse instanceof \Auth_OpenID_ConsumerResponse) {
130 $flashMessage = GeneralUtility::makeInstance(
131 FlashMessage::class,
132 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:error.no-response'),
133 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:title.error'),
134 FlashMessage::ERROR
135 );
136 } elseif ($this->openIDResponse->status == Auth_OpenID_SUCCESS) {
137 // all fine
138 $this->claimedId = $this->getSignedParameter('openid_claimed_id');
139 $flashMessage = GeneralUtility::makeInstance(
140 FlashMessage::class,
141 sprintf(
142 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:youropenid'),
143 htmlspecialchars($this->claimedId)
144 ),
145 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:title.success'),
146 FlashMessage::OK
147 );
148 } elseif ($this->openIDResponse->status == Auth_OpenID_CANCEL) {
149 $flashMessage = GeneralUtility::makeInstance(
150 FlashMessage::class,
151 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:error.cancelled'),
152 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:title.error'),
153 FlashMessage::ERROR
154 );
155 } else {
156 // another failure. show error message and form again
157 $flashMessage = GeneralUtility::makeInstance(
158 FlashMessage::class,
159 sprintf(
160 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:error.general'),
161 htmlspecialchars($this->openIDResponse->status),
162 htmlspecialchars($this->openIDResponse->message)
163 ),
164 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:title.error'),
165 FlashMessage::ERROR
166 );
167 }
168
169 $defaultFlashMessageQueue->enqueue($flashMessage);
170 }
171
172 /**
173 * Render HTML with messagse and OpenID form and output it
174 *
175 * @return void
176 */
177 protected function renderHtml() {
178 // use FLUID standalone view for wizard content
179 $view = GeneralUtility::makeInstance(\TYPO3\CMS\Fluid\View\StandaloneView::class);
180 $view->setTemplatePathAndFilename(
181 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('openid') .
182 'Resources/Private/Templates/Wizard/Content.html'
183 );
184
185 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
186 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
187 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
188
189 $messages = array();
190 foreach ($defaultFlashMessageQueue->getAllMessagesAndFlush() as $message) {
191 $messages[] = $message->render();
192 }
193 $view->assign('messages', $messages);
194 $view->assign('formAction', $this->getSelfURL());
195 $view->assign('claimedId', $this->claimedId);
196 $view->assign('parentFormItemName', $this->parentFormItemName);
197 $view->assign('parentFormItemNameNoHr', strtr($this->parentFormItemName, array('_hr' => '')));
198 $view->assign('parentFormFieldChangeFunc', $this->parentFormFieldChangeFunc);
199 $view->assign('showForm', TRUE);
200 if (isset($_REQUEST['openid_url'])) {
201 $view->assign('openid_url', $_REQUEST['openid_url']);
202 }
203
204 header('HTTP/1.0 200 OK');
205 echo $view->render();
206 }
207
208 }