All database names are now 'back-ticked' and constant _MYSQL_PREFIX is wrapped. Partl...
[mailer.git] / inc / libs / yoomedia_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/10/2008 *
4  * ===============                              Last change: 10/10/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : yoomedia_functions.php                           *
8  * -------------------------------------------------------------------- *
9  * Short description : Special functions for yoomedia extension         *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Spezielle Funktion fuer Yoo!Media-Erweiterung    *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Test if the extension settings did work
41 function YOOMEDIA_TEST_CONFIG ($data) {
42         global $_CONFIG;
43
44         // Is this admin?
45         if (!IS_ADMIN()) {
46                 // No admin!
47                 return false;
48         } // END - if
49
50         // Transfer config data
51         $_CONFIG = merge_array($_CONFIG, $data);
52
53         // Temporary allow maximum
54         $_CONFIG['yoomedia_tm_max_reload']    = 1000;
55         $_CONFIG['yoomedia_tm_min_wait']      = 0;
56         $_CONFIG['yoomedia_tm_clicks_remain'] = 10;
57         $_CONFIG['yoomedia_tm_min_pay']       = 0;
58         $_CONFIG['yoomedia_erotic_allowed']   = 1;
59
60         // Query the API with a test request without couting it
61         // If zero reply comes back the data is invalid!
62         $response = YOOMEDIA_QUERY_API("out_textmail.php", true); // TODO Ask Yoo!Media for test script
63
64         // Log the response if failed
65         if (count($response) == 0) {
66                 // Queries depleted (as we count here!)
67                 DEBUG_LOG(__FUNCTION__, __LINE__, " Requested depleted. Maxmimum was: ".getConfig('yoomedia_requests_total'));
68         } elseif (count($response) <= 10) {
69                 // Log serialized raw response
70                 DEBUG_LOG(__FUNCTION__, __LINE__, " Raw response=".base64_encode(serialize($response)));
71         } // END - if
72
73         // Do we have some data there?
74         return (count($response) > 10);
75 }
76
77 // Queries the given Yoo!Media API 2.0 script
78 function YOOMEDIA_QUERY_API ($script, $countQuery = true) {
79         // Init response array
80         $response = array();
81
82         // Enougth queries left?
83         if ((getConfig('yoomedia_requests_remain') > 0) || (!$countQuery)) {
84                 // Prepare the low-level request
85                 $requestString = sprintf("http://www.yoomedia.de/interface_2.0/%s?id=%s&sid=%s&pw=%s&reload=%s&ma=%s&uebrig=%s&verguetung=%s&erotik=%s",
86                         $script,
87                         bigintval(getConfig('yoomedia_id')),
88                         bigintval(getConfig('yoomedia_sid')),
89                         getConfig('yoomedia_passwd'),
90                         bigintval(getConfig('yoomedia_tm_max_reload')),
91                         bigintval(getConfig('yoomedia_tm_min_wait')),
92                         bigintval(getConfig('yoomedia_tm_clicks_remain')),
93                         bigintval(getConfig('yoomedia_tm_min_pay')),
94                         bigintval(getConfig('yoomedia_erotic_allowed'))
95                 );
96
97                 // Run the query
98                 $response = GET_URL($requestString);
99
100                 // Shall we count the query as used?
101                 if ($countQuery) {
102                         // Then update the config!
103                         UPDATE_CONFIG("yoomedia_requests_remain", 1, "-");
104                 } // END - if
105         } // END - if
106
107         // Return the data
108         return $response;
109 }
110
111 // "Getter" for a parsed result for all text mails. This means an array without
112 // the header lines will be returned
113 function YOOMEDIA_GET_PARSED_RESULT_TEXTMAILS () {
114         // Get the raw response
115         $response = YOOMEDIA_QUERY_API("out_textmail.php");
116
117         // Parse the response
118         $result = YOOMEDIA_PARSE_RESPONSE($response, "textmail");
119
120         // Return result
121         return $result;
122 }
123
124 // Parser function for Yoo!Media API responses
125 function YOOMEDIA_PARSE_RESPONSE ($response, $type) {
126         // Init result
127         $result = array();
128
129         // Cut off the header
130         $dummy = $response;
131         foreach ($response as $line) {
132                 // Remove line
133                 array_shift($dummy);
134
135                 // Is this line empty?
136                 if (empty($line)) {
137                         // Then stop here
138                         break;
139                 } // END - if
140         } // END - foreach
141
142         // The result is now still raw, so we must split it up and trim spaces away
143         $responseLine = trim(implode("\n", $dummy));
144
145         // Last line should never be a pipe!
146         if (substr($responseLine, -1, 1) == "|") $responseLine = substr($responseLine, 0, -1);
147
148         // Now, explode all in one array
149         $dataArray = explode("|", $responseLine);
150
151         // Now make the result array with two dimensions
152         $cnt = 0; $entry = 0;
153         foreach ($dataArray as $line) {
154                 // Add the line
155                 $result[$entry][YOOMEDIA_TRANSLATE_INDEX($type, $cnt)] = $line;
156
157                 // End of data of first entry reached?
158                 if ($cnt == 6) {
159                         // Then advance to next entry and reset counter
160                         $entry++;
161                         $cnt = 0;
162                 } else {
163                         // Count up
164                         $cnt++;
165                 }
166         } // END - foreach
167
168         // Return it
169         return $result;
170 }
171
172 // Prepares a bonus mail for delivery. Works only if extension "bonus" is active
173 function YOOMEDIA_PREPARE_MAIL_DELIVERY ($data) {
174         // Is this an admin?
175         if (!IS_ADMIN()) {
176                 // Abort here
177                 return false;
178         } elseif (!EXT_IS_ACTIVE("bonus")) {
179                 // Abort here
180                 return false;
181         }
182
183         // Is the waiting time below one second? Then fix it to one (zero seconds are not yet supported!)
184         if ($data['wait'] < 1) $data['wait'] = 1;
185
186         // Half of waiting time is a good reward!
187         $data['reward'] = round($data['wait'] / 2 + 0.4);
188
189         // Is the reward below one?
190         if ($data['reward'] < 1) $data['reward'] = 1;
191
192         // Add website id
193         $data['sid'] = getConfig('yoomedia_sid');
194
195         // Add total receivers
196         $data['all'] = TRANSLATE_COMMA(GET_TOTAL_RECEIVERS());
197
198         // Add categories
199         $data['categories'] = ADD_CATEGORY_OPTIONS("normal");
200
201         // Load template
202         LOAD_TEMPLATE("admin_send_yoomedia", false, $data);
203 }
204
205 // Adds the mail to the bonus mail pool
206 function YOOMEDIA_SEND_BONUS_MAIL ($data, $mode) {
207         // Is this an admin?
208         if (!IS_ADMIN()) {
209                 // Abort here
210                 return false;
211         } elseif (!EXT_IS_ACTIVE("bonus")) {
212                 // Abort here
213                 return false;
214         }
215
216         // Add dummy receiver to avoid notice
217         $data['receiver'] = 0;
218
219         // HTML or normal? (normal is default...)
220         $type = "t";
221         if (($mode == "html") && (EXT_IS_ACTIVE("html"))) $type = "h";
222
223         // Auto-generate URL
224         $data['url'] = sprintf("http://www.yoomedia.de/code/%s-mail.php?id=%s&sid=%s",
225                 $type,
226                 $data['id'],
227                 $data['sid']
228         );
229
230         // Lock this mail for new delivery
231         YOOMEDIA_RELOAD_LOCK($data, $mode);
232
233         // Call the lower function
234         ADD_NEW_BONUS_MAIL($data, $mode);
235 }
236
237 // Lockdown given id
238 function YOOMEDIA_EXCLUDE_MAIL ($data, $mode) {
239         // Search for the entry
240         if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
241                 // Convert mode for mails
242                 $mode = YOOMEDIA_CONVERT_MODE($mode);
243
244                 // Add the entry
245                 SQL_QUERY_ESC("INSERT INTO `{!MYSQL_PREFIX!}_yoomedia_reload` (`type`,`y_id`,`y_reload`,`inserted`) VALUES ('%s',%s,%s,'0000-00-00 00:00')",
246                         array($mode, bigintval($data['id']), bigintval($data['reload'])), __FILE__, __LINE__);
247         } // END - if
248 }
249
250 // Remove lock of given mail
251 function YOOMEDIA_UNLIST_MAIL ($data, $mode) {
252         // Convert mode for mails
253         $mode = YOOMEDIA_CONVERT_MODE($mode);
254
255         // Add the entry
256         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!MYSQL_PREFIX!}_yoomedia_reload` WHERE `type`='%s' AND `y_id`=%s LIMIT 1",
257                 array($mode, bigintval($data['id'])), __FILE__, __LINE__);
258 }
259
260 // "Translates" the index number into an assosiative value
261 function YOOMEDIA_TRANSLATE_INDEX ($type, $index) {
262         global $yoomediaTranslationTable;
263
264         // Default is the index
265         $return = $index;
266
267         // Is the element there?
268         if (isset($yoomediaTranslationTable[$type][$index])) {
269                 // Use this element
270                 $return = $yoomediaTranslationTable[$type][$index];
271         } else {
272                 // Not found!
273                 DEBUG_LOG(__FUNCTION__, __LINE__, " type={$type},index={$index} not found.");
274         }
275
276         // Return value
277         return $return;
278 }
279
280 // "Translate" error code
281 function YOOMEDIA_TRANSLATE_ERROR ($errorCode) {
282         global $yoomediaTranslationTable;
283
284         // Default is "failed"
285         $return = "failed";
286
287         // Is the entry there?
288         if (isset($yoomediaTranslationTable['error_codes'][$errorCode])) {
289                 // Entry found!
290                 $return = $yoomediaTranslationTable['error_codes'][$errorCode];
291         } else {
292                 // Log missing entries
293                 DEBUG_LOG(__FUNCTION__, __LINE__, " errorCode={$errorCode}");
294         }
295
296         // Return value
297         return $return;
298 }
299
300 // Checks if the mail id is in reload lock
301 function YOOMEDIA_CHECK_RELOAD ($id, $reload, $type) {
302         // Default is not in reload lock
303         $reloaded = false;
304
305         // Query database
306         $result = SQL_QUERY_ESC("SELECT `id`, UNIX_TIMESTAMP(`inserted`) AS inserted FROM `{!MYSQL_PREFIX!}_yoomedia_reload` WHERE `type`='%s' AND `y_id`=%s LIMIT 1",
307                 array($type, bigintval($id)), __FILE__, __LINE__);
308
309         // Entry found?
310         if (SQL_NUMROWS($result) == 1) {
311                 // Load time
312                 list($id, $time) = SQL_FETCHROW($result);
313
314                 // Are we ready to sent again?
315                 if (((time() - $time) >= ($reload * 60*60)) && ($time > 0)) {
316                         // Remove entry
317                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!MYSQL_PREFIX!}_yoomedia_reload` WHERE id=%s LIMIT 1",
318                                 array($id), __FILE__, __LINE__);
319                 } else {
320                         // Dont' sent again this mail
321                         $reloaded = $time;
322                 }
323         } // END - if
324
325         // Free result
326         SQL_FREERESULT($result);
327
328         // Return result
329         return $reloaded;
330 }
331
332 // Lock given mail down for reload lock
333 function YOOMEDIA_RELOAD_LOCK ($data, $mode) {
334         // Search for the entry
335         if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
336                 // Convert mode for mails
337                 $mode = YOOMEDIA_CONVERT_MODE($mode);
338
339                 // Add the entry
340                 SQL_QUERY_ESC("INSERT INTO `{!MYSQL_PREFIX!}_yoomedia_reload` (`type`,`y_id`,`y_reload`) VALUES ('%s',%s,%s)",
341                         array($mode, bigintval($data['id']), bigintval($data['reload'])), __FILE__, __LINE__);
342         } // END - if
343 }
344
345 // Convert mode for mails
346 function YOOMEDIA_CONVERT_MODE ($mode) {
347         // Convert mode for normal/html
348         switch ($mode) {
349                 case "normal":
350                         $mode = "textmail";
351                         break;
352
353                 case "html":
354                         $mode = "htmlmail";
355                         break;
356         } // END - switch
357
358         // Return result
359         return $mode;
360 }
361
362 //
363 ?>