Exemple #1
0
 /**
  * Returns a string containing the required files xml for the requesting display
  * @param string $serverKey The Server Key
  * @param string $hardwareKey Display Hardware Key
  * @return string $requiredXml Xml Formatted String
  * @throws SoapFault
  */
 function RequiredFiles($serverKey, $hardwareKey)
 {
     // Sanitize
     $serverKey = Kit::ValidateParam($serverKey, _STRING);
     $hardwareKey = Kit::ValidateParam($hardwareKey, _STRING);
     $rfLookAhead = Kit::ValidateParam(Config::GetSetting('REQUIRED_FILES_LOOKAHEAD'), _INT);
     // Check the serverKey matches
     if ($serverKey != Config::GetSetting('SERVER_KEY')) {
         throw new SoapFault('Sender', 'The Server key you entered does not match with the server key at this address');
     }
     // Make sure we are sticking to our bandwidth limit
     if (!$this->CheckBandwidth()) {
         throw new SoapFault('Receiver', 'Bandwidth Limit exceeded');
     }
     $libraryLocation = Config::GetSetting("LIBRARY_LOCATION");
     // auth this request...
     if (!$this->AuthDisplay($hardwareKey)) {
         throw new SoapFault('Sender', 'This display is not licensed.');
     }
     if ($this->isAuditing == 1) {
         Debug::Audit('hardwareKey = ' . $hardwareKey, $this->displayId);
     }
     // Remove all Nonces for this display
     $nonce = new Nonce();
     $nonce->RemoveAllXmdsNonce($this->displayId);
     // Build a new RF
     $requiredFilesXml = new DOMDocument("1.0");
     $fileElements = $requiredFilesXml->createElement("files");
     $requiredFilesXml->appendChild($fileElements);
     // Hour to hour time bands for the query
     // Start at the current hour
     $fromFilter = time();
     // Move forwards an hour and the rf look ahead
     $rfLookAhead = $fromFilter + 3600 + $rfLookAhead;
     // Dial both items back to the top of the hour
     $fromFilter = $fromFilter - $fromFilter % 3600;
     $toFilter = $rfLookAhead - $rfLookAhead % 3600;
     if ($this->isAuditing == 1) {
         Debug::Audit(sprintf('Required files date criteria. FromDT = %s. ToDt = %s', date('Y-m-d h:i:s', $fromFilter), date('Y-m-d h:i:s', $toFilter)), $this->displayId);
     }
     try {
         $dbh = PDOConnect::init();
         // Get a list of all layout ids in the schedule right now.
         $SQL = " SELECT DISTINCT layout.layoutID ";
         $SQL .= " FROM `campaign` ";
         $SQL .= "   INNER JOIN schedule ON schedule.CampaignID = campaign.CampaignID ";
         $SQL .= "   INNER JOIN schedule_detail ON schedule_detail.eventID = schedule.eventID ";
         $SQL .= "   INNER JOIN `lkcampaignlayout` ON lkcampaignlayout.CampaignID = campaign.CampaignID ";
         $SQL .= "   INNER JOIN `layout` ON lkcampaignlayout.LayoutID = layout.LayoutID ";
         $SQL .= "   INNER JOIN lkdisplaydg ON lkdisplaydg.DisplayGroupID = schedule_detail.DisplayGroupID ";
         $SQL .= " WHERE lkdisplaydg.DisplayID = :displayId ";
         $SQL .= " AND schedule_detail.FromDT < :fromdt AND schedule_detail.ToDT > :todt ";
         $SQL .= "   AND layout.retired = 0  ";
         $sth = $dbh->prepare($SQL);
         $sth->execute(array('displayId' => $this->displayId, 'fromdt' => $toFilter, 'todt' => $fromFilter));
         // Our layout list will always include the default layout
         $layouts = array();
         $layouts[] = $this->defaultLayoutId;
         // Build up the other layouts into an array
         foreach ($sth->fetchAll() as $row) {
             $layouts[] = Kit::ValidateParam($row['layoutID'], _INT);
         }
     } catch (Exception $e) {
         Debug::Error('Error getting layout listing. ' . $e->getMessage(), $this->displayId);
         return new SoapFault('Sender', 'Unable to get a list of layouts');
     }
     // Create a comma separated list to pass into the query which gets file nodes
     $layoutIdList = implode(',', $layouts);
     try {
         $dbh = PDOConnect::init();
         // Add file nodes to the $fileElements
         $SQL = "\n                    SELECT 1 AS DownloadOrder, 'media' AS RecordType, storedAs AS path, media.mediaID AS id, media.`MD5`, media.FileSize, NULL AS xml\n                       FROM `media`\n                     WHERE media.type = 'font'\n                        OR (media.type = 'module' AND media.moduleSystemFile = 1)\n                    UNION\n                    ";
         $SQL .= " SELECT 4 AS DownloadOrder, 'layout' AS RecordType, layout.layoutID AS path, layout.layoutID AS id, MD5(layout.xml) AS `MD5`, NULL AS FileSize, layout.xml AS xml ";
         $SQL .= "   FROM layout ";
         $SQL .= sprintf(" WHERE layout.layoutid IN (%s)  ", $layoutIdList);
         $SQL .= " UNION ";
         $SQL .= " SELECT 3 AS DownloadOrder, 'media' AS RecordType, storedAs AS path, media.mediaID AS id, media.`MD5`, media.FileSize, NULL AS xml ";
         $SQL .= "   FROM media ";
         $SQL .= "   INNER JOIN lklayoutmedia ";
         $SQL .= "   ON lklayoutmedia.MediaID = media.MediaID ";
         $SQL .= "   INNER JOIN layout ";
         $SQL .= "   ON layout.LayoutID = lklayoutmedia.LayoutID";
         $SQL .= sprintf(" WHERE layout.layoutid IN (%s)  ", $layoutIdList);
         $SQL .= "\n                    UNION\n                    SELECT 2 AS DownloadOrder, 'media' AS RecordType, storedAs AS path, media.mediaID AS id, media.`MD5`, media.FileSize, NULL AS xml\n                       FROM `media`\n                        INNER JOIN `lkmediadisplaygroup`\n                        ON lkmediadisplaygroup.mediaid = media.MediaID\n                        INNER JOIN lkdisplaydg\n                        ON lkdisplaydg.DisplayGroupID = lkmediadisplaygroup.DisplayGroupID\n                    ";
         $SQL .= " WHERE lkdisplaydg.DisplayID = :displayId ";
         $SQL .= " ORDER BY DownloadOrder, RecordType DESC";
         $sth = $dbh->prepare($SQL);
         $sth->execute(array('displayId' => $this->displayId));
         // Prepare a SQL statement in case we need to update the MD5 and FileSize on media nodes.
         $mediaSth = $dbh->prepare('UPDATE media SET `MD5` = :md5, FileSize = :size WHERE MediaID = :mediaid');
         // What is the send file mode?
         $sendFileMode = Config::GetSetting('SENDFILE_MODE');
         // Keep a list of path names added to RF to prevent duplicates
         $pathsAdded = array();
         foreach ($sth->fetchAll() as $row) {
             $recordType = Kit::ValidateParam($row['RecordType'], _WORD);
             $path = Kit::ValidateParam($row['path'], _STRING);
             $id = Kit::ValidateParam($row['id'], _STRING);
             $md5 = Kit::ValidateParam($row['MD5'], _HTMLSTRING);
             $fileSize = Kit::ValidateParam($row['FileSize'], _INT);
             $xml = Kit::ValidateParam($row['xml'], _HTMLSTRING);
             $mediaNonce = '';
             if ($recordType == 'layout') {
                 // Check we haven't added this before
                 if (in_array('layout_' . $id, $pathsAdded)) {
                     continue;
                 }
                 // For layouts the MD5 column is the layout xml
                 $fileSize = strlen($xml);
                 if ($this->isAuditing == 1) {
                     Debug::Audit('MD5 for layoutId ' . $id . ' is: [' . $md5 . ']', $this->displayId);
                 }
                 // Add nonce
                 $nonce->AddXmdsNonce('layout', $this->displayId, NULL, $fileSize, NULL, $id);
                 $pathsAdded[] = 'layout_' . $id;
             } else {
                 if ($recordType == 'media') {
                     // Check we haven't added this before
                     if (in_array('media_' . $path, $pathsAdded)) {
                         continue;
                     }
                     // If they are empty calculate them and save them back to the media.
                     if ($md5 == '' || $fileSize == 0) {
                         $md5 = md5_file($libraryLocation . $path);
                         $fileSize = filesize($libraryLocation . $path);
                         // Update the media record with this information
                         $mediaSth->execute(array('md5' => $md5, 'size' => $fileSize, 'mediaid' => $id));
                     }
                     // Add nonce
                     $mediaNonce = $nonce->AddXmdsNonce('file', $this->displayId, $id, $fileSize, $path);
                     $pathsAdded[] = 'media_' . $path;
                 } else {
                     continue;
                 }
             }
             // Add the file node
             $file = $requiredFilesXml->createElement("file");
             $file->setAttribute("type", $recordType);
             $file->setAttribute("id", $id);
             $file->setAttribute("size", $fileSize);
             $file->setAttribute("md5", $md5);
             if ($recordType == 'media' && $sendFileMode != 'Off') {
                 // Serve a link instead (standard HTTP link)
                 $file->setAttribute("path", Kit::GetXiboRoot() . '?file=' . $mediaNonce);
                 $file->setAttribute("saveAs", $path);
                 $file->setAttribute("download", 'http');
             } else {
                 $file->setAttribute("download", 'xmds');
                 $file->setAttribute("path", $path);
             }
             $fileElements->appendChild($file);
         }
     } catch (Exception $e) {
         Debug::Error('Unable to get a list of required files. ' . $e->getMessage(), $this->displayId);
         return new SoapFault('Sender', 'Unable to get a list of files');
     }
     // Go through each layout and see if we need to supply any resource nodes.
     foreach ($layouts as $layoutId) {
         // Load the layout XML and work out if we have any ticker / text / data set media items
         $layout = new Layout();
         $layoutInformation = $layout->LayoutInformation($layoutId);
         foreach ($layoutInformation['regions'] as $region) {
             foreach ($region['media'] as $media) {
                 if ($media['render'] == 'html' || $media['mediatype'] == 'ticker' || $media['mediatype'] == 'text' || $media['mediatype'] == 'datasetview' || $media['mediatype'] == 'webpage' || $media['mediatype'] == 'embedded') {
                     // Append this item to required files
                     $file = $requiredFilesXml->createElement("file");
                     $file->setAttribute('type', 'resource');
                     $file->setAttribute('id', rand());
                     $file->setAttribute('layoutid', $layoutId);
                     $file->setAttribute('regionid', $region['regionid']);
                     $file->setAttribute('mediaid', $media['mediaid']);
                     $file->setAttribute('updated', isset($media['updated']) ? $media['updated'] : 0);
                     $fileElements->appendChild($file);
                     $nonce->AddXmdsNonce('resource', $this->displayId, NULL, NULL, NULL, $layoutId, $region['regionid'], $media['mediaid']);
                 }
             }
         }
     }
     // Add a blacklist node
     $blackList = $requiredFilesXml->createElement("file");
     $blackList->setAttribute("type", "blacklist");
     $fileElements->appendChild($blackList);
     try {
         $dbh = PDOConnect::init();
         $sth = $dbh->prepare('SELECT MediaID FROM blacklist WHERE DisplayID = :displayid AND isIgnored = 0');
         $sth->execute(array('displayid' => $this->displayId));
         // Add a black list element for each file
         foreach ($sth->fetchAll() as $row) {
             $file = $requiredFilesXml->createElement("file");
             $file->setAttribute("id", $row['MediaID']);
             $blackList->appendChild($file);
         }
     } catch (Exception $e) {
         Debug::Error('Unable to get a list of blacklisted files. ' . $e->getMessage(), $this->displayId);
         return new SoapFault('Sender', 'Unable to get a list of blacklisted files');
     }
     // Phone Home?
     $this->PhoneHome();
     if ($this->isAuditing == 1) {
         Debug::Audit($requiredFilesXml->saveXML(), $this->displayId);
     }
     // Return the results of requiredFiles()
     $requiredFilesXml->formatOutput = true;
     $output = $requiredFilesXml->saveXML();
     // Log Bandwidth
     $this->LogBandwidth($this->displayId, Bandwidth::$RF, strlen($output));
     return $output;
 }