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