123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- if (!class_exists("plug_usermanager")) {
- function arrayFromCSV($file, $hasFieldNames = false, $delimiter = ',', $enclosure = '') {
- $result = Array();
- $size = filesize($file) + 1;
- $file = fopen($file, 'r');
- #TO DO: There must be a better way of finding out the size of the longest row... until then
- if ($hasFieldNames) {
- $keys = fgetcsv($file, $size, $delimiter, $enclosure);
- }
- while ($row = fgetcsv($file, $size, $delimiter, $enclosure)) {
- $n = count($row);
- $res = array();
- for ($i = 0; $i < $n; $i++) {
- $idx = ($hasFieldNames) ? $keys[$i] : $i;
- $res[$idx] = $row[$i];
- }
- $result[] = $res;
- }
- fclose($file);
- return $result;
- }
- function sanitize_username($username) {
- $parts = explode("@", $username);
- if (count($parts) == 2) {
- $username = $parts[0];
- }
- return $username;
- }
- class plug_usermanager {
- function __construct($ns) {
- $this->title = __("plug_usermanager", "dagsopt");
- $this->pluginname = $ns->pluginname;
- $this->file = $ns->file;
- $this->ns = $ns;
- }
- function start() {
- // register_setting("section", "demo-file", array(&this,"handle_file_upload"));
- add_role("betalende-abonnent", "Betalende abonnent", array('read' => true, 'level_0' => true));
- }
- function handle_file_upload($option) {
- if (!empty($_FILES["demo-file"]["tmp_name"])) {
- $urls = wp_handle_upload($_FILES["demo-file"], array('test_form' => FALSE));
- $temp = $urls["url"];
- return $temp;
- }
- return $option;
- }
- function help() {
- ?>
- plug_usermanager
- <?php
- }
- function Option($pre) {
- update_option($pre . '_delimiter', $_POST[$pre . '_delimiter']);
- update_option($pre . '_textenclose', $_POST[$pre . '_textenclose']);
- $nonce = $_REQUEST['_wpnonce'];
- if (!wp_verify_nonce($nonce, 'filen-nonce')) {
- // This nonce is not valid.
- die(__('Security check', 'textdomain'));
- } else {
- // The nonce was valid.
- // Do stuff here.
- if (isset($_FILES) && !empty($_FILES)) {
- if ($_FILES['filen']) {
- $upload = wp_upload_dir();
- $upload_dir = $upload['basedir'];
- $upload_dir = $upload_dir . '/assets';
- move_uploaded_file($_FILES['filen']['tmp_name'], $upload_dir . "/users.csv");
- }
- }
- }
- }
- function Cruncher($pre) {
- if (isset($_REQUEST['nodryrun'])) {
- $testmode = false;
- echo ("RUNNING LIVE<br>");
- } else {
- echo ("RUNNING TEST<br>");
- $testmode = true;
- }
- $upload = wp_upload_dir();
- $upload_dir = $upload['basedir'];
- $upload_dir = $upload_dir . '/assets';
- $arr = arrayFromCSV($upload_dir . "/users.csv", true, get_option($pre . '_delimiter', ';'), get_option($pre . '_textenclose', 'double') == "double" ? '"' : "'");
- $ii = 0;
- $max = count($arr);
- $resu = array();
- foreach ($arr as $num => $user) {
- $ii++;
- echo ("$max : $ii -> ");
- flush();
- if ($user['Spærret'] == "") {
- $user_email = $user['E-mail'];
- $user_name = sanitize_username("T" . $user['Telefon']);
- $user_id = username_exists($user_name);
- if (!$user_id && false == email_exists($user_email)) {
- $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
- if ($testmode) {
- //$user_id = wp_create_user( $user_name, $random_password, $user_email );
- //wp_update_user( array( 'ID' => $user_id, 'role' => 'betalende-abonnent' ) );
- //wp_send_new_user_notifications($user_id,'both');
- $resu[] = "$user_id mock created : $user_name $user_email ";
- } else {
- $user_id = wp_create_user($user_name, $random_password, $user_email);
- wp_update_user(array('ID' => $user_id, 'role' => 'betalende-abonnent'));
- wp_send_new_user_notifications($user_id, 'both');
- $resu[] = "$user_id created : $user_name $user_email ";
- }
- } else {
- $resu[] = "$user_id or $user_email exists";
- $random_password = __('User already exists. Password inherited.', 'textdomain');
- }
- echo ("$user_id, $user_name, $random_password, $user_email <br>");
- flush();
- } else {
- $user_email = $user['E-mail'];
- $resu[] = "$user_email spærret";
- }
- }
- if ($testmode) {
- print_r($resu);
- echo ("DONE test");
- } else {
- $aemail = get_bloginfo('admin_email');
- $rr = wp_mail($aemail, 'CCR ' . count($resu) . " : " . getenv('HOSTNAME') . " : " . $_SERVER['REMOTE_ADDR'] . " : " . $_SERVER['SERVER_ADDR'] . " : " . $_SERVER['SERVER_NAME'] . " " . date("U"), "::" . print_r($resu, true));
- echo ("DONE live");
- }
- if (!isset($_REQUEST['nodryrun'])) {
- ?>
- <a href="/wp-admin/admin.php?page=dagsopt-options&crunchit=1&nodryrun=1" class="btn">Crunch live</a>
- <?php
- }
- }
- /*<input id="link_image" value="test">
- <img src="" id="background_image">
- <a id="upload_image_button" href="#"><?php _e('Set image', 'dagsopt'); ?></a>
- */
- function admin_line($pre) {
- $upload = wp_upload_dir();
- $upload_dir = $upload['basedir'];
- $upload_dir = $upload_dir . '/assets';
- ?>
- <table>
- <?php
- if (file_exists($upload_dir . "/users.csv")) {
- $lastmodified = date("F d Y H:i:s.", filemtime($upload_dir . "/users.csv"));
- $data = file_get_contents($upload_dir . "/users.csv");
- $lines = explode("\n", $data);
- ?>
- <tr><td>
- Sidste opdateret
- </td><td>
- <?php echo $lastmodified ?>
- </td></tr>
- <tr><td>
- Størrelse
- </td><td>
- <?php echo strlen($data) ?> bytes / linier: <?php echo (count($lines) - 1); ?>
- </td></tr>
- <tr><td>Kør opdatering</td><td><a href="/wp-admin/admin.php?page=dagsopt-options&crunchit=1" class="btn">Crunch</a></td></tr>
- <?php }?>
- <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>
- <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>
- </table>
- <?php
- $nonce = wp_create_nonce('filen-nonce');
- ?>
- <input type="file" name="filen">
- <input type="hidden" name="_wpnonce" value="<?php echo $nonce ?>">
- <?php
- }
- }
- global $plug_usermanager;
- $plug_usermanager = new plug_usermanager($this);
- $this->dagsopt['plug_usermanager'] = $plug_usermanager;
- }
|