]> git.sur5r.net Git - contagged/blob - entry.php
PHP5 notice cleanups
[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']['jpegPhoto'][]=_getUploadData();
29     $_REQUEST['entry']['marker'] = explode(',',$_REQUEST['entry']['markers']);
30     $_REQUEST['entry']['marker'] = array_map('trim',$_REQUEST['entry']['marker']);
31     $_REQUEST['entry']['marker'] = array_unique($_REQUEST['entry']['marker']);
32     $_REQUEST['entry']['marker'] = array_filter($_REQUEST['entry']['marker']);
33     sort($_REQUEST['entry']['marker']);
34     unset($_REQUEST['entry']['markers']);
35     
36     $_REQUEST['entry']['mail'] = array_map('trim',$_REQUEST['entry']['mail']);
37     $_REQUEST['entry']['mail'] = array_unique($_REQUEST['entry']['mail']);
38     $_REQUEST['entry']['mail'] = array_filter($_REQUEST['entry']['mail']);
39     sort($_REQUEST['entry']['mail']);
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     $entries = namedentries();
124     $entries['mail']='mail';  //special field mail isn't in entries so we add it here
125     if($conf['extended']){
126       $entries['marker']='marker'; //same for marker in extended schema
127     }
128
129     $entry = $_REQUEST['entry'];
130     $dn    = $_REQUEST['dn'];
131     //construct new dn
132     $now    = time();
133     $newdn  = 'uid='.$now;
134     if (empty($_REQUEST['type'])) { $_REQUEST['type']='public'; }
135     if($_REQUEST['type'] == 'private'){
136       $newdn .= ', '.$conf['privatebook'].', '.$_SESSION['ldapab']['binddn'];
137     }else{
138       $newdn .= ', '.$conf['publicbook'];
139     }
140     $entry['cn']          = $entry['givenname'].' '.$entry['name'];;
141     $entry = prepare_ldap_entry($entry);
142
143 /*
144 print '<pre>';
145 print_r($entry);
146 print '</pre>';
147 */
148
149     if(empty($dn)){
150       //new entry
151       $entry['uid'][] = $now;
152       $r = ldap_add($LDAP_CON,$newdn,$entry);
153       tpl_ldaperror();
154       return $newdn;
155     }else{
156       // in extended mode we have to make sure the right classes are set
157       if($conf['extended']){
158         ldap_store_objectclasses($dn,array('inetOrgPerson','contactPerson'));
159       }
160       // in openxchange mode we have to make sure the right classes are set
161       if ($conf['openxchange']){
162         ldap_store_objectclasses($dn,array('inetOrgPerson','OXUserObject'));
163       }
164       //modify entry (touches only our attributes)
165       foreach (array_keys($entries) as $key){
166         if($key == 'dn'){
167           continue;
168         }elseif(empty($entry[$key])){
169           if (empty($_REQUEST['delphoto'])) { $_REQUEST['delphoto']=0; }
170           if($key == 'jpegPhoto' && !$_REQUEST['delphoto']){
171             continue;
172           }
173           unset($del);
174           $del[$key]=array();
175           $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
176           tpl_ldaperror("del $key");
177         }else{
178           unset($add);
179           $add[$key]=$entry[$key];
180           $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
181           tpl_ldaperror("mod $key");
182         }
183       }
184       return $dn;
185     }
186   }
187
188   /**
189    * does as the name says - delete the whole entry
190    */
191   function _delEntry($dn){
192     global $LDAP_CON;
193     if(ldap_full_delete($LDAP_CON,$dn,true)){
194       header("Location: index.php");
195       exit;
196     }
197   }
198
199   /**
200    * gets the binary data from an uploaded file
201    */
202   function _getUploadData(){
203     $file = $_FILES['photoupload'];
204
205     if (is_uploaded_file($file['tmp_name'])) {
206       if(preg_match('=image/p?jpe?g=',$file['type'])){
207         $fh = fopen($file['tmp_name'],'r');
208         $data = fread($fh,$file['size']);
209         fclose($fh);
210         unlink($file['tmp_name']);
211         return $data;
212       }
213     }
214     return '';
215   }
216 ?>