Security update merged from 2.1.
[fa-stable.git] / admin / db / printers_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 function write_printer_def($id, $name, $descr, $queue, $host, $port, $timeout)
14 {
15         if ($id>0)
16                 $sql = "UPDATE ".TB_PREF."printers SET description=".db_escape($descr)
17                 .",name=".db_escape($name).",queue=".db_escape($queue)
18                 .",host=".db_escape($host).",port=".db_escape($port).",timeout=".db_escape($timeout)
19                 ." WHERE id=".db_escape($id);
20         else 
21                 $sql = "INSERT INTO ".TB_PREF."printers ("
22                         ."name,description,queue,host,port,timeout) "
23                         ."VALUES (".db_escape($name).",".db_escape($descr).","
24                         .db_escape($queue).",".db_escape($host).",".db_escape($port).",".db_escape($timeout).")";
25
26         return db_query($sql,"could not write printer definition");
27 }
28
29 function get_all_printers() 
30 {
31         $sql = "SELECT * FROM ".TB_PREF."printers";
32         return db_query($sql,"could not get printer definitions");
33 }
34
35 function get_printer($id)
36 {
37                 $sql = "SELECT * FROM ".TB_PREF."printers
38                         WHERE id=".db_escape($id);
39
40                 $result = db_query($sql,"could not get printer definition");
41                 return  db_fetch($result);
42 }
43
44 //============================================================================
45 // printer profiles functions
46 //
47 function update_printer_profile($name, $dest)
48 {
49         foreach( $dest as $rep => $printer) {
50                 if ($printer != '' || $rep == '') {
51                         $sql = "REPLACE INTO ".TB_PREF."print_profiles "
52                         ."(profile, report, printer) VALUES ("
53                         .db_escape($name).","
54                         .db_escape($rep).","
55                         .db_escape($printer).")";
56                 } else {
57                         $sql = "DELETE FROM ".TB_PREF."print_profiles WHERE ("
58                                 ."report=" . db_escape($rep)
59                                 ." AND profile=".db_escape($name).")";
60                 }
61                 $result = db_query($sql,"could not update printing profile");
62                 if(!$result) {
63                         return false;
64                 }
65         }
66         return true;
67 }
68 //
69 //      Get destination for report defined in given printing profile.
70 //
71 function get_report_printer($profile, $report)
72 {
73         $sql = "SELECT printer FROM ".TB_PREF."print_profiles WHERE "
74                 ."profile=".db_escape($profile)." AND report=".db_escape($report);
75
76         $result = db_query($sql.db_escape($report), 'report printer lookup failed');
77
78         if (!$result) return false;
79         $ret = db_fetch($result);
80         if ($ret === false) {
81                 $result = db_query($sql."''", 'default report printer lookup failed');
82                 if (!$result) return false;
83
84                 $ret = db_fetch($result);
85                 if (!$ret) return false;
86         }
87         return get_printer($ret['printer']);
88 }
89
90 function delete_printer_profile($name)
91 {
92         $sql="DELETE FROM ".TB_PREF."print_profiles WHERE profile=".db_escape($name);
93         return db_query($sql,"could not delete printing profile");
94 }
95 //
96 // Get all report destinations for given profile.
97 //
98 function get_print_profile($name)
99 {
100         $sql = "SELECT  * FROM ".TB_PREF."print_profiles WHERE profile=".db_escape($name);
101         return db_query($sql,"could not get printing profile");
102 }
103
104 ?>