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