[FEATURE] Use mod dispatcher for setup extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / rsaauth / hooks / class.tx_rsaauth_usersetuphook.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Helmut Hummel <helmut.hummel@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * This class provides a hook to the login form to add extra javascript code
28 * and supply a proper form tag.
29 *
30 * @author Helmut Hummel <helmut.hummel@typo3.org>
31 * @package TYPO3
32 * @subpackage tx_rsaauth
33 */
34 class tx_rsaauth_usersetuphook {
35
36 /**
37 * Decrypt the password fields if they are filled.
38 *
39 * @param array $parameters Parameters to the script
40 * @return void
41 */
42 public function decryptPassword(array $parameters) {
43
44 if ($this->isRsaAvailable()) {
45 $be_user_data = &$parameters['be_user_data'];
46
47 if (substr($be_user_data['password'], 0, 4) === 'rsa:' && substr($be_user_data['password2'], 0, 4) === 'rsa:') {
48 $backend = tx_rsaauth_backendfactory::getBackend();
49 $storage = tx_rsaauth_storagefactory::getStorage();
50 /* @var $storage tx_rsaauth_abstract_storage */
51
52 $key = $storage->get();
53
54 $password = $backend->decrypt($key, substr($be_user_data['password'], 4));
55 $password2 = $backend->decrypt($key, substr($be_user_data['password2'], 4));
56
57 $be_user_data['password'] = $password ? $password : $be_user_data['password'];
58 $be_user_data['password2'] = $password2 ? $password2 : $be_user_data['password2'];
59 }
60 }
61 }
62
63
64 /**
65 * Provides form code and javascript for the user setup.
66 *
67 * @param array $parameters Parameters to the script
68 * @param SC_index $userSetupObject Calling object: user setup module
69 * @return string The code for the user setup
70 */
71 public function getLoginScripts(array $parameters, SC_mod_user_setup_index $userSetupObject) {
72 $content = '';
73 if ($this->isRsaAvailable()) {
74 // If we can get the backend, we can proceed
75 $backend = tx_rsaauth_backendfactory::getBackend();
76
77 $javascriptPath = t3lib_extMgm::siteRelPath('rsaauth') . 'resources/';
78 $files = array(
79 'jsbn/jsbn.js',
80 'jsbn/prng4.js',
81 'jsbn/rng.js',
82 'jsbn/rsa.js',
83 'jsbn/base64.js',
84 'rsaauth_min.js'
85 );
86
87 $content = '';
88 foreach ($files as $file) {
89 $content .= '<script type="text/javascript" src="' .
90 t3lib_div::getIndpEnv('TYPO3_SITE_URL') .
91 $javascriptPath . $file . '"></script>';
92 }
93 // Generate a new key pair
94 $keyPair = $backend->createNewKeyPair();
95
96 // Save private key
97 $storage = tx_rsaauth_storagefactory::getStorage();
98 /* @var $storage tx_rsaauth_abstract_storage */
99 $storage->put($keyPair->getPrivateKey());
100
101 // Add form tag
102 $form = '<form action="' . t3lib_BEfunc::getModuleUrl('user_setup') . '" method="post" name="usersetup" enctype="application/x-www-form-urlencoded" onsubmit="tx_rsaauth_encryptUserSetup();">';
103 // Add RSA hidden fields
104 $form .= '<input type="hidden" id="rsa_n" name="n" value="' . htmlspecialchars($keyPair->getPublicKeyModulus()) . '" />';
105 $form .= '<input type="hidden" id="rsa_e" name="e" value="' . sprintf('%x', $keyPair->getExponent()) . '" />';
106
107 $userSetupObject->doc->form = $form;
108 }
109 return $content;
110 }
111
112 /**
113 * Rsa is available if loginSecurityLevel is set and rsa backend is working.
114 *
115 * @return bool
116 */
117 protected function isRsaAvailable() {
118 return (trim($GLOBALS['TYPO3_CONF_VARS']['BE']['loginSecurityLevel']) === 'rsa') && (tx_rsaauth_backendfactory::getBackend() !== NULL);
119 }
120 }
121 ?>