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