Esempio n. 1
0
 /**
  * Creates a channel if it does not exist and returns it.
  * If channel exists it is just returned.
  *
  * @param string $channelName
  *
  * @return WiseChatChannel
  */
 public function createAndGetChannel($channelName)
 {
     $channel = $this->channelsDAO->getByName($channelName);
     if ($channel === null) {
         $channel = new WiseChatChannel();
         $channel->setName($channelName);
         $this->channelsDAO->save($channel);
     }
     return $channel;
 }
 /**
  * Renders shortcode: [wise-chat-channel-stats]
  *
  * @param array $attributes
  * @return string
  */
 public function getRenderedChannelStatsShortcode($attributes)
 {
     if (!is_array($attributes)) {
         $attributes = array();
     }
     $attributes['channel'] = $this->service->getValidChatChannelName(array_key_exists('channel', $attributes) ? $attributes['channel'] : '');
     $channel = $this->channelsDAO->getByName($attributes['channel']);
     if ($channel !== null) {
         $this->options->replaceOptions($attributes);
         $this->messagesService->startUpMaintenance($channel);
         return $this->renderer->getRenderedChannelStats($channel);
     } else {
         return 'ERROR: channel does not exist';
     }
 }
Esempio n. 3
0
 /**
  * Returns array of various statistics for each channel.
  *
  * @return array Array of objects (fields: channel, messages, users, last_message)
  */
 public function getChannelsSummary()
 {
     global $wpdb;
     $table = WiseChatInstaller::getMessagesTable();
     $conditions = array();
     $conditions[] = "user != 'System'";
     $sql = "SELECT channel, count(*) AS messages, max(time) AS last_message FROM {$table} " . " WHERE " . implode(" AND ", $conditions) . " GROUP BY channel " . " ORDER BY channel ASC " . " LIMIT 1000;";
     $mainSummary = $wpdb->get_results($sql);
     $usersSummary = $this->channelUsersDAO->getAllChannelsStats();
     $usersSummaryMap = array();
     foreach ($usersSummary as $userDetails) {
         $usersSummaryMap[$userDetails->getChannel()->getName()] = intval($userDetails->getNumberOfUsers());
     }
     $mainSummaryMap = array();
     foreach ($mainSummary as $mainDetails) {
         $mainDetails->users = array_key_exists($mainDetails->channel, $usersSummaryMap) ? $usersSummaryMap[$mainDetails->channel] : 0;
         $mainSummaryMap[$mainDetails->channel] = $mainDetails;
     }
     $channels = $this->channelsDAO->getAll();
     $fullSummary = array();
     foreach ($channels as $channel) {
         if (array_key_exists($channel->getName(), $mainSummaryMap)) {
             $channelPrepared = $mainSummaryMap[$channel->getName()];
             $channelPrepared->secured = strlen($channel->getPassword()) > 0;
             $fullSummary[] = $channelPrepared;
         } else {
             $fullSummary[] = (object) array('channel' => $channel->getName(), 'messages' => 0, 'users' => array_key_exists($channel->getName(), $usersSummaryMap) ? $usersSummaryMap[$channel->getName()] : 0, 'last_message' => null, 'secured' => strlen($channel->getPassword()) > 0);
         }
     }
     return $fullSummary;
 }
Esempio n. 4
0
 /**
  * Endpoint for user's settings.
  */
 public function settingsEndpoint()
 {
     $this->verifyCheckSum();
     $response = array();
     try {
         $this->checkChatOpen();
         $this->checkUserAuthentication();
         $this->checkUserAuthorization();
         $this->checkUserWriteAuthorization();
         $this->checkPostParams(array('property', 'value'));
         $property = $this->getPostParam('property');
         $value = $this->getPostParam('value');
         switch ($property) {
             case 'userName':
                 $this->checkPostParams(array('channelId'));
                 $channel = $this->channelsDAO->get($this->getPostParam('channelId'));
                 $this->checkChannel($channel);
                 $this->checkChannelAuthorization($channel);
                 $response['value'] = $this->userService->changeUserName($value);
                 break;
             case 'textColor':
                 $this->userService->setUserTextColor($value);
                 break;
             default:
                 $this->userSettingsDAO->setSetting($property, $value);
         }
     } catch (WiseChatUnauthorizedAccessException $exception) {
         $response['error'] = $exception->getMessage();
         $this->sendUnauthorizedStatus();
     } catch (Exception $exception) {
         $response['error'] = $exception->getMessage();
         $this->sendBadRequestStatus();
     }
     echo json_encode($response);
     die;
 }