forked from pdavila13/consultesLdap
/
uploadPhotos.php
174 lines (126 loc) · 4.01 KB
/
uploadPhotos.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
#IMPORTANT: no poseu les paraules de pas a aquest fitxer:
include "/etc/paraulesdepas.php";
$ldapconfig['host'] = '192.168.0.8';
#Només cal indicar el port si es diferent del port per defecte
$ldapconfig['port'] = NULL;
$ldapconfig['basedn'] = 'dc=iesebre,dc=com';
$ds=ldap_connect($ldapconfig['host'], $ldapconfig['port']);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
$password=$PASSWD;
$dn="cn=admin,dc=iesebre,dc=com";
$basedn="ou=All,dc=iesebre,dc=com";
if ($bind=ldap_bind($ds, $dn, $password)) {
echo("Login correct\n");
} else {
# Error
}
$dnis=array();
function getDirectory( $path = '.', $level = 0 ) {
$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$dh = @opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
str_repeat( ' ', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
echo "$path/$file;";
getDirectory( "$path/$file", ($level+1) );
// Re-call this same function but on a new directory.
// this is what makes function recursive.
} else {
echo "$path/$file;";
// Just print out the filename
}
}
}
closedir( $dh );
// Close the directory handle
}
ob_start();
getDirectory( "/home/sergi/aborrar/fotos" );
$files = ob_get_contents();
ob_end_clean();
//echo $files."\n\n\n\n\n\n";
$files_a=split(";",$files);
$files_a_filtered = preg_grep("/^.*\.(png)$/i", $files_a);
//DEBUG
//print_r($files_a);
//print_r($fl_array);
/*
foreach ($files_a_filtered as $filea) {
echo $filea."\n";
}
exit;
* */
foreach ($files_a_filtered as $file) {
echo $file."\n";
//echo basename($file,".png")."\n";
//Search Ldap object with this DNI
echo "Searching DNI: " . basename($file,".png") . "...";
// Search surname entry
$filter="(irisPersonalUniqueID=".basename($file,".png").")";
$sr=ldap_search($ds,$basedn, $filter);
$found=ldap_count_entries($ds,$sr);
switch ($found) {
case 0:
echo "DNI NOT FOUND! $file\n";
break;
case 1:
//Comprovar si existeix el camp jpegPhoto
$info = ldap_get_entries($ds, $sr);
//echo "Data for ".$info["count"]." items returned:<p>";
echo " dn is: ". $info[0]["dn"] . " ";
//echo "array: ". print_r($info[0]) ."\n";
if (in_array("jpegphoto", $info[0])) {
echo "PHOTO OK!\n";
} else {
echo "PHOTO NOT FOUND! Adding photo... ";
if(class_exists('Imagick')){
$im = new Imagick($file);
$im->setImageOpacity(1.0);
//$im->resizeImage(147,200,Imagick::FILTER_UNDEFINED,0.5,TRUE);
//$im->setCompressionQuality(90);
$im->setImageFormat('jpeg');
$attrs['jpegphoto']=$im->getImageBlob();
} else {
echo "ERROR!";
}
$ret1=ldap_mod_add($ds,$info[0]["dn"],$attrs);
if ($ret1) {
echo "PHOTO CORRECTLY ADDED: $ret1\n";
} else {
echo "Error adding photo: $ret1" . ldap_error($ds) . " \n";
}
}
/*
for ($i=0; $i<$info["count"]; $i++ ) {
echo "dn is: ". $info[$i]["dn"] ."\n";
echo "i:"+$i."\n";
} *
*/
break;
default:
echo "MULTIPLE RESULTS FOUND! $file\n";
break;
}
/*
echo "Getting entries ...<p>";
$info = ldap_get_entries($ds, $sr);
echo "Data for ".$info["count"]." items returned:<p>";
for ($i=0; $i<$info["count"]; $i++ ) {
echo "dn is: ". $info[$i]["dn"] ."\n";
echo "i:"+$i."\n";
}
*/
}
echo "Closing connection";
ldap_close($ds);
?>