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 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2011 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Queries the given Yoo!Media API 2.0 script
44 function YOOMEDIA_QUERY_API ($script, $countQuery = true) {
45 // Init response array
48 // Enougth queries left?
49 if ((getConfig('yoomedia_requests_remain') > 0) || ($countQuery === false)) {
50 // Prepare request array
52 'id' => getConfig('yoomedia_id'),
53 'sid' => getConfig('yoomedia_sid'),
54 'pw' => getConfig('yoomedia_passwd'),
55 'reload' => getConfig('yoomedia_tm_max_reload'),
56 'ma' => getConfig('yoomedia_tm_min_wait'),
57 'uebrig' => getConfig('yoomedia_tm_clicks_remain'),
58 'verguetung' => getConfig('yoomedia_tm_min_pay'),
59 'erotik' => getConfig('yoomedia_erotic_allowed')
63 $response = sendGetRequest('http://www.yoomedia.de/interface_2.0/' . $script, $requestData, true);
65 // Convert from ISO to UTF-8 only if count is > 3 because <= 3 means timeout
66 if (count($response) > 3) {
67 // Convert all lines to UTF-8
68 foreach ($response as $k => $v) {
70 $response[$k] = iconv('windows-1252', 'UTF-8//TRANSLIT', $v);
74 // Shall we count the query as used?
75 if ($countQuery === true) {
76 // Then update the config!
77 updateConfiguration('yoomedia_requests_remain', 1, '-');
85 // Test if the extension settings did work
86 function YOOMEDIA_TEST_CONFIG ($data) {
93 // Transfer config data
96 // Temporary allow maximum
97 setConfigEntry('yoomedia_tm_max_reload' , 100000);
98 setConfigEntry('yoomedia_tm_min_wait' , 0);
99 setConfigEntry('yoomedia_tm_clicks_remain', 10);
100 setConfigEntry('yoomedia_tm_min_pay' , 0);
101 setConfigEntry('yoomedia_erotic_allowed' , 1);
103 // Query the API with a test request without couting it
104 // If zero reply comes back the data is invalid!
105 $response = YOOMEDIA_QUERY_API('out_textmail.php', true); // @TODO Ask Yoo!Media for test script
107 // Default error code is 0 = all fine!
108 $errorCode = YOOMEDIA_GET_ERRORCODE_FROM_RESULT($response);
110 // Log the response if failed
111 if ((count($response) == 0) && ($errorCode > 0)) {
112 // Queries depleted (as we count here!)
113 logDebugMessage(__FUNCTION__, __LINE__, 'Requested depleted. Maxmimum was: ' . getConfig('yoomedia_requests_total'));
115 } elseif ((($errorCode <= 4) && ($errorCode > 0)) || ($errorCode >= 8)) {
116 // An error has returned from the account
117 logDebugMessage(__FUNCTION__, __LINE__, 'Unexpected error code ' . $errorCode . ' received.');
118 } elseif ((count($response) > 0) && ($errorCode != 0)) {
119 // Log serialized raw response
120 logDebugMessage(__FUNCTION__, __LINE__, 'Raw response=' . base64_encode(serialize($response)));
123 // This is fine, because the result array is okay and the response code on element 8 is fine
127 // Do we have some data there?
128 return ($errorCode == '0');
131 // "Getter" for a parsed result for all text mails. This means an array without
132 // the header lines will be returned
133 function YOOMEDIA_GET_PARSED_RESULT_TEXTMAILS () {
134 // Get the raw response
135 $response = YOOMEDIA_QUERY_API('out_textmail.php');
137 // Parse the response
138 $result = YOOMEDIA_PARSE_RESPONSE($response, 'textmail');
144 // Parser function for Yoo!Media API responses
145 function YOOMEDIA_PARSE_RESPONSE ($response, $type) {
149 // Cut off the header
150 $dummy = removeHttpHeaderFromResponse($response);
152 // If we have no result, abort here
153 if (count($dummy) == 0) {
154 // Empty response from API
155 logDebugMessage(__FUNCTION__, __LINE__, 'Empy result from API received.');
159 // The result is now still raw, so we must split it up and trim spaces away
160 $responseLine = trim(implode("\n", $dummy));
162 // Last line should never be a pipe!
163 if (substr($responseLine, -1, 1) == '|') {
164 $responseLine = substr($responseLine, 0, -1);
167 // Now, explode all in one array
168 $dataArray = explode('|', $responseLine);
170 // Now make the result array with two dimensions
171 $count = '0'; $entry = '0';
172 foreach ($dataArray as $line) {
174 $result[$entry][yoomediaTranslateIndex($type, $count)] = $line;
176 // End of data of first entry reached?
178 // Then advance to next entry and reset counter
191 // Prepares a bonus mail for delivery. Works only if extension 'bonus' is active
192 function YOOMEDIA_PREPARE_MAIL_DELIVERY ($data) {
197 } elseif (!isExtensionActive('bonus')) {
202 // Is the waiting time below one second? Then fix it to one (zero seconds are not yet supported!)
203 if ($data['wait'] < 1) $data['wait'] = 1;
205 // Half of waiting time is a good reward!
206 $data['reward'] = round($data['wait'] / 2 + 0.4);
208 // Is the reward below one?
209 if ($data['reward'] < 1) $data['reward'] = 1;
212 loadTemplate('admin_send_yoomedia', false, $data);
215 // Adds the mail to the bonus mail pool
216 function YOOMEDIA_SEND_BONUS_MAIL ($data, $mode) {
221 } elseif (!isExtensionActive('bonus')) {
226 // Add dummy receiver to avoid notice
227 $data['receiver'] = '0';
229 // HTML or normal? (normal is default...)
231 if (($mode == 'html') && (isExtensionActive('html_mail'))) $type = 'h';
234 $data['url'] = sprintf("http://www.yoomedia.de/code/%s-mail.php?id=%s&sid=%s",
240 // Lock this mail for new delivery
241 YOOMEDIA_RELOAD_LOCK($data, $mode);
243 // Call the lower function
244 addNewBonusMail($data, $mode);
248 function YOOMEDIA_EXCLUDE_MAIL ($data, $mode) {
249 // Search for the entry
250 if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
251 // Convert mode for mails
252 $mode = YOOMEDIA_CONVERT_MODE($mode);
255 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_yoomedia_reload` (`type`,`y_id`,`y_reload`,`inserted`) VALUES ('%s',%s,%s,'0000-00-00 00:00')",
258 bigintval($data['id']),
259 bigintval($data['reload'])
260 ), __FUNCTION__, __LINE__);
264 // Remove lock of given mail
265 function YOOMEDIA_UNLIST_MAIL ($data, $mode) {
266 // Convert mode for mails
267 $mode = YOOMEDIA_CONVERT_MODE($mode);
270 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_yoomedia_reload` WHERE `type`='%s' AND `y_id`=%s LIMIT 1",
271 array($mode, bigintval($data['id'])), __FUNCTION__, __LINE__);
274 // "Translates" the index number into an assosiative value
275 function yoomediaTranslateIndex ($type, $index) {
276 // Default is the index
279 // Is the element there?
280 if (isset($GLOBALS['translation_tables']['yoomedia'][$type][$index])) {
282 $return = $GLOBALS['translation_tables']['yoomedia'][$type][$index];
285 logDebugMessage(__FUNCTION__, __LINE__, 'type=' . $type . ',index=' . $index . ' not found');
292 // "Translate" error code
293 function translateYooMediaError ($errorCode) {
294 // Default is 'failed'
295 $return = 'failed (Code: ' . $errorCode . ')';
297 // Is the entry there?
298 if (isset($GLOBALS['translation_tables']['yoomedia']['error_codes'][$errorCode])) {
300 $return = $GLOBALS['translation_tables']['yoomedia']['error_codes'][$errorCode];
302 // Log missing entries
303 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Unknown error code <strong>%s[%s]</strong> detected.", $errorCode, gettype($errorCode)));
310 // Checks if the mail id is in reload lock
311 function YOOMEDIA_CHECK_RELOAD ($id, $reload, $type) {
312 // Default is not in reload lock
316 $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",
317 array($type, bigintval($id)), __FUNCTION__, __LINE__);
320 if (SQL_NUMROWS($result) == 1) {
322 list($id, $time) = SQL_FETCHROW($result);
324 // Are we ready to sent again?
325 if (((time() - $time) >= ($reload * 60*60)) && ($time > 0)) {
327 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_yoomedia_reload` WHERE `id`=%s LIMIT 1",
328 array($id), __FUNCTION__, __LINE__);
330 // Dont' sent again this mail
336 SQL_FREERESULT($result);
342 // Lock given mail down for reload lock
343 function YOOMEDIA_RELOAD_LOCK ($data, $mode) {
344 // Search for the entry
345 if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
346 // Convert mode for mails
347 $mode = YOOMEDIA_CONVERT_MODE($mode);
350 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_yoomedia_reload` (`type`,`y_id`,`y_reload`) VALUES ('%s',%s,%s)",
351 array($mode, bigintval($data['id']), bigintval($data['reload'])), __FUNCTION__, __LINE__);
355 // Convert mode for mails
356 function YOOMEDIA_CONVERT_MODE ($mode) {
357 // Convert mode for normal/html
372 // Extract code from response
373 function YOOMEDIA_GET_ERRORCODE_FROM_RESULT ($response) {
374 // The response must be an array
375 assert(is_array($response));
377 // Bad code as default
380 // Which response should we parse?
381 if ((isset($response[8])) && (count($response) == 9)) {
382 // Use error code from element 8 (mostly API errors)
383 $codeArray = explode('<br>', $response[8]);
385 // Use only the first element
386 $code = bigintval($codeArray[0]);
387 } elseif (!empty($response[0])) {
388 // Begin with extraction
389 $codeArray = explode(' ', $response[0]['id']);
390 $code = $codeArray[0];
391 $codeArray = explode('<br />', $code);
392 $code = $codeArray[0];
393 $codeArray = explode('<br>', $code);
394 $code = $codeArray[0];
396 // Remove all new-line characters
397 $codeArray = explode("\n", $code);
398 $code = $codeArray[0];
400 // Remove carrige-return
401 $code = str_replace("\n", '', $code);
402 } elseif (count($response) == 0) {
403 // All fine, but empty result
406 // Should not happen!
407 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot parse response. Raw response:<pre>' . print_r($response, true) . '</pre>');
410 // Fix empty code to bad