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