]> git.sur5r.net Git - contagged/blob - entry.php
reverted deafult modifier, added shortcut for manual escaping
[contagged] / entry.php
1 <?
2   require_once('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   }else{
15     $template='entry_show.tpl';
16   }
17
18   if (empty($_REQUEST['dn'])) {
19     $dn = "";
20   }else{
21     $dn = $_REQUEST['dn'];
22     #$dn = 'cn=bar foo, ou=contacts, o=cosmocode, c=de';
23   }
24
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']);
31
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]);
39         }
40     }
41     $dn = _saveData();
42   }
43
44   if(empty($dn)){
45     if(!$_REQUEST['mode']=='edit'){
46       $smarty->assign('error','No dn was given');
47       $template = 'error.tpl';
48     }
49   }elseif(!empty($_REQUEST['del']) && $_REQUEST['del']){
50     _delEntry($dn);
51   }elseif(!_fetchData($dn)){
52     $smarty->assign('error',"The requested entry '$dn' was not found");
53     $template = 'error.tpl';
54   }
55
56   //prepare templates
57   $smarty->assign('dn',$dn);
58   $smarty->assign('managers',$users);
59   tpl_std();
60   tpl_orgs();
61   tpl_markers();
62   tpl_categories();
63   tpl_timezone();
64   tpl_country();
65   //display templates
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);
72   }else{
73     header('Content-Type: text/html; charset=utf-8');
74     $smarty->display($template);
75   }
76
77   //--------------------------------------------------------------
78
79   /**
80    * fetches the Data from the LDAP directory and assigns it to
81    * the global smarty object using tpl_entry()
82    */
83   function _fetchData($dn){
84     global $LDAP_CON;
85     global $conf;
86     global $smarty;
87     global $users; //contains the users for manager role
88
89     $sr = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)');
90     if(!ldap_count_entries($LDAP_CON,$sr)){
91       return false;
92     }
93     $result = ldap_get_binentries($LDAP_CON, $sr);
94     $entry  = $result[0];
95
96     //remove dn from entry when copy
97     if(!empty($_REQUEST['mode']) && $_REQUEST['mode'] == 'copy'){
98       $entry['dn']='';
99     }
100
101     //assign entry to template:
102     tpl_entry($entry);
103
104 /*print '<pre>';
105 print_r($entry);
106 print '</pre>';*/
107
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]]);
112     return true;
113   }
114
115   /**
116    * saves the data from $_REQUEST['entry'] to the LDAP directory
117    *
118    * returns given or constructed dn
119    */
120   function _saveData(){
121     global $LDAP_CON;
122     global $conf;
123     global $FIELDS;
124     global $OCLASSES;
125
126     $entry = $_REQUEST['entry'];
127     $dn    = $_REQUEST['dn'];
128     //construct new dn
129     $now    = time();
130     $newdn  = 'uid='.$now;
131     if (empty($_REQUEST['type'])) { $_REQUEST['type']='public'; }
132     if($_REQUEST['type'] == 'private'){
133       $newdn .= ', '.$conf['privatebook'].', '.$_SESSION['ldapab']['binddn'];
134     }else{
135       $newdn .= ', '.$conf['publicbook'];
136     }
137     $entry['displayname'] = $entry['givenname'].' '.$entry['name'];;
138     $entry = prepare_ldap_entry($entry);
139
140 /*
141 print '<pre>';
142 print_r($entry);
143 print '</pre>';
144 */
145
146     if(empty($dn)){
147       //new entry
148       $entry['uid'][] = $now;
149       $r = ldap_add($LDAP_CON,$newdn,$entry);
150       tpl_ldaperror();
151       return $newdn;
152     }else{
153       // update the objectClasses
154       ldap_store_objectclasses($dn,$OCLASSES);
155       unset($entry['objectclass']);
156
157       //modify entry attribute by attribute - this ensure we don't delete unknown stuff
158       foreach (array_values($FIELDS) as $key){
159         if($key == 'dn'){
160           continue;
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']){
165             continue;
166           }
167           unset($del);
168           $del[$key]=array();
169           $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
170           tpl_ldaperror("del $key");
171         }else{
172           unset($add);
173           $add[$key]=$entry[$key];
174           $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
175           tpl_ldaperror("mod $key");
176         }
177       }
178       return $dn;
179     }
180   }
181
182   /**
183    * does as the name says - delete the whole entry
184    */
185   function _delEntry($dn){
186     global $LDAP_CON;
187     if(ldap_full_delete($LDAP_CON,$dn,true)){
188       header("Location: index.php");
189       exit;
190     }
191   }
192
193   /**
194    * gets the binary data from an uploaded file
195    */
196   function _getUploadData(){
197     $file = $_FILES['photoupload'];
198
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']);
203         fclose($fh);
204         unlink($file['tmp_name']);
205         return $data;
206       }
207     }
208     return '';
209   }
210