2 /************************************************************************
3 * MXChange v0.2.1 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 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
44 // Test if the extension settings did work
45 function YOOMEDIA_TEST_CONFIG ($data) {
52 // Transfer config data
55 // Temporary allow maximum
56 setConfigEntry('yoomedia_tm_max_reload' , 100000);
57 setConfigEntry('yoomedia_tm_min_wait' , 0);
58 setConfigEntry('yoomedia_tm_clicks_remain', 10);
59 setConfigEntry('yoomedia_tm_min_pay' , 0);
60 setConfigEntry('yoomedia_erotic_allowed' , 1);
62 // Query the API with a test request without couting it
63 // If zero reply comes back the data is invalid!
64 $response = YOOMEDIA_QUERY_API('out_textmail.php', true); // @TODO Ask Yoo!Media for test script
66 // Default error code is 0 = all fine!
67 $errorCode = YOOMEDIA_GET_ERRORCODE_FROM_RESULT($response);
69 // Log the response if failed
70 if (count($response) == 0) {
71 // Queries depleted (as we count here!)
72 logDebugMessage(__FUNCTION__, __LINE__, 'Requested depleted. Maxmimum was: ' . getConfig('yoomedia_requests_total'));
74 } elseif (!isset($response[8])) {
76 logDebugMessage(__FUNCTION__, __LINE__, 'Missing response line [8]. Raw response=' . base64_encode(serialize($response)));
78 } elseif ((($errorCode <= 4) && ($errorCode > 0)) || ($errorCode >= 8)) {
79 // An error has returned from the account
80 logDebugMessage(__FUNCTION__, __LINE__, 'Unexpected error code ' . $errorCode . ' received.');
81 } elseif (count($response) < 9) {
82 // Log serialized raw response
83 logDebugMessage(__FUNCTION__, __LINE__, 'Raw response=' . base64_encode(serialize($response)));
86 // This is fine, because the result array is okay and the response code on element 8 is fine
90 // Do we have some data there?
91 return ($errorCode == 0);
94 // Queries the given Yoo!Media API 2.0 script
95 function YOOMEDIA_QUERY_API ($script, $countQuery = true) {
96 // Init response array
99 // Enougth queries left?
100 if ((getConfig('yoomedia_requests_remain') > 0) || ($countQuery === false)) {
101 // Prepare the low-level request
102 $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",
104 getConfig('yoomedia_id'),
105 getConfig('yoomedia_sid'),
106 getConfig('yoomedia_passwd'),
107 getConfig('yoomedia_tm_max_reload'),
108 getConfig('yoomedia_tm_min_wait'),
109 getConfig('yoomedia_tm_clicks_remain'),
110 getConfig('yoomedia_tm_min_pay'),
111 getConfig('yoomedia_erotic_allowed')
115 $response = sendGetRequest($requestString);
117 // Convert from ISO to UTF-8
118 foreach ($response as $k => $v) {
119 $response[$k] = iconv('ISO-8859-1', 'UTF-8//TRANSLIT',$v);
122 // Shall we count the query as used?
123 if ($countQuery === true) {
124 // Then update the config!
125 updateConfiguration('yoomedia_requests_remain', 1, '-');
133 // "Getter" for a parsed result for all text mails. This means an array without
134 // the header lines will be returned
135 function YOOMEDIA_GET_PARSED_RESULT_TEXTMAILS () {
136 // Get the raw response
137 $response = YOOMEDIA_QUERY_API('out_textmail.php');
139 // Parse the response
140 $result = YOOMEDIA_PARSE_RESPONSE($response, 'textmail');
146 // Parser function for Yoo!Media API responses
147 function YOOMEDIA_PARSE_RESPONSE ($response, $type) {
151 // Cut off the header
153 foreach ($response as $line) {
157 // Is this line empty?
164 // The result is now still raw, so we must split it up and trim spaces away
165 $responseLine = trim(implode("\n", $dummy));
167 // Last line should never be a pipe!
168 if (substr($responseLine, -1, 1) == '|') $responseLine = substr($responseLine, 0, -1);
170 // Now, explode all in one array
171 $dataArray = explode('|', $responseLine);
173 // Now make the result array with two dimensions
174 $cnt = 0; $entry = 0;
175 foreach ($dataArray as $line) {
177 $result[$entry][yoomediaTranslateIndex($type, $cnt)] = $line;
179 // End of data of first entry reached?
181 // Then advance to next entry and reset counter
194 // Prepares a bonus mail for delivery. Works only if extension 'bonus' is active
195 function YOOMEDIA_PREPARE_MAIL_DELIVERY ($data) {
200 } elseif (!isExtensionActive('bonus')) {
205 // Is the waiting time below one second? Then fix it to one (zero seconds are not yet supported!)
206 if ($data['wait'] < 1) $data['wait'] = 1;
208 // Half of waiting time is a good reward!
209 $data['reward'] = round($data['wait'] / 2 + 0.4);
211 // Is the reward below one?
212 if ($data['reward'] < 1) $data['reward'] = 1;
215 $data['sid'] = getConfig('yoomedia_sid');
217 // Add total receivers
218 $data['all'] = translateComma(getTotalReceivers());
221 $data['categories'] = generateCategoryOptionsList('normal');
224 $data['text'] = (decodeEntities($data['text']));
227 loadTemplate('admin_send_yoomedia', false, $data);
230 // Adds the mail to the bonus mail pool
231 function YOOMEDIA_SEND_BONUS_MAIL ($data, $mode) {
236 } elseif (!isExtensionActive('bonus')) {
241 // Add dummy receiver to avoid notice
242 $data['receiver'] = 0;
244 // HTML or normal? (normal is default...)
246 if (($mode == 'html') && (isExtensionActive('html_mail'))) $type = 'h';
249 $data['url'] = sprintf("http://www.yoomedia.de/code/%s-mail.php?id=%s&sid=%s",
255 // Lock this mail for new delivery
256 YOOMEDIA_RELOAD_LOCK($data, $mode);
258 // Call the lower function
259 addNewBonusMail($data, $mode);
263 function YOOMEDIA_EXCLUDE_MAIL ($data, $mode) {
264 // Search for the entry
265 if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
266 // Convert mode for mails
267 $mode = YOOMEDIA_CONVERT_MODE($mode);
270 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_yoomedia_reload` (`type`,`y_id`,`y_reload`,`inserted`) VALUES ('%s',%s,%s,'0000-00-00 00:00')",
273 bigintval($data['id']),
274 bigintval($data['reload'])
275 ), __FUNCTION__, __LINE__);
279 // Remove lock of given mail
280 function YOOMEDIA_UNLIST_MAIL ($data, $mode) {
281 // Convert mode for mails
282 $mode = YOOMEDIA_CONVERT_MODE($mode);
285 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_yoomedia_reload` WHERE `type`='%s' AND `y_id`=%s LIMIT 1",
286 array($mode, bigintval($data['id'])), __FUNCTION__, __LINE__);
289 // "Translates" the index number into an assosiative value
290 function yoomediaTranslateIndex ($type, $index) {
291 // Default is the index
294 // Is the element there?
295 if (isset($GLOBALS['translation_tables']['yoomedia'][$type][$index])) {
297 $return = $GLOBALS['translation_tables']['yoomedia'][$type][$index];
300 logDebugMessage(__FUNCTION__, __LINE__, "type={$type},index={$index} not found.");
307 // "Translate" error code
308 function translateYooMediaError ($errorCode) {
309 // Default is 'failed'
310 $return = 'failed (Code: ' . $errorCode . ')';
312 // Is the entry there?
313 if (isset($GLOBALS['translation_tables']['yoomedia']['error_codes'][$errorCode])) {
315 $return = $GLOBALS['translation_tables']['yoomedia']['error_codes'][$errorCode];
317 // Log missing entries
318 logDebugMessage(__FUNCTION__, __LINE__, "errorCode={$errorCode}");
320 debug_print_backtrace();
328 // Checks if the mail id is in reload lock
329 function YOOMEDIA_CHECK_RELOAD ($id, $reload, $type) {
330 // Default is not in reload lock
334 $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",
335 array($type, bigintval($id)), __FUNCTION__, __LINE__);
338 if (SQL_NUMROWS($result) == 1) {
340 list($id, $time) = SQL_FETCHROW($result);
342 // Are we ready to sent again?
343 if (((time() - $time) >= ($reload * 60*60)) && ($time > 0)) {
345 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_yoomedia_reload` WHERE `id`=%s LIMIT 1",
346 array($id), __FUNCTION__, __LINE__);
348 // Dont' sent again this mail
354 SQL_FREERESULT($result);
360 // Lock given mail down for reload lock
361 function YOOMEDIA_RELOAD_LOCK ($data, $mode) {
362 // Search for the entry
363 if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
364 // Convert mode for mails
365 $mode = YOOMEDIA_CONVERT_MODE($mode);
368 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_yoomedia_reload` (`type`,`y_id`,`y_reload`) VALUES ('%s',%s,%s)",
369 array($mode, bigintval($data['id']), bigintval($data['reload'])), __FUNCTION__, __LINE__);
373 // Convert mode for mails
374 function YOOMEDIA_CONVERT_MODE ($mode) {
375 // Convert mode for normal/html
390 // Extract code from response
391 function YOOMEDIA_GET_ERRORCODE_FROM_RESULT ($response) {
392 // Bad code as default
395 // Which response should we parse?
396 if ((isset($response[8])) && (count($response) == 9)) {
397 // Use error code from element 8 (mostly API errors)
398 $codeArray = explode("<br>", $response[8]);
400 // Use only the first element
401 $code = bigintval($codeArray[0]);
402 } elseif ((is_array($response[0])) && (isset($response[0]['id']))) {
403 // Begin with extraction
404 $codeArray = explode(" ", $response[0]['id']);
405 $code = $codeArray[0];
406 $codeArray = explode("<br />", $code);
407 $code = $codeArray[0];
408 $codeArray = explode("<br>", $code);
409 $code = $codeArray[0];
411 // Remove all new-line characters
412 $codeArray = explode("\n", $code);
413 $code = $codeArray[0];
415 // Remove carrige-return
416 $code = str_replace("\n", '', $code);
418 // Should not happen!
419 logDebugMessage(__FUNCTION__, __LINE__, "Cannot parse response. Raw response=" . base64_encode(serialize($response)));