2 require_once('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';
15 $template='entry_show.tpl';
18 if (empty($_REQUEST['dn'])) {
21 $dn = $_REQUEST['dn'];
22 #$dn = 'cn=bar foo, ou=contacts, o=cosmocode, c=de';
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']);
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]);
45 if(!$_REQUEST['mode']=='edit'){
46 $smarty->assign('error','No dn was given');
47 $template = 'error.tpl';
49 }elseif(!empty($_REQUEST['del']) && $_REQUEST['del']){
51 }elseif(!_fetchData($dn)){
52 $smarty->assign('error',"The requested entry '$dn' was not found");
53 $template = 'error.tpl';
57 $smarty->assign('dn',$dn);
58 $smarty->assign('managers',$users);
66 if($_REQUEST['mode']=='vcf'){
67 $entry = $smarty->get_template_vars('entry');
68 $filename = $entry['givenname'].'_'.$entry['name'].'.vcf';
69 header("Content-Disposition: attachment; filename=\"$filename\"");
70 header("Content-type: text/x-vcard; name=\"$filename\"; charset=utf-8");
71 $smarty->display($template);
73 header('Content-Type: text/html; charset=utf-8');
74 $smarty->display($template);
77 //--------------------------------------------------------------
80 * fetches the Data from the LDAP directory and assigns it to
81 * the global smarty object using tpl_entry()
83 function _fetchData($dn){
87 global $users; //contains the users for manager role
89 $sr = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)');
90 if(!ldap_count_entries($LDAP_CON,$sr)){
93 $result = ldap_get_binentries($LDAP_CON, $sr);
96 //remove dn from entry when copy
97 if(!empty($_REQUEST['mode']) && $_REQUEST['mode'] == 'copy'){
101 //assign entry to template:
108 // make username from dn for manager:
109 if (empty($entry['manager'])) { $entry['manager']=array(""); }
110 if (empty($users[$entry['manager'][0]])) { $users[$entry['manager'][0]]=''; }
111 $smarty->assign('managername',$users[$entry['manager'][0]]);
116 * saves the data from $_REQUEST['entry'] to the LDAP directory
118 * returns given or constructed dn
120 function _saveData(){
126 $entry = $_REQUEST['entry'];
127 $dn = $_REQUEST['dn'];
130 $newdn = 'uid='.$now;
131 if (empty($_REQUEST['type'])) { $_REQUEST['type']='public'; }
132 if($_REQUEST['type'] == 'private'){
133 $newdn .= ', '.$conf['privatebook'].', '.$_SESSION['ldapab']['binddn'];
135 $newdn .= ', '.$conf['publicbook'];
137 $entry['displayname'] = $entry['givenname'].' '.$entry['name'];;
138 $entry = prepare_ldap_entry($entry);
148 $entry['uid'][] = $now;
149 $r = ldap_add($LDAP_CON,$newdn,$entry);
153 // update the objectClasses
154 ldap_store_objectclasses($dn,$OCLASSES);
155 unset($entry['objectclass']);
157 //modify entry attribute by attribute - this ensure we don't delete unknown stuff
158 foreach (array_values($FIELDS) as $key){
161 }elseif(empty($entry[$key])){
162 // field is empty -> handle deletion (except for photo unless deletion triggered)
163 if (empty($_REQUEST['delphoto'])) { $_REQUEST['delphoto']=0; }
164 if($key == 'jpegPhoto' && !$_REQUEST['delphoto']){
169 $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
170 tpl_ldaperror("del $key");
173 $add[$key]=$entry[$key];
174 $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
175 tpl_ldaperror("mod $key");
183 * does as the name says - delete the whole entry
185 function _delEntry($dn){
187 if(ldap_full_delete($LDAP_CON,$dn,true)){
188 header("Location: index.php");
194 * gets the binary data from an uploaded file
196 function _getUploadData(){
197 $file = $_FILES['photoupload'];
199 if (is_uploaded_file($file['tmp_name'])) {
200 if(preg_match('=image/p?jpe?g=',$file['type'])){
201 $fh = fopen($file['tmp_name'],'r');
202 $data = fread($fh,$file['size']);
204 unlink($file['tmp_name']);