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