contact_json.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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) 2022
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes files
  22. require_once dirname(__DIR__, 2) . "/resources/require.php";
  23. require_once "resources/check_auth.php";
  24. //check permissions
  25. if (permission_exists('contact_view')) {
  26. //access granted
  27. }
  28. else {
  29. echo "access denied";
  30. exit;
  31. }
  32. //set additional variables
  33. $show = $_GET["show"] ?? '';
  34. //retrieve current user's assigned groups (uuids)
  35. foreach ($_SESSION['groups'] as $group_data) {
  36. $user_group_uuids[] = $group_data['group_uuid'];
  37. }
  38. //add user's uuid to group uuid list to include private (non-shared) contacts
  39. $user_group_uuids[] = $_SESSION["user_uuid"];
  40. //add the search term
  41. if (!empty($_GET["search"])) {
  42. $search = strtolower($_GET["search"]);
  43. }
  44. //get the list of contacts
  45. $sql = "select *, ";
  46. $sql .= "( ";
  47. $sql .= " select a.contact_attachment_uuid from v_contact_attachments as a ";
  48. $sql .= " where a.contact_uuid = c.contact_uuid and a.attachment_primary = 1 ";
  49. $sql .= ") as contact_attachment_uuid ";
  50. $sql .= "from v_contacts as c ";
  51. $sql .= "where true ";
  52. if ($show != "all" || !permission_exists('contact_all')) {
  53. $sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) ";
  54. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  55. }
  56. if (!permission_exists('contact_domain_view')) {
  57. $sql .= "and ( "; //only contacts assigned to current user's group(s) and those not assigned to any group
  58. $sql .= " contact_uuid in ( ";
  59. $sql .= " select contact_uuid from v_contact_groups ";
  60. $sql .= " where ";
  61. if (!empty($user_group_uuids)) {
  62. foreach ($user_group_uuids as $index => $user_group_uuid) {
  63. if (is_uuid($user_group_uuid)) {
  64. $sql_where_or[] = "group_uuid = :group_uuid_".$index;
  65. $parameters['group_uuid_'.$index] = $user_group_uuid;
  66. }
  67. }
  68. if (!empty($sql_where_or)) {
  69. $sql .= " ( ".implode(' or ', $sql_where_or)." ) ";
  70. }
  71. unset($sql_where_or, $index, $user_group_uuid);
  72. }
  73. $sql .= " and domain_uuid = :domain_uuid ";
  74. $sql .= " ) ";
  75. $sql .= " or contact_uuid in ( ";
  76. $sql .= " select contact_uuid from v_contact_users ";
  77. $sql .= " where user_uuid = :user_uuid ";
  78. $sql .= " and domain_uuid = :domain_uuid ";
  79. $sql .= "";
  80. $sql .= " ) ";
  81. $sql .= ") ";
  82. $parameters['user_uuid'] = $_SESSION['user_uuid'];
  83. }
  84. if (isset($search)) {
  85. if (is_numeric($search)) {
  86. $sql .= "and contact_uuid in ( ";
  87. $sql .= " select contact_uuid from v_contact_phones ";
  88. $sql .= " where phone_number like :search ";
  89. $sql .= ") ";
  90. }
  91. else {
  92. //open container
  93. $sql .= "and ( ";
  94. //search contact
  95. $sql .= "contact_uuid in ( ";
  96. $sql .= " select contact_uuid from v_contacts ";
  97. $sql .= " where domain_uuid = :domain_uuid ";
  98. $sql .= " and ( ";
  99. $sql .= " lower(contact_organization) like :search or ";
  100. $sql .= " lower(contact_name_given) like :search or ";
  101. $sql .= " lower(contact_name_family) like :search or ";
  102. $sql .= " lower(contact_nickname) like :search or ";
  103. $sql .= " lower(contact_title) like :search or ";
  104. $sql .= " lower(contact_category) like :search or ";
  105. $sql .= " lower(contact_role) like :search or ";
  106. $sql .= " lower(contact_url) like :search or ";
  107. $sql .= " lower(contact_time_zone) like :search or ";
  108. $sql .= " lower(contact_note) like :search or ";
  109. $sql .= " lower(contact_type) like :search ";
  110. $sql .= " ) ";
  111. $sql .= ") ";
  112. //search contact emails
  113. if (permission_exists('contact_email_view')) {
  114. $sql .= "or contact_uuid in ( ";
  115. $sql .= " select contact_uuid from v_contact_emails ";
  116. $sql .= " where domain_uuid = :domain_uuid ";
  117. $sql .= " and ( ";
  118. $sql .= " lower(email_address) like :search or ";
  119. $sql .= " lower(email_description) like :search ";
  120. $sql .= " ) ";
  121. $sql .= ") ";
  122. }
  123. //search contact notes
  124. if (permission_exists('contact_note_view')) {
  125. $sql .= "or contact_uuid in ( ";
  126. $sql .= " select contact_uuid from v_contact_notes ";
  127. $sql .= " where domain_uuid = :domain_uuid ";
  128. $sql .= " and lower(contact_note) like :search ";
  129. $sql .= ") ";
  130. }
  131. //close container
  132. $sql .= ") ";
  133. }
  134. $parameters['search'] = '%'.$search.'%';
  135. }
  136. $sql .= "order by contact_organization asc ";
  137. $sql .= "limit 300 ";
  138. $database = new database;
  139. $contact_array = $database->select($sql, $parameters ?? null, 'all');
  140. unset($sql, $parameters);
  141. //return the contacts as json
  142. $i = 0;
  143. if (!empty($contact_array)) {
  144. foreach($contact_array as $row) {
  145. $contact_name = array();
  146. if (!empty($row['contact_organization'])) { $contact_name[] = $row['contact_organization']; }
  147. if (!empty($row['contact_name_family'])) { $contact_name[] = $row['contact_name_family']; }
  148. if (!empty($row['contact_name_given'])) { $contact_name[] = $row['contact_name_given']; }
  149. if (empty($row['contact_name_family']) && empty($row['contact_name_given']) && !empty($row['contact_nickname'])) { $contact_name[] = $row['contact_nickname']; }
  150. $contacts[$i]['id'] = $row['contact_uuid'];
  151. $contacts[$i]['name'] = implode(', ', $contact_name);
  152. $i++;
  153. }
  154. echo json_encode($contacts, true);
  155. }
  156. ?>