/** * @param $pid * @param $data * @param $server * @param $prof * @param $verb * @param $mail **/ function syncEntity($pid, $data, $server, $prof, $verb, $mail) { global $DB, $LANG, $CFG_GLPI; // Re-establish DB connexion - mandatory in each forked process if (!DBConnection::switchToMaster()) { echo " {$pid}: lost DB connection\n"; return 0; } // Server from entity (if not given from option) if ($data['authldaps_id'] > 0) { $server = $data['authldaps_id']; } $entity = new Entity(); if ($entity->getFromDB($id = $data['id'])) { $tps = microtime(true); if ($verb) { echo " {$pid}: Synchonizing entity '" . $entity->getField('completename') . "' ({$id}, mail={$mail})\n"; } $sql = "SELECT DISTINCT glpi_users.*\n FROM glpi_users\n INNER JOIN glpi_profiles_users\n ON (glpi_profiles_users.users_id = glpi_users.id\n AND glpi_profiles_users.entities_id = {$id}"; if ($prof > 0) { $sql .= " AND glpi_profiles_users.profiles_id = {$prof}"; } $sql .= ")\n WHERE glpi_users.authtype = " . Auth::LDAP; if ($server > 0) { $sql .= " AND glpi_users.auths_id = {$server}"; } $users = array(); $results = array(AuthLDAP::USER_IMPORTED => 0, AuthLDAP::USER_SYNCHRONIZED => 0, AuthLDAP::USER_DELETED_LDAP => 0); $req = $DB->request($sql); $i = 0; $nb = $req->numrows(); foreach ($req as $row) { $i++; $result = AuthLdap::ldapImportUserByServerId(array('method' => AuthLDAP::IDENTIFIER_LOGIN, 'value' => $row['name']), AuthLDAP::ACTION_SYNCHRONIZE, $row['auths_id']); if ($result) { $results[$result['action']] += 1; $users[$row['id']] = $row['name']; if ($result['action'] == AuthLDAP::USER_SYNCHRONIZED) { if ($verb) { echo " {$pid}: User '" . $row['name'] . "' synchronized ({$i}/{$nb})\n"; } } else { if ($verb) { echo " {$pid}: User '" . $row['name'] . "' deleted\n"; } } } else { if ($verb) { echo " {$pid}: Problem with LDAP for user '" . $row['name'] . "'\n"; } } } $tps = microtime(true) - $tps; printf(" %d: Entity '%s' - Synchronized: %d, Deleted from LDAP: %d, Time: %.2f\"\n", $pid, $entity->getField('completename'), $results[AuthLDAP::USER_SYNCHRONIZED], $results[AuthLDAP::USER_DELETED_LDAP], $tps); if ($mail) { $report = ''; $user = new User(); foreach ($users as $id => $name) { if ($user->getFromDB($id)) { $logs = Log::getHistoryData($user, 0, $_SESSION['glpilist_limit'], "`date_mod`='" . $_SESSION['glpi_currenttime'] . "'"); if (count($logs)) { $report .= "\n{$name} (" . $user->getName() . ")\n"; foreach ($logs as $log) { $report .= "\t"; if ($log['field']) { $report .= $log['field'] . ": "; } $report .= Html::clean($log['change']) . "\n"; } } } else { $report .= "\n" . $name . "\n\t deleted\n"; } } if ($report) { $report = "Synchronization of already imported users\n " . "Entité: " . $entity->getField('completename') . "\n " . "Date: " . Html::convDateTime($_SESSION['glpi_currenttime']) . "\n " . $report; $entdata = new Entity(); $mmail = new NotificationMail(); $mmail->AddCustomHeader("Auto-Submitted: auto-generated"); $mmail->From = $CFG_GLPI["admin_email"]; $mmail->FromName = "GLPI"; $mmail->Subject = "[GLPI] LDAP directory link"; $mmail->Body = $report . "\n--\n" . $CFG_GLPI["mailing_signature"]; if ($mail & 1 && $entdata->getFromDB($entity->getField('id')) && $entdata->fields['admin_email']) { $mmail->AddAddress($entdata->fields['admin_email']); } else { if ($mail & 1 && $verb) { echo " {$pid}: No address found for email entity\n"; } $mail = $mail & 2; } if ($mail & 2 && $CFG_GLPI['admin_email']) { $mmail->AddAddress($CFG_GLPI['admin_email']); } else { if ($mail & 2 && $verb) { echo " {$pid}: No address found for email admin\n"; } $mail = $mail & 1; } if ($mail) { if ($mmail->Send() && $verb) { echo " {$pid}: Report sent by email\n"; } } else { echo " {$pid}: Cannot send report (" . $entity->getField('completename') . ") " . "invalid address\n"; } } } return $results[AuthLDAP::USER_DELETED_LDAP] + $results[AuthLDAP::USER_SYNCHRONIZED]; } return 0; }