a66577b270e0a175317848523d18797fe36c4521
[mailer.git] / inc / modules / admin / what-unlock_sponsor.php
1 <?php
2 /************************************************************************
3  * M-XChange v0.2.1                                   Start: 04/23/2005 *
4  * ================                             Last change: 05/19/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-unlock_sponsor.php                          *
8  * -------------------------------------------------------------------- *
9  * Short description : Unlock sponsor accounts                          *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Sponsorenaccounts freigeben                      *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software. You can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License.       *
21  ************************************************************************/
22
23 // Some security stuff...
24 if ((ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) || (!is_admin())) {
25         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";
26         require($INC);
27 }
28
29 // Add description as navigation point
30 ADD_DESCR("admin", basename(__FILE__));
31
32 // Check if admin has submitted form
33 if (isset($_POST['ok'])) {
34         // Does he have selected at least one sponsor?
35         if (SELECTION_COUNT($_POST['id']) > 0) {
36                 // At least one entry selected
37                 foreach ($_POST['id'] as $id => $sel) {
38                         // Secure ID number
39                         $id = bigintval($id);
40
41                         // Load his personal data
42                         $result_main = SQL_QUERY_ESC("SELECT gender, surname, family, email, remote_addr, sponsor_created, points_amount, refid
43 FROM "._MYSQL_PREFIX."_sponsor_data
44 WHERE status='PENDING' AND id='%s' LIMIT 1",
45  array($id), __FILE__, __LINE__);
46                         $refid = 0;
47                         if (SQL_NUMROWS($result_main) == 1) {
48                                 // Load data and free memory
49                                 list($gender, $sname, $fname, $email, $ip, $created, $points, $refid) = SQL_FETCHROW($result_main);
50
51                                 // Check for open payments and close them
52                                 $result = SQL_QUERY_ESC("SELECT DISTINCT so.aid, so.pay_count, so.pay_ordered, so.pay_status,
53 sp.pay_name, sp.pay_rate, sp.pay_currency
54 FROM "._MYSQL_PREFIX."_sponsor_orders AS so
55 LEFT JOIN "._MYSQL_PREFIX."_sponsor_paytypes AS sp
56 ON sp.id=so.payid
57 WHERE so.sponsorid='%s'
58 ORDER BY sp.pay_name",
59  array($id), __FILE__, __LINE__);
60                                 if (SQL_NUMROWS($result) > 0) {
61                                         // Payment does exist
62                                         while(list($aid, $count, $ordered, $status, $pname, $prate, $pcurr) = SQL_FETCHROW($result))
63                                         {
64                                                 if ($aid == "0") {
65                                                         // No admin assigned!
66                                                         $aid = SPONSOR_NO_ADMIN;
67                                                 } else {
68                                                         // Load admin's email address for contact
69                                                         $result_aid = SQL_QUERY_ESC("SELECT email FROM "._MYSQL_PREFIX."_admins WHERE id='%s' LIMIT 1",
70                                                          array(bigintval($aid)), __FILE__, __LINE__);
71                                                         list($aid) = SQL_FETCHROW($result_aid);
72                                                         SQL_FREERESULT($result_aid);
73
74                                                         // Check if admin login is still in db
75                                                         if (empty($aid)) $aid = ADMIN_NOT_FOUND;
76                                                 }
77
78                                                 // Transfer data to array
79                                                 $content = array(
80                                                         'aid'   => $aid,
81                                                         'order' => ($count * $prate)." ".$pcurr,
82                                                         'stamp' => MAKE_DATETIME($ordered, "2"),
83                                                         'pname' => $pname,
84                                                 );
85         
86                                                 // Load email template
87                                                 $content['msg'] = LOAD_EMAIL_TEMPLATE("sponsor_unlock_sponsor_pay", $content, $id);
88                                         }
89                                 } else {
90                                         // No payments found
91                                         $content['msg'] = SPONSOR_NO_PAYMENTS_FOUND;
92                                 }
93
94                                 // Free memory
95                                 SQL_FREERESULT($result);
96                         }
97
98                         // Free memory
99                         SQL_FREERESULT($result_main);
100
101                         // Unlock sponsor account
102                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_sponsor_data SET status='CONFIRMED'
103 WHERE id='%s' AND status='PENDING' LIMIT 1",
104  array($id), __FILE__, __LINE__);
105
106                         // Update, if applyable, referral count and points
107                         if (($refid > 0) && ($refid != $id)) {
108                                 // Update referral account
109                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_sponsor_data
110 SET points_amount=points_amount+%s, ref_count=ref_count+1
111 WHERE id='%s' LIMIT 1",
112  array($_CONFIG['sponsor_ref_points'], bigintval($refid)), __FILE__, __LINE__);
113
114                                 // Whas that update fine?
115                                 if (SQL_AFFECTEDROWS() == 1) {
116                                         // Load referral's data
117                                         $result = SQL_QUERY_ESC("SELECT id, gender, surname, family, email,
118 (points_amount - points_used) AS points, receive_warnings, ref_count AS refs
119 FROM "._MYSQL_PREFIX."_sponsor_data
120 WHERE id='%s' LIMIT 1",
121  array(bigintval($refid)), __FILE__, __LINE__);
122                                         $REFERRAL = SQL_FETCHARRAY($result);
123
124                                         // Send warnings out?
125                                         if ($REFERRAL['receive_warnings'] == "Y") {
126                                                 // Translate some data
127                                                 $REFERRAL['points']     = TRANSLATE_COMMA($REFERRAL['points']);
128                                                 $REFERRAL['ref_points'] = TRANSLATE_COMMA($_CONFIG['sponsor_ref_points']);
129                                                 $REFERRAL['gender']      = TRANSLATE_GENDER($REFERRAL['gender']);
130
131                                                 // Send notification to referral
132                                                 $REF_MSG = LOAD_EMAIL_TEMPLATE("sponsor_ref_notify", $REFERRAL);
133                                                 SEND_EMAIL($REFERRAL['email'], SPONSOR_REF_NOTIFY_SUBJ, $REF_MSG);
134                                         }
135
136                                         // Free memory
137                                         SQL_FREERESULT($result);
138                                 }
139                         }
140
141                         // Transfer data to array
142                         $content['gender']   = TRANSLATE_GENDER($gender);
143                         $content['surname'] = $sname;
144                         $content['family']  = $fname;
145                         $content['sponsor'] = $id;
146                         $content['points']  = TRANSLATE_COMMA($points);
147
148                         // So let's send the email away
149                         $msg = LOAD_EMAIL_TEMPLATE("sponsor_unlocked", $content);
150                         SEND_EMAIL($email, SPONSOR_UNLOCKED_SUBJ, $msg);
151                 }
152         } else {
153                 // Nothing selected
154                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_SPONSOR_NONE_SELECTED_UNLOCK);
155         }
156
157         // Add seperator
158         OUTPUT_HTML("<BR>");
159 }
160
161 // Begin listing of all pending sponsor accounts
162 $result = SQL_QUERY("SELECT id, gender, surname, family, email, remote_addr, sponsor_created
163 FROM "._MYSQL_PREFIX."_sponsor_data
164 WHERE status='PENDING' ORDER BY id", __FILE__, __LINE__);
165
166 if (SQL_NUMROWS($result) > 0) {
167         // Entries found so let's list them!
168         $OUT = ""; $SW = 2;
169         while(list($id, $gender, $sname, $fname, $email, $ip, $created) = SQL_FETCHROW($result)) {
170                 // Transfer data to array
171                 $content = array(
172                         'sw'      => $SW,
173                         'id'      => $id,
174                         'gender'   => TRANSLATE_GENDER($gender),
175                         'surname' => $sname,
176                         'family'  => $fname,
177                         'email'   => "mailto:".$email,
178                         'remote'  => $ip,
179                         'created' => MAKE_DATETIME($created, "2"),
180                 );
181
182                 // Load row template and switch colors
183                 $OUT .= LOAD_TEMPLATE("admin_unlock_sponsor_row", true, $content);
184                 $SW = 3 - $SW;
185         }
186         define('__SPONSOR_ROWS', $OUT);
187
188         // Load template
189         LOAD_TEMPLATE("admin_unlock_sponsor");
190 } else {
191         // No pending accounts found
192         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_SPONSOR_NONE_PENDING);
193 }
194
195 // Free memory
196 SQL_FREERESULT($result);
197
198 //
199 ?>