<?php
if ($_SERVER['REMOTE_ADDR'] != '46.139.109.235') {
  die();
}

die();

require('../maintenance/config.php');

$msh2->query("SELECT user_id,group_id,count(*) FROM " . JMLADATBAZIS . ".eng_user_usergroup_map GROUP BY user_id HAVING COUNT(*)=1  AND group_id=2;");
$res = $msh2->fetchAssoc();

foreach ($res as $key => $row) {
  $msh2->query("SELECT * FROM " . JMLADATBAZIS . ".eng_users WHERE `id`=" . $row['user_id'] . " LIMIT 1;");
  $user = $msh2->fetchAssoc();
  $user = $user[0];

  if (trim($user['email']) > '') {
    $msh2->query("SELECT * FROM " . JMLADATBAZIS . ".jml_users WHERE `email`='" . trim($user['email']) . "';");
    $exist = $msh2->fetchAssoc();

    if (empty($exist)) {
      unset($user['id']);
      $msh2->insert(JMLADATBAZIS . ".jml_users", $user);
      $newID = $msh2->getInsertedId();
      print('added: ' . $user['email'] . ' | ');
    } else {
      $newID = $exist[0]['id'];
      print('already exist: ' . $exist[0]['email'] . ' | ');
    }

    print 'newID: ' . $newID . ' | ';

    $msh2->query("SELECT * FROM " . JMLADATBAZIS . ".jml_user_usergroup_map WHERE `user_id`=" . $newID . ";");
    $ugm = $msh2->fetchAssoc();
    if (empty($ugm) && (int)$newID > 0) {
      $msh2->insert(JMLADATBAZIS . ".jml_user_usergroup_map", array('user_id' => $newID, 'group_id' => 2));
      print('group added | ');
    } else {
      print('group not added | ');
    }

    $msh2->query("SELECT * FROM " . JMLADATBAZIS . ".jml_user_profiles WHERE `user_id`=" . $newID . ";");
    $profile = $msh2->fetchAssoc();
    if (empty($profile) && (int)$newID > 0) {
      $msh2->query("SELECT * FROM " . JMLADATBAZIS . ".eng_user_profiles WHERE `user_id`=" . $row['user_id'] . ";");
      $prof = $msh2->fetchAssoc();
      foreach ($prof as $k => $r) {
        $r['user_id'] = $newID;
        $msh2->insert(JMLADATBAZIS . ".jml_user_profiles", $r);
      }
      print('profile added | ');
    } else {
      print('profile not added | ');
    }

    print '<br />';
  }
  unset($newID);
}
