db7f6f012767f3c2a6fc6522b6b462ac4d6cbc7c
[mailer.git] / inc / modules / member / what-order.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/19/2003 *
4  * ===============                              Last change: 08/26/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-order.php                                   *
8  * -------------------------------------------------------------------- *
9  * Short description : Order mails here                                 *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Hier koennen Ihre Mitglieder Mails buchen        *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
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 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
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.                                  *
27  *                                                                      *
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.                         *
32  *                                                                      *
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,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
42         require($INC);
43 } elseif (!IS_MEMBER()) {
44         LOAD_URL("modules.php?module=index");
45 } elseif ((!EXT_IS_ACTIVE("order")) && (!IS_ADMIN())) {
46         addFatalMessage(__FILE__, __LINE__, getMessage('EXTENSION_PROBLEM_EXT_INACTIVE'), "order");
47         return;
48 }
49
50 // Add description as navigation point
51 ADD_DESCR("member", __FILE__);
52
53 $URL = ""; $id = 0;
54 $whereStatement = " WHERE `visible`='Y'";
55
56 // Set undefined array elements
57 if (!REQUEST_ISSET_GET(('msg')))       REQUEST_SET_GET('msg'      , "");
58 if (!REQUEST_ISSET_POST(('zip')))      REQUEST_SET_POST('zip'     , "");
59 if (!REQUEST_ISSET_POST(('html')))     REQUEST_SET_POST('html'    , "");
60 if (!REQUEST_ISSET_POST(('receiver'))) REQUEST_SET_POST('receiver', "");
61 if (IS_ADMIN()) $whereStatement = "";
62
63 // Minimum mails / order
64 define('__MIN_VALUE', getConfig('order_min'));
65
66 // Count unconfirmed mails
67 $links = GET_TOTAL_DATA(getUserId(), "user_links", "id", "userid", true);
68
69 // Does the user has more than 0 mails per day set?
70 $HOLIDAY = "userid";
71 if (GET_EXT_VERSION("holiday") >= "0.1.3") {
72         // Fetch also holiday activation data
73         $HOLIDAY = "holiday_active";
74 } // END - if
75
76 $result_mmails = SQL_QUERY_ESC("SELECT userid, receive_mails, mail_orders, ".$HOLIDAY."
77 FROM `{!_MYSQL_PREFIX!}_user_data`
78 WHERE userid=%s AND max_mails > 0 LIMIT 1",
79         array(getUserId()), __FILE__, __LINE__);
80
81 $mmails = SQL_NUMROWS($result_mmails);
82 list($DMY, $MAXI, $ORDERS, $HOLIDAY) = SQL_FETCHROW($result_mmails);
83 SQL_FREERESULT($result_mmails);
84 if ($HOLIDAY == $DMY) $HOLIDAY='N';
85
86 $ALLOWED = $MAXI - $ORDERS;
87 if (getConfig('order_max_full') == "MAX") $ALLOWED = $MAXI;
88
89 // Now check his points amount
90 $TOTAL = GET_TOTAL_DATA(getUserId(), "user_points", "points") - GET_TOTAL_DATA(getUserId(), "user_data", "used_points");;
91
92 if (($HOLIDAY == "Y") && (GET_EXT_VERSION("holiday") >= "0.1.3")) {
93         // Holiday is active!
94         LOAD_TEMPLATE("admin_settings_saved", false, getMessage('HOLIDAY_ORDER_NOT_POSSIBLE'));
95 } elseif ((REQUEST_ISSET_POST(('frametester'))) && ($ALLOWED > 0) && (REQUEST_POST('receiver') > 0)) {
96         // Continue with the frametester, we first need to store the data temporary in the pool
97         //
98         // First we would like to store the data and get it's pool position back...
99         $result = SQL_QUERY_ESC("SELECT id, data_type
100 FROM `{!_MYSQL_PREFIX!}_pool`
101 WHERE sender=%s AND url='%s' AND timestamp > (UNIX_TIMESTAMP() - %s) LIMIT 1",
102                 array(getUserId(), REQUEST_POST('url'), getConfig('url_tlock')), __FILE__, __LINE__);
103
104         $type = "TEMP"; $id = 0;
105         if (SQL_NUMROWS($result) == 1) {
106                 // Load id and mail type
107                 list($id, $type) = SQL_FETCHROW($result);
108         } // END - if
109
110         // Free result
111         SQL_FREERESULT($result);
112
113         if ($type == "TEMP") {
114                 // No entry found, so we need to check out the stats table as well... :)
115                 // We have to add that suff here, now we continue WITHOUT checking and check the text and subject against some filters
116                 $URL = "";
117                 if (getConfig('test_text') == "Y") {
118                         // Test submitted text against some filters (length, URLs in text etc.)
119                         if ((strpos(strtolower(REQUEST_POST('text')), "https://") > -1) || (strpos(strtolower(REQUEST_POST('text')), "http://") > -1) || (strpos(strtolower(REQUEST_POST('text')), "www") > -1)) {
120                                 // URL found!
121                                 $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('URL_FOUND');
122                         } // END - if
123
124                         // Remove new-line and carriage-return characters
125                         $TEST = str_replace("\n", "", str_replace("\r", "", REQUEST_POST('text')));
126
127                         // Text length within allowed length?
128                         if (strlen($TEST) > getConfig('max_tlength')) {
129                                 // Text is too long!
130                                 $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('OVERLENGTH');
131                         } // END - if
132                 } // END - if
133
134                 // Shall I test the subject line against URLs?
135                 if (getConfig('test_subj') == "Y") {
136                         // Check the subject line for issues
137                         REQUEST_SET_POST('subject', str_replace("\\", "[nl]", substr(REQUEST_POST('subject'), 0, 200)));
138                         if ((strpos(strtolower(REQUEST_POST('subject')), "http://") > -1) || (strpos(strtolower(REQUEST_POST('subject')), "www") > -1)) {
139                                 // URL in subject found
140                                 $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('SUBJ_URL');
141                         } // END - if
142                 } // END - if
143
144                 // And shall I check that his URL is not in the black list?
145                 if (getConfig('url_blacklist') == "Y") {
146                         // Ok, I do that for you know...
147                         $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`timestamp`) AS tstamp FROM `{!_MYSQL_PREFIX!}_url_blacklist` WHERE `url`='%s' LIMIT 1",
148                                 array(REQUEST_POST('url')), __FILE__, __LINE__);
149
150                         if (SQL_NUMROWS($result) == 1) {
151                                 // Jupp, we got one listed
152                                 list($blist) = SQL_FETCHROW($result);
153
154                                 // Create redirect-URL
155                                 $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('BLIST_URL')."&amp;blist=".$blist;
156                         } // END - if
157
158                         // Free result
159                         SQL_FREERESULT($result);
160                 } // END - if
161
162                 // Enougth receivers entered?
163                 if ((REQUEST_POST('receiver') < getConfig('order_min')) && (!IS_ADMIN())) {
164                         // Less than allowed receivers entered!
165                         $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('MORE_RECEIVERS3');
166                 } // END - if
167
168                 // Validate URL
169                 if (!VALIDATE_URL(REQUEST_POST('url'))) {
170                         // URL is invalid!
171                         $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('INVALID_URL');
172                 } // END - if
173
174                 // Probe for HTML extension
175                 if (EXT_IS_ACTIVE("html_mail")) {
176                         // HTML or regular text mail?
177                         if (REQUEST_POST('html') == "Y") {
178                                 // Chek for valid HTML tags
179                                 REQUEST_SET_POST('text', HTML_CHECK_TAGS(REQUEST_POST('text')));
180
181                                 // Maybe invalid tags found?
182                                 if (!REQUEST_ISSET_POST(('text'))) $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('INVALID_TAGS')."&amp;id=".$id;
183                         } else {
184                                 // Remove any HTML code
185                                 REQUEST_SET_POST('text', str_replace("<", "{OPEN_HTML}", str_replace(">", "{CLOSE_HTML}", REQUEST_POST('text'))));
186                         }
187                 }
188         } elseif (!IS_ADMIN()) {
189                 // He has already sent a mail within a specific time
190                 $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('URL_TLOCK')."&amp;id=".$id;
191         }
192
193         // Still no error?
194         if (empty($URL)) {
195                 // Check if category and number of receivers is okay
196                 $ADD = "";
197                 if ((getConfig('order_multi_page') == "Y") && (REQUEST_ISSET_POST(('zip')))) {
198                         // Choose recipients by ZIP code
199                         $ADD = " AND d.zip LIKE '".bigintval(REQUEST_POST('zip'))."{PER}'";
200                 } // END - if
201
202                 // Check for userids
203                 $result = SQL_QUERY_ESC("SELECT DISTINCT c.userid FROM `{!_MYSQL_PREFIX!}_user_cats` AS c
204 LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS d
205 ON c.userid=d.userid
206 WHERE c.cat_id=%s AND c.userid != '%s' AND d.`status`='CONFIRMED' AND d.receive_mails > 0".$ADD."
207 ORDER BY d.%s %s",
208  array(
209         bigintval(REQUEST_POST('cat')),
210         getUserId(),
211         getConfig('order_select'),
212         getConfig('order_mode'),
213  ), __FILE__, __LINE__);
214
215                 // Do we enougth receivers left?
216                 if (SQL_NUMROWS($result) >= REQUEST_POST('receiver')) {
217                         // Check for holiday extensions
218                         $HOLIDAY = false;
219                         if (GET_EXT_VERSION("holiday") >= "0.1.3") {
220                                 // Include checking for users in holiday
221                                 $HOLIDAY = true;
222                         } // END - if
223
224                         // Load receivers from database
225                         $TEST = array(); $cnt = 0;
226                         while ($content = SQL_FETCHARRAY($result)) {
227                                 if ($HOLIDAY) {
228                                         // Check for his holiday status
229                                         $result_holiday = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_holidays`
230 WHERE userid=%s AND holiday_start < UNIX_TIMESTAMP() AND holiday_end > UNIX_TIMESTAMP() LIMIT 1",
231  array(bigintval($content['userid'])), __FILE__, __LINE__);
232                                         if (SQL_NUMROWS($result_holiday) == 1) $content['userid'] = 0; // Exclude user who are in holiday
233
234                                         // Free memory
235                                         SQL_FREERESULT($result_holiday);
236                                 } // END - if
237
238                                 if ($content['userid'] > 0) {
239                                         // Add receiver
240                                         $TEST[] = $content['userid'];
241                                         $cnt++;
242                                 } // END - if
243                         } // END - while
244
245                         // Free memory
246                         SQL_FREERESULT($result);
247
248                         // Implode array into string for the sending pool
249                         $RECEIVER = implode($TEST, ";");
250
251                         // Count array for maximum sent
252                         $MAX_SEND = count($TEST);
253
254                         // Update receiver list
255                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET receive_mails=receive_mails-1 WHERE userid IN (%s) LIMIT %s",
256                                 array(str_replace(";", ", ", $RECEIVER), $MAX_SEND), __FILE__, __LINE__);
257
258                         // Is calculated max receivers larger than wanted receivers then reset it
259                         if ($MAX_SEND > REQUEST_POST('receiver')) $MAX_SEND = REQUEST_POST('receiver');
260
261                         // Calculate used points
262                         $USED = $MAX_SEND * GET_PAY_POINTS(bigintval(REQUEST_POST('type')));
263
264                         // Fix empty zip code
265                         if (!REQUEST_ISSET_POST(('zip'))) REQUEST_SET_POST('zip', "0");
266
267                         // Check if he has enougth points for this order and selected more than 0 receivers
268                         if (($USED > 0) && ($USED <= $TOTAL) && ($MAX_SEND > 0)) {
269                                 // Gettings points is okay, so we can add $USED later from
270                                 $TIME = time();
271                                 if (($id == "0") || ($type != "TEMP")) {
272                                         // New order
273                                         $id = 0;
274                                         if (EXT_IS_ACTIVE("html_mail")) {
275                                                 // HTML extension is active
276                                                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_pool` (sender, subject, text, receivers, payment_id, data_type, timestamp, url, cat_id, target_send, zip, html_msg)
277  VALUES ('%s','%s','%s','%s','%s','TEMP','%s','%s','%s','%s','%s','%s')",
278 array(
279         getUserId(),
280         REQUEST_POST('subject'),
281         REQUEST_POST('text'),
282         $RECEIVER,
283         bigintval(REQUEST_POST('type')),
284         $TIME,
285         REQUEST_POST('url'),
286         bigintval(REQUEST_POST('cat')),
287         $MAX_SEND,
288         bigintval(REQUEST_POST('zip')),
289         REQUEST_POST('html')
290 ), __FILE__, __LINE__);
291                                         } else {
292                                                 // No HTML extension is active
293                                                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_pool` (sender, subject, text, receivers, payment_id, data_type, timestamp, url, cat_id, target_send, zip)
294  VALUES ('%s','%s','%s','%s','%s','TEMP','%s','%s','%s','%s','%s')",
295 array(
296         getUserId(),
297         REQUEST_POST('subject'),
298         REQUEST_POST('text'),
299         $RECEIVER,
300         bigintval(REQUEST_POST('type')),
301         $TIME,
302         REQUEST_POST('url'),
303         bigintval(REQUEST_POST('cat')),
304         $MAX_SEND,
305         bigintval(REQUEST_POST('zip')),
306 ), __FILE__, __LINE__);
307                                         }
308                                 } else {
309                                         // Change current order
310                                         if (EXT_IS_ACTIVE("html_mail")) {
311                                                 // HTML extension is active
312                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_pool` SET
313 subject='%s',
314 text='%s',
315 receivers='%s',
316 payment_id=%s,
317 timestamp=UNIX_TIMESTAMP(),
318 url='%s',
319 cat_id=%s,
320 target_send=%s,
321 zip=%s,
322 html_msg='%s'
323 WHERE id=%s LIMIT 1",
324 array(
325         REQUEST_POST('subject'),
326         REQUEST_POST('text'),
327         $RECEIVER,
328         bigintval(REQUEST_POST('type')),
329         REQUEST_POST('url'),
330         bigintval(REQUEST_POST('cat')),
331         $MAX_SEND,
332         bigintval(REQUEST_POST('zip')),
333         REQUEST_POST('html'),
334         bigintval($id)
335 ), __FILE__, __LINE__);
336                                         } else {
337                                                 // No HTML extension is active
338                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_pool` SET
339 subject='%s',
340 text='%s',
341 receivers='%s',
342 payment_id=%s,
343 timestamp=UNIX_TIMESTAMP(),
344 url='%s',
345 cat_id=%s,
346 target_send=%s,
347 zip=%s
348 WHERE id=%s LIMIT 1",
349 array(
350         REQUEST_POST('subject'),
351         REQUEST_POST('text'),
352         $RECEIVER,
353         bigintval(REQUEST_POST('type')),
354         REQUEST_POST('url'),
355         bigintval(REQUEST_POST('cat')),
356         $MAX_SEND,
357         bigintval(REQUEST_POST('zip')),
358         bigintval($id)
359 ), __FILE__, __LINE__);
360                                         }
361                                 }
362
363                                 // Do we need to get the ID number?
364                                 if ($id == 0) {
365                                         // Order is placed as temporary. We need to get it's id for the frametester
366                                         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_pool` WHERE sender=%s AND subject='%s' AND payment_id=%s AND data_type='TEMP' AND timestamp=%s LIMIT 1",
367                                         array(
368                                                 getUserId(),
369                                                 REQUEST_POST('subject'),
370                                                 bigintval(REQUEST_POST('type')),
371                                                 $TIME
372                                         ), __FILE__, __LINE__);
373
374                                         list($id) = SQL_FETCHROW($result);
375                                         SQL_FREERESULT($result);
376                                 }
377
378                                 // ID is received so we can redirect the user, used points will be added when he send's out the mail
379                                 $URL = "modules.php?module=frametester&amp;order=".$id."";
380                         } elseif ($MAX_SEND == 0) {
381                                 // Not enougth receivers found which can receive mails
382                                 $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('MORE_RECEIVERS2');
383                         } else {
384                                 // No enougth points left!
385                                 $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('MORE_POINTS');
386                         }
387                 } else  {
388                         // Ordered more mails than he can send in this category
389                         $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('NO_RECS_LEFT');
390                 }
391         }
392 } elseif (REQUEST_POST('receiver') == "0") {
393         // Not enougth receivers selected
394         $URL = "modules.php?module=login&amp;what=order&amp;msg=".getCode('MORE_RECEIVERS1');
395 } elseif (($ALLOWED == 0) && (getConfig('order_max_full') == "ORDER")) {
396         // No more mail orders allowed
397         LOAD_TEMPLATE("admin_settings_saved", false, getMessage('MEMBER_ORDER_ALLOWED_EXHAUSTED'));
398 } elseif (($links < getConfig('unconfirmed')) && ($mmails == "1")) {
399         // Display order form
400         $result_cats = SQL_QUERY("SELECT id, cat FROM `{!_MYSQL_PREFIX!}_cats`".$whereStatement." ORDER BY `sort`", __FILE__, __LINE__);
401         if (SQL_NUMROWS($result_cats) > 0) {
402                 if ($TOTAL > 0) {
403                         // Initialize array...
404                         $CATS = array(
405                                 'id'   => array(),
406                                 'name' => array(),
407                                 'uids' => array()
408                         );
409
410                         // Enable HTML checking
411                         $HTML = ""; $HOLIDAY = false; $HOL_STRING = "";
412                         if ((EXT_IS_ACTIVE("html_mail")) && (REQUEST_POST('html') == "Y")) $HTML = " AND html='Y'";
413                         if (GET_EXT_VERSION("holiday") >= "0.1.3") {
414                                 // Extension's version is fine
415                                 $HOLIDAY = true; $HOL_STRING = " AND holiday_active='N'";
416                         } // END - if
417
418                         // ... and begin loading stuff
419                         while ($content = SQL_FETCHARRAY($result_cats)) {
420                                 $CATS['id'][]   = bigintval($content['id']);
421                                 $CATS['name'][] = $content['cat'];
422
423                                 // Select users in current category
424                                 $result_uids = SQL_QUERY_ESC("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_cats` WHERE cat_id=%s AND userid != '%s' ORDER BY userid",
425                                         array(bigintval($content['id']), getUserId()), __FILE__, __LINE__);
426
427                                 $uid_cnt = 0;
428                                 while (list($ucat) = SQL_FETCHROW($result_uids)) {
429                                         // Check for holiday system
430                                         $HOL_ACTIVE = false;
431                                         if ($HOLIDAY) {
432                                                 // Check user's holiday status
433                                                 $result_holiday = SQL_QUERY_ESC("SELECT DISTINCT d.userid FROM `{!_MYSQL_PREFIX!}_user_data` AS d
434 LEFT JOIN `{!_MYSQL_PREFIX!}_user_holidays` AS h
435 ON d.userid=h.userid
436 WHERE d.userid=%s AND d.receive_mails > 0 AND d.`status`='CONFIRMED' AND d.holiday_active='Y'
437 AND h.holiday_start < UNIX_TIMESTAMP() AND h.holiday_end > UNIX_TIMESTAMP()
438 LIMIT 1", array(bigintval($ucat)), __FILE__, __LINE__);
439                                                 if (SQL_NUMROWS($result_holiday) == 1) {
440                                                         // Holiday is active!
441                                                         $HOL_ACTIVE = true;
442                                                 } // END - if
443
444                                                 // Free memory
445                                                 SQL_FREERESULT($result_holiday);
446                                         } // END - if
447
448                                         if (!$HOL_ACTIVE) {
449                                                 // Check if the user want's to receive mails?
450                                                 $result_ver = SQL_QUERY_ESC("SELECT zip FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s".$HTML." AND receive_mails > 0 AND `status`='CONFIRMED' LIMIT 1",
451                                                         array(bigintval($ucat)), __FILE__, __LINE__);
452
453                                                 if ((SQL_NUMROWS($result_ver) == 1) && (REQUEST_ISSET_POST(('zip'))) && (getConfig('order_multi_page') == "Y")) {
454                                                         list($zip) = SQL_FETCHROW($result_ver);
455                                                         SQL_FREERESULT($result_ver);
456                                                         if (substr($zip, 0, strlen(REQUEST_POST('zip'))) == REQUEST_POST('zip')) {
457                                                                 // Ok, ZIP part is found
458                                                                 $uid_cnt++;
459                                                         } // END - if
460                                                 } else {
461                                                         // Count numbers up!
462                                                         $uid_cnt += SQL_NUMROWS($result_ver);
463                                                 }
464                                         } // END - if
465                                 } // END - while
466
467                                 // Free memory
468                                 SQL_FREERESULT($result_uids);
469                                 $CATS['uids'][] = $uid_cnt;
470                         } // END - while
471
472                         // Free memory
473                         SQL_FREERESULT($result_cats);
474
475                         // Now we need to load the mail types...
476                         $result = SQL_QUERY("SELECT id, price, payment, mail_title FROM `{!_MYSQL_PREFIX!}_payments` ORDER BY payment", __FILE__, __LINE__);
477
478                         $typeS = array();
479                         if (SQL_NUMROWS($result) > 0) {
480                                 // Check for message ID in URL
481                                 $MSG = "";
482                                 switch (REQUEST_GET('msg'))
483                                 {
484                                 case getCode('URL_TLOCK'):
485                                         $result = SQL_QUERY_ESC("SELECT timestamp FROM `{!_MYSQL_PREFIX!}_pool` WHERE id=%s LIMIT 1",
486                                          array(bigintval(REQUEST_GET('id'))), __FILE__, __LINE__);
487
488                                         // Load timestamp from last order
489                                         list($LORDER) = SQL_FETCHROW($result);
490                                         $LORDER = MAKE_DATETIME($LORDER, "1");
491
492                                         // Free memory
493                                         SQL_FREERESULT($result);
494
495                                         // Calculate hours...
496                                         $STD = round(getConfig('url_tlock') / 60 / 60);
497
498                                         // Minutes...
499                                         $MIN = round((getConfig('url_tlock') - $STD * 60 * 60) / 60);
500
501                                         // And seconds
502                                         $SEC = getConfig('url_tlock') - $STD * 60 * 60 - $MIN * 60;
503
504                                         // Finally contruct the message
505                                         // @TODO Rewrite this old lost code to a template
506                                         $MSG = "{--MEMBER_URL_TIME_LOCK--}<br />{--CONFIG_URL_TLOCK--} ".$STD."
507                                                 {--_HOURS--}, ".$MIN." {--_MINUTES--} {--_AND--} ".$SEC." {--_SECONDS--}<br />
508                                                 {--MEMBER_LAST_TLOCK--}: ".$LORDER;
509                                         break;
510
511                                 case getCode('OVERLENGTH'):
512                                         $MSG = getMessage('MEMBER_TEXT_OVERLENGTH');
513                                         break;
514
515                                 case getCode('URL_FOUND'):
516                                         $MSG = getMessage('MEMBER_TEXT_CONTAINS_URL');
517                                         break;
518
519                                 case getCode('SUBJ_URL'):
520                                         $MSG = getMessage('MEMBER_SUBJ_CONTAINS_URL');
521                                         break;
522
523                                 case getCode('BLIST_URL'):
524                                         $MSG = "{--MEMBER_URL_BLACK_LISTED--}<br />\n{--MEMBER_BLIST_TIME--}: ".MAKE_DATETIME(REQUEST_GET('blist'), "0");
525                                         break;
526
527                                 case getCode('NO_RECS_LEFT'):
528                                         $MSG = getMessage('MEMBER_SELECTED_MORE_RECS');
529                                         break;
530
531                                 case getCode('INVALID_TAGS'):
532                                         $MSG = getMessage('MEMBER_HTML_INVALID_TAGS');
533                                         break;
534
535                                 case getCode('MORE_POINTS'):
536                                         $MSG = getMessage('MEMBER_MORE_POINTS_NEEDED');
537                                         break;
538
539                                 case getCode('MORE_RECEIVERS1'):
540                                         $MSG = getMessage('MEMBER_ENTER_MORE_RECEIVERS');
541                                         break;
542
543                                 case getCode('MORE_RECEIVERS2'):
544                                         $MSG = getMessage('MEMBER_NO_MORE_RECEIVERS_FOUND');
545                                         break;
546
547                                 case getCode('MORE_RECEIVERS3'):
548                                         $MSG = sprintf(getMessage('MEMBER_ENTER_MORE_MIN_RECEIVERS'), getConfig('order_min'));
549                                         break;
550
551                                 case getCode('INVALID_URL'):
552                                         $MSG = getMessage('MEMBER_ENTER_INVALID_URL');
553                                         break;
554
555                                 case "": // When no error code is included in the URL we do not need to output an error message as well...
556                                         break;
557
558                                 default:
559                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown error code %s detected.", REQUEST_GET('msg')));
560                                         $MSG = sprintf(getMessage('UNKNOWN_CODE'), REQUEST_GET('msg'));
561                                         break;
562                                 }
563
564                                 if (!empty($MSG)) {
565                                         // We got system message so we drop it out to the user
566                                         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
567                                 } // END - if
568
569                                 // Load all email types...
570                                 while ($typeS[] = SQL_FETCHROW($result)) {
571                                         // Nothing to do here... ;-)
572                                 } // END - while
573
574                                 // Free memory
575                                 SQL_FREERESULT($result);
576
577                                 // Output user's points
578                                 $TOTAL = TRANSLATE_COMMA($TOTAL);
579
580                                 // Check how many mail orders he has placed today and how many he's allowed to send
581                                 switch (getConfig('order_max_full'))
582                                 {
583                                 case "MAX": // He is allowed to send as much as possible
584                                         define('ORDER_MAX_VALUE', getMessage('ORDER_ALLOWED_MAX'));
585                                         break;
586
587                                 case "ORDER": // He is allowed to send as much as he setup the receiving value
588                                         define('ORDER_MAX_VALUE', sprintf(getMessage('ORDER_ALLOWED_RECEIVE'), $ALLOWED, $MAXI));
589                                         break;
590
591                                 default: // Unknown/invalid
592                                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unknown order_mas_full config detected.", getConfig('order_max_full')));
593                                         define('ORDER_MAX_VALUE', getMessage('ORDER_ALLOWED_UNKNOWN'));
594                                         break;
595                                 }
596
597                                 // Load final template
598                                 LOAD_TEMPLATE("member_order_points", false, $TOTAL);
599
600                                 // Reset variables
601                                 $OLD_ORDER = false; $subject = ""; $text = ""; $target = "";
602
603                                 // Check if we already have an order placed and make it editable
604                                 $result = SQL_QUERY_ESC("SELECT subject, text, payment_id, timestamp, url, target_send, cat_id, zip FROM `{!_MYSQL_PREFIX!}_pool` WHERE sender=%s AND data_type='TEMP' LIMIT 1",
605                                  array(getUserId()), __FILE__, __LINE__);
606
607                                 if (SQL_NUMROWS($result) == 1) {
608                                         // Old order found
609                                         list($subject, $text, $payment, $tstamp, $url, $target, $content['cat'], $zip) = SQL_FETCHROW($result);
610
611                                         // Fix max receivers when it is too much
612                                         if ((isset($CATS['uids'][$content['cat']])) && ($target > $CATS['uids'][$content['cat']])) $target = $CATS['uids'][$content['cat']];
613
614                                         // Old order is grabbed
615                                         $OLD_ORDER = true;
616                                 } else {
617                                         // Default output for that your members don't forget it...
618                                         $url = "http://";
619                                 }
620
621                                 // Free result
622                                 SQL_FREERESULT($result);
623
624                                 // 01      2              21    12                                    2    23         443    3          3210
625                                 if ((REQUEST_ISSET_POST(('data'))) || ((getConfig('order_multi_page') != "Y") && ((!IS_ADMIN()) && (!EXT_IS_ACTIVE("html_mail"))))) {
626                                         // Pre-output categories
627                                         $CAT = "";
628                                         foreach ($CATS['id'] as $key => $value) {
629                                                 $CAT .= "      <option value=\"".$value."\"";
630                                                 if (($OLD_ORDER) && ($content['cat'] == $value)) $CAT .= " selected=\"selected\"";
631                                                 $CAT .= ">".$CATS['name'][$key]." (".$CATS['uids'][$key]." {--USER_IN_CAT--})</option>\n";
632                                         } // END - foreach
633
634                                         // Mail type
635                                         $type = "";
636                                         foreach ($typeS as $key => $value) {
637                                                 $P = TRANSLATE_COMMA($typeS[$key][1]);
638                                                 if (is_array($value)) {
639                                                         // Output option line
640                                                         $type .= "      <option value=\"".$typeS[$key][0]."\"";
641                                                         if (($OLD_ORDER) && ($payment == $typeS[$key][0])) $type .= " selected=\"selected\"";
642                                                         $type .= ">".$P." {--PER_MAIL--} - ".$typeS[$key][3]." - ".round($typeS[$key][2])." {--PAYMENT--}</option>\n";
643                                                 } // END - if
644                                         } // END - foreach
645
646                                         // Put all in constants for the template
647                                         define('CATEGORY_SELECTION', $CAT);
648                                         define('TYPE_SELECTION', $type);
649                                         define('TARGET', $target);
650                                         define('SUBJECT', $subject);
651                                         define('TEXT', COMPILE_CODE($text));
652                                         define('T_URL', $url);
653
654                                         if (REQUEST_ISSET_POST(('zip'))) {
655                                                 // Output entered ZIP code
656                                                 define('ZIP_OUTPUT', LOAD_TEMPLATE("member_order-zip2", true, REQUEST_POST('zip')));
657                                         } else {
658                                                 define('ZIP_OUTPUT', "<tr><td colspan=\"5\" height=\"5\" class=\"seperator\">&nbsp;</td></tr>");
659                                         }
660
661                                         // HTML extension
662                                         if ((EXT_IS_ACTIVE("html_mail")) && (REQUEST_POST('html') == "Y")) {
663                                                 // Extension is active so output valid HTML tags
664                                                 define('MEMBER_HTML_EXTENSION', LOAD_TEMPLATE("member_order-html_ext", true, HTML_ADD_VALID_TAGS()));
665                                         } else {
666                                                 // Extension not active and/or class not uploaded
667                                                 define('MEMBER_HTML_EXTENSION', "<tr><td colspan=\"5\"><input type=\"hidden\" name=\"html\" value=\"N\"> /</td></tr>");
668                                         }
669
670                                         // Output form for page 2
671                                         LOAD_TEMPLATE("member_order_page2");
672                                 } else {
673                                         // Remember maybe entered ZIP code in constant
674                                         $ADD = "";
675                                         if (EXT_IS_ACTIVE("html_mail")) {
676                                                 // Add some content when html extension is active
677                                                 if ((getConfig('order_multi_page') == "Y") || (IS_ADMIN())) $ADD = "<tr><td colspan=\"2\" class=\"seperator bottom2\" height=\"5\">&nbsp;</td></tr>\n";
678                                                 define('MEMBER_HTML_EXTENSION', LOAD_TEMPLATE("member_order-html_intro", true));
679                                         } else {
680                                                 // No HTML extension installed
681                                                 define('MEMBER_HTML_EXTENSION', "<tr><td colspan=\"2\"><input type=\"hidden\" name=\"html\" value=\"N\" /></td></tr>");
682                                         }
683
684                                         // Do we want ZIP code or not?
685                                         if ((getConfig('order_multi_page') == "Y") || (IS_ADMIN())) {
686                                                 // Yes
687                                                 if (REQUEST_POST('zip') > 0) {
688                                                         $content = array(
689                                                                 'zip' => bigintval(REQUEST_POST('zip')),
690                                                                 'add' => $ADD
691                                                         );
692                                                 } else {
693                                                         $content = array(
694                                                                 'zip' => "",
695                                                                 'add' => $ADD
696                                                         );
697                                                 }
698                                                 define('MEMBER_ZIP_CONTENT', LOAD_TEMPLATE("member_order-zip1", true, $content));
699                                         } else {
700                                                 // No
701                                                 define('MEMBER_ZIP_CONTENT', "");
702                                         }
703
704                                         // Output form for page 1 (ZIP code or HTML)
705                                         LOAD_TEMPLATE("member_order_page1");
706                                 }
707                         } else {
708                                 // No mail types defined
709                                 LOAD_TEMPLATE("admin_settings_saved", false, "<span class=\"member_failed\">{--MEMBER_NO_PAYMENTS--}</span>");
710                         }
711                 } else {
712                         // No points left
713                         LOAD_TEMPLATE("admin_settings_saved", false, "<span class=\"member_failed\">{--MEMBER_NO_POINTS--}</span>");
714                 }
715         } else {
716                 // No cateogries are defined yet
717                 LOAD_TEMPLATE("admin_settings_saved", false, "<span class=\"member_failed\">{--MEMBER_NO_CATS--}</span>");
718         }
719 } elseif ($mmails == "0") {
720         // Please set more than 0 mails per day
721         LOAD_TEMPLATE("admin_settings_saved", false, getMessage('MEMBER_HAS_ZERO_MMAILS'));
722 } else {
723         // Please confirm some mails first
724         LOAD_TEMPLATE("admin_settings_saved", false, sprintf(getMessage('MEMBER_LINKS_LEFT'), $links, getConfig('unconfirmed')));
725 }
726
727 if (!empty($URL)) {
728         // Redirect to requested URL
729         LOAD_URL($URL);
730 } // END - if
731
732 //
733 ?>