]> git.sur5r.net Git - contagged/blob - entry.php
mod_auth_ldap, non-anon binding and openxchange support
[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\"");
53     $smarty->display($template);
54   }else{
55     $smarty->display('header.tpl');
56     $smarty->display($template);
57     $smarty->display('footer.tpl');
58   }
59
60   //--------------------------------------------------------------
61
62   /**
63    * fetches the Data from the LDAP directory and assigns it to
64    * the global smarty object using tpl_entry()
65    */
66   function _fetchData($dn){
67     global $LDAP_CON;
68     global $conf;
69     global $smarty;
70     global $users; //contains the users for manager role
71
72     $sr = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)');
73     if(!ldap_count_entries($LDAP_CON,$sr)){
74       return false;
75     }
76     $result = ldap_get_binentries($LDAP_CON, $sr);
77     $entry  = $result[0];
78
79     //remove dn from entry when copy
80     if($_REQUEST[mode] == 'copy'){
81       $entry[dn]='';
82     }
83
84     //assign entry to template:
85     tpl_entry($entry);
86
87 /*print '<pre>';
88 print_r($entry);
89 print '</pre>';*/
90
91     // make username from dn for manager:
92     $smarty->assign('managername',$users[$entry[manager][0]]);
93     return true;
94   }
95
96   /**
97    * saves the data from $_REQUEST[entry] to the LDAP directory
98    *
99    * returns given or constructed dn
100    */
101   function _saveData(){
102     global $LDAP_CON;
103     global $conf;
104     $entries = namedentries();
105     $entries['mail']='mail';  //special field mail isn't in entries so we add it here
106     if($conf[extended]){
107       $entries['marker']='marker'; //same for marker inextended schema
108     }
109
110     $entry = $_REQUEST[entry];
111     $dn    = $_REQUEST[dn];
112     //construct new dn
113     $now    = time();
114     $newdn  = 'uid='.$now;
115     if($_REQUEST[type] == 'private'){
116       $newdn .= ', '.$conf[privatebook].', '.$_SESSION[ldapab][binddn];
117     }else{
118       $newdn .= ', '.$conf[publicbook];
119     }
120     $entry[cn]          = $entry[givenname].' '.$entry[name];;
121     $entry = prepare_ldap_entry($entry);
122
123 /*print '<pre>';
124 print_r($entry);
125 print '</pre>';*/
126
127     if(empty($dn)){
128       //new entry
129       $entry[uid][] = $now;
130       $r = ldap_add($LDAP_CON,$newdn,$entry);
131       tpl_ldaperror();
132       return $newdn;
133     }else{
134       // in extended mode we have to make sure the right classes are set
135       if($conf[extended]){
136         ldap_store_objectclasses($dn,array('inetOrgPerson','contactPerson'));
137       }
138       // in openxchange mode we have to make sure the right classes are set
139       if ($conf[openxchange]){
140         ldap_store_objectclasses($dn,array('inetOrgPerson','OXUserObject'));
141       }
142       //modify entry (touches only our attributes)
143       foreach (array_keys($entries) as $key){
144         if($key == 'dn'){
145           continue;
146         }elseif(empty($entry[$key])){
147           if($key == 'jpegPhoto' && !$_REQUEST['delphoto']){
148             continue;
149           }
150           unset($del);
151           $del[$key]=array();
152           $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
153           tpl_ldaperror("del $key");
154         }else{
155           unset($add);
156           $add[$key]=$entry[$key];
157           $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
158           tpl_ldaperror("mod $key");
159         }
160       }
161       return $dn;
162     }
163   }
164
165   /**
166    * does as the name says - delete the whole entry
167    */
168   function _delEntry($dn){
169     global $LDAP_CON;
170     if(ldap_full_delete($LDAP_CON,$dn,true)){
171       header("Location: index.php");
172       exit;
173     }
174   }
175
176   /**
177    * gets the binary data from an uploaded file
178    */
179   function _getUploadData(){
180     $file = $_FILES[photoupload];
181
182     if (is_uploaded_file($file[tmp_name])) {
183       if(preg_match('=image/p?jpe?g=',$file[type])){
184         $fh = fopen($file[tmp_name],'r');
185         $data = fread($fh,$file[size]);
186         fclose($fh);
187         unlink($file[tmp_name]);
188         return $data;
189       }
190     }
191     return '';
192   }
193 ?>