Пример #1
0
 public function deliverContent(Request $request, \record_adapter $record, $subdef, $watermark, $stamp)
 {
     $file = $record->get_subdef($subdef);
     $pathOut = $file->get_pathfile();
     if ($watermark === true && $file->get_type() === \media_subdef::TYPE_IMAGE) {
         $pathOut = \recordutils_image::watermark($this->app, $file);
     } elseif ($stamp === true && $file->get_type() === \media_subdef::TYPE_IMAGE) {
         $pathOut = \recordutils_image::stamp($this->app, $file);
     }
     $disposition = $request->query->get('download') ? DeliverDataInterface::DISPOSITION_ATTACHMENT : DeliverDataInterface::DISPOSITION_INLINE;
     /** @var Response $response */
     $response = $this->deliverFile($pathOut, $file->get_file(), $disposition, $file->get_mime());
     if (in_array($subdef, array('document', 'preview'))) {
         $response->setPrivate();
         $this->logView($record, $request);
     } elseif ($subdef !== 'thumbnail') {
         try {
             if ($file->getDataboxSubdef()->get_class() != \databox_subdef::CLASS_THUMBNAIL) {
                 $response->setPrivate();
                 $this->logView($record, $request);
             }
         } catch (\Exception $e) {
             // Ignore exception
         }
     }
     $response->isNotModified($request);
     return $response;
 }
Пример #2
0
 public function testStampWithFile()
 {
     $this->addStampConf(self::$DI['record_1']->get_collection());
     self::$DI['app']->getApplicationBox()->write_collection_pic(self::$DI['app']['media-alchemyst'], self::$DI['app']['filesystem'], self::$DI['record_1']->get_collection(), new SymfoFile(__DIR__ . '/../../files/logocoll.gif'), \collection::PIC_STAMP);
     $path = recordutils_image::stamp(self::$DI['app'], self::$DI['record_1']->get_subdef('preview'));
     $this->assertTrue(0 === strpos(basename($path), 'stamp_'));
     unlink($path);
 }
Пример #3
0
 public function deliverContent(Request $request, \record_adapter $record, $subdef, $watermark, $stamp, Application $app)
 {
     $file = $record->get_subdef($subdef);
     $pathOut = $file->get_pathfile();
     if ($watermark === true && $file->get_type() === \media_subdef::TYPE_IMAGE) {
         $pathOut = \recordutils_image::watermark($app, $file);
     } elseif ($stamp === true && $file->get_type() === \media_subdef::TYPE_IMAGE) {
         $pathOut = \recordutils_image::stamp($app, $file);
     }
     $log_id = null;
     try {
         $logger = $app['phraseanet.logger']($record->get_databox());
         $log_id = $logger->get_id();
         $referrer = 'NO REFERRER';
         if (isset($_SERVER['HTTP_REFERER'])) {
             $referrer = $_SERVER['HTTP_REFERER'];
         }
         $record->log_view($log_id, $referrer, $app['conf']->get(['main', 'key']));
     } catch (\Exception $e) {
     }
     $disposition = $request->query->get('download') ? DeliverDataInterface::DISPOSITION_ATTACHMENT : DeliverDataInterface::DISPOSITION_INLINE;
     $response = $app['phraseanet.file-serve']->deliverFile($pathOut, $file->get_file(), $disposition, $file->get_mime());
     $response->setPrivate();
     /* @var $response \Symfony\Component\HttpFoundation\Response */
     if ($file->getEtag()) {
         $response->setEtag($file->getEtag());
         $response->setLastModified($file->get_modification_date());
     }
     if (false === $record->is_grouping() && $subdef !== 'document') {
         try {
             if ($file->getDataboxSubdef()->get_class() == \databox_subdef::CLASS_THUMBNAIL) {
                 // default expiration is 5 days
                 $expiration = 60 * 60 * 24 * 5;
                 $response->setExpires(new \DateTime(sprintf('+%d seconds', $expiration)));
                 $response->setMaxAge($expiration);
                 $response->setSharedMaxAge($expiration);
                 $response->setPublic();
             }
         } catch (\Exception $e) {
         }
     }
     $response->isNotModified($request);
     return $response;
 }
Пример #4
0
 protected function print_preview($withtdm, $write_caption)
 {
     if ($withtdm === true) {
         $this->print_thumbnailGrid($this->pdf, $this->records, true);
     }
     foreach ($this->records as $krec => $rec) {
         /* @var $rec record_adapter */
         $this->pdf->AddPage();
         if ($withtdm === "CALCPAGES") {
             $rec->setNumber($this->pdf->PageNo());
         }
         $lmargin = $this->pdf->GetX();
         $himg = 0;
         $y = 0;
         $miniConv = NULL;
         $LEFT__TEXT = "";
         $LEFT__IMG = NULL;
         $RIGHT_TEXT = "";
         $RIGHT_IMG = NULL;
         $LEFT__IMG = $this->app['root.path'] . "/config/minilogos/logopdf_" . $rec->get_sbas_id() . ".jpg";
         if (!is_file($LEFT__IMG)) {
             $databox = $rec->get_databox();
             $str = $databox->get_sxml_structure();
             $vn = (string) $str->pdfPrintLogo;
             if ($vn * 1 == 1) {
                 $LEFT__TEXT = $databox->get_label($this->app['locale']);
             }
         }
         $collection = \collection::get_from_base_id($this->app, $rec->get_base_id());
         $vn = "";
         if (false !== ($str = simplexml_load_string($collection->get_prefs()))) {
             $vn = (string) $str->pdfPrintappear;
         }
         if ($vn == "" || $vn == "1") {
             $RIGHT_TEXT = \phrasea::bas_labels($rec->get_base_id(), $this->app);
         } elseif ($vn == "2") {
             $RIGHT_IMG = $this->app['root.path'] . "/config/minilogos/" . $rec->get_base_id();
         }
         $xtmp = $this->pdf->GetX();
         $ytmp = $this->pdf->GetY();
         $this->pdf->SetFont(PhraseaPDF::FONT, '', 12);
         $this->pdf->SetFillColor(220, 220, 220);
         $y = $this->pdf->GetY();
         $this->pdf->MultiCell(95, 7, $LEFT__TEXT, "LTB", "L", 1);
         $y2 = $this->pdf->GetY();
         $h = $y2 - $y;
         $this->pdf->SetY($y);
         $this->pdf->SetX(105);
         $this->pdf->Cell(95, $h, $RIGHT_TEXT, "TBR", 1, "R", 1);
         if ($LEFT__TEXT == "" && is_file($LEFT__IMG)) {
             if ($size = @getimagesize($LEFT__IMG)) {
                 $wmm = (int) $size[0] * 25.4 / 72;
                 $hmm = (int) $size[1] * 25.4 / 72;
                 if ($hmm > 6) {
                     $coeff = $hmm / 6;
                     $wmm = (int) $wmm / $coeff;
                     $hmm = (int) $hmm / $coeff;
                 }
                 $this->pdf->Image($LEFT__IMG, $xtmp + 0.5, $ytmp + 0.5, $wmm, $hmm);
             }
         }
         if ($RIGHT_IMG != NULL && is_file($RIGHT_IMG)) {
             if ($size = @getimagesize($RIGHT_IMG)) {
                 if ($size[2] == '1') {
                     if (!isset($miniConv[$RIGHT_IMG])) {
                         $tmp_filename = tempnam('minilogos/', 'gif4fpdf');
                         $img = imagecreatefromgif($RIGHT_IMG);
                         imageinterlace($img, 0);
                         imagepng($img, $tmp_filename);
                         rename($tmp_filename, $tmp_filename . '.png');
                         $miniConv[$RIGHT_IMG] = $tmp_filename . '.png';
                         $RIGHT_IMG = $tmp_filename . '.png';
                     } else {
                         $RIGHT_IMG = $miniConv[$RIGHT_IMG];
                     }
                     $wmm = (int) $size[0] * 25.4 / 72;
                     $hmm = (int) $size[1] * 25.4 / 72;
                     if ($hmm > 6) {
                         $coeff = $hmm / 6;
                         $wmm = (int) $wmm / $coeff;
                         $hmm = (int) $hmm / $coeff;
                     }
                     $tt = 0;
                     if ($hmm < 6) {
                         $tt = (6 - $hmm) / 2;
                     }
                     $this->pdf->Image($RIGHT_IMG, 200 - 0.5 - $wmm, $ytmp + 0.5 + $tt);
                 } else {
                     $wmm = (int) $size[0] * 25.4 / 72;
                     $hmm = (int) $size[1] * 25.4 / 72;
                     if ($hmm > 6) {
                         $coeff = $hmm / 6;
                         $wmm = (int) $wmm / $coeff;
                         $hmm = (int) $hmm / $coeff;
                     }
                     $this->pdf->Image($RIGHT_IMG, 200 - 0.5 - $wmm, $ytmp + 0.5);
                 }
             }
         }
         $y = $this->pdf->GetY() + 5;
         $subdef = $rec->get_subdef('preview');
         if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
             $subdef = $rec->get_thumbnail();
         }
         $f = $subdef->get_pathfile();
         if (!$this->app->getAclForUser($this->app->getAuthenticatedUser())->has_right_on_base($rec->get_base_id(), "nowatermark") && $subdef->get_type() == \media_subdef::TYPE_IMAGE) {
             $f = \recordutils_image::watermark($this->app, $subdef);
         }
         // original height / original width x new width = new height
         $wimg = $himg = 150;
         // preview dans un carre de 150 mm
         // 1px = 3.77952 mm
         $finalWidth = round($subdef->get_width() / 3.779528, 2);
         $finalHeight = round($subdef->get_height() / 3.779528, 2);
         $aspectH = $finalWidth / $finalHeight;
         $aspectW = $finalHeight / $finalWidth;
         if ($finalWidth > 0 && $finalHeight > 0) {
             if ($finalWidth > $finalHeight && $finalWidth > $wimg) {
                 $finalWidth = $wimg;
                 $finalHeight = $wimg * $aspectW;
             } else {
                 if ($finalHeight > $finalWidth && $finalHeight > $himg) {
                     $finalHeight = $himg;
                     $finalWidth = $himg * $aspectH;
                 } else {
                     if ($finalHeight == $finalWidth & $finalWidth > $wimg) {
                         $finalHeight = $wimg;
                         $finalWidth = $himg;
                     }
                 }
             }
         }
         $this->pdf->Image($f, (210 - $finalWidth) / 2, $y, $finalWidth, $finalHeight);
         if ($miniConv != NULL) {
             foreach ($miniConv as $oneF) {
                 unlink($oneF);
             }
         }
         $this->pdf->SetXY($lmargin, $y += $finalHeight + 5);
         $nf = 0;
         if ($write_caption) {
             foreach ($rec->get_caption()->get_fields() as $field) {
                 /* @var $field caption_field */
                 if ($nf > 0) {
                     $this->pdf->Write(6, "\n");
                 }
                 $this->pdf->SetFont(PhraseaPDF::FONT, 'B', 12);
                 $this->pdf->Write(5, $field->get_name() . " : ");
                 $this->pdf->SetFont(PhraseaPDF::FONT, '', 12);
                 $t = str_replace(["&lt;", "&gt;", "&amp;"], ["<", ">", "&"], strip_tags($field->get_serialized_values()));
                 $this->pdf->Write(5, $t);
                 $nf++;
             }
         }
     }
     return;
 }
Пример #5
0
 /**
  *
  * @param User       $user
  * @param Filesystem $filesystem
  * @param Array      $subdefs
  * @param boolean    $rename_title
  * @param boolean    $includeBusinessFields
  *
  * @return Array
  */
 public function prepare_export(User $user, Filesystem $filesystem, array $subdefs, $rename_title, $includeBusinessFields)
 {
     if (!is_array($subdefs)) {
         throw new Exception('No subdefs given');
     }
     $includeBusinessFields = !!$includeBusinessFields;
     $files = [];
     $n_files = 0;
     $file_names = [];
     $size = 0;
     $unicode = new \unicode();
     foreach ($this->elements as $download_element) {
         $id = count($files);
         $files[$id] = ['base_id' => $download_element->get_base_id(), 'record_id' => $download_element->get_record_id(), 'original_name' => '', 'export_name' => '', 'subdefs' => []];
         $BF = false;
         if ($includeBusinessFields && $this->app['acl']->get($user)->has_right_on_base($download_element->get_base_id(), 'canmodifrecord')) {
             $BF = true;
         }
         $desc = $this->app['serializer.caption']->serialize($download_element->get_caption(), CaptionSerializer::SERIALIZE_XML, $BF);
         $files[$id]['original_name'] = $files[$id]['export_name'] = $download_element->get_original_name(true);
         $files[$id]['original_name'] = trim($files[$id]['original_name']) != '' ? $files[$id]['original_name'] : $id;
         $infos = pathinfo($files[$id]['original_name']);
         $extension = isset($infos['extension']) ? $infos['extension'] : '';
         if ($rename_title) {
             $title = strip_tags($download_element->get_title(null, null, true));
             $files[$id]['export_name'] = $unicode->remove_nonazAZ09($title, true, true, true);
         } else {
             $files[$id]["export_name"] = $infos['filename'];
         }
         $sizeMaxAjout = 0;
         $sizeMaxExt = 0;
         $sd = $download_element->get_subdefs();
         foreach ($download_element->get_downloadable() as $name => $properties) {
             if ($properties === false || !in_array($name, $subdefs)) {
                 continue;
             }
             if (!in_array($name, ['caption', 'caption-yaml']) && !isset($sd[$name])) {
                 continue;
             }
             set_time_limit(100);
             $subdef_export = $subdef_alive = false;
             $n_files++;
             $tmp_pathfile = ['path' => null, 'file' => null];
             switch ($properties['class']) {
                 case 'caption':
                 case 'caption-yaml':
                     $subdef_export = true;
                     $subdef_alive = true;
                     break;
                 case 'thumbnail':
                     $tmp_pathfile = ['path' => $sd[$name]->get_path(), 'file' => $sd[$name]->get_file()];
                     $subdef_export = true;
                     $subdef_alive = true;
                     break;
                 case 'document':
                     $subdef_export = true;
                     $path = \recordutils_image::stamp($this->app, $sd[$name]);
                     $tmp_pathfile = ['path' => $sd[$name]->get_path(), 'file' => $sd[$name]->get_file()];
                     if (file_exists($path)) {
                         $tmp_pathfile = ['path' => dirname($path), 'file' => basename($path)];
                         $subdef_alive = true;
                     }
                     break;
                 case 'preview':
                     $subdef_export = true;
                     $tmp_pathfile = ['path' => $sd[$name]->get_path(), 'file' => $sd[$name]->get_file()];
                     if (!$this->app['acl']->get($user)->has_right_on_base($download_element->get_base_id(), "nowatermark") && !$this->app['acl']->get($user)->has_preview_grant($download_element) && $sd[$name]->get_type() == media_subdef::TYPE_IMAGE) {
                         $path = recordutils_image::watermark($this->app, $sd[$name]);
                         if (file_exists($path)) {
                             $tmp_pathfile = ['path' => dirname($path), 'file' => basename($path)];
                             $subdef_alive = true;
                         }
                     } else {
                         $subdef_alive = true;
                     }
                     break;
             }
             if ($subdef_export === true && $subdef_alive === true) {
                 switch ($properties['class']) {
                     case 'caption':
                         if ($name == 'caption-yaml') {
                             $suffix = '_captionyaml';
                             $extension = 'yml';
                             $mime = 'text/x-yaml';
                         } else {
                             $suffix = '_caption';
                             $extension = 'xml';
                             $mime = 'text/xml';
                         }
                         $files[$id]["subdefs"][$name]["ajout"] = $suffix;
                         $files[$id]["subdefs"][$name]["exportExt"] = $extension;
                         $files[$id]["subdefs"][$name]["label"] = $properties['label'];
                         $files[$id]["subdefs"][$name]["path"] = null;
                         $files[$id]["subdefs"][$name]["file"] = null;
                         $files[$id]["subdefs"][$name]["size"] = 0;
                         $files[$id]["subdefs"][$name]["folder"] = $download_element->get_directory();
                         $files[$id]["subdefs"][$name]["mime"] = $mime;
                         break;
                     case 'document':
                     case 'preview':
                     case 'thumbnail':
                         $infos = pathinfo(p4string::addEndSlash($tmp_pathfile["path"]) . $tmp_pathfile["file"]);
                         $files[$id]["subdefs"][$name]["ajout"] = $name == 'document' ? '' : "_" . $name;
                         $files[$id]["subdefs"][$name]["path"] = $tmp_pathfile["path"];
                         $files[$id]["subdefs"][$name]["file"] = $tmp_pathfile["file"];
                         $files[$id]["subdefs"][$name]["label"] = $properties['label'];
                         $files[$id]["subdefs"][$name]["size"] = $sd[$name]->get_size();
                         $files[$id]["subdefs"][$name]["mime"] = $sd[$name]->get_mime();
                         $files[$id]["subdefs"][$name]["folder"] = $download_element->get_directory();
                         $files[$id]["subdefs"][$name]["exportExt"] = isset($infos['extension']) ? $infos['extension'] : '';
                         $size += $sd[$name]->get_size();
                         break;
                 }
                 $longueurAjoutCourant = mb_strlen($files[$id]["subdefs"][$name]["ajout"]);
                 $sizeMaxAjout = max($longueurAjoutCourant, $sizeMaxAjout);
                 $longueurExtCourant = mb_strlen($files[$id]["subdefs"][$name]["exportExt"]);
                 $sizeMaxExt = max($longueurExtCourant, $sizeMaxExt);
             }
         }
         $max_length = 31 - $sizeMaxExt - $sizeMaxAjout;
         $name = $files[$id]["export_name"];
         $start_length = mb_strlen($name);
         if ($start_length > $max_length) {
             $name = mb_substr($name, 0, $max_length);
         }
         $n = 1;
         while (in_array(mb_strtolower($name), $file_names)) {
             $n++;
             $suffix = "-" . $n;
             // pour diese si besoin
             $max_length = 31 - $sizeMaxExt - $sizeMaxAjout - mb_strlen($suffix);
             $name = mb_strtolower($files[$id]["export_name"]);
             if ($start_length > $max_length) {
                 $name = mb_substr($name, 0, $max_length) . $suffix;
             } else {
                 $name = $name . $suffix;
             }
         }
         $file_names[] = mb_strtolower($name);
         $files[$id]["export_name"] = $name;
         $files[$id]["export_name"] = $unicode->remove_nonazAZ09($files[$id]["export_name"], true, true, true);
         $files[$id]["original_name"] = $unicode->remove_nonazAZ09($files[$id]["original_name"], true, true, true);
         $i = 0;
         $name = utf8_decode($files[$id]["export_name"]);
         $tmp_name = "";
         $good_keys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_', '.', '#'];
         while (isset($name[$i])) {
             if (!in_array(mb_strtolower($name[$i]), $good_keys)) {
                 $tmp_name .= '_';
             } else {
                 $tmp_name .= $name[$i];
             }
             $tmp_name = str_replace('__', '_', $tmp_name);
             $i++;
         }
         $files[$id]["export_name"] = $tmp_name;
         if (in_array('caption', $subdefs)) {
             $caption_dir = $this->app['root.path'] . '/tmp/desc_tmp/' . time() . $this->app['authentication']->getUser()->getId() . '/';
             $filesystem->mkdir($caption_dir, 0750);
             $desc = $this->app['serializer.caption']->serialize($download_element->get_caption(), CaptionSerializer::SERIALIZE_XML, $BF);
             $file = $files[$id]["export_name"] . $files[$id]["subdefs"]['caption']["ajout"] . '.' . $files[$id]["subdefs"]['caption']["exportExt"];
             $path = $caption_dir;
             file_put_contents($path . $file, $desc);
             $files[$id]["subdefs"]['caption']["path"] = $path;
             $files[$id]["subdefs"]['caption']["file"] = $file;
             $files[$id]["subdefs"]['caption']["size"] = filesize($path . $file);
             $files[$id]["subdefs"]['caption']['businessfields'] = $BF ? '1' : '0';
         }
         if (in_array('caption-yaml', $subdefs)) {
             $caption_dir = $this->app['root.path'] . '/tmp/desc_tmp/' . time() . $this->app['authentication']->getUser()->getId() . '/';
             $filesystem->mkdir($caption_dir, 0750);
             $desc = $this->app['serializer.caption']->serialize($download_element->get_caption(), CaptionSerializer::SERIALIZE_YAML, $BF);
             $file = $files[$id]["export_name"] . $files[$id]["subdefs"]['caption-yaml']["ajout"] . '.' . $files[$id]["subdefs"]['caption-yaml']["exportExt"];
             $path = $caption_dir;
             file_put_contents($path . $file, $desc);
             $files[$id]["subdefs"]['caption-yaml']["path"] = $path;
             $files[$id]["subdefs"]['caption-yaml']["file"] = $file;
             $files[$id]["subdefs"]['caption-yaml']["size"] = filesize($path . $file);
             $files[$id]["subdefs"]['caption-yaml']['businessfields'] = $BF ? '1' : '0';
         }
     }
     $this->list = ['files' => $files, 'names' => $file_names, 'size' => $size, 'count' => $n_files];
     return $this->list;
 }