Example #1
0
            $json = array('status' => false);
        }
        break;
    case 'countNewMessages':
        if (MessagesWebService::isValidApiKey($username, $apiKey)) {
            $webService = new MessagesWebService();
            $webService->setApiKey($apiKey);
            $lastId = isset($_POST['last']) ? $_POST['last'] : 0;
            $count = $webService->countNewMessages($username, $lastId);
            $json = array('status' => true, 'count' => $count);
        } else {
            $json = array('status' => false);
        }
        break;
    case 'getNewMessages':
        if (MessagesWebService::isValidApiKey($username, $apiKey)) {
            $webService = new MessagesWebService();
            $webService->setApiKey($apiKey);
            $lastId = isset($_POST['last']) ? $_POST['last'] : 0;
            $messages = $webService->getNewMessages($username, $lastId);
            $json = array('status' => true, 'messages' => $messages);
        } else {
            $json = array('status' => false);
        }
        break;
    default:
}
/* View */
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
echo json_encode($json);
 /**
  * Save message notification
  * @param int	    $type message type
  * NOTIFICATION_TYPE_MESSAGE,
  * NOTIFICATION_TYPE_INVITATION,
  * NOTIFICATION_TYPE_GROUP
  * @param array	    $user_list recipients: user list of ids
  * @param string	$title
  * @param string	$content
  * @param array	    $sender_info
  * result of api_get_user_info() or GroupPortalManager:get_group_data()
  */
 public function save_notification($type, $user_list, $title, $content, $senderInfo = array(), $attachments = array())
 {
     $this->type = intval($type);
     $content = $this->formatContent($content, $senderInfo);
     $titleToNotification = $this->formatTitle($title, $senderInfo);
     $settingToCheck = '';
     $avoid_my_self = false;
     switch ($this->type) {
         case self::NOTIFICATION_TYPE_DIRECT_MESSAGE:
         case self::NOTIFICATION_TYPE_MESSAGE:
             $settingToCheck = 'mail_notify_message';
             $defaultStatus = self::NOTIFY_MESSAGE_AT_ONCE;
             break;
         case self::NOTIFICATION_TYPE_INVITATION:
             $settingToCheck = 'mail_notify_invitation';
             $defaultStatus = self::NOTIFY_INVITATION_AT_ONCE;
             break;
         case self::NOTIFICATION_TYPE_GROUP:
             $settingToCheck = 'mail_notify_group_message';
             $defaultStatus = self::NOTIFY_GROUP_AT_ONCE;
             $avoid_my_self = true;
             break;
         default:
             $defaultStatus = self::NOTIFY_MESSAGE_AT_ONCE;
             break;
     }
     $settingInfo = UserManager::get_extra_field_information_by_name($settingToCheck);
     if (!empty($user_list)) {
         foreach ($user_list as $user_id) {
             if ($avoid_my_self) {
                 if ($user_id == api_get_user_id()) {
                     continue;
                 }
             }
             $userInfo = api_get_user_info($user_id);
             // Extra field was deleted or removed? Use the default status.
             $userSetting = $defaultStatus;
             if (!empty($settingInfo)) {
                 $extra_data = UserManager::get_extra_user_data($user_id);
                 if (isset($extra_data[$settingToCheck]) && !empty($extra_data[$settingToCheck])) {
                     $userSetting = $extra_data[$settingToCheck];
                 }
             }
             $sendDate = null;
             switch ($userSetting) {
                 // No notifications
                 case self::NOTIFY_MESSAGE_NO:
                 case self::NOTIFY_INVITATION_NO:
                 case self::NOTIFY_GROUP_NO:
                     break;
                     // Send notification right now!
                 // Send notification right now!
                 case self::NOTIFY_MESSAGE_AT_ONCE:
                 case self::NOTIFY_INVITATION_AT_ONCE:
                 case self::NOTIFY_GROUP_AT_ONCE:
                     $extraHeaders = [];
                     if (isset($senderInfo['email'])) {
                         $extraHeaders = array('reply_to' => array('name' => $senderInfo['complete_name'], 'mail' => $senderInfo['email']));
                     }
                     if (!empty($userInfo['email'])) {
                         api_mail_html($userInfo['complete_name'], $userInfo['mail'], Security::filter_terms($titleToNotification), Security::filter_terms($content), $this->adminName, $this->adminEmail, $extraHeaders, $attachments);
                     }
                     $sendDate = api_get_utc_datetime();
             }
             // Saving the notification to be sent some day.
             $content = cut($content, $this->max_content_length);
             $params = array('sent_at' => $sendDate, 'dest_user_id' => $user_id, 'dest_mail' => $userInfo['email'], 'title' => $title, 'content' => $content, 'send_freq' => $userSetting);
             $this->save($params);
         }
         MessagesWebService::sendPushNotification($user_list, $title, $content);
     }
 }