Example #1
0
 public function testHSort()
 {
     $first = new TestModel();
     $first->name = 'Thing A';
     $firstsub1 = new TestModel();
     $firstsub1->name = 'Thing 0';
     $firstsub1->parent = $first;
     $firstsub2 = new TestModel();
     $firstsub2->name = 'Thing 1';
     $firstsub2->parent = $first;
     $second = new TestModel();
     $second->name = 'thing B';
     $third = new TestModel();
     $third->name = 'Thing C';
     $firstsub3 = new TestModel();
     $firstsub3->name = 'Thing 0';
     $firstsub3->parent = $third;
     $fourth = new TestModel();
     $fourth->name = 'Thing D';
     $firstsub4 = new TestModel();
     $firstsub4->name = 'Thing 0';
     $firstsub4->parent = $fourth;
     $firstsub5 = new TestModel();
     $firstsub5->name = 'Thing 0.0';
     $firstsub5->parent = $firstsub4;
     $firstsub6 = new TestModel();
     $firstsub6->name = 'Thing 1';
     $firstsub6->parent = $fourth;
     $fifth = new TestModel();
     $fifth->name = 'Thing E';
     $sixth = new TestModel();
     $sixth->name = 'Thing F';
     $seventh = new TestModel();
     $seventh->name = 'Thing G';
     $eighth = new TestModel();
     $eighth->name = 'Thing H';
     $ninth = new TestModel();
     $ninth->name = 'Thing I';
     $tenth = new TestModel();
     $tenth->name = 'Thing J';
     $arr = [$second, $sixth, $firstsub6, $ninth, $firstsub2, $fifth, $third, $firstsub3, $tenth, $fourth, $first, $firstsub5, $seventh, $eighth, $firstsub4, $firstsub1];
     Nymph::hsort($arr, 'name', 'parent');
     /*foreach ($arr as $cur) {
         echo "\n".(isset($cur->parent) ? (isset($cur->parent->parent) ? "{$cur->parent->parent->name} : " : '')."{$cur->parent->name} : " : '')."$cur->name\n";
       }*/
     $this->assertEquals([$first, $firstsub1, $firstsub2, $second, $third, $firstsub3, $fourth, $firstsub4, $firstsub5, $firstsub6, $fifth, $sixth, $seventh, $eighth, $ninth, $tenth], $arr);
     Nymph::hsort($arr, 'name', 'parent', true, true);
     /*foreach ($arr as $cur) {
         echo "\n".(isset($cur->parent) ? (isset($cur->parent->parent) ? "{$cur->parent->parent->name} : " : '')."{$cur->parent->name} : " : '')."$cur->name\n";
       }*/
     $this->assertEquals([$second, $tenth, $ninth, $eighth, $seventh, $sixth, $fifth, $fourth, $firstsub6, $firstsub4, $firstsub5, $third, $firstsub3, $first, $firstsub2, $firstsub1], $arr);
 }
<?php

error_reporting(E_ALL);
require file_exists(__DIR__ . '/../vendor/autoload.php') ? __DIR__ . '/../vendor/autoload.php' : __DIR__ . '/../../autoload-dev.php';
use SciActive\RequirePHP;
RequirePHP::undef('NymphConfig');
RequirePHP::undef('Nymph');
$nymph_config = ['driver' => 'PostgreSQL'];
if (getenv('DATABASE_PGSQL')) {
    $dbopts = parse_url(getenv('DATABASE_PGSQL'));
    $nymph_config['PostgreSQL'] = ['database' => ltrim($dbopts["path"], '/'), 'host' => $dbopts["host"], 'port' => $dbopts["port"], 'user' => $dbopts["user"], 'password' => key_exists("pass", $dbopts) ? $dbopts["pass"] : ''];
} else {
    $nymph_config['PostgreSQL'] = ['database' => 'nymph_testing', 'user' => 'nymph_testing', 'password' => 'password'];
}
$nymph_config['pubsub'] = false;
\Nymph\Nymph::configure($nymph_config);
require_once 'TestModel.php';
Example #3
0
 protected function loadEntity($entityData)
 {
     if (!class_exists($entityData['class'])) {
         return false;
     }
     if ((int) $entityData['guid'] > 0) {
         $entity = Nymph::getEntity(['class' => $entityData['class']], ['&', 'guid' => (int) $entityData['guid']]);
         if ($entity === null) {
             return false;
         }
     } elseif (is_callable([$entityData['class'], 'factory'])) {
         $entity = call_user_func([$entityData['class'], 'factory']);
     } else {
         $entity = new $entityData['class']();
     }
     $entity->jsonAcceptTags($entityData['tags']);
     if (isset($entityData['cdate'])) {
         $entityData['data']['cdate'] = $entityData['cdate'];
     }
     if (isset($entityData['mdate'])) {
         $entityData['data']['mdate'] = $entityData['mdate'];
     }
     $entity->jsonAcceptData($entityData['data']);
     return $entity;
 }
Example #4
0
 /**
  * @param \µMailPHP\Definition The name of the mail definition class.
  * @param mixed $recipient The recipient's email address, or a recipient object. If left null, the rendition must have a recipient.
  * @param array $macros An associative array of macros. These override macros from the definition.
  * @param string|null $sender The sender's email address. If left null, the rendition or default sender will be used.
  * @param \µMailPHP\Rendition An optional rendition. If left null, the latest ready rendition will be used. If false, no rendition will be used.
  * @param \µMailPHP\Template An optional template. If left null, the latest ready template will be used. If false, the default template will be used.
  */
 public function __construct($definition, $recipient = null, $macros = [], $sender = null, $rendition = null, $template = null)
 {
     if (!class_exists($definition) || !is_subclass_of($definition, '\\µMailPHP\\Definition')) {
         throw new \InvalidArgumentException('Mail definition is required.');
     }
     $config = \SciActive\RequirePHP::_('µMailPHPConfig');
     // Format recipient.
     if ($recipient && is_string($recipient)) {
         $recipient = (object) ['email' => $recipient];
     }
     // Find any renditions.
     if ($rendition === null) {
         $renditions = (array) \Nymph\Nymph::getEntities(['class' => '\\µMailPHP\\Rendition', 'reverse' => true], ['&', 'strict' => [['enabled', true], ['definition', $definition]]]);
         foreach ($renditions as $cur_rendition) {
             if ($cur_rendition->ready()) {
                 $rendition = $cur_rendition;
                 break;
             }
         }
         unset($renditions, $cur_rendition);
     }
     // Get the email sender.
     if ($rendition && !isset($sender) && !empty($rendition->from)) {
         $sender = $rendition->from;
     }
     // Get the email recipient(s).
     if (!$recipient) {
         // If it's supposed to have a recipient already, report failure.
         if ($definition::expectsRecipient) {
             throw new \UnexpectedValueException('This email definition requires a recipient.');
         }
         if ($rendition) {
             if ($rendition->to) {
                 // Check Tilmeld users/groups if Tilmeld is loaded.
                 if (class_exists('\\Tilmeld\\User') && strpos($rendition->to, ',') === false) {
                     if (preg_match('/<.+@.+>/', $rendition->to)) {
                         $check_email = trim(preg_replace('/^.*<(.+@.+)>.*$/', '$1', $rendition->to));
                     } else {
                         $check_email = trim($rendition->to);
                     }
                     // Check for a user or group with that email.
                     $user = \Nymph\Nymph::getEntity(['class' => '\\Tilmeld\\User'], ['&', 'strict' => ['email', $check_email]]);
                     if ($user) {
                         $recipient = $user;
                     } else {
                         $group = \Nymph\Nymph::getEntity(['class' => '\\Tilmeld\\Group'], ['&', 'strict' => ['email', $check_email]]);
                         if ($group) {
                             $recipient = $group;
                         }
                     }
                 }
                 if (!$recipient) {
                     $recipient = (object) ['email' => $rendition->to];
                 }
             }
         } else {
             // Send to the master address if there's no recipient.
             if (!$config->master_address['value']) {
                 throw new \UnexpectedValueException('This email needs a recipient and no master address is set.');
             }
             $recipient = (object) ['email' => $config->master_address['value']];
         }
     }
     // Get the email contents.
     $body = [];
     if ($rendition) {
         $body['subject'] = $rendition->subject;
         $body['content'] = $rendition->content;
     } else {
         $body['subject'] = $definition::getSubject();
         $body['content'] = $definition::getHTML();
     }
     // Get the template.
     if ($template === null) {
         $templates = (array) \Nymph\Nymph::getEntities(['class' => '\\µMailPHP\\Template', 'reverse' => true], ['&', 'strict' => ['enabled', true]]);
         // Get the first template that's ready.
         foreach ($templates as $cur_template) {
             if ($cur_template->ready()) {
                 $template = $cur_template;
                 break;
             }
         }
         unset($templates, $cur_template);
     }
     // If there is no template, use a default one.
     if (!$template) {
         $template = new Template();
     }
     // Build the body of the email.
     $body['content'] = str_replace('#content#', $body['content'], str_replace('#content#', $template->content, $template->document));
     // Replace macros and search strings.
     foreach ($body as &$cur_field) {
         // Some of these str_replace calls are wrapped in a strpos call,
         // because they involve some processing just to make the call. In a
         // system sending out millions of emails, that adds up to a lot of
         // wasted CPU.
         foreach ((array) $template->replacements as $cur_string) {
             if (!$cur_string['macros']) {
                 continue;
             }
             $cur_field = str_replace($cur_string['search'], $cur_string['replace'], $cur_field);
         }
         if (strpos($cur_field, '#subject#') !== false) {
             $cur_field = str_replace('#subject#', htmlspecialchars($body['subject']), $cur_field);
         }
         // Links
         if (strpos($cur_field, '#site_link#') !== false) {
             $cur_field = str_replace('#site_link#', htmlspecialchars($config->site_link['value']), $cur_field);
         }
         // Recipient
         if (strpos($cur_field, '#to_username#') !== false) {
             $cur_field = str_replace('#to_username#', htmlspecialchars(isset($recipient->username) ? $recipient->username : (isset($recipient->groupname) ? $recipient->groupname : '')), $cur_field);
         }
         if (strpos($cur_field, '#to_name#') !== false) {
             $cur_field = str_replace('#to_name#', htmlspecialchars(isset($recipient->name) ? $recipient->name : ''), $cur_field);
         }
         if (strpos($cur_field, '#to_first_name#') !== false) {
             $cur_field = str_replace('#to_first_name#', htmlspecialchars(isset($recipient->nameFirst) ? $recipient->nameFirst : ''), $cur_field);
         }
         if (strpos($cur_field, '#to_last_name#') !== false) {
             $cur_field = str_replace('#to_last_name#', htmlspecialchars(isset($recipient->nameLast) ? $recipient->nameLast : ''), $cur_field);
         }
         if (strpos($cur_field, '#to_email#') !== false) {
             $cur_field = str_replace('#to_email#', htmlspecialchars(isset($recipient->email) ? $recipient->email : ''), $cur_field);
         }
         // Current User with Tilmeld.
         if (class_exists('\\Tilmeld\\User') && \Tilmeld\User::current()) {
             if (strpos($cur_field, '#username#') !== false) {
                 $cur_field = str_replace('#username#', htmlspecialchars(\Tilmeld\User::current()->username), $cur_field);
             }
             if (strpos($cur_field, '#name#') !== false) {
                 $cur_field = str_replace('#name#', htmlspecialchars(\Tilmeld\User::current()->name), $cur_field);
             }
             if (strpos($cur_field, '#first_name#') !== false) {
                 $cur_field = str_replace('#first_name#', htmlspecialchars(\Tilmeld\User::current()->nameFirst), $cur_field);
             }
             if (strpos($cur_field, '#last_name#') !== false) {
                 $cur_field = str_replace('#last_name#', htmlspecialchars(\Tilmeld\User::current()->nameLast), $cur_field);
             }
             if (strpos($cur_field, '#email#') !== false) {
                 $cur_field = str_replace('#email#', htmlspecialchars(\Tilmeld\User::current()->email), $cur_field);
             }
         }
         // Date/Time
         if (strpos($cur_field, '#datetime_sort#') !== false) {
             $cur_field = str_replace('#datetime_sort#', htmlspecialchars(Mail::formatDate(time(), 'full_sort')), $cur_field);
         }
         if (strpos($cur_field, '#datetime_short#') !== false) {
             $cur_field = str_replace('#datetime_short#', htmlspecialchars(Mail::formatDate(time(), 'full_short')), $cur_field);
         }
         if (strpos($cur_field, '#datetime_med#') !== false) {
             $cur_field = str_replace('#datetime_med#', htmlspecialchars(Mail::formatDate(time(), 'full_med')), $cur_field);
         }
         if (strpos($cur_field, '#datetime_long#') !== false) {
             $cur_field = str_replace('#datetime_long#', htmlspecialchars(Mail::formatDate(time(), 'full_long')), $cur_field);
         }
         if (strpos($cur_field, '#date_sort#') !== false) {
             $cur_field = str_replace('#date_sort#', htmlspecialchars(Mail::formatDate(time(), 'date_sort')), $cur_field);
         }
         if (strpos($cur_field, '#date_short#') !== false) {
             $cur_field = str_replace('#date_short#', htmlspecialchars(Mail::formatDate(time(), 'date_short')), $cur_field);
         }
         if (strpos($cur_field, '#date_med#') !== false) {
             $cur_field = str_replace('#date_med#', htmlspecialchars(Mail::formatDate(time(), 'date_med')), $cur_field);
         }
         if (strpos($cur_field, '#date_long#') !== false) {
             $cur_field = str_replace('#date_long#', htmlspecialchars(Mail::formatDate(time(), 'date_long')), $cur_field);
         }
         if (strpos($cur_field, '#time_sort#') !== false) {
             $cur_field = str_replace('#time_sort#', htmlspecialchars(Mail::formatDate(time(), 'time_sort')), $cur_field);
         }
         if (strpos($cur_field, '#time_short#') !== false) {
             $cur_field = str_replace('#time_short#', htmlspecialchars(Mail::formatDate(time(), 'time_short')), $cur_field);
         }
         if (strpos($cur_field, '#time_med#') !== false) {
             $cur_field = str_replace('#time_med#', htmlspecialchars(Mail::formatDate(time(), 'time_med')), $cur_field);
         }
         if (strpos($cur_field, '#time_long#') !== false) {
             $cur_field = str_replace('#time_long#', htmlspecialchars(Mail::formatDate(time(), 'time_long')), $cur_field);
         }
         // System
         if (strpos($cur_field, '#site_name#') !== false) {
             $cur_field = str_replace('#site_name#', htmlspecialchars($config->site_name['value']), $cur_field);
         }
         // Argument Macros
         foreach ($macros as $cur_name => $cur_value) {
             $cur_field = str_replace("#{$cur_name}#", $cur_value, $cur_field);
         }
         // Definition Macros
         foreach ($definition::macros as $cur_name => $cur_desc) {
             if (strpos($cur_field, "#{$cur_name}#") !== false) {
                 $cur_field = str_replace("#{$cur_name}#", $definition::getMacro($cur_name), $cur_field);
             }
         }
         foreach ((array) $template->replacements as $cur_string) {
             if ($cur_string['macros']) {
                 continue;
             }
             $cur_field = str_replace($cur_string['search'], $cur_string['replace'], $cur_field);
         }
     }
     unset($cur_field);
     // Add additional recipients.
     if ($rendition) {
         if ($rendition->cc) {
             $email->addHeader('CC', $rendition->cc);
         }
         if ($rendition->bcc) {
             $email->addHeader('BCC', $rendition->bcc);
         }
     }
     // Get default values for missing parameters.
     if (!isset($sender)) {
         $sender = $config->from_address['value'];
     }
     $destination = isset($recipient->name) ? "\"" . str_replace('"', '', $recipient->name) . "\" <{$recipient->email}>" : (isset($recipient->email) ? $recipient->email : '');
     // Validate incoming parameters.
     if (!preg_match('/^.+@.+$/', $sender)) {
         throw new \InvalidArgumentException('Invalid value for email sender.');
     }
     if (!preg_match('/^.+@.+$/', $destination)) {
         throw new \InvalidArgumentException('Invalid value for email recipient.');
     }
     if (!isset($body['subject']) || !is_string($body['subject']) || strlen($body['subject']) > 255) {
         throw new \LengthException('Invalid length for email subject.');
     }
     if (!isset($body['content']) || !is_string($body['content']) || strlen($body['content']) < 1) {
         throw new \UnexpectedValueException('Invalid value for email message.');
     }
     // Assign the complete variables.
     $this->sender = $sender;
     $this->recipient = $destination;
     $this->subject = $body['subject'];
     $this->message = $body['content'];
     // Define some default MIME headers
     $this->headers['MIME-Version'] = '1.0';
     $this->headers['Content-Type'] = 'multipart/mixed;boundary="MIME_BOUNDRY"';
     //$this->headers['X-Mailer'] = 'PHP5';
     $this->headers['X-Priority'] = '3';
     $this->headers['User-Agent'] = 'µMailPHP ' . Mail::VERSION;
     // Define some default MIME types
     $this->mimeTypes['doc'] = 'application/msword';
     $this->mimeTypes['pdf'] = 'application/pdf';
     $this->mimeTypes['gz'] = 'application/x-gzip';
     $this->mimeTypes['exe'] = 'application/x-msdos-program';
     $this->mimeTypes['rar'] = 'application/x-rar-compressed';
     $this->mimeTypes['swf'] = 'application/x-shockwave-flash';
     $this->mimeTypes['tgz'] = 'application/x-tar-gz';
     $this->mimeTypes['tar'] = 'application/x-tar';
     $this->mimeTypes['zip'] = 'application/zip';
     $this->mimeTypes['mid'] = 'audio/midi';
     $this->mimeTypes['mp3'] = 'audio/mpeg';
     $this->mimeTypes['au'] = 'audio/ulaw';
     $this->mimeTypes['aif'] = 'audio/x-aiff';
     $this->mimeTypes['aiff'] = 'audio/x-aiff';
     $this->mimeTypes['wma'] = 'audio/x-ms-wma';
     $this->mimeTypes['wav'] = 'audio/x-wav';
     $this->mimeTypes['gif'] = 'image/gif';
     $this->mimeTypes['jpg'] = 'image/jpeg';
     $this->mimeTypes['jpeg'] = 'image/jpeg';
     $this->mimeTypes['jpe'] = 'image/jpeg';
     $this->mimeTypes['png'] = 'image/png';
     $this->mimeTypes['tif'] = 'image/tiff';
     $this->mimeTypes['tiff'] = 'image/tiff';
     $this->mimeTypes['css'] = 'text/css';
     $this->mimeTypes['htm'] = 'text/html';
     $this->mimeTypes['html'] = 'text/html';
     $this->mimeTypes['txt'] = 'text/plain';
     $this->mimeTypes['rtf'] = 'text/rtf';
     $this->mimeTypes['xml'] = 'text/xml';
     $this->mimeTypes['flv'] = 'video/flv';
     $this->mimeTypes['mpe'] = 'video/mpeg';
     $this->mimeTypes['mpeg'] = 'video/mpeg';
     $this->mimeTypes['mpg'] = 'video/mpeg';
     $this->mimeTypes['mov'] = 'video/quicktime';
     $this->mimeTypes['asf'] = 'video/x-ms-asf';
     $this->mimeTypes['wmv'] = 'video/x-ms-wmv';
     $this->mimeTypes['avi'] = 'video/x-msvideo';
 }
Example #5
0
 /**
  * @depends testCreateEntity
  */
 public function testDelete($arr)
 {
     $testEntity = $arr['entity'];
     $guid = $testEntity->guid;
     // Deleting entity...
     $this->assertTrue($testEntity->delete());
     $this->assertNull($testEntity->guid);
     $entity = Nymph::getEntity(['class' => 'TestModel'], ['&', 'guid' => $guid]);
     $this->assertNull($entity);
 }
 /**
  * Handle a message from a client.
  *
  * @param WebSocketTransportInterface $user
  * @param WebSocketMessageInterface $msg
  */
 public function onMessage(WebSocketTransportInterface $user, WebSocketMessageInterface $msg)
 {
     $data = json_decode($msg->getData(), true);
     if (!$data['action'] || !in_array($data['action'], ['subscribe', 'unsubscribe', 'publish'])) {
         return;
     }
     switch ($data['action']) {
         case 'subscribe':
         case 'unsubscribe':
             if (isset($data['query'])) {
                 $args = json_decode($data['query'], true);
                 $count = count($args);
                 if ($count > 1) {
                     for ($i = 1; $i < $count; $i++) {
                         $newArg = \Nymph\REST::translateSelector($args[$i]);
                         if ($newArg === false) {
                             return;
                         }
                         $args[$i] = $newArg;
                     }
                 }
                 if (count($args) > 1) {
                     $options = $args[0];
                     unset($args[0]);
                     \Nymph\Nymph::formatSelectors($args);
                     $args = array_merge([$options], $args);
                 }
                 $args[0]['skip_ac'] = true;
                 $serialArgs = serialize($args);
                 if ($data['action'] === 'subscribe') {
                     if (!key_exists($serialArgs, $this->subscriptions['queries'])) {
                         $guidArgs = $args;
                         $guidArgs[0]['return'] = 'guid';
                         $this->subscriptions['queries'][$serialArgs] = ['current' => call_user_func_array("\\Nymph\\Nymph::getEntities", $guidArgs)];
                     }
                     $this->subscriptions['queries'][$serialArgs][] = ['client' => $user, 'query' => $data['query'], 'count' => !!$data['count']];
                     $this->logger->notice("Client subscribed to a query! ({$serialArgs}, {$user->getId()})");
                     if (RequirePHP::_('NymphPubSubConfig')['broadcast_counts']) {
                         // Notify clients of the subscription count.
                         $count = count($this->subscriptions['queries'][$serialArgs]) - 1;
                         foreach ($this->subscriptions['queries'][$serialArgs] as $key => $curClient) {
                             if ($key === 'current') {
                                 continue;
                             }
                             if ($curClient['count']) {
                                 $curClient['client']->sendString(json_encode(['query' => $curClient['query'], 'count' => $count]));
                             }
                         }
                     }
                 } elseif ($data['action'] === 'unsubscribe') {
                     if (!key_exists($serialArgs, $this->subscriptions['queries'])) {
                         return;
                     }
                     foreach ($this->subscriptions['queries'][$serialArgs] as $key => $value) {
                         if ($key === 'current') {
                             continue;
                         }
                         if ($user->getId() === $value['client']->getId() && $data['query'] === $value['query']) {
                             unset($this->subscriptions['queries'][$serialArgs][$key]);
                             $this->logger->notice("Client unsubscribed from a query! ({$serialArgs}, {$user->getId()})");
                             if (RequirePHP::_('NymphPubSubConfig')['broadcast_counts']) {
                                 // Notify clients of the subscription count.
                                 $count = count($this->subscriptions['queries'][$serialArgs]) - 1;
                                 foreach ($this->subscriptions['queries'][$serialArgs] as $key => $curClient) {
                                     if ($key === 'current') {
                                         continue;
                                     }
                                     if ($curClient['count']) {
                                         $curClient['client']->sendString(json_encode(['query' => $curClient['query'], 'count' => $count]));
                                     }
                                 }
                             }
                             if (count($this->subscriptions['queries'][$serialArgs]) === 1) {
                                 unset($this->subscriptions['queries'][$serialArgs]);
                             }
                         }
                     }
                 }
             } elseif (isset($data['uid']) && is_string($data['uid'])) {
                 if ($data['action'] === 'subscribe') {
                     if (!key_exists($data['uid'], $this->subscriptions['uids'])) {
                         $this->subscriptions['uids'][$data['uid']] = [];
                     }
                     $this->subscriptions['uids'][$data['uid']][] = ['client' => $user, 'count' => !!$data['count']];
                     $this->logger->notice("Client subscribed to a UID! ({$data['uid']}, {$user->getId()})");
                     if (RequirePHP::_('NymphPubSubConfig')['broadcast_counts']) {
                         // Notify clients of the subscription count.
                         $count = count($this->subscriptions['uids'][$data['uid']]);
                         foreach ($this->subscriptions['uids'][$data['uid']] as $curClient) {
                             if ($curClient['count']) {
                                 $curClient['client']->sendString(json_encode(['uid' => $data['uid'], 'count' => $count]));
                             }
                         }
                     }
                 } elseif ($data['action'] === 'unsubscribe') {
                     if (!key_exists($data['uid'], $this->subscriptions['uids'])) {
                         return;
                     }
                     foreach ($this->subscriptions['uids'][$data['uid']] as $key => $value) {
                         if ($user->getId() === $value['client']->getId()) {
                             unset($this->subscriptions['uids'][$data['uid']][$key]);
                             $this->logger->notice("Client unsubscribed from a UID! ({$data['uid']}, {$user->getId()})");
                             if (RequirePHP::_('NymphPubSubConfig')['broadcast_counts']) {
                                 // Notify clients of the subscription count.
                                 $count = count($this->subscriptions['uids'][$data['uid']]);
                                 foreach ($this->subscriptions['uids'][$data['uid']] as $curClient) {
                                     if ($curClient['count']) {
                                         $curClient['client']->sendString(json_encode(['uid' => $data['uid'], 'count' => $count]));
                                     }
                                 }
                             }
                             break;
                         }
                     }
                     if (count($this->subscriptions['uids'][$data['uid']]) === 0) {
                         unset($this->subscriptions['uids'][$data['uid']]);
                     }
                 }
             }
             break;
         case 'publish':
             if (isset($data['guid']) && ($data['event'] === 'delete' || isset($data['entity']) && ($data['event'] === 'create' || $data['event'] === 'update'))) {
                 $this->logger->notice("Received an entity publish! ({$data['guid']}, {$data['event']}, {$user->getId()})");
                 // Relay the publish to other servers.
                 $this->relay($msg->getData());
                 foreach ($this->subscriptions['queries'] as $curQuery => &$curClients) {
                     if ($data['event'] === 'delete' || $data['event'] === 'update') {
                         // Check if it is in any queries' currents.
                         if (in_array($data['guid'], $curClients['current'])) {
                             // Update currents list.
                             $guidArgs = unserialize($curQuery);
                             $guidArgs[0]['return'] = 'guid';
                             $curClients['current'] = call_user_func_array("\\Nymph\\Nymph::getEntities", $guidArgs);
                             // Notify subscribers.
                             foreach ($curClients as $key => $curClient) {
                                 if ($key === 'current') {
                                     continue;
                                 }
                                 $this->logger->notice("Notifying client of modification! ({$curClient['client']->getId()})");
                                 $curClient['client']->sendString(json_encode(['query' => $curClient['query']]));
                             }
                             continue;
                         }
                     }
                     // It isn't in the current matching entities.
                     if ($data['event'] === 'create' || $data['event'] === 'update') {
                         // Check if it matches any queries.
                         $selectors = unserialize($curQuery);
                         $options = $selectors[0];
                         unset($selectors[0]);
                         $entityData = $data['entity']['data'];
                         $entityData['cdate'] = $data['entity']['cdate'];
                         $entityData['mdate'] = $data['entity']['mdate'];
                         $entitySData = [];
                         if ($options['class'] === $data['entity']['class'] && \Nymph\Nymph::checkData($entityData, $entitySData, $selectors, $data['guid'], $data['entity']['tags'])) {
                             // Update currents list.
                             $guidArgs = unserialize($curQuery);
                             $guidArgs[0]['return'] = 'guid';
                             $curClients['current'] = call_user_func_array("\\Nymph\\Nymph::getEntities", $guidArgs);
                             // If we're here, it means the query didn't
                             // match the entity before, and now it does. We
                             // could check currents to see if it's been
                             // removed by limits, but that may give us bad
                             // data, since the client queries are filtered
                             // by Tilmeld.
                             // Notify subscribers.
                             foreach ($curClients as $key => $curClient) {
                                 if ($key === 'current') {
                                     continue;
                                 }
                                 $this->logger->notice("Notifying client of new match! ({$curClient['client']->getId()})");
                                 $curClient['client']->sendString(json_encode(['query' => $curClient['query']]));
                             }
                         }
                     }
                 }
                 unset($curClients);
             } elseif ((isset($data['name']) || isset($data['oldName']) && isset($data['newName'])) && in_array($data['event'], ['newUID', 'setUID', 'renameUID', 'deleteUID'])) {
                 $this->logger->notice("Received a UID publish! (" . (isset($data['name']) ? $data['name'] : "{$data['oldName']} => {$data['newName']}") . ", {$data['event']}, {$user->getId()})");
                 // Relay the publish to other servers.
                 $this->relay($msg->getData());
                 foreach ($data as $key => $name) {
                     if (!in_array($key, ['name', 'newName', 'oldName']) || !key_exists($name, $this->subscriptions['uids'])) {
                         continue;
                     }
                     foreach ($this->subscriptions['uids'][$name] as $curClient) {
                         $this->logger->notice("Notifying client of {$data['event']}! ({$name}, {$curClient['client']->getId()})");
                         $curClient['client']->sendString(json_encode(['uid' => $name]));
                     }
                 }
             }
             break;
     }
 }
Example #7
0
 public function testDeleteUID()
 {
     $this->assertTrue(Nymph::deleteUID('TestUID'));
     $this->assertNull(Nymph::getUID('TestUID'));
 }