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