/**
  * @see TemplatePluginModifier::execute()
  */
 public function execute($tagArgs, Template $tplObj)
 {
     if (isset($tagArgs[2])) {
         $useStrftime = $tagArgs[2] ? true : false;
     } else {
         $useStrftime = isset($tagArgs[1]) ? true : false;
     }
     return DateUtil::formatShortTime(isset($tagArgs[1]) ? $tagArgs[1] : null, $tagArgs[0], isset($tagArgs[1]) ? false : true, $useStrftime);
 }
 /**
  * @see Action::execute()
  */
 public function execute()
 {
     parent::execute();
     // check permission
     WCF::getUser()->checkPermission('admin.user.canPMToUserGroups');
     $sql = "SELECT COUNT(DISTINCT u.userID) AS cnt" . "\n  FROM wcf" . WCF_N . "_user u" . "\n  LEFT JOIN wcf" . WCF_N . "_user_to_groups g ON (g.userID = u.userID)" . "\n  LEFT JOIN wcf" . WCF_N . "_group_option_value v ON (v.groupID = g.groupID)" . "\n  LEFT JOIN wcf" . WCF_N . "_group_option o ON (o.optionID = v.optionID)" . "\n WHERE o.optionName = 'user.pm.canUsePm'" . "\n   AND v.optionValue = '1'" . "\n   AND u.userID != " . $this->userID . "\n   AND g.groupID IN (" . $this->pmData['groupIDs'] . ")";
     $row = WCF::getDB()->getFirstRow($sql);
     $count = $row['cnt'];
     if (!$count > 0) {
         // clear session
         if (isset($this->pmData)) {
             $pmData = WCF::getSession()->getVar('pmData');
             unset($pmData[$this->pmSessionID]);
             WCF::getSession()->register('pmData', $pmData);
         }
         $this->finish('wcf.pmToUgrps.error.noRecipients', 'index.php?form=PMToUserGroups&packageID=' . PACKAGE_ID . SID_ARG_2ND_NOT_ENCODED);
     }
     if ($count <= $this->limit * $this->loop) {
         $endTime = TIME_NOW;
         $lf = "\n";
         // remove from outbox
         $sql = "UPDATE wcf" . WCF_N . "_pm" . "\n   SET saveInOutbox = 0" . "\n WHERE pmID = " . $this->pmID;
         WCF::getDB()->sendQuery($sql);
         // groups...
         $groups = '';
         $sql = "SELECT groupName" . "\n  FROM wcf" . WCF_N . "_group" . "\n WHERE groupID IN (" . $this->pmData['groupIDs'] . ")" . "\n ORDER BY groupName";
         $result = WCF::getDB()->sendQuery($sql);
         while ($row = WCF::getDB()->fetchArray($result)) {
             if (!empty($groups)) {
                 $groups .= ', ';
             }
             $groups .= StringUtil::decodeHTML($row['groupName']);
         }
         // log...
         $log = '';
         $subject = WCF::getLanguage()->get('wcf.pmToUgrps.log.subject', array('$pmID' => $this->pmID)) . ' ' . $this->pmData['subject'];
         if ($this->pmData['enableHtml']) {
             $log .= '<pre>';
         }
         $log .= WCF::getLanguage()->get('wcf.pmToUgrps.log.started', array('$startTime' => DateUtil::formatDate('%d.%m.%Y %H:%M:%S', $this->pmData['startTime']))) . $lf;
         $log .= WCF::getLanguage()->get('wcf.pmToUgrps.log.finished', array('$endTime' => DateUtil::formatDate('%d.%m.%Y %H:%M:%S', $endTime))) . $lf;
         $log .= WCF::getLanguage()->get('wcf.pmToUgrps.log.recipients', array('$groups' => $groups, '$count' => StringUtil::decodeHTML(StringUtil::formatInteger($count)))) . $lf;
         $log .= str_repeat('-', 60) . $lf;
         if ($this->pmData['enableHtml']) {
             $log .= '</pre>' . $lf;
         }
         $log .= $this->pmData['text'];
         $this->recipientArray = $this->blindCopyArray = array();
         $this->recipientArray[0]['userID'] = $this->userID;
         $this->recipientArray[0]['username'] = $this->username;
         PMEditor::create($this->draft, $this->recipientArray, $this->blindCopyArray, $subject, $log, $this->userID, $this->username, array('enableSmilies' => $this->pmData['enableSmilies'], 'enableHtml' => $this->pmData['enableHtml'], 'enableBBCodes' => $this->pmData['enableBBCodes'], 'showSignature' => false));
         // clear session
         $pmData = WCF::getSession()->getVar('pmData');
         unset($pmData[$this->pmSessionID]);
         WCF::getSession()->register('pmData', $pmData);
         $this->calcProgress();
         $msg = WCF::getLanguage()->get('wcf.pmToUgrps.finish', array('$count' => StringUtil::decodeHTML(StringUtil::formatInteger($count)), '$startTime' => DateUtil::formatShortTime('%H:%M:%S', $this->pmData['startTime']), '$endTime' => DateUtil::formatShortTime('%H:%M:%S', $endTime)));
         $this->finish($msg, 'index.php?form=PMToUserGroups&packageID=' . PACKAGE_ID . SID_ARG_2ND_NOT_ENCODED);
     }
     // get users
     $sql = "SELECT DISTINCT u.userID, u.username" . "\n  FROM wcf" . WCF_N . "_user u" . "\n  LEFT JOIN wcf" . WCF_N . "_user_to_groups g ON (g.userID = u.userID)" . "\n  LEFT JOIN wcf" . WCF_N . "_group_option_value v ON (v.groupID = g.groupID)" . "\n  LEFT JOIN wcf" . WCF_N . "_group_option o ON (o.optionID = v.optionID)" . "\n WHERE o.optionName = 'user.pm.canUsePm'" . "\n   AND v.optionValue = '1'" . "\n   AND u.userID != " . $this->userID . "\n   AND g.groupID IN (" . $this->pmData['groupIDs'] . ")" . "\n ORDER BY u.userID";
     $this->blindCopyArray = array();
     $i = 0;
     $result = WCF::getDB()->sendQuery($sql, $this->limit, $this->limit * $this->loop);
     while ($row = WCF::getDB()->fetchArray($result)) {
         $this->blindCopyArray[$i]['userID'] = $row['userID'];
         $this->blindCopyArray[$i]['username'] = $row['username'];
         $i++;
     }
     if (count($this->blindCopyArray)) {
         if (empty($this->pmID)) {
             $tmp = PMEditor::create($this->draft, $this->recipientArray, $this->blindCopyArray, $this->pmData['subject'], $this->pmData['text'], $this->userID, $this->username, array('enableSmilies' => $this->pmData['enableSmilies'], 'enableHtml' => $this->pmData['enableHtml'], 'enableBBCodes' => $this->pmData['enableBBCodes'], 'showSignature' => $this->pmData['showSignature']));
             if ($tmp->pmID) {
                 $this->pmID = intval($tmp->pmID);
                 $pmData = WCF::getSession()->getVar('pmData');
                 $pmData[$this->pmSessionID]['pmID'] = $this->pmID;
                 WCF::getSession()->register('pmData', $pmData);
                 $mlt = intval($this->pmData['maxLifeTime']);
                 if ($mlt > 0) {
                     $mlt = $this->pmData['startTime'] + 86400 * $mlt;
                 } else {
                     $mlt = 0;
                 }
                 $sql = "INSERT IGNORE INTO wcf" . WCF_N . "_pm_bulk_mailing" . "\n       (pmID, elapsedTime, time, userID)" . "\nVALUES (" . $this->pmID . ", " . $mlt . ", " . $this->pmData['startTime'] . ", " . $this->userID . ")";
                 WCF::getDB()->sendQuery($sql);
             }
         } else {
             $recipientIDs = $inserts = '';
             foreach ($this->blindCopyArray as $k => $v) {
                 $username = WCF::getDB()->escapeString($this->blindCopyArray[$k]['username']);
                 if (!empty($recipientIDs)) {
                     $recipientIDs .= ',';
                 }
                 $recipientIDs .= $this->blindCopyArray[$k]['userID'];
                 if (!empty($inserts)) {
                     $inserts .= ',';
                 }
                 $inserts .= "\n       (" . $this->pmID . ", " . intval($this->blindCopyArray[$k]['userID']) . ", '" . $username . "', 1)";
             }
             if (!empty($recipientIDs) && !empty($inserts)) {
                 $sql = "INSERT IGNORE INTO wcf" . WCF_N . "_pm_to_user" . "\n       (pmID, recipientID, recipient, isBlindCopy)" . "\nVALUES " . $inserts;
                 WCF::getDB()->sendQuery($sql);
                 PMEditor::updateUnreadMessageCount($recipientIDs);
                 PMEditor::updateTotalMessageCount($recipientIDs);
                 Session::resetSessions($recipientIDs, true, false);
             }
         }
     }
     $this->executed();
     $this->calcProgress($this->limit * $this->loop, $count);
     $msg = WCF::getLanguage()->get('wcf.pmToUgrps.progress', array('$loop' => StringUtil::decodeHTML(StringUtil::formatInteger($this->limit * $this->loop)), '$count' => StringUtil::decodeHTML(StringUtil::formatInteger($count))));
     $this->nextLoop($msg, 'index.php?action=' . $this->action . '&pmSessionID=' . $this->pmSessionID . '&limit=' . $this->limit . '&loop=' . ($this->loop + 1) . '&packageID=' . PACKAGE_ID . SID_ARG_2ND_NOT_ENCODED);
 }
 public function getAppointmentList()
 {
     $ret = array();
     $i = 0;
     $limit = intval(WBBCore::getUser()->monthlyCalendarBox_maxAppointments);
     $showPublic = intval(WBBCore::getUser()->monthlyCalendarBox_showPublicAppointments);
     $showBirthdays = intval(WBBCore::getUser()->monthlyCalendarBox_showBirthdaysInAppointments);
     $maxDays = intval(WBBCore::getUser()->monthlyCalendarBox_maxAppointmentDays);
     $userID = intval(WCF::getUser()->userID);
     if (!$limit > 0) {
         $limit = 10;
     }
     if (empty($maxDays)) {
         $maxDays = 30;
     }
     if (empty($userID)) {
         $showPublic = 1;
         $showBirthdays = 1;
     }
     $m = intval(date('n'));
     $y = intval(date('Y'));
     $d = intval(date('j'));
     $sTimestamp = mktime(0, 0, 0, $m, $d, $y);
     $eTimestamp = $sTimestamp + 86400;
     // WoltLab Calendar...
     if (WBBCore::getUser()->getPermission('user.calendar.canUseCalendar')) {
         require_once WCF_DIR . 'lib/util/CalendarUtil.class.php';
         require_once WCF_DIR . 'lib/data/calendar/event/EventList.class.php';
         $cals = Calendar::getEnabledCalendars();
         if (empty($showBirthdays)) {
             foreach ($cals as $k => $v) {
                 if ($v->className == 'BirthdayEvent') {
                     unset($cals[$k]);
                     break;
                 }
             }
         }
         if (!empty($userID) && empty($showPublic)) {
             foreach ($cals as $k => $v) {
                 if ($v->ownerID != $userID && $v->className != 'BirthdayEvent') {
                     unset($cals[$k]);
                 }
             }
         }
         $events = new EventList($sTimestamp, $sTimestamp + 86400 * $maxDays, $cals);
         $events->readEvents();
         $myEvents = $events->getEvents($limit);
         //            file_put_contents('/tmp/debug.txt', print_r($myEvents, true));
         foreach ($myEvents as $event) {
             if ($showBirthdays && $event->calendar->className == 'BirthdayEvent' && $event->user && !$event->eventID) {
                 $ret[$i]['birthday'] = true;
                 $ret[$i]['userID'] = $event->userID;
                 $ret[$i]['username'] = $event->user->username;
                 $ret[$i]['age'] = $event->user->age;
                 $ret[$i]['time'] = $event->startTime;
                 $ret[$i]['eventID'] = null;
             } else {
                 $ret[$i]['birthday'] = false;
                 $ret[$i]['eventID'] = $event->eventID;
             }
             if ($event->isFullDay) {
                 $ret[$i]['startTime'] = DateUtil::getUTC($event->startTime);
                 $ret[$i]['endTime'] = DateUtil::getUTC($event->endTime);
             } else {
                 $ret[$i]['startTime'] = $event->startTime;
                 $ret[$i]['endTime'] = $event->endTime;
             }
             $ret[$i]['fullDay'] = $event->isFullDay;
             $ret[$i]['subject'] = $event->subject;
             $ret[$i]['severalDays'] = false;
             $ret[$i]['curYear'] = true;
             $ret[$i]['sameDay'] = false;
             $ret[$i]['color'] = $event->color;
             $ret[$i]['today'] = false;
             $ret[$i]['title'] = '';
             if ($ret[$i]['startTime'] >= $sTimestamp && $ret[$i]['endTime'] < $eTimestamp) {
                 $ret[$i]['today'] = true;
             } else {
                 $ret[$i]['today'] = false;
                 if (date('j', $ret[$i]['startTime']) != date('j', $ret[$i]['endTime'])) {
                     $ret[$i]['severalDays'] = true;
                 }
                 if (date('Y', $ret[$i]['startTime']) != date('Y')) {
                     $ret[$i]['curYear'] = false;
                 }
             }
             if ($ret[$i]['severalDays']) {
                 $ret[$i]['title'] = DateUtil::formatDate('%d.%m. %H:%M', $ret[$i]['startTime']) . ' - ' . DateUtil::formatDate('%d.%m. %H:%M', $ret[$i]['endTime']);
             } else {
                 $ret[$i]['title'] = DateUtil::formatDate('%H:%M', $ret[$i]['startTime']) . '-' . DateUtil::formatDate('%H:%M', $ret[$i]['endTime']);
             }
             $ret[$i]['title'] .= ': ' . $ret[$i]['subject'];
             $i++;
         }
     } else {
         if (WBBCore::getUser()->getPermission('user.calendar.canEnter')) {
             if (!empty($showBirthdays)) {
                 $birthdays = self::getBirthdayList($y, $m, $d);
                 $color = '';
                 $isEnabled = 1;
                 foreach ($birthdays as $k => $v) {
                     $ret[$i]['birthday'] = true;
                     $ret[$i]['color'] = $color;
                     $ret[$i]['userID'] = $v['userID'];
                     $ret[$i]['username'] = $v['username'];
                     $ret[$i]['age'] = $v['age'];
                     $ret[$i]['time'] = $v['time'];
                     $i++;
                 }
             }
             $sql = "SELECT ce.eventID, cem.subject AS subject, ce.eventTime AS startTime, ce.eventEndTime AS endTime, ce.isFullDay AS fullDay" . "\n  FROM wcf" . WCF_N . "_calendar_event ce" . "\n  LEFT JOIN wcf" . WCF_N . "_calendar_event_message cem ON (cem.eventID = ce.eventID)" . "\n WHERE (ce.eventTime >= " . TIME_NOW . "\n    OR (ce.isFullDay = 1 AND ce.eventTime >= " . $sTimestamp . ")" . "\n    OR (ce.eventEndTime > ce.eventTime AND ce.eventEndTime > " . $sTimestamp . "))" . "\n   AND cem.isDeleted != 1";
             if (!empty($userID) && empty($showPublic)) {
                 $sql .= "\n   AND cem.userID = " . $userID;
             }
             $sql .= "\n ORDER BY ce.eventTime" . "\n LIMIT " . $limit;
             $result = WBBCore::getDB()->sendQuery($sql);
             while ($row = WBBCore::getDB()->fetchArray($result)) {
                 if (!empty($row['fullDay'])) {
                     $tM = intval(date('n', $row['startTime']));
                     $tY = intval(date('Y', $row['startTime']));
                     $tD = intval(date('j', $row['startTime']));
                     $row['startTime'] = mktime(0, 0, 0, $tM, $tD, $tY);
                     if (empty($row['endTime'])) {
                         $row['endTime'] = mktime(0, 0, 0, $tM, $tD, $tY);
                     } else {
                         $tM = intval(date('n', $row['endTime']));
                         $tY = intval(date('Y', $row['endTime']));
                         $tD = intval(date('j', $row['endTime']));
                         $row['endTime'] = mktime(0, 0, 0, $tM, $tD, $tY);
                     }
                 }
                 if (empty($row['endTime'])) {
                     $row['endTime'] = $row['startTime'];
                 }
                 $ret[$i]['fullDay'] = $row['fullDay'];
                 $ret[$i]['birthday'] = false;
                 $ret[$i]['color'] = '';
                 $ret[$i]['eventID'] = $row['eventID'];
                 $ret[$i]['subject'] = $row['subject'];
                 $ret[$i]['startTime'] = $row['startTime'];
                 $ret[$i]['endTime'] = $row['endTime'];
                 $ret[$i]['severalDays'] = false;
                 $ret[$i]['curYear'] = true;
                 $ret[$i]['sameDay'] = false;
                 if ($row['startTime'] >= $sTimestamp && $row['endTime'] <= $eTimestamp) {
                     $ret[$i]['today'] = true;
                 } else {
                     $ret[$i]['today'] = false;
                     if (date('j', $row['startTime']) != date('j', $row['endTime'])) {
                         $ret[$i]['severalDays'] = true;
                     }
                     if (date('Y', $row['startTime']) != date('Y')) {
                         $ret[$i]['curYear'] = false;
                     }
                 }
                 if ($ret[$i]['severalDays']) {
                     $ret[$i]['title'] = DateUtil::formatShortTime('%d.%m. %H:%M', $row['startTime']) . ' - ' . DateUtil::formatShortTime('%d.%m. %H:%M', $row['endTime']);
                 } else {
                     $ret[$i]['title'] = DateUtil::formatShortTime('%H:%M', $row['startTime']) . '-' . DateUtil::formatShortTime('%H:%M', $row['endTime']);
                 }
                 $ret[$i]['title'] .= ': ' . $row['subject'];
                 $i++;
             }
         }
     }
     return $ret;
 }