PageRenderTime 24ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

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