PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/opt/gemeinschaft/htdocs/prov/tiptel/pb.php

https://github.com/hehol/GemeinschaftPBX
PHP | 327 lines | 230 code | 60 blank | 37 comment | 22 complexity | 6bd68453a700112e7196730acdcdf711 MD5 | raw file
  1. <?php
  2. /*******************************************************************\
  3. * Gemeinschaft - asterisk cluster gemeinschaft
  4. *
  5. * $Revision$
  6. *
  7. * Copyright 2010, amooma GmbH, Bachstr. 126, 56566 Neuwied, Germany,
  8. * http://www.amooma.de/
  9. * Sebastian Ertz <gemeinschaft@swastel.eisfair.net>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  24. * MA 02110-1301, USA.
  25. \*******************************************************************/
  26. define( 'GS_VALID', true ); /// this is a parent file
  27. require_once( dirName(__FILE__) .'/../../../inc/conf.php' );
  28. include_once( GS_DIR .'inc/db_connect.php' );
  29. include_once( GS_DIR .'inc/gettext.php' );
  30. include_once( GS_DIR .'inc/string.php' );
  31. include_once( GS_DIR .'inc/group-fns.php' );
  32. header( 'Content-Type: application/xml; charset=utf-8' );
  33. header( 'Expires: 0' );
  34. header( 'Pragma: no-cache' );
  35. header( 'Cache-Control: private, no-cache, must-revalidate' );
  36. header( 'Vary: *' );
  37. function tiptelXmlEsc( $str )
  38. {
  39. return htmlEnt( $str );
  40. }
  41. function _ob_send()
  42. {
  43. if (! headers_sent()) {
  44. header( 'Content-Type: application/xml; charset=utf-8' );
  45. header( 'Content-Length: '. (int)@ob_get_length() );
  46. }
  47. @ob_end_flush();
  48. die();
  49. }
  50. function _err( $msg='' )
  51. {
  52. @ob_end_clean();
  53. ob_start();
  54. echo
  55. '<?','xml version="1.0" encoding="utf-8"?','>', "\n",
  56. '<TiptelIPPhoneTextScreen>', "\n",
  57. '<Title>', __('Fehler'), '</Title>', "\n",
  58. '<Text>', tiptelXmlEsc( __('Fehler') .': '. $msg ), '</Text>', "\n",
  59. '</TiptelIPPhoneTextScreen>', "\n";
  60. _ob_send();
  61. }
  62. function getUserID( $ext )
  63. {
  64. global $db;
  65. if (! preg_match('/^\d+$/', $ext))
  66. _err( 'Invalid username' );
  67. $user_id = (int)$db->executeGetOne( 'SELECT `_user_id` FROM `ast_sipfriends` WHERE `name`=\''. $db->escape($ext) .'\'' );
  68. if ($user_id < 1)
  69. _err( 'Unknown user' );
  70. return $user_id;
  71. }
  72. if (! gs_get_conf('GS_TIPTEL_PROV_ENABLED')) {
  73. gs_log( GS_LOG_DEBUG, "Tiptel provisioning not enabled" );
  74. _err( 'Not enabled' );
  75. }
  76. $type = trim( @$_REQUEST['t'] );
  77. if (! in_array( $type, array('gs','prv','imported'), true )) {
  78. $type = false;
  79. }
  80. $page = (int)trim( @$_REQUEST['p'] );
  81. $search = (int)trim( @$_REQUEST['s'] );
  82. $query = trim( @$_REQUEST['q'] );
  83. $per_page = 10;
  84. $db = gs_db_slave_connect();
  85. /*
  86. $typeToTitle = array(
  87. 'imported' => "Firma (aus LDAP)",
  88. 'gs' => "Firma", # should normally be "Gemeinschaft"
  89. 'prv' => "Pers\xC3\xB6nlich",
  90. );
  91. */
  92. $tmp = array(
  93. 15=>array(
  94. 'k' => 'gs' ,
  95. 'v' => gs_get_conf('GS_PB_INTERNAL_TITLE', __("Intern")) ),
  96. 25=>array(
  97. 'k' => 'prv',
  98. 'v' => gs_get_conf('GS_PB_PRIVATE_TITLE' , __("Pers\xC3\xB6nlich")) )
  99. );
  100. if (gs_get_conf('GS_PB_IMPORTED_ENABLED')) {
  101. $pos = (int)gs_get_conf('GS_PB_IMPORTED_ORDER', 9) * 10;
  102. $tmp[$pos] = array(
  103. 'k' => 'imported',
  104. 'v' => gs_get_conf('GS_PB_IMPORTED_TITLE', __("Extern"))
  105. );
  106. }
  107. kSort($tmp);
  108. foreach ($tmp as $arr) {
  109. $typeToTitle[$arr['k']] = $arr['v'];
  110. }
  111. $url_tiptel_pb = GS_PROV_SCHEME .'://'. GS_PROV_HOST . (GS_PROV_PORT ? ':'.GS_PROV_PORT : '') . GS_PROV_PATH .'tiptel/pb.php';
  112. #################################### SEARCH SCREEN {
  113. if ($search === 1) {
  114. $user = trim( @$_REQUEST['u'] );
  115. $user_id = getUserID( $user );
  116. ob_start();
  117. echo '<?','xml version="1.0" encoding="utf-8"?','>',"\n";
  118. echo
  119. '<TiptelIPPhoneInputScreen>', "\n",
  120. '<Title>Suchen</Title>', "\n",
  121. '<URL>', tiptelXmlEsc($url_tiptel_pb.'?u='.$user.'&t='.$type) ,'</URL>', "\n",
  122. '<InputField type="string" password="no" editable="yes">', "\n",
  123. '<Prompt>Suche nach</Prompt>', "\n",
  124. '<Parameter>q</Parameter>',"\n",
  125. '</InputField>', "\n",
  126. '</TiptelIPPhoneInputScreen>', "\n";
  127. _ob_send();
  128. }
  129. #################################### SEARCH SCREEN }
  130. #################################### INITIAL SCREEN {
  131. if (! $type) {
  132. $user = trim( @$_REQUEST['u'] );
  133. $user_id = getUserID( $user );
  134. $user_groups = gs_group_members_groups_get(array($user_id), 'user');
  135. $permission_groups = gs_group_permissions_get($user_groups, 'phonebook_user');
  136. $group_members = gs_group_members_get($permission_groups);
  137. ob_start();
  138. echo
  139. '<?','xml version="1.0" encoding="utf-8"?','>', "\n",
  140. '<TiptelIPPhoneTextMenu>', "\n",
  141. '<Title>', __('Telefonbuch') ,'</Title>', "\n\n";
  142. foreach ($typeToTitle as $t => $title) {
  143. $cq = 'SELECT COUNT(*) FROM ';
  144. switch ($t) {
  145. case 'gs' : $cq .= '`users` WHERE `id` IN ('.implode(',',$group_members).') AND `id`!='.$user_id; break;
  146. case 'imported': $cq .= '`pb_ldap`' ; break;
  147. case 'prv' : $cq .= '`pb_prv` WHERE `user_id`='. $user_id ; break;
  148. default : $cq = false;
  149. }
  150. $c = $cq ? (' ('. (int)@$db->executeGetOne( $cq ) .')') : '';
  151. echo
  152. '<MenuItem>', "\n",
  153. '<Prompt>', tiptelXmlEsc($title), $c, '</Prompt>', "\n",
  154. '<URI>', tiptelXmlEsc($url_tiptel_pb), '</URI>', "\n",
  155. '<Selection>', tiptelXmlEsc('0&u='.$user.'&t='.$t),'</Selection>', "\n",
  156. '</MenuItem>', "\n\n";
  157. }
  158. echo '</TiptelIPPhoneTextMenu>', "\n";
  159. _ob_send();
  160. }
  161. #################################### INITIAL SCREEN }
  162. #################################### PHONEBOOK {
  163. if ($type === 'gs') {
  164. $user = trim( @ $_REQUEST['u'] );
  165. $user_id = getUserID( $user );
  166. $user_groups = gs_group_members_groups_get(array($user_id), 'user');
  167. $permission_groups = gs_group_permissions_get($user_groups, 'phonebook_user');
  168. $group_members = gs_group_members_get($permission_groups);
  169. $where = '';
  170. if (strLen($query) > 0)
  171. $where = 'AND (`u`.`lastname` LIKE \'%'. $query .'%\' OR `u`.`firstname` LIKE \'%'. $query .'%\')';
  172. $query =
  173. 'SELECT SQL_CALC_FOUND_ROWS `u`.`lastname` `ln`, `u`.`firstname` `fn`, `s`.`name` `ext`
  174. FROM
  175. `users` `u` JOIN
  176. `ast_sipfriends` `s` ON (`s`.`_user_id`=`u`.`id`)
  177. WHERE
  178. `u`.`id` IN ('.implode(',',$group_members).') AND
  179. `u`.`id`!='.$user_id.'
  180. '. ($where ? $where : '') .'
  181. ORDER BY `u`.`lastname`, `u`.`firstname`
  182. LIMIT '. ($page * (int)$per_page) .','. (int)$per_page;
  183. }
  184. elseif ($type === 'prv') {
  185. $user = trim( @ $_REQUEST['u'] );
  186. $user_id = getUserID( $user );
  187. $where = '';
  188. if (strLen($query) > 0)
  189. $where = 'AND (`pb`.`lastname` LIKE \'%'. $query .'%\' OR `pb`.`firstname` LIKE \'%'. $query .'%\')';
  190. $query =
  191. 'SELECT SQL_CALC_FOUND_ROWS `pb`.`lastname` `ln`, `pb`.`firstname` `fn`, `pb`.`number` `ext`
  192. FROM `pb_prv` `pb`
  193. WHERE
  194. `pb`.`user_id`='.$user_id.'
  195. '. ($where ? $where : '') .'
  196. ORDER BY `pb`.`lastname`, `pb`.`firstname`
  197. LIMIT '. ($page * (int)$per_page) .','. (int)$per_page;
  198. }
  199. elseif ($type === 'imported') {
  200. $query =
  201. 'SELECT SQL_CALC_FOUND_ROWS `lastname` `ln`, `firstname` `fn`, `number` `ext`
  202. FROM `pb_ldap`
  203. ORDER BY `lastname`, `firstname`
  204. LIMIT '. ($page * (int)$per_page) .','. (int)$per_page;
  205. }
  206. if (in_array( $type, array('gs','prv','imported'), true )) {
  207. ob_start();
  208. echo '<?','xml version="1.0" encoding="utf-8"?','>',"\n";
  209. $rs = $db->execute($query);
  210. $num_total = @$db->numFoundRows();
  211. $num_pages = ceil($num_total / $per_page);
  212. $page_title = $typeToTitle[$type];
  213. if ($num_pages > 1) $page_title.= ' '.($page+1).'/'.$num_pages;
  214. if ( $rs && $rs->numRows() !== 0 ) {
  215. echo
  216. '<TiptelIPPhoneDirectory>', "\n",
  217. '<Title>', tiptelXmlEsc( $page_title ), '</Title>', "\n";
  218. while ($r = $rs->fetchRow()) {
  219. $name = $r['ln'] .( strLen($r['fn'])>0 ? (', '.$r['fn']) : '' );
  220. $number = $r['ext'];
  221. echo
  222. '<MenuItem>',
  223. '<Prompt>', tiptelXmlEsc( $name ) ,' (', tiptelXmlEsc( $number ) ,')</Prompt>',
  224. '<URI>', $number ,'</URI>',
  225. '</MenuItem>', "\n";
  226. }
  227. echo "\n";
  228. echo
  229. '<SoftKey index="1">', "\n",
  230. '<Label>', tiptelXmlEsc(__("Zur\xC3\xBCck")), '</Label>', "\n",
  231. '<URI>SoftKey:Exit</URI>', "\n",
  232. '</SoftKey>', "\n";
  233. echo '<SoftKey index="2">', "\n";
  234. if($page > 0) {
  235. echo '<Label>', tiptelXmlEsc('<< '.$page), '</Label>', "\n";
  236. echo '<URI>', tiptelXmlEsc($url_tiptel_pb.'?u='.$user.'&t='.$type.'&p='.($page-1)), '</URI>', "\n";
  237. } else {
  238. echo '<Label>', tiptelXmlEsc(__('Suchen')), '</Label>', "\n";
  239. echo '<URI>', tiptelXmlEsc($url_tiptel_pb.'?u='.$user.'&t='.$type.'&s=1'), '</URI>', "\n";
  240. }
  241. echo '</SoftKey>', "\n";
  242. echo '<SoftKey index="3">', "\n";
  243. if($page < $num_pages-1 ) {
  244. echo '<Label>', tiptelXmlEsc(($page+2).' >>'), '</Label>', "\n";
  245. echo '<URI>', tiptelXmlEsc($url_tiptel_pb.'?u='.$user.'&t='.$type.'&p='.($page+1)), '</URI>', "\n";
  246. } else {
  247. echo '<Label></Label>', "\n";
  248. echo '<URI></URI>', "\n";
  249. }
  250. echo '</SoftKey>', "\n";
  251. echo
  252. '<SoftKey index="4">', "\n",
  253. '<Label>', tiptelXmlEsc(__("W\xC3\xA4hlen")), '</Label>', "\n",
  254. '<URI>SoftKey:Dial</URI>', "\n",
  255. '</SoftKey>', "\n";
  256. echo '</TiptelIPPhoneDirectory>', "\n";
  257. } else {
  258. echo
  259. '<TiptelIPPhoneTextScreen>', "\n",
  260. '<Title>', tiptelXmlEsc( $typeToTitle[$type] ), '</Title>', "\n",
  261. '<Text>', tiptelXmlEsc( __("Dieses Telefonbuch enth\xC3\xA4lt keine Eintr\xC3\xA4ge.") ), '</Text>', "\n",
  262. '</TiptelIPPhoneTextScreen>', "\n";
  263. }
  264. _ob_send();
  265. }
  266. #################################### PHONEBOOK }
  267. ?>