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%}');
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 $totalPoints = getTotalPoints(getMemberId());
59 if ((isExtensionInstalledAndNewer('holiday', '0.1.3')) && (isUserDataEnabled('holiday_active'))) {
61 displayMessage('{--MEMBER_HOLIDAY_ORDER_NOT_POSSIBLE--}');
62 } elseif ((isPostRequestElementSet('frametester')) && ($ALLOWED > 0) && (postRequestElement('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
70 `{?_MYSQL_PREFIX?}_pool`
74 (UNIX_TIMESTAMP() - `timestamp`) >= {?url_tlock?}
78 postRequestElement('url')
79 ), __FILE__, __LINE__);
81 $type = 'TEMP'; $id = '0';
82 if (SQL_NUMROWS($result) == 1) {
83 // Load id and mail type
84 // @TODO Rewrite this to SQL_FETCHARRAY()
85 list($id, $type) = SQL_FETCHROW($result);
89 SQL_FREERESULT($result);
91 if ($type == 'TEMP') {
92 // No entry found, so we need to check out the stats table as well... :)
93 // We have to add that suff here, now we continue WITHOUT checking and check the text and subject against some filters
95 if (getConfig('allow_url_in_text') == 'Y') {
96 // Test submitted text against some filters (length, URLs in text etc.)
97 if ((isInStringIgnoreCase('https://', postRequestElement('text'))) || (isInStringIgnoreCase('http://', postRequestElement('text'))) || (isInStringIgnoreCase('www', postRequestElement('text')))) {
99 $url = 'modules.php?module=login&what=order&code=' . getCode('URL_FOUND');
102 // Remove new-line and carriage-return characters
103 $TEST = str_replace(array(chr(10), chr(13)), array('', ''), postRequestElement('text'));
105 // Text length within allowed length?
106 if (strlen($TEST) > getConfig('max_tlength')) {
108 $url = 'modules.php?module=login&what=order&code=' . getCode('OVERLENGTH');
112 // Shall I test the subject line against URLs?
113 if (getConfig('allow_url_in_subject') == 'Y') {
114 // Check the subject line for issues
115 setPostRequestElement('subject', str_replace(chr(92), '[nl]', substr(postRequestElement('subject'), 0, 200)));
116 if ((isInStringIgnoreCase('https://', postRequestElement('subject'))) || (isInStringIgnoreCase('http://', postRequestElement('subject'))) || (isInStringIgnoreCase('www', postRequestElement('subject')))) {
117 // URL in subject found
118 $url = 'modules.php?module=login&what=order&code=' . getCode('SUBJECT_URL');
122 // And shall I check that his URL is not in the black list?
123 if (isUrlBlacklistEnabled()) {
124 // Ok, I do that for you know...
125 $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`timestamp`) AS tstamp FROM `{?_MYSQL_PREFIX?}_url_blacklist` WHERE `url`='%s' LIMIT 1",
126 array(postRequestElement('url')), __FILE__, __LINE__);
128 if (SQL_NUMROWS($result) == 1) {
129 // Jupp, we got one listed
130 list($blist) = SQL_FETCHROW($result);
132 // Create redirect-URL
133 $url = 'modules.php?module=login&what=order&code=' . getCode('BLIST_URL') . '&blist=' . $blist;
137 SQL_FREERESULT($result);
140 // Enougth receivers entered?
141 if ((postRequestElement('receiver') < getConfig('order_min')) && (!isAdmin())) {
142 // Less than allowed receivers entered!
143 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS3');
147 if (!isUrlValid(postRequestElement('url'))) {
149 $url = 'modules.php?module=login&what=order&code=' . getCode('INVALID_URL');
152 // Probe for HTML extension
153 if (isExtensionActive('html_mail')) {
154 // HTML or regular text mail?
155 if (postRequestElement('html') == 'Y') {
156 // Chek for valid HTML tags
157 setPostRequestElement('text', checkHtmlTags(postRequestElement('text')));
159 // Maybe invalid tags found?
160 if (!isPostRequestElementSet('text')) $url = 'modules.php?module=login&what=order&code=' . getCode('INVALID_TAGS')."&id=".$id;
162 // Remove any HTML code
163 setPostRequestElement('text', str_replace(array('<', '>'), array('{OPEN_HTML}', '{CLOSE_HTML}'), postRequestElement('text')));
168 if ((!isPostRequestElementSet('mail_type')) || (postRequestElement('mail_type') < 1)) {
170 $url = 'modules.php?module=login&what=order&code=' . getCode('NO_MAIL_TYPE');
172 } elseif (!isAdmin()) {
173 // He has already sent a mail within a specific time
174 $url = 'modules.php?module=login&what=order&code=' . getCode('URL_TIME_LOCK') . '&id=' . $id;
180 $result = SQL_QUERY_ESC("SELECT
183 `{?_MYSQL_PREFIX?}_user_cats` AS c
185 `{?_MYSQL_PREFIX?}_user_data` AS d
187 c.`userid`=d.`userid`
191 d.`status`='CONFIRMED'
192 " . runFilterChain('user_exclusion_sql', ' ') . " AND
193 d.`receive_mails` > 0
195 d.`{?order_select?}` {?order_mode?}",
197 bigintval(postRequestElement('cat')),
199 ), __FILE__, __LINE__);
201 // Are there still receivers left?
202 if (SQL_NUMROWS($result) >= postRequestElement('receiver')) {
203 // Load receivers from database
204 $TEST = array(); $count = '0';
205 while ($holidayContent = SQL_FETCHARRAY($result)) {
206 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
207 // Check for his holiday status
208 $result_holiday = SQL_QUERY_ESC("SELECT
211 `{?_MYSQL_PREFIX?}_user_holidays`
214 `holiday_start` < UNIX_TIMESTAMP() AND
215 `holiday_end` > UNIX_TIMESTAMP()
217 array($holidayContent['userid']), __FILE__, __LINE__);
218 if (SQL_NUMROWS($result_holiday) == 1) {
219 // Exclude user who are in holiday
220 $holidayContent['userid'] = '0';
224 SQL_FREERESULT($result_holiday);
227 if ($holidayContent['userid'] > 0) {
229 array_push($TEST, $holidayContent['userid']);
235 SQL_FREERESULT($result);
237 // Implode array into string for the sending pool
238 $receiver = implode($TEST, ';');
240 // Count array for maximum sent
241 $content['target_send'] = count($TEST);
243 // Update receiver list
244 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receive_mails`=`receive_mails`-1 WHERE `userid` IN (%s) LIMIT %s",
246 convertReceivers($receiver),
247 bigintval($content['target_send'])
248 ), __FILE__, __LINE__);
250 // Is calculated max receivers larger than wanted receivers then reset it
251 if ($content['target_send'] > postRequestElement('receiver')) {
252 $content['target_send'] = bigintval(postRequestElement('receiver'));
255 // Calculate used points
256 $usedPoints = $content['target_send'] * getPaymentData(bigintval(postRequestElement('mail_type')));
258 // Fix empty zip code
259 if (!isPostRequestElementSet('zip')) {
260 setPostRequestElement('zip', 0);
263 // Check if he has enougth points for this order and selected more than 0 receivers
264 if (($usedPoints > 0) && ($usedPoints <= $totalPoints) && ($content['target_send'] > 0)) {
265 // Gettings points is okay, so we can add $usedPoints later from
266 if (($id == '0') || ($type != 'TEMP')) {
269 if (isExtensionActive('html_mail')) {
270 // HTML extension is active
271 SQL_QUERY_ESC("INSERT INTO
272 `{?_MYSQL_PREFIX?}_pool`
302 postRequestElement('subject'),
303 postRequestElement('text'),
305 bigintval(postRequestElement('mail_type')),
306 postRequestElement('url'),
307 bigintval(postRequestElement('cat')),
308 bigintval($content['target_send']),
309 bigintval(postRequestElement('zip'), true, false),
310 postRequestElement('html')
311 ), __FILE__, __LINE__);
313 // No HTML extension is active
314 SQL_QUERY_ESC("INSERT INTO
315 `{?_MYSQL_PREFIX?}_pool`
343 postRequestElement('subject'),
344 postRequestElement('text'),
346 bigintval(postRequestElement('mail_type')),
347 postRequestElement('url'),
348 bigintval(postRequestElement('cat')),
349 bigintval($content['target_send']),
350 bigintval(postRequestElement('zip'), true, false),
351 ), __FILE__, __LINE__);
355 $id = SQL_INSERTID();
357 // Change current order
358 if (isExtensionActive('html_mail')) {
359 // HTML extension is active
360 SQL_QUERY_ESC("UPDATE
361 `{?_MYSQL_PREFIX?}_pool`
367 `timestamp`=UNIX_TIMESTAMP(),
377 postRequestElement('subject'),
378 postRequestElement('text'),
380 bigintval(postRequestElement('mail_type')),
381 postRequestElement('url'),
382 bigintval(postRequestElement('cat')),
383 $content['target_send'],
384 bigintval(postRequestElement('zip')),
385 postRequestElement('html'),
387 ), __FILE__, __LINE__);
389 // No HTML extension is active
390 SQL_QUERY_ESC("UPDATE
391 `{?_MYSQL_PREFIX?}_pool`
397 `timestamp`=UNIX_TIMESTAMP(),
406 postRequestElement('subject'),
407 postRequestElement('text'),
409 bigintval(postRequestElement('mail_type')),
410 postRequestElement('url'),
411 bigintval(postRequestElement('cat')),
412 $content['target_send'],
413 bigintval(postRequestElement('zip')),
415 ), __FILE__, __LINE__);
419 // Make sure only valid id numbers can pass
420 assert((!is_bool($id)) && ($id > 0));
422 // Id is received so we can redirect the user, used points will be added when he send's out the mail
423 $url = 'modules.php?module=frametester&order=' . $id;
424 } elseif ($content['target_send'] == '0') {
425 // Not enougth receivers found which can receive mails
426 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS2');
428 // No enougth points left!
429 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_POINTS');
432 // Ordered more mails than he can send in this category
433 $url = 'modules.php?module=login&what=order&code=' . getCode('NO_RECS_LEFT');
436 } elseif (postRequestElement('receiver') == '0') {
437 // Not enougth receivers selected
438 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS1');
439 } elseif (($ALLOWED == '0') && (getConfig('order_max_full') == 'ORDER')) {
440 // No more mail orders allowed
441 displayMessage('{--MEMBER_ORDER_ALLOWED_EXHAUSTED--}');
442 } elseif (getTotalUnconfirmedMails(getMemberId()) < getConfig('unconfirmed')) {
443 // Show only enabled categories to the user ...
444 $whereStatement = " WHERE `visible`='Y'";
446 // ... but all to the admin
447 if (isAdmin()) $whereStatement = '';
449 // Display order form
450 $result_cats = SQL_QUERY("SELECT
454 `{?_MYSQL_PREFIX?}_cats`
457 `sort` ASC", __FILE__, __LINE__);
459 // Some categories found?
460 if (!SQL_HASZERONUMS($result_cats)) {
461 // Enought points left?
462 if ($totalPoints > 0) {
463 // Initialize array...
470 // Enable HTML checking
471 // @TODO Rewrite this to a filter
472 $HTML = ''; $HOL_STRING = '';
473 if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
474 $HTML = " AND `html`='Y'";
476 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
477 // Extension's version is fine
478 $HOL_STRING = " AND `holiday_active`='N'";
481 // ... and begin loading stuff
482 while ($categoriesContent = SQL_FETCHARRAY($result_cats)) {
483 $categories['id'][] = bigintval($categoriesContent['id']);
484 array_push($categories['name'], $categoriesContent['cat']);
486 // Select users in current category
487 $result_userids = SQL_QUERY_ESC("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s AND `userid` != '%s' ORDER BY `userid` ASC",
488 array(bigintval($categoriesContent['id']), getMemberId()), __FILE__, __LINE__);
491 while (list($userid) = SQL_FETCHROW($result_userids)) {
492 // Check for holiday system
493 $isHolidayActive = false;
494 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
495 // Check user's holiday status
496 $result_holiday = SQL_QUERY_ESC("SELECT
497 COUNT(d.`userid`) AS `cnt`
499 `{?_MYSQL_PREFIX?}_user_data` AS d
501 `{?_MYSQL_PREFIX?}_user_holidays` AS h
503 d.`userid`=h.`userid`
506 d.`receive_mails` > 0 AND
507 d.`status`='CONFIRMED' AND
508 d.`holiday_active`='Y' AND
509 h.`holiday_start` < UNIX_TIMESTAMP() AND
510 h.`holiday_end` > UNIX_TIMESTAMP()
512 array(bigintval($userid)), __FILE__, __LINE__);
515 list($count) = SQL_FETCHROW($result_holiday);
518 SQL_FREERESULT($result_holiday);
520 // Is holiday is active?
521 $isHolidayActive = ($count == 1);
524 if ($isHolidayActive === false) {
525 // Check if the user want's to receive mails?
526 $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",
527 array(bigintval($userid)), __FILE__, __LINE__);
529 if ((SQL_NUMROWS($result_ver) == 1) && (isPostRequestElementSet('zip')) && (isOrderMultiPageEnabled())) {
531 list($zip) = SQL_FETCHROW($result_ver);
532 if (substr($zip, 0, strlen(postRequestElement('zip'))) == postRequestElement('zip')) {
533 // Ok, ZIP code part is found
538 $userid_cnt += SQL_NUMROWS($result_ver);
542 SQL_FREERESULT($result_ver);
547 SQL_FREERESULT($result_userids);
548 array_push($categories['userids'], $userid_cnt);
552 SQL_FREERESULT($result_cats);
554 // Now we need to load the mail types...
555 $result = SQL_QUERY("SELECT `id`,`price`,`payment`,`mail_title` FROM `{?_MYSQL_PREFIX?}_payments` ORDER BY `payment` ASC", __FILE__, __LINE__);
558 if (!SQL_HASZERONUMS($result)) {
559 // Is the error code set?
560 if (isGetRequestElementSet('code')) {
561 // Display error message
562 displayMessage(getMessageFromErrorCode(getRequestElement('code')));
565 // Load all email types...
566 while ($type = SQL_FETCHARRAY($result)) {
567 array_push($types, $type);
571 SQL_FREERESULT($result);
573 // Output user's points
574 $content['total_points'] = $totalPoints;
576 // Check how many mail orders he has placed today and how many he's allowed to send
577 switch (getConfig('order_max_full')) {
578 case 'MAX': // He is allowed to send as much as possible
579 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_MAX--}';
582 case 'ORDER': // He is allowed to send as much as he setup the receiving value
583 $content['order_max_full'] = sprintf(getMessage('MEMBER_ORDER_ALLOWED_RECEIVE'), $ALLOWED, getUserData('receive_mails'));
586 default: // Unknown/invalid
587 logDebugMessage(__FILE__, __LINE__, sprintf("Unknown order_mas_full config detected.", getConfig('order_max_full')));
588 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_UNKNOWN--}';
592 // Load final template
593 loadTemplate('member_order_points', false, $content);
601 // Check if we already have an order placed and make it editable
602 $result = SQL_QUERY_ESC("SELECT
612 `{?_MYSQL_PREFIX?}_pool`
617 array(getMemberId()), __FILE__, __LINE__);
619 if (SQL_NUMROWS($result) == 1) {
621 $content = merge_array($content, SQL_FETCHARRAY($result));
623 // Fix max receivers when it is too much
624 if ((isset($categories['userids'][$content['cat_id']])) && ($content['target_send'] > $categories['userids'][$content['cat_id']])) {
626 $content['target_send'] = $categories['userids'][$content['cat_id']];
629 // Old order is grabbed
632 // Default output for that your members don't forget it...
633 $content['url'] = 'http://';
634 $content['target_send'] = '{?order_min?}';
635 $content['subject'] = '';
636 $content['text'] = '';
640 SQL_FREERESULT($result);
642 if ((isPostRequestElementSet('data')) || ((getOrderMultiPage() != 'Y') && ((!isAdmin()) && (!isExtensionActive('html_mail'))))) {
643 // Pre-output categories
644 $content['category_selection'] = generateCategoryOptionsList(((isExtensionActive('html_mail')) && (isPostRequestElementSet('html'))) ? postRequestElement('html') : 'N', getMemberId());
647 $content['type_selection'] = '';
648 foreach ($types as $key => $value) {
649 if (is_array($value)) {
650 // Output option line
651 $content['type_selection'] .= ' <option value="' . $types[$key]['id'] . '"';
652 if (($OLD_ORDER) && ($content['payment_id'] == $types[$key]['id'])) $content['type_selection'] .= ' selected="selected"';
653 $content['type_selection'] .= '>{%pipe,translateComma=' . $types[$key]['price'] . '%} {--PER_MAIL--} - ' . $types[$key]['mail_title'] . ' - ' . round($types[$key]['payment']) . ' {--PAYMENT--}</option>';
657 // No content is default
658 $content['zip_content'] = '';
660 if (isPostRequestElementSet('zip')) {
661 // Output entered ZIP code
662 $content['zip_content'] = loadTemplate('member_order_zip2', true, postRequestElement('zip'));
665 // No HTML extension installed by default
666 $content['html_extension'] = '<input type="hidden" name="html" value="N" />';
669 if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
670 // Extension is active so output valid HTML tags
671 $content['html_extension'] = loadTemplate('member_order_html_ext', true);
674 // Output form for page 2
675 loadTemplate('member_order_page2', false, $content);
677 // No HTML extension installed by default
678 $content['html_extension'] = '<input type="hidden" name="html" value="N" />';
680 // Remember maybe entered ZIP code in constant
681 if (isExtensionActive('html_mail')) {
682 // Add some content when html extension is active
683 $content['html_extension'] = loadTemplate('member_order_html_intro', true);
686 // Default is no ZIP code
687 $content['zip_content'] = '';
689 // Do we want ZIP code or not?
690 if ((isOrderMultiPageEnabled()) || (isAdmin())) {
692 if (postRequestElement('zip') > 0) {
694 'zip' => bigintval(postRequestElement('zip'))
701 $content['zip_content'] = loadTemplate('member_order_zip1', true, $data);
704 // Output form for page 1 (ZIP code or HTML)
705 loadTemplate('member_order_page1', false, $content);
708 // No mail types defined
709 displayMessage('<span class="bad">{--MEMBER_ORDER_NO_PAYMENTS--}</span>');
713 displayMessage('<span class="bad">{--MEMBER_ORDER_NO_POINTS--}</span>');
716 // No cateogries are defined yet
717 displayMessage('<span class="bad">{--MEMBER_NO_CATEGORIES--}</span>');
720 // Please confirm some mails first
721 displayMessage('<span class="notice">{%message,MEMBER_ORDER_LINKS_LEFT=' . getTotalUnconfirmedMails(getMemberId()) . '%}</span>');
725 // Redirect to requested URL