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