app_defaults.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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-2023
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. if ($domains_processed == 1) {
  22. //if the default groups do not exist add them
  23. $group = new groups;
  24. $group->defaults();
  25. //create the user view combines username, organization, contact first and last name
  26. $database->execute("DROP VIEW view_users;", null);
  27. $sql = "CREATE VIEW view_users AS ( \n";
  28. $sql .= " select u.domain_uuid, u.user_uuid, d.domain_name, u.username, u.user_status, u.user_enabled, u.add_date, \n";
  29. $sql .= " c.contact_uuid, c.contact_organization, c.contact_name_given ||' '|| c.contact_name_family as contact_name, c.contact_name_given, c.contact_name_family, \n";
  30. $sql .= " ( \n";
  31. $sql .= " select \n";
  32. $sql .= " string_agg(g.group_name, ', ') \n";
  33. $sql .= " from \n";
  34. $sql .= " v_user_groups as ug, \n";
  35. $sql .= " v_groups as g \n";
  36. $sql .= " where \n";
  37. $sql .= " ug.group_uuid = g.group_uuid \n";
  38. $sql .= " and u.user_uuid = ug.user_uuid \n";
  39. $sql .= " ) AS group_names, \n";
  40. $sql .= " ( \n";
  41. $sql .= " select \n";
  42. $sql .= " string_agg(g.group_uuid::text, ', ') \n";
  43. //$sql .= " array_agg(g.group_uuid::text) \n";
  44. $sql .= " from \n";
  45. $sql .= " v_user_groups as ug, \n";
  46. $sql .= " v_groups as g \n";
  47. $sql .= " where \n";
  48. $sql .= " ug.group_uuid = g.group_uuid \n";
  49. $sql .= " and u.user_uuid = ug.user_uuid \n";
  50. $sql .= " ) AS group_uuids, \n";
  51. $sql .= " ( \n";
  52. $sql .= " SELECT group_level \n";
  53. $sql .= " FROM v_user_groups ug, v_groups g \n";
  54. $sql .= " WHERE (ug.group_uuid = g.group_uuid) \n";
  55. $sql .= " AND (u.user_uuid = ug.user_uuid) \n";
  56. $sql .= " ORDER BY group_level DESC \n";
  57. $sql .= " LIMIT 1 \n";
  58. $sql .= " ) AS group_level \n";
  59. $sql .= " from v_contacts as c \n";
  60. $sql .= " right join v_users u on u.contact_uuid = c.contact_uuid \n";
  61. $sql .= " inner join v_domains as d on d.domain_uuid = u.domain_uuid \n";
  62. $sql .= " where 1 = 1 \n";
  63. $sql .= " order by u.username asc \n";
  64. $sql .= "); \n";
  65. $database->execute($sql, null);
  66. unset($sql);
  67. //find rows that have a null group_uuid and set the correct group_uuid
  68. $sql = "select * from v_user_groups ";
  69. $sql .= "where group_uuid is null; ";
  70. $result = $database->select($sql, null, 'all');
  71. if (is_array($result)) {
  72. foreach($result as $row) {
  73. if (!empty($row['group_name'])) {
  74. //get the group_uuid
  75. $sql = "select group_uuid from v_groups ";
  76. $sql .= "where group_name = :group_name ";
  77. $parameters['group_name'] = $row['group_name'];
  78. $group_uuid = $database->execute($sql, $parameters, 'column');
  79. unset($sql, $parameters);
  80. //set the user_group_uuid
  81. $sql = "update v_user_groups set ";
  82. $sql .= "group_uuid = :group_uuid ";
  83. $sql .= "where user_group_uuid = :user_group_uuid; ";
  84. $parameters['group_uuid'] = $group_uuid;
  85. $parameters['user_group_uuid'] = $row['user_group_uuid'];
  86. $database->execute($sql, $parameters);
  87. unset($sql, $parameters);
  88. }
  89. }
  90. }
  91. unset($result);
  92. //update users email if they are all null
  93. $sql = "select count(*) from v_users ";
  94. $sql .= "where user_email is not null; ";
  95. $num_rows = $database->select($sql, null, 'column');
  96. if ($num_rows == 0) {
  97. $sql = "with users AS ( ";
  98. $sql .= " select u.domain_uuid, u.user_uuid, u.username, u.contact_uuid, e.email_address ";
  99. $sql .= " from v_users as u, v_contact_emails as e ";
  100. $sql .= " where u.contact_uuid is not null ";
  101. $sql .= " and u.contact_uuid = e.contact_uuid ";
  102. $sql .= " and e.email_primary = 1 ";
  103. $sql .= ") ";
  104. $sql .= "update v_users ";
  105. $sql .= "set user_email = users.email_address ";
  106. $sql .= "from users ";
  107. $sql .= "where v_users.user_uuid = users.user_uuid;";
  108. $database->execute($sql, null);
  109. }
  110. //find rows that have a null group_uuid and set the correct group_uuid
  111. $sql = "select count(*) from v_default_settings ";
  112. $sql .= "where default_setting_category = 'user'; ";
  113. $num_rows = $database->select($sql, null, 'column');
  114. if ($num_rows > 0) {
  115. //build the array
  116. $x=0;
  117. $array['default_settings'][$x]['default_setting_uuid'] = "38cf53d2-5fae-43ed-be93-33b0a5cc1c38";
  118. $array['default_settings'][$x]['default_setting_category'] = "users";
  119. $array['default_settings'][$x]['default_setting_subcategory'] = "unique";
  120. $x++;
  121. $array['default_settings'][$x]['default_setting_uuid'] = "e3f5f4cd-0f17-428a-b788-2f2db91b6dc7";
  122. $array['default_settings'][$x]['default_setting_category'] = "users";
  123. $array['default_settings'][$x]['default_setting_subcategory'] = "password_length";
  124. $x++;
  125. $array['default_settings'][$x]['default_setting_uuid'] = "51c106d9-9aba-436b-b9b1-ff4937cef706";
  126. $array['default_settings'][$x]['default_setting_category'] = "users";
  127. $array['default_settings'][$x]['default_setting_subcategory'] = "password_number";
  128. $x++;
  129. $array['default_settings'][$x]['default_setting_uuid'] = "f0e601b9-b619-4247-9624-c33605e96fd8";
  130. $array['default_settings'][$x]['default_setting_category'] = "users";
  131. $array['default_settings'][$x]['default_setting_subcategory'] = "password_lowercase";
  132. $x++;
  133. $array['default_settings'][$x]['default_setting_uuid'] = "973b6773-dac0-4041-844e-71c48fc9542c";
  134. $array['default_settings'][$x]['default_setting_category'] = "users";
  135. $array['default_settings'][$x]['default_setting_subcategory'] = "password_uppercase";
  136. $x++;
  137. $array['default_settings'][$x]['default_setting_uuid'] = "a6b6d9cc-fb25-4bc3-ad85-fa530d9b334d";
  138. $array['default_settings'][$x]['default_setting_category'] = "users";
  139. $array['default_settings'][$x]['default_setting_subcategory'] = "password_special";
  140. //add the temporary permission
  141. $p = permissions::new();
  142. $p->add("default_setting_edit", 'temp');
  143. //save to the data
  144. $database->app_name = 'default_setting';
  145. $database->app_uuid = '2c2453c0-1bea-4475-9f44-4d969650de09';
  146. $database->save($array, false);
  147. unset($array);
  148. //remove the temporary permission
  149. $p->delete("default_setting_edit", 'temp');
  150. }
  151. //insert default password reset email template
  152. if (file_exists($_SERVER['DOCUMENT_ROOT'].'/app/email_templates')) {
  153. //add the email templates to the database
  154. $sql = "select count(*) as num_rows from v_email_templates ";
  155. $sql .= "where email_template_uuid = '05b529c4-fba7-4071-bab3-143b076392e7' ";
  156. $num_rows = $database->select($sql, null, 'column');
  157. if ($num_rows == 0) {
  158. //build the array
  159. $x = 0;
  160. $array['email_templates'][$x]['email_template_uuid'] = '05b529c4-fba7-4071-bab3-143b076392e7';
  161. $array['email_templates'][$x]['template_language'] = 'en-us';
  162. $array['email_templates'][$x]['template_category'] = 'password_reset';
  163. $array['email_templates'][$x]['template_subcategory'] = 'default';
  164. $array['email_templates'][$x]['template_subject'] = 'Password Reset';
  165. $array['email_templates'][$x]['template_body'] .= "<html>\n";
  166. $array['email_templates'][$x]['template_body'] .= "<body>\n";
  167. $array['email_templates'][$x]['template_body'] .= "<center><a href='https://\${domain}'><img src='\${logo_full}' style='width: 200px; height: auto; border: none;'></a></center><br />\n";
  168. $array['email_templates'][$x]['template_body'] .= "A password reset was just requested for the FusionPBX user account associated with this email address.<br /><br />\n";
  169. $array['email_templates'][$x]['template_body'] .= "<b>If you submitted this request</b>, click the button below to begin the password reset process for your user account.<br /><br />";
  170. $array['email_templates'][$x]['template_body'] .= "\${reset_button}<br /><br />\n";
  171. $array['email_templates'][$x]['template_body'] .= "If you did not initiate this action, however, please ignore this message and your password will remain unchanged.\n";
  172. $array['email_templates'][$x]['template_body'] .= "If you have questions or concerns regarding this email, please contact your system administrator.";
  173. $array['email_templates'][$x]['template_body'] .= "<br /><br /><br />\n";
  174. $array['email_templates'][$x]['template_body'] .= "<a href='https://\${domain}'><img src='\${logo_shield}' width='31' height='30' border='0' align='left' style='margin-top: 3px; margin-right: 5px;'></a>\n";
  175. $array['email_templates'][$x]['template_body'] .= "<i><b>FusionPBX</b></i><br />\n";
  176. $array['email_templates'][$x]['template_body'] .= "<a href='https://\${domain}'>\${domain}</a>\n";
  177. $array['email_templates'][$x]['template_body'] .= "<br /><br /><br />\n";
  178. $array['email_templates'][$x]['template_body'] .= "</body>\n";
  179. $array['email_templates'][$x]['template_body'] .= "</html>\n";
  180. $array['email_templates'][$x]['template_type'] = 'html';
  181. $array['email_templates'][$x]['template_enabled'] = 'true';
  182. $array['email_templates'][$x]['template_description'] = 'Default password reset email template.';
  183. $x++;
  184. //add the temporary permission
  185. $p = permissions::new();
  186. $p->add("email_template_add", 'temp');
  187. $p->add("email_template_edit", 'temp');
  188. //save to the data
  189. $database->app_name = 'email_templates';
  190. $database->app_uuid = '8173e738-2523-46d5-8943-13883befd2fd';
  191. $database->save($array, false);
  192. unset($array);
  193. //remove the temporary permission
  194. $p->delete("email_template_add", 'temp');
  195. $p->delete("email_template_edit", 'temp');
  196. }
  197. }
  198. //update the user_type when the value is null
  199. $sql = "select count(*) from v_users ";
  200. $sql .= "where user_type is null; ";
  201. $num_rows = $database->select($sql, null, 'column');
  202. if ($num_rows > 0) {
  203. $sql = "update v_users ";
  204. $sql .= "set user_type = 'default' ";
  205. $sql .= "where user_type is null;";
  206. $database->execute($sql, null);
  207. }
  208. }
  209. ?>