LdapConnector   D
last analyzed

Complexity

Total Complexity 93

Size/Duplication

Total Lines 422
Duplicated Lines 4.74 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 93
c 1
b 1
f 0
lcom 1
cbo 3
dl 20
loc 422
ccs 0
cts 270
cp 0
rs 4.8717

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getLdapEntries() 0 17 4
A insertEmptyEntries() 0 9 4
A __construct() 0 8 2
A convertDate() 0 8 1
C ldapToVCard() 0 48 8
A updateVCardProperty() 0 11 3
A updateVCardImageProperty() 6 15 3
B getVCardProperty() 0 19 8
C getLdifEntry() 0 28 12
A VCardToLdap() 0 15 3
C getLdifProperty() 0 35 11
A updateLdifProperty() 0 9 3
A getUnassignedVCardProperty() 7 7 3
A getLdapId() 7 7 3
A getXmlConfigName() 0 7 3
C validateLdapEntry() 0 44 13
D getOrCreateVCardProperty() 0 45 9

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like LdapConnector often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LdapConnector, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * ownCloud - Addressbook LDAP
4
 *
5
 * @author Nicolas Mora
6
 * @copyright 2013 Nicolas Mora [email protected]
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation
11
 * version 3 of the License
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.	If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
 
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
23
namespace OCA\Contacts\Connector;
24
25
class LdapConnector {
26
	
27
	public function __construct($xml_config) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $xml_config is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $xml_config is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
28
		try {
29
			//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', setting xml config', \OCP\Util::DEBUG);
30
			$this->config_content = new \SimpleXMLElement($xml_config);
0 ignored issues
show
Bug introduced by
The property config_content does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
		} catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class OCA\Contacts\Connector\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
32
			\OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', error in setting xml config', \OCP\Util::DEBUG);
33
		}
34
	}
35
	private function convertDate ($ldapDate) {
36
37
		$tstamp = strtotime($ldapDate);
38
		$theDate = new \DateTime;
39
		$theDate->setTimestamp($tstamp);
40
		
41
		return $theDate;
42
	}
43
	/**
44
	 * @brief transform a ldap entry into an VCard object
45
	 *	for each ldap entry which is like "property: value"
46
	 *	to a VCard entry which is like "PROPERTY[;PARAMETER=param]:value"
47
	 * @param array $ldapEntry
48
	 * @return OC_VCard
49
	 */
50
	public function ldapToVCard($ldapEntry) {
0 ignored issues
show
Coding Style Naming introduced by
The variable $v_params is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $v_param is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $v_property is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
51
		$vcard = new \OCA\Contacts\VObject\VCard();
0 ignored issues
show
Bug introduced by
The call to VCard::__construct() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
52
		$vcard->REV = $this->convertDate($ldapEntry['modifytimestamp'][0])->format(\DateTime::W3C);
53
		//error_log("modifytimestamp: ".$vcard->REV);
54
		$vcard->{'X-LDAP-DN'} = base64_encode($ldapEntry['dn']);
55
		// OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' vcard is '.$vcard->serialize(), \OCP\Util::DEBUG);
56
		
57
		for ($i=0; $i<$ldapEntry["count"]; $i++) {
58
			// ldap property name : $ldap_entry[$i]
59
			$lProperty = $ldapEntry[$i];
60
			for ($j=0;$j<$ldapEntry[$lProperty]["count"];$j++){
61
				
62
				// What to do :
63
				// convert the ldap property into vcard property, type and position (if needed)
64
				// $v_params format: array('property' => property, 'type' => array(types), 'position' => position)
65
				$v_params = $this->getVCardProperty($lProperty);
66
				
67
				foreach ($v_params as $v_param) {
68
					
69
					if (isset($v_param['unassigned'])) {
70
						// if the value comes from the unassigned entry, it's a vcard property dumped
71
						try {
72
							$property = \Sabre\VObject\Reader::read($ldapEntry[$lProperty][$j]);
73
							$vcard->add($property);
74
						} catch (exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class OCA\Contacts\Connector\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
75
						}
76
					} else {
77
						// Checks if a same kind of property already exists in the VCard (property and parameters)
78
						// if so, sets a property variable with the current data
79
						// else, creates a property variable
80
						$v_property = $this->getOrCreateVCardProperty($vcard, $v_param, $j);
81
						
82
						// modify the property with the new data
83
						if (strcasecmp($v_param['image'], 'true') == 0) {
84
							$this->updateVCardImageProperty($v_property, $ldapEntry[$lProperty][$j], $vcard->VERSION);
85
						} else {
86
							$this->updateVCardProperty($v_property, $ldapEntry[$lProperty][$j], $v_param['position']);
87
						}
88
					}
89
				}
90
			}
91
		}
92
		
93
		if (!isset($vcard->UID)) {
94
			$vcard->UID = base64_encode($ldapEntry['dn']);
95
		}
96
		return $vcard;
97
	}
98
	
99
	/**
100
	 * @brief returns the vcard property corresponding to the ldif parameter
101
	 * creates the property if it doesn't exists yet
102
	 * @param $vcard the vcard to get or create the properties with
103
	 * @param $v_param the parameter the find
104
	 * @param integer $index the position of the property in the vcard to find
105
	 */
106
	public function getOrCreateVCardProperty(&$vcard, $v_param, $index) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $v_param is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $v_param is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
107
		
108
		// looking for one
109
		//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' entering '.$vcard->serialize(), \OCP\Util::DEBUG);
110
		$properties = $vcard->select($v_param['property']);
111
		$counter = 0;
112
		foreach ($properties as $property) {
113
			if ($v_param['type'] == null) {
114
				//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' property '.$v_param['type'].' found', \OCP\Util::DEBUG);
115
				return $property;
116
			}
117
			foreach ($property->parameters as $parameter) {
118
				//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' parameter '.$parameter->getValue().' <> '.$v_param['type'], \OCP\Util::DEBUG);
119
				if (!strcmp($parameter->getValue(), $v_param['type'])) {
120
					//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' parameter '.$parameter->getValue().' found', \OCP\Util::DEBUG);
121
					if ($counter==$index) {
122
						return $property;
123
					}
124
					$counter++;
125
				}
126
			}
127
		}
128
		
129
		// Property not found, creating one
130
		//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', create one '.$v_param['property'].';TYPE='.$v_param['type'], \OCP\Util::DEBUG);
131
		$line = count($vcard->children) - 1;
0 ignored issues
show
Unused Code introduced by
$line is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
132
		$property = $vcard->createProperty($v_param['property']);
133
		$vcard->add($property);
134
		if ($v_param['type']!=null) {
135
			//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', creating one '.$v_param['property'].';TYPE='.$v_param['type'], \OCP\Util::DEBUG);
136
			//\OC_Log::write('ldapconnector', __METHOD__.', creating one '.$v_param['property'].';TYPE='.$v_param['type'], \OC_Log::DEBUG);
137
			$property->parameters[] = new	\Sabre\VObject\Parameter('TYPE', ''.$v_param['type']);
138
			switch ($v_param['property']) {
139
				case "ADR":
140
					//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', we have an address '.$v_param['property'].';TYPE='.$v_param['type'], \OCP\Util::DEBUG);
141
					$property->setValue(";;;;;;");
142
					break;
143
				case "FN":
144
					$property->setValue(";;;;");
145
					break;
146
			}
147
		}
148
		//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' exiting '.$vcard->serialize(), \OCP\Util::DEBUG);
149
		return $property;
150
	}
151
	
152
	/**
153
	 * @brief modifies a vcard property array with the ldap_entry given in parameter at the given position
154
	 */
155
	public function updateVCardProperty(&$v_property, $ldap_entry, $position=null) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $v_property is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $ldap_entry is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $v_property is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $v_array is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $ldap_entry is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
156
		for ($i=0; $i<count($v_property); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Performance Best Practice introduced by
Consider avoiding function calls on each iteration of the for loop.

If you have a function call in the test part of a for loop, this function is executed on each iteration. Often such a function, can be moved to the initialization part and be cached.

// count() is called on each iteration
for ($i=0; $i < count($collection); $i++) { }

// count() is only called once
for ($i=0, $c=count($collection); $i<$c; $i++) { }
Loading history...
157
			if ($position != null) {
158
				$v_array = explode(";", $v_property[$i]);
159
				$v_array[intval($position)] = $ldap_entry;
160
				$v_property[$i]->setValue(implode(";", $v_array));
161
			} else {
162
				$v_property[$i]->setValue($ldap_entry);
163
			}
164
		}
165
	}
166
	
167
	/**
168
	 * @brief modifies a vcard property array with the image
169
	 */
170
	public function updateVCardImageProperty(&$v_property, $ldap_entry, $version) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $v_property is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $ldap_entry is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $v_property is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $ldap_entry is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
171
		for ($i=0; $i<count($v_property); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Performance Best Practice introduced by
Consider avoiding function calls on each iteration of the for loop.

If you have a function call in the test part of a for loop, this function is executed on each iteration. Often such a function, can be moved to the initialization part and be cached.

// count() is called on each iteration
for ($i=0; $i < count($collection); $i++) { }

// count() is only called once
for ($i=0, $c=count($collection); $i<$c; $i++) { }
Loading history...
172
			$image = new \OC_Image();
173
			$image->loadFromData($ldap_entry);
174 View Code Duplication
			if (strcmp($version, '4.0') == 0) {
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...
175
				$type = $image->mimeType();
176
			} else {
177
				$arrayType = explode('/', $image->mimeType());
178
				$type = strtoupper(array_pop($arrayType));
179
			}
180
			$v_property[$i]->add('ENCODING', 'b');
181
			$v_property[$i]->add('TYPE', $type);
182
			$v_property[$i]->setValue($image->__toString());
183
		}
184
	}
185
	
186
	/**
187
	 * @brief gets the vcard property values from an ldif entry name
188
	 * @param $lProperty the ldif property name
189
	 * @return array('property' => property, 'type' => type, 'position' => position)
0 ignored issues
show
Documentation introduced by
The doc-type array('property' could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
190
	 */
191
	public function getVCardProperty($lProperty) {
0 ignored issues
show
Coding Style Naming introduced by
The variable $ldif_entry is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $vcard_entry is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
192
		$properties = array();
193
		if (strcmp($lProperty, $this->getUnassignedVCardProperty()) == 0) {
194
			$properties[] = array('unassigned' => true);
195
		} else {
196
			foreach ($this->config_content->ldap_entries->ldif_entry as $ldif_entry) {
197
				if ($lProperty == $ldif_entry['name']) {
198
					// $ldif_entry['name'] is the right config xml
199
					foreach ($ldif_entry->vcard_entry as $vcard_entry) {
200
						$type=isset($vcard_entry['type'])?$vcard_entry['type']:"";
201
						$position=isset($vcard_entry['position'])?$vcard_entry['position']:"";
202
						$image=isset($ldif_entry['image'])?$ldif_entry['image']:"";
203
						$properties[] = array('property' => $vcard_entry['property'], 'type' => $type, 'position' => $position, 'image' => $image);
204
					}
205
				}
206
			}
207
		}
208
		return $properties;
209
	}
210
	
211
	/**
212
	 * @brief return the ldif entries corresponding to the name and type given in parameter
213
	 * @param $propertyName the name of the vcard parameter
214
	 * @param $propertyType the type of the parameter
215
	 */
216
	public function getLdifEntry($propertyName, $propertyType) {
217
		//\OC_Log::write('ldapconnector', __METHOD__."looking for $propertyName, $propertyType", \OC_Log::DEBUG);
218
		if ($this->config_content !=null) {
219
			$ldifEntries = array();
220
			foreach ($this->config_content->vcard_entries->vcard_entry as $vcardEntry) {
221
				if (strcasecmp($vcardEntry['property'], $propertyName) == 0 && (!isset($vcardEntry['type']) || strcasecmp($vcardEntry['type'], $propertyType) == 0) && strcasecmp($vcardEntry['enabled'], 'true') == 0) {
222
					foreach($vcardEntry->ldif_entry as $ldifEntry) {
223
						$params = array();
224
						$params['name'] = $ldifEntry['name'];
225
						if (isset($ldifEntry['vcard_position'])) {
226
							$params['vcard_position'] = intval($ldifEntry['vcard_position']);
227
						}
228
						if (isset($vcardEntry['image']) && strcasecmp($vcardEntry['image'], 'true') == 0) {
229
							$params['image'] = true;
230
						} else {
231
							$params['image'] = false;
232
						}
233
						$ldifEntries[] = $params;
234
					}
235
				}
236
			}
237
			if (count($ldifEntries) == 0) {
238
				$ldifEntries[] = array('unassigned' => true);
239
			}
240
			return $ldifEntries;
241
		}
242
		return null;
243
	}
244
	
245
	/**
246
	 * @brief transform a vcard into a ldap entry
247
	 * @param VCard $vcard
248
	 * @return array|false
249
	 */
250
	public function VCardToLdap($vcard) {
0 ignored issues
show
Coding Style Naming introduced by
The method VCardToLdap is not named in camelCase.

This check marks method names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
251
252
		$ldifReturn = array(); // Array to return
253
		foreach ($vcard->children() as $property) {
254
			// Basically, a $property can be converted into one or multiple ldif entries
255
			// and also some vcard properties can have data that can be split to fill different ldif entries
256
			$ldifArray = self::getLdifProperty($property);
257
			
258
			if (count($ldifArray) > 0) {
259
				self::updateLdifProperty($ldifReturn, $ldifArray);
260
			}
261
		}
262
		self::validateLdapEntry($ldifReturn);
263
		return $ldifReturn;
264
	}
265
	
266
	/**
267
	 * @brief transform finds the ldif entries associated with the property
268
	 * @param Property $property
269
	 * @return array|false
270
	 */
271
	public function getLdifProperty($property) {
272
		$ldifReturn = array();
273
		// Only one value per property, so we loop into types
274
		// then for each one, look into config xml if there are ldif entries matching
275
		$ldifEntries = self::getLdifEntry($property->name, $property['TYPE']);
276
		// If one is found, create a tab entry like tab['ldif_entry']
277
		if ($ldifEntries != null && count($ldifEntries)>0) {
278
			foreach ($ldifEntries as $ldifEntry) {
279
				if (isset($ldifEntry['unassigned'])) {
280
					if ((strcasecmp($property->name, "REV") != 0) && (strcasecmp($property->name, "VERSION") != 0) && (strcasecmp($property->name, "X-LDAP-DN") != 0)) {
281
						// The unassigned properties are set in the ldap unassignedVCardProperty
282
						$ldifReturn[(string)$this->getUnassignedVCardProperty()] = array($property->serialize());
283
					}
284
				} else {
285
					// Last, if the ldif entry has a vcard_position set, take only the value in the position index
286
					$value = $property->getValue();
287
					if (isset($ldifEntry['vcard_position'])) {
288
						//\OC_Log::write('ldapconnector', __METHOD__." position set ".$ldifEntry['vcard_position'], \OC_Log::DEBUG);
289
						$tmpValues = explode(";", $property->getValue());
290
						$value = $tmpValues[$ldifEntry['vcard_position']];
291
					}
292
					//\OC_Log::write('ldapconnector', __METHOD__.__METHOD__." entry : ".$ldifEntry['name']." - value : $value", \OC_Log::DEBUG);
293
					// finally, sets tab['ldif_entry'][] with the value
294
					if (strcmp($value, "") != 0) {
295
						if ($ldifEntry['image']) {
296
							$ldifReturn[(string)$ldifEntry['name']] = array(base64_decode($value));
297
						} else {
298
							$ldifReturn[(string)$ldifEntry['name']] = array($value);
299
						}
300
					}
301
				}
302
			}
303
		}
304
		return $ldifReturn;
305
	}
306
	
307
	/**
308
	 * @brief updates the ldifEntry with $ldifNewValues
309
	 * @param $ldifEntry the array to modify
310
	 * @param $ldifNewValues the new values
311
	 * @return boolean|null
312
	 */
313
	public function updateLdifProperty(&$ldifEntries, $ldifNewValues) {
314
		foreach ($ldifNewValues as $key => $value) {
315
			if (isset($ldifEntries[$key])) {
316
				$ldifEntries[$key] = array_merge($ldifEntries[$key], $value);
317
			} else {
318
				$ldifEntries[$key] = $value;
319
			}
320
		}
321
	}
322
	
323
	/**
324
	 * @brief returns all the ldap entries managed
325
	 * @return array
326
	 */
327
	public function getLdapEntries() {
0 ignored issues
show
Coding Style Naming introduced by
The variable $to_return is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $ldif_entry is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
328
		if ($this->config_content != null) {
329
			$to_return = array('modifytimestamp');
330
			
331
			$unassigned = $this->getUnassignedVCardProperty();
332
			if ($unassigned) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $unassigned of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
333
				$to_return[] = $unassigned;
334
			}
335
			
336
			foreach ($this->config_content->ldap_entries[0]->ldif_entry as $ldif_entry) {
337
				$to_return[] = $ldif_entry['name'];
338
			}
339
			return $to_return;
340
		} else {
341
			return null;
342
		}
343
	}
344
	
345
	/**
346
	 * @brief returns the ldif entry for the VCard properties that don't have a ldap correspondance
347
	 * @return string|false
348
	 */
349 View Code Duplication
	public function getUnassignedVCardProperty() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
350
		if ($this->config_content != null && $this->config_content->ldap_entries[0]->ldap_core[0]->unassigned_vcard_property['ldap_name'] != null) {
351
			return ($this->config_content->ldap_entries[0]->ldap_core[0]->unassigned_vcard_property['ldap_name']);
352
		} else {
353
			return false;
354
		}
355
	}
356
	
357
	/**
358
	 * @brief get the id attribute in the ldap
359
	 * @return string|false
360
	 */
361 View Code Duplication
	public function getLdapId() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
362
		if ($this->config_content != null && $this->config_content->ldap_entries[0]->ldap_core[0]->ldap_id['name'] != null) {
363
			return ($this->config_content->ldap_entries[0]->ldap_core[0]->ldap_id['name']);
364
		} else {
365
			return false;
366
		}
367
	}
368
	
369
	/**
370
	 * @brief get the xml config name
371
	 * @return string|false
372
	 */
373
	public function getXmlConfigName() {
374
		if ($this->config_content != null && $this->config_content['name'] != null) {
375
			return ($this->config_content['name']);
376
		} else {
377
			return false;
378
		}
379
	}
380
	
381
	/**
382
	 * @brief checks if the ldapEntry is valid and fixes if possible
383
	 * @param $ldapEntry array
384
	 * @return boolean
385
	 */
386
	public function validateLdapEntry(&$ldapEntry) {
0 ignored issues
show
Coding Style Naming introduced by
The variable $object_class is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $not_null is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
387
		if ($this->config_content != null) {
388
			$ldapEntry['objectclass'] = array();
389
			
390
			// Fill $ldapEntry with the objectClass params
391
			foreach ($this->config_content->ldap_entries[0]->ldap_core[0]->object_class as $object_class) {
392
				$ldapEntry['objectclass'][] = (string)$object_class['name'];
393
			}
394
			
395
			foreach ($this->config_content->ldap_entries[0]->ldap_core[0]->not_null as $not_null) {
396
				$key = (string)$not_null['name'];
397
				
398
				// Repair $ldapEntry if a field is null or empty
399
				if (!array_key_exists($key, $ldapEntry) || strcmp('', $ldapEntry[$key][0]) == 0) {
400
					
401
					// Switch with another entry
402
					if (isset($not_null->action_switch[0])) {
403
						$ldapEntry[$key][0] = $ldapEntry[(string)$not_null->action_switch[0]['name']][0];
404
						unset($ldapEntry[(string)$not_null->action_switch[0]['name']][0]);
405
						if (count($ldapEntry[(string)$not_null->action_switch[0]['name']]) == 0) {
406
							unset($ldapEntry[(string)$not_null->action_switch[0]['name']]);
407
						}
408
					} else if (isset($not_null->action_default[0])) {
409
						// Fill with a default value
410
						$ldapEntry[$key][0] = (string)$not_null->action_default[0]['value'];
411
					}
412
				}
413
			}
414
			
415
			foreach ($this->config_content->vcard_entries[0]->vcard_entry as $vcardEntry)  {
416
				foreach ($vcardEntry->ldif_entry as $ldifEntry) {
417
					// Remove duplicates if relevant
418
					if (strcmp("true", $ldifEntry['unique'])==0 && array_key_exists((string)$ldifEntry['name'], $ldapEntry)) { // Y aller à coup de "bool array_key_exists ( mixed $key , array $search )"
419
						// Holy hand-grenade, there are like 3 imbricated loops...
420
						$ldapEntry[(string)$ldifEntry['name']] = array_unique($ldapEntry[(string)$ldifEntry['name']]);
421
					}
422
				}
423
			}
424
			
425
			return true;
426
		} else {
427
			return false;
428
		}
429
	}
430
	
431
	/**
432
	 * @brief adds empty entries in $dest if $dest doesn't have those entries and if $source has
433
	 * otherwise, I couldn't find how to remove attributes
434
	 * @param $source the source ldap entry as model
435
	 * @param $dest the destination entry to add empty params if we have to
436
	 */
437
	public function insertEmptyEntries($source, &$dest) {
438
		for ($i=0; $i<$source["count"]; $i++) {
439
			
440
			$lProperty = $source[$i];
441
			if (!isset($dest[$lProperty]) && $lProperty != 'modifytimestamp') {
442
				$dest[$lProperty] = array();
443
			}
444
		}
445
	}
446
}
447
448
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
449