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