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