1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 Cajus Pollmeier
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 /* Basic setup, remove eventually registered sessions */
22 @require_once ("../include/php_setup.inc");
23 @require_once ("functions.inc");
24 error_reporting (0);
25 session_start ();
27 /* Logged in? Simple security check */
28 if (!isset($_SESSION['ui'])){
29 gosa_log ("Error: getfax.php called without session");
30 header ("Location: ../index.php");
31 exit;
32 }
33 $ui= $_SESSION["ui"];
35 /* User object present? */
36 if (!isset($_SESSION['fuserfilter'])){
37 gosa_log ("Error: getfax.php called without propper session data");
38 header ("Location: ../index.php");
39 exit;
40 }
42 /* Get loaded servers */
43 foreach (array("FAX_SERVER", "FAX_LOGIN", "FAX_PASSWORD") as $val){
44 if (isset ($_SESSION[$val])){
45 $$val= $_SESSION[$val];
46 }
47 }
49 /* Load fax entry */
50 $config= $_SESSION['config'];
51 $cfg= $config->data['SERVERS']['FAX'];
52 $link = mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD'])
53 or die(_("Could not connect to database server!"));
55 mysql_select_db("gofax") or die(_("Could not select database!"));
58 /* Permission to view? */
59 $query = "SELECT id,uid FROM faxlog WHERE id = '".validate(stripcslashes($_GET['id']))."'";
60 $result = mysql_query($query) or die(_("Database query failed!"));
61 $line = mysql_fetch_array($result, MYSQL_ASSOC);
62 if (!preg_match ("/'".$line["uid"]."'/", $_SESSION['fuserfilter'])){
63 die ("No permissions to view fax!");
64 }
66 $query = "SELECT id,fax_data FROM faxdata WHERE id = '".
67 validate(stripcslashes($_GET['id']))."'";
68 $result = mysql_query($query) or die(_("Database query failed!"));
70 /* Load pic */
71 $data = mysql_result ($result, 0, "fax_data");
72 mysql_close ($link);
74 if (!isset($_GET['download'])){
76 /* display picture */
77 header("Content-type: image/png");
79 /* Fallback if there's no image magick support in PHP */
80 if (!function_exists("imagick_blob2image")){
82 /* Write to temporary file and call convert, because TIFF sucks */
83 $tmpfname = tempnam ("/tmp", "GOsa");
84 $temp= fopen($tmpfname, "w");
85 fwrite($temp, $data);
86 fclose($temp);
88 /* Read data written by convert */
89 $output= "";
90 $query= "convert -size 420x594 $tmpfname -resize 420x594 +profile \"*\" png:- 2> /dev/null";
91 $sh= popen($query, 'r');
92 $data= "";
93 while (!feof($sh)){
94 $data.= fread($sh, 4096);
95 }
96 pclose($sh);
98 unlink($tmpfname);
100 } else {
102 /* Loading image */
103 if(!$handle = imagick_blob2image($data)) {
104 gosa_log("Can't Load image");
105 }
107 /* Converting image to PNG */
108 if(!imagick_convert($handle,"PNG")) {
109 gosa_log("Can't Convert to PNG");
110 }
112 /* Resizing image to 420x594 and blur */
113 if(!imagick_resize($handle,420,594,IMAGICK_FILTER_GAUSSIAN,1)){
114 gosa_log("imagick_resize failed");
115 }
117 /* Creating binary Code for the Image */
118 if(!$data = imagick_image2blob($handle)){
119 gosa_log("Can't create blob for image");
120 }
121 }
123 } else {
125 /* force download dialog */
126 header("Content-type: application/tiff\n");
127 if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
128 preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
129 header('Content-Disposition: filename="fax.tif"');
130 } else {
131 header('Content-Disposition: attachment; filename="fax.tif"');
132 }
133 header("Content-transfer-encoding: binary\n");
134 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
135 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
136 header("Cache-Control: no-cache");
137 header("Pragma: no-cache");
138 header("Cache-Control: post-check=0, pre-check=0");
140 }
142 /* print the tiff image and close the connection */
143 echo "$data";
145 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
146 ?>