Ejemplo n.º 1
0
 /**
  * Adds an image or PDF (todo: should scan file extensions properly & import word docs etc).
  * @param mixed $src
  * @param mixed $returnType
  * @param mixed $tag
  * @return mixed
  */
 protected function findOrAddMediaItem($src, $returnType = 'id', $tag = true)
 {
     $mediaId = null;
     $slug = null;
     $info = pathinfo($src);
     $path = $info['dirname'] . '/' . $info['filename'];
     $dirname = $info['dirname'];
     // Move any query string or hash string into the filename and out of the "extension"
     if (isset($info['extension'])) {
         $qat = strpos($info['extension'], '?');
         if ($qat !== false) {
             $path .= substr($info['extension'], $qat);
             $info['extension'] = substr($info['extension'], 0, $qat);
         }
         $hashat = strpos($info['extension'], '#');
         if ($hashat !== false) {
             $path .= substr($info['extension'], $hashat);
             $info['extension'] = substr($info['extension'], 0, $hashat);
         }
         // Extension should be a clean Unix path component
         $info['extension'] = preg_replace('/[^\\w]/', '', $info['extension']);
     }
     // Remove any hostname before splitting for tags, also dump case differences
     $dirname = strtolower(preg_replace('|^\\w+://.*?/|', '', $dirname));
     $tags = preg_split('#/#', $dirname);
     $newTags = array();
     foreach ($tags as $tag) {
         if (strlen($tag) > 1) {
             $newTags[] = $tag;
         }
     }
     $tags = $newTags;
     $extension = isset($info['extension']) ? $info['extension'] : 'unknown';
     $slug = aTools::slugify($path . "-{$extension}");
     // We need to encode spaces but not slashes...
     $src = str_replace(' ', '%20', $src);
     if (substr($src, 0, 5) !== 'http:') {
         $src = $this->imagesDir . '/' . $src;
     }
     $result = $this->sql->query('SELECT id FROM a_media_item WHERE slug = :slug', array('slug' => $slug));
     if (isset($result[0]['id'])) {
         $mediaId = $result[0]['id'];
     } else {
         $mediaItem = new aMediaItem();
         $mediaItem->setTitle($slug);
         $mediaItem->setSlug($slug);
         if ($extension === 'pdf') {
             $mediaItem->setType('pdf');
         } else {
             $mediaItem->setType('image');
         }
         $filename = $mediaItem->getOriginalPath($extension);
         if (file_exists($filename)) {
             // Avoids costly double imports of media
             $mediaItem->preSaveFile($filename);
         } else {
             $bad = isset($this->failedMedia[$src]);
             if (!$bad) {
                 $tmpFile = aFiles::getTemporaryFilename();
                 try {
                     if (!copy($src, $tmpFile)) {
                         throw new sfException(sprintf('Could not copy file: %s', $src));
                     }
                     if (!$mediaItem->saveFile($tmpFile)) {
                         throw new sfException(sprintf('Could not save file: %s', $src));
                     }
                 } catch (Exception $e) {
                     $this->failedMedia[$src] = true;
                 }
                 if (file_exists($tmpFile)) {
                     unlink($tmpFile);
                 }
             }
         }
         if (!isset($this->failedMedia[$src])) {
             $this->sql->fastSaveMediaItem($mediaItem);
             if ($tag) {
                 $this->sql->fastSaveTags('aMediaItem', $mediaItem->id, $tags);
             }
             $mediaId = $mediaItem->id;
             // getOriginalPath needs a context, ugh
             $path = '/uploads/media_items/' . $mediaItem->slug . '.original.pdf';
             $mediaItem->free(true);
         }
     }
     if ($returnType === 'path') {
         return $path;
     } else {
         return $mediaId;
     }
     return false;
 }