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