Example #1
0
 /**
  * Media File Upload
  * Upload a media file in parts
  * @return <XiboAPIResponse>
  */
 public function LibraryMediaFileUpload()
 {
     // Does this user have permission to call this webservice method?
     if (!$this->user->PageAuth('content')) {
         return $this->Error(1, 'Access Denied');
     }
     Kit::ClassLoader('file');
     $file = new File();
     $fileId = $this->GetParam('fileId', _INT);
     $checkSum = $this->GetParam('checksum', _STRING);
     $payload = $this->GetParam('payload', _STRING);
     $payloadMd5 = md5($payload);
     // Checksum the payload
     if ($payloadMd5 != $checkSum) {
         // Debug::LogEntry('audit', 'Sent Checksum: ' . $checkSum, 'RestXml', 'LibraryMediaFileUpload');
         // Debug::LogEntry('audit', 'Calculated Checksum: ' . $payloadMd5, 'RestXml', 'LibraryMediaFileUpload');
         // Debug::LogEntry('audit', 'Payload: ' . $payload, 'RestXml', 'LibraryMediaFileUpload');
         return $this->Error(2);
     }
     // Payload will be encoded in base64. Need to decode before handing to File class
     $payload = base64_decode($payload);
     if ($fileId == 0) {
         // New upload. All users have permissions to upload files if they have gotten this far
         if (!($fileId = $file->NewFile($payload, $this->user->userid))) {
             return $this->Error($file->GetErrorNumber());
         }
     } else {
         // Check permissions
         if (!$this->user->FileAuth($fileId)) {
             return $this->Error(1, 'Access Denied');
         }
         // Continue upload
         if (!$file->Append($fileId, $payload)) {
             return $this->Error($file->GetErrorNumber());
         }
     }
     // Current offset
     $size = $file->Size($fileId);
     // Return the fileId
     return $this->Respond($this->ReturnAttributes('file', array('id' => $fileId, 'offset' => $size)));
 }
Example #2
0
 function Import($zipFile, $layout, $userId, $template, $replaceExisting, $importTags, $delete = true)
 {
     // I think I might add a layout and then import
     if (!file_exists($zipFile)) {
         return $this->SetError(__('File does not exist'));
     }
     // Open the Zip file
     $zip = new ZipArchive();
     if (!$zip->open($zipFile)) {
         return $this->SetError(__('Unable to open ZIP'));
     }
     try {
         $dbh = PDOConnect::init();
         $sth = $dbh->prepare('SELECT mediaid, storedAs FROM `media` WHERE name = :name AND IsEdited = 0');
         // Get the layout details
         $layoutDetails = json_decode($zip->getFromName('layout.json'), true);
         // Set the layout name
         $layout = $layout != '' ? $layout : $layoutDetails['layout'];
         $description = isset($layoutDetails['description']) ? $layoutDetails['description'] : '';
         // Get the layout xml
         $xml = $zip->getFromName('layout.xml');
         // Add the layout
         if (!($layoutId = $this->Add($layout, $description, NULL, $userId, NULL, NULL, $xml))) {
             return false;
         }
         // Either remove out the tags, or add them to the DB
         if ($importTags) {
             // Pull the tags out of the XML
             $xmlDoc = new DOMDocument();
             $xmlDoc->loadXML($xml);
             $xpath = new DOMXPath($xmlDoc);
             $tagsNode = $xpath->query("//tags");
             foreach ($tagsNode as $tag) {
                 $this->tag($tag->nodeValue, $layoutId);
             }
         } else {
             $this->EditTags($layoutId, array());
         }
         // Are we a template?
         if ($template) {
             $this->tag('template', $layoutId);
         }
         // Tag as imported
         $this->tag('imported', $layoutId);
         // Set the DOM XML
         $this->SetDomXml($layoutId);
         // Set the user on each region
         foreach ($this->DomXml->getElementsByTagName('region') as $region) {
             $region->setAttribute('userId', $userId);
         }
         // Set the user on each media node
         foreach ($this->DomXml->getElementsByTagName('media') as $media) {
             $media->setAttribute('userId', $userId);
         }
         // We will need a file object and a media object
         $fileObject = new File();
         $mediaObject = new Media();
         $currentType = '';
         // Go through each region and add the media (updating the media ids)
         $mappings = json_decode($zip->getFromName('mapping.json'), true);
         foreach ($mappings as $file) {
             Debug::LogEntry('audit', 'Found file ' . json_encode($file));
             // Do we need to recharge our media object
             if ($currentType != '' && $file['type'] != $currentType) {
                 $mediaObject = new Media();
             }
             // Set the current type
             $currentType = $file['type'];
             // Does a media item with this name already exist?
             $sth->execute(array('name' => $file['name']));
             $rows = $sth->fetchAll();
             if (count($rows) > 0) {
                 if ($replaceExisting) {
                     // Alter the name of the file and add it
                     $file['name'] = 'import_' . $layout . '_' . uniqid();
                     // Add the file
                     if (!($fileId = $fileObject->NewFile($zip->getFromName('library/' . $file['file']), $userId))) {
                         return $this->SetError(__('Unable to add a media item'));
                     }
                     // Add this media to the library
                     if (!($mediaId = $mediaObject->Add($fileId, $file['type'], $file['name'], $file['duration'], $file['file'], $userId))) {
                         return $this->SetError($mediaObject->GetErrorMessage());
                     }
                     // Tag it
                     $mediaObject->tag('imported', $mediaId);
                 } else {
                     // Don't add the file, use the one that already exists
                     $mediaObject->mediaId = $rows[0]['mediaid'];
                     $mediaObject->storedAs = $rows[0]['storedAs'];
                 }
             } else {
                 // Add the file
                 if (!($fileId = $fileObject->NewFile($zip->getFromName('library/' . $file['file']), $userId))) {
                     return $this->SetError(__('Unable to add a media item'));
                 }
                 // Add this media to the library
                 if (!($mediaId = $mediaObject->Add($fileId, $file['type'], $file['name'], $file['duration'], $file['file'], $userId))) {
                     return $this->SetError($mediaObject->GetErrorMessage());
                 }
                 // Tag it
                 $mediaObject->tag('imported', $mediaId);
             }
             Debug::LogEntry('audit', 'Post File Import Fix', get_class(), __FUNCTION__);
             // Get this media node from the layout using the old media id
             if (!$this->PostImportFix($userId, $layoutId, $file['mediaid'], $mediaObject->mediaId, $mediaObject->storedAs, $file['background'])) {
                 return false;
             }
         }
         Debug::LogEntry('audit', 'Saving XLF', get_class(), __FUNCTION__);
         // Save the updated XLF
         if (!$this->SetLayoutXml($layoutId, $this->DomXml->saveXML())) {
             return false;
         }
         $this->SetValid($layoutId);
         // Finished, so delete
         $zip->close();
         if ($delete) {
             @unlink($zipFile);
         }
         return true;
     } catch (Exception $e) {
         Debug::LogEntry('error', $e->getMessage());
         if (!$this->IsError()) {
             $this->SetError(1, __('Unknown Error'));
         }
         return false;
     }
 }