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