plug_usermanager.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. if (!class_exists("plug_usermanager")) {
  3. function arrayFromCSV($file, $hasFieldNames = false, $delimiter = ',', $enclosure = '') {
  4. $result = Array();
  5. $size = filesize($file) + 1;
  6. $file = fopen($file, 'r');
  7. #TO DO: There must be a better way of finding out the size of the longest row... until then
  8. if ($hasFieldNames) {
  9. $keys = fgetcsv($file, $size, $delimiter, $enclosure);
  10. }
  11. while ($row = fgetcsv($file, $size, $delimiter, $enclosure)) {
  12. $n = count($row);
  13. $res = array();
  14. for ($i = 0; $i < $n; $i++) {
  15. $idx = ($hasFieldNames) ? $keys[$i] : $i;
  16. $res[$idx] = $row[$i];
  17. }
  18. $result[] = $res;
  19. }
  20. fclose($file);
  21. return $result;
  22. }
  23. function sanitize_username($username) {
  24. $parts = explode("@", $username);
  25. if (count($parts) == 2) {
  26. $username = $parts[0];
  27. }
  28. return $username;
  29. }
  30. class plug_usermanager {
  31. function __construct($ns) {
  32. $this->title = __("plug_usermanager", "dagsopt");
  33. $this->pluginname = $ns->pluginname;
  34. $this->file = $ns->file;
  35. $this->ns = $ns;
  36. }
  37. function start() {
  38. // register_setting("section", "demo-file", array(&this,"handle_file_upload"));
  39. add_role("betalende-abonnent", "Betalende abonnent", array('read' => true, 'level_0' => true));
  40. }
  41. function handle_file_upload($option) {
  42. if (!empty($_FILES["demo-file"]["tmp_name"])) {
  43. $urls = wp_handle_upload($_FILES["demo-file"], array('test_form' => FALSE));
  44. $temp = $urls["url"];
  45. return $temp;
  46. }
  47. return $option;
  48. }
  49. function help() {
  50. ?>
  51. plug_usermanager
  52. <?php
  53. }
  54. function Option($pre) {
  55. update_option($pre . '_delimiter', $_POST[$pre . '_delimiter']);
  56. update_option($pre . '_textenclose', $_POST[$pre . '_textenclose']);
  57. $nonce = $_REQUEST['_wpnonce'];
  58. if (!wp_verify_nonce($nonce, 'filen-nonce')) {
  59. // This nonce is not valid.
  60. die(__('Security check', 'textdomain'));
  61. } else {
  62. // The nonce was valid.
  63. // Do stuff here.
  64. if (isset($_FILES) && !empty($_FILES)) {
  65. if ($_FILES['filen']) {
  66. $upload = wp_upload_dir();
  67. $upload_dir = $upload['basedir'];
  68. $upload_dir = $upload_dir . '/assets';
  69. move_uploaded_file($_FILES['filen']['tmp_name'], $upload_dir . "/users.csv");
  70. }
  71. }
  72. }
  73. }
  74. function Cruncher($pre) {
  75. if (isset($_REQUEST['nodryrun'])) {
  76. $testmode = false;
  77. echo ("RUNNING LIVE<br>");
  78. } else {
  79. echo ("RUNNING TEST<br>");
  80. $testmode = true;
  81. }
  82. $upload = wp_upload_dir();
  83. $upload_dir = $upload['basedir'];
  84. $upload_dir = $upload_dir . '/assets';
  85. $arr = arrayFromCSV($upload_dir . "/users.csv", true, get_option($pre . '_delimiter', ';'), get_option($pre . '_textenclose', 'double') == "double" ? '"' : "'");
  86. $ii = 0;
  87. $max = count($arr);
  88. $resu = array();
  89. foreach ($arr as $num => $user) {
  90. $ii++;
  91. echo ("$max : $ii -> ");
  92. flush();
  93. if ($user['Spærret'] == "") {
  94. $user_email = $user['E-mail'];
  95. $user_name = sanitize_username("T" . $user['Telefon']);
  96. $user_id = username_exists($user_name);
  97. if (!$user_id && false == email_exists($user_email)) {
  98. $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
  99. if ($testmode) {
  100. //$user_id = wp_create_user( $user_name, $random_password, $user_email );
  101. //wp_update_user( array( 'ID' => $user_id, 'role' => 'betalende-abonnent' ) );
  102. //wp_send_new_user_notifications($user_id,'both');
  103. $resu[] = "$user_id mock created : $user_name $user_email ";
  104. } else {
  105. $user_id = wp_create_user($user_name, $random_password, $user_email);
  106. wp_update_user(array('ID' => $user_id, 'role' => 'betalende-abonnent'));
  107. wp_send_new_user_notifications($user_id, 'both');
  108. $resu[] = "$user_id created : $user_name $user_email ";
  109. }
  110. } else {
  111. $resu[] = "$user_id or $user_email exists";
  112. $random_password = __('User already exists. Password inherited.', 'textdomain');
  113. }
  114. echo ("$user_id, $user_name, $random_password, $user_email <br>");
  115. flush();
  116. } else {
  117. $user_email = $user['E-mail'];
  118. $resu[] = "$user_email spærret";
  119. }
  120. }
  121. if ($testmode) {
  122. print_r($resu);
  123. echo ("DONE test");
  124. } else {
  125. $aemail = get_bloginfo('admin_email');
  126. $rr = wp_mail($aemail, 'CCR ' . count($resu) . " : " . getenv('HOSTNAME') . " : " . $_SERVER['REMOTE_ADDR'] . " : " . $_SERVER['SERVER_ADDR'] . " : " . $_SERVER['SERVER_NAME'] . " " . date("U"), "::" . print_r($resu, true));
  127. echo ("DONE live");
  128. }
  129. if (!isset($_REQUEST['nodryrun'])) {
  130. ?>
  131. <a href="/wp-admin/admin.php?page=dagsopt-options&crunchit=1&nodryrun=1" class="btn">Crunch live</a>
  132. <?php
  133. }
  134. }
  135. /*<input id="link_image" value="test">
  136. <img src="" id="background_image">
  137. <a id="upload_image_button" href="#"><?php _e('Set image', 'dagsopt'); ?></a>
  138. */
  139. function admin_line($pre) {
  140. $upload = wp_upload_dir();
  141. $upload_dir = $upload['basedir'];
  142. $upload_dir = $upload_dir . '/assets';
  143. ?>
  144. <table>
  145. <?php
  146. if (file_exists($upload_dir . "/users.csv")) {
  147. $lastmodified = date("F d Y H:i:s.", filemtime($upload_dir . "/users.csv"));
  148. $data = file_get_contents($upload_dir . "/users.csv");
  149. $lines = explode("\n", $data);
  150. ?>
  151. <tr><td>
  152. Sidste opdateret
  153. </td><td>
  154. <?php echo $lastmodified ?>
  155. </td></tr>
  156. <tr><td>
  157. Størrelse
  158. </td><td>
  159. <?php echo strlen($data) ?> bytes / linier: <?php echo (count($lines) - 1); ?>
  160. </td></tr>
  161. <tr><td>Kør opdatering</td><td><a href="/wp-admin/admin.php?page=dagsopt-options&crunchit=1" class="btn">Crunch</a></td></tr>
  162. <?php }?>
  163. <tr><td><?php echo (__("delimiter:", "dagsopt")) ?></td><td><input type="text" name="<?php echo ($pre . '_delimiter') ?>" value="<?php echo (get_option($pre . '_delimiter', ';')) ?>"></td></tr>
  164. <tr><td><?php echo (__("textenclose:", "dagsopt")) ?></td><td><input type="text" name="<?php echo ($pre . '_textenclose') ?>" value="<?php echo (get_option($pre . '_textenclose', '"')) ?>"></td></tr>
  165. </table>
  166. <?php
  167. $nonce = wp_create_nonce('filen-nonce');
  168. ?>
  169. <input type="file" name="filen">
  170. <input type="hidden" name="_wpnonce" value="<?php echo $nonce ?>">
  171. <?php
  172. }
  173. }
  174. global $plug_usermanager;
  175. $plug_usermanager = new plug_usermanager($this);
  176. $this->dagsopt['plug_usermanager'] = $plug_usermanager;
  177. }