Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 37.50 KB | None | 0 0
  1. set_time_limit(0);
  2. ini_set("error_log", 1);
  3. error_reporting(E_ALL);
  4. $start = microtime(TRUE);
  5. //require_once('inc/header.inc.php');
  6. require_once(BX_DIRECTORY_PATH_INC . 'phpmailer.inc.php');
  7. bx_import('BxDolDb');
  8. $db = new BxDolDb();
  9. echo "Comienza el cron <br>";
  10. $inserts = $updates = 0;
  11. $thecore_users = file_get_contents("http://tag/thecore/webservices/tlife_sync.php");
  12. $mail_body = "";
  13. $new_profiles = $update_profiles = $no_new = $no_updates = array();
  14. $vhurs = $netlogins = array();
  15.  
  16. if ($thecore_users === FALSE) {
  17.     echo "No se pudo leer el web service<br>";
  18. } else {
  19.     $thecore_users = json_decode($thecore_users, TRUE);
  20.     if ($thecore_users === FALSE) {
  21.         echo "No se pudo convertir la data leida del web service a array: $thecore_users<br>";
  22.     } else {
  23.         echo "Se leyeron " . count($thecore_users) . " desde The Core<br>";
  24.         foreach ($thecore_users as $user) {
  25.             $processed = NULL;
  26.             $vhur = $user["VHUR"];
  27.             $network_login = addslashes($user["network_login"]);
  28.  
  29.  
  30.  
  31.  
  32.             if ((!is_null($vhur) || strlen($vhur) > 0) && $vhur !== "0") {// Actualizar por VHUR
  33.                 $query_select = "SELECT ID_VHUR,Position_id,SuperUnit_id,Position_id FROM Profiles WHERE ID_VHUR = $vhur";
  34.                 $profile = $db->getAll($query_select);
  35.  
  36.                 if (count($profile) >= 1) {
  37.                     $profile = $profile[0];
  38.                     if (validate_user($user)) {
  39.                         $query_update = prepared_update($user, $db, 1);
  40.  
  41.                         if ($db->query($query_update)) {
  42.                             $updates++;
  43.                         }
  44.                         $processed = TRUE;
  45.  
  46.                         $last_user = $db->getAll("SELECT * FROM Profiles WHERE ID_VHUR = $vhur");
  47.                         $last_user = $last_user[0];
  48.                         if ($last_user["SuperUnit_id"] !== $profile["SuperUnit_id"]) {
  49.                             $net = $last_user['network_login'];
  50.                             $oldsu = $last_user['SuperUnit_id'];
  51.                             $newsu = $profile['SuperUnit_id'];
  52.                             privacy_group($last_user, $user, $db);
  53.                         }
  54.                         array_push($vhurs, $vhur);
  55.                     } else {
  56.                         $temp_array = array("network_login" => $user["NickName"], "email" => $user["email"], "vhur" => $user["VHUR"], "position" => $user["position"], "position_id" => $user["position_id"], "unit" => $user["unit"], "unit_id" => $user["unit_id"], "superunit" => $user["superunit"], "superunit_id" => $user["superunit_id"], "domain_id" => $user["domain_id"], "domain" => $user["domain"]);
  57.                         array_push($no_updates, $temp_array);
  58.                         $processed = TRUE;
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             if (!empty($network_login) && is_null($processed)) {//Actualizar por network login
  64.                 $query_select = "SELECT ID_VHUR,Position_id,SuperUnit_id,Position_id FROM Profiles WHERE network_login = '$network_login'";
  65.                 $profile2 = $db->getAll($query_select);
  66.                 if (count($profile2) >= 1) {
  67.                     //Actualizar toda la informacion menos el network_login
  68.                     $profile2 = $profile2[0];
  69.                     if (validate_user($user)) {//is_numeric
  70.                         $query_update = prepared_update($user, $db, 2);
  71.                         if ($db->query($query_update)) {
  72.                             $updates++;
  73.                         }
  74.  
  75.                         $processed = TRUE;
  76.                         $last_user = $db->getAll("SELECT * FROM Profiles WHERE network_login = '$network_login'");
  77.                         $last_user = $last_user[0];
  78.  
  79.                         if ($last_user["SuperUnit_id"] !== $profile2["SuperUnit_id"]) {
  80.                             $net = $last_user['network_login'];
  81.                             $oldsu = $last_user['SuperUnit_id'];
  82.                             $newsu = $profile2['SuperUnit_id'];
  83.                             privacy_group($last_user, $user, $db);
  84.                         }
  85.  
  86.                         $vhur = $user["VHUR"];
  87.                         array_push($vhurs, $vhur);
  88.                     } else {
  89.                         $temp_array = array("network_login" => $user["NickName"], "email" => $user["email"], "vhur" => $user["VHUR"], "position" => $user["position"], "position_id" => $user["position_id"], "unit" => $user["unit"], "unit_id" => $user["unit_id"], "superunit" => $user["superunit"], "superunit_id" => $user["superunit_id"], "domain_id" => $user["domain_id"], "domain" => $user["domain"]);
  90.                         array_push($no_updates, $temp_array);
  91.                         $processed = TRUE;
  92.                     }
  93.                 }
  94.             }
  95.  
  96.             if (!$processed || is_null($processed)) {
  97.                 if (validate_user($user)) {//is_numeric
  98.                     $vhur = $user["VHUR"];
  99.                     $network_login = addslashes($user["NickName"]);
  100.                     $query_insert = prepared_insert($user, $db);
  101.  
  102.                     if ($db->query($query_insert)) {
  103.                         $inserts++;
  104.                     }
  105.  
  106.                     $processed = TRUE;
  107.                     $temp = $db->lastId();
  108.                     $last_user = $db->getAll("SELECT * FROM Profiles WHERE ID = $temp");
  109.                     $last_user = $last_user[0];
  110.                     /* Hacer al usuario amigo de los perfiles administrativos */
  111.                     $query = "SELECT ID
  112.                                              FROM Profiles AS p
  113.                                              WHERE p.NickName LIKE 'TLifeSupport'";
  114.  
  115.                     $admin_profiles = $db->getAll($query);
  116.  
  117.                     foreach ($admin_profiles as $profile) {
  118.                         $id = $profile["ID"];
  119.                         $pro = $last_user["ID"];
  120.                         $query_insert = "INSERT INTO `sys_friend_list` (`ID`, `Profile`, `Check`) VALUES ($pro,$id, 1);"; //"INSERT INTO sys_friend_list (ID, Profile,Check) VALUES ($id,$pro,1);";
  121.  
  122.                         $db->query($query_insert);
  123.                     }
  124.                     $query_insert = "";
  125.  
  126.                     $date_time = date("Y-m-d H:i:s");
  127.                     $db->query("INSERT INTO `sys_acl_levels_members` SET `IDMember` = '{$last_user['ID']}', `IDLevel` = 5, `DateStarts` = '$date_time', `TransactionID` = 0; ");
  128.                     privacy_group($last_user, $user, $db);
  129.                     $temp_array = array("network_login" => $user["NickName"], "email" => $user["email"], "vhur" => $user["VHUR"], "position" => $user["position"], "position_id" => $user["position_id"], "unit" => $user["unit"], "unit_id" => $user["unit_id"], "superunit" => $user["superunit"], "superunit_id" => $user["superunit_id"], "domain_id" => $user["domain_id"], "domain" => $user["domain"]);
  130.                     array_push($new_profiles, $temp_array);
  131.                     array_push($vhurs, $vhur);
  132.                 } else {
  133.                     $temp_array = array("network_login" => $user["NickName"], "email" => $user["email"], "vhur" => $user["VHUR"], "position" => $user["position"], "position_id" => $user["position_id"], "unit" => $user["unit"], "unit_id" => $user["unit_id"], "superunit" => $user["superunit"], "superunit_id" => $user["superunit_id"], "domain_id" => $user["domain_id"], "domain" => $user["domain"]);
  134.                     array_push($no_new, $temp_array);
  135.                     $processed = TRUE;
  136.                 }//else no se pudo insertar el usuario
  137.             }
  138.  
  139.  
  140.             if (is_null($user["VHUR"]) || strlen($user["VHUR"]) === 0) {
  141.                 $vhur = 0;
  142.             } else {
  143.                 $vhur = $user["VHUR"];
  144.             }
  145.             array_push($vhurs, $vhur);
  146.         }//foreach para recorrer los usuarios
  147.         $new_disabled = disable_profiles($db, $vhurs, $netlogins);
  148.     }//else se pudo convertir el contenido del WS a array
  149. }//else se pudo leer el WS
  150.  
  151. $end = microtime(TRUE);
  152. $time = $end - $start;
  153. $hours = (int) ($time / 60 / 60);
  154. $minutes = (int) ($time / 60) - $hours * 60;
  155. $seconds = (int) $time - $hours * 60 * 60 - $minutes * 60;
  156. $no_new_count = count($no_new);
  157. $no_updates_count = count($no_updates);
  158. $time = "$hours horas - $minutes minutos - $seconds segundos.<br>";
  159. $resultados = "Se realizaron $inserts inserciones y $updates actualizaciones.<br> No se pudieron realizar $no_new_count nuevas inserciones y $no_updates_count actulizaciones <br>";
  160. $mail_body .= "La ejecucion tardo: $time $resultados $insert_table";
  161. $body = "<html><body>";
  162.  
  163. if ($inserts > 0) {
  164.     $insert_table = "<b>New profiles: $inserts</b><br><br>
  165.                         <table style='border: 1px solid #ccc;width: 100%;margin: 0;padding: 0;border-collapse: collapse;border-spacing: 0;'>
  166.                         <tr style='border: 1px solid #ddd;padding: 5px;'>
  167.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Network login</th>
  168.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Email</th>
  169.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >VHUR</th>
  170.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Position</th>
  171.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Position ID</th>   
  172.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Unit</th>
  173.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Unit ID</th>
  174.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Superunit</th>
  175.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Superunit ID</th>
  176.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Domain</th>
  177.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Domain ID</th>
  178.                         </tr>";
  179.     foreach ($new_profiles as $temp) {
  180.         $insert_table .= "<tr style='border: 1px solid #ddd;padding: 5px;'>";
  181.         $insert_table .= validate_data($temp['network_login']);
  182.         $insert_table .= validate_data($temp['email']);
  183.         $insert_table .= validate_data($temp['vhur']);
  184.         $insert_table .= validate_data($temp['position']);
  185.         $insert_table .= validate_data($temp['position_id']);
  186.         $insert_table .= validate_data($temp['unit']);
  187.         $insert_table .= validate_data($temp['unit_id']);
  188.         $insert_table .= validate_data($temp['superunit']);
  189.         $insert_table .= validate_data($temp['superunit_id']);
  190.         $insert_table .= validate_data($temp['domain']);
  191.         $insert_table .= validate_data($temp['domain_id']);
  192.         /* $insert_table .= "<tr>
  193.           <td>{$temp['network_login']}</td>
  194.           <td>{$temp['email']}</td>
  195.           <td>{$temp['position']}</td>
  196.           <td>{$temp['position_id']}</td>
  197.           <td>{$temp['unit']}</td>
  198.           <td>{$temp['unit_id']}</td>
  199.           <td>{$temp['superunit']}</td>
  200.           <td>{$temp['superunit_id']}</td>
  201.           </tr>"; */
  202.     }
  203.     $insert_table .= "</table><br><br>";
  204.     $body .= "$insert_table";
  205.     //$mail_body .= "$insert_table";
  206. }
  207.  
  208. $new_disabled_count = count($new_disabled);
  209. if ($new_disabled_count > 0) {
  210.  
  211.     $disabled_table = "<b>Disbale users: {$new_disabled_count}</b><br><br>
  212.                                 <table style='border: 1px solid #ccc;width: 100%;margin: 0;padding: 0;border-collapse: collapse;border-spacing: 0;'>
  213.                                     <tr style='border: 1px solid #ddd;padding: 5px;'>
  214.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Network login</th>
  215.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Email</th>
  216.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >VHUR</th>
  217.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Position</th>
  218.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Position Id</th>
  219.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Unit</th>
  220.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Unit Id</th>
  221.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Superunit</th>
  222.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Superunit Id</th>
  223.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Domain</th>
  224.                                         <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Domain ID</th>
  225.                                     </tr>";
  226.     foreach ($new_disabled as $temp) {
  227.         $disabled_table .= "<tr style='border: 1px solid #ddd;padding: 5px;'>";
  228.         $disabled_table .= validate_data($temp['network_login']);
  229.         $disabled_table .= validate_data($temp['email']);
  230.         $disabled_table .= validate_data($temp['vhur']);
  231.         $disabled_table .= validate_data($temp['position']);
  232.         $disabled_table .= validate_data($temp['position_id']);
  233.         $disabled_table .= validate_data($temp['unit']);
  234.         $disabled_table .= validate_data($temp['unit_id']);
  235.         $disabled_table .= validate_data($temp['superunit']);
  236.         $disabled_table .= validate_data($temp['superunit_id']);
  237.         $disabled_table .= validate_data($temp['domain']);
  238.         $disabled_table .= validate_data($temp['domain_id']);
  239.     }
  240.     $disabled_table .= "</table><br><br>";
  241.     $body .= "$disabled_table";
  242.     //$mail_body .= "$disabled_table";
  243. }
  244.  
  245. if ($no_new_count > 0) {
  246.     $no_insert_table = "<b>Profiles that cannot be inserted: $no_new_count</b><br><br>
  247.                         <table style='border: 1px solid #ccc;width: 100%;margin: 0;padding: 0;border-collapse: collapse;border-spacing: 0;'>
  248.                         <tr style='border: 1px solid #ddd;padding: 5px;'>
  249.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Network login</th>
  250.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Email</th>
  251.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >VHUR</th>
  252.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Position</th>
  253.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Position Id</th>
  254.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Unit</th>
  255.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Unit Id</th>
  256.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Superunit</th>
  257.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Superunit Id</th>
  258.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Domain</th>
  259.                             <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Domain ID</th>
  260.                         </tr>";
  261.     foreach ($no_new as $temp) {
  262.         $no_insert_table .= "<tr style='border: 1px solid #ddd;padding: 5px;'>";
  263.         $no_insert_table .= validate_data($temp['network_login']);
  264.         $no_insert_table .= validate_data($temp['email']);
  265.         $no_insert_table .= validate_data($temp['vhur']);
  266.         $no_insert_table .= validate_data($temp['position']);
  267.         $no_insert_table .= validate_data($temp['position_id']);
  268.         $no_insert_table .= validate_data($temp['unit']);
  269.         $no_insert_table .= validate_data($temp['unit_id']);
  270.         $no_insert_table .= validate_data($temp['superunit']);
  271.         $no_insert_table .= validate_data($temp['superunit_id']);
  272.         $no_insert_table .= validate_data($temp['domain']);
  273.         $no_insert_table .= validate_data($temp['domain_id']);
  274.     }
  275.     $no_insert_table .= "</table><br><br>";
  276.     $body .= "$no_insert_table";
  277.     //$mail_body .= "$no_insert_table";
  278. }
  279.  
  280. if ($no_updates_count > 0) {
  281.     $no_update_table = "<br><br><b>Profiles that cannot be updated: $no_updates_count</b><br><br>
  282.                             <table style='border: 1px solid #ccc;width: 100%;margin: 0;padding: 0;border-collapse: collapse;border-spacing: 0;'>
  283.                             <tr style='border: 1px solid #ddd;padding: 5px;'>
  284.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Network login</th>
  285.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Email</th>
  286.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >VHUR</th>
  287.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Position</th>
  288.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Position Id</th>    
  289.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Unit</th>
  290.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Unit Id</th>
  291.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Superunit</th>
  292.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;'>Superunit Id</th>
  293.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Domain</th>
  294.                                 <th style = 'padding: 10px;text-align: left;font-size: 14px;letter-spacing: 1px;background-color:#49166D;color:white;' >Domain ID</th>
  295.                             </tr>";
  296.     foreach ($no_updates as $temp) {
  297.         $no_update_table .= "<tr style='border: 1px solid #ddd;padding: 5px;'>";
  298.         $no_update_table .= validate_data($temp['network_login']);
  299.         $no_update_table .= validate_data($temp['email']);
  300.         $no_update_table .= validate_data($temp['vhur']);
  301.         $no_update_table .= validate_data($temp['position']);
  302.         $no_update_table .= validate_data($temp['position_id']);
  303.         $no_update_table .= validate_data($temp['unit']);
  304.         $no_update_table .= validate_data($temp['unit_id']);
  305.         $no_update_table .= validate_data($temp['superunit']);
  306.         $no_update_table .= validate_data($temp['superunit_id']);
  307.         $no_update_table .= validate_data($temp['domain']);
  308.         $no_update_table .= validate_data($temp['domain_id']);
  309.         /* $no_update_table .= "<tr style='border: 1px solid #ddd;padding: 5px;'>
  310.           <td style = 'padding:10px;text-align:left;' >{$temp['network_login']}</td>
  311.           <td style = 'padding:10px;text-align:left;' >{$temp['email']}</td>
  312.           <td style = 'padding:10px;text-align:left;' >{$temp['position']}</td>
  313.           <td style = 'padding:10px;text-align:left;' >{$temp['position_id']}</td>
  314.           <td style = 'padding:10px;text-align:left;' >{$temp['unit']}</td>
  315.           <td style = 'padding:10px;text-align:left;' >{$temp['unit_id']}</td>
  316.           <td style = 'padding:10px;text-align:left;' >{$temp['superunit']}</td>
  317.           <td style = 'padding:10px;text-align:left;' >{$temp['superunit_id']}</td>
  318.           </tr>"; */
  319.     }
  320.     $no_update_table .= "</table><br><br>";
  321.     $body .= "$no_update_table";
  322.     $mail_body .= "$no_update_table";
  323. }
  324.  
  325. $body .= "</body></html>";
  326. echo "<br>" . $mail_body;
  327. echo "<br>" . $body;
  328.  
  329. send_report_synch($body, "Tlife SV - Profiles synchronization", $db);
  330.  
  331. function validate_user($user) {
  332.     if (!is_null($user["VHUR"]) && strlen($user["VHUR"]) > 0 && !empty($user["NickName"]) && !is_null($user["first"]) && strlen($user["first"]) > 0 && !is_null($user["last"]) && strlen($user["last"]) > 0 && !is_null($user["email"]) && strlen($user["email"]) > 0 && !is_null($user["unit_id"]) && strlen($user["unit_id"]) > 0 && !is_null($user["superunit_id"]) && strlen($user["superunit_id"]) > 0 && !is_null($user["position_id"]) && strlen($user["position_id"]) > 0 && !is_null($user["domain_id"]) && strlen($user["domain_id"]) > 0 && !is_null($user["domain"]) && strlen($user["domain"]) > 0) {
  333.         return TRUE;
  334.     } else {
  335.         return FALSE;
  336.     }
  337. }
  338.  
  339. function prepared_update($user, $db, $type) {
  340.     //echo "entra a la funcion <br>";
  341.     $data = prepared_data($user, $db);
  342.     //echo "Va a entrar al la funcion <br>";
  343.     if ($type === 1) {
  344.         $query_update = "UPDATE Profiles SET `Email` = '{$data['email']}',`Password` = '{$data['password']}',`Salt` = '{$data['salt']}',`Status` = '{$data['status']}',
  345.                                                  `Role` = {$data['role']},`Sex` = '{$data['sex']}',`Country` = '{$data['country']}',`PrivacyDefaultGroup` = {$data['privacy_default_group']},
  346.                                                  `FirstName` = '{$data['first_name']}', `LastName` = '{$data['last_name']}', `Position` = '{$data['position']}', `Unit`= '{$data['unit']}',
  347.                                                  `SuperUnit` = '{$data['superunit']}', `Position_id` = {$data['position_id']},`Unit_id` = {$data['unit_id']}, `SuperUnit_id` = {$data['superunit_id']},
  348.                                                  `Supervisor` = '{$data['supervisor']}', `Active` = {$data['active']},`Full_name`= '{$data['full_name']}',`PersonalEmail` = '{$data['personal_email']}',
  349.                                                  `FullName` = '{$data['full_name']}', `network_login` = '{$data['network_login']}',`Is_sup` = {$data['is_sup']},`adl_domain` = '{$data['adl_domain']}',
  350.                                                  `adl_domain_id` = {$data['adl_domain_id']},`hire_date` = '{$data['hire_date']}', `DateLastEdit` = NOW(),`NickName` = '{$data['Nickname']}' WHERE ID_VHUR = {$data['vhur']} AND `NickName`NOT LIKE '%disabled%' AND CHAR_LENGTH(`NickName`) > 3 LIMIT 1;";
  351.     } elseif ($type === 2) {
  352.         $query_update = "UPDATE Profiles SET `Email` = '{$data['email']}',`Password` = '{$data['password']}',`Salt` = '{$data['salt']}',`Status` = '{$data['status']}',
  353.                                                  `Role` = {$data['role']},`Sex` = '{$data['sex']}',`Country` = '{$data['country']}',`PrivacyDefaultGroup` = {$data['privacy_default_group']},
  354.                                                  `FirstName` = '{$data['first_name']}', `LastName` = '{$data['last_name']}', `Position` = '{$data['position']}', `Unit`= '{$data['unit']}',
  355.                                                  `SuperUnit` = '{$data['superunit']}', `Position_id` = {$data['position_id']},`Unit_id` = {$data['unit_id']}, `SuperUnit_id` = {$data['superunit_id']},
  356.                                                  `Supervisor` = '{$data['supervisor']}', `Active` = {$data['active']},`Full_name`= '{$data['full_name']}',`PersonalEmail` = '{$data['personal_email']}',
  357.                                                  `FullName` = '{$data['full_name']}', ID_VHUR = {$data['vhur']},`Is_sup` = {$data['is_sup']},`adl_domain` = '{$data['adl_domain']}',
  358.                                                  `adl_domain_id` = {$data['adl_domain_id']},`hire_date` = '{$data['hire_date']}',`DateLastEdit` = NOW(),`NickName` = '{$data['Nickname']}' WHERE `network_login` = '{$data['network_login']}' AND `NickName`NOT LIKE '%disabled%' AND CHAR_LENGTH(`NickName`) > 3 LIMIT 1;";
  359.     }
  360.  
  361.     return $query_update;
  362. }
  363.  
  364. function prepared_insert($user, $db) {
  365.     $data = prepared_data($user, $db);
  366.     $query_insert = "INSERT INTO `Profiles` (`NickName`, `Email`,`Password`, `Salt`, `Status`, `Role`, `Sex`, `Country`, `DateReg`, `EmailNotify`, `LangID`, `PrivacyDefaultGroup`, `FirstName`, `LastName`, `Position`, `Unit`, `SuperUnit`, `ID_VHUR`, `Position_id`, `Unit_id`, `SuperUnit_id`, `Supervisor`, `Active`, `Full_name`, `PersonalEmail`, `FullName`, `network_login`, `Is_sup`, `hire_date`, `adl_domain`, `adl_domain_id`)
  367.                                 VALUES ('{$data['Nickname']}','{$data['email']}','{$data['password']}','{$data['salt']}','{$data['status']}',{$data['role']},'{$data['sex']}','{$data['country']}',NOW(),'{$data['email_notify']}',{$data['lang_id']},{$data['privacy_default_group']},'{$data['first_name']}','{$data['last_name']}','{$data['position']}','{$data['unit']}','{$data['superunit']}',{$data['vhur']},{$data['position_id']},{$data['unit_id']},{$data['superunit_id']},'{$data['supervisor']}',{$data['active']},'{$data['full_name']}','{$data['personal_email']}','{$data['full_name']}','{$data['network_login']}',{$data['is_sup']},'{$data['hire_date']}','{$data['adl_domain']}',{$data['adl_domain_id']})";
  368.     return $query_insert;
  369. }
  370.  
  371. function prepared_data($user, $db) {
  372.     $user_data = array();
  373.     $email = strtolower(addslashes($user["email"]));
  374.     $user_data["email"] = $email;
  375.     $first_new = ucfirst(strtolower($user["first"]));
  376.     $last_new = ucfirst(strtolower($user["last"]));
  377.     $first_new = str_replace('"', '', $first_new);
  378.     $last_new = str_replace('"', '', $last_new);
  379.     $first_new = str_replace("'", "", $first_new);
  380.     $user_data["first_name"] = $first_new;
  381.     $last_new = str_replace("'", "", $last_new);
  382.     $user_data["last_name"] = $last_new;
  383.     $password = "eb74a10e0eff3b74f1e10408db817f59c0b8e744";
  384.     $user_data["password"] = $password;
  385.     $salt = "AWwHT9Pt";
  386.     $user_data["salt"] = $salt;
  387.     $status = "Active";
  388.     $user_data["status"] = $status;
  389.     $role = 1;
  390.     $user_data["role"] = $role;
  391.     $sex = ucfirst(strtolower($user["gender"]));
  392.     $user_data["sex"] = $sex;
  393.     $country = $db->getParam('default_country');
  394.     $user_data["country"] = $country;
  395.     $email_notify = 1;
  396.     $user_data["email_notify"] = $email_notify;
  397.     $lang_id = 1;
  398.     $user_data["lang_id"] = $lang_id;
  399.     $privacy_default_group = 3;
  400.     $user_data["privacy_default_group"] = $privacy_default_group;
  401.     $first_name = addslashes(ucfirst(strtolower($first_new)));
  402.     $user_data["first_name"] = $first_name;
  403.     $last_name = addslashes(ucfirst(strtolower($last_new)));
  404.     $user_data["last_name"] = $last_name;
  405.     $unit = addslashes($user["unit"]);
  406.     $user_data["unit"] = $unit;
  407.     $superunit = addslashes($user["superunit"]);
  408.     $user_data["superunit"] = $superunit;
  409.     $vhur = $user["VHUR"];
  410.     $user_data["vhur"] = $vhur;
  411.     $position_id = $user["position_id"];
  412.     $user_data["position_id"] = $position_id;
  413.     $unit_id = $user["unit_id"];
  414.     $user_data["unit_id"] = $unit_id;
  415.     $superunit_id = $user["superunit_id"];
  416.     $user_data["superunit_id"] = $superunit_id;
  417.     $active = 1;
  418.     $user_data["active"] = $active;
  419.     $full_name = $first_name . " " . $last_name;
  420.     $user_data["full_name"] = $full_name;
  421.     $personal_email = strtolower(addslashes($user["personal_email"]));
  422.     $user_data["personal_email"] = $personal_email;
  423.     $network_login = addslashes($user["NickName"]);
  424.     $user_data["network_login"] = $network_login;
  425.     $is_sup = $user["Is_sup"];
  426.     $user_data["is_sup"] = $is_sup;
  427.  
  428.     if (strtolower($user["position"]) !== "null") {
  429.         $position = $user["position"];
  430.     } else {
  431.         $position = "";
  432.     }
  433.     $user_data["position"] = $position;
  434.  
  435.     if (strtolower($user["hire_date"]) !== "null") {
  436.         $hire_date = $user["hire_date"];
  437.     } else {
  438.         $hire_date = "";
  439.     }
  440.     $user_data["hire_date"] = $hire_date;
  441.  
  442.     if (is_numeric($user["position_id"])) {
  443.         $position_id = $user["position_id"];
  444.     } else {
  445.         $position_id = 0;
  446.     }
  447.     $user_data["position_id"] = $position_id;
  448.  
  449.     if (!is_null($user["supervisor"]) && strlen($user["supervisor"]) > 0) {
  450.         $supervisor = $user["supervisor"];
  451.     } else {
  452.         $supervisor = 0;
  453.     }
  454.  
  455.     $user_data["supervisor"] = $supervisor;
  456.  
  457.     $adl_domain = strtoupper($user["domain"]);
  458.     switch ($adl_domain) {
  459.         case 'SV':
  460.             $adl_domain_id = 3; //$user["ad_domain_id"];
  461.             break;
  462.         case 'TRTEL':
  463.             $adl_domain_id = 2; //$user["ad_domain_id"];
  464.             break;
  465.         case 'TI':
  466.             $adl_domain_id = 1; //$user["ad_domain_id"];
  467.             break;
  468.         default:
  469.             break;
  470.     }
  471.     $user_data["adl_domain"] = $adl_domain;
  472.     $user_data["adl_domain_id"] = $adl_domain_id;
  473.  
  474.     $user_data["Nickname"] = create_nickname2($first_name, $last_name, $email, $vhur, $db);
  475.  
  476.     return $user_data;
  477. }
  478.  
  479. function create_nickname2($first, $last, $email, $vhur, $db) {
  480.     if (empty($first) || empty($last)) {
  481.         $parts = explode("@", $email);
  482.         $temp = explode(".", $parts[0]);
  483.         preg_replace('/[0-9]+/', '', $words);
  484.         $new_nickname = ucfirst($temp[0]) . ucfirst($temp[1]);
  485.         $nick_name = $new_nickname . "_" . $vhur;
  486.     } else {
  487.         $first = trim($first);
  488.         $last = trim($last);
  489.         $first_parts = explode(" ", $first);
  490.         $last_parts = explode(" ", $last);
  491.         $first_new = $last_new = "";
  492.  
  493.         foreach ($first_parts as $key) {
  494.             $key = ucfirst($key);
  495.             $first_new .= $key;
  496.         }
  497.  
  498.         foreach ($last_parts as $key) {
  499.             $key = ucfirst($key);
  500.             $last_new .= $key;
  501.         }
  502.  
  503.         $nick_name = $first_new . $last_new . "_" . $vhur;
  504.     }
  505.  
  506.     //echo "$nick_name ---- $vhur<br>";
  507.     return $nick_name;
  508. }
  509.  
  510. function create_nickname($first, $last, $email, $db) {
  511.     $first_temp = str_replace(" ", "-", $first);
  512.  
  513.     $first_temp2 = explode("-", $first_temp);
  514.     if (is_array($first_temp2)) {
  515.         for ($i = 0; $i < count($first_temp2); $i++) {
  516.             $first_temp2[$i] = ucfirst(strtolower($first_temp2[$i]));
  517.         }
  518.         $first_new = "";
  519.         $first_new = implode("", $first_temp2);
  520.     } else {
  521.         $first_new = ucfirst(strtolower($first_temp2));
  522.     }
  523.  
  524.     $last_temp = str_replace(" ", "-", $last);
  525.     $last_temp2 = explode("-", $last_temp);
  526.     if (is_array($first_temp2)) {
  527.         for ($i = 0; $i < count($last_temp2); $i++) {
  528.             $last_temp2[$i] = ucfirst(strtolower($last_temp2[$i]));
  529.         }
  530.         $last_new = "";
  531.         $last_new = implode("", $last_temp2);
  532.     } else {
  533.         $first_new = ucfirst(strtolower($first_temp2));
  534.     }
  535.  
  536.     //echo "$first_new $last_new<br>";
  537.  
  538.     $nick_name = $first_new . $last_new;
  539.  
  540.     $exit = TRUE;
  541.     $new_nickname = "";
  542.     $query = "SELECT ID_VHUR FROM Profiles WHERE NickName LIKE '$nick_name'";
  543.     $exits = $db->getAll($query);
  544.     if (count($exits) === 1) {
  545.         $temp_nickname = explode("@", $email);
  546.         $temp_nickname2 = explode(".", $temp_nickname[0]);
  547.         //echo count($temp_nickname2)."<br>";
  548.         for ($i = 0; $i < count($temp_nickname2); $i++) {
  549.             $temp_nickname2[$i] = ucfirst(strtolower($temp_nickname2[$i]));
  550.         }
  551.         $new_nickname = "";
  552.         $new_nickname = implode("", $temp_nickname2);
  553.  
  554.         /* foreach ($temp_nickname2 as $temp_n) {
  555.           echo "Nickname: $new_nickname<br>";
  556.           $new_nickname .= $new_nickname.ucfirst(strtolower($temp_n));
  557.           } */
  558.         $prefix = 1;
  559.         while ($exit) {
  560.             $query = "SELECT ID_VHUR FROM Profiles WHERE NickName LIKE '$new_nickname'";
  561.             //echo "$query <br>";
  562.             $exits = $db->getAll($query);
  563.             if (count($exits) >= 1) {
  564.                 $new_nickname = $new_nickname . $prefix;
  565.                 $prefix++;
  566.             } else {
  567.                 $exit = FALSE;
  568.             }
  569.         }
  570.     } else {
  571.         $new_nickname = $nick_name;
  572.     }
  573.     return $new_nickname;
  574. }
  575.  
  576. function privacy_group($last_user, $user, $db) {
  577.     $privacy_groups = array();
  578.     $query = "SELECT `id` FROM `sys_privacy_groups` WHERE `unit_id_relac` = '{$last_user['SuperUnit_id']}'; ";
  579.  
  580.     $superunit_pg = (int) $db->getOne($query);
  581.  
  582.     if (!$superunit_pg) {
  583.         $last_user['SuperUnit'] = addslashes($last_user['SuperUnit']);
  584.         $query = "INSERT INTO `sys_privacy_groups` SET `title` = '{$last_user['SuperUnit']}', `owner_id` = 1, `unit_id_relac` = '{$last_user['SuperUnit_id']}';";
  585.  
  586.         $db->query($query);
  587.         $superunit_pg = $db->lastId();
  588.     }
  589.  
  590.     $privacy_groups[] = $superunit_pg;
  591.     if ($last_user["SuperUnit_id"] !== $last_user["Unit_id"]) {
  592.         $superunit_pg = (int) $db->getOne("SELECT `id` FROM `sys_privacy_groups` WHERE `unit_id_relac` = '{$user['unit_id']}'; ");
  593.         if (!$superunit_pg) {
  594.             $last_user['SuperUnit'] = addslashes($last_user['Unit']);
  595.             $db->query("INSERT INTO `sys_privacy_groups` SET `title` = '{$last_user['Unit']}', `owner_id` = 1, `unit_id_relac` = '{$last_user['Unit_id']}';     ");
  596.             $superunit_pg = $db->lastId();
  597.         }
  598.         $privacy_groups[] = $superunit_pg;
  599.     }
  600.  
  601.     $query_delete = "DELETE FROM sys_privacy_members WHERE member_id = '{$last_user['ID']}'";
  602.     if ($db->query($query_delete)) {
  603.         //
  604.     }
  605.  
  606.     foreach ($privacy_groups as $privacy_group) {
  607.         $db->query("INSERT IGNORE INTO `sys_privacy_members` SET `group_id` = '$privacy_group', `member_id` = '{$last_user['ID']}';");
  608.     }
  609. }
  610.  
  611. function validate_data($var) {
  612.     $temp = set_null($var);
  613.     if ($temp === "N/A") {
  614.         $row = "<td style = 'padding:10px;text-align:left;color:red;font-weight: bold;'>$temp</td>";
  615.     } else {
  616.         $row = "<td style = 'padding:10px;text-align:left;'>$temp</td>";
  617.     }
  618.     return $row;
  619. }
  620.  
  621. function set_null($var) {
  622.     if (is_null($var) || strlen($var) === 0) {
  623.         return "N/A";
  624.     } else {
  625.         return $var;
  626.     }
  627. }
  628.  
  629. function disable_profiles($db, $vhurs, $netlogins) {
  630.     $sql_vhurs = "(";
  631.     $sql_vhurs .= implode(",", $vhurs);
  632.     $sql_vhurs .= ")";
  633.  
  634.     $sql_netlogins = "(";
  635.     $sql_netlogins .= implode(",", $netlogins);
  636.     $sql_netlogins .= ")";
  637.  
  638.     $query = "SELECT pro.network_login AS 'network_login',pro.Email AS 'email',pro.ID_VHUR AS 'vhur',pro.Position AS 'position',pro.Position_id AS 'position_id',pro.Unit AS 'unit',pro.Unit_id AS 'unit_id',pro.SuperUnit AS 'superunit',pro.SuperUnit_id AS 'superunit_id',pro.adl_domain AS 'domain',pro.adl_domain_id AS 'domain_id'
  639.                   FROM Profiles AS pro
  640.                   WHERE pro.ID_VHUR NOT IN $sql_vhurs AND
  641.                         pro.Role <> 3 AND pro.Active = 1
  642.                         ORDER BY pro.ID_VHUR";
  643.  
  644.     $oldVhrus = array();
  645.     $result = $db->getAll($query);
  646.  
  647.     if (count($result) > 0) {
  648.         foreach ($result as $vhur) {
  649.             array_push($oldVhrus, $vhur["vhur"]);
  650.         }
  651.  
  652.         $sql_vhurs = "(";
  653.         $sql_vhurs .= implode(",", $oldVhrus);
  654.         $sql_vhurs .= ")";
  655.  
  656.         $query_disable = "UPDATE Profiles SET Active = 2, Status = 'Suspended' WHERE ID_VHUR IN $sql_vhurs";
  657.         echo "$query_disable <br><br><br><br>";
  658.         $result2 = $db->query($query_disable);
  659.     }
  660.     return $result; //echo $query;
  661. }
  662.  
  663. function send_report_synch($body, $email_subject, $db) {
  664.     /* Get email params */
  665.     $host = $db->getParam("email_setting_host");
  666.     $from = $db->getParam("bx_mailread_mail");
  667.     $email_password = $db->getParam("email_setting_password");
  668.     $email_port = $db->getParam("email_setting_port");
  669.     $recipients = $db->getParam("synch_profile_recipients");
  670.     /* Send mail */
  671.     $mail = new PHPMailer();  // the true param means it will throw exceptions on errors, which we need to catch
  672.     $mail->CharSet = 'UTF-8';
  673.     $mail->Host = $host; //"imap.gmail.com"; // SMTP server
  674.     //$mail->IsSMTP();
  675.     $mail->Username = $from; //"notifications@telusinternational.com"; // SMTP server
  676.     $mail->Password = $email_password; //"Tr@nsact3l2013"; // SMTP server
  677.     $mail->From = $from; //"notifications@telusinternational.com"; // SMTP server
  678.     $mail->Port = $email_port; //25;
  679.     //$mail->addAddress("carlos.recinos@telusinternational.com");
  680.     $emails = explode(";", $recipients);
  681.     foreach ($emails as $temp) {
  682.         $mail->addAddress($temp);
  683.     }
  684.     $mail->SetFrom($from, 'Tlife SV - Profiles synchronization');
  685.     $mail->Subject = $email_subject;
  686.     $mail->MsgHTML($body);
  687.  
  688.     if (!$mail->Send()) {
  689.         //echo "Message was not sent";
  690.         echo "<div>$email_subject\n</div> \n
  691.              \n host: $host from: $from Password: $email_password port: $email_port recipients: $recipients\n" . $mail->ErrorInfo;
  692.         //echo "Mailer Error: " . $mail->ErrorInfo;
  693.         //exit;
  694.     } else {
  695.         echo "<div>$email_subject\n
  696.              </div>\n host: $host from: $from Password: $email_password port: $email_port recipients: $recipients";
  697.     }
  698. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement