contacts.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2019
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes
  22. require_once "root.php";
  23. require_once "resources/require.php";
  24. require_once "resources/check_auth.php";
  25. require_once "resources/paging.php";
  26. //check permissions
  27. if (permission_exists('contact_view')) {
  28. //access granted
  29. }
  30. else {
  31. echo "access denied";
  32. exit;
  33. }
  34. //add multi-lingual support
  35. $language = new text;
  36. $text = $language->get();
  37. //get posted data
  38. if (is_array($_POST['contacts'])) {
  39. $action = $_POST['action'];
  40. $search = $_POST['search'];
  41. $contacts = $_POST['contacts'];
  42. }
  43. //process the http post data by action
  44. if ($action != '' && is_array($contacts) && @sizeof($contacts) != 0) {
  45. switch ($action) {
  46. case 'delete':
  47. if (permission_exists('contact_delete')) {
  48. $obj = new contacts;
  49. $obj->delete($contacts);
  50. }
  51. break;
  52. }
  53. header('Location: contacts.php'.($search != '' ? '?search='.urlencode($search) : null));
  54. exit;
  55. }
  56. //retrieve current user's assigned groups (uuids)
  57. foreach ($_SESSION['groups'] as $group_data) {
  58. $user_group_uuids[] = $group_data['group_uuid'];
  59. }
  60. //add user's uuid to group uuid list to include private (non-shared) contacts
  61. $user_group_uuids[] = $_SESSION["user_uuid"];
  62. //get contact settings - sync sources
  63. $sql = "select ";
  64. $sql .= "contact_uuid, ";
  65. $sql .= "contact_setting_value ";
  66. $sql .= "from ";
  67. $sql .= "v_contact_settings ";
  68. $sql .= "where ";
  69. $sql .= "domain_uuid = :domain_uuid ";
  70. $sql .= "and contact_setting_category = 'sync' ";
  71. $sql .= "and contact_setting_subcategory = 'source' ";
  72. $sql .= "and contact_setting_name = 'array' ";
  73. $sql .= "and contact_setting_value <> '' ";
  74. $sql .= "and contact_setting_value is not null ";
  75. if (!permission_exists('contact_domain_view')) {
  76. $sql .= "and ( "; //only contacts assigned to current user's group(s) and those not assigned to any group
  77. $sql .= " contact_uuid in ( ";
  78. $sql .= " select contact_uuid from v_contact_groups ";
  79. $sql .= " where ";
  80. if (is_array($user_group_uuids) && @sizeof($user_group_uuids) != 0) {
  81. foreach ($user_group_uuids as $index => $user_group_uuid) {
  82. if (is_uuid($user_group_uuid)) {
  83. $sql_where_or[] = "group_uuid = :group_uuid_".$index;
  84. $parameters['group_uuid_'.$index] = $user_group_uuid;
  85. }
  86. }
  87. if (is_array($sql_where_or) && @sizeof($sql_where_or) != 0) {
  88. $sql .= " ( ".implode(' or ', $sql_where_or)." ) ";
  89. }
  90. unset($sql_where_or, $index, $user_group_uuid);
  91. }
  92. $sql .= " and domain_uuid = :domain_uuid ";
  93. $sql .= " ) ";
  94. $sql .= " or ";
  95. $sql .= " contact_uuid not in ( ";
  96. $sql .= " select contact_uuid from v_contact_groups ";
  97. $sql .= " where group_uuid = :group_uuid ";
  98. $sql .= " and domain_uuid = :domain_uuid ";
  99. $sql .= " ) ";
  100. $sql .= ") ";
  101. }
  102. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  103. $parameters['group_uuid'] = $_SESSION['group_uuid'];
  104. $database = new database;
  105. $result = $database->select($sql, $parameters, 'all');
  106. if (is_array($result) && @sizeof($result) != 0) {
  107. foreach($result as $row) {
  108. $contact_sync_sources[$row['contact_uuid']][] = $row['contact_setting_value'];
  109. }
  110. }
  111. unset($sql, $parameters, $result);
  112. //get variables used to control the order
  113. $order_by = $_GET["order_by"];
  114. $order = $_GET["order"];
  115. //add the search term
  116. $search = strtolower($_GET["search"]);
  117. if (strlen($search) > 0) {
  118. if (is_numeric($search)) {
  119. $sql_search .= "and contact_uuid in ( ";
  120. $sql_search .= " select contact_uuid from v_contact_phones ";
  121. $sql_search .= " where phone_number like :search ";
  122. $sql_search .= ") ";
  123. }
  124. else {
  125. //open container
  126. $sql_search .= "and ( ";
  127. //search contact
  128. $sql_search .= "contact_uuid in ( ";
  129. $sql_search .= " select contact_uuid from v_contacts ";
  130. $sql_search .= " where domain_uuid = :domain_uuid ";
  131. $sql_search .= " and ( ";
  132. $sql_search .= " lower(contact_organization) like :search or ";
  133. $sql_search .= " lower(contact_name_given) like :search or ";
  134. $sql_search .= " lower(contact_name_family) like :search or ";
  135. $sql_search .= " lower(contact_nickname) like :search or ";
  136. $sql_search .= " lower(contact_title) like :search or ";
  137. $sql_search .= " lower(contact_category) like :search or ";
  138. $sql_search .= " lower(contact_role) like :search or ";
  139. $sql_search .= " lower(contact_url) like :search or ";
  140. $sql_search .= " lower(contact_time_zone) like :search or ";
  141. $sql_search .= " lower(contact_note) like :search or ";
  142. $sql_search .= " lower(contact_type) like :search ";
  143. $sql_search .= " ) ";
  144. $sql_search .= ") ";
  145. //search contact emails
  146. if (permission_exists('contact_email_view')) {
  147. $sql_search .= "or contact_uuid in ( ";
  148. $sql_search .= " select contact_uuid from v_contact_emails ";
  149. $sql_search .= " where domain_uuid = :domain_uuid ";
  150. $sql_search .= " and ( ";
  151. $sql_search .= " lower(email_address) like :search or ";
  152. $sql_search .= " lower(email_description) like :search ";
  153. $sql_search .= " ) ";
  154. $sql_search .= ") ";
  155. }
  156. //search contact notes
  157. if (permission_exists('contact_note_view')) {
  158. $sql_search .= "or contact_uuid in ( ";
  159. $sql_search .= " select contact_uuid from v_contact_notes ";
  160. $sql_search .= " where domain_uuid = :domain_uuid ";
  161. $sql_search .= " and lower(contact_note) like :search ";
  162. $sql_search .= ") ";
  163. }
  164. //close container
  165. $sql_search .= ") ";
  166. }
  167. $parameters['search'] = '%'.$search.'%';
  168. }
  169. //build query for paging and list
  170. $sql = "select count(*) ";
  171. $sql .= "from v_contacts as c ";
  172. $sql .= "where true ";
  173. if ($_GET['show'] != "all" || !permission_exists('contact_all')) {
  174. $sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) ";
  175. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  176. }
  177. if (!permission_exists('contact_domain_view')) {
  178. $sql .= "and ( "; //only contacts assigned to current user's group(s) and those not assigned to any group
  179. $sql .= " contact_uuid in ( ";
  180. $sql .= " select contact_uuid from v_contact_groups ";
  181. $sql .= " where ";
  182. if (is_array($user_group_uuids) && @sizeof($user_group_uuids) != 0) {
  183. foreach ($user_group_uuids as $index => $user_group_uuid) {
  184. if (is_uuid($user_group_uuid)) {
  185. $sql_where_or[] = "group_uuid = :group_uuid_".$index;
  186. $parameters['group_uuid_'.$index] = $user_group_uuid;
  187. }
  188. }
  189. if (is_array($sql_where_or) && @sizeof($sql_where_or) != 0) {
  190. $sql .= " ( ".implode(' or ', $sql_where_or)." ) ";
  191. }
  192. unset($sql_where_or, $index, $user_group_uuid);
  193. }
  194. $sql .= " and domain_uuid = :domain_uuid ";
  195. $sql .= " ) ";
  196. $sql .= " or contact_uuid in ( ";
  197. $sql .= " select contact_uuid from v_contact_users ";
  198. $sql .= " where user_uuid = :user_uuid ";
  199. $sql .= " and domain_uuid = :domain_uuid ";
  200. $sql .= "";
  201. $sql .= " ) ";
  202. $sql .= ") ";
  203. $parameters['user_uuid'] = $_SESSION['user_uuid'];
  204. }
  205. $sql .= $sql_search;
  206. $database = new database;
  207. $num_rows = $database->select($sql, $parameters, 'column');
  208. //prepare to page the results
  209. $rows_per_page = ($_SESSION['domain']['paging']['numeric'] != '') ? $_SESSION['domain']['paging']['numeric'] : 50;
  210. $param = "&search=".urlencode($search);
  211. if ($_GET['show'] == "all" && permission_exists('contact_all')) {
  212. $param .= "&show=all";
  213. }
  214. $page = $_GET['page'];
  215. if (strlen($page) == 0) { $page = 0; $_GET['page'] = 0; }
  216. list($paging_controls, $rows_per_page) = paging($num_rows, $param, $rows_per_page); //bottom
  217. list($paging_controls_mini, $rows_per_page) = paging($num_rows, $param, $rows_per_page, true); //top
  218. $offset = $rows_per_page * $page;
  219. //get the list
  220. $sql = str_replace('count(*)', '*, (select a.contact_attachment_uuid from v_contact_attachments as a where a.contact_uuid = c.contact_uuid and a.attachment_primary = 1) as contact_attachment_uuid', $sql);
  221. if ($order_by != '') {
  222. $sql .= order_by($order_by, $order);
  223. $sql .= ", contact_organization asc ";
  224. }
  225. else {
  226. $contact_default_sort_column = $_SESSION['contacts']['default_sort_column']['text'] != '' ? $_SESSION['contacts']['default_sort_column']['text'] : "last_mod_date";
  227. $contact_default_sort_order = $_SESSION['contacts']['default_sort_order']['text'] != '' ? $_SESSION['contacts']['default_sort_order']['text'] : "desc";
  228. $sql .= order_by($contact_default_sort_column, $contact_default_sort_order);
  229. if ($db_type == "pgsql") {
  230. $sql .= " nulls last ";
  231. }
  232. }
  233. $sql .= limit_offset($rows_per_page, $offset);
  234. $database = new database;
  235. $contacts = $database->select($sql, $parameters, 'all');
  236. unset($sql, $parameters);
  237. //create token
  238. $object = new token;
  239. $token = $object->create($_SERVER['PHP_SELF']);
  240. //includes and title
  241. $document['title'] = $text['title-contacts'];
  242. require_once "resources/header.php";
  243. //contact attachment layer
  244. echo "<style>\n";
  245. echo " #contact_attachment_layer {\n";
  246. echo " z-index: 999999;\n";
  247. echo " position: absolute;\n";
  248. echo " left: 0px;\n";
  249. echo " top: 0px;\n";
  250. echo " right: 0px;\n";
  251. echo " bottom: 0px;\n";
  252. echo " text-align: center;\n";
  253. echo " vertical-align: middle;\n";
  254. echo " }\n";
  255. echo "</style>\n";
  256. echo "<div id='contact_attachment_layer' style='display: none;'></div>\n";
  257. //show the content
  258. echo "<div class='action_bar' id='action_bar'>\n";
  259. echo " <div class='heading'><b>".$text['header-contacts']." (".$num_rows.")</b></div>\n";
  260. echo " <div class='actions'>\n";
  261. if (permission_exists('contact_add')) {
  262. echo button::create(['type'=>'button','label'=>$text['button-import'],'icon'=>$_SESSION['theme']['button_icon_import'],'collapse'=>'hide-sm-dn','style'=>'margin-right: 15px;','link'=>'contact_import.php']);
  263. }
  264. if (permission_exists('contact_add')) {
  265. echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'id'=>'btn_add','collapse'=>'hide-sm-dn','link'=>'contact_edit.php']);
  266. }
  267. if (permission_exists('contact_delete') && $contacts) {
  268. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'id'=>'btn_delete','name'=>'btn_delete','style'=>'display: none;','collapse'=>'hide-sm-dn','onclick'=>"modal_open('modal-delete','btn_delete');"]);
  269. }
  270. echo "<form id='form_search' class='inline' method='get'>\n";
  271. if (permission_exists('contact_all')) {
  272. if ($_GET['show'] == 'all') {
  273. echo " <input type='hidden' name='show' value='all'>";
  274. }
  275. else {
  276. echo button::create(['type'=>'button','label'=>$text['button-show_all'],'icon'=>$_SESSION['theme']['button_icon_all'],'link'=>'?type=&show=all'.($search != '' ? "&search=".urlencode($search) : null)]);
  277. }
  278. }
  279. echo "<input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown=''>";
  280. echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search','collapse'=>'hide-sm-dn']);
  281. //echo button::create(['label'=>$text['button-reset'],'icon'=>$_SESSION['theme']['button_icon_reset'],'type'=>'button','id'=>'btn_reset','collapse'=>'hide-sm-dn','link'=>'contacts.php','style'=>($search == '' ? 'display: none;' : null)]);
  282. if ($paging_controls_mini != '') {
  283. echo "<span style='margin-left: 15px;'>".$paging_controls_mini."</span>";
  284. }
  285. echo " </form>\n";
  286. echo " </div>\n";
  287. echo " <div style='clear: both;'></div>\n";
  288. echo "</div>\n";
  289. if (permission_exists('contact_delete') && $contacts) {
  290. echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('delete'); list_form_submit('form_list');"])]);
  291. }
  292. echo $text['description-contacts']."\n";
  293. echo "<br /><br />\n";
  294. echo "<form id='form_list' method='post'>\n";
  295. echo "<input type='hidden' id='action' name='action' value=''>\n";
  296. echo "<input type='hidden' name='search' value=\"".escape($search)."\">\n";
  297. echo "<table class='list'>\n";
  298. echo "<tr class='list-header'>\n";
  299. if (permission_exists('contact_delete')) {
  300. echo " <th class='checkbox'>\n";
  301. echo " <input type='checkbox' id='checkbox_all' name='checkbox_all' onclick='list_all_toggle(); checkbox_on_change(this);' ".($contacts ?: "style='visibility: hidden;'").">\n";
  302. echo " </th>\n";
  303. }
  304. if ($_GET['show'] == "all" && permission_exists('contact_all')) {
  305. echo th_order_by('domain_name', $text['label-domain'], $order_by, $order, $param, "class='shrink'");
  306. }
  307. echo th_order_by('contact_type', $text['label-contact_type'], $order_by, $order);
  308. echo th_order_by('contact_organization', $text['label-contact_organization'], $order_by, $order);
  309. echo "<th class='shrink hide-xs'>&nbsp;</th>\n";
  310. echo th_order_by('contact_name_given', $text['label-contact_name_given'], $order_by, $order);
  311. echo th_order_by('contact_name_family', $text['label-contact_name_family'], $order_by, $order);
  312. echo th_order_by('contact_nickname', $text['label-contact_nickname'], $order_by, $order, null, "class='hide-xs'");
  313. echo th_order_by('contact_title', $text['label-contact_title'], $order_by, $order, null, "class='hide-sm-dn'");
  314. echo th_order_by('contact_role', $text['label-contact_role'], $order_by, $order, null, "class='hide-sm-dn'");
  315. echo "<th class='shrink hide-sm-dn'>&nbsp;</th>\n";
  316. if ($_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') {
  317. echo " <td class='action-button'>&nbsp;</td>\n";
  318. }
  319. echo "</tr>\n";
  320. if (is_array($contacts) && @sizeof($contacts) != 0) {
  321. $x = 0;
  322. foreach($contacts as $row) {
  323. $list_row_url = "contact_view.php?id=".urlencode($row['contact_uuid'])."&query_string=".urlencode($_SERVER["QUERY_STRING"]);
  324. echo "<tr class='list-row' href='".$list_row_url."'>\n";
  325. if (permission_exists('contact_delete')) {
  326. echo " <td class='checkbox'>\n";
  327. echo " <input type='checkbox' name='contacts[$x][checked]' id='checkbox_".$x."' value='true' onclick=\"checkbox_on_change(this); if (!this.checked) { document.getElementById('checkbox_all').checked = false; }\">\n";
  328. echo " <input type='hidden' name='contacts[$x][uuid]' value='".escape($row['contact_uuid'])."' />\n";
  329. echo " </td>\n";
  330. }
  331. if ($_GET['show'] == "all" && permission_exists('contact_all')) {
  332. if (strlen($_SESSION['domains'][$row['domain_uuid']]['domain_name']) > 0) {
  333. $domain = $_SESSION['domains'][$row['domain_uuid']]['domain_name'];
  334. }
  335. else {
  336. $domain = $text['label-global'];
  337. }
  338. echo " <td>".escape($domain)."</td>\n";
  339. }
  340. echo " <td>".ucwords(escape($row['contact_type']))."&nbsp;</td>\n";
  341. echo " <td class='overflow'><a href='".$list_row_url."'>".escape($row['contact_organization'])."</a>&nbsp;</td>\n";
  342. echo " <td class='shrink no-link hide-xs center'>";
  343. if (is_uuid($row['contact_attachment_uuid'])) {
  344. echo "<i class='fas fa-portrait' style='cursor: pointer;' onclick=\"display_attachment('".escape($row['contact_attachment_uuid'])."');\"></i>";
  345. }
  346. echo " </td>\n";
  347. echo " <td class='no-wrap'><a href='".$list_row_url."'>".escape($row['contact_name_given'])."</a>&nbsp;</td>\n";
  348. echo " <td class='no-wrap'><a href='".$list_row_url."'>".escape($row['contact_name_family'])."</a>&nbsp;</td>\n";
  349. echo " <td class='no-wrap hide-xs'>".escape($row['contact_nickname'])."&nbsp;</td>\n";
  350. echo " <td class='overflow hide-sm-dn'>".escape($row['contact_title'])."&nbsp;</td>\n";
  351. echo " <td class='overflow hide-sm-dn'>".escape($row['contact_role'])."&nbsp;</td>\n";
  352. echo " <td class='hide-sm-dn'>";
  353. if (is_array($contact_sync_sources[$row['contact_uuid']]) && @sizeof($contact_sync_sources[$row['contact_uuid']]) != 0) {
  354. foreach ($contact_sync_sources[$row['contact_uuid']] as $contact_sync_source) {
  355. switch ($contact_sync_source) {
  356. case 'google': echo "<img src='resources/images/icon_gcontacts.png' style='width: 21px; height: 21px; border: none; padding-left: 2px;' alt='".$text['label-contact_google']."'>"; break;
  357. }
  358. }
  359. }
  360. else {
  361. echo "&nbsp;";
  362. }
  363. echo " </td>\n";
  364. if ($_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') {
  365. echo " <td class='action-button'>";
  366. echo button::create(['type'=>'button','title'=>$text['button-view'],'icon'=>$_SESSION['theme']['button_icon_view'],'link'=>$list_row_url]);
  367. echo " </td>\n";
  368. }
  369. echo "</tr>\n";
  370. $x++;
  371. }
  372. unset($contacts);
  373. }
  374. echo "</table>\n";
  375. echo "<br />\n";
  376. echo "<div align='center'>".$paging_controls."</div>\n";
  377. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  378. echo "</form>\n";
  379. //javascript
  380. echo "<script>\n";
  381. echo " function display_attachment(id) {\n";
  382. echo " $('#contact_attachment_layer').load('contact_attachment.php?id=' + id + '&action=display', function(){\n";
  383. echo " $('#contact_attachment_layer').fadeIn(200);\n";
  384. echo " });\n";
  385. echo " }\n";
  386. echo "</script>\n";
  387. //include the footer
  388. require_once "resources/footer.php";
  389. ?>