2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 10/19/2003 *
4 * =================== Last change: 08/26/2004 *
6 * -------------------------------------------------------------------- *
7 * File : what-order.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Order mails here *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Hier koennen Ihre Mitglieder Mails buchen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2012 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * GNU General Public License for more details. *
30 * You should have received a copy of the GNU General Public License *
31 * along with this program; if not, write to the Free Software *
32 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
34 ************************************************************************/
36 // Some security stuff...
37 if (!defined('__SECURITY')) {
39 } elseif (!isMember()) {
40 redirectToIndexMemberOnlyModule();
43 // Add description as navigation point
44 addYouAreHereLink('member', __FILE__);
46 if ((!isExtensionActive('order')) && (!isAdmin())) {
47 displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=order%}');
54 'data_type' => 'TEMP',
58 $ALLOWED = getUserData('receive_mails') - getUserData('mail_orders');
59 if (getConfig('order_max_full') == 'MAX') $ALLOWED = getUserData('receive_mails');
61 // Now check his points amount
62 $totalPoints = getTotalPoints(getMemberId());
64 // Admin can always book
65 if ((isExtensionInstalledAndNewer('holiday', '0.1.3')) && (isUserDataEnabled('holiday_active')) && (!isAdmin())) {
67 displayMessage('{--MEMBER_HOLIDAY_ORDER_NOT_POSSIBLE--}');
68 } elseif ((isPostRequestElementSet('frametester')) && ($ALLOWED > 0) && (postRequestElement('receiver') > 0)) {
69 // Continue with the frametester, we first need to store the data temporary in the pool
71 // First we would like to store the data and get it's pool position back...
72 $result = SQL_QUERY_ESC("SELECT
76 `{?_MYSQL_PREFIX?}_pool`
80 (UNIX_TIMESTAMP() - `timestamp`) >= {?url_tlock?}
84 postRequestElement('url')
85 ), __FILE__, __LINE__);
87 if (SQL_NUMROWS($result) == 1) {
88 // Load id and mail type
89 $data = SQL_FETCHARRAY($result);
93 SQL_FREERESULT($result);
95 if ($data['data_type'] == 'TEMP') {
96 // No entry found, so we need to check out the stats table as well... :)
97 // We have to add that suff here, now we continue WITHOUT checking and check the text and subject against some filters
99 if (getConfig('allow_url_in_text') == 'Y') {
100 // Test submitted text against some filters (length, URLs in text etc.)
101 if ((isInStringIgnoreCase('https://', postRequestElement('text'))) || (isInStringIgnoreCase('http://', postRequestElement('text'))) || (isInStringIgnoreCase('www', postRequestElement('text')))) {
103 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('URL_FOUND');
106 // Remove new-line and carriage-return characters
107 $TEST = str_replace(array(PHP_EOL, chr(13)), array('', ''), postRequestElement('text'));
109 // Text length within allowed length?
110 if (strlen($TEST) > getConfig('max_tlength')) {
112 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('OVERLENGTH');
116 // Shall I test the subject line against URLs?
117 if (getConfig('allow_url_in_subject') == 'Y') {
118 // Check the subject line for issues
119 setPostRequestElement('subject', str_replace(chr(92), '[nl]', substr(postRequestElement('subject'), 0, 200)));
120 if ((isInStringIgnoreCase('https://', postRequestElement('subject'))) || (isInStringIgnoreCase('http://', postRequestElement('subject'))) || (isInStringIgnoreCase('www', postRequestElement('subject')))) {
121 // URL in subject found
122 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('SUBJECT_URL');
126 // And shall I check that his URL is not in the black list?
127 if ((isExtensionActive('blacklist')) && (isUrlBlacklisted(postRequestElement('url')))) {
128 // Create redirect-URL
129 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('BLIST_URL') . '&blist=' . $GLOBALS['blacklist_data'][postRequestElement('url')]['timestamp'];
132 // Enougth receivers entered?
133 if ((postRequestElement('receiver') < getConfig('order_min')) && (!isAdmin())) {
134 // Less than allowed receivers entered!
135 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS3');
139 if (!isUrlValid(postRequestElement('url'))) {
141 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('INVALID_URL');
144 // Probe for HTML extension
145 if (isExtensionActive('html_mail')) {
146 // HTML or regular text mail?
147 if (postRequestElement('html') == 'Y') {
148 // Chek for valid HTML tags
149 setPostRequestElement('text', checkHtmlTags(postRequestElement('text')));
151 // Maybe invalid tags found?
152 if (!isPostRequestElementSet('text')) $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('INVALID_TAGS') . '&id=' . $data['id'];
154 // Remove any HTML code
155 setPostRequestElement('text', str_replace(array('<', '>'), array('{OPEN_HTML}', '{CLOSE_HTML}'), postRequestElement('text')));
160 if ((!isPostRequestElementSet('mail_type')) || (postRequestElement('mail_type') < 1)) {
162 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('NO_MAIL_TYPE');
164 } elseif (!isAdmin()) {
165 // He has already sent a mail within a specific time
166 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('URL_TIME_LOCK') . '&id=' . $data['id'];
170 if (empty($data['url'])) {
172 $result = SQL_QUERY_ESC("SELECT
175 `{?_MYSQL_PREFIX?}_user_cats` AS `c`
177 `{?_MYSQL_PREFIX?}_user_data` AS `d`
179 `c`.`userid`=`d`.`userid`
182 `c`.`userid` != %s AND
183 `d`.`status`='CONFIRMED'
184 " . runFilterChain('user_exclusion_sql', ' ') . " AND
185 `d`.`receive_mails` > 0
187 `d`.`{?order_select?}` {?order_mode?}",
189 bigintval(postRequestElement('cat')),
191 ), __FILE__, __LINE__);
193 // Are there still receivers left?
194 if (SQL_NUMROWS($result) >= postRequestElement('receiver')) {
195 // Load receivers from database
196 $TEST = array(); $count = '0';
197 while ($holidayContent = SQL_FETCHARRAY($result)) {
198 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
199 // Check for his holiday status
200 $result_holiday = SQL_QUERY_ESC("SELECT
203 `{?_MYSQL_PREFIX?}_user_holidays`
206 `holiday_start` < UNIX_TIMESTAMP() AND
207 `holiday_end` > UNIX_TIMESTAMP()
209 array($holidayContent['userid']), __FILE__, __LINE__);
210 if (SQL_NUMROWS($result_holiday) == 1) {
211 // Exclude user who are in holiday
212 $holidayContent['userid'] = '0';
216 SQL_FREERESULT($result_holiday);
219 if (isValidId($holidayContent['userid'])) {
221 array_push($TEST, $holidayContent['userid']);
227 SQL_FREERESULT($result);
229 // Implode array into string for the sending pool
230 $receiver = implode($TEST, ';');
232 // Count array for maximum sent
233 $content['target_send'] = count($TEST);
235 // Update receiver list
236 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receive_mails`=`receive_mails`-1 WHERE `userid` IN (%s) LIMIT %s",
238 convertReceivers($receiver),
239 bigintval($content['target_send'])
240 ), __FILE__, __LINE__);
242 // Is calculated max receivers larger than wanted receivers then reset it
243 if ($content['target_send'] > postRequestElement('receiver')) {
244 $content['target_send'] = bigintval(postRequestElement('receiver'));
247 // Calculate used points
248 $usedPoints = $content['target_send'] * getPaymentPrice(bigintval(postRequestElement('mail_type')));
250 // Fix empty zip code
251 if (!isPostRequestElementSet('zip')) {
252 setPostRequestElement('zip', 0);
255 // Check if he has enougth points for this order and selected more than 0 receivers
256 if (($usedPoints > 0) && ($usedPoints <= $totalPoints) && ($content['target_send'] > 0)) {
257 // Gettings points is okay, so we can add $usedPoints later from
258 if ((!isValidId($data['id'])) || ($data['data_type'] != 'TEMP')) {
261 if (isExtensionActive('html_mail')) {
262 // HTML extension is active
263 SQL_QUERY_ESC("INSERT INTO
264 `{?_MYSQL_PREFIX?}_pool`
294 postRequestElement('subject'),
295 postRequestElement('text'),
297 bigintval(postRequestElement('mail_type')),
298 postRequestElement('url'),
299 bigintval(postRequestElement('cat')),
300 bigintval($content['target_send']),
301 bigintval(postRequestElement('zip'), TRUE, FALSE),
302 postRequestElement('html')
303 ), __FILE__, __LINE__);
305 // No HTML extension is active
306 SQL_QUERY_ESC("INSERT INTO
307 `{?_MYSQL_PREFIX?}_pool`
335 postRequestElement('subject'),
336 postRequestElement('text'),
338 bigintval(postRequestElement('mail_type')),
339 postRequestElement('url'),
340 bigintval(postRequestElement('cat')),
341 bigintval($content['target_send']),
342 bigintval(postRequestElement('zip'), TRUE, FALSE),
343 ), __FILE__, __LINE__);
347 $data['id'] = SQL_INSERTID();
349 // Change current order
350 if (isExtensionActive('html_mail')) {
351 // HTML extension is active
352 SQL_QUERY_ESC("UPDATE
353 `{?_MYSQL_PREFIX?}_pool`
359 `timestamp`=UNIX_TIMESTAMP(),
369 postRequestElement('subject'),
370 postRequestElement('text'),
372 bigintval(postRequestElement('mail_type')),
373 postRequestElement('url'),
374 bigintval(postRequestElement('cat')),
375 $content['target_send'],
376 bigintval(postRequestElement('zip')),
377 postRequestElement('html'),
378 bigintval($data['id'])
379 ), __FILE__, __LINE__);
381 // No HTML extension is active
382 SQL_QUERY_ESC("UPDATE
383 `{?_MYSQL_PREFIX?}_pool`
389 `timestamp`=UNIX_TIMESTAMP(),
398 postRequestElement('subject'),
399 postRequestElement('text'),
401 bigintval(postRequestElement('mail_type')),
402 postRequestElement('url'),
403 bigintval(postRequestElement('cat')),
404 $content['target_send'],
405 bigintval(postRequestElement('zip')),
406 bigintval($data['id'])
407 ), __FILE__, __LINE__);
411 // Make sure only valid id numbers can pass
412 assert(isValidId($data['id']));
414 // Id is received so we can redirect the user, used points will be added when he send's out the mail
415 $data['url'] = 'modules.php?module=frametester&order=' . $data['id'];
416 } elseif ($content['target_send'] == '0') {
417 // Not enougth receivers found which can receive mails
418 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS2');
420 // No enougth points left!
421 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_POINTS');
424 // Ordered more mails than he can send in this category
425 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('NO_RECS_LEFT');
428 } elseif (postRequestElement('receiver') == '0') {
429 // Not enougth receivers selected
430 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS1');
431 } elseif (($ALLOWED == '0') && (getConfig('order_max_full') == 'ORDER')) {
432 // No more mail orders allowed
433 displayMessage('{--MEMBER_ORDER_ALLOWED_EXHAUSTED--}');
434 } elseif (getTotalUnconfirmedMails(getMemberId()) < getConfig('unconfirmed')) {
435 // Show only enabled categories to the user ...
436 $whereStatement = " WHERE `visible`='Y'";
438 // ... but all to the admin
439 if (isAdmin()) $whereStatement = '';
441 // Display order form
442 $result_cats = SQL_QUERY('SELECT
446 `{?_MYSQL_PREFIX?}_cats`
447 ' . $whereStatement . '
449 `sort` ASC', __FILE__, __LINE__);
451 // Some categories found?
452 if (!SQL_HASZERONUMS($result_cats)) {
453 // Enought points left?
454 if ($totalPoints > 0) {
455 // Initialize array...
462 // Enable HTML checking
463 // @TODO Rewrite this to a filter
464 $HTML = ''; $HOL_STRING = '';
465 if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
466 $HTML = " AND `html`='Y'";
468 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
469 // Extension's version is fine
470 $HOL_STRING = " AND `holiday_active`='N'";
473 // ... and begin loading stuff
474 while ($categoriesContent = SQL_FETCHARRAY($result_cats)) {
475 $categories['id'][] = bigintval($categoriesContent['id']);
476 array_push($categories['name'], $categoriesContent['cat']);
478 // Select users in current category
479 $result_userids = SQL_QUERY_ESC('SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s AND `userid` != %s ORDER BY `userid` ASC',
480 array(bigintval($categoriesContent['id']), getMemberId()), __FILE__, __LINE__);
483 while (list($userid) = SQL_FETCHROW($result_userids)) {
484 // Check for holiday system
485 $isHolidayActive = FALSE;
486 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
487 // Check user's holiday status
488 $result_holiday = SQL_QUERY_ESC("SELECT
489 COUNT(`d`.`userid`) AS `cnt`
491 `{?_MYSQL_PREFIX?}_user_data` AS `d`
493 `{?_MYSQL_PREFIX?}_user_holidays` AS `h`
495 `d`.`userid`=`h`.`userid`
498 `d`.`receive_mails` > 0 AND
499 `d`.`status`='CONFIRMED' AND
500 `d`.`holiday_active`='Y' AND
501 `h`.`holiday_start` < UNIX_TIMESTAMP() AND
502 `h`.`holiday_end` > UNIX_TIMESTAMP()
504 array(bigintval($userid)), __FILE__, __LINE__);
507 list($count) = SQL_FETCHROW($result_holiday);
510 SQL_FREERESULT($result_holiday);
512 // Is holiday is active?
513 $isHolidayActive = ($count == 1);
516 if ($isHolidayActive === FALSE) {
517 // Check if the user want's to receive mails?
518 $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",
519 array(bigintval($userid)), __FILE__, __LINE__);
521 if ((SQL_NUMROWS($result_ver) == 1) && (isPostRequestElementSet('zip')) && (isOrderMultiPageEnabled())) {
523 list($zip) = SQL_FETCHROW($result_ver);
524 if (substr($zip, 0, strlen(postRequestElement('zip'))) == postRequestElement('zip')) {
525 // Ok, ZIP code part is found
530 $userid_cnt += SQL_NUMROWS($result_ver);
534 SQL_FREERESULT($result_ver);
539 SQL_FREERESULT($result_userids);
540 array_push($categories['userids'], $userid_cnt);
544 SQL_FREERESULT($result_cats);
546 // Now we need to load the mail types...
547 $result = SQL_QUERY("SELECT `id`, `price`, `payment`, `mail_title` FROM `{?_MYSQL_PREFIX?}_payments` ORDER BY `payment` ASC", __FILE__, __LINE__);
550 if (!SQL_HASZERONUMS($result)) {
551 // Is the error code set?
552 if (isGetRequestElementSet('code')) {
553 // Display error message
554 displayMessage(getMessageFromErrorCode(getRequestElement('code')));
557 // Load all email types...
558 while ($payment = SQL_FETCHARRAY($result)) {
559 array_push($payments, $payment);
563 SQL_FREERESULT($result);
565 // Output user's points
566 $content['total_points'] = $totalPoints;
568 // Check how many mail orders he has placed today and how many he's allowed to send
569 switch (getConfig('order_max_full')) {
570 case 'MAX': // He is allowed to send as much as possible
571 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_MAX--}';
574 case 'ORDER': // He is allowed to send as much as he setup the receiving value
575 $content['order_max_full'] = sprintf(getMessage('MEMBER_ORDER_ALLOWED_RECEIVE'), $ALLOWED, getUserData('receive_mails'));
578 default: // Unknown/invalid
579 logDebugMessage(__FILE__, __LINE__, sprintf("Unknown order_mas_full config detected.", getConfig('order_max_full')));
580 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_UNKNOWN--}';
584 // Load final template
585 loadTemplate('member_order_points', FALSE, $content);
593 // Check if we already have an order placed and make it editable
594 $result = SQL_QUERY_ESC("SELECT
604 `{?_MYSQL_PREFIX?}_pool`
609 array(getMemberId()), __FILE__, __LINE__);
611 if (SQL_NUMROWS($result) == 1) {
613 $content = merge_array($content, SQL_FETCHARRAY($result));
615 // Fix max receivers when it is too much
616 if ((isset($categories['userids'][$content['cat_id']])) && ($content['target_send'] > $categories['userids'][$content['cat_id']])) {
618 $content['target_send'] = $categories['userids'][$content['cat_id']];
621 // Old order is grabbed
624 // Default output for that your members don't forget it...
625 $content['url'] = 'http://';
626 $content['target_send'] = '{?order_min?}';
627 $content['subject'] = '';
628 $content['text'] = '';
632 SQL_FREERESULT($result);
634 if ((isPostRequestElementSet('data')) || ((getOrderMultiPage() != 'Y') && ((!isAdmin()) && (!isExtensionActive('html_mail'))))) {
635 // Pre-output categories
636 $content['category_selection'] = generateCategoryOptionsList(((isExtensionActive('html_mail')) && (isPostRequestElementSet('html'))) ? postRequestElement('html') : 'N', getMemberId());
639 $content['payment_selection'] = '';
640 foreach ($payments as $key => $value) {
641 if (is_array($value)) {
642 // Output option line
643 $content['payment_selection'] .= ' <option value="' . $payments[$key]['id'] . '"';
644 if (($OLD_ORDER) && ($content['payment_id'] == $payments[$key]['id'])) $content['payment_selection'] .= ' selected="selected"';
645 $content['payment_selection'] .= '>{%pipe,translateComma=' . $payments[$key]['price'] . '%} {--PER_MAIL--} - ' . $payments[$key]['mail_title'] . ' - ' . round($payments[$key]['payment']) . ' {--PAYMENT--}</option>';
649 // No content is default
650 $content['zip_content'] = '';
652 if (isPostRequestElementSet('zip')) {
653 // Output entered ZIP code
654 $content['zip_content'] = loadTemplate('member_order_zip2', TRUE, postRequestElement('zip'));
657 // No HTML extension installed by default
658 $content['html_extension'] = '<input type="hidden" name="html" value="N" />';
661 if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
662 // Extension is active so output valid HTML tags
663 $content['html_extension'] = loadTemplate('member_order_html_ext', TRUE);
666 // Output form for page 2
667 loadTemplate('member_order_page2', FALSE, $content);
669 // No HTML extension installed by default
670 $content['html_extension'] = '<input type="hidden" name="html" value="N" />';
672 // Remember maybe entered ZIP code in constant
673 if (isExtensionActive('html_mail')) {
674 // Add some content when html extension is active
675 $content['html_extension'] = loadTemplate('member_order_html_intro', TRUE);
678 // Default is no ZIP code
679 $content['zip_content'] = '';
681 // Is sending to ZIP code enabled? (logged-in admin can always send to it)
682 if ((isOrderMultiPageEnabled()) || (isAdmin())) {
684 if (postRequestElement('zip') > 0) {
686 'zip' => bigintval(postRequestElement('zip'))
693 $content['zip_content'] = loadTemplate('member_order_zip1', TRUE, $data);
696 // Output form for page 1 (ZIP code or HTML)
697 loadTemplate('member_order_page1', FALSE, $content);
700 // No mail types defined
701 displayMessage('<span class="bad">{--MEMBER_ORDER_NO_PAYMENTS--}</span>');
705 displayMessage('<span class="bad">{--MEMBER_ORDER_NO_POINTS--}</span>');
708 // No cateogries are defined yet
709 displayMessage('<span class="bad">{--MEMBER_NO_CATEGORIES--}</span>');
712 // Please confirm some mails first
713 displayMessage('<span class="notice">{%message,MEMBER_ORDER_LINKS_LEFT=' . getTotalUnconfirmedMails(getMemberId()) . '%}</span>');
716 if (!empty($data['url'])) {
717 // Redirect to requested URL
718 redirectToUrl($data['url']);