PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/template.php

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