]> git.sur5r.net Git - contagged/blob - entry.php
fix missing space in company address
[contagged] / entry.php
1 <?php
2 require_once('inc/init.php');
3 ldap_login();
4
5 if ($conf['userlogreq'] && !isset($_SESSION['ldapab']['username'])){
6   header('Location: login.php');
7   exit();
8 }
9
10 $users = get_users();
11
12 //select template to use
13 if (empty($_REQUEST['mode'])) { $_REQUEST['mode']='show'; }
14 if( $_SESSION['ldapab']['username'] &&
15    ($_REQUEST['mode']=='edit' || $_REQUEST['mode']=='copy')){
16   $template='entry_edit.tpl';
17 }elseif($_REQUEST['mode']=='vcf'){
18   $template='entry_vcf.tpl';
19 }elseif($_REQUEST['mode']=='map'){
20   $template='entry_map.tpl';
21 }else{
22   $template='entry_show.tpl';
23 }
24
25 if (empty($_REQUEST['dn'])) {
26   $dn = "";
27 }else{
28   $dn = $_REQUEST['dn'];
29   #$dn = 'cn=bar foo, ou=contacts, o=cosmocode, c=de';
30 }
31
32 //save data if asked for
33 if($_SESSION['ldapab']['username'] && !empty($_REQUEST['save']) && $_REQUEST['save']){
34   // prepare special data
35   $_REQUEST['entry']['photo']  = _getUploadData();
36   if($_REQUEST['entry']['markers'])
37     $_REQUEST['entry']['marker'] = explode(',',$_REQUEST['entry']['markers']);
38   unset($_REQUEST['entry']['markers']);
39
40   foreach(array_keys($_REQUEST['entry']) as $field){
41       if($FIELDS['_'.$field]){
42           // entry has to be handled as array -> clean it up (trim, unique, sort)
43           $_REQUEST['entry'][$field] = array_map('trim',$_REQUEST['entry'][$field]);
44           $_REQUEST['entry'][$field] = array_unique($_REQUEST['entry'][$field]);
45           $_REQUEST['entry'][$field] = array_filter($_REQUEST['entry'][$field]);
46           natcasesort($_REQUEST['entry'][$field]);
47       }
48   }
49   $dn = _saveData();
50 }
51
52 if(empty($dn)){
53   if(!$_REQUEST['mode']=='edit'){
54     $smarty->assign('error','No dn was given');
55     $template = 'error.tpl';
56   }
57 }elseif(!empty($_REQUEST['del']) && $_REQUEST['del']){
58   _delEntry($dn);
59 }elseif(!_fetchData($dn)){
60   $smarty->assign('error',"The requested entry '$dn' was not found");
61   $template = 'error.tpl';
62 }
63
64 //prepare templates
65 $smarty->assign('dn',$dn);
66 $smarty->assign('managers',$users);
67 tpl_std();
68 //display templates
69 if($_REQUEST['mode']=='vcf'){
70   $entry = $smarty->get_template_vars('entry');
71   $filename = $entry['givenname'].'_'.$entry['name'].'.vcf';
72   header("Content-Disposition: attachment; filename=\"$filename\"");
73   header("Content-type: text/x-vcard; name=\"$filename\"; charset=utf-8");
74   $output = $smarty->fetch($template) . "\n";
75   $output = str_replace("\n", "\r\n", $output);
76   echo $output;
77 }else{
78   if($_REQUEST['mode']=='map'){
79     $entry = $smarty->get_template_vars('entry');
80
81     require_once 'inc/Geocoder.php';
82     $geocoder = new Geocoder();
83     $coords = array();
84     addCoords($coords, $entry, $geocoder);
85     $smarty->assign('coords', $coords);
86   }
87
88   header('Content-Type: text/html; charset=utf-8');
89   $smarty->display($template);
90 }
91
92 //--------------------------------------------------------------
93
94 /**
95  * fetches the Data from the LDAP directory and assigns it to
96  * the global smarty object using tpl_entry()
97  */
98 function _fetchData($dn){
99   global $LDAP_CON;
100   global $conf;
101   global $smarty;
102   global $users; //contains the users for manager role
103
104   $sr = @ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)');
105   tpl_ldaperror();
106   if(!@ldap_count_entries($LDAP_CON,$sr)){
107     return false;
108   }
109   $result = ldap_get_binentries($LDAP_CON, $sr);
110   $entry  = $result[0];
111
112   //remove dn from entry when copy
113   if(!empty($_REQUEST['mode']) && $_REQUEST['mode'] == 'copy'){
114     $entry['dn']='';
115   }
116
117   //assign entry to template:
118   tpl_entry($entry);
119
120 /*print '<pre>';
121 print_r($entry);
122 print '</pre>';*/
123
124   // make username from dn for manager:
125   if (empty($entry['manager'])) { $entry['manager']=array(""); }
126   if (empty($users[$entry['manager'][0]])) { $users[$entry['manager'][0]]=''; }
127   $smarty->assign('managername',$users[$entry['manager'][0]]);
128   return true;
129 }
130
131 /**
132  * saves the data from $_REQUEST['entry'] to the LDAP directory
133  *
134  * returns given or constructed dn
135  */
136 function _saveData(){
137   global $LDAP_CON;
138   global $conf;
139   global $FIELDS;
140   global $OCLASSES;
141
142   $entry = $_REQUEST['entry'];
143   $dn    = $_REQUEST['dn'];
144   //construct new dn
145   $new_uid = time().str_pad(mt_rand(0,99999999),8,"0", STR_PAD_LEFT);
146   $newdn   = 'uid='.$new_uid;
147   if (empty($_REQUEST['type'])) { $_REQUEST['type']='public'; }
148   if($_REQUEST['type'] == 'private' && $conf['privatebook']){
149     $newdn .= ','.$conf['privatebook'].','.$_SESSION['ldapab']['binddn'];
150   }else{
151     $newdn .= ','.$conf['publicbook'];
152   }
153   $entry['displayname'] = $entry['givenname'].' '.$entry['name'];;
154   $entry = prepare_ldap_entry($entry);
155
156 /*
157 print '<pre>';
158 print_r($entry);
159 print '</pre>';
160 */
161
162   if(empty($dn)){
163     //new entry
164     $entry['uid'][] = $new_uid;
165     $r = @ldap_add($LDAP_CON,$newdn,$entry);
166     tpl_ldaperror();
167     return $newdn;
168   }else{
169     // update the objectClasses
170     ldap_store_objectclasses($dn,$OCLASSES);
171     unset($entry['objectclass']);
172
173     //modify entry attribute by attribute - this ensure we don't delete unknown stuff
174     foreach (array_values($FIELDS) as $key){
175       if($key == 'dn'){
176         continue;
177       }elseif(empty($entry[$key])){
178         // field is empty -> handle deletion (except for photo unless deletion triggered)
179         if (empty($_REQUEST['delphoto'])) { $_REQUEST['delphoto']=0; }
180         if($key == 'jpegPhoto' && !$_REQUEST['delphoto']){
181           continue;
182         }
183         unset($del);
184         $del[$key]=array();
185         $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
186         tpl_ldaperror("del $key");
187       }else{
188         unset($add);
189         $add[$key]=$entry[$key];
190         $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
191         tpl_ldaperror("mod $key");
192       }
193     }
194
195     // special tag handling for Thunderbird
196     if($conf['tbtaghack'] && in_array('contactPerson',$OCLASSES)){
197         for($i=1;$i<5;$i++){
198             if(empty($entry["custom$i"])){
199                 // deletion
200                 unset($del);
201                 $del["custom$i"]=array();
202                 $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
203                 tpl_ldaperror("del custom$i");
204             }else{
205                 // modification
206                 unset($add);
207                 $add["custom$i"]=$entry["custom$i"];
208                 $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
209                 tpl_ldaperror("mod custom$i");
210             }
211         }
212     }
213
214
215     return $dn;
216   }
217 }
218
219 /**
220  * does as the name says - delete the whole entry
221  */
222 function _delEntry($dn){
223   global $LDAP_CON;
224   if(ldap_full_delete($LDAP_CON,$dn,true)){
225     header("Location: index.php");
226     exit;
227   }
228 }
229
230 /**
231  * gets the binary data from an uploaded file
232  */
233 function _getUploadData(){
234   global $smarty;
235   global $lang;
236   $file = $_FILES['photoupload'];
237
238   if (is_uploaded_file($file['tmp_name'])) {
239     if(preg_match('=image/p?jpe?g=',$file['type'])){
240       $fh = fopen($file['tmp_name'],'r');
241       $data = fread($fh,$file['size']);
242       fclose($fh);
243       unlink($file['tmp_name']);
244       return $data;
245     } else {
246       $smarty->assign('jpegError',$lang['err_wrongFileType']);
247     }
248   } elseif (preg_match('/http:\/\//', $_REQUEST["photo"])) {
249     $fd = fopen($_REQUEST["photo"], "rb");
250     $data = '';
251     while (!feof($fd)) {
252       $data .= fread($fd, 8192);
253     }
254     fclose($fd);
255     return $data;
256   } else {
257     $smarty->assign('jpegError',$lang['err_fileNotUploaded']);
258   }
259   return '';
260 }
261