PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/philipp-kempgen/amooma-gemeinschaft-pbx
PHP | 474 lines | 318 code | 116 blank | 40 comment | 28 complexity | acdf1f28159ab3913deadd9fc8ab5a10 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*******************************************************************\
  3. * Gemeinschaft - asterisk cluster gemeinschaft
  4. *
  5. * $Revision$
  6. *
  7. * Copyright 2007-2010, amooma GmbH, Bachstr. 126, 56566 Neuwied, Germany,
  8. * http://www.amooma.de/
  9. * Stefan Wintermeyer <stefan.wintermeyer@amooma.de>
  10. * Philipp Kempgen <philipp.kempgen@amooma.de>
  11. * Peter Kozak <peter.kozak@amooma.de>
  12. *
  13. * Author: Daniel Scheller <scheller@loca.net>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version 2
  18. * of the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  28. * MA 02110-1301, USA.
  29. \*******************************************************************/
  30. define("GS_VALID", true); // this is a parent file
  31. require_once(dirname(__FILE__) ."/../../../inc/conf.php");
  32. include_once(GS_DIR ."inc/db_connect.php");
  33. include_once(GS_DIR ."inc/gettext.php");
  34. include_once(GS_DIR ."inc/group-fns.php");
  35. header("Content-Type: text/html; charset=utf-8");
  36. header("Expires: 0");
  37. header("Pragma: no-cache");
  38. header("Cache-Control: private, no-cache, must-revalidate");
  39. header("Vary: *");
  40. $phonebook_doctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
  41. //---------------------------------------------------------------------------
  42. function _ob_send()
  43. {
  44. if(!headers_sent())
  45. {
  46. Header("Content-Type: text/html; charset=utf-8");
  47. Header("Content-Length: ". (int) @ob_get_length());
  48. }
  49. @ob_end_flush();
  50. die();
  51. }
  52. function _err($msg = "")
  53. {
  54. @ob_end_clean();
  55. ob_start();
  56. echo "<html>\n";
  57. echo "<head><title>". __("Fehler") ."</title></head>\n";
  58. echo "<body><b>". __("Fehler") ."</b>: ". $msg ."</body>\n";
  59. echo "</html>\n";
  60. _ob_send();
  61. }
  62. function getUserID($ext)
  63. {
  64. global $db;
  65. if(!preg_match("/^\d+$/", $ext)) _err("Invalid username");
  66. $user_id = (int) $db->executeGetOne("SELECT `_user_id` FROM `ast_sipfriends` WHERE `name`='". $db->escape($ext) ."'");
  67. if ($user_id < 1) _err("Unknown user");
  68. return $user_id;
  69. }
  70. //---------------------------------------------------------------------------
  71. if ( !gs_get_conf('GS_POLYCOM_PROV_ENABLED') )
  72. {
  73. gs_log(GS_LOG_DEBUG, 'Polycom provisioning not enabled');
  74. _err('Not enabled.');
  75. }
  76. $type = trim(@$_REQUEST['t']);
  77. if (! in_array($type, array('gs', 'prv', 'imported'), true) )
  78. {
  79. $type = false;
  80. }
  81. $searchform = (int)trim(@$_REQUEST['searchform']);
  82. $querystring = trim(@$_REQUEST['q']);
  83. $db = gs_db_slave_connect();
  84. $tmp = array(
  85. 15 => array(
  86. 'k' => 'gs',
  87. 'v' => gs_get_conf('GS_PB_INTERNAL_TITLE', __('Intern'))),
  88. 25 => array(
  89. 'k' => 'prv',
  90. 'v' => gs_get_conf('GS_PB_PRIVATE_TITLE' , __('Pers\xC3\xB6nlich')))
  91. );
  92. if ( gs_get_conf('GS_PB_IMPORTED_ENABLED') )
  93. {
  94. $pos = (int) gs_get_conf('GS_PB_IMPORTED_ORDER', 9) * 10;
  95. $tmp[$pos] = array(
  96. 'k' => 'imported',
  97. 'v' => gs_get_conf('GS_PB_IMPORTED_TITLE', __('Extern'))
  98. );
  99. }
  100. kSort($tmp);
  101. foreach ($tmp as $arr)
  102. {
  103. $typeToTitle[$arr['k']] = $arr['v'];
  104. }
  105. $url_polycom_pb = GS_PROV_SCHEME ."://". GS_PROV_HOST . (GS_PROV_PORT ? ":". GS_PROV_PORT : "") . GS_PROV_PATH ."polycom/pb.php";
  106. #################################### INITIAL SCREEN {
  107. if (!$type)
  108. {
  109. $mac = preg_replace('/[^\dA-Z]/', '', strToUpper(trim(@$_REQUEST['m'])));
  110. $user = trim(@$_REQUEST['u']);
  111. $user_id = getUserID($user);
  112. $user_groups = gs_group_members_groups_get(array($user_id), "user");
  113. $permission_groups = gs_group_permissions_get($user_groups, "phonebook_user");
  114. $group_members = gs_group_members_get($permission_groups);
  115. ob_start();
  116. echo $phonebook_doctype ."\n";
  117. echo "<html>\n";
  118. echo "<head><title>". __("Telefonbuch") ."</title></head>\n";
  119. echo "<body><br />\n";
  120. foreach($typeToTitle as $t => $title)
  121. {
  122. $cq = 'SELECT COUNT(*) FROM ';
  123. switch ($t)
  124. {
  125. case 'gs' :
  126. $cq .= "`users` WHERE `id` IN (". implode(",", $group_members) .") AND `id` != ". $user_id;
  127. break;
  128. case 'imported' :
  129. $cq .= "`pb_ldap`";
  130. break;
  131. case 'prv' :
  132. $cq .= "`pb_prv` WHERE `user_id`=". $user_id;
  133. break;
  134. default :
  135. $cq = false;
  136. break;
  137. }
  138. $c = $cq ? (" (". (int)@$db->executeGetOne($cq) .")") : "";
  139. echo "- <a href=\"". $url_polycom_pb ."?m=". $mac ."&amp;u=". $user ."&amp;t=". $t ."\">". $title . $c ."</a><br />\n";
  140. }
  141. echo "</body>\n";
  142. echo "</html>\n";
  143. _ob_send();
  144. }
  145. #################################### INITIAL SCREEN }
  146. #################################### SEARCH FORM {
  147. if ($searchform === 1)
  148. {
  149. $mac = preg_replace("/[^\dA-Z]/", "", strtoupper(trim(@$_REQUEST["m"])));
  150. $user = trim(@$_REQUEST["u"]);
  151. ob_start();
  152. echo $phonebook_doctype ."\n";
  153. echo "<html>\n";
  154. echo "<head><title>". __("Telefonbuch") ." - ". $typeToTitle[$type] ."</title></head>\n";
  155. echo "<body><br />\n";
  156. echo "<form name=\"search\" method=\"GET\" action=\"". $url_polycom_pb ."\">\n";
  157. echo "<input type=\"hidden\" name=\"u\" value=\"". $user ."\" />";
  158. echo "<input type=\"hidden\" name=\"m\" value=\"". $mac ."\" />";
  159. echo "<input type=\"hidden\" name=\"t\" value=\"". $type ."\" />\n";
  160. echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" width=\"100%\">\n";
  161. echo "<tr>";
  162. echo "<th align=\"center\" width=\"100%\">Telefonbuch '". $typeToTitle[$type] ."' durchsuchen:</th>";
  163. echo "</tr>";
  164. echo "<tr><td align=\"center\" width=\"100%\"><input type=\"text\" name=\"q\" /></td></tr>\n";
  165. echo "<tr><td align=\"center\" width=\"100%\"><input type=\"submit\" value=\" Finden \" /></td></tr>\n";
  166. echo "</table>\n";
  167. echo "</form>\n";
  168. echo "</body>\n";
  169. echo "</html>\n";
  170. _ob_send();
  171. }
  172. $num_results = (int) gs_get_conf("GS_POLYCOM_PROV_PB_NUM_RESULTS", 10);
  173. #################################### IMPORTED PHONEBOOK {
  174. if( $type === "imported" )
  175. {
  176. // we don't need $user for this
  177. ob_start();
  178. echo $phonebook_doctype ."\n";
  179. $pagetitle = __("Telefonbuch") ." - ". $typeToTitle[$type];
  180. $searchsql = "1";
  181. $noresultsmsg = __("Dieses Telefonbuch enth\xC3\xA4lt keine Eintr\xC3\xA4ge.");
  182. if (strlen($querystring) > 0)
  183. {
  184. $pagetitle .= " ('". $querystring ."')";
  185. $searchsql = "`lastname` LIKE '%". $querystring ."%' OR `firstname` LIKE '%". $querystring ."%'";
  186. $noresultsmsg = sprintf(__("Keine Treffer f\xC3\xBCr \"%s\". Dr\xC3\xBCcken Sie 'Zur\xC3\xBCck', um eine neue Suche auszuf\xC3\xBChren."), $querystring);
  187. }
  188. echo "<html>\n";
  189. echo "<head><title>". $pagetitle ."</title></head>\n";
  190. echo "<body><br />\n";
  191. $query =
  192. "SELECT `lastname` `ln`, `firstname` `fn`, `number` `ext` ".
  193. "FROM `pb_ldap` ".
  194. "WHERE ". $searchsql ." ".
  195. "ORDER BY `lastname`, `firstname` ".
  196. "LIMIT ". $num_results;
  197. $rs = $db->execute($query);
  198. if ($rs->numRows() !== 0)
  199. {
  200. echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" width=\"100%\">\n";
  201. echo "<tr>";
  202. echo "<th width=\"50%\">Name</th>";
  203. echo "<th width=\"50%\">Nummer</th></tr>\n";
  204. while ( $r = $rs->fetchRow() )
  205. {
  206. $name = $r["ln"] .(strlen($r["fn"]) > 0 ? (", ". $r["fn"]) : "");
  207. $number = $r["ext"];
  208. echo "<tr>";
  209. echo "<td width=\"50%\">". $name ."</td>";
  210. echo "<td width=\"50%\"><a href=\"tel://". $number."\">". $number ."</a></td></tr>\n";
  211. }
  212. echo "</table>\n";
  213. }
  214. else
  215. {
  216. echo "<br />". $noresultsmsg ."<br />\n";
  217. }
  218. echo "</body>\n";
  219. echo "<softkey index=\"1\" label=\"Zur\xC3\xBCck\" action=\"Softkey:Back\" />\n";
  220. echo "<softkey index=\"2\" label=\"\" action=\"\" />\n";
  221. echo "<softkey index=\"3\" label=\"Beenden\" action=\"Softkey:Exit\" />\n";
  222. echo "<softkey index=\"4\" label=\"\" action=\"\" />\n";
  223. echo "</html>\n";
  224. _ob_send();
  225. }
  226. #################################### IMPORTED PHONEBOOK }
  227. #################################### INTERNAL PHONEBOOK {
  228. if ($type === "gs")
  229. {
  230. $mac = preg_replace("/[^\dA-Z]/", "", strToUpper(trim(@$_REQUEST["m"])));
  231. $user = trim(@$_REQUEST["u"]);
  232. $user_id = getUserID($user);
  233. $user_groups = gs_group_members_groups_get(array($user_id), "user");
  234. $permission_groups = gs_group_permissions_get($user_groups, "phonebook_user");
  235. $group_members = gs_group_members_get($permission_groups);
  236. ob_start();
  237. echo $phonebook_doctype ."\n";
  238. $pagetitle = __("Telefonbuch") ." - ". $typeToTitle[$type];
  239. $searchsql = "1";
  240. $noresultsmsg = __("Dieses Telefonbuch enth\xC3\xA4lt keine Eintr\xC3\xA4ge.");
  241. if ( strlen($querystring) > 0 )
  242. {
  243. $pagetitle .= " ('". $querystring ."')";
  244. $searchsql = "`u`.`lastname` LIKE '%". $querystring ."%' OR `u`.`firstname` LIKE '%". $querystring ."%'";
  245. $noresultsmsg = sprintf(__("Keine Treffer f\xC3\xBCr \"%s\". Dr\xC3\xBCcken Sie 'Zur\xC3\xBCck', um eine neue Suche auszuf\xC3\xBChren."), $querystring);
  246. }
  247. echo "<html>\n";
  248. echo "<head><title>". $pagetitle ."</title></head>\n";
  249. echo "<body><br />\n";
  250. $query =
  251. "SELECT `u`.`lastname` `ln`, `u`.`firstname` `fn`, `s`.`name` `ext` ".
  252. "FROM ".
  253. " `users` `u` JOIN ".
  254. " `ast_sipfriends` `s` ON (`s`.`_user_id`=`u`.`id`) ".
  255. "WHERE ".
  256. " `u`.`id` IN (". implode(",", $group_members) .") AND (".
  257. " `u`.`id` != ". $user_id ." ) AND ".
  258. $searchsql ." ".
  259. "ORDER BY `u`.`lastname`, `u`.`firstname` ".
  260. "LIMIT ". $num_results;
  261. $rs = $db->execute($query);
  262. if($rs && $rs->numRows() !== 0)
  263. {
  264. echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" width=\"100%\">\n";
  265. echo "<tr>";
  266. echo "<th width=\"50%\">Name</th>";
  267. echo "<th width=\"50%\">Nummer</th></tr>\n";
  268. while ( $r = $rs->fetchRow() )
  269. {
  270. $name = $r["ln"] .(strlen($r["fn"]) > 0 ? (", ". $r["fn"]) : "");
  271. $number = $r["ext"];
  272. echo "<tr>";
  273. echo "<td width=\"50%\">". $name ."</td>";
  274. echo "<td width=\"50%\"><a href=\"tel://". $number."\">". $number ."</a></td></tr>\n";
  275. }
  276. echo "</table>\n";
  277. }
  278. else
  279. {
  280. echo "<br />". $noresultsmsg. "<br />\n";
  281. }
  282. echo "</body>\n";
  283. echo "<softkey index=\"1\" label=\"Zur\xC3\xBCck\" action=\"Softkey:Back\" />\n";
  284. echo "<softkey index=\"2\" label=\"Suchen\" action=\"Softkey:Fetch;". $url_polycom_pb ."?u=". $user ."&amp;m=". $mac ."&amp;t=". $type ."&amp;searchform=1\" />\n";
  285. echo "<softkey index=\"3\" label=\"Beenden\" action=\"Softkey:Exit\" />\n";
  286. echo "<softkey index=\"4\" label=\"\" action=\"\" />\n";
  287. echo "</html>\n";
  288. _ob_send();
  289. }
  290. #################################### INTERNAL PHONEBOOK }
  291. #################################### PRIVATE PHONEBOOK {
  292. if ( $type === "prv" )
  293. {
  294. $mac = preg_replace("/[^\dA-Z]/", "", strtoupper(trim(@$_REQUEST["m"])));
  295. $user = trim(@$_REQUEST["u"]);
  296. $user_id = getUserID($user);
  297. ob_start();
  298. echo $phonebook_doctype ."\n";
  299. $pagetitle = __("Telefonbuch") ." - ". $typeToTitle[$type];
  300. $searchsql = "1";
  301. $noresultsmsg = __("Ihr pers\xC3\xB6nliches Telefonbuch enth\xC3\xA4lt keine Eintr\xC3\xA4ge.");
  302. if ( strlen($querystring) > 0 )
  303. {
  304. $pagetitle .= " ('". $querystring ."')";
  305. $searchsql = "`lastname` LIKE '%". $querystring ."%' OR `firstname` LIKE '%". $querystring ."%'";
  306. $noresultsmsg = sprintf(__("Keine Treffer f\xC3\xBCr \"%s\". Dr\xC3\xBCcken Sie 'Zur\xC3\xBCck', um eine neue Suche auszuf\xC3\xBChren."), $querystring);
  307. }
  308. echo "<html>\n";
  309. echo "<head><title>". $pagetitle ."</title></head>\n";
  310. echo "<body><br />\n";
  311. $user_id_check = $db->executeGetOne("SELECT `user_id` FROM `phones` WHERE `mac_addr`='". $db->escape($mac) ."'");
  312. if ($user_id != $user_id_check)
  313. _err("Not authorized");
  314. $remote_addr = @$_SERVER["REMOTE_ADDR"];
  315. $remote_addr_check = $db->executeGetOne("SELECT `current_ip` FROM `users` WHERE `id`=". $user_id);
  316. if ($remote_addr != $remote_addr_check)
  317. _err("Not authorized");
  318. $query =
  319. "SELECT `lastname` `ln`, `firstname` `fn`, `number` ".
  320. "FROM ".
  321. " `pb_prv` ".
  322. "WHERE ".
  323. " `user_id`=". $user_id ." AND ".
  324. $searchsql ." ".
  325. "ORDER BY `lastname`, `firstname` ".
  326. "LIMIT ". $num_results;
  327. $rs = $db->execute($query);
  328. if ($rs->numRows() !== 0)
  329. {
  330. echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" width=\"100%\">\n";
  331. echo "<tr>";
  332. echo "<th width=\"50%\">Name</th>";
  333. echo "<th width=\"50%\">Nummer</th></tr>\n";
  334. while ( $r = $rs->fetchRow() )
  335. {
  336. $name = $r["ln"] .(strlen($r["fn"]) > 0 ? (", ". $r["fn"]) : "");
  337. $number = $r["number"];
  338. echo "<tr>";
  339. echo "<td width=\"50%\">". $name ."</td>";
  340. echo "<td width=\"50%\"><a href=\"tel://". $number."\">". $number ."</a></td>";
  341. echo "</tr>\n";
  342. }
  343. echo "</table>\n";
  344. }
  345. else
  346. {
  347. echo "<br />". $noresultsmsg ."<br />\n";
  348. }
  349. echo "</body>\n";
  350. echo "<softkey index=\"1\" label=\"Zur\xC3\xBCck\" action=\"Softkey:Back\" />\n";
  351. echo "<softkey index=\"2\" label=\"Suchen\" action=\"Softkey:Fetch;". $url_polycom_pb ."?u=". $user ."&amp;m=". $mac ."&amp;t=". $type ."&amp;searchform=1\" />\n";
  352. echo "<softkey index=\"3\" label=\"Beenden\" action=\"Softkey:Exit\" />\n";
  353. echo "<softkey index=\"4\" label=\"\" action=\"\" />\n";
  354. echo "</html>\n";
  355. _ob_send();
  356. }
  357. #################################### PRIVATE PHONEBOOK }
  358. ?>