[TASK] Remove unused use statements
[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\Core\Utility\GeneralUtility;
18
19 /**
20 * OpenID selection wizard for the backend
21 *
22 * @author Christian Weiske <cweiske@cweiske.de>
23 */
24 class Wizard extends OpenidService {
25 /**
26 * OpenID of the user after authentication
27 *
28 * @var string
29 */
30 protected $claimedId;
31
32 /**
33 * Name of the form element this wizard should write the OpenID into
34 *
35 * @var string
36 */
37 protected $parentFormItemName;
38
39 /**
40 * Name of the function that needs to be called after setting the value
41 *
42 * @var string
43 */
44 protected $parentFormFieldChangeFunc;
45
46 /**
47 * Run the wizard and output HTML.
48 *
49 * @return void
50 */
51 public function main() {
52 $p = GeneralUtility::_GP('P');
53 if (isset($p['itemName'])) {
54 $this->parentFormItemName = $p['itemName'];
55 }
56 if (isset($p['fieldChangeFunc']['TBE_EDITOR_fieldChanged'])) {
57 $this->parentFormFieldChangeFunc = $p['fieldChangeFunc']['TBE_EDITOR_fieldChanged'];
58 }
59
60 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('tx_openid_mode') === 'finish'
61 && $this->openIDResponse === NULL
62 ) {
63 $this->includePHPOpenIDLibrary();
64 $openIdConsumer = $this->getOpenIDConsumer();
65 $this->openIDResponse = $openIdConsumer->complete($this->getReturnUrl());
66 $this->handleResponse();
67 $this->renderHtml();
68 return;
69 } elseif (GeneralUtility::_POST('openid_url') != '') {
70 $openIDIdentifier = GeneralUtility::_POST('openid_url');
71 $this->sendOpenIDRequest($openIDIdentifier);
72
73 // When sendOpenIDRequest() returns, there was an error
74 $flashMessageService = GeneralUtility::makeInstance(
75 'TYPO3\\CMS\\Core\\Messaging\\FlashMessageService'
76 );
77 $flashMessage = GeneralUtility::makeInstance(
78 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
79 sprintf(
80 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:error.setup'),
81 htmlspecialchars($openIDIdentifier)
82 ),
83 $GLOBALS['LANG']->sL('LLL:EXT:openid/Resources/Private/Language/Wizard.xlf:title.error'),
84 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
85 );
86 $flashMessageService->getMessageQueueByIdentifier()->enqueue($flashMessage);
87 }
88 $this->renderHtml();
89 }
90
91 /**
92 * Return URL to this wizard
93 *
94 * @return string Full URL with protocol and hostname
95 */
96 protected function getSelfUrl() {
97 return GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir .
98 $GLOBALS['MCONF']['_'];
99 }
100
101 /**
102 * Return URL that shall be called by the OpenID server
103 *
104 * @return string Full URL with protocol and hostname
105 */
106 protected function getReturnUrl() {
107 return $this->getSelfURL() .
108 '&tx_openid_mode=finish' .
109 '&P[itemName]=' . urlencode($this->parentFormItemName) .
110 '&P[fieldChangeFunc][TBE_EDITOR_fieldChanged]]=' . urlencode($this->parentFormFieldChangeFunc);
111 }
112
113 /**
114 * Check OpenID response and set flash messages depending on its state
115 *
116 * @return void
117 *
118 * @uses $openIDResponse
119 */
120 protected function handleResponse() {
121 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
122 $flashMessageService = GeneralUtility::makeInstance(
123 'TYPO3\\CMS\\Core\\Messaging\\FlashMessageService'
124 );
125 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
126
127 if (!$this->openIDResponse instanceof \Auth_OpenID_ConsumerResponse) {
128 $flashMessage = GeneralUtility::makeInstance(
129 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
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 \TYPO3\CMS\Core\Messaging\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 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
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 \TYPO3\CMS\Core\Messaging\FlashMessage::OK
145 );
146 } elseif ($this->openIDResponse->status == Auth_OpenID_CANCEL) {
147 $flashMessage = GeneralUtility::makeInstance(
148 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
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 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
152 );
153 } else {
154 // another failure. show error message and form again
155 $flashMessage = GeneralUtility::makeInstance(
156 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
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 \TYPO3\CMS\Core\Messaging\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');
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(
185 'TYPO3\\CMS\\Core\\Messaging\\FlashMessageService'
186 );
187 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
188 $messages = array();
189 foreach ($defaultFlashMessageQueue->getAllMessagesAndFlush() as $message) {
190 $messages[] = $message->render();
191 }
192 $view->assign('messages', $messages);
193 $view->assign('formAction', $this->getSelfURL());
194 $view->assign('claimedId', $this->claimedId);
195 $view->assign('parentFormItemName', $this->parentFormItemName);
196 $view->assign('parentFormItemNameNoHr', strtr($this->parentFormItemName, array('_hr' => '')));
197 $view->assign('parentFormFieldChangeFunc', $this->parentFormFieldChangeFunc);
198 $view->assign('showForm', TRUE);
199 if (isset($_REQUEST['openid_url'])) {
200 $view->assign('openid_url', $_REQUEST['openid_url']);
201 }
202
203 header('HTTP/1.0 200 OK');
204 echo $view->render();
205 }
206 }
207 ?>