Esempio n. 1
0
 protected function getTwitterFeed($displayId = 0, $isPreview = true)
 {
     if (!extension_loaded('curl')) {
         trigger_error(__('cURL extension is required for Twitter'));
         return false;
     }
     // Do we need to add a geoCode?
     $geoCode = '';
     $distance = $this->GetOption('tweetDistance');
     if ($distance != 0) {
         // Use the display ID or the default.
         if ($displayId != 0) {
             // Look up the lat/long
             $display = new Display();
             $display->displayId = $displayId;
             $display->Load();
             $defaultLat = $display->latitude;
             $defaultLong = $display->longitude;
         } else {
             $defaultLat = Config::GetSetting('DEFAULT_LAT');
             $defaultLong = Config::GetSetting('DEFAULT_LONG');
         }
         // Built the geoCode string.
         $geoCode = implode(',', array($defaultLat, $defaultLong, $distance)) . 'mi';
     }
     // Connect to twitter and get the twitter feed.
     $key = md5($this->GetOption('searchTerm') . $this->GetOption('resultType') . $this->GetOption('tweetCount', 15) . $geoCode);
     if (!Cache::has($key) || Cache::get($key) == '') {
         Debug::Audit('Querying API for ' . $this->GetOption('searchTerm'));
         // We need to search for it
         if (!($token = $this->getToken())) {
             return false;
         }
         // We have the token, make a tweet
         if (!($data = $this->searchApi($token, $this->GetOption('searchTerm'), $this->GetOption('resultType'), $geoCode, $this->GetOption('tweetCount', 15)))) {
             return false;
         }
         // Cache it
         Cache::put($key, $data, $this->GetSetting('cachePeriod'));
     } else {
         Debug::Audit('Served from Cache');
         $data = Cache::get($key);
     }
     Debug::Audit(var_export(json_encode($data), true));
     // Get the template
     $template = $this->GetRawNode('template');
     // Parse the text template
     $matches = '';
     preg_match_all('/\\[.*?\\]/', $template, $matches);
     // Build an array to return
     $return = array();
     // Media Object to get profile images
     $media = new Media();
     $layout = new Layout();
     // Expiry time for any media that is downloaded
     $expires = time() + $this->GetSetting('cachePeriodImages') * 60 * 60;
     // Remove URL setting
     $removeUrls = $this->GetOption('removeUrls', 1);
     // If we have nothing to show, display a no tweets message.
     if (count($data->statuses) <= 0) {
         // Create ourselves an empty tweet so that the rest of the code can continue as normal
         $user = new stdClass();
         $user->name = '';
         $user->screen_name = '';
         $user->profile_image_url = '';
         $tweet = new stdClass();
         $tweet->text = $this->GetOption('noTweetsMessage', __('There are no tweets to display'));
         $tweet->created_at = date("Y-m-d H:i:s");
         $tweet->user = $user;
         // Append to our statuses
         $data->statuses[] = $tweet;
     }
     // This should return the formatted items.
     foreach ($data->statuses as $tweet) {
         // Substitute for all matches in the template
         $rowString = $template;
         foreach ($matches[0] as $sub) {
             // Always clear the stored template replacement
             $replace = '';
             // Maybe make this more generic?
             switch ($sub) {
                 case '[Tweet]':
                     // Get the tweet text to operate on
                     $tweetText = $tweet->text;
                     // Replace URLs with their display_url before removal
                     if (isset($tweet->entities->urls)) {
                         foreach ($tweet->entities->urls as $url) {
                             $tweetText = str_replace($url->url, $url->display_url, $tweetText);
                         }
                     }
                     // Handle URL removal if requested
                     if ($removeUrls == 1) {
                         $tweetText = preg_replace("((https?|ftp|gopher|telnet|file|notes|ms-help):((\\/\\/)|(\\\\))+[\\w\\d:#\\@%\\/;\$()~_?\\+-=\\\\.&]*)", '', $tweetText);
                     }
                     $replace = emoji_unified_to_html($tweetText);
                     break;
                 case '[User]':
                     $replace = $tweet->user->name;
                     break;
                 case '[ScreenName]':
                     $replace = $tweet->user->screen_name;
                     break;
                 case '[Date]':
                     $replace = date($this->GetOption('dateFormat', Config::GetSetting('DATE_FORMAT')), DateManager::getDateFromGregorianString($tweet->created_at));
                     break;
                 case '[ProfileImage]':
                     // Grab the profile image
                     if ($tweet->user->profile_image_url != '') {
                         $file = $media->addModuleFileFromUrl($tweet->user->profile_image_url, 'twitter_' . $tweet->user->id, $expires);
                         // Tag this layout with this file
                         $layout->AddLk($this->layoutid, 'module', $file['mediaId']);
                         $replace = $isPreview ? '<img src="index.php?p=module&mod=image&q=Exec&method=GetResource&mediaid=' . $file['mediaId'] . '" />' : '<img src="' . $file['storedAs'] . '" />';
                     }
                     break;
                 case '[Photo]':
                     // See if there are any photos associated with this tweet.
                     if (isset($tweet->entities->media) && count($tweet->entities->media) > 0) {
                         // Only take the first one
                         $photoUrl = $tweet->entities->media[0]->media_url;
                         if ($photoUrl != '') {
                             $file = $media->addModuleFileFromUrl($photoUrl, 'twitter_photo_' . $tweet->user->id . '_' . $tweet->entities->media[0]->id_str, $expires);
                             $replace = $isPreview ? '<img src="index.php?p=module&mod=image&q=Exec&method=GetResource&mediaid=' . $file['mediaId'] . '" />' : '<img src="' . $file['storedAs'] . '" />';
                             // Tag this layout with this file
                             $layout->AddLk($this->layoutid, 'module', $file['mediaId']);
                         }
                     }
                     break;
                 default:
                     $replace = '';
             }
             $rowString = str_replace($sub, $replace, $rowString);
         }
         // Substitute the replacement we have found (it might be '')
         $return[] = $rowString;
     }
     // Return the data array
     return $return;
 }
Esempio n. 2
0
 /**
  * Modify Display form
  */
 function displayForm()
 {
     $response = new ResponseManager();
     // Get the display Id
     $displayObject = new Display();
     $displayObject->displayId = Kit::GetParam('displayid', _GET, _INT);
     $auth = $this->user->DisplayGroupAuth($this->GetDisplayGroupId($displayObject->displayId), true);
     if (!$auth->edit) {
         trigger_error(__('You do not have permission to edit this display'), E_USER_ERROR);
     }
     // Load this display
     if (!$displayObject->Load()) {
         trigger_error($displayObject->GetErrorMessage(), E_USER_ERROR);
     }
     // Set some information about the form
     Theme::Set('form_id', 'DisplayEditForm');
     Theme::Set('form_action', 'index.php?p=display&q=modify');
     Theme::Set('form_meta', '<input type="hidden" name="displayid" value="' . $displayObject->displayId . '" />');
     // Column 1
     $formFields = array();
     $formFields[] = FormManager::AddText('display', __('Display'), $displayObject->display, __('The Name of the Display - (1 - 50 characters).'), 'd', 'required');
     $formFields[] = FormManager::AddText('hardwareKey', __('Display\'s Hardware Key'), $displayObject->license, __('A unique identifier for this display.'), 'h', 'required', NULL, false);
     $formFields[] = FormManager::AddText('description', __('Description'), $displayObject->description, __('A description - (1 - 254 characters).'), 'p', 'maxlength="50"');
     $formFields[] = FormManager::AddCombo('licensed', __('Licence Display?'), $displayObject->licensed, array(array('licensedid' => '1', 'licensed' => 'Yes'), array('licensedid' => '0', 'licensed' => 'No')), 'licensedid', 'licensed', __('Use one of the available licenses for this display?'), 'l');
     $formFields[] = FormManager::AddCombo('defaultlayoutid', __('Default Layout'), $displayObject->defaultLayoutId, $this->user->LayoutList(), 'layoutid', 'layout', __('The Default Layout to Display where there is no other content.'), 't');
     Theme::Set('form_fields_general', $formFields);
     // Maintenance
     $formFields = array();
     $formFields[] = FormManager::AddCombo('email_alert', __('Email Alerts'), $displayObject->emailAlert, array(array('id' => '1', 'value' => 'Yes'), array('id' => '0', 'value' => 'No')), 'id', 'value', __('Do you want to be notified by email if there is a problem with this display?'), 'a');
     $formFields[] = FormManager::AddCheckbox('alert_timeout', __('Use the Global Timeout?'), $displayObject->alertTimeout, __('Should this display be tested against the global time out or the client collection interval?'), 'o');
     Theme::Set('form_fields_maintenance', $formFields);
     // Location
     $formFields = array();
     $formFields[] = FormManager::AddNumber('latitude', __('Latitude'), $displayObject->latitude, __('The Latitude of this display'), 'g');
     $formFields[] = FormManager::AddNumber('longitude', __('Longitude'), $displayObject->longitude, __('The Longitude of this Display'), 'g');
     Theme::Set('form_fields_location', $formFields);
     // Wake on LAN
     $formFields = array();
     $formFields[] = FormManager::AddCheckbox('wakeOnLanEnabled', __('Enable Wake on LAN'), $displayObject->wakeOnLanEnabled, __('Wake on Lan requires the correct network configuration to route the magic packet to the display PC'), 'w');
     $formFields[] = FormManager::AddText('broadCastAddress', __('BroadCast Address'), $displayObject->broadCastAddress == '' ? $displayObject->clientAddress : $displayObject->broadCastAddress, __('The IP address of the remote host\'s broadcast address (or gateway)'), 'b');
     $formFields[] = FormManager::AddText('secureOn', __('Wake on LAN SecureOn'), $displayObject->secureOn, __('Enter a hexadecimal password of a SecureOn enabled Network Interface Card (NIC) of the remote host. Enter a value in this pattern: \'xx-xx-xx-xx-xx-xx\'. Leave the following field empty, if SecureOn is not used (for example, because the NIC of the remote host does not support SecureOn).'), 's');
     $formFields[] = FormManager::AddText('wakeOnLanTime', __('Wake on LAN Time'), $displayObject->wakeOnLanTime, __('The time this display should receive the WOL command, using the 24hr clock - e.g. 19:00. Maintenance must be enabled.'), 't');
     $formFields[] = FormManager::AddText('cidr', __('Wake on LAN CIDR'), $displayObject->cidr, __('Enter a number within the range of 0 to 32 in the following field. Leave the following field empty, if no subnet mask should be used (CIDR = 0). If the remote host\'s broadcast address is unknown: Enter the host name or IP address of the remote host in Broad Cast Address and enter the CIDR subnet mask of the remote host in this field.'), 'c');
     Theme::Set('form_fields_wol', $formFields);
     // Advanced
     $formFields = array();
     $displayProfileList = $this->user->DisplayProfileList(NULL, array('type' => $displayObject->clientType));
     array_unshift($displayProfileList, array('displayprofileid' => 0, 'name' => ''));
     $formFields[] = FormManager::AddCombo('displayprofileid', __('Settings Profile?'), $displayObject->displayProfileId, $displayProfileList, 'displayprofileid', 'name', __('What display profile should this display use?'), 'p');
     $formFields[] = FormManager::AddCombo('inc_schedule', __('Interleave Default'), $displayObject->incSchedule, array(array('id' => '1', 'value' => 'Yes'), array('id' => '0', 'value' => 'No')), 'id', 'value', __('Whether to always put the default layout into the cycle.'), 'i');
     $formFields[] = FormManager::AddCombo('auditing', __('Auditing'), $displayObject->isAuditing, array(array('id' => '1', 'value' => 'Yes'), array('id' => '0', 'value' => 'No')), 'id', 'value', __('Collect auditing from this client. Should only be used if there is a problem with the display.'), 'a');
     // Show the resolved settings for this display.
     $formFields[] = FormManager::AddMessage(__('The settings for this display are shown below. They are taken from the active Display Profile for this Display, which can be changed in Display Settings. If you have altered the Settings Profile above, you will need to save and re-show the form.'));
     // Build a table for the settings to be shown in
     $cols = array(array('name' => 'title', 'title' => __('Setting')), array('name' => 'valueString', 'title' => __('Value')));
     // Get the settings from the profile
     $profile = $displayObject->getSettingsProfile();
     // Go through each one, and see if it is a drop down
     for ($i = 0; $i < count($profile); $i++) {
         // Always update the value string with the source value
         $profile[$i]['valueString'] = $profile[$i]['value'];
         // Overwrite the value string when we are dealing with dropdowns
         if ($profile[$i]['fieldType'] == 'dropdown') {
             // Update our value
             foreach ($profile[$i]['options'] as $option) {
                 if ($option['id'] == $profile[$i]['value']) {
                     $profile[$i]['valueString'] = $option['value'];
                 }
             }
         } else {
             if ($profile[$i]['fieldType'] == 'timePicker') {
                 $profile[$i]['valueString'] = DateManager::getLocalDate($profile[$i]['value'] / 1000, 'H:i');
             }
         }
     }
     Theme::Set('table_cols', $cols);
     Theme::Set('table_rows', $profile);
     $formFields[] = FormManager::AddRaw(Theme::RenderReturn('table_render'));
     Theme::Set('form_fields_advanced', $formFields);
     // Two tabs
     $tabs = array();
     $tabs[] = FormManager::AddTab('general', __('General'));
     $tabs[] = FormManager::AddTab('location', __('Location'));
     $tabs[] = FormManager::AddTab('maintenance', __('Maintenance'));
     $tabs[] = FormManager::AddTab('wol', __('Wake on LAN'));
     $tabs[] = FormManager::AddTab('advanced', __('Advanced'));
     Theme::Set('form_tabs', $tabs);
     $response->SetFormRequestResponse(NULL, __('Edit a Display'), '650px', '350px');
     $response->AddButton(__('Help'), 'XiboHelpRender("' . HelpManager::Link('Display', 'Edit') . '")');
     $response->AddButton(__('Cancel'), 'XiboDialogClose()');
     $response->AddButton(__('Save'), '$("#DisplayEditForm").submit()');
     $response->Respond();
 }
Esempio n. 3
0
 private function getForecastData($displayId)
 {
     $defaultLat = Config::GetSetting('DEFAULT_LAT');
     $defaultLong = Config::GetSetting('DEFAULT_LONG');
     if ($this->GetOption('useDisplayLocation') == 1) {
         // Use the display ID or the default.
         if ($displayId != 0) {
             $display = new Display();
             $display->displayId = $displayId;
             $display->Load();
             $defaultLat = $display->latitude;
             $defaultLong = $display->longitude;
         }
     } else {
         $defaultLat = $this->GetOption('latitude', $defaultLat);
         $defaultLong = $this->GetOption('longitude', $defaultLong);
     }
     $apiKey = $this->GetSetting('apiKey');
     if ($apiKey == '') {
         die(__('Incorrectly configured module'));
     }
     // Query the API and Dump the Results.
     $forecast = new Forecast($apiKey);
     $apiOptions = array('units' => $this->GetOption('units', 'auto'), 'lang' => $this->GetOption('lang', 'en'), 'exclude' => 'flags,minutely,hourly');
     $key = md5($defaultLat . $defaultLong . 'null' . implode('.', $apiOptions));
     if (!Cache::has($key)) {
         Debug::LogEntry('audit', 'Getting Forecast from the API', $this->type, __FUNCTION__);
         if (!($data = $forecast->get($defaultLat, $defaultLong, null, $apiOptions))) {
             return false;
         }
         // If the response is empty, cache it for less time
         $cacheDuration = $this->GetSetting('cachePeriod');
         // Cache
         Cache::put($key, $data, $cacheDuration);
     } else {
         Debug::LogEntry('audit', 'Getting Forecast from the Cache with key: ' . $key, $this->type, __FUNCTION__);
         $data = Cache::get($key);
     }
     //Debug::Audit('Data: ' . var_export($data, true));
     // Icon Mappings
     $icons = array('unmapped' => 'wi-alien', 'clear-day' => 'wi-day-sunny', 'clear-night' => 'wi-night-clear', 'rain' => 'wi-rain', 'snow' => 'wi-snow', 'sleet' => 'wi-hail', 'wind' => 'wi-windy', 'fog' => 'wi-fog', 'cloudy' => 'wi-cloudy', 'partly-cloudy-day' => 'wi-day-cloudy', 'partly-cloudy-night' => 'wi-night-partly-cloudy');
     // Temperature Unit Mappings
     $temperatureUnit = '';
     foreach ($this->unitsAvailable() as $unit) {
         if ($unit['id'] == $this->GetOption('units', 'auto')) {
             $temperatureUnit = $unit['tempUnit'];
             break;
         }
     }
     // Are we set to only show daytime weather conditions?
     if ($this->GetOption('dayConditionsOnly') == 1) {
         if ($data->currently->icon == 'partly-cloudy-night') {
             $data->currently->icon = 'clear-day';
         }
     }
     $data->currently->wicon = isset($icons[$data->currently->icon]) ? $icons[$data->currently->icon] : $icons['unmapped'];
     $data->currently->temperatureFloor = isset($data->currently->temperature) ? floor($data->currently->temperature) : '--';
     $data->currently->summary = isset($data->currently->summary) ? $data->currently->summary : '--';
     $data->currently->weekSummary = isset($data->daily->summary) ? $data->daily->summary : '--';
     $data->currently->temperatureUnit = $temperatureUnit;
     // Convert a stdObject to an array
     $data = json_decode(json_encode($data), true);
     // Process the icon for each day
     for ($i = 0; $i < 7; $i++) {
         // Are we set to only show daytime weather conditions?
         if ($this->GetOption('dayConditionsOnly') == 1) {
             if ($data['daily']['data'][$i]['icon'] == 'partly-cloudy-night') {
                 $data['daily']['data'][$i]['icon'] = 'clear-day';
             }
         }
         $data['daily']['data'][$i]['wicon'] = isset($icons[$data['daily']['data'][$i]['icon']]) ? $icons[$data['daily']['data'][$i]['icon']] : $icons['unmapped'];
         $data['daily']['data'][$i]['temperatureMaxFloor'] = isset($data['daily']['data'][$i]['temperatureMax']) ? floor($data['daily']['data'][$i]['temperatureMax']) : '--';
         $data['daily']['data'][$i]['temperatureMinFloor'] = isset($data['daily']['data'][$i]['temperatureMin']) ? floor($data['daily']['data'][$i]['temperatureMin']) : '--';
         $data['daily']['data'][$i]['temperatureFloor'] = $data['daily']['data'][$i]['temperatureMinFloor'] != '--' && $data['daily']['data'][$i]['temperatureMaxFloor'] != '--' ? floor(($data['daily']['data'][$i]['temperatureMinFloor'] + $data['daily']['data'][$i]['temperatureMaxFloor']) / 2) : '--';
         $data['daily']['data'][$i]['temperatureUnit'] = $temperatureUnit;
     }
     return $data;
 }