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