Fixed bug 'sprintf() too few arguments':
[mailer.git] / inc / pool-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 01/22/2013 *
4  * ===================                          Last change: 01/22/2013 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : pool-functions.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for handling status codes              *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen zum Umgang mit Status-Funktionen      *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // "Getter" for pool data from given id number
44 // @TODO "Hook" ext-html_mail into this to allow column `html_msg`
45 function getPoolDataFromId ($poolId) {
46         // Init content array
47         $content = array();
48
49         // Search for pool data
50         $result = sqlQueryEscaped('SELECT
51         `id`,
52         `sender`,
53         `subject`,
54         `text`,
55         `receivers`,
56         `payment_id`,
57         `data_type`,
58         `timestamp`,
59         `url`,
60         `target_send`,
61         `cat_id`,
62         `zip`
63 FROM
64         `{?_MYSQL_PREFIX?}_pool`
65 WHERE
66         `id`=%s
67 LIMIT 1',
68                 array(bigintval($poolId)), __FUNCTION__, __LINE__);
69
70         // Is there an entry?
71         if (sqlNumRows($result) == 1) {
72                 // Load data
73                 $content = sqlFetchArray($result);
74         } // END - if
75
76         // Free result
77         sqlFreeResult($result);
78
79         // Return found data
80         return $content;
81 }
82
83 // Update the pool
84 function updatePoolDataById ($poolId, $columnName, $data, $updateMode = NULL, $whereSql = '', $moreSql = '') {
85         // Is update mode set?
86         if (!is_null($updateMode)) {
87                 // Don't allow array as data here
88                 assert(!is_array($data));
89
90                 // Then use this on the column with this mode (mostly counters)
91                 sqlQueryEscaped('UPDATE `{?_MYSQL_PREFIX?}_pool` SET `%s`=`%s`%s%s WHERE `id`=%s' . $whereSql . ' LIMIT 1',
92                         array(
93                                 $columnName,
94                                 $columnName,
95                                 $updateMode,
96                                 $data,
97                                 bigintval($poolId)
98                         ), __FUNCTION__, __LINE__);
99         } elseif (is_array($data)) {
100                 /*
101                  * Update multiple columns, $columnName and $updateMode are being
102                  * ignored as last doesn't work and first is given in $data array.
103                  */
104                 $sql = 'UPDATE `{?_MYSQL_PREFIX?}_pool` SET ';
105                 foreach ($data as $key => $value) {
106                         // Add it
107                         $sql .= sprintf("`%s`='%s',", sqlEscapeString($key), sqlEscapeString($value));
108                 } // END - foreach
109
110                 // Finish SQL
111                 $sql = substr($sql, 0, -1) . $moreSql . ' WHERE `id`=' . bigintval($id) . $whereSql . ' LIMIT 1';
112
113                 // And finally run it
114                 sqlQuery($sql, __FUNCTION__, __LINE__);
115         } else {
116                 // Regular update
117                 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `%s`='%s' WHERE `id`=%s" . $whereSql . ' LIMIT 1',
118                         array(
119                                 $columnName,
120                                 $data,
121                                 bigintval($poolId)
122                         ), __FUNCTION__, __LINE__);
123         }
124
125         // Return if it has an affected row
126         return (sqlAffectedRows() == 1);
127 }
128
129 // Insert data into pool and return its insert id
130 function insertDataIntoPool ($data) {
131         // Construct SQL query
132         $sql = 'INSERT INTO `{?_MYSQL_PREFIX?}_pool (`' . implode('`,`', array_map('SQL_ESCAPE', array_keys($data))) . "`) VALUES ('" . implode("','", array_values($data)) . "')";
133
134         // Run the query
135         sqlQuery($sql, __FUNCTION__, __LINE__);
136
137         // Return insert id
138         return getSqlInsertId();
139 }
140
141 // [EOF]
142 ?>