]> git.sur5r.net Git - contagged/blob - entry.php
UTF-8 in interface
[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( $_SESSION[ldapab][username] &&
9      ($_REQUEST[mode]=='edit' || $_REQUEST[mode]=='copy')){
10     $template='entry_edit.tpl';
11   }elseif($_REQUEST[mode]=='vcf'){
12     $template='entry_vcf.tpl';
13   }else{
14     $template='entry_show.tpl';
15   }
16
17   $dn = $_REQUEST[dn];
18   #$dn = 'cn=bar foo, ou=contacts, o=cosmocode, c=de';
19
20   //save data if asked for
21   if($_SESSION[ldapab][username] && $_REQUEST[save]){
22     $_REQUEST[entry][jpegPhoto][]=_getUploadData();
23     $dn = _saveData();
24   }
25
26   if(empty($dn)){
27     if(!$_REQUEST[mode]=='edit'){
28       $smarty->assign('error','No dn was given');
29       $template = 'error.tpl';
30     }
31   }elseif($_REQUEST[del]){
32     _delEntry($dn);
33   }elseif(!_fetchData($dn)){
34     $smarty->assign('error',"The requested entry '$dn' was not found");
35     $template = 'error.tpl';
36   }
37
38   //prepare templates
39   $smarty->assign('dn',$dn);
40   $smarty->assign('managers',$users);
41   tpl_std();
42   tpl_orgs();
43   tpl_markers();
44   tpl_categories();
45   tpl_timezone();
46   tpl_country();
47   //display templates
48   if($_REQUEST[mode]=='vcf'){
49     $entry = $smarty->get_template_vars('entry');
50     $filename = $entry[givenname].'_'.$entry[name].'.vcf';
51     header("Content-Disposition: attachment; filename=\"$filename\"");
52     header("Content-type: text/x-vcard; name=\"$filename\"; charset=utf-8");
53     $smarty->display($template);
54   }else{
55     header('Content-Type: text/html; charset=utf-8');
56     $smarty->display('header.tpl');
57     $smarty->display($template);
58     $smarty->display('footer.tpl');
59   }
60
61   //--------------------------------------------------------------
62
63   /**
64    * fetches the Data from the LDAP directory and assigns it to
65    * the global smarty object using tpl_entry()
66    */
67   function _fetchData($dn){
68     global $LDAP_CON;
69     global $conf;
70     global $smarty;
71     global $users; //contains the users for manager role
72
73     $sr = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)');
74     if(!ldap_count_entries($LDAP_CON,$sr)){
75       return false;
76     }
77     $result = ldap_get_binentries($LDAP_CON, $sr);
78     $entry  = $result[0];
79
80     //remove dn from entry when copy
81     if($_REQUEST[mode] == 'copy'){
82       $entry[dn]='';
83     }
84
85     //assign entry to template:
86     tpl_entry($entry);
87
88 /*print '<pre>';
89 print_r($entry);
90 print '</pre>';*/
91
92     // make username from dn for manager:
93     $smarty->assign('managername',$users[$entry[manager][0]]);
94     return true;
95   }
96
97   /**
98    * saves the data from $_REQUEST[entry] to the LDAP directory
99    *
100    * returns given or constructed dn
101    */
102   function _saveData(){
103     global $LDAP_CON;
104     global $conf;
105     $entries = namedentries();
106     $entries['mail']='mail';  //special field mail isn't in entries so we add it here
107     if($conf[extended]){
108       $entries['marker']='marker'; //same for marker inextended schema
109     }
110
111     $entry = $_REQUEST[entry];
112     $dn    = $_REQUEST[dn];
113     //construct new dn
114     $now    = time();
115     $newdn  = 'uid='.$now;
116     if($_REQUEST[type] == 'private'){
117       $newdn .= ', '.$conf[privatebook].', '.$_SESSION[ldapab][binddn];
118     }else{
119       $newdn .= ', '.$conf[publicbook];
120     }
121     $entry[cn]          = $entry[givenname].' '.$entry[name];;
122     $entry = prepare_ldap_entry($entry);
123
124 /*print '<pre>';
125 print_r($entry);
126 print '</pre>';*/
127
128     if(empty($dn)){
129       //new entry
130       $entry[uid][] = $now;
131       $r = ldap_add($LDAP_CON,$newdn,$entry);
132       tpl_ldaperror();
133       return $newdn;
134     }else{
135       // in extended mode we have to make sure the right classes are set
136       if($conf[extended]){
137         ldap_store_objectclasses($dn,array('inetOrgPerson','contactPerson'));
138       }
139       // in openxchange mode we have to make sure the right classes are set
140       if ($conf[openxchange]){
141         ldap_store_objectclasses($dn,array('inetOrgPerson','OXUserObject'));
142       }
143       //modify entry (touches only our attributes)
144       foreach (array_keys($entries) as $key){
145         if($key == 'dn'){
146           continue;
147         }elseif(empty($entry[$key])){
148           if($key == 'jpegPhoto' && !$_REQUEST['delphoto']){
149             continue;
150           }
151           unset($del);
152           $del[$key]=array();
153           $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
154           tpl_ldaperror("del $key");
155         }else{
156           unset($add);
157           $add[$key]=$entry[$key];
158           $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
159           tpl_ldaperror("mod $key");
160         }
161       }
162       return $dn;
163     }
164   }
165
166   /**
167    * does as the name says - delete the whole entry
168    */
169   function _delEntry($dn){
170     global $LDAP_CON;
171     if(ldap_full_delete($LDAP_CON,$dn,true)){
172       header("Location: index.php");
173       exit;
174     }
175   }
176
177   /**
178    * gets the binary data from an uploaded file
179    */
180   function _getUploadData(){
181     $file = $_FILES[photoupload];
182
183     if (is_uploaded_file($file[tmp_name])) {
184       if(preg_match('=image/p?jpe?g=',$file[type])){
185         $fh = fopen($file[tmp_name],'r');
186         $data = fread($fh,$file[size]);
187         fclose($fh);
188         unlink($file[tmp_name]);
189         return $data;
190       }
191     }
192     return '';
193   }
194 ?>