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();
26 session::set('errorsAlreadyPosted',array());
28 /* Logged in? Simple security check */
29 if (!session::is_set('ui')){
30 new log("security","faxreport/faxreport","",array(),"Error: getfax.php called without session") ;
31 header ("Location: index.php");
32 exit;
33 }
34 $ui= session::is_set("ui");
36 /* User object present? */
37 if (!session::is_set('fuserfilter')){
38 new log("security","faxreport/faxreport","",array(),"getfax.php called without propper session data") ;
39 header ("Location: index.php");
40 exit;
41 }
43 /* Get loaded servers */
44 foreach (array("FAX_SERVER", "FAX_LOGIN", "FAX_PASSWORD") as $val){
45 if (session::is_set($val)){
46 $$val= session::get($val);
47 }
48 }
50 /* Load fax entry */
51 $config= session::get('config');
52 $cfg= $config->data['SERVERS']['FAX'];
53 $link = mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD'])
54 or die(_("Could not connect to database server!"));
56 mysql_select_db("gofax") or die(_("Could not select database!"));
59 /* Permission to view? */
60 $query = "SELECT id,uid FROM faxlog WHERE id = '".validate(stripcslashes($_GET['id']))."'";
61 $result = mysql_query($query) or die(_("Database query failed!"));
62 $line = mysql_fetch_array($result, MYSQL_ASSOC);
63 if (!preg_match ("/'".$line["uid"]."'/", session::get('fuserfilter'))){
64 die ("No permissions to view fax!");
65 }
67 $query = "SELECT id,fax_data FROM faxdata WHERE id = '".
68 validate(stripcslashes($_GET['id']))."'";
69 $result = mysql_query($query) or die(_("Database query failed!"));
71 /* Load pic */
72 $data = mysql_result ($result, 0, "fax_data");
73 mysql_close ($link);
75 if (!isset($_GET['download'])){
77 /* display picture */
78 header("Content-type: image/png");
80 /* Fallback if there's no image magick support in PHP */
81 if (!function_exists("imagick_blob2image")){
83 /* Write to temporary file and call convert, because TIFF sucks */
84 $tmpfname = tempnam ("/tmp", "GOsa");
85 $temp= fopen($tmpfname, "w");
86 fwrite($temp, $data);
87 fclose($temp);
89 /* Read data written by convert */
90 $output= "";
91 $query= "convert -size 420x594 $tmpfname -resize 420x594 +profile \"*\" png:- 2> /dev/null";
92 $sh= popen($query, 'r');
93 $data= "";
94 while (!feof($sh)){
95 $data.= fread($sh, 4096);
96 }
97 pclose($sh);
99 unlink($tmpfname);
101 } else {
103 /* Loading image */
104 if(!$handle = imagick_blob2image($data)) {
105 new log("view","faxreport/faxreport","",array(),"Can't load fax image") ;
106 }
108 /* Converting image to PNG */
109 if(!imagick_convert($handle,"PNG")) {
110 new log("view","faxreport/faxreport","",array(),"Can't convert fax image to png") ;
111 }
113 /* Resizing image to 420x594 and blur */
114 if(!imagick_resize($handle,420,594,IMAGICK_FILTER_GAUSSIAN,1)){
115 new log("view","faxreport/faxreport","",array(),"Can't resize fax image") ;
116 }
118 /* Creating binary Code for the Image */
119 if(!$data = imagick_image2blob($handle)){
120 new log("view","faxreport/faxreport","",array(),"Reading fax image image failed") ;
121 }
122 }
124 } else {
126 /* force download dialog */
127 header("Content-type: application/tiff\n");
128 if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) ||
129 preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)) {
130 header('Content-Disposition: filename="fax.tif"');
131 } else {
132 header('Content-Disposition: attachment; filename="fax.tif"');
133 }
134 header("Content-transfer-encoding: binary\n");
135 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
136 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
137 header("Cache-Control: no-cache");
138 header("Pragma: no-cache");
139 header("Cache-Control: post-check=0, pre-check=0");
141 }
143 /* print the tiff image and close the connection */
144 echo "$data";
146 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
147 ?>