2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 10/10/2008 *
4 * =================== Last change: 10/10/2008 *
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 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 - 2009 by Roland Haeder *
21 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
32 * GNU General Public License for more details. *
34 * You should have received a copy of the GNU General Public License *
35 * along with this program; if not, write to the Free Software *
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Queries the given Yoo!Media API 2.0 script
46 function YOOMEDIA_QUERY_API ($script, $countQuery = true) {
47 // Init response array
50 // Enougth queries left?
51 if ((getConfig('yoomedia_requests_remain') > 0) || ($countQuery === false)) {
52 // Prepare the low-level request
53 $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",
55 getConfig('yoomedia_id'),
56 getConfig('yoomedia_sid'),
57 getConfig('yoomedia_passwd'),
58 getConfig('yoomedia_tm_max_reload'),
59 getConfig('yoomedia_tm_min_wait'),
60 getConfig('yoomedia_tm_clicks_remain'),
61 getConfig('yoomedia_tm_min_pay'),
62 getConfig('yoomedia_erotic_allowed')
66 $response = sendGetRequest($requestString);
68 // Convert from ISO to UTF-8 only if count is > 3 because <= 3 means timeout
69 if (count($response) > 3) {
70 // Convert all lines to UTF-8
71 foreach ($response as $k => $v) {
73 $response[$k] = iconv('windows-1252', 'UTF-8//TRANSLIT', $v);
75 // iconv()-less ISO-8859-1 -> UTF-8
76 $response[$k] = preg_replace(
78 "chr(0xC0|ord('\\1')>>6).chr(0x80|ord('\\1')&0x3F)",
85 // Shall we count the query as used?
86 if ($countQuery === true) {
87 // Then update the config!
88 updateConfiguration('yoomedia_requests_remain', 1, '-');
96 // Test if the extension settings did work
97 function YOOMEDIA_TEST_CONFIG ($data) {
104 // Transfer config data
107 // Temporary allow maximum
108 setConfigEntry('yoomedia_tm_max_reload' , 100000);
109 setConfigEntry('yoomedia_tm_min_wait' , 0);
110 setConfigEntry('yoomedia_tm_clicks_remain', 10);
111 setConfigEntry('yoomedia_tm_min_pay' , 0);
112 setConfigEntry('yoomedia_erotic_allowed' , 1);
114 // Query the API with a test request without couting it
115 // If zero reply comes back the data is invalid!
116 $response = YOOMEDIA_QUERY_API('out_textmail.php', true); // @TODO Ask Yoo!Media for test script
118 // Default error code is 0 = all fine!
119 $errorCode = YOOMEDIA_GET_ERRORCODE_FROM_RESULT($response);
121 // Log the response if failed
122 if (count($response) == 0) {
123 // Queries depleted (as we count here!)
124 logDebugMessage(__FUNCTION__, __LINE__, 'Requested depleted. Maxmimum was: ' . getConfig('yoomedia_requests_total'));
126 } elseif (!isset($response[8])) {
128 logDebugMessage(__FUNCTION__, __LINE__, 'Missing response line [8]. Raw response=' . base64_encode(serialize($response)));
130 } elseif ((($errorCode <= 4) && ($errorCode > 0)) || ($errorCode >= 8)) {
131 // An error has returned from the account
132 logDebugMessage(__FUNCTION__, __LINE__, 'Unexpected error code ' . $errorCode . ' received.');
133 } elseif (count($response) < 9) {
134 // Log serialized raw response
135 logDebugMessage(__FUNCTION__, __LINE__, 'Raw response=' . base64_encode(serialize($response)));
138 // This is fine, because the result array is okay and the response code on element 8 is fine
142 // Do we have some data there?
143 return ($errorCode == '0');
146 // "Getter" for a parsed result for all text mails. This means an array without
147 // the header lines will be returned
148 function YOOMEDIA_GET_PARSED_RESULT_TEXTMAILS () {
149 // Get the raw response
150 $response = YOOMEDIA_QUERY_API('out_textmail.php');
152 // Parse the response
153 $result = YOOMEDIA_PARSE_RESPONSE($response, 'textmail');
159 // Parser function for Yoo!Media API responses
160 function YOOMEDIA_PARSE_RESPONSE ($response, $type) {
164 // Cut off the header
166 foreach ($response as $line) {
170 // Is this line empty?
177 // If we have no result, abort here
178 if (count($dummy) == 0) {
179 // Empty response from API
180 logDebugMessage(__FUNCTION__, __LINE__, 'Empy result from API received.');
184 // The result is now still raw, so we must split it up and trim spaces away
185 $responseLine = trim(implode("\n", $dummy));
187 // Last line should never be a pipe!
188 if (substr($responseLine, -1, 1) == '|') $responseLine = substr($responseLine, 0, -1);
190 // Now, explode all in one array
191 $dataArray = explode('|', $responseLine);
193 // Now make the result array with two dimensions
194 $cnt = '0'; $entry = '0';
195 foreach ($dataArray as $line) {
197 $result[$entry][yoomediaTranslateIndex($type, $cnt)] = $line;
199 // End of data of first entry reached?
201 // Then advance to next entry and reset counter
214 // Prepares a bonus mail for delivery. Works only if extension 'bonus' is active
215 function YOOMEDIA_PREPARE_MAIL_DELIVERY ($data) {
220 } elseif (!isExtensionActive('bonus')) {
225 // Is the waiting time below one second? Then fix it to one (zero seconds are not yet supported!)
226 if ($data['wait'] < 1) $data['wait'] = 1;
228 // Half of waiting time is a good reward!
229 $data['reward'] = round($data['wait'] / 2 + 0.4);
231 // Is the reward below one?
232 if ($data['reward'] < 1) $data['reward'] = 1;
235 $data['sid'] = getConfig('yoomedia_sid');
237 // Add total receivers
238 $data['all'] = translateComma(getTotalReceivers());
241 $data['categories'] = generateCategoryOptionsList('normal');
244 $data['text'] = decodeEntities($data['text']);
247 loadTemplate('admin_send_yoomedia', false, $data);
250 // Adds the mail to the bonus mail pool
251 function YOOMEDIA_SEND_BONUS_MAIL ($data, $mode) {
256 } elseif (!isExtensionActive('bonus')) {
261 // Add dummy receiver to avoid notice
262 $data['receiver'] = '0';
264 // HTML or normal? (normal is default...)
266 if (($mode == 'html') && (isExtensionActive('html_mail'))) $type = 'h';
269 $data['url'] = sprintf("http://www.yoomedia.de/code/%s-mail.php?id=%s&sid=%s",
275 // Lock this mail for new delivery
276 YOOMEDIA_RELOAD_LOCK($data, $mode);
278 // Call the lower function
279 addNewBonusMail($data, $mode);
283 function YOOMEDIA_EXCLUDE_MAIL ($data, $mode) {
284 // Search for the entry
285 if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
286 // Convert mode for mails
287 $mode = YOOMEDIA_CONVERT_MODE($mode);
290 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_yoomedia_reload` (`type`,`y_id`,`y_reload`,`inserted`) VALUES ('%s',%s,%s,'0000-00-00 00:00')",
293 bigintval($data['id']),
294 bigintval($data['reload'])
295 ), __FUNCTION__, __LINE__);
299 // Remove lock of given mail
300 function YOOMEDIA_UNLIST_MAIL ($data, $mode) {
301 // Convert mode for mails
302 $mode = YOOMEDIA_CONVERT_MODE($mode);
305 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_yoomedia_reload` WHERE `type`='%s' AND `y_id`=%s LIMIT 1",
306 array($mode, bigintval($data['id'])), __FUNCTION__, __LINE__);
309 // "Translates" the index number into an assosiative value
310 function yoomediaTranslateIndex ($type, $index) {
311 // Default is the index
314 // Is the element there?
315 if (isset($GLOBALS['translation_tables']['yoomedia'][$type][$index])) {
317 $return = $GLOBALS['translation_tables']['yoomedia'][$type][$index];
320 logDebugMessage(__FUNCTION__, __LINE__, "type={$type},index={$index} not found.");
327 // "Translate" error code
328 function translateYooMediaError ($errorCode) {
329 // Default is 'failed'
330 $return = 'failed (Code: ' . $errorCode . ')';
332 // Is the entry there?
333 if (isset($GLOBALS['translation_tables']['yoomedia']['error_codes'][$errorCode])) {
335 $return = $GLOBALS['translation_tables']['yoomedia']['error_codes'][$errorCode];
337 // Log missing entries
338 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Unknown error code <strong>%s[%s]</strong> detected.", $errorCode, gettype($errorCode)));
345 // Checks if the mail id is in reload lock
346 function YOOMEDIA_CHECK_RELOAD ($id, $reload, $type) {
347 // Default is not in reload lock
351 $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",
352 array($type, bigintval($id)), __FUNCTION__, __LINE__);
355 if (SQL_NUMROWS($result) == 1) {
357 list($id, $time) = SQL_FETCHROW($result);
359 // Are we ready to sent again?
360 if (((time() - $time) >= ($reload * 60*60)) && ($time > 0)) {
362 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_yoomedia_reload` WHERE `id`=%s LIMIT 1",
363 array($id), __FUNCTION__, __LINE__);
365 // Dont' sent again this mail
371 SQL_FREERESULT($result);
377 // Lock given mail down for reload lock
378 function YOOMEDIA_RELOAD_LOCK ($data, $mode) {
379 // Search for the entry
380 if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
381 // Convert mode for mails
382 $mode = YOOMEDIA_CONVERT_MODE($mode);
385 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_yoomedia_reload` (`type`,`y_id`,`y_reload`) VALUES ('%s',%s,%s)",
386 array($mode, bigintval($data['id']), bigintval($data['reload'])), __FUNCTION__, __LINE__);
390 // Convert mode for mails
391 function YOOMEDIA_CONVERT_MODE ($mode) {
392 // Convert mode for normal/html
407 // Extract code from response
408 function YOOMEDIA_GET_ERRORCODE_FROM_RESULT ($response) {
409 // Bad code as default
412 // Which response should we parse?
413 if ((isset($response[8])) && (count($response) == 9)) {
414 // Use error code from element 8 (mostly API errors)
415 $codeArray = explode('<br>', $response[8]);
417 // Use only the first element
418 $code = bigintval($codeArray[0]);
419 } elseif ((is_array($response[0])) && (isset($response[0]['id']))) {
420 // Begin with extraction
421 $codeArray = explode(' ', $response[0]['id']);
422 $code = $codeArray[0];
423 $codeArray = explode('<br />', $code);
424 $code = $codeArray[0];
425 $codeArray = explode('<br>', $code);
426 $code = $codeArray[0];
428 // Remove all new-line characters
429 $codeArray = explode("\n", $code);
430 $code = $codeArray[0];
432 // Remove carrige-return
433 $code = str_replace("\n", '', $code);
436 // Should not happen!
437 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot parse response. Raw response:<pre>' . print_r($response, true) . '</pre>');
440 // Fix empty code to bad