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 - 2011 by Mailer Developer Team *
20 * For more information visit: http://www.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%}');
53 $ALLOWED = getUserData('receive_mails') - getUserData('mail_orders');
54 if (getConfig('order_max_full') == 'MAX') $ALLOWED = getUserData('receive_mails');
56 // Now check his points amount
57 $total = getTotalPoints(getMemberId());
59 if ((isExtensionInstalledAndNewer('holiday', '0.1.3')) && (isUserDataEnabled('holiday_active'))) {
61 displayMessage('{--MEMBER_HOLIDAY_ORDER_NOT_POSSIBLE--}');
62 } elseif ((isPostRequestParameterSet('frametester')) && ($ALLOWED > 0) && (postRequestParameter('receiver') > 0)) {
63 // Continue with the frametester, we first need to store the data temporary in the pool
65 // First we would like to store the data and get it's pool position back...
66 $result = SQL_QUERY_ESC("SELECT `id`,`data_type`
68 `{?_MYSQL_PREFIX?}_pool`
72 (UNIX_TIMESTAMP() - `timestamp`) >= {?url_tlock?}
76 postRequestParameter('url')
77 ), __FILE__, __LINE__);
79 $type = 'TEMP'; $id = '0';
80 if (SQL_NUMROWS($result) == 1) {
81 // Load id and mail type
82 // @TODO Rewrite this to SQL_FETCHARRAY()
83 list($id, $type) = SQL_FETCHROW($result);
87 SQL_FREERESULT($result);
89 if ($type == 'TEMP') {
90 // No entry found, so we need to check out the stats table as well... :)
91 // We have to add that suff here, now we continue WITHOUT checking and check the text and subject against some filters
93 if (getConfig('allow_url_in_text') == 'Y') {
94 // Test submitted text against some filters (length, URLs in text etc.)
95 if ((strpos(strtolower(postRequestParameter('text')), 'https://') > -1) || (strpos(strtolower(postRequestParameter('text')), 'http://') > -1) || (strpos(strtolower(postRequestParameter('text')), "www") > -1)) {
97 $url = 'modules.php?module=login&what=order&code=' . getCode('URL_FOUND');
100 // Remove new-line and carriage-return characters
101 $TEST = str_replace("\n", '', str_replace("\r", '', postRequestParameter('text')));
103 // Text length within allowed length?
104 if (strlen($TEST) > getConfig('max_tlength')) {
106 $url = 'modules.php?module=login&what=order&code=' . getCode('OVERLENGTH');
110 // Shall I test the subject line against URLs?
111 if (getConfig('allow_url_in_subject') == 'Y') {
112 // Check the subject line for issues
113 setPostRequestParameter('subject', str_replace("\\", '[nl]', substr(postRequestParameter('subject'), 0, 200)));
114 if ((strpos(strtolower(postRequestParameter('subject')), 'http://') > -1) || (strpos(strtolower(postRequestParameter('subject')), "www") > -1)) {
115 // URL in subject found
116 $url = 'modules.php?module=login&what=order&code=' . getCode('SUBJECT_URL');
120 // And shall I check that his URL is not in the black list?
121 if (isUrlBlacklistEnabled()) {
122 // Ok, I do that for you know...
123 $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`timestamp`) AS tstamp FROM `{?_MYSQL_PREFIX?}_url_blacklist` WHERE `url`='%s' LIMIT 1",
124 array(postRequestParameter('url')), __FILE__, __LINE__);
126 if (SQL_NUMROWS($result) == 1) {
127 // Jupp, we got one listed
128 list($blist) = SQL_FETCHROW($result);
130 // Create redirect-URL
131 $url = 'modules.php?module=login&what=order&code=' . getCode('BLIST_URL') . '&blist=' . $blist;
135 SQL_FREERESULT($result);
138 // Enougth receivers entered?
139 if ((postRequestParameter('receiver') < getConfig('order_min')) && (!isAdmin())) {
140 // Less than allowed receivers entered!
141 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS3');
145 if (!isUrlValid(postRequestParameter('url'))) {
147 $url = 'modules.php?module=login&what=order&code=' . getCode('INVALID_URL');
150 // Probe for HTML extension
151 if (isExtensionActive('html_mail')) {
152 // HTML or regular text mail?
153 if (postRequestParameter('html') == 'Y') {
154 // Chek for valid HTML tags
155 setPostRequestParameter('text', checkHtmlTags(postRequestParameter('text')));
157 // Maybe invalid tags found?
158 if (!isPostRequestParameterSet('text')) $url = 'modules.php?module=login&what=order&code=' . getCode('INVALID_TAGS')."&id=".$id;
160 // Remove any HTML code
161 setPostRequestParameter('text', str_replace('<', '{OPEN_HTML}', str_replace('>', '{CLOSE_HTML}', postRequestParameter('text'))));
166 if ((!isPostRequestParameterSet('mail_type')) || (postRequestParameter('mail_type') < 1)) {
168 $url = 'modules.php?module=login&what=order&code=' . getCode('NO_MAIL_TYPE');
170 } elseif (!isAdmin()) {
171 // He has already sent a mail within a specific time
172 $url = 'modules.php?module=login&what=order&code=' . getCode('URL_TIME_LOCK') . '&id=' . $id;
177 // Check if category and number of receivers is okay
179 if ((isOrderMultiPageEnabled()) && (isPostRequestParameterSet('zip')) && (postRequestParameter('zip') != '')) {
180 // Choose recipients by ZIP code
181 $add = sprintf(" AND d.zip LIKE '%s%%'",
182 bigintval(postRequestParameter('zip'))
187 $result = SQL_QUERY_ESC("SELECT
190 `{?_MYSQL_PREFIX?}_user_cats` AS c
192 `{?_MYSQL_PREFIX?}_user_data` AS d
198 d.`status`='CONFIRMED' AND
202 d.{?order_select?} {?order_mode?}",
204 bigintval(postRequestParameter('cat')),
206 ), __FILE__, __LINE__);
208 // Do we enougth receivers left?
209 if (SQL_NUMROWS($result) >= postRequestParameter('receiver')) {
210 // Load receivers from database
211 $TEST = array(); $count = '0';
212 while ($holidayContent = SQL_FETCHARRAY($result)) {
213 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
214 // Check for his holiday status
215 $result_holiday = SQL_QUERY_ESC("SELECT
218 `{?_MYSQL_PREFIX?}_user_holidays`
221 `holiday_start` < UNIX_TIMESTAMP() AND
222 `holiday_end` > UNIX_TIMESTAMP()
224 array($holidayContent['userid']), __FILE__, __LINE__);
225 if (SQL_NUMROWS($result_holiday) == 1) {
226 // Exclude user who are in holiday
227 $holidayContent['userid'] = '0';
231 SQL_FREERESULT($result_holiday);
234 if ($holidayContent['userid'] > 0) {
236 $TEST[] = $holidayContent['userid'];
242 SQL_FREERESULT($result);
244 // Implode array into string for the sending pool
245 $receiver = implode($TEST, ';');
247 // Count array for maximum sent
248 $content['target_send'] = count($TEST);
250 // Update receiver list
251 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receive_mails`=`receive_mails`-1 WHERE `userid` IN (%s) LIMIT %s",
253 convertReceivers($receiver),
254 bigintval($content['target_send'])
255 ), __FILE__, __LINE__);
257 // Is calculated max receivers larger than wanted receivers then reset it
258 if ($content['target_send'] > postRequestParameter('receiver')) {
259 $content['target_send'] = bigintval(postRequestParameter('receiver'));
262 // Calculate used points
263 $USED = $content['target_send'] * getPaymentPoints(bigintval(postRequestParameter('mail_type')));
265 // Fix empty zip code
266 if (!isPostRequestParameterSet('zip')) {
267 setPostRequestParameter('zip', 0);
270 // Check if he has enougth points for this order and selected more than 0 receivers
271 if (($USED > 0) && ($USED <= $total) && ($content['target_send'] > 0)) {
272 // Gettings points is okay, so we can add $USED later from
273 if (($id == '0') || ($type != 'TEMP')) {
276 if (isExtensionActive('html_mail')) {
277 // HTML extension is active
278 SQL_QUERY_ESC("INSERT INTO
279 `{?_MYSQL_PREFIX?}_pool`
309 postRequestParameter('subject'),
310 postRequestParameter('text'),
312 bigintval(postRequestParameter('mail_type')),
313 postRequestParameter('url'),
314 bigintval(postRequestParameter('cat')),
315 bigintval($content['target_send']),
316 bigintval(postRequestParameter('zip'), true, false),
317 postRequestParameter('html')
318 ), __FILE__, __LINE__);
320 // No HTML extension is active
321 SQL_QUERY_ESC("INSERT INTO
322 `{?_MYSQL_PREFIX?}_pool`
350 postRequestParameter('subject'),
351 postRequestParameter('text'),
353 bigintval(postRequestParameter('mail_type')),
354 postRequestParameter('url'),
355 bigintval(postRequestParameter('cat')),
356 bigintval($content['target_send']),
357 bigintval(postRequestParameter('zip'), true, false),
358 ), __FILE__, __LINE__);
361 // Change current order
362 if (isExtensionActive('html_mail')) {
363 // HTML extension is active
364 SQL_QUERY_ESC("UPDATE
365 `{?_MYSQL_PREFIX?}_pool`
371 `timestamp`=UNIX_TIMESTAMP(),
381 postRequestParameter('subject'),
382 postRequestParameter('text'),
384 bigintval(postRequestParameter('mail_type')),
385 postRequestParameter('url'),
386 bigintval(postRequestParameter('cat')),
387 $content['target_send'],
388 bigintval(postRequestParameter('zip')),
389 postRequestParameter('html'),
391 ), __FILE__, __LINE__);
393 // No HTML extension is active
394 SQL_QUERY_ESC("UPDATE
395 `{?_MYSQL_PREFIX?}_pool`
401 `timestamp`=UNIX_TIMESTAMP(),
410 postRequestParameter('subject'),
411 postRequestParameter('text'),
413 bigintval(postRequestParameter('mail_type')),
414 postRequestParameter('url'),
415 bigintval(postRequestParameter('cat')),
416 $content['target_send'],
417 bigintval(postRequestParameter('zip')),
419 ), __FILE__, __LINE__);
423 // Do we need to get the id number?
425 // Order is placed as temporary. We need to get it's id for the frametester
426 $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",
429 postRequestParameter('subject'),
430 bigintval(postRequestParameter('mail_type'))
431 ), __FILE__, __LINE__);
434 list($id) = SQL_FETCHROW($result);
437 SQL_FREERESULT($result);
440 // id is received so we can redirect the user, used points will be added when he send's out the mail
441 $url = 'modules.php?module=frametester&order=' . $id;
442 } elseif ($content['target_send'] == '0') {
443 // Not enougth receivers found which can receive mails
444 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS2');
446 // No enougth points left!
447 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_POINTS');
450 // Ordered more mails than he can send in this category
451 $url = 'modules.php?module=login&what=order&code=' . getCode('NO_RECS_LEFT');
454 } elseif (postRequestParameter('receiver') == '0') {
455 // Not enougth receivers selected
456 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS1');
457 } elseif (($ALLOWED == '0') && (getConfig('order_max_full') == 'ORDER')) {
458 // No more mail orders allowed
459 displayMessage('{--MEMBER_ORDER_ALLOWED_EXHAUSTED--}');
460 } elseif (getTotalUnconfirmedMails(getMemberId()) < getConfig('unconfirmed')) {
461 // Show only enabled categories to the user ...
462 $whereStatement = " WHERE `visible`='Y'";
464 // ... but all to the admin
465 if (isAdmin()) $whereStatement = '';
467 // Display order form
468 $result_cats = SQL_QUERY("SELECT
471 `{?_MYSQL_PREFIX?}_cats`
474 `sort` ASC", __FILE__, __LINE__);
475 if (!SQL_HASZERONUMS($result_cats)) {
477 // Initialize array...
484 // Enable HTML checking
485 // @TODO Rewrite this to a filter
486 $HTML = ''; $HOL_STRING = '';
487 if ((isExtensionActive('html_mail')) && (postRequestParameter('html') == 'Y')) {
488 $HTML = " AND `html`='Y'";
490 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
491 // Extension's version is fine
492 $HOL_STRING = " AND `holiday_active`='N'";
495 // ... and begin loading stuff
496 while ($categoriesContent = SQL_FETCHARRAY($result_cats)) {
497 $categories['id'][] = bigintval($categoriesContent['id']);
498 $categories['name'][] = $categoriesContent['cat'];
500 // Select users in current category
501 $result_userids = SQL_QUERY_ESC("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s AND `userid` != '%s' ORDER BY `userid` ASC",
502 array(bigintval($categoriesContent['id']), getMemberId()), __FILE__, __LINE__);
505 while (list($userid) = SQL_FETCHROW($result_userids)) {
506 // Check for holiday system
507 $isHolidayActive = false;
508 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
509 // Check user's holiday status
510 $result_holiday = SQL_QUERY_ESC("SELECT
511 COUNT(d.userid) AS `cnt`
513 `{?_MYSQL_PREFIX?}_user_data` AS d
515 `{?_MYSQL_PREFIX?}_user_holidays` AS h
520 d.receive_mails > 0 AND
521 d.`status`='CONFIRMED' AND
522 d.`holiday_active`='Y' AND
523 h.holiday_start < UNIX_TIMESTAMP() AND
524 h.holiday_end > UNIX_TIMESTAMP()
526 array(bigintval($userid)), __FILE__, __LINE__);
529 list($count) = SQL_FETCHROW($result_holiday);
532 SQL_FREERESULT($result_holiday);
534 // Is holiday is active?
535 $isHolidayActive = ($count == 1);
538 if ($isHolidayActive === false) {
539 // Check if the user want's to receive mails?
540 $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",
541 array(bigintval($userid)), __FILE__, __LINE__);
543 if ((SQL_NUMROWS($result_ver) == 1) && (isPostRequestParameterSet('zip')) && (isOrderMultiPageEnabled())) {
545 list($zip) = SQL_FETCHROW($result_ver);
546 if (substr($zip, 0, strlen(postRequestParameter('zip'))) == postRequestParameter('zip')) {
547 // Ok, ZIP code part is found
552 $userid_cnt += SQL_NUMROWS($result_ver);
556 SQL_FREERESULT($result_ver);
561 SQL_FREERESULT($result_userids);
562 $categories['userids'][] = $userid_cnt;
566 SQL_FREERESULT($result_cats);
568 // Now we need to load the mail types...
569 $result = SQL_QUERY("SELECT `id`,`price`,`payment`,`mail_title` FROM `{?_MYSQL_PREFIX?}_payments` ORDER BY `payment` ASC", __FILE__, __LINE__);
572 if (!SQL_HASZERONUMS($result)) {
573 // Check for message id in URL
574 $message = getMessageFromErrorCode(getRequestParameter('code'));
576 if (!empty($message)) {
577 // We got system message so we drop it out to the user
578 displayMessage($message);
581 // Load all email types...
582 while ($types[] = SQL_FETCHROW($result)) {
583 // Nothing to do here... ;-)
587 SQL_FREERESULT($result);
589 // Output user's points
590 $content['total_points'] = $total;
592 // Check how many mail orders he has placed today and how many he's allowed to send
593 switch (getConfig('order_max_full')) {
594 case 'MAX': // He is allowed to send as much as possible
595 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_MAX--}';
598 case 'ORDER': // He is allowed to send as much as he setup the receiving value
599 $content['order_max_full'] = sprintf(getMessage('MEMBER_ORDER_ALLOWED_RECEIVE'), $ALLOWED, getUserData('receive_mails'));
602 default: // Unknown/invalid
603 logDebugMessage(__FILE__, __LINE__, sprintf("Unknown order_mas_full config detected.", getConfig('order_max_full')));
604 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_UNKNOWN--}';
608 // Load final template
609 loadTemplate('member_order_points', false, $content);
617 // Check if we already have an order placed and make it editable
618 $result = SQL_QUERY_ESC("SELECT
619 `subject`,`text`,`payment_id`,`timestamp`,`url`,`target_send`,`cat_id`,`zip`
621 `{?_MYSQL_PREFIX?}_pool`
626 array(getMemberId()), __FILE__, __LINE__);
628 if (SQL_NUMROWS($result) == 1) {
630 $content = merge_array($content, SQL_FETCHARRAY($result));
632 // Fix max receivers when it is too much
633 if ((isset($categories['userids'][$content['cat_id']])) && ($content['target_send'] > $categories['userids'][$content['cat_id']])) {
635 $content['target_send'] = $categories['userids'][$content['cat_id']];
638 // Old order is grabbed
641 // Default output for that your members don't forget it...
642 $content['url'] = 'http://';
643 $content['target_send'] = '{?order_min?}';
644 $content['subject'] = '{--ORDER_DEFAULT_SUBJECT--}';
645 $content['text'] = '{--ORDER_DEFAULT_TEXT--}';
649 SQL_FREERESULT($result);
651 if ((isPostRequestParameterSet('data')) || ((getOrderMultiPage() != 'Y') && ((!isAdmin()) && (!isExtensionActive('html_mail'))))) {
652 // Pre-output categories
653 $content['category_selection'] = '';
654 foreach ($categories['id'] as $key => $value) {
655 $content['category_selection'] .= ' <option value="' . $value . '"';
656 if (($OLD_ORDER) && ($content['cat_id'] == $value)) $content['category_selection'] .= ' selected="selected"';
657 $content['category_selection'] .= '>' . $categories['name'][$key]." (".$categories['userids'][$key] . ' {--USER_IN_CAT--})</option>';
661 $content['type_selection'] = '';
662 foreach ($types as $key => $value) {
663 if (is_array($value)) {
664 // Output option line
665 $content['type_selection'] .= ' <option value="' . $types[$key][0] . '"';
666 if (($OLD_ORDER) && ($content['payment_id'] == $types[$key][0])) $content['type_selection'] .= ' selected="selected"';
667 $content['type_selection'] .= '>{%pipe,translateComma=' . $types[$key][1] . '%} {--PER_MAIL--} - ' . $types[$key][3] . ' - ' . round($types[$key][2]) . ' {--PAYMENT--}</option>';
671 // No content is default
672 $content['zip_content'] = '';
674 if (isPostRequestParameterSet('zip')) {
675 // Output entered ZIP code
676 $content['zip_content'] = loadTemplate('member_order-zip2', true, postRequestParameter('zip'));
680 if ((isExtensionActive('html_mail')) && (postRequestParameter('html') == 'Y')) {
681 // Extension is active so output valid HTML tags
682 $content['html_extension'] = loadTemplate('member_order-html_ext', true, addValidHtmlTags());
684 // Extension not active and/or class not uploaded
685 $content['html_extension'] = '<tr><td colspan="3"><input type="hidden" name="html" value="N" /></td></tr>';
688 // Output form for page 2
689 loadTemplate('member_order_page2', false, $content);
691 // Remember maybe entered ZIP code in constant
692 if (isExtensionActive('html_mail')) {
693 // Add some content when html extension is active
694 $content['html_extension'] = loadTemplate('member_order-html_intro', true);
696 // No HTML extension installed
697 $content['html_extension'] = '<tr><td colspan="3"><input type="hidden" name="html" value="N" /></td></tr>';
700 // Default is no ZIP code
701 $content['zip_content'] = '';
703 // Do we want ZIP code or not?
704 if ((isOrderMultiPageEnabled()) || (isAdmin())) {
706 if (postRequestParameter('zip') > 0) {
708 'zip' => bigintval(postRequestParameter('zip'))
715 $content['zip_content'] = loadTemplate('member_order-zip1', true, $data);
718 // Output form for page 1 (ZIP code or HTML)
719 loadTemplate('member_order_page1', false, $content);
722 // No mail types defined
723 displayMessage('<span class="notice">{--MEMBER_ORDER_NO_PAYMENTS--}</span>');
727 displayMessage('<span class="notice">{--MEMBER_ORDER_NO_POINTS--}</span>');
730 // No cateogries are defined yet
731 displayMessage('<span class="notice">{--MEMBER_NO_CATEGORIES--}</span>');
734 // Please confirm some mails first
735 displayMessage('<span class="notice">{%message,MEMBER_ORDER_LINKS_LEFT=' . getTotalUnconfirmedMails(getMemberId()) . '%}</span>');
739 // Redirect to requested URL