/**
  * Override standard validation and sending notification to send the good PDF reports with 
  * appropriate rigths.
  * 
  * @see NotificationTarget::validateSendTo()
  * 
  * @param string $event notification event
  * @param Array $infos Current user informations
  * @param Boolean $notify_me Notify the current user of his own actions ?
  * 
  * @return boolean false to prevent standard mail sending
  */
 function validateSendTo($event, array $infos, $notify_me = false)
 {
     global $DB;
     if (isset($infos['users_id'])) {
         // save session variables
         $saved_session = $_SESSION;
         // Get current user full informations
         $user = new User();
         $user->getFromDB($infos['users_id']);
         // inialize session for user to build the proper PDF report
         unset($_SESSION['glpiprofiles'], $_SESSION['glpiactiveentities'], $_SESSION['glpiactiveprofile']);
         Session::initEntityProfiles($infos['users_id']);
         // Use default profile if exist
         if (isset($_SESSION['glpiprofiles'][$user->fields['profiles_id']])) {
             Session::changeProfile($user->fields['profiles_id']);
             // Else use first
         } else {
             Session::changeProfile(key($_SESSION['glpiprofiles']));
         }
         $user_name = $infos['username'] . '_';
         $file_name = $this->_buildPDF($user_name);
         $path = GLPI_PLUGIN_DOC_DIR . '/mreporting/notifications/' . $file_name;
         $mmail = new NotificationMail();
         $mmail->AddCustomHeader("Auto-Submitted: auto-generated");
         // For exchange
         $mmail->AddCustomHeader("X-Auto-Response-Suppress: OOF, DR, NDR, RN, NRN");
         // Get current entity administrator info to send the email from him
         $admin = $this->getSender();
         $mmail->From = $admin['email'];
         $mmail->FromName = $admin['name'];
         // Attach pdf to mail
         $mmail->AddAttachment($path, $file_name);
         // Get content infos
         $query = 'SELECT * 
               FROM glpi_notificationtemplatetranslations
               WHERE notificationtemplates_id = (
                  SELECT id 
                  FROM glpi_notificationtemplates 
                  WHERE itemtype = "PluginMreportingNotification"
               )
               AND (language LIKE "' . $_SESSION['glpilanguage'] . '" OR language LIKE "")
               ORDER BY language DESC
               LIMIT 0, 1';
         $result = $DB->query($query);
         $translation = $result->fetch_array();
         $mmail->isHTML(true);
         $mmail->Subject = $translation['subject'];
         $mmail->Body = $translation['content_html'];
         $mmail->AltBody = $translation['content_text'];
         $mmail->AddAddress($infos['email']);
         if ($mmail->Send()) {
         }
         //restore session
         unset($_SESSION);
         $_SESSION = $saved_session;
     }
     return false;
 }
Exemplo n.º 2
0
/**
 * @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;
}
 function sendMailRefusedResponse($to = '', $subject = '')
 {
     global $CFG_GLPI, $LANG;
     $mmail = new NotificationMail();
     $mmail->AddCustomHeader("Auto-Submitted: auto-replied");
     $mmail->SetFrom($CFG_GLPI["admin_email"], $CFG_GLPI["admin_email_name"]);
     $mmail->AddAddress($to);
     $mmail->Subject = $LANG['mailgate'][16] . ' ' . $subject;
     $mmail->Body = $LANG['mailgate'][9] . "\n-- \n" . $CFG_GLPI["mailing_signature"];
     $mmail->Send();
 }
 static function testNotification()
 {
     global $CFG_GLPI, $LANG;
     $mmail = new NotificationMail();
     $mmail->AddCustomHeader("Auto-Submitted: auto-generated");
     $mmail->SetFrom($CFG_GLPI["admin_email"], $CFG_GLPI["admin_email_name"]);
     $mmail->AddAddress($CFG_GLPI["admin_email"], $CFG_GLPI["admin_email_name"]);
     $mmail->Subject = "[GLPI] " . $LANG['mailing'][32];
     $mmail->Body = $LANG['mailing'][31] . "\n-- \n" . $CFG_GLPI["mailing_signature"];
     if (!$mmail->Send()) {
         addMessageAfterRedirect($LANG['setup'][206], false, ERROR);
     } else {
         addMessageAfterRedirect($LANG['setup'][205]);
     }
 }
Exemplo n.º 5
0
 /**
  * @param $to        (default '')
  * @param $subject   (default '')
  **/
 function sendMailRefusedResponse($to = '', $subject = '')
 {
     global $CFG_GLPI;
     $mmail = new NotificationMail();
     $mmail->AddCustomHeader("Auto-Submitted: auto-replied");
     $mmail->SetFrom($CFG_GLPI["admin_email"], $CFG_GLPI["admin_email_name"]);
     $mmail->AddAddress($to);
     // Normalized header, no translation
     $mmail->Subject = 'Re: ' . $subject;
     $mmail->Body = __("Your email could not be processed.\nIf the problem persists, contact the administrator") . "\n-- \n" . $CFG_GLPI["mailing_signature"];
     $mmail->Send();
 }