Fixed logfile writing in installation phase, .revision is now ignored
[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:: 856                                                    $ *
14  * $Date:: 2009-03-06 20:24:32 +0100 (Fr, 06. Mär 2009)              $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author:: stelzi                                                   $ *
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 = GET_URL($requestString);
102
103                 // Shall we count the query as used?
104                 if ($countQuery) {
105                         // Then update the config!
106                         UPDATE_CONFIG("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][YOOMEDIA_TRANSLATE_INDEX($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'] = TRANSLATE_COMMA(GET_TOTAL_RECEIVERS());
200
201         // Add categories
202         $data['categories'] = ADD_CATEGORY_OPTIONS("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"))) $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         ADD_NEW_BONUS_MAIL($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 YOOMEDIA_TRANSLATE_INDEX ($type, $index) {
265         global $yoomediaTranslationTable;
266
267         // Default is the index
268         $return = $index;
269
270         // Is the element there?
271         if (isset($yoomediaTranslationTable[$type][$index])) {
272                 // Use this element
273                 $return = $yoomediaTranslationTable[$type][$index];
274         } else {
275                 // Not found!
276                 DEBUG_LOG(__FUNCTION__, __LINE__, " type={$type},index={$index} not found.");
277         }
278
279         // Return value
280         return $return;
281 }
282
283 // "Translate" error code
284 function YOOMEDIA_TRANSLATE_ERROR ($errorCode) {
285         global $yoomediaTranslationTable;
286
287         // Default is "failed"
288         $return = "failed";
289
290         // Is the entry there?
291         if (isset($yoomediaTranslationTable['error_codes'][$errorCode])) {
292                 // Entry found!
293                 $return = $yoomediaTranslationTable['error_codes'][$errorCode];
294         } else {
295                 // Log missing entries
296                 DEBUG_LOG(__FUNCTION__, __LINE__, " errorCode={$errorCode}");
297         }
298
299         // Return value
300         return $return;
301 }
302
303 // Checks if the mail id is in reload lock
304 function YOOMEDIA_CHECK_RELOAD ($id, $reload, $type) {
305         // Default is not in reload lock
306         $reloaded = false;
307
308         // Query database
309         $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",
310                 array($type, bigintval($id)), __FUNCTION__, __LINE__);
311
312         // Entry found?
313         if (SQL_NUMROWS($result) == 1) {
314                 // Load time
315                 list($id, $time) = SQL_FETCHROW($result);
316
317                 // Are we ready to sent again?
318                 if (((time() - $time) >= ($reload * 60*60)) && ($time > 0)) {
319                         // Remove entry
320                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_yoomedia_reload` WHERE id=%s LIMIT 1",
321                                 array($id), __FUNCTION__, __LINE__);
322                 } else {
323                         // Dont' sent again this mail
324                         $reloaded = $time;
325                 }
326         } // END - if
327
328         // Free result
329         SQL_FREERESULT($result);
330
331         // Return result
332         return $reloaded;
333 }
334
335 // Lock given mail down for reload lock
336 function YOOMEDIA_RELOAD_LOCK ($data, $mode) {
337         // Search for the entry
338         if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
339                 // Convert mode for mails
340                 $mode = YOOMEDIA_CONVERT_MODE($mode);
341
342                 // Add the entry
343                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_yoomedia_reload` (`type`,`y_id`,`y_reload`) VALUES ('%s',%s,%s)",
344                         array($mode, bigintval($data['id']), bigintval($data['reload'])), __FUNCTION__, __LINE__);
345         } // END - if
346 }
347
348 // Convert mode for mails
349 function YOOMEDIA_CONVERT_MODE ($mode) {
350         // Convert mode for normal/html
351         switch ($mode) {
352                 case "normal":
353                         $mode = "textmail";
354                         break;
355
356                 case "html":
357                         $mode = "htmlmail";
358                         break;
359         } // END - switch
360
361         // Return result
362         return $mode;
363 }
364
365 //
366 ?>