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