users_bulk_add.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-2012
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. $dq = '"';
  22. $self = $_SERVER['PHP_SELF'];
  23. ini_set('auto_detect_line_endings', '1');
  24. function get_domain_uuids(PDO $db) {
  25. $query = sprintf("SELECT domain_uuid, domain_name FROM v_domains;");
  26. $stmt = $db->query($query);
  27. $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
  28. $result_count = count($results);
  29. for ($i = 0; $i < $result_count; $i++) {
  30. $domain_name = $results[$i]['domain_name'];
  31. $domain_uuid = $results[$i]['domain_uuid'];
  32. $domain_uuids[$domain_name] = $domain_uuid;
  33. }
  34. return $domain_uuids;
  35. }
  36. function generate_insert_query($line, $places, $table, PDO $db, $domain_uuids) {
  37. global $v_salt;
  38. foreach ($places as $field => $place) {
  39. $fields[] = $field;
  40. if ($field == 'password') {
  41. $values[] = $db->quote(md5($v_salt.$line[$place]));
  42. } else {
  43. $values[] = $db->quote($line[$place]);
  44. }
  45. }
  46. $username = $line[$places['username']];
  47. switch ($table) {
  48. case 'v_users':
  49. if (empty($username) || $username == 'NULL') return false;
  50. // we'll assume that every user should be able to login
  51. if (!in_array('password', $fields)){
  52. $fields[] = 'password';
  53. $values[] = $db->quote(md5($v_salt.$username));
  54. }
  55. break;
  56. case 'v_extensions':
  57. $ext = $line[$places['extension']];
  58. if (empty($ext) || $ext == 'NULL') return false;
  59. // let's also assume every extension should also have a vm pin
  60. if (!in_array('vm_password', $fields)) {
  61. $fields[] = 'vm_password';
  62. $values[] = $db->quote($ext);
  63. }
  64. /* if we have a username but no user_list,
  65. * let's assume we want the extension tied to the current user
  66. */
  67. if (!in_array('user_list', $fields) && $username) {
  68. $fields[] = 'user_list';
  69. $values[] = $db->quote(sprintf('|%s|', $username));
  70. }
  71. $idx = array_search('username', $fields);
  72. unset($fields[$idx]);
  73. unset($values[$idx]);
  74. break;
  75. default:
  76. break;
  77. }
  78. if (!in_array('domain_uuid')) {
  79. //print "domain_uuid not found, adding one for localhost<br>\n";
  80. //printf('<pre>%s</pre>', print_r($domain_uuids, true));
  81. $fields[] = 'domain_uuid';
  82. $values[] = $domain_uuids['localhost'];
  83. }
  84. $query = sprintf('INSERT INTO %s (%s) VALUES (%s);'
  85. , $table, join(', ', $fields), join(', ', $values)
  86. );
  87. return $query;
  88. }
  89. function get_field_places($first_line, $valid_fields) {
  90. $places = array();
  91. foreach ($valid_fields as $key => $value) {
  92. //print "Looking for $value in valid fields<br>\n";
  93. $idx = array_search($value, $first_line);
  94. if ($idx !== false) {
  95. $places[$value] = $idx;
  96. }
  97. }
  98. return $places;
  99. }
  100. function check_required_fields($line, $all_fields) {
  101. //printf('<pre>%s</pre>', print_r($line, true));
  102. if (!in_array('username', $line) || !in_array('extension', $line)) {
  103. $all_fields = array_unique($all_fields);
  104. sort($all_fields);
  105. printf("You need to add a header line to the csv, the valid fields are:<br>\n%s"
  106. , implode("<br>\n", $all_fields)
  107. );
  108. return false;
  109. }
  110. return true;
  111. }
  112. function insert_db_row($db, $line, $places, $table, $domain_uuids) {
  113. global $inserted;
  114. //printf("<pre>%s</pre>\n", print_r($inserted, true));
  115. $inserted[$table]++;
  116. $query = generate_insert_query($line, $places, $table, $db, $domain_uuids);
  117. //print "QUERY: $query<br>\n";
  118. if (empty($query)) {
  119. return;
  120. }
  121. $affected_rows = $db->exec($query);
  122. //printf("we affected %s rows<br>\n", $affected_rows);
  123. }
  124. include "root.php";
  125. require_once "resources/require.php";
  126. require_once "resources/check_auth.php";
  127. if (!if_group("admin") && !if_group("superadmin")) {
  128. printf("access denied");
  129. exit;
  130. }
  131. require_once "resources/header.php";
  132. require_once "resources/paging.php";
  133. $inserted = array('v_users' => 0, 'v_extensions' => 0, 'v_group_users' => 0);
  134. if (is_array($_FILES) && array_key_exists('users_file', $_FILES)) {
  135. $domain_uuids = get_domain_uuids($db);
  136. $user_fields = get_db_field_names($db, 'v_users');
  137. //printf("<pre>users => %s<br></pre>\n", print_r($user_fields, true));
  138. $extension_fields = get_db_field_names($db, 'v_extensions');
  139. //printf("<pre>exts => %s<br></pre>\n", print_r($extension_fields, true));
  140. $all_fields = array_merge($user_fields, $extension_fields);
  141. //printf("<pre>all => %s</pre>\n", print_r($all_fields, true));
  142. $fh = fopen($_FILES['users_file']['tmp_name'], 'r');
  143. if (!$fh) {
  144. //printf('<pre>%s</pre>', print_r($_FILES, true));
  145. print "Couldn't open the uploaded file<br>\n";
  146. } else {
  147. $line = fgetcsv($fh, null, ',');
  148. $user_places = get_field_places($line, $user_fields);
  149. $extension_places = get_field_places($line, $extension_fields);
  150. if (array_key_exists('username', $user_places)) {
  151. $extension_places['username'] = $user_places['username'];
  152. }
  153. //printf("<pre>user_places => %s</pre>\n", print_r($user_places, true));
  154. //printf("<pre>ext_places => %s</pre>\n", print_r($extension_places, true));
  155. //printf("<pre>FIRST LINE => %s</pre>", print_r($line, true));
  156. if (check_required_fields($line, $all_fields)) {
  157. while ($line = fgetcsv($fh, null, ',')) {
  158. // create user
  159. insert_db_row($db, $line, $user_places, 'v_users', $domain_uuids);
  160. // add user to members group
  161. $grp_line = array('member', $line[$user_places['username']]);
  162. $grp_places = array('group_name' => 0, 'username' => 1);
  163. insert_db_row($db, $grp_line, $grp_places, 'v_group_users', $domain_uuids);
  164. // add user's extension
  165. insert_db_row($db, $line, $extension_places, 'v_extensions', $domain_uuids);
  166. }
  167. }
  168. fclose($fh);
  169. //printf("<pre>%s</pre>\n", print_r($inserted, true));
  170. printf("<h3>Bulk Add Results:</h3>\n");
  171. printf("<table>\n");
  172. foreach ($inserted as $key => $value) {
  173. printf("<tr>\n");
  174. $name_parts = explode('_', $key);
  175. $table_prefix = array_shift($name_parts);
  176. printf("<td>");
  177. foreach ($name_parts as $phrase_word) {
  178. printf('%s ', ucfirst($phrase_word));
  179. }
  180. printf("</td>\n");
  181. printf("<td>%d</td>\n", $value);
  182. printf("</tr>\n");
  183. }
  184. printf("</table>\n");
  185. }
  186. }
  187. printf("<form method=${dq}POST${dq} action=${dq}$self${dq} enctype=${dq}multipart/form-data${dq}");
  188. printf("<input type=${dq}file${dq} name=${dq}users_file${dq}");
  189. printf("<input type=${dq}submit${dq} value=${dq}Upload${dq}");
  190. printf("</form>");
  191. require_once "resources/footer.php";