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 $_REQUEST['entry']['marker'] = explode(',',$_REQUEST['entry']['markers']);
32 unset($_REQUEST['entry']['markers']);
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]);
47 if(!$_REQUEST['mode']=='edit'){
48 $smarty->assign('error','No dn was given');
49 $template = 'error.tpl';
51 }elseif(!empty($_REQUEST['del']) && $_REQUEST['del']){
53 }elseif(!_fetchData($dn)){
54 $smarty->assign('error',"The requested entry '$dn' was not found");
55 $template = 'error.tpl';
59 $smarty->assign('dn',$dn);
60 $smarty->assign('managers',$users);
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);
70 header('Content-Type: text/html; charset=utf-8');
71 $smarty->display($template);
74 //--------------------------------------------------------------
77 * fetches the Data from the LDAP directory and assigns it to
78 * the global smarty object using tpl_entry()
80 function _fetchData($dn){
84 global $users; //contains the users for manager role
86 $sr = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)');
87 if(!ldap_count_entries($LDAP_CON,$sr)){
90 $result = ldap_get_binentries($LDAP_CON, $sr);
93 //remove dn from entry when copy
94 if(!empty($_REQUEST['mode']) && $_REQUEST['mode'] == 'copy'){
98 //assign entry to template:
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]]);
113 * saves the data from $_REQUEST['entry'] to the LDAP directory
115 * returns given or constructed dn
117 function _saveData(){
123 $entry = $_REQUEST['entry'];
124 $dn = $_REQUEST['dn'];
127 $newdn = 'uid='.$now;
128 if (empty($_REQUEST['type'])) { $_REQUEST['type']='public'; }
129 if($_REQUEST['type'] == 'private'){
130 $newdn .= ', '.$conf['privatebook'].', '.$_SESSION['ldapab']['binddn'];
132 $newdn .= ', '.$conf['publicbook'];
134 $entry['displayname'] = $entry['givenname'].' '.$entry['name'];;
135 $entry = prepare_ldap_entry($entry);
145 $entry['uid'][] = $now;
146 $r = ldap_add($LDAP_CON,$newdn,$entry);
150 // update the objectClasses
151 ldap_store_objectclasses($dn,$OCLASSES);
152 unset($entry['objectclass']);
154 //modify entry attribute by attribute - this ensure we don't delete unknown stuff
155 foreach (array_values($FIELDS) as $key){
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']){
166 $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
167 tpl_ldaperror("del $key");
170 $add[$key]=$entry[$key];
171 $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
172 tpl_ldaperror("mod $key");
180 * does as the name says - delete the whole entry
182 function _delEntry($dn){
184 if(ldap_full_delete($LDAP_CON,$dn,true)){
185 header("Location: index.php");
191 * gets the binary data from an uploaded file
193 function _getUploadData(){
194 $file = $_FILES['photoupload'];
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']);
201 unlink($file['tmp_name']);