2 require_once('inc/init.php');
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';
17 $template='entry_show.tpl';
20 if (empty($_REQUEST['dn'])) {
23 $dn = $_REQUEST['dn'];
24 #$dn = 'cn=bar foo, ou=contacts, o=cosmocode, c=de';
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']);
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]);
48 if(!$_REQUEST['mode']=='edit'){
49 $smarty->assign('error','No dn was given');
50 $template = 'error.tpl';
52 }elseif(!empty($_REQUEST['del']) && $_REQUEST['del']){
54 }elseif(!_fetchData($dn)){
55 $smarty->assign('error',"The requested entry '$dn' was not found");
56 $template = 'error.tpl';
60 $smarty->assign('dn',$dn);
61 $smarty->assign('managers',$users);
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);
71 header('Content-Type: text/html; charset=utf-8');
72 $smarty->display($template);
75 //--------------------------------------------------------------
78 * fetches the Data from the LDAP directory and assigns it to
79 * the global smarty object using tpl_entry()
81 function _fetchData($dn){
85 global $users; //contains the users for manager role
87 $sr = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)');
88 if(!ldap_count_entries($LDAP_CON,$sr)){
91 $result = ldap_get_binentries($LDAP_CON, $sr);
94 //remove dn from entry when copy
95 if(!empty($_REQUEST['mode']) && $_REQUEST['mode'] == 'copy'){
99 //assign entry to template:
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]]);
114 * saves the data from $_REQUEST['entry'] to the LDAP directory
116 * returns given or constructed dn
118 function _saveData(){
124 $entry = $_REQUEST['entry'];
125 $dn = $_REQUEST['dn'];
128 $newdn = 'uid='.$now;
129 if (empty($_REQUEST['type'])) { $_REQUEST['type']='public'; }
130 if($_REQUEST['type'] == 'private'){
131 $newdn .= ', '.$conf['privatebook'].', '.$_SESSION['ldapab']['binddn'];
133 $newdn .= ', '.$conf['publicbook'];
135 $entry['displayname'] = $entry['givenname'].' '.$entry['name'];;
136 $entry = prepare_ldap_entry($entry);
146 $entry['uid'][] = $now;
147 $r = ldap_add($LDAP_CON,$newdn,$entry);
151 // update the objectClasses
152 ldap_store_objectclasses($dn,$OCLASSES);
153 unset($entry['objectclass']);
155 //modify entry attribute by attribute - this ensure we don't delete unknown stuff
156 foreach (array_values($FIELDS) as $key){
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']){
167 $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
168 tpl_ldaperror("del $key");
171 $add[$key]=$entry[$key];
172 $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
173 tpl_ldaperror("mod $key");
181 * does as the name says - delete the whole entry
183 function _delEntry($dn){
185 if(ldap_full_delete($LDAP_CON,$dn,true)){
186 header("Location: index.php");
192 * gets the binary data from an uploaded file
194 function _getUploadData(){
195 $file = $_FILES['photoupload'];
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']);
202 unlink($file['tmp_name']);