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 (isUrlBlacklisted(postRequestElement('url'))) {
124 // Create redirect-URL
125 $url = 'modules.php?module=login&what=order&code=' . getCode('BLIST_URL') . '&blist=' . $GLOBALS['blacklist_data'][postRequestElement('url')]['timestamp'];
128 // Enougth receivers entered?
129 if ((postRequestElement('receiver') < getConfig('order_min')) && (!isAdmin())) {
130 // Less than allowed receivers entered!
131 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS3');
135 if (!isUrlValid(postRequestElement('url'))) {
137 $url = 'modules.php?module=login&what=order&code=' . getCode('INVALID_URL');
140 // Probe for HTML extension
141 if (isExtensionActive('html_mail')) {
142 // HTML or regular text mail?
143 if (postRequestElement('html') == 'Y') {
144 // Chek for valid HTML tags
145 setPostRequestElement('text', checkHtmlTags(postRequestElement('text')));
147 // Maybe invalid tags found?
148 if (!isPostRequestElementSet('text')) $url = 'modules.php?module=login&what=order&code=' . getCode('INVALID_TAGS')."&id=".$id;
150 // Remove any HTML code
151 setPostRequestElement('text', str_replace(array('<', '>'), array('{OPEN_HTML}', '{CLOSE_HTML}'), postRequestElement('text')));
156 if ((!isPostRequestElementSet('mail_type')) || (postRequestElement('mail_type') < 1)) {
158 $url = 'modules.php?module=login&what=order&code=' . getCode('NO_MAIL_TYPE');
160 } elseif (!isAdmin()) {
161 // He has already sent a mail within a specific time
162 $url = 'modules.php?module=login&what=order&code=' . getCode('URL_TIME_LOCK') . '&id=' . $id;
168 $result = SQL_QUERY_ESC("SELECT
171 `{?_MYSQL_PREFIX?}_user_cats` AS c
173 `{?_MYSQL_PREFIX?}_user_data` AS d
175 c.`userid`=d.`userid`
179 d.`status`='CONFIRMED'
180 " . runFilterChain('user_exclusion_sql', ' ') . " AND
181 d.`receive_mails` > 0
183 d.`{?order_select?}` {?order_mode?}",
185 bigintval(postRequestElement('cat')),
187 ), __FILE__, __LINE__);
189 // Are there still receivers left?
190 if (SQL_NUMROWS($result) >= postRequestElement('receiver')) {
191 // Load receivers from database
192 $TEST = array(); $count = '0';
193 while ($holidayContent = SQL_FETCHARRAY($result)) {
194 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
195 // Check for his holiday status
196 $result_holiday = SQL_QUERY_ESC("SELECT
199 `{?_MYSQL_PREFIX?}_user_holidays`
202 `holiday_start` < UNIX_TIMESTAMP() AND
203 `holiday_end` > UNIX_TIMESTAMP()
205 array($holidayContent['userid']), __FILE__, __LINE__);
206 if (SQL_NUMROWS($result_holiday) == 1) {
207 // Exclude user who are in holiday
208 $holidayContent['userid'] = '0';
212 SQL_FREERESULT($result_holiday);
215 if ($holidayContent['userid'] > 0) {
217 array_push($TEST, $holidayContent['userid']);
223 SQL_FREERESULT($result);
225 // Implode array into string for the sending pool
226 $receiver = implode($TEST, ';');
228 // Count array for maximum sent
229 $content['target_send'] = count($TEST);
231 // Update receiver list
232 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receive_mails`=`receive_mails`-1 WHERE `userid` IN (%s) LIMIT %s",
234 convertReceivers($receiver),
235 bigintval($content['target_send'])
236 ), __FILE__, __LINE__);
238 // Is calculated max receivers larger than wanted receivers then reset it
239 if ($content['target_send'] > postRequestElement('receiver')) {
240 $content['target_send'] = bigintval(postRequestElement('receiver'));
243 // Calculate used points
244 $usedPoints = $content['target_send'] * getPaymentData(bigintval(postRequestElement('mail_type')));
246 // Fix empty zip code
247 if (!isPostRequestElementSet('zip')) {
248 setPostRequestElement('zip', 0);
251 // Check if he has enougth points for this order and selected more than 0 receivers
252 if (($usedPoints > 0) && ($usedPoints <= $totalPoints) && ($content['target_send'] > 0)) {
253 // Gettings points is okay, so we can add $usedPoints later from
254 if (($id == '0') || ($type != 'TEMP')) {
257 if (isExtensionActive('html_mail')) {
258 // HTML extension is active
259 SQL_QUERY_ESC("INSERT INTO
260 `{?_MYSQL_PREFIX?}_pool`
290 postRequestElement('subject'),
291 postRequestElement('text'),
293 bigintval(postRequestElement('mail_type')),
294 postRequestElement('url'),
295 bigintval(postRequestElement('cat')),
296 bigintval($content['target_send']),
297 bigintval(postRequestElement('zip'), TRUE, FALSE),
298 postRequestElement('html')
299 ), __FILE__, __LINE__);
301 // No HTML extension is active
302 SQL_QUERY_ESC("INSERT INTO
303 `{?_MYSQL_PREFIX?}_pool`
331 postRequestElement('subject'),
332 postRequestElement('text'),
334 bigintval(postRequestElement('mail_type')),
335 postRequestElement('url'),
336 bigintval(postRequestElement('cat')),
337 bigintval($content['target_send']),
338 bigintval(postRequestElement('zip'), TRUE, FALSE),
339 ), __FILE__, __LINE__);
343 $id = SQL_INSERTID();
345 // Change current order
346 if (isExtensionActive('html_mail')) {
347 // HTML extension is active
348 SQL_QUERY_ESC("UPDATE
349 `{?_MYSQL_PREFIX?}_pool`
355 `timestamp`=UNIX_TIMESTAMP(),
365 postRequestElement('subject'),
366 postRequestElement('text'),
368 bigintval(postRequestElement('mail_type')),
369 postRequestElement('url'),
370 bigintval(postRequestElement('cat')),
371 $content['target_send'],
372 bigintval(postRequestElement('zip')),
373 postRequestElement('html'),
375 ), __FILE__, __LINE__);
377 // No HTML extension is active
378 SQL_QUERY_ESC("UPDATE
379 `{?_MYSQL_PREFIX?}_pool`
385 `timestamp`=UNIX_TIMESTAMP(),
394 postRequestElement('subject'),
395 postRequestElement('text'),
397 bigintval(postRequestElement('mail_type')),
398 postRequestElement('url'),
399 bigintval(postRequestElement('cat')),
400 $content['target_send'],
401 bigintval(postRequestElement('zip')),
403 ), __FILE__, __LINE__);
407 // Make sure only valid id numbers can pass
408 assert((!is_bool($id)) && ($id > 0));
410 // Id is received so we can redirect the user, used points will be added when he send's out the mail
411 $url = 'modules.php?module=frametester&order=' . $id;
412 } elseif ($content['target_send'] == '0') {
413 // Not enougth receivers found which can receive mails
414 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS2');
416 // No enougth points left!
417 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_POINTS');
420 // Ordered more mails than he can send in this category
421 $url = 'modules.php?module=login&what=order&code=' . getCode('NO_RECS_LEFT');
424 } elseif (postRequestElement('receiver') == '0') {
425 // Not enougth receivers selected
426 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS1');
427 } elseif (($ALLOWED == '0') && (getConfig('order_max_full') == 'ORDER')) {
428 // No more mail orders allowed
429 displayMessage('{--MEMBER_ORDER_ALLOWED_EXHAUSTED--}');
430 } elseif (getTotalUnconfirmedMails(getMemberId()) < getConfig('unconfirmed')) {
431 // Show only enabled categories to the user ...
432 $whereStatement = " WHERE `visible`='Y'";
434 // ... but all to the admin
435 if (isAdmin()) $whereStatement = '';
437 // Display order form
438 $result_cats = SQL_QUERY("SELECT
442 `{?_MYSQL_PREFIX?}_cats`
445 `sort` ASC", __FILE__, __LINE__);
447 // Some categories found?
448 if (!SQL_HASZERONUMS($result_cats)) {
449 // Enought points left?
450 if ($totalPoints > 0) {
451 // Initialize array...
458 // Enable HTML checking
459 // @TODO Rewrite this to a filter
460 $HTML = ''; $HOL_STRING = '';
461 if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
462 $HTML = " AND `html`='Y'";
464 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
465 // Extension's version is fine
466 $HOL_STRING = " AND `holiday_active`='N'";
469 // ... and begin loading stuff
470 while ($categoriesContent = SQL_FETCHARRAY($result_cats)) {
471 $categories['id'][] = bigintval($categoriesContent['id']);
472 array_push($categories['name'], $categoriesContent['cat']);
474 // Select users in current category
475 $result_userids = SQL_QUERY_ESC("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s AND `userid` != '%s' ORDER BY `userid` ASC",
476 array(bigintval($categoriesContent['id']), getMemberId()), __FILE__, __LINE__);
479 while (list($userid) = SQL_FETCHROW($result_userids)) {
480 // Check for holiday system
481 $isHolidayActive = FALSE;
482 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
483 // Check user's holiday status
484 $result_holiday = SQL_QUERY_ESC("SELECT
485 COUNT(d.`userid`) AS `cnt`
487 `{?_MYSQL_PREFIX?}_user_data` AS d
489 `{?_MYSQL_PREFIX?}_user_holidays` AS h
491 d.`userid`=h.`userid`
494 d.`receive_mails` > 0 AND
495 d.`status`='CONFIRMED' AND
496 d.`holiday_active`='Y' AND
497 h.`holiday_start` < UNIX_TIMESTAMP() AND
498 h.`holiday_end` > UNIX_TIMESTAMP()
500 array(bigintval($userid)), __FILE__, __LINE__);
503 list($count) = SQL_FETCHROW($result_holiday);
506 SQL_FREERESULT($result_holiday);
508 // Is holiday is active?
509 $isHolidayActive = ($count == 1);
512 if ($isHolidayActive === FALSE) {
513 // Check if the user want's to receive mails?
514 $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",
515 array(bigintval($userid)), __FILE__, __LINE__);
517 if ((SQL_NUMROWS($result_ver) == 1) && (isPostRequestElementSet('zip')) && (isOrderMultiPageEnabled())) {
519 list($zip) = SQL_FETCHROW($result_ver);
520 if (substr($zip, 0, strlen(postRequestElement('zip'))) == postRequestElement('zip')) {
521 // Ok, ZIP code part is found
526 $userid_cnt += SQL_NUMROWS($result_ver);
530 SQL_FREERESULT($result_ver);
535 SQL_FREERESULT($result_userids);
536 array_push($categories['userids'], $userid_cnt);
540 SQL_FREERESULT($result_cats);
542 // Now we need to load the mail types...
543 $result = SQL_QUERY("SELECT `id`, `price`, `payment`, `mail_title` FROM `{?_MYSQL_PREFIX?}_payments` ORDER BY `payment` ASC", __FILE__, __LINE__);
546 if (!SQL_HASZERONUMS($result)) {
547 // Is the error code set?
548 if (isGetRequestElementSet('code')) {
549 // Display error message
550 displayMessage(getMessageFromErrorCode(getRequestElement('code')));
553 // Load all email types...
554 while ($type = SQL_FETCHARRAY($result)) {
555 array_push($types, $type);
559 SQL_FREERESULT($result);
561 // Output user's points
562 $content['total_points'] = $totalPoints;
564 // Check how many mail orders he has placed today and how many he's allowed to send
565 switch (getConfig('order_max_full')) {
566 case 'MAX': // He is allowed to send as much as possible
567 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_MAX--}';
570 case 'ORDER': // He is allowed to send as much as he setup the receiving value
571 $content['order_max_full'] = sprintf(getMessage('MEMBER_ORDER_ALLOWED_RECEIVE'), $ALLOWED, getUserData('receive_mails'));
574 default: // Unknown/invalid
575 logDebugMessage(__FILE__, __LINE__, sprintf("Unknown order_mas_full config detected.", getConfig('order_max_full')));
576 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_UNKNOWN--}';
580 // Load final template
581 loadTemplate('member_order_points', FALSE, $content);
589 // Check if we already have an order placed and make it editable
590 $result = SQL_QUERY_ESC("SELECT
600 `{?_MYSQL_PREFIX?}_pool`
605 array(getMemberId()), __FILE__, __LINE__);
607 if (SQL_NUMROWS($result) == 1) {
609 $content = merge_array($content, SQL_FETCHARRAY($result));
611 // Fix max receivers when it is too much
612 if ((isset($categories['userids'][$content['cat_id']])) && ($content['target_send'] > $categories['userids'][$content['cat_id']])) {
614 $content['target_send'] = $categories['userids'][$content['cat_id']];
617 // Old order is grabbed
620 // Default output for that your members don't forget it...
621 $content['url'] = 'http://';
622 $content['target_send'] = '{?order_min?}';
623 $content['subject'] = '';
624 $content['text'] = '';
628 SQL_FREERESULT($result);
630 if ((isPostRequestElementSet('data')) || ((getOrderMultiPage() != 'Y') && ((!isAdmin()) && (!isExtensionActive('html_mail'))))) {
631 // Pre-output categories
632 $content['category_selection'] = generateCategoryOptionsList(((isExtensionActive('html_mail')) && (isPostRequestElementSet('html'))) ? postRequestElement('html') : 'N', getMemberId());
635 $content['type_selection'] = '';
636 foreach ($types as $key => $value) {
637 if (is_array($value)) {
638 // Output option line
639 $content['type_selection'] .= ' <option value="' . $types[$key]['id'] . '"';
640 if (($OLD_ORDER) && ($content['payment_id'] == $types[$key]['id'])) $content['type_selection'] .= ' selected="selected"';
641 $content['type_selection'] .= '>{%pipe,translateComma=' . $types[$key]['price'] . '%} {--PER_MAIL--} - ' . $types[$key]['mail_title'] . ' - ' . round($types[$key]['payment']) . ' {--PAYMENT--}</option>';
645 // No content is default
646 $content['zip_content'] = '';
648 if (isPostRequestElementSet('zip')) {
649 // Output entered ZIP code
650 $content['zip_content'] = loadTemplate('member_order_zip2', TRUE, postRequestElement('zip'));
653 // No HTML extension installed by default
654 $content['html_extension'] = '<input type="hidden" name="html" value="N" />';
657 if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
658 // Extension is active so output valid HTML tags
659 $content['html_extension'] = loadTemplate('member_order_html_ext', TRUE);
662 // Output form for page 2
663 loadTemplate('member_order_page2', FALSE, $content);
665 // No HTML extension installed by default
666 $content['html_extension'] = '<input type="hidden" name="html" value="N" />';
668 // Remember maybe entered ZIP code in constant
669 if (isExtensionActive('html_mail')) {
670 // Add some content when html extension is active
671 $content['html_extension'] = loadTemplate('member_order_html_intro', TRUE);
674 // Default is no ZIP code
675 $content['zip_content'] = '';
677 // Do we want ZIP code or not?
678 if ((isOrderMultiPageEnabled()) || (isAdmin())) {
680 if (postRequestElement('zip') > 0) {
682 'zip' => bigintval(postRequestElement('zip'))
689 $content['zip_content'] = loadTemplate('member_order_zip1', TRUE, $data);
692 // Output form for page 1 (ZIP code or HTML)
693 loadTemplate('member_order_page1', FALSE, $content);
696 // No mail types defined
697 displayMessage('<span class="bad">{--MEMBER_ORDER_NO_PAYMENTS--}</span>');
701 displayMessage('<span class="bad">{--MEMBER_ORDER_NO_POINTS--}</span>');
704 // No cateogries are defined yet
705 displayMessage('<span class="bad">{--MEMBER_NO_CATEGORIES--}</span>');
708 // Please confirm some mails first
709 displayMessage('<span class="notice">{%message,MEMBER_ORDER_LINKS_LEFT=' . getTotalUnconfirmedMails(getMemberId()) . '%}</span>');
713 // Redirect to requested URL