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 - 2013 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 (getOrderMaxFull() == '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 // @TODO Try to move out url_tlock to a filter for extra SQL statements
73 $result = SQL_QUERY_ESC("SELECT
77 `{?_MYSQL_PREFIX?}_pool`
81 (UNIX_TIMESTAMP() - `timestamp`) >= {?url_tlock?}
85 postRequestElement('url')
86 ), __FILE__, __LINE__);
88 if (SQL_NUMROWS($result) == 1) {
89 // Load id and mail type
90 $data = SQL_FETCHARRAY($result);
94 SQL_FREERESULT($result);
96 if ($data['data_type'] == 'TEMP') {
98 * No entry found, so we need to check out the stats table as well...
99 * :) We have to add that suff here, now we continue WITHOUT checking
100 * and check the text and subject against some filters
103 if (!isAllowUrlInTextEnabled()) {
104 // Test submitted text against some filters (length, URLs in text etc.)
105 if ((isInStringIgnoreCase('https://', postRequestElement('text'))) || (isInStringIgnoreCase('http://', postRequestElement('text'))) || (isInStringIgnoreCase('www', postRequestElement('text')))) {
107 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('URL_FOUND');
110 // Remove new-line and carriage-return characters
111 $TEST = str_replace(array(PHP_EOL, chr(13)), array('', ''), postRequestElement('text'));
113 // Text length within allowed length?
114 if (strlen($TEST) > getMaxTextLength()) {
116 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('OVERLENGTH');
120 // Shall I test the subject line against URLs?
121 if (!isAllowUrlInSubjectEnabled()) {
122 // Check the subject line for issues
123 setPostRequestElement('subject', str_replace(chr(92), '[nl]', substr(postRequestElement('subject'), 0, 200)));
124 if ((isInStringIgnoreCase('https://', postRequestElement('subject'))) || (isInStringIgnoreCase('http://', postRequestElement('subject'))) || (isInStringIgnoreCase('www', postRequestElement('subject')))) {
125 // URL in subject found
126 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('SUBJECT_URL');
130 // And shall I check that his URL is not in the black list?
131 if ((isExtensionActive('blacklist')) && (isUrlBlacklisted(postRequestElement('url')))) {
132 // Create redirect-URL
133 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('BLIST_URL') . '&blist=' . $GLOBALS['blacklist_data'][postRequestElement('url')]['timestamp'];
136 // Enougth receivers entered?
137 if ((postRequestElement('receiver') < getOrderMin()) && (!isAdmin())) {
138 // Less than allowed receivers entered!
139 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS3');
143 if (!isUrlValid(postRequestElement('url'))) {
145 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('INVALID_URL');
148 // Probe for HTML extension
149 if (isExtensionActive('html_mail')) {
150 // HTML or regular text mail?
151 if (postRequestElement('html') == 'Y') {
152 // Chek for valid HTML tags
153 $checked = checkHtmlTags(postRequestElement('text')));
155 // Maybe invalid tags found?
156 if (empty($checked)) {
157 // Invalid HTML tags found
158 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('INVALID_TAGS') . '&id=' . $data['id'];
161 // Remove any HTML code
162 setPostRequestElement('text', str_replace(array('<', '>'), array('{OPEN_HTML}', '{CLOSE_HTML}'), postRequestElement('text')));
167 if ((!isPostRequestElementSet('mail_type')) || (postRequestElement('mail_type') < 1)) {
169 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('NO_MAIL_TYPE');
171 } elseif (!isAdmin()) {
172 // He has already sent a mail within a specific time
173 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('URL_TIME_LOCK') . '&id=' . $data['id'];
177 if (empty($data['url'])) {
179 $result = SQL_QUERY_ESC("SELECT
182 `{?_MYSQL_PREFIX?}_user_cats` AS `c`
184 `{?_MYSQL_PREFIX?}_user_data` AS `d`
186 `c`.`userid`=`d`.`userid`
189 `c`.`userid` != %s AND
190 `d`.`status`='CONFIRMED'
191 " . runFilterChain('user_exclusion_sql', ' ') . " AND
192 `d`.`receive_mails` > 0
194 `d`.`{?order_select?}` {?order_mode?}",
196 bigintval(postRequestElement('cat')),
198 ), __FILE__, __LINE__);
200 // Are there still receivers left?
201 if (SQL_NUMROWS($result) >= postRequestElement('receiver')) {
202 // Load receivers from database
203 $TEST = array(); $count = '0';
204 while ($holidayContent = SQL_FETCHARRAY($result)) {
205 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
206 // Check for his holiday status
207 $result_holiday = SQL_QUERY_ESC("SELECT
210 `{?_MYSQL_PREFIX?}_user_holidays`
213 `holiday_start` < UNIX_TIMESTAMP() AND
214 `holiday_end` > UNIX_TIMESTAMP()
216 array($holidayContent['userid']), __FILE__, __LINE__);
217 if (SQL_NUMROWS($result_holiday) == 1) {
218 // Exclude user who are in holiday
219 $holidayContent['userid'] = '0';
223 SQL_FREERESULT($result_holiday);
226 if (isValidId($holidayContent['userid'])) {
228 array_push($TEST, $holidayContent['userid']);
234 SQL_FREERESULT($result);
236 // Implode array into string for the sending pool
237 $receiver = implode($TEST, ';');
239 // Count array for maximum sent
240 $content['target_send'] = count($TEST);
242 // Update receiver list
243 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receive_mails`=`receive_mails`-1 WHERE `userid` IN (%s) LIMIT %s",
245 convertReceivers($receiver),
246 bigintval($content['target_send'])
247 ), __FILE__, __LINE__);
249 // Is calculated max receivers larger than wanted receivers then reset it
250 if ($content['target_send'] > postRequestElement('receiver')) {
251 $content['target_send'] = bigintval(postRequestElement('receiver'));
254 // Calculate used points
255 $usedPoints = $content['target_send'] * getPaymentPrice(bigintval(postRequestElement('mail_type')));
257 // Fix empty zip code
258 if (!isPostRequestElementSet('zip')) {
259 setPostRequestElement('zip', 0);
262 // Check if he has enougth points for this order and selected more than 0 receivers
263 if (($usedPoints > 0) && ($usedPoints <= $totalPoints) && ($content['target_send'] > 0)) {
264 // Gettings points is okay, so we can add $usedPoints later from
265 if ((!isValidId($data['id'])) || ($data['data_type'] != 'TEMP')) {
266 // New order, init data array elements
268 'sender' => getMemberId(),
269 'subject' => postRequestElement('subject'),
270 'text' => postRequestElement('text'),
271 'receivers' => $receiver
272 'payment_id' => bigintval(postRequestElement('mail_type')),
273 'data_type' => 'TEMP',
274 'timestamp' => 'UNIX_TIMESTAMP()',
275 'url' => postRequestElement('url'),
276 'cat_id' => bigintval(postRequestElement('cat')),
277 'target_send' => bigintval($content['target_send']),
278 'zip' => bigintval(postRequestElement('zip'), TRUE, FALSE),
281 // Is ext-html_mail active?
282 if (isExtensionActive('html_mail')) {
283 // HTML extension is active
284 $data['html_msg'] = postRequestElement('html');
288 $data['id'] = insertDataIntoPool($data);
292 'subject' => postRequestElement('subject'),
293 'text' => postRequestElement('text'),
294 'receivers' => $receiver
295 'payment_id' => bigintval(postRequestElement('mail_type')),
296 'timestamp' => 'UNIX_TIMESTAMP()',
297 'url' => postRequestElement('url'),
298 'cat_id' => bigintval(postRequestElement('cat')),
299 'target_send' => bigintval($content['target_send']),
300 'zip' => bigintval(postRequestElement('zip'), TRUE, FALSE),
303 // Is ext-html_mail active?
304 if (isExtensionActive('html_mail')) {
305 // HTML extension is active
306 $data['html_msg'] = postRequestElement('html');
310 updatePoolDataById($data['id'], NULL, $data);
313 // Make sure only valid id numbers can pass
314 assert(isValidId($data['id']));
316 // Id is received so we can redirect the user, used points will be added when he send's out the mail
317 $data['url'] = 'modules.php?module=frametester&order=' . bigintval($data['id']);
318 } elseif ($content['target_send'] == '0') {
319 // Not enougth receivers found which can receive mails
320 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS2');
322 // No enougth points left!
323 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_POINTS');
326 // Ordered more mails than he can send in this category
327 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('NO_RECS_LEFT');
330 } elseif (postRequestElement('receiver') == '0') {
331 // Not enougth receivers selected
332 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS1');
333 } elseif (($ALLOWED == '0') && (getOrderMaxFull() == 'ORDER')) {
334 // No more mail orders allowed
335 displayMessage('{--MEMBER_ORDER_ALLOWED_EXHAUSTED--}');
336 } elseif (getTotalUnconfirmedMails(getMemberId()) < getConfig('max_unconfirmed')) {
337 // Show only enabled categories to the user ...
338 $whereStatement = " WHERE `visible`='Y'";
340 // ... but all to the admin
341 if (isAdmin()) $whereStatement = '';
343 // Display order form
344 $result_cats = SQL_QUERY('SELECT
348 `{?_MYSQL_PREFIX?}_cats`
349 ' . $whereStatement . '
351 `sort` ASC', __FILE__, __LINE__);
353 // Some categories found?
354 if (!SQL_HASZERONUMS($result_cats)) {
355 // Enought points left?
356 if ($totalPoints > 0) {
357 // Initialize array...
364 // Enable HTML checking
365 // @TODO Rewrite this to a filter
366 $HTML = ''; $HOL_STRING = '';
367 if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
368 $HTML = " AND `html`='Y'";
370 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
371 // Extension's version is fine
372 $HOL_STRING = " AND `holiday_active`='N'";
375 // ... and begin loading stuff
376 while ($categoriesContent = SQL_FETCHARRAY($result_cats)) {
377 $categories['id'][] = bigintval($categoriesContent['id']);
378 array_push($categories['name'], $categoriesContent['cat']);
380 // Select users in current category
381 $result_userids = SQL_QUERY_ESC('SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s AND `userid` != %s ORDER BY `userid` ASC',
382 array(bigintval($categoriesContent['id']), getMemberId()), __FILE__, __LINE__);
385 while (list($userid) = SQL_FETCHROW($result_userids)) {
386 // Check for holiday system
387 $isHolidayActive = FALSE;
388 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
389 // Check user's holiday status
390 $result_holiday = SQL_QUERY_ESC("SELECT
391 COUNT(`d`.`userid`) AS `cnt`
393 `{?_MYSQL_PREFIX?}_user_data` AS `d`
395 `{?_MYSQL_PREFIX?}_user_holidays` AS `h`
397 `d`.`userid`=`h`.`userid`
400 `d`.`receive_mails` > 0 AND
401 `d`.`status`='CONFIRMED' AND
402 `d`.`holiday_active`='Y' AND
403 `h`.`holiday_start` < UNIX_TIMESTAMP() AND
404 `h`.`holiday_end` > UNIX_TIMESTAMP()
406 array(bigintval($userid)), __FILE__, __LINE__);
409 list($count) = SQL_FETCHROW($result_holiday);
412 SQL_FREERESULT($result_holiday);
414 // Is holiday is active?
415 $isHolidayActive = ($count == 1);
418 if ($isHolidayActive === FALSE) {
419 // Check if the user want's to receive mails?
420 $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",
421 array(bigintval($userid)), __FILE__, __LINE__);
423 if ((SQL_NUMROWS($result_ver) == 1) && (isPostRequestElementSet('zip')) && (isOrderMultiPageEnabled())) {
425 list($zip) = SQL_FETCHROW($result_ver);
426 if (substr($zip, 0, strlen(postRequestElement('zip'))) == postRequestElement('zip')) {
427 // Ok, ZIP code part is found
432 $userid_cnt += SQL_NUMROWS($result_ver);
436 SQL_FREERESULT($result_ver);
441 SQL_FREERESULT($result_userids);
442 array_push($categories['userids'], $userid_cnt);
446 SQL_FREERESULT($result_cats);
448 // Now we need to load the mail types...
449 $result = SQL_QUERY("SELECT `id`, `price`, `payment`, `mail_title` FROM `{?_MYSQL_PREFIX?}_payments` WHERE `price` > 0 AND `payment` > 0 ORDER BY `payment` ASC", __FILE__, __LINE__);
452 if (!SQL_HASZERONUMS($result)) {
453 // Is the error code set?
454 if (isGetRequestElementSet('code')) {
455 // Display error message
456 displayMessage(getMessageFromErrorCode(getRequestElement('code')));
459 // Load all email types...
460 while ($payment = SQL_FETCHARRAY($result)) {
461 array_push($payments, $payment);
465 SQL_FREERESULT($result);
467 // Output user's points
468 $content['total_points'] = $totalPoints;
470 // Check how many mail orders he has placed today and how many he's allowed to send
471 switch (getOrderMaxFull()) {
472 case 'MAX': // He is allowed to send as much as possible
473 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_MAX--}';
476 case 'ORDER': // He is allowed to send as much as he setup the receiving value
477 $content['order_max_full'] = sprintf(getMessage('MEMBER_ORDER_ALLOWED_RECEIVE'), $ALLOWED, getUserData('receive_mails'));
480 default: // Unknown/invalid
481 logDebugMessage(__FILE__, __LINE__, sprintf("Unknown order_mas_full config detected.", getOrderMaxFull()));
482 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_UNKNOWN--}';
486 // Load final template
487 loadTemplate('member_order_points', FALSE, $content);
495 // Check if we already have an order placed and make it editable
496 $result = SQL_QUERY_ESC("SELECT
506 `{?_MYSQL_PREFIX?}_pool`
511 array(getMemberId()), __FILE__, __LINE__);
513 if (SQL_NUMROWS($result) == 1) {
515 $content = merge_array($content, SQL_FETCHARRAY($result));
517 // Fix max receivers when it is too much
518 if ((isset($categories['userids'][$content['cat_id']])) && ($content['target_send'] > $categories['userids'][$content['cat_id']])) {
520 $content['target_send'] = $categories['userids'][$content['cat_id']];
523 // Old order is grabbed
526 // Default output for that your members don't forget it...
527 $content['url'] = 'http://';
528 $content['target_send'] = '{?order_min?}';
529 $content['subject'] = '';
530 $content['text'] = '';
534 SQL_FREERESULT($result);
536 if ((isPostRequestElementSet('data')) || ((getOrderMultiPage() != 'Y') && ((!isAdmin()) && (!isExtensionActive('html_mail'))))) {
537 // Pre-output categories
538 $content['category_selection'] = generateCategoryOptionsList(((isExtensionActive('html_mail')) && (isPostRequestElementSet('html'))) ? postRequestElement('html') : 'N', getMemberId());
541 $content['payment_selection'] = '';
542 foreach ($payments as $key => $value) {
543 if (is_array($value)) {
544 // Output option line
545 $content['payment_selection'] .= ' <option value="' . $payments[$key]['id'] . '"';
546 if (($OLD_ORDER) && ($content['payment_id'] == $payments[$key]['id'])) $content['payment_selection'] .= ' selected="selected"';
547 $content['payment_selection'] .= '>{%pipe,translateComma=' . $payments[$key]['price'] . '%} {--PER_MAIL--} - ' . $payments[$key]['mail_title'] . ' - ' . round($payments[$key]['payment']) . ' {--PAYMENT--}</option>';
551 // No content is default
552 $content['zip_content'] = '';
554 if (isPostRequestElementSet('zip')) {
555 // Output entered ZIP code
556 $content['zip_content'] = loadTemplate('member_order_zip2', TRUE, postRequestElement('zip'));
559 // No HTML extension installed by default
560 $content['html_extension'] = '<input type="hidden" name="html" value="N" />';
563 if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
564 // Extension is active so output valid HTML tags
565 $content['html_extension'] = loadTemplate('member_order_html_ext', TRUE);
568 // Output form for page 2
569 loadTemplate('member_order_page2', FALSE, $content);
571 // No HTML extension installed by default
572 $content['html_extension'] = '<input type="hidden" name="html" value="N" />';
574 // Remember maybe entered ZIP code in constant
575 if (isExtensionActive('html_mail')) {
576 // Add some content when html extension is active
577 $content['html_extension'] = loadTemplate('member_order_html_intro', TRUE);
580 // Default is no ZIP code
581 $content['zip_content'] = '';
583 // Is sending to ZIP code enabled? (logged-in admin can always send to it)
584 if ((isOrderMultiPageEnabled()) || (isAdmin())) {
586 if (postRequestElement('zip') > 0) {
588 'zip' => bigintval(postRequestElement('zip'))
595 $content['zip_content'] = loadTemplate('member_order_zip1', TRUE, $data);
598 // Output form for page 1 (ZIP code or HTML)
599 loadTemplate('member_order_page1', FALSE, $content);
602 // No mail types defined
603 displayMessage('<span class="bad">{--MEMBER_ORDER_NO_PAYMENTS--}</span>');
607 displayMessage('<span class="bad">{--MEMBER_ORDER_NO_POINTS--}</span>');
610 // No cateogries are defined yet
611 displayMessage('<span class="bad">{--MEMBER_NO_CATEGORIES--}</span>');
614 // Please confirm some mails first
615 displayMessage('<span class="notice">{%message,MEMBER_ORDER_LINKS_LEFT=' . getTotalUnconfirmedMails(getMemberId()) . '%}</span>');
618 if (!empty($data['url'])) {
619 // Redirect to requested URL
620 redirectToUrl($data['url']);