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