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';
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);
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);
68 header('Content-Type: text/html; charset=utf-8');
69 $smarty->display($template);
72 //--------------------------------------------------------------
75 * fetches the Data from the LDAP directory and assigns it to
76 * the global smarty object using tpl_entry()
78 function _fetchData($dn){
82 global $users; //contains the users for manager role
84 $sr = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)');
85 if(!ldap_count_entries($LDAP_CON,$sr)){
88 $result = ldap_get_binentries($LDAP_CON, $sr);
91 //remove dn from entry when copy
92 if(!empty($_REQUEST['mode']) && $_REQUEST['mode'] == 'copy'){
96 //assign entry to template:
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]]);
111 * saves the data from $_REQUEST['entry'] to the LDAP directory
113 * returns given or constructed dn
115 function _saveData(){
121 $entry = $_REQUEST['entry'];
122 $dn = $_REQUEST['dn'];
125 $newdn = 'uid='.$now;
126 if (empty($_REQUEST['type'])) { $_REQUEST['type']='public'; }
127 if($_REQUEST['type'] == 'private'){
128 $newdn .= ', '.$conf['privatebook'].', '.$_SESSION['ldapab']['binddn'];
130 $newdn .= ', '.$conf['publicbook'];
132 $entry['displayname'] = $entry['givenname'].' '.$entry['name'];;
133 $entry = prepare_ldap_entry($entry);
143 $entry['uid'][] = $now;
144 $r = ldap_add($LDAP_CON,$newdn,$entry);
148 // update the objectClasses
149 ldap_store_objectclasses($dn,$OCLASSES);
150 unset($entry['objectclass']);
152 //modify entry attribute by attribute - this ensure we don't delete unknown stuff
153 foreach (array_values($FIELDS) as $key){
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']){
164 $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
165 tpl_ldaperror("del $key");
168 $add[$key]=$entry[$key];
169 $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
170 tpl_ldaperror("mod $key");
178 * does as the name says - delete the whole entry
180 function _delEntry($dn){
182 if(ldap_full_delete($LDAP_CON,$dn,true)){
183 header("Location: index.php");
189 * gets the binary data from an uploaded file
191 function _getUploadData(){
192 $file = $_FILES['photoupload'];
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']);
199 unlink($file['tmp_name']);