Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
exportfile.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Export Vault Files
9  *
10  * @author Anakeen 2000
11  * @version $Id: exportfile.php,v 1.21 2008/05/20 15:26:48 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 
19 include_once ("FDL/Class.Doc.php");
20 include_once ("FDL/Class.DocAttr.php");
21 include_once ("FDL/Lib.Vault.php");
22 include_once ("VAULT/Class.VaultFile.php");
23 
24 define("RESIZEDIR", DEFAULT_PUBDIR . "/.img-resize/");
25 // --------------------------------------------------------------------
26 function exportfile(&$action)
27 {
28  $dbaccess = $action->GetParam("FREEDOM_DB");
29  $docid = GetHttpVars("docid", GetHttpVars("id", 0)); // same docid id
30  $attrid = GetHttpVars("attrid", 0);
31  $vaultid = GetHttpVars("vaultid", 0); // only for public file
32  $index = GetHttpVars("index");
33  // $imgheight = GetHttpVars("height");
34  $imgwidth = GetHttpVars("width");
35  $inline = (GetHttpVars("inline") == "yes");
36  $cache = (GetHttpVars("cache", "yes") == "yes");
37  $latest = GetHttpVars("latest");
38  $state = GetHttpVars("state"); // search doc in this state
39  $type = GetHttpVars("type"); // [pdf|png]
40  $pngwidth = GetHttpVars("width"); // [pdf|png]
41  $pngpage = GetHttpVars("page"); // [pdf|png]
42  $isControled = false;
43 
44  if ($vaultid == 0) {
45 
47  if ($state != "") {
48  $docid = $doc->getRevisionState($state, true);
49  if ($docid == 0) {
50  $action->exitError(sprintf(_("Document %s in %s state not found") , $doc->title, _($state)));
51  }
53  } else {
54  if (($latest == "Y") && ($doc->locked == - 1)) {
55  // get latest revision
56  $docid = $doc->latestId();
58  }
59  }
60  // ADD CONTROL ACCESS HERE
61  $err = $doc->control("view");
62  if ($err != "") $action->exiterror($err);
63  $isControled = true;
64  if ($doc->doctype == "C") {
65  $ovalue = $doc->getParamValue($attrid);
66  if (!$ovalue) $ovalue = $doc->getDefValue($attrid);
67  } else $ovalue = $doc->getValue($attrid);
68  if (($index !== "") && ($index >= 0)) {
69  $tvalue = explode("\n", $ovalue);
70  $ovalue = $tvalue[$index];
71  }
72  $oa = $doc->getAttribute($attrid);
73  if (!$oa) $action->exitError(sprintf(_("attribute %s not found") , $attrid));
74  if ($oa->getOption("preventfilechange") == "yes") {
75  if (preg_match(PREGEXPFILE, $ovalue, $reg)) {
76  $vaultid = $reg[2];
77  $mimetype = $reg[1];
78  $info = vault_properties($vaultid);
79  $othername = vault_uniqname($vaultid);
80  }
81  }
82 
83  if ($ovalue == "") {
84  print (sprintf(_("no file referenced for %s document") , $doc->title));
85  exit;
86  }
87  if ($ovalue == "") $action->exiterror(sprintf(_("no file referenced for %s document") , $doc->title));
88  preg_match(PREGEXPFILE, $ovalue, $reg);
89  $vaultid = $reg[2];
90  $mimetype = $reg[1];
91  } else {
92  $mimetype = "";
93  }
94 
95  DownloadVault($action, $vaultid, $isControled, $mimetype, $imgwidth, $inline, $cache, $type, $pngpage, $othername);
96  exit;
97 }
98 /**
99  * Idem like exportfile instead that download first file attribute found
100  */
102 {
103 
104  $dbaccess = $action->GetParam("FREEDOM_DB");
105  $docid = GetHttpVars("docid", GetHttpVars("id", 0));
106 
108  $attr = $doc->GetFirstFileAttributes();
109  if (!$attr) $action->exiterror(_("no attribute file found"));
110 
111  setHttpVar("attrid", $attr->id);
112 
114 }
115 // --------------------------------------------------------------------
116 function DownloadVault(&$action, $vaultid, $isControled, $mimetype = "", $width = "", $inline = false, $cache = true, $type = "", $pngpage = 0, $othername = '')
117 {
118  $dbaccess = $action->GetParam("FREEDOM_DB");
120  $info = null;
121  if ($type == "pdf") {
122  $teng_name = 'pdf';
123  $err = $vf->Show($vaultid, $info, $teng_name);
124  if ($err != "") $err = sprintf(_("PDF conversion not found")) . "\n$err";
125  } else {
126  $err = $vf->Show($vaultid, $info);
127  //print_r2(substr($info->mime_s,0,5));
128  //print_r2("width=".$width);
129  if (substr($info->mime_s, 0, 5) == "image") $type = "original";
130 
131  if ($type == "png") {
132  $teng_name = 'pdf';
133  $err = $vf->Show($vaultid, $info, $teng_name);
134  if ($err == "") {
135  $filecache = sprintf("%s/.img-resize/vid-%s-%d.png", DEFAULT_PUBDIR, $info->id_file, $pngpage);
136  if (file_exists($filecache)) {
137  // print_r2($filecache);
138  $resample = true;
139  if ($resample) {
140  $filename = $filecache;
141  list($owidth, $oheight) = getimagesize($filename);
142  $newwidth = $width;
143  $newheight = $oheight * ($width / $owidth);
144  // chargement
145  $thumb = imagecreatetruecolor($newwidth, $newheight);
146  $source = imagecreatefrompng($filename);
147  // Redimensionnement
148  imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $owidth, $oheight);
149  // Affichage
150  header('Content-type: image/png');
151  imagepng($thumb);
152  exit;
153  } else {
154  Http_DownloadFile($filecache, $info->name . ".png", "image/png", $inline, $cache);
155  exit;
156  }
157  }
158 
159  $cible = uniqid(getTmpDir() . "/thumb") . ".png";
160  if (!$width) $width = 150;
161  $quality = 200;
162  $resample = false;
163  // option 1
164  //$cmd=sprintf("gs -q -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r%d -sOutputFile=- -dFirstPage=%d -dLastPage=%d %s | convert - -thumbnail %s %s", min(intval($width/8.06),$quality),$pngpage+1,$pngpage+1,$info->path,$width,$cible);
165  // option 2
166  $cmd = sprintf("convert -thumbnail %s -density %d %s[%d] %s", $width, $quality, $info->path, $pngpage, $cible);
167  // option 3
168  //$cmd=sprintf("gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r%d -sOutputFile=%s -dFirstPage=%d -dLastPage=%d %s", min(intval($width/8.06),$quality),$cible,$pngpage+1,$pngpage+1,$info->path);
169  // option 4
170  //$cmd=sprintf("gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r%d -sOutputFile=%s -dFirstPage=%d -dLastPage=%d %s", min(intval($width/8.06),$quality),$cible,$pngpage+1,$pngpage+1,$info->path); $resample=true;
171  // option 5
172  //$cmd=sprintf("gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r%d -sOutputFile=%s -dFirstPage=%d -dLastPage=%d %s", intval($width/8.06),$cible,$pngpage+1,$pngpage+1,$info->path);
173  exec($cmd, $out, $ret);
174 
175  if ($ret == 1) $err = implode("\n", $out);
176  if (file_exists($cible)) {
177  // update cache
178  if ($pngpage == 0) {
179  include_once ("FDL/insertfile.php");
180  createPdf2Png($info->path, $info->id_file);
181  }
182  if ($resample) {
183  $filename = $cible;
184  list($owidth, $oheight) = getimagesize($filename);
185  $newwidth = $width;
186  $newheight = $oheight * ($width / $owidth);
187  // chargement
188  $thumb = imagecreatetruecolor($newwidth, $newheight);
189  $source = imagecreatefrompng($filename);
190  // Redimensionnement
191  imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $owidth, $oheight);
192  // Affichage
193  header('Content-type: image/png');
194  imagepng($thumb);
195  exit;
196  } else {
197  Http_DownloadFile($cible, $info->name . ".png", "image/png", $inline, $cache);
198  }
199  unlink($cible);
200  exit;
201  } else $err = sprintf(_("cannot get image transformation for %s") , $info->name) . "\n$err";
202  } else {
204  $vf->Show($vaultid, $info);
205  if ($info) $err = sprintf(_("conversion png not found for %s") , $info->name) . "\n$err";
206  }
207  } else {
208  $err = $vf->Show($vaultid, $info);
209  //print_r2(substr($info->mime_s,0,5));
210  //print_r2("width=".$width);
211  if ((substr($info->mime_s, 0, 5) == "image") && ($width > 0)) {
212  //print_r2($info);
213  $dest = rezizelocalimage($info->path, $width, $width . "-" . $info->id_file . ".png");
214  if ($dest) Http_DownloadFile($dest, $info->name . ".png", "image/png", $inline);
215  }
216  }
217  }
218 
219  if ($err != "") {
221  // Http_DownloadFile("FREEDOM/Images/doc.gif", "unknow", "image/gif");
222 
223  } else {
224  if ($info->mime_s) $mimetype = $info->mime_s;
225  //Header("Location: $url");
226  if ($isControled || ($info->public_access)) {
227  if (($mimetype != "image/jpeg") || ($width == 0)) {
228  if ($othername) $info->name = $othername;
229  Http_DownloadFile($info->path, $info->name, $mimetype, $inline, $cache);
230  } else {
231  $filename = $info->path;
232  $name = $info->name;
233  if (!$inline) header("Content-Disposition: form-data;filename=$name");
234  if ($inline) {
235  global $_SERVER;
236  $nav = $_SERVER['HTTP_USER_AGENT'];
237  $pos = strpos($nav, "MSIE");
238  if ($pos) {
239  // add special header for extension
240  header("Content-Disposition: form-data;filename=\"$name\"");
241  }
242  }
243  // header("Cache-Control: private, max-age=3600"); // use cache client (one hour) for speed optimsation
244  // header("Expires: ".gmdate ("D, d M Y H:i:s T\n",time()+3600)); // for mozilla
245  // header("Pragma: "); // HTTP 1.0
246  header('Content-type: image/jpeg');
247 
248  $mb = microtime();
249  // Calcul des nouvelles dimensions
250  list($owidth, $oheight) = getimagesize($filename);
251  $newwidth = $width;
252  $newheight = $oheight * ($width / $owidth);
253  // chargement
254  $thumb = imagecreatetruecolor($newwidth, $newheight);
255  $source = imagecreatefromjpeg($filename);
256  // Redimensionnement
257  imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $owidth, $oheight);
258  // Affichage
259  imagejpeg($thumb);
260  exit;
261  }
262  if (!$info->public_access) AddlogMsg(sprintf(_("%s has be sended") , $info->name));
263  } else {
264  $action->exiterror(_("file must be controlled : read permission needed"));
265  }
266  }
267 
268  exit;
269 }
270 /**
271  * send a text in a image file
272  * @param string $text text to display in the image
273  */
274 function sendimgerror($text)
275 {
276  // Set font size
277  $font_size = 4;
278 
279  if (seems_utf8($text)) $text = utf8_decode($text); // support only iso8859
280  $ts = explode("\n", $text);
281  $width = 0;
282  foreach ($ts as $k => $string) {
283  $width = max($width, strlen($string));
284  }
285  // Create image width dependant on width of the string
286  $width = imagefontwidth($font_size) * $width;
287  // Set height to that of the font
288  $height = imagefontheight($font_size) * count($ts);
289  $el = imagefontheight($font_size);
290  $em = imagefontwidth($font_size);
291  // Create the image pallette
292  $img = imagecreatetruecolor($width, $height);
293  // Dark red background
294  $bg = imagecolorallocate($img, 0xAA, 0x00, 0x00);
295  // White font color
296  imagefilledrectangle($img, 0, 0, $width, $height, $bg);
297  $color = imagecolorallocate($img, 255, 255, 255);
298 
299  foreach ($ts as $k => $string) {
300  // Length of the string
301  $len = strlen($string);
302  // Y-coordinate of character, X changes, Y is static
303  $ypos = 0;
304  // Loop through the string
305  for ($i = 0; $i < $len; $i++) {
306  // Position of the character horizontally
307  $xpos = $i * $em;
308  $ypos = $k * $el;
309  // Draw character
310  imagechar($img, $font_size, $xpos, $ypos, $string, $color);
311  // Remove character from string
312  $string = substr($string, 1);
313  }
314  }
315  // Return the image
316  header("Content-Type: image/png");
317  imagepng($img);
318  // Remove image
319  imagedestroy($img);
320  exit;
321 }
322 
324 {
325  $source = $img;
326 
328 
329  if (!is_dir(RESIZEDIR)) {
330  mkdir(RESIZEDIR);
331  }
332  if (!file_exists($dest)) {
333  $cmd = sprintf("convert -thumbnail %d %s %s", $size, escapeshellarg($source) , escapeshellarg($dest));
334  //print_r2($cmd);
335  //$cmd=sprintf("convert -scale %dx%d $source $dest",$size,$size);
336  system($cmd);
337  if (file_exists($dest)) return $dest;
338  } else {
339  return $dest;
340  }
341  return false;
342 }
343 ?>
← centre documentaire © anakeen - published under CC License - Dynacase