Updated copyright year.
[mailer.git] / inc / modules / member / what-payout.php
index 75125e018964b7247ed87d06834cbae5e7388853..8cc6bc730de05b1ba390de38456cdb85673b2830 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 04/12/2004 *
- * ================                             Last change: 12/01/2008 *
+ * Mailer v0.2.1-FINAL                                Start: 04/12/2004 *
+ * ===================                          Last change: 12/01/2008 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : what-payout.php                                  *
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Auszahlungsanfragen                              *
  * -------------------------------------------------------------------- *
- *                                                                      *
+ * $Revision::                                                        $ *
+ * $Date::                                                            $ *
+ * $Tag:: 0.2.1-FINAL                                                 $ *
+ * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
- * Copyright (c) 2003 - 2008 by Roland Haeder                           *
- * For more information visit: http://www.mxchange.org                  *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009 - 2016 by Mailer Developer Team                   *
+ * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
 // Some security stuff...
 if (!defined('__SECURITY')) {
        // Don't call this directly!
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
-       require($INC);
-} elseif (!IS_MEMBER()) {
+       exit();
+} elseif (!isMember()) {
        // Not logged in
-       LOAD_URL("modules.php?module=index");
-} elseif ((!EXT_IS_ACTIVE("payout")) && (!IS_ADMIN())) {
-       // Extension "payout" is not active
-       ADD_FATAL(EXTENSION_PROBLEM_EXT_INACTIVE, "payout");
-       return;
+       redirectToIndexMemberOnlyModule();
 }
 
 // Add description as navigation point
-ADD_DESCR("member", basename(__FILE__));
-
-$result_depths = SQL_QUERY("SELECT level, percents FROM "._MYSQL_PREFIX."_refdepths ORDER BY level", __FILE__, __LINE__);
-$TPTS = 0;
-while (list($lvl, $per) = SQL_FETCHROW($result_depths))
-{
-       // Load referal points
-       $result_points = SQL_QUERY_ESC("SELECT points FROM "._MYSQL_PREFIX."_user_points WHERE userid=%s AND ref_depth=%d LIMIT 1",
-        array($GLOBALS['userid'], bigintval($lvl)), __FILE__, __LINE__);
-       if (SQL_NUMROWS($result_points) == 1)
-       {
-               list($points) = SQL_FETCHROW($result_points);
-               SQL_FREERESULT($result_points);
-               $TPTS += $points;
-       }
-}
+addYouAreHereLink('member', __FILE__);
 
-// Free memory
-SQL_FREERESULT($result_depths);
-
-// Get used points
-$USED = GET_TOTAL_DATA($GLOBALS['userid'], "user_data", "used_points");
+if ((!isExtensionActive('payout')) && (!isAdmin())) {
+       displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=payout%}');
+       return;
+} // END - if
 
 // Translate point into comma
-$TPTS = TRANSLATE_COMMA($TPTS - $USED);
+$payoutPoints = getPayoutPoints(getMemberId());
 
-// Sanity check...
-if (empty($TPTS)) $TPTS = "0.00000";
+// Output amount
+displayMessage('{%message,MEMBER_TOTAL_PAYOUT_POINTS=' . $payoutPoints . '%}');
 
-if (empty($_GET['payout']))
-{
+// GET parameter set?
+if (!isGetRequestElementSet('payout')) {
        // Load payout types
-       $result = SQL_QUERY_ESC("SELECT id, type, rate, min_points, allow_url
-FROM "._MYSQL_PREFIX."_payout_types
-WHERE %s >= min_points
-ORDER BY type", array(REVERT_COMMA($TPTS)), __FILE__, __LINE__);
-       if (SQL_NUMROWS($result) > 0)
-       {
-               // Free memory
-               SQL_FREERESULT($result);
+       $result = sqlQueryEscaped('SELECT
+       `id`,
+       `type`,
+       `rate`,
+       `min_points`,
+       `allow_url`
+FROM
+       `{?_MYSQL_PREFIX?}_payout_types`
+WHERE
+       %s >= `min_points`
+ORDER BY
+       `type` ASC',
+               array($payoutPoints), __FILE__, __LINE__);
 
+       // Some entries found?
+       if (!ifSqlHasZeroNumRows($result)) {
                // Check for his payouts
-               $result_payouts = SQL_QUERY_ESC("SELECT DISTINCT p.id, p.payout_total, p.target_account, p.target_bank, t.type, p.payout_timestamp, p.status, t.allow_url AS allow, p.target_url AS url, p.link_text AS alt, p.banner_url AS bannerm
-FROM "._MYSQL_PREFIX."_user_payouts AS p
-LEFT JOIN "._MYSQL_PREFIX."_payout_types AS t
-ON p.payout_id = t.id
-WHERE p.userid = %s
-ORDER BY p.payout_timestamp DESC",
-                array($GLOBALS['userid']), __FILE__, __LINE__);
-               if (SQL_NUMROWS($result_payouts) > 0)
-               {
+               $result_payouts = sqlQueryEscaped('SELECT
+       `p`.`id`,
+       `p`.`payout_total`,
+       `p`.`target_account`,
+       `p`.`target_bank`,
+       `t`.`type`,
+       `p`.`payout_timestamp`,
+       `p`.`status`,
+       `t`.`allow_url`,
+       `p`.`target_url`,
+       `p`.`link_text`,
+       `p`.`banner_url`
+FROM
+       `{?_MYSQL_PREFIX?}_user_payouts` AS `p`
+LEFT JOIN
+       `{?_MYSQL_PREFIX?}_payout_types` AS `t`
+ON
+       `p`.`payout_id`=`t`.`id`
+WHERE
+       `p`.`userid`=%s
+ORDER BY
+       `p`.`payout_timestamp` DESC',
+                       array(getMemberId()), __FILE__, __LINE__);
+               if (!ifSqlHasZeroNumRows($result_payouts)) {
                        // List all his requests
-                       $SW = 2; $OUT = "";
-                       while (list($pid, $total, $account, $bank, $type, $tstamp, $status, $allow, $url, $alt, $banner) = SQL_FETCHROW($result_payouts))
-                       {
-                               // Translate status
-                               $evl = "\$status = PAYOUT_MEMBER_STATUS_".strtoupper($status).";";
-                               eval($evl);
-                               $status = "<FONT class=\"member_failed\">".$status."</FONT>";
-
+                       $OUT = '';
+                       while ($content = sqlFetchArray($result_payouts)) {
                                // Nothing entered must be secured in member/what-payputs.php !
-                               if ($allow == "Y")
-                               {
+                               if ($content['allow_url'] == 'Y') {
                                        // Banner/Textlink views/clicks request
-                                       if (!empty($banner))
-                                       {
+                                       if (!empty($content['banner'])) {
                                                // Banner
-                                               $account = "<IMG src=\"".$banner."\" alt=\"".$alt."\" title=\"".$alt."\" border=\"0\">";
-                                       }
-                                        else
-                                       {
+                                               $content['target_account'] = '<img src="' . $content['banner'] . '" alt="' . $content['link_text'] . '" title="' . $content['link_text'] . '" border="0" />';
+                                       } else {
                                                // Textlink
-                                               $account = $alt;
+                                               $content['target_account'] = $content['link_text'];
                                        }
-                                       $bank = "<A href=\"".DEREFERER($url)."\" target=\"_blank\">".CLICK_HERE."</A>";
-                               }
-                                else
-                               {
-                                       // e-currency payout request
-                                       if (empty($account)) $account = "---";
-                                       if (empty($bank))    $bank    = "---";
-                               }
+                                       $content['target_bank'] = '<a href="{%pipe,generateDereferrerUrl=' . $content['target_url'] . '%}" target="_blank">{--CLICK_HERE--}</a>';
+                               } // END - if
 
                                // Prepare data for the template
-                               $content = array(
-                                       'sw'     => $SW,
-                                       'acc'    => $account,
-                                       'points' => TRANSLATE_COMMA($total)." ".COMPILE_CODE($type),
-                                       'bank'   => $bank,
-                                       'stamp'  => MAKE_DATETIME($tstamp, "2"),
-                                       'status' => $status
-                               );
-                               // Load row template and switch colors
-                               $OUT .= LOAD_TEMPLATE("member_payout_row", true, $content);
-                               $SW = 3 - $SW;
-                       }
+                               $content['payout_timestamp'] = generateDateTime($content['payout_timestamp'], 2);
 
-                       // Remember rows in constant
-                       define('__PAYOUT_ROWS', $OUT);
+                               // Load row template and switch colors
+                               $OUT .= loadTemplate('member_payout_row', TRUE, $content);
+                       } // END - while
 
                        // Load template
-                       LOAD_TEMPLATE("member_payout");
-               }
+                       loadTemplate('member_payout', FALSE, $OUT);
+               } // END - if
 
                // Free memory
-               SQL_FREERESULT($result_payouts);
+               sqlFreeResult($result_payouts);
 
                // Output payout list
-               PAYOUT_OUTPUT_PAYOUT_LIST($TPTS);
+               outputPayoutList($payoutPoints);
+       } else {
+               // No payout types setup
+               displayMessage('{--MEMBER_PAYOUT_SETUP_INCOMPLETE_LOW_POINTS--}');
        }
-}
- else
-{
+
+       // Free memory
+       sqlFreeResult($result);
+} else {
        // Chedk if he can get paid by selected type
-       $result = SQL_QUERY_ESC("SELECT type, rate, min_points, allow_url FROM "._MYSQL_PREFIX."_payout_types WHERE id=%s LIMIT 1",
-        array(bigintval($_GET['payout'])), __FILE__, __LINE__);
+       $result = sqlQueryEscaped('SELECT
+       `type`,
+       `rate`,
+       `min_points`,
+       `allow_url`
+FROM
+       `{?_MYSQL_PREFIX?}_payout_types`
+WHERE
+       `id`=%s
+LIMIT 1',
+               array(bigintval(getRequestElement('payout'))), __FILE__, __LINE__);
 
-       if (SQL_NUMROWS($result) == 1)
-       {
-               // ID is valid
-               list($type, $rate, $min, $allow) = SQL_FETCHROW($result);
-               SQL_FREERESULT($result);
+       if (sqlNumRows($result) == 1) {
+               // id is valid so load the data
+               $content = sqlFetchArray($result);
 
                // Calculate maximum value
-               $max = round($TPTS * $rate - 0.5);
+               $max     = round($payoutPoints * $content['rate'] - 0.5);
+               $PAY_MAX = '0';
 
                // Calulcate points from submitted amount
-               $PAYOUT = 0;
-               if (!empty($_POST['payout']))
-               {
-                       $PAYOUT  = bigintval($_POST['payout']) / $rate;
-                       $PAY_MAX = $max / $rate;
-               }
-
-               // Move variables into constants for templates
-               define('PAYOUT_MAX_VALUE' , $max);
-               define('PAYOUT_TYPE_VALUE', COMPILE_CODE($type));
+               $points = '0';
+               if (isPostRequestElementSet('payout')) {
+                       $points  = bigintval(postRequestElement('payout')) / $content['rate'];
+                       $PAY_MAX = $max / $content['rate'];
+               } // END - if
 
-               if (REVERT_COMMA($TPTS) >= $min)
-               {
+               // Has enougth points to payout?
+               if ($payoutPoints >= $content['min_points']) {
                        // Ok, he can get be paid
-                       if ((isset($_POST['ok'])) && ($PAYOUT <= $PAY_MAX) && ($PAYOUT >= $min))
-                       {
-                               // Calculate exact value
-                               define('PAYOUT_POINTS_VALUE', $PAYOUT);
+                       if ((isFormSent()) && ($points <= $PAY_MAX) && ($points >= $content['min_points'])) {
+                               // Remember points in array
+                               setPostRequestElement('payout_points', $points);
+                               setPostRequestElement('type'         , $content['type']);
 
-                               // Subtract points from member's account
-                               SUB_POINTS($GLOBALS['userid'], $PAYOUT);
+                               // Subtract points from member's account and ignore return status
+                               subtractPoints('payout', getMemberId(), $points);
 
                                // Add entry to his tranfer history
-                               if ($allow == "Y")
-                               {
+                               if ($content['allow_url'] == 'Y') {
                                        // Banner/textlink ordered
-                                       $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_payouts (userid, payout_total, payout_id, payout_timestamp, status, target_url, link_text, banner_url)
+                                       sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `payout_id`, `payout_timestamp`, `status`, `target_url`, `link_text`, `banner_url`)
 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
- array(
-       $GLOBALS['userid'],
-       bigintval($_POST['payout']),
-       bigintval($_GET['payout']),
-       $_POST['turl'],
-       $_POST['alt'],
-       $_POST['banner']
-), __FILE__, __LINE__);
                                      array(
+                                               getMemberId(),
+                                               bigintval(postRequestElement('payout')),
+                                               bigintval(getRequestElement('payout')),
+                                               postRequestElement('turl'),
+                                               postRequestElement('link_text'),
+                                               postRequestElement('banner')
+                                       ), __FILE__, __LINE__);
 
                                        // Load templates
-                                       $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request_banner", array(), $GLOBALS['userid']);
-                                       if (GET_EXT_VERSION("admins") >= "0.4.1")
-                                       {
-                                               $adm_tpl = "admin_payout_request_banner";
+                                       $message_mem = loadEmailTemplate('member_payout_request_banner', postRequestArray(), getMemberId());
+                                       if (isExtensionInstalledAndNewer('admins', '0.4.1')) {
+                                               $adm_tpl = 'admin_payout_request_banner';
+                                       } else {
+                                               $message_adm = loadEmailTemplate('admin_payout_request_banner', postRequestArray(), getMemberId());
                                        }
-                                        else
-                                       {
-                                               $msg_adm = addslashes(LOAD_EMAIL_TEMPLATE("admin_payout_request_banner", array(), $GLOBALS['userid']));
-                                       }
-                               }
-                                else
-                               {
+                               } else {
                                        // e-currency payout requested
-                                       $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_payouts (userid, payout_total, target_account, target_bank, payout_id, payout_timestamp, status, password)
+                                       sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `target_account`, `target_bank`, `payout_id`, `payout_timestamp`, `status`, `password`)
 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
- array(
-       $GLOBALS['userid'],
-       bigintval($_POST['payout']),
-       bigintval($_POST['account']),
-       $_POST['bank'],
-       bigintval($_GET['payout']),
-       $_POST['pass']
-), __FILE__, __LINE__);
                                      array(
+                                               getMemberId(),
+                                               bigintval(postRequestElement('payout')),
+                                               bigintval(postRequestElement('account')),
+                                               postRequestElement('bank'),
+                                               bigintval(getRequestElement('payout')),
+                                               postRequestElement('password')
+                                       ), __FILE__, __LINE__);
 
                                        // Load templates
-                                       $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request", array(), $GLOBALS['userid']);
-                                       $msg_adm = addslashes(LOAD_EMAIL_TEMPLATE("admin_payout_request", array(), $GLOBALS['userid']));
-                                       $admin_tpl = "";
-                                       if (GET_EXT_VERSION("admins") >= "0.4.1")
-                                       {
-                                               $admin_tpl = "admin_payout_request";
-                                       }
+                                       $message_mem = loadEmailTemplate('member_payout_request', postRequestArray(), getMemberId());
+                                       $message_adm = loadEmailTemplate('admin_payout_request', postRequestArray(), getMemberId());
+                                       $admin_tpl = '';
+
+                                       // @TODO Rewrite this to a filter
+                                       if (isExtensionInstalledAndNewer('admins', '0.4.1')) {
+                                               $admin_tpl = 'admin_payout_request';
+                                       } // END - if
                                }
 
-                               // Generate task
-                               $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, status, task_type, subject, text, task_created, userid)
-VALUES (0, 'NEW','PAYOUT_REQUEST','[payout:] ".PAYOUT_REQUEST_ADMIN."','%s', UNIX_TIMESTAMP(), %s)",
- array(
-       $msg_adm,
-       $GLOBALS['userid']
-), __FILE__, __LINE__);
+                               // Generate task (we ignore the task id here)
+                               createNewTask('[payout:] {--ADMIN_PAYOUY_REQUEST_SUBJECT--}', $message_adm, 'PAYOUT_REQUEST', getMemberId());
 
                                // Send out mails
-                               SEND_EMAIL($GLOBALS['userid'], PAYOUT_REQUEST_MEMBER, $msg_mem);
+                               sendEmail(getMemberId(), '{--MEMBER_PAYOUT_REQUEST_SUBJECT--}', $message_mem);
 
                                // To admin(s)
-                               SEND_ADMIN_NOTIFICATION(PAYOUT_REQUEST_ADMIN, $admin_tpl, array(), $GLOBALS['userid']);
+                               sendAdminNotification('{--ADMIN_PAYOUY_REQUEST_SUBJECT--}', $admin_tpl, postRequestArray(), getMemberId());
 
                                // Load template and output it
-                               LOAD_TEMPLATE("admin_settings_saved", false, PAYOUT_REQUEST_SENT);
-                       }
-                        elseif ($allow == "Y")
-                       {
+                               displayMessage('{--MEMBER_PAYOUT_REQUEST_SENT--}');
+                       } elseif ($content['allow_url'] == 'Y') {
+                               // Prepare content
+                               $content = array(
+                                       'max'    => $max,
+                                       'type'   => $content['type'],
+                                       'payout' => bigintval(getRequestElement('payout'))
+                               );
+
                                // Generate banner order form
-                               LOAD_TEMPLATE("member_payout_form_banner");
-                       }
-                        else
-                       {
+                               loadTemplate('member_payout_form_banner', FALSE, $content);
+                       } else {
+                               // Prepare content
+                               $content = array(
+                                       'max'    => $max,
+                                       'type'   => $content['type'],
+                                       'payout' => bigintval(getRequestElement('payout'))
+                               );
+
                                // Generate normal form
-                               LOAD_TEMPLATE("member_payout_form");
+                               loadTemplate('member_payout_form', FALSE, $content);
                        }
-               }
-                else
-               {
+               } else {
                        // Not enougth points
-                       LOAD_TEMPLATE("admin_settings_saved", false, PAYOUT_POINTS_NOT_ENOUGTH);
+                       displayMessage('{--MEMBER_PAYOUT_POINTS_NOT_ENOUGTH--}');
                }
+       } else {
+               // id is invalid
+               displayMessage('{--MEMBER_PAYOUT_ID_INVALID--}');
        }
-        else
-       {
-               // ID is invalid
-               LOAD_TEMPLATE("admin_settings_saved", false, PAYOUT_ID_INVALID);
-       }
+
+       // Free result
+       sqlFreeResult($result);
 }
-//
+
+// [EOF]
 ?>