]> git.sur5r.net Git - contagged/blob - inc/template.php
6042c7b9a75d89c1cad3fdb3bf6aeb455658c6de
[contagged] / inc / template.php
1 <?php
2
3 /**
4  * assigns some standard variables to smarty templates
5  */
6 function tpl_std(){
7   global $smarty;
8   global $lang;
9   global $conf;
10   global $FIELDS;
11
12   if(empty($_SESSION['ldapab']['username'])){
13     $_SESSION['ldapab']['username'] = '';
14   }
15   if(empty($_SESSION['ldapab']['binddn'])){
16     $_SESSION['ldapab']['binddn'] = '';
17   }
18
19   $smarty->assign('user',$_SESSION['ldapab']['username']);
20   $smarty->assign('binddn',$_SESSION['ldapab']['binddn']);
21   if(!empty($_SESSION['ldapab']['lastlocation'])){
22     $smarty->assign('home',$_SESSION['ldapab']['lastlocation']);
23   }else{
24      $smarty->assign('home','index.php');
25   }
26   $smarty->assign('conf',$conf);
27   $smarty->assign('lang',$lang);
28   $smarty->assign('fields',$FIELDS);
29   $smarty->assign('lettertabs',explode(' ',$lang['lettertabs']));
30 }
31
32 /**
33  * assigns all the interesting data from an ldap result to
34  * the smarty template
35  */
36 function tpl_entry($in){
37   global $smarty;
38   global $conf;
39   global $RFIELDS;
40
41   $out=array();
42
43   // handle named entries
44   foreach($RFIELDS as $key => $name){
45     if(empty($in[$key])) continue;
46
47     // keep arrays for multi fields
48     if($name{0} == '_'){
49         $name  = substr($name,1);
50         if(is_array($in[$key])){
51             $out[$name] = $in[$key];
52         }else{
53             $out[$name] = array($in[$key]);
54         }
55     }else{
56         if(is_array($in[$key])){
57             $out[$name] = $in[$key][0];
58         }else{
59             $out[$name] = $in[$key];
60         }
61     }
62   }
63
64   // set the type
65   if (empty($out['dn'])) { $out['dn']=''; }
66   $out['dn']          = normalize_dn($out['dn']);
67   $conf['publicbook'] = normalize_dn($conf['publicbook']);
68   if($out['dn']){
69       if(stristr($out['dn'],$conf['publicbook'])){
70           $out['type'] = 'public';
71       }else{
72           $out['type'] = 'private';
73       }
74   }
75
76   // join marker field to markers
77   if(is_array($out['marker'])) $out['markers'] = join(', ',$out['marker']);
78
79   $out['qrcode'] = tpl_qrcode($out);
80
81 /*
82 print '<pre>';
83 print_r($out);
84 print '</pre>';
85 */
86
87   $smarty->assign('entry',$out);
88 }
89
90 function tpl_qrcode($in){
91     $data = "BEGIN:VCARD\n";
92     $data .= "N:{$in['name']};{$in['givenname']}\n";
93     if($in['mobile'])    $data .= "TEL;CELL:{$in['mobile']}\n";
94     if($in['phone'])     $data .= "TEL;WORK:{$in['phone']}\n";
95     if($in['homephone']) $data .= "TEL;HOME:{$in['homephone']}\n";
96     if($in['mail'][0])   $data .= "EMAIL:{$in['mail'][0]}\n";
97     $data .= "END:VCARD";
98     $data = rawurlencode($data);
99
100     return 'http://chart.apis.google.com/chart?cht=qr&chld=L|5&chs=500x500&chl='.$data.'&.png';
101 }
102
103
104 /**
105  * assigns the last LDAP error to the template
106  */
107 function tpl_ldaperror($message=""){
108   global $LDAP_CON;
109   global $__LDAPERROR__;
110   global $smarty;
111   $errno = ldap_errno($LDAP_CON);
112   if($errno){
113     $__LDAPERROR__ .= ldap_err2str($errno);
114     if(!empty($message)){
115       $__LDAPERROR__ .= "($message)";
116     }elseif($errno == 4){
117       $__LDAPERROR__ .= "(You need to increase this limit in your server config)";
118     }
119     $__LDAPERROR__ .= '<br />';
120   }
121   $smarty->assign("LDAPERRORS",$__LDAPERROR__);
122 }
123
124 /**
125  * assigns all markers to the template
126  */
127 function tpl_markers(){
128   global $conf;
129   global $LDAP_CON;
130   global $smarty;
131
132   if(!$conf['extended']) return;
133
134   $markers = array();
135
136   $sr = ldap_list($LDAP_CON,$conf['publicbook'],"ObjectClass=inetOrgPerson",array("marker"));
137   $result1 = ldap_get_binentries($LDAP_CON, $sr);
138   //check users private addressbook
139   if(!empty($_SESSION['ldapab']['binddn']) && $conf['privatebook']){
140     $sr = @ldap_list($LDAP_CON,
141                     $conf['privatebook'].','.$_SESSION['ldapab']['binddn'],
142                     "ObjectClass=inetOrgPerson",array("marker"));
143     $result2 = ldap_get_binentries($LDAP_CON, $sr);
144   }else{
145     $result2 = '';
146   }
147   $result = array_merge((array)$result1,(array)$result2);
148
149   if(count($result)){
150     foreach ($result as $entry){
151       if(!empty($entry['marker']) && count($entry['marker'])){
152         foreach($entry['marker'] as $marker){
153           array_push($markers, $marker);
154         }
155       }
156     }
157   }
158   $markers = array_unique($markers);
159   sort($markers,SORT_STRING);
160
161   $smarty->assign('markers',$markers);
162 }
163
164 /**
165  * Assigns all distinct organization names to the template
166  */
167 function tpl_orgs(){
168   global $conf;
169   global $LDAP_CON;
170   global $smarty;
171   global $FIELDS;
172
173   $orgs = array();
174   $result = ldap_queryabooks("ObjectClass=inetOrgPerson",array($FIELDS['organization']));
175
176   if(count($result)){
177     foreach ($result as $entry){
178       if(!empty($entry[$FIELDS['organization']][0])){
179         array_push($orgs, $entry[$FIELDS['organization']][0]);
180       }
181     }
182   }
183   $orgs = array_unique($orgs);
184   natcasesort($orgs);
185   $smarty->assign('orgs',$orgs);
186 }
187
188 /**
189  * assigns all categories to the template
190  */
191 function tpl_categories(){
192   global $conf;
193   global $LDAP_CON;
194   global $smarty;
195
196   if(!$conf['openxchange']) return;
197
198   $categories = array();
199
200   $sr = ldap_list($LDAP_CON,$conf['publicbook'],"ObjectClass=OXUserObject",array("OXUserCategories"));
201   $result1 = ldap_get_binentries($LDAP_CON, $sr);
202   //check users private addressbook
203   if(!empty($_SESSION['ldapab']['binddn']) && $conf['privatebook']){
204     $sr = @ldap_list($LDAP_CON,
205                     $conf['privatebook'].','.$_SESSION['ldapab']['binddn'],
206                     "ObjectClass=OXUserObject",array("OXUserCategories"));
207     $result2 = ldap_get_binentries($LDAP_CON, $sr);
208   }
209   $result = array_merge((array)$result1,(array)$result2);
210
211   if(count($result)){
212     foreach ($result as $entry){
213       if(count($entry['OXUserCategories'])){
214         foreach($entry['OXUserCategories'] as $category){
215           array_push($categories, $category);
216         }
217       }
218     }
219   }
220   $categories = array_unique($categories);
221   sort($categories,SORT_STRING);
222
223   $smarty->assign('categories',$categories);
224 }
225
226 /**
227  * assigns all timezones to the template
228  */
229 function tpl_timezone(){
230   global $conf;
231   global $LDAP_CON;
232   global $smarty;
233
234   if(!$conf['openxchange']) return;
235
236   $timezone = array();
237
238   $sr = ldap_list($LDAP_CON,$conf['publicbook'],"ObjectClass=OXUserObject",array("OXTimeZone"));
239   $result1 = ldap_get_binentries($LDAP_CON, $sr);
240   //check users private addressbook
241   if(!empty($_SESSION['ldapab']['binddn']) && $conf['privatebook']){
242     $sr = @ldap_list($LDAP_CON,
243                     $conf['privatebook'].','.$_SESSION['ldapab']['binddn'],
244                     "ObjectClass=OXUserObject",array("OXTimeZone"));
245     $result2 = ldap_get_binentries($LDAP_CON, $sr);
246   }
247   $result = array_merge((array)$result1,(array)$result2);
248
249   if(count($result)){
250     foreach ($result as $entry){
251       if(count($entry['OXTimeZone'])){
252         foreach($entry['OXTimeZone'] as $tz){
253           array_push($timezone, $tz);
254         }
255       }
256     }
257   }
258   $timezone = array_unique($timezone);
259   sort($timezone,SORT_STRING);
260
261   $smarty->assign('timezone',$timezone);
262 }
263
264 /**
265  * assigns all countries to the template
266  */
267 function tpl_country(){
268   global $conf;
269   global $LDAP_CON;
270   global $smarty;
271
272   if(!$conf['openxchange']) return;
273
274   $country = array();
275
276   $sr = ldap_list($LDAP_CON,$conf['publicbook'],"ObjectClass=OXUserObject",array("userCountry"));
277   $result1 = ldap_get_binentries($LDAP_CON, $sr);
278   //check users private addressbook
279   if(!empty($_SESSION['ldapab']['binddn']) && $conf['privatebook']){
280     $sr = @ldap_list($LDAP_CON,
281                     $conf['privatebook'].','.$_SESSION['ldapab']['binddn'],
282                     "ObjectClass=OXUserObject",array("userCountry"));
283     $result2 = ldap_get_binentries($LDAP_CON, $sr);
284   }
285   $result = array_merge((array)$result1,(array)$result2);
286
287   if(count($result)){
288     foreach ($result as $entry){
289       if(count($entry['userCountry'])){
290         foreach($entry['userCountry'] as $c){
291           array_push($country, $c);
292         }
293       }
294     }
295   }
296   $country = array_unique($country);
297   sort($country,SORT_STRING);
298
299   $smarty->assign('country',$country);
300 }
301
302 ?>