A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[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         // Is this admin?
43         if (!IS_ADMIN()) {
44                 // No admin!
45                 return false;
46         } // END - if
47
48         // Transfer config data
49         mergeConfig($data);
50
51         // Temporary allow maximum
52         setConfigEntry('yoomedia_tm_max_reload'   , 1000);
53         setConfigEntry('yoomedia_tm_min_wait'     , 0);
54         setConfigEntry('yoomedia_tm_clicks_remain', 10);
55         setConfigEntry('yoomedia_tm_min_pay'      , 0);
56         setConfigEntry('yoomedia_erotic_allowed'  , 1);
57
58         // Query the API with a test request without couting it
59         // If zero reply comes back the data is invalid!
60         $response = YOOMEDIA_QUERY_API("out_textmail.php", true); // @TODO Ask Yoo!Media for test script
61
62         // Log the response if failed
63         if (count($response) == 0) {
64                 // Queries depleted (as we count here!)
65                 DEBUG_LOG(__FUNCTION__, __LINE__, " Requested depleted. Maxmimum was: ".getConfig('yoomedia_requests_total'));
66         } elseif (count($response) <= 10) {
67                 // Log serialized raw response
68                 DEBUG_LOG(__FUNCTION__, __LINE__, " Raw response=".base64_encode(serialize($response)));
69         } // END - if
70
71         // Do we have some data there?
72         return (count($response) > 10);
73 }
74
75 // Queries the given Yoo!Media API 2.0 script
76 function YOOMEDIA_QUERY_API ($script, $countQuery = true) {
77         // Init response array
78         $response = array();
79
80         // Enougth queries left?
81         if ((getConfig('yoomedia_requests_remain') > 0) || (!$countQuery)) {
82                 // Prepare the low-level request
83                 $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",
84                         $script,
85                         getConfig(('yoomedia_id')),
86                         getConfig(('yoomedia_sid')),
87                         getConfig('yoomedia_passwd'),
88                         getConfig(('yoomedia_tm_max_reload')),
89                         getConfig(('yoomedia_tm_min_wait')),
90                         getConfig(('yoomedia_tm_clicks_remain')),
91                         getConfig(('yoomedia_tm_min_pay')),
92                         getConfig(('yoomedia_erotic_allowed'))
93                 );
94
95                 // Run the query
96                 $response = GET_URL($requestString);
97
98                 // Shall we count the query as used?
99                 if ($countQuery) {
100                         // Then update the config!
101                         UPDATE_CONFIG("yoomedia_requests_remain", 1, "-");
102                 } // END - if
103         } // END - if
104
105         // Return the data
106         return $response;
107 }
108
109 // "Getter" for a parsed result for all text mails. This means an array without
110 // the header lines will be returned
111 function YOOMEDIA_GET_PARSED_RESULT_TEXTMAILS () {
112         // Get the raw response
113         $response = YOOMEDIA_QUERY_API("out_textmail.php");
114
115         // Parse the response
116         $result = YOOMEDIA_PARSE_RESPONSE($response, "textmail");
117
118         // Return result
119         return $result;
120 }
121
122 // Parser function for Yoo!Media API responses
123 function YOOMEDIA_PARSE_RESPONSE ($response, $type) {
124         // Init result
125         $result = array();
126
127         // Cut off the header
128         $dummy = $response;
129         foreach ($response as $line) {
130                 // Remove line
131                 array_shift($dummy);
132
133                 // Is this line empty?
134                 if (empty($line)) {
135                         // Then stop here
136                         break;
137                 } // END - if
138         } // END - foreach
139
140         // The result is now still raw, so we must split it up and trim spaces away
141         $responseLine = trim(implode("\n", $dummy));
142
143         // Last line should never be a pipe!
144         if (substr($responseLine, -1, 1) == "|") $responseLine = substr($responseLine, 0, -1);
145
146         // Now, explode all in one array
147         $dataArray = explode("|", $responseLine);
148
149         // Now make the result array with two dimensions
150         $cnt = 0; $entry = 0;
151         foreach ($dataArray as $line) {
152                 // Add the line
153                 $result[$entry][YOOMEDIA_TRANSLATE_INDEX($type, $cnt)] = $line;
154
155                 // End of data of first entry reached?
156                 if ($cnt == 6) {
157                         // Then advance to next entry and reset counter
158                         $entry++;
159                         $cnt = 0;
160                 } else {
161                         // Count up
162                         $cnt++;
163                 }
164         } // END - foreach
165
166         // Return it
167         return $result;
168 }
169
170 // Prepares a bonus mail for delivery. Works only if extension "bonus" is active
171 function YOOMEDIA_PREPARE_MAIL_DELIVERY ($data) {
172         // Is this an admin?
173         if (!IS_ADMIN()) {
174                 // Abort here
175                 return false;
176         } elseif (!EXT_IS_ACTIVE("bonus")) {
177                 // Abort here
178                 return false;
179         }
180
181         // Is the waiting time below one second? Then fix it to one (zero seconds are not yet supported!)
182         if ($data['wait'] < 1) $data['wait'] = 1;
183
184         // Half of waiting time is a good reward!
185         $data['reward'] = round($data['wait'] / 2 + 0.4);
186
187         // Is the reward below one?
188         if ($data['reward'] < 1) $data['reward'] = 1;
189
190         // Add website id
191         $data['sid'] = getConfig('yoomedia_sid');
192
193         // Add total receivers
194         $data['all'] = TRANSLATE_COMMA(GET_TOTAL_RECEIVERS());
195
196         // Add categories
197         $data['categories'] = ADD_CATEGORY_OPTIONS("normal");
198
199         // Load template
200         LOAD_TEMPLATE("admin_send_yoomedia", false, $data);
201 }
202
203 // Adds the mail to the bonus mail pool
204 function YOOMEDIA_SEND_BONUS_MAIL ($data, $mode) {
205         // Is this an admin?
206         if (!IS_ADMIN()) {
207                 // Abort here
208                 return false;
209         } elseif (!EXT_IS_ACTIVE("bonus")) {
210                 // Abort here
211                 return false;
212         }
213
214         // Add dummy receiver to avoid notice
215         $data['receiver'] = 0;
216
217         // HTML or normal? (normal is default...)
218         $type = "t";
219         if (($mode == "html") && (EXT_IS_ACTIVE("html"))) $type = "h";
220
221         // Auto-generate URL
222         $data['url'] = sprintf("http://www.yoomedia.de/code/%s-mail.php?id=%s&sid=%s",
223                 $type,
224                 $data['id'],
225                 $data['sid']
226         );
227
228         // Lock this mail for new delivery
229         YOOMEDIA_RELOAD_LOCK($data, $mode);
230
231         // Call the lower function
232         ADD_NEW_BONUS_MAIL($data, $mode);
233 }
234
235 // Lockdown given id
236 function YOOMEDIA_EXCLUDE_MAIL ($data, $mode) {
237         // Search for the entry
238         if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
239                 // Convert mode for mails
240                 $mode = YOOMEDIA_CONVERT_MODE($mode);
241
242                 // Add the entry
243                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_yoomedia_reload` (`type`,`y_id`,`y_reload`,`inserted`) VALUES ('%s',%s,%s,'0000-00-00 00:00')",
244                         array($mode, bigintval($data['id']), bigintval($data['reload'])), __FUNCTION__, __LINE__);
245         } // END - if
246 }
247
248 // Remove lock of given mail
249 function YOOMEDIA_UNLIST_MAIL ($data, $mode) {
250         // Convert mode for mails
251         $mode = YOOMEDIA_CONVERT_MODE($mode);
252
253         // Add the entry
254         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_yoomedia_reload` WHERE `type`='%s' AND `y_id`=%s LIMIT 1",
255                 array($mode, bigintval($data['id'])), __FUNCTION__, __LINE__);
256 }
257
258 // "Translates" the index number into an assosiative value
259 function YOOMEDIA_TRANSLATE_INDEX ($type, $index) {
260         global $yoomediaTranslationTable;
261
262         // Default is the index
263         $return = $index;
264
265         // Is the element there?
266         if (isset($yoomediaTranslationTable[$type][$index])) {
267                 // Use this element
268                 $return = $yoomediaTranslationTable[$type][$index];
269         } else {
270                 // Not found!
271                 DEBUG_LOG(__FUNCTION__, __LINE__, " type={$type},index={$index} not found.");
272         }
273
274         // Return value
275         return $return;
276 }
277
278 // "Translate" error code
279 function YOOMEDIA_TRANSLATE_ERROR ($errorCode) {
280         global $yoomediaTranslationTable;
281
282         // Default is "failed"
283         $return = "failed";
284
285         // Is the entry there?
286         if (isset($yoomediaTranslationTable['error_codes'][$errorCode])) {
287                 // Entry found!
288                 $return = $yoomediaTranslationTable['error_codes'][$errorCode];
289         } else {
290                 // Log missing entries
291                 DEBUG_LOG(__FUNCTION__, __LINE__, " errorCode={$errorCode}");
292         }
293
294         // Return value
295         return $return;
296 }
297
298 // Checks if the mail id is in reload lock
299 function YOOMEDIA_CHECK_RELOAD ($id, $reload, $type) {
300         // Default is not in reload lock
301         $reloaded = false;
302
303         // Query database
304         $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",
305                 array($type, bigintval($id)), __FUNCTION__, __LINE__);
306
307         // Entry found?
308         if (SQL_NUMROWS($result) == 1) {
309                 // Load time
310                 list($id, $time) = SQL_FETCHROW($result);
311
312                 // Are we ready to sent again?
313                 if (((time() - $time) >= ($reload * 60*60)) && ($time > 0)) {
314                         // Remove entry
315                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_yoomedia_reload` WHERE id=%s LIMIT 1",
316                                 array($id), __FUNCTION__, __LINE__);
317                 } else {
318                         // Dont' sent again this mail
319                         $reloaded = $time;
320                 }
321         } // END - if
322
323         // Free result
324         SQL_FREERESULT($result);
325
326         // Return result
327         return $reloaded;
328 }
329
330 // Lock given mail down for reload lock
331 function YOOMEDIA_RELOAD_LOCK ($data, $mode) {
332         // Search for the entry
333         if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
334                 // Convert mode for mails
335                 $mode = YOOMEDIA_CONVERT_MODE($mode);
336
337                 // Add the entry
338                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_yoomedia_reload` (`type`,`y_id`,`y_reload`) VALUES ('%s',%s,%s)",
339                         array($mode, bigintval($data['id']), bigintval($data['reload'])), __FUNCTION__, __LINE__);
340         } // END - if
341 }
342
343 // Convert mode for mails
344 function YOOMEDIA_CONVERT_MODE ($mode) {
345         // Convert mode for normal/html
346         switch ($mode) {
347                 case "normal":
348                         $mode = "textmail";
349                         break;
350
351                 case "html":
352                         $mode = "htmlmail";
353                         break;
354         } // END - switch
355
356         // Return result
357         return $mode;
358 }
359
360 //
361 ?>