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