app_defaults.php 10 KB

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