userconfiguration.php ➔ caldav_editUserConfiguration()   C
last analyzed

Complexity

Conditions 8
Paths 21

Size

Total Lines 136

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 21
nop 1
dl 0
loc 136
rs 6.7555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
//-------------------------------------------------------------------------
3
// OVIDENTIA http://www.ovidentia.org
4
// Ovidentia is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2, or (at your option)
7
// any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
// See the GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17
// USA.
18
//-------------------------------------------------------------------------
19
/**
20
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
21
 * @copyright Copyright (c) 2008 by CANTICO ({@link http://www.cantico.fr})
22
 */
23
24
require_once $GLOBALS['babInstallPath'].'utilit/urlincl.php';
25
require_once dirname(__FILE__) . '/functions.php';
26
27
28
29
function caldav_editUserConfiguration($userId)
30
{
31
	global $babBody, $babDB;
32
33
	$babBody->setTitle(caldav_translate('CalDAV personal calendar configuration'));
34
35
	/* @var $caldav Func_CalendarBackend_Caldav */
36
	$caldav = bab_functionality::get('CalendarBackend/Caldav');
37
38
	if (!$caldav) {
39
		$babBody->addError(caldav_translate('Unable to instanciate "CalendarBackend/Caldav" functionality.'));
40
		$babBody->addError(caldav_translate('Please check that the LibCaldav addon is correctly installed.'));
41
		return;
42
	}
43
44
	if (!bab_isUserAdministrator() || empty($userId)) {
45
		$userId = $GLOBALS['BAB_SESS_USERID'];
46
	}
47
48
	$userIdentifier =  $caldav->getUserIdentifier($userId);
49
	$userPassword = $caldav->getUserPassword($userId);
50
	$useruniqueid = $caldav->getUniqueId($userId);
51
	
52
	$srv = $babDB->db_query('SELECT id, name,use_unique_id FROM libcaldav_servers ORDER BY name');
53
54
	$csrfField = '';
55
	if (class_exists('bab_CsrfProtect')) {
56
    	$csrf = bab_getInstance('bab_CsrfProtect');
57
    	/*@var $csrf bab_CsrfProtect */
58
	
59
	    $csrfField = '<input type="hidden" name="babCsrfProtect" value="'.bab_toHtml($csrf->getToken()).'" />';
60
	}
61
	
62
	$form = '<form name="spaces_configuration" method="post" action="' . $GLOBALS['babPhpSelf'] . '">
63
	    '.$csrfField.'
64
 		<input type="hidden" name="tg" value="' . bab_toHtml(bab_rp('tg')). '">
65
 		<input type="hidden" name="idx" value="saveConfiguration">
66
 		<input type="hidden" name="user" value="' . bab_toHtml($userId) . '">
67
68
  		<table width="90%" border="0" cellspacing="0" cellpadding="2" align="center">
69
		  <tr>
70
		   <td class="BabLoginCadreBackground" align="center" valign="middle">
71
		   <table class="BabLoginMenuBackground"  width="100%" border="0" cellspacing="0" cellpadding="6" align="center">
72
		    <tr>
73
		     <td align="left" colspan="2"><b>' . sprintf(caldav_translate('CalDav account for %s'), bab_getUserName($userId, true)) . '</b></td>
74
		    </tr>
75
		    <tr>
76
		     <td align="right">' . caldav_translate('Server') . '</td>
77
		     <td align="left">
78
		     	<select id=selecter name="server">
79
		     	';
80
	
81
	$selected_server = $caldav->getUserServerId($userId);
82
	while($arr = $babDB->db_fetch_assoc($srv))
83
	{
84
		$form .= 	'<option';
85
		if($arr['use_unique_id']== "true")
86
		{
87
		    $form .= " id=need ";
88
		}
89
		else
90
		{
91
		    $form .= " id=dontneed ";
92
		}
93
        $form .= 'value="'.bab_toHtml($arr['id']).'"';
94
		if ($selected_server == $arr['id'])
95
		{
96
			$form .= ' selected="selected"';
97
		}
98
		$form .= 	'>'.bab_toHtml($arr['name']).'</option>';
99
	}
100
	$form .= 	'</select>
101
		     </td>
102
		    </tr>
103
		    <tr>
104
		     <td align="right">' . caldav_translate('Caldav user identifier') . '</td>
105
		     <td align="left"><input type="text" name="user_identifier" size="20" value="' . bab_toHtml($userIdentifier) . '"></td>
106
		    </tr>
107
		    <tr>
108
		     <td align="right">' . caldav_translate('Caldav user password') . '</td>
109
		     <td align="left"><input type="password" name="user_password" size="20" value="' . bab_toHtml($userPassword) . '"></td>
110
		    </tr>
111
            <tr id="demandeid2" display:none>
112
                <td colspan="3" align="center">'.caldav_translate("This server need your unique id.").'</td>
113
            </tr>
114
            <tr id="demandeid" style="display:none;"';
115
                    
116
                    $form .= '>
117
                <td align="right">'.caldav_translate('Caldav unique identifier') . '</td>
118
                <td align="left"><input type="text" name="unique_id" size="20" value="'.bab_toHtml($useruniqueid).'"></td>    
119
            </tr>
120
            <tr id="demandeid3" display:none>
121
                <td colspan="3" align="center">'.caldav_translate("This id can be found in the Url, example: https://url.yourcaldavserver.com/xxxxx/xxxxx").'</td>
122
            </tr>
123
            <tr>
124
		     <td colspan="2" align="center"><input type="submit" value="' . caldav_translate('Save') . '" /></td>
125
		    </tr>
126
		   </table>
127
		   </td>
128
		  </tr>
129
		 </table>
130
		</form>';
131
	
132
	$form .= "\n <br><script type=text/javascript> $(document).ready(function(){
133
		if($('option:selected',$('#selecter')).attr('id')=='need')
134
               {
135
                $('#demandeid').show();
136
                $('#demandeid2').show();
137
                $('#demandeid3').show();
138
139
            }
140
            else
141
            {
142
                $('#demandeid').hide();
143
                $('#demandeid2').hide();
144
                $('#demandeid3').hide();
145
            }
146
        $('#selecter').change(function(){
147
            if($('option:selected',this).attr('id')=='need')
148
               {
149
                $('#demandeid').show();
150
                $('#demandeid2').show();
151
                $('#demandeid3').show();
152
            }
153
            else
154
            {
155
                $('#demandeid').hide();
156
                $('#demandeid2').hide();
157
                $('#demandeid3').hide();
158
            }
159
        });
160
    })</script>
161
";
162
163
	$babBody->babPopup($form);
164
}
165
166
167
168
function caldav_saveUserConfiguration($userId, $serverId, $userIdentifier, $userPassword,$useuniqueid)
169
{
170
	global $babBody;
171
172
	/* @var $caldav Func_CalendarBackend_Caldav */
173
	$caldav = bab_functionality::get('CalendarBackend/Caldav');
174
175 View Code Duplication
	if (!$caldav) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176
		$babBody->addError(caldav_translate('Unable to instanciate \'CalendarBackend/Caldav\' functionality.'));
177
		$babBody->addError(caldav_translate('Please check that the LibCaldav addon is correctly installed.'));
178
		return false;
179
	}
180
181
	if (!bab_isUserAdministrator() || empty($userId)) {
182
		$userId = $GLOBALS['BAB_SESS_USERID'];
183
	}
184
185
186
	// Test connection to caldav server.
187
	$server = $caldav->getServer($serverId);
188
	$caldav->setServerId($userId, $serverId);
189
	$caldav->setUserIdentifier($userId, $userIdentifier);
190
	$caldav->setUserPassword($userId, $userPassword);
191
    $caldav->setUniqueId($userId, $useuniqueid);
192
	$user_calendar_path = $caldav->processCalendarPathTemplate($server['user_calendar_path'], $userIdentifier, $userId);
193
	$path = $server['server_url'] . $user_calendar_path;
194
	
195
	if (substr($path, -1, 1) !== '/') {
196
		$path .= '/';
197
	}
198
199
	$caldavClient = new CalDAVClient($path, $userIdentifier, $userPassword, null);
200
201
	$xml = '<?xml version="1.0" encoding="utf-8" ?>
202
	<D:propfind xmlns:D="DAV:">
203
	<D:prop>
204
	<D:current-user-privilege-set/>
205
	</D:prop>
206
	</D:propfind>';
207
208
	try {
209
		$caldavClient->SetDepth(0);
210
		$response = $caldavClient->DoXMLRequest('PROPFIND', $xml, '');
211
	} catch(Exception $e) {
212
		bab_debug($e->getMessage());
213
	}
214
215
216
	if (empty($response))
217
	{
218
		// no response, bad calendar or bad password
219
		$babBody->addError(caldav_translate('This account does not exists or the password is invalid'));
220
		return false;
221
	}
222
223
	bab_debug($response);
224
	$babBody->babEcho('<div style="border: 1px solid #cca; padding: 1em; margin: 2em; background-color: #eec">' . caldav_translate('Configuration saved.') . '</div>');
225
226
	return true;
227
}
228
229
230
231
232
233
234
$idx = bab_rp('idx');
235
236
switch($idx) {
237
238
	case 'saveConfiguration':
239
		caldav_saveUserConfiguration(bab_rp('user'), bab_rp('server'), bab_rp('user_identifier'), bab_rp('user_password'), bab_rp('unique_id'));
240
		caldav_editUserConfiguration(bab_rp('user'));
241
		break;
242
243
	default:
244
	case 'configuration':
245
		caldav_editUserConfiguration(bab_rp('user'));
246
		break;
247
}
248