]> git.sur5r.net Git - contagged/blob - entry.php
better enforcement of the userlogreq option
[contagged] / entry.php
1 <?php
2 require_once('inc/init.php');
3 ldap_login();
4
5 if ($conf['userlogreq'] && $user == ''){
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   if(!ldap_count_entries($LDAP_CON,$sr)){
94     return false;
95   }
96   $result = ldap_get_binentries($LDAP_CON, $sr);
97   $entry  = $result[0];
98
99   //remove dn from entry when copy
100   if(!empty($_REQUEST['mode']) && $_REQUEST['mode'] == 'copy'){
101     $entry['dn']='';
102   }
103
104   //assign entry to template:
105   tpl_entry($entry);
106
107 /*print '<pre>';
108 print_r($entry);
109 print '</pre>';*/
110
111   // make username from dn for manager:
112   if (empty($entry['manager'])) { $entry['manager']=array(""); }
113   if (empty($users[$entry['manager'][0]])) { $users[$entry['manager'][0]]=''; }
114   $smarty->assign('managername',$users[$entry['manager'][0]]);
115   return true;
116 }
117
118 /**
119  * saves the data from $_REQUEST['entry'] to the LDAP directory
120  *
121  * returns given or constructed dn
122  */
123 function _saveData(){
124   global $LDAP_CON;
125   global $conf;
126   global $FIELDS;
127   global $OCLASSES;
128
129   $entry = $_REQUEST['entry'];
130   $dn    = $_REQUEST['dn'];
131   //construct new dn
132   $now    = time();
133   $newdn  = 'uid='.$now;
134   if (empty($_REQUEST['type'])) { $_REQUEST['type']='public'; }
135   if($_REQUEST['type'] == 'private'){
136     $newdn .= ', '.$conf['privatebook'].', '.$_SESSION['ldapab']['binddn'];
137   }else{
138     $newdn .= ', '.$conf['publicbook'];
139   }
140   $entry['displayname'] = $entry['givenname'].' '.$entry['name'];;
141   $entry = prepare_ldap_entry($entry);
142
143 /*
144 print '<pre>';
145 print_r($entry);
146 print '</pre>';
147 */
148
149   if(empty($dn)){
150     //new entry
151     $entry['uid'][] = $now;
152     $r = ldap_add($LDAP_CON,$newdn,$entry);
153     tpl_ldaperror();
154     return $newdn;
155   }else{
156     // update the objectClasses
157     ldap_store_objectclasses($dn,$OCLASSES);
158     unset($entry['objectclass']);
159
160     //modify entry attribute by attribute - this ensure we don't delete unknown stuff
161     foreach (array_values($FIELDS) as $key){
162       if($key == 'dn'){
163         continue;
164       }elseif(empty($entry[$key])){
165         // field is empty -> handle deletion (except for photo unless deletion triggered)
166         if (empty($_REQUEST['delphoto'])) { $_REQUEST['delphoto']=0; }
167         if($key == 'jpegPhoto' && !$_REQUEST['delphoto']){
168           continue;
169         }
170         unset($del);
171         $del[$key]=array();
172         $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
173         tpl_ldaperror("del $key");
174       }else{
175         unset($add);
176         $add[$key]=$entry[$key];
177         $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
178         tpl_ldaperror("mod $key");
179       }
180     }
181
182     // special tag handling for Thunderbird
183     if($conf['tbtaghack'] && in_array('contactPerson',$OCLASSES)){
184         for($i=1;$i<5;$i++){
185             if(empty($entry["custom$i"])){
186                 // deletion
187                 unset($del);
188                 $del["custom$i"]=array();
189                 $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
190                 tpl_ldaperror("del custom$i");
191             }else{
192                 // modification
193                 unset($add);
194                 $add["custom$i"]=$entry["custom$i"];
195                 $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
196                 tpl_ldaperror("mod custom$i");
197             }
198         }
199     }
200
201
202     return $dn;
203   }
204 }
205
206 /**
207  * does as the name says - delete the whole entry
208  */
209 function _delEntry($dn){
210   global $LDAP_CON;
211   if(ldap_full_delete($LDAP_CON,$dn,true)){
212     header("Location: index.php");
213     exit;
214   }
215 }
216
217 /**
218  * gets the binary data from an uploaded file
219  */
220 function _getUploadData(){
221   $file = $_FILES['photoupload'];
222
223   if (is_uploaded_file($file['tmp_name'])) {
224     if(preg_match('=image/p?jpe?g=',$file['type'])){
225       $fh = fopen($file['tmp_name'],'r');
226       $data = fread($fh,$file['size']);
227       fclose($fh);
228       unlink($file['tmp_name']);
229       return $data;
230     }
231   }
232   return '';
233 }
234