Code

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