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('windows-1252', 'UTF-8//TRANSLIT', $v);
121 // iconv()-less ISO-8859-1 -> UTF-8
122 $response[$k] = preg_replace(
124 "chr(0xC0|ord('\\1')>>6).chr(0x80|ord('\\1')&0x3F)",
130 // Shall we count the query as used?
131 if ($countQuery === true) {
132 // Then update the config!
133 updateConfiguration('yoomedia_requests_remain', 1, '-');
141 // "Getter" for a parsed result for all text mails. This means an array without
142 // the header lines will be returned
143 function YOOMEDIA_GET_PARSED_RESULT_TEXTMAILS () {
144 // Get the raw response
145 $response = YOOMEDIA_QUERY_API('out_textmail.php');
147 // Parse the response
148 $result = YOOMEDIA_PARSE_RESPONSE($response, 'textmail');
154 // Parser function for Yoo!Media API responses
155 function YOOMEDIA_PARSE_RESPONSE ($response, $type) {
159 // Cut off the header
161 foreach ($response as $line) {
165 // Is this line empty?
172 // The result is now still raw, so we must split it up and trim spaces away
173 $responseLine = trim(implode("\n", $dummy));
175 // Last line should never be a pipe!
176 if (substr($responseLine, -1, 1) == '|') $responseLine = substr($responseLine, 0, -1);
178 // Now, explode all in one array
179 $dataArray = explode('|', $responseLine);
181 // Now make the result array with two dimensions
182 $cnt = 0; $entry = 0;
183 foreach ($dataArray as $line) {
185 $result[$entry][yoomediaTranslateIndex($type, $cnt)] = $line;
187 // End of data of first entry reached?
189 // Then advance to next entry and reset counter
202 // Prepares a bonus mail for delivery. Works only if extension 'bonus' is active
203 function YOOMEDIA_PREPARE_MAIL_DELIVERY ($data) {
208 } elseif (!isExtensionActive('bonus')) {
213 // Is the waiting time below one second? Then fix it to one (zero seconds are not yet supported!)
214 if ($data['wait'] < 1) $data['wait'] = 1;
216 // Half of waiting time is a good reward!
217 $data['reward'] = round($data['wait'] / 2 + 0.4);
219 // Is the reward below one?
220 if ($data['reward'] < 1) $data['reward'] = 1;
223 $data['sid'] = getConfig('yoomedia_sid');
225 // Add total receivers
226 $data['all'] = translateComma(getTotalReceivers());
229 $data['categories'] = generateCategoryOptionsList('normal');
232 $data['text'] = decodeEntities($data['text']);
235 loadTemplate('admin_send_yoomedia', false, $data);
238 // Adds the mail to the bonus mail pool
239 function YOOMEDIA_SEND_BONUS_MAIL ($data, $mode) {
244 } elseif (!isExtensionActive('bonus')) {
249 // Add dummy receiver to avoid notice
250 $data['receiver'] = 0;
252 // HTML or normal? (normal is default...)
254 if (($mode == 'html') && (isExtensionActive('html_mail'))) $type = 'h';
257 $data['url'] = sprintf("http://www.yoomedia.de/code/%s-mail.php?id=%s&sid=%s",
263 // Lock this mail for new delivery
264 YOOMEDIA_RELOAD_LOCK($data, $mode);
266 // Call the lower function
267 addNewBonusMail($data, $mode);
271 function YOOMEDIA_EXCLUDE_MAIL ($data, $mode) {
272 // Search for the entry
273 if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
274 // Convert mode for mails
275 $mode = YOOMEDIA_CONVERT_MODE($mode);
278 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_yoomedia_reload` (`type`,`y_id`,`y_reload`,`inserted`) VALUES ('%s',%s,%s,'0000-00-00 00:00')",
281 bigintval($data['id']),
282 bigintval($data['reload'])
283 ), __FUNCTION__, __LINE__);
287 // Remove lock of given mail
288 function YOOMEDIA_UNLIST_MAIL ($data, $mode) {
289 // Convert mode for mails
290 $mode = YOOMEDIA_CONVERT_MODE($mode);
293 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_yoomedia_reload` WHERE `type`='%s' AND `y_id`=%s LIMIT 1",
294 array($mode, bigintval($data['id'])), __FUNCTION__, __LINE__);
297 // "Translates" the index number into an assosiative value
298 function yoomediaTranslateIndex ($type, $index) {
299 // Default is the index
302 // Is the element there?
303 if (isset($GLOBALS['translation_tables']['yoomedia'][$type][$index])) {
305 $return = $GLOBALS['translation_tables']['yoomedia'][$type][$index];
308 logDebugMessage(__FUNCTION__, __LINE__, "type={$type},index={$index} not found.");
315 // "Translate" error code
316 function translateYooMediaError ($errorCode) {
317 // Default is 'failed'
318 $return = 'failed (Code: ' . $errorCode . ')';
320 // Is the entry there?
321 if (isset($GLOBALS['translation_tables']['yoomedia']['error_codes'][$errorCode])) {
323 $return = $GLOBALS['translation_tables']['yoomedia']['error_codes'][$errorCode];
325 // Log missing entries
326 debug_report_bug(sprintf("Unknown error code <strong>%s</strong> detected.", $errorCode));
333 // Checks if the mail id is in reload lock
334 function YOOMEDIA_CHECK_RELOAD ($id, $reload, $type) {
335 // Default is not in reload lock
339 $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",
340 array($type, bigintval($id)), __FUNCTION__, __LINE__);
343 if (SQL_NUMROWS($result) == 1) {
345 list($id, $time) = SQL_FETCHROW($result);
347 // Are we ready to sent again?
348 if (((time() - $time) >= ($reload * 60*60)) && ($time > 0)) {
350 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_yoomedia_reload` WHERE `id`=%s LIMIT 1",
351 array($id), __FUNCTION__, __LINE__);
353 // Dont' sent again this mail
359 SQL_FREERESULT($result);
365 // Lock given mail down for reload lock
366 function YOOMEDIA_RELOAD_LOCK ($data, $mode) {
367 // Search for the entry
368 if (YOOMEDIA_CHECK_RELOAD($data['id'], $data['reload'], $mode) === false) {
369 // Convert mode for mails
370 $mode = YOOMEDIA_CONVERT_MODE($mode);
373 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_yoomedia_reload` (`type`,`y_id`,`y_reload`) VALUES ('%s',%s,%s)",
374 array($mode, bigintval($data['id']), bigintval($data['reload'])), __FUNCTION__, __LINE__);
378 // Convert mode for mails
379 function YOOMEDIA_CONVERT_MODE ($mode) {
380 // Convert mode for normal/html
395 // Extract code from response
396 function YOOMEDIA_GET_ERRORCODE_FROM_RESULT ($response) {
397 // Bad code as default
400 // Which response should we parse?
401 if ((isset($response[8])) && (count($response) == 9)) {
402 // Use error code from element 8 (mostly API errors)
403 $codeArray = explode("<br>", $response[8]);
405 // Use only the first element
406 $code = bigintval($codeArray[0]);
407 } elseif ((is_array($response[0])) && (isset($response[0]['id']))) {
408 // Begin with extraction
409 $codeArray = explode(" ", $response[0]['id']);
410 $code = $codeArray[0];
411 $codeArray = explode("<br />", $code);
412 $code = $codeArray[0];
413 $codeArray = explode("<br>", $code);
414 $code = $codeArray[0];
416 // Remove all new-line characters
417 $codeArray = explode("\n", $code);
418 $code = $codeArray[0];
420 // Remove carrige-return
421 $code = str_replace("\n", '', $code);
423 // Should not happen!
424 logDebugMessage(__FUNCTION__, __LINE__, "Cannot parse response. Raw response=" . base64_encode(serialize($response)));