]> git.sur5r.net Git - contagged/blob - entry.php
2bf488a7f15e3bff60579c19f1baf7bf37de9756
[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   $smarty->display($template);
75 }else{
76   header('Content-Type: text/html; charset=utf-8');
77   $smarty->display($template);
78 }
79
80 //--------------------------------------------------------------
81
82 /**
83  * fetches the Data from the LDAP directory and assigns it to
84  * the global smarty object using tpl_entry()
85  */
86 function _fetchData($dn){
87   global $LDAP_CON;
88   global $conf;
89   global $smarty;
90   global $users; //contains the users for manager role
91
92   $sr = @ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)');
93   tpl_ldaperror();
94   if(!@ldap_count_entries($LDAP_CON,$sr)){
95     return false;
96   }
97   $result = ldap_get_binentries($LDAP_CON, $sr);
98   $entry  = $result[0];
99
100   //remove dn from entry when copy
101   if(!empty($_REQUEST['mode']) && $_REQUEST['mode'] == 'copy'){
102     $entry['dn']='';
103   }
104
105   //assign entry to template:
106   tpl_entry($entry);
107
108 /*print '<pre>';
109 print_r($entry);
110 print '</pre>';*/
111
112   // make username from dn for manager:
113   if (empty($entry['manager'])) { $entry['manager']=array(""); }
114   if (empty($users[$entry['manager'][0]])) { $users[$entry['manager'][0]]=''; }
115   $smarty->assign('managername',$users[$entry['manager'][0]]);
116   return true;
117 }
118
119 /**
120  * saves the data from $_REQUEST['entry'] to the LDAP directory
121  *
122  * returns given or constructed dn
123  */
124 function _saveData(){
125   global $LDAP_CON;
126   global $conf;
127   global $FIELDS;
128   global $OCLASSES;
129
130   $entry = $_REQUEST['entry'];
131   $dn    = $_REQUEST['dn'];
132   //construct new dn
133   $new_uid = time().str_pad(mt_rand(0,99999999),8,"0", STR_PAD_LEFT);
134   $newdn   = 'uid='.$new_uid;
135   if (empty($_REQUEST['type'])) { $_REQUEST['type']='public'; }
136   if($_REQUEST['type'] == 'private' && $conf['privatebook']){
137     $newdn .= ','.$conf['privatebook'].','.$_SESSION['ldapab']['binddn'];
138   }else{
139     $newdn .= ','.$conf['publicbook'];
140   }
141   $entry['displayname'] = $entry['givenname'].' '.$entry['name'];;
142   $entry = prepare_ldap_entry($entry);
143
144 /*
145 print '<pre>';
146 print_r($entry);
147 print '</pre>';
148 */
149
150   if(empty($dn)){
151     //new entry
152     $entry['uid'][] = $new_uid;
153     $r = @ldap_add($LDAP_CON,$newdn,$entry);
154     tpl_ldaperror();
155     return $newdn;
156   }else{
157     // update the objectClasses
158     ldap_store_objectclasses($dn,$OCLASSES);
159     unset($entry['objectclass']);
160
161     //modify entry attribute by attribute - this ensure we don't delete unknown stuff
162     foreach (array_values($FIELDS) as $key){
163       if($key == 'dn'){
164         continue;
165       }elseif(empty($entry[$key])){
166         // field is empty -> handle deletion (except for photo unless deletion triggered)
167         if (empty($_REQUEST['delphoto'])) { $_REQUEST['delphoto']=0; }
168         if($key == 'jpegPhoto' && !$_REQUEST['delphoto']){
169           continue;
170         }
171         unset($del);
172         $del[$key]=array();
173         $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
174         tpl_ldaperror("del $key");
175       }else{
176         unset($add);
177         $add[$key]=$entry[$key];
178         $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
179         tpl_ldaperror("mod $key");
180       }
181     }
182
183     // special tag handling for Thunderbird
184     if($conf['tbtaghack'] && in_array('contactPerson',$OCLASSES)){
185         for($i=1;$i<5;$i++){
186             if(empty($entry["custom$i"])){
187                 // deletion
188                 unset($del);
189                 $del["custom$i"]=array();
190                 $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
191                 tpl_ldaperror("del custom$i");
192             }else{
193                 // modification
194                 unset($add);
195                 $add["custom$i"]=$entry["custom$i"];
196                 $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
197                 tpl_ldaperror("mod custom$i");
198             }
199         }
200     }
201
202
203     return $dn;
204   }
205 }
206
207 /**
208  * does as the name says - delete the whole entry
209  */
210 function _delEntry($dn){
211   global $LDAP_CON;
212   if(ldap_full_delete($LDAP_CON,$dn,true)){
213     header("Location: index.php");
214     exit;
215   }
216 }
217
218 /**
219  * gets the binary data from an uploaded file
220  */
221 function _getUploadData(){
222   global $smarty;
223   global $lang;
224   $file = $_FILES['photoupload'];
225
226   if (is_uploaded_file($file['tmp_name'])) {
227     if(preg_match('=image/p?jpe?g=',$file['type'])){
228       $fh = fopen($file['tmp_name'],'r');
229       $data = fread($fh,$file['size']);
230       fclose($fh);
231       unlink($file['tmp_name']);
232       return $data;
233     } else {
234       $smarty->assign('jpegError',$lang['err_wrongFileType']);
235     }
236   } elseif (preg_match('/http:\/\//', $_REQUEST["photo"])) {
237     $fd = fopen($_REQUEST["photo"], "rb");
238     $data = '';
239     while (!feof($fd)) {
240       $data .= fread($fd, 8192);
241     }
242     fclose($fd);
243     return $data;
244   } else {
245     $smarty->assign('jpegError',$lang['err_fileNotUploaded']);
246   }
247   return '';
248 }
249