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