Example #1
0
 /**
  * get timezone for a given user id
  * @param  int     $userId
  * @return varchar
  */
 public static function getTimezone($userId = false)
 {
     $rez = 'UTC';
     $pref = @$_SESSION['user'];
     if ($userId !== false) {
         $pref = User::getPreferences($userId);
     }
     if (!empty($pref['cfg']['timezone']) && System::isValidTimezone($pref['cfg']['timezone'])) {
         $rez = $pref['cfg']['timezone'];
     }
     return $rez;
 }
Example #2
0
 /**
  * send recovery password email for given user id
  * so that the user can set new password and enter the system
  * @param  int     $userId
  * @return boolean
  */
 public static function sendResetPasswordMail($userId, $template = 'recover')
 {
     if (!is_numeric($userId) || User::isLoged() && !Security::canEditUser($userId)) {
         return false;
     }
     $mail = '';
     $subject = '';
     switch ($template) {
         case 'invite':
             $mail = System::getEmailTemplate('email_invite');
             $subject = L\get('MailInviteSubject');
             break;
         case 'recover':
             $mail = System::getEmailTemplate('password_recovery_email');
             $subject = L\get('MailRecoverSubject');
             break;
         default:
             return false;
     }
     if (empty($mail)) {
         return false;
     }
     $userData = User::getPreferences($userId);
     $userEmail = User::getEmail($userData);
     if (empty($userEmail)) {
         return false;
     }
     /* generating invite hash and sending mail */
     $hash = User::generateRecoveryHash($userId, $userId . $userEmail . date(DATE_ISO8601));
     $href = Util\getCoreHost() . 'recover/reset-password/?h=' . $hash;
     /* replacing placeholders in template and subject */
     $replacements = array('{projectTitle}' => Config::getProjectName(), '{fullName}' => User::getDisplayName($userData), '{username}' => User::getUsername($userData), '{userEmail}' => $userEmail, '{creatorFullName}' => User::getDisplayName(), '{creatorUsername}' => User::getUsername(), '{creatorEmail}' => User::getEmail(), '{href}' => $href, '{link}' => '<a href="' . $href . '" >' . $href . '</a>');
     $search = array_keys($replacements);
     $replace = array_values($replacements);
     $mail = str_replace($search, $replace, $mail);
     $subject = str_replace($search, $replace, $subject);
     return @System::sendMail($userEmail, $subject, $mail);
 }
function sendUserMails($u)
{
    $uid = $u['id'];
    if (empty($u['email'])) {
        return;
    }
    $sendType = User::canSendNotifications($uid);
    if ($sendType == false) {
        return;
    }
    $coreName = Config::get('core_name');
    // $coreUrl = Config::get('core_url');
    $languages = Config::get('languages');
    $lang = $languages[$u['language_id'] - 1];
    if (filter_var($u['email'], FILTER_VALIDATE_EMAIL)) {
        //group mails into digest and separate ones (where user is mentioned)
        $mails = array('digest' => array(), 'separate' => array());
        foreach ($u['mails'] as $notificationId => $notification) {
            //[$core #$nodeId] $action_type $template_name: $object_title
            $templateId = Objects::getTemplateId($notification['object_id']);
            $templateName = Objects::getName($templateId);
            $subject = '[' . $coreName . ' #' . $notification['object_id'] . '] ' . Notifications::getActionDeclination($notification['action_type'], $lang) . ' ' . $templateName . ' "' . htmlspecialchars_decode($notification['data']['name']) . '"';
            $sender = Notifications::getSender($notification['from_user_id']);
            //divide notification into separate number of actions it consist of
            $actions = getNotificationActions($notification);
            for ($i = 0; $i < sizeof($actions); $i++) {
                $a = $actions[$i];
                $message = Notifications::getMailBodyForAction($a, $u);
                $isMentioned = !empty($a['data']['mentioned']) && in_array($uid, $a['data']['mentioned']);
                $mails[$isMentioned ? 'separate' : 'digest'][] = array('subject' => $subject, 'body' => $message, 'sender' => $sender, 'nId' => $notificationId);
            }
        }
        //merge digest emails group into one email and put it into separate group
        if (sizeof($mails['digest']) == 1) {
            $mails['separate'][] = $mails['digest'][0];
        } elseif (!empty($mails['digest'])) {
            $mail = array();
            $ids = array();
            $sender = '';
            foreach ($mails['digest'] as $m) {
                $mail[] = $m['body'];
                $sender = $m['sender'];
                $ids[] = $m['nId'];
            }
            $mails['separate'][] = array('subject' => '[' . $coreName . '] Notifications digest', 'body' => implode('<hr />', $mail), 'sender' => $sender, 'nId' => $ids);
        }
        foreach ($mails['separate'] as $mail) {
            echo $u['email'] . ': ' . $mail['subject'] . "\n";
            if (!mail($u['email'], $mail['subject'], $mail['body'], "Content-type: text/html; charset=utf-8\r\nFrom: " . $mail['sender'] . "\r\n")) {
                System::notifyAdmin('CaseBox cron notification: Cant send notification (' . $mail['nId'] . ') mail to "' . $u['email'] . '"', $mail['body']);
            } else {
                DM\Notifications::markAsSeen($mail['nId'], $uid);
            }
        }
        if (!empty($mails['digest'])) {
            User::setUserConfigParam('lastNotifyTime', Util\dateISOToMysql('now'), $uid);
        }
    }
}
/* check if this core has an email template defined */
$email_template_id = false;
$res = DB\dbQuery('SELECT id FROM templates WHERE `type` = \'email\'') or die(DB\dbQueryError());
if ($r = $res->fetch_assoc()) {
    $email_template_id = $r['id'];
}
$res->close();
if (!$email_template_id) {
    echo " there is no Email template defined in this core.\n";
    continue;
}
/* end of check if this core has an email template defined */
try {
    $mailbox = new Zend\Mail\Storage\Imap(array('host' => config\mail_host, 'port' => Util\coalesce(config\mail_port, 993), 'ssl' => config\mail_ssl == true, 'user' => config\mail_user, 'password' => config\mail_password));
} catch (\Exception $e) {
    System::notifyAdmin('Casebox: check mail Exception for core' . $coreName, $e->getMessage());
    echo " Error connecting to email\n";
    exit;
    // skip this core if mail cannot be accesed
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$files = new Files();
$mail_count = $mailbox->countMessages();
echo ' Mail count: ' . $mail_count . "\n";
$delete_ids = array();
$processed_ids = array();
$i = 0;
foreach ($mailbox as $k => $mail) {
    $i++;
    echo $i . ' ';
    if ($mail->hasFlag(Zend\Mail\Storage::FLAG_SEEN)) {
    $lang = $languages[$u['language_id'] - 1];
    if (filter_var($u['email'], FILTER_VALIDATE_EMAIL)) {
        foreach ($u['mails'] as $notificationId => $action) {
            //[$core #$nodeId] $action_type $template_name: $object_title
            $templateId = Objects::getTemplateId($action['object_id']);
            $templateName = Objects::getName($templateId);
            $subject = '[' . $coreName . ' #' . $action['object_id'] . '] ' . Notifications::getActionDeclination($action['action_type'], $lang) . ' ' . $templateName . ' "' . htmlspecialchars_decode($action['data']['name']) . '"';
            //skip sending notifications from devel server to other emails than Admin
            if (!$sendNotificationMails && $u['email'] !== $adminEmail) {
                echo 'Devel skip: ' . $u['email'] . ': ' . $subject . "\n";
            } else {
                echo $u['email'] . ': ' . $subject . "\n";
                $message = Notifications::getMailBodyForAction($action, $u);
                $sender = Notifications::getSender($action['from_user_id']);
                // file_put_contents(TEMP_DIR . $action['id'].'.html', "$sender<br />\n<h1>$subject<h1>" . $message);
                //  COMMENTED FOR TEST
                if (!mail($u['email'], $subject, $message, "Content-type: text/html; charset=utf-8\r\nFrom: " . $sender . "\r\n")) {
                    $markNotificationAsSent = false;
                    System::notifyAdmin('CaseBox cron notification: Cant send notification (' . $notificationId . ') mail to "' . $u['email'] . '"', $message);
                } else {
                    DB\dbQuery('UPDATE notifications
                        SET email_sent = 1
                        WHERE id = $1', $notificationId) or die(DB\dbQueryError());
                }
            }
        }
    }
    DB\dbQuery('UPDATE crons
        SET last_action = CURRENT_TIMESTAMP
        WHERE cron_id = $1', $cron_id) or die('error updating crons last action');
}