Ejemplo n.º 1
0
 /**
  * Retrive all entities from database
  *
  * The method retrives all entities from the database together with the
  * newest revision id.
  *
  * @param string        $subject The message title
  * @param string        $message The mesage body
  * @param arrayt|string $address Address for which the messege is sent to
  * @param int           $from    Uid of user responsible for sending the message
  *
  * @return false|array All entities from the database
  * @throws \Exception
  */
 public function post($subject, $message, $address, $from)
 {
     $external_messengers = $this->_config->getArray('messenger.external', array());
     $fromUser = $this->getUserService()->findById($from);
     // and prepend the userid to the message
     $message = 'User: '******'<br />' . $message;
     $addresses = array();
     if (!is_array($address)) {
         $addresses[] = $address;
     } else {
         $addresses = $address;
     }
     $entityManager = $this->getEntityManager();
     $addSpecialUserForHooks = $this->checkIfSpecialHookUserExists();
     foreach ($addresses as $ad) {
         $subscripers = $this->_getSubscripers($ad);
         if ($addSpecialUserForHooks) {
             $subscripers[] = array('uid' => '0', 'type' => 'INBOX');
         }
         foreach ($subscripers as $subscriper) {
             $subscribingUser = $this->getUserService()->findById($subscriper['uid']);
             // Create message
             $messageEntity = new Janus\ServiceRegistry\Entity\User\Message($subscribingUser, $subject, $message, $fromUser, $ad);
             $entityManager->persist($messageEntity);
             if (array_key_exists($subscriper['type'], $external_messengers)) {
                 $externalconfig = $external_messengers[$subscriper['type']];
                 try {
                     $messenger = sspmod_janus_Messenger::getInstance($externalconfig['class'], $externalconfig['option']);
                     $messenger->send(array('uid' => $subscriper['uid'], 'subject' => $subject, 'message' => $message, 'from' => $from, 'address' => $ad));
                 } catch (Exception $e) {
                     SimpleSAML_Logger::error('JANUS: Error sending external message. ' . $e->getMessage());
                 }
             }
         }
     }
     $entityManager->flush();
     return true;
 }
 /**
  * Retrive all entities from database
  *
  * The method retrives all entities from the database together with the
  * newest revision id.
  *
  * @param string        $subject The message title
  * @param string        $message The mesage body
  * @param arrayt|string $address Address for which the messege is sent to
  * @param int           $from    Uid of user responsible for sending the message
  *
  * @return false|array All entities from the database
  */
 public function post($subject, $message, $address, $from)
 {
     $external_messengers = $this->_config->getArray('messenger.external', array());
     // Grab the user who send the message
     $user = new sspmod_janus_User($this->_config);
     $user->setUid($from);
     $user->load();
     // and prepend the userid to the message
     $message = 'User: '******'<br />' . $message;
     $addresses = array();
     if (!is_array($address)) {
         $addresses[] = $address;
     } else {
         $addresses = $address;
     }
     foreach ($addresses as $ad) {
         $subscripers = $this->_getSubscripers($ad);
         $subscripers[] = array('uid' => '0', 'type' => 'INBOX');
         foreach ($subscripers as $subscriper) {
             $st = self::execute('INSERT INTO `' . self::$prefix . 'message`
                 (
                 `uid`, 
                 `subject`, 
                 `message`, 
                 `from`, 
                 `subscription`, 
                 `created`, 
                 `ip`
                 ) VALUES (?, ?, ?, ?, ?, ?, ?);', array($subscriper['uid'], $subject, $message, $from, $ad, date('c'), $_SERVER['REMOTE_ADDR']));
             if ($st === false) {
                 SimpleSAML_Logger::error('JANUS: Error fetching all entities');
                 return false;
             }
             if (array_key_exists($subscriper['type'], $external_messengers)) {
                 $externalconfig = $external_messengers[$subscriper['type']];
                 try {
                     $messenger = sspmod_janus_Messenger::getInstance($externalconfig['class'], $externalconfig['option']);
                     $messenger->send(array('uid' => $subscriper['uid'], 'subject' => $subject, 'message' => $message, 'from' => $from, 'address' => $ad));
                 } catch (Exception $e) {
                     SimpleSAML_Logger::error('JANUS: Error sending external message. ' . var_export($messenger, true));
                 }
             }
         }
     }
     return true;
 }