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 (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 (getConfig('allow_url_in_text') == 'Y') {
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 (getConfig('allow_url_in_subject') == 'Y') {
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') < getConfig('order_min')) && (!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 setPostRequestElement('text', checkHtmlTags(postRequestElement('text')));
155 // Maybe invalid tags found?
156 if (!isPostRequestElementSet('text')) $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('INVALID_TAGS') . '&id=' . $data['id'];
158 // Remove any HTML code
159 setPostRequestElement('text', str_replace(array('<', '>'), array('{OPEN_HTML}', '{CLOSE_HTML}'), postRequestElement('text')));
164 if ((!isPostRequestElementSet('mail_type')) || (postRequestElement('mail_type') < 1)) {
166 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('NO_MAIL_TYPE');
168 } elseif (!isAdmin()) {
169 // He has already sent a mail within a specific time
170 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('URL_TIME_LOCK') . '&id=' . $data['id'];
174 if (empty($data['url'])) {
176 $result = SQL_QUERY_ESC("SELECT
179 `{?_MYSQL_PREFIX?}_user_cats` AS `c`
181 `{?_MYSQL_PREFIX?}_user_data` AS `d`
183 `c`.`userid`=`d`.`userid`
186 `c`.`userid` != %s AND
187 `d`.`status`='CONFIRMED'
188 " . runFilterChain('user_exclusion_sql', ' ') . " AND
189 `d`.`receive_mails` > 0
191 `d`.`{?order_select?}` {?order_mode?}",
193 bigintval(postRequestElement('cat')),
195 ), __FILE__, __LINE__);
197 // Are there still receivers left?
198 if (SQL_NUMROWS($result) >= postRequestElement('receiver')) {
199 // Load receivers from database
200 $TEST = array(); $count = '0';
201 while ($holidayContent = SQL_FETCHARRAY($result)) {
202 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
203 // Check for his holiday status
204 $result_holiday = SQL_QUERY_ESC("SELECT
207 `{?_MYSQL_PREFIX?}_user_holidays`
210 `holiday_start` < UNIX_TIMESTAMP() AND
211 `holiday_end` > UNIX_TIMESTAMP()
213 array($holidayContent['userid']), __FILE__, __LINE__);
214 if (SQL_NUMROWS($result_holiday) == 1) {
215 // Exclude user who are in holiday
216 $holidayContent['userid'] = '0';
220 SQL_FREERESULT($result_holiday);
223 if (isValidId($holidayContent['userid'])) {
225 array_push($TEST, $holidayContent['userid']);
231 SQL_FREERESULT($result);
233 // Implode array into string for the sending pool
234 $receiver = implode($TEST, ';');
236 // Count array for maximum sent
237 $content['target_send'] = count($TEST);
239 // Update receiver list
240 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receive_mails`=`receive_mails`-1 WHERE `userid` IN (%s) LIMIT %s",
242 convertReceivers($receiver),
243 bigintval($content['target_send'])
244 ), __FILE__, __LINE__);
246 // Is calculated max receivers larger than wanted receivers then reset it
247 if ($content['target_send'] > postRequestElement('receiver')) {
248 $content['target_send'] = bigintval(postRequestElement('receiver'));
251 // Calculate used points
252 $usedPoints = $content['target_send'] * getPaymentPrice(bigintval(postRequestElement('mail_type')));
254 // Fix empty zip code
255 if (!isPostRequestElementSet('zip')) {
256 setPostRequestElement('zip', 0);
259 // Check if he has enougth points for this order and selected more than 0 receivers
260 if (($usedPoints > 0) && ($usedPoints <= $totalPoints) && ($content['target_send'] > 0)) {
261 // Gettings points is okay, so we can add $usedPoints later from
262 if ((!isValidId($data['id'])) || ($data['data_type'] != 'TEMP')) {
263 // New order, init data array elements
265 'sender' => getMemberId(),
266 'subject' => postRequestElement('subject'),
267 'text' => postRequestElement('text'),
268 'receivers' => $receiver
269 'payment_id' => bigintval(postRequestElement('mail_type')),
270 'data_type' => 'TEMP',
271 'timestamp' => 'UNIX_TIMESTAMP()',
272 'url' => postRequestElement('url'),
273 'cat_id' => bigintval(postRequestElement('cat')),
274 'target_send' => bigintval($content['target_send']),
275 'zip' => bigintval(postRequestElement('zip'), TRUE, FALSE),
278 // Is ext-html_mail active?
279 if (isExtensionActive('html_mail')) {
280 // HTML extension is active
281 $data['html_msg'] = postRequestElement('html');
285 $data['id'] = insertDataIntoPool($data);
289 'subject' => postRequestElement('subject'),
290 'text' => postRequestElement('text'),
291 'receivers' => $receiver
292 'payment_id' => bigintval(postRequestElement('mail_type')),
293 'timestamp' => 'UNIX_TIMESTAMP()',
294 'url' => postRequestElement('url'),
295 'cat_id' => bigintval(postRequestElement('cat')),
296 'target_send' => bigintval($content['target_send']),
297 'zip' => bigintval(postRequestElement('zip'), TRUE, FALSE),
300 // Is ext-html_mail active?
301 if (isExtensionActive('html_mail')) {
302 // HTML extension is active
303 $data['html_msg'] = postRequestElement('html');
307 updatePoolDataById($data['id'], NULL, $data);
310 // Make sure only valid id numbers can pass
311 assert(isValidId($data['id']));
313 // Id is received so we can redirect the user, used points will be added when he send's out the mail
314 $data['url'] = 'modules.php?module=frametester&order=' . bigintval($data['id']);
315 } elseif ($content['target_send'] == '0') {
316 // Not enougth receivers found which can receive mails
317 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS2');
319 // No enougth points left!
320 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_POINTS');
323 // Ordered more mails than he can send in this category
324 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('NO_RECS_LEFT');
327 } elseif (postRequestElement('receiver') == '0') {
328 // Not enougth receivers selected
329 $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS1');
330 } elseif (($ALLOWED == '0') && (getOrderMaxFull() == 'ORDER')) {
331 // No more mail orders allowed
332 displayMessage('{--MEMBER_ORDER_ALLOWED_EXHAUSTED--}');
333 } elseif (getTotalUnconfirmedMails(getMemberId()) < getConfig('max_unconfirmed')) {
334 // Show only enabled categories to the user ...
335 $whereStatement = " WHERE `visible`='Y'";
337 // ... but all to the admin
338 if (isAdmin()) $whereStatement = '';
340 // Display order form
341 $result_cats = SQL_QUERY('SELECT
345 `{?_MYSQL_PREFIX?}_cats`
346 ' . $whereStatement . '
348 `sort` ASC', __FILE__, __LINE__);
350 // Some categories found?
351 if (!SQL_HASZERONUMS($result_cats)) {
352 // Enought points left?
353 if ($totalPoints > 0) {
354 // Initialize array...
361 // Enable HTML checking
362 // @TODO Rewrite this to a filter
363 $HTML = ''; $HOL_STRING = '';
364 if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
365 $HTML = " AND `html`='Y'";
367 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
368 // Extension's version is fine
369 $HOL_STRING = " AND `holiday_active`='N'";
372 // ... and begin loading stuff
373 while ($categoriesContent = SQL_FETCHARRAY($result_cats)) {
374 $categories['id'][] = bigintval($categoriesContent['id']);
375 array_push($categories['name'], $categoriesContent['cat']);
377 // Select users in current category
378 $result_userids = SQL_QUERY_ESC('SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s AND `userid` != %s ORDER BY `userid` ASC',
379 array(bigintval($categoriesContent['id']), getMemberId()), __FILE__, __LINE__);
382 while (list($userid) = SQL_FETCHROW($result_userids)) {
383 // Check for holiday system
384 $isHolidayActive = FALSE;
385 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
386 // Check user's holiday status
387 $result_holiday = SQL_QUERY_ESC("SELECT
388 COUNT(`d`.`userid`) AS `cnt`
390 `{?_MYSQL_PREFIX?}_user_data` AS `d`
392 `{?_MYSQL_PREFIX?}_user_holidays` AS `h`
394 `d`.`userid`=`h`.`userid`
397 `d`.`receive_mails` > 0 AND
398 `d`.`status`='CONFIRMED' AND
399 `d`.`holiday_active`='Y' AND
400 `h`.`holiday_start` < UNIX_TIMESTAMP() AND
401 `h`.`holiday_end` > UNIX_TIMESTAMP()
403 array(bigintval($userid)), __FILE__, __LINE__);
406 list($count) = SQL_FETCHROW($result_holiday);
409 SQL_FREERESULT($result_holiday);
411 // Is holiday is active?
412 $isHolidayActive = ($count == 1);
415 if ($isHolidayActive === FALSE) {
416 // Check if the user want's to receive mails?
417 $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",
418 array(bigintval($userid)), __FILE__, __LINE__);
420 if ((SQL_NUMROWS($result_ver) == 1) && (isPostRequestElementSet('zip')) && (isOrderMultiPageEnabled())) {
422 list($zip) = SQL_FETCHROW($result_ver);
423 if (substr($zip, 0, strlen(postRequestElement('zip'))) == postRequestElement('zip')) {
424 // Ok, ZIP code part is found
429 $userid_cnt += SQL_NUMROWS($result_ver);
433 SQL_FREERESULT($result_ver);
438 SQL_FREERESULT($result_userids);
439 array_push($categories['userids'], $userid_cnt);
443 SQL_FREERESULT($result_cats);
445 // Now we need to load the mail types...
446 $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__);
449 if (!SQL_HASZERONUMS($result)) {
450 // Is the error code set?
451 if (isGetRequestElementSet('code')) {
452 // Display error message
453 displayMessage(getMessageFromErrorCode(getRequestElement('code')));
456 // Load all email types...
457 while ($payment = SQL_FETCHARRAY($result)) {
458 array_push($payments, $payment);
462 SQL_FREERESULT($result);
464 // Output user's points
465 $content['total_points'] = $totalPoints;
467 // Check how many mail orders he has placed today and how many he's allowed to send
468 switch (getOrderMaxFull()) {
469 case 'MAX': // He is allowed to send as much as possible
470 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_MAX--}';
473 case 'ORDER': // He is allowed to send as much as he setup the receiving value
474 $content['order_max_full'] = sprintf(getMessage('MEMBER_ORDER_ALLOWED_RECEIVE'), $ALLOWED, getUserData('receive_mails'));
477 default: // Unknown/invalid
478 logDebugMessage(__FILE__, __LINE__, sprintf("Unknown order_mas_full config detected.", getOrderMaxFull()));
479 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_UNKNOWN--}';
483 // Load final template
484 loadTemplate('member_order_points', FALSE, $content);
492 // Check if we already have an order placed and make it editable
493 $result = SQL_QUERY_ESC("SELECT
503 `{?_MYSQL_PREFIX?}_pool`
508 array(getMemberId()), __FILE__, __LINE__);
510 if (SQL_NUMROWS($result) == 1) {
512 $content = merge_array($content, SQL_FETCHARRAY($result));
514 // Fix max receivers when it is too much
515 if ((isset($categories['userids'][$content['cat_id']])) && ($content['target_send'] > $categories['userids'][$content['cat_id']])) {
517 $content['target_send'] = $categories['userids'][$content['cat_id']];
520 // Old order is grabbed
523 // Default output for that your members don't forget it...
524 $content['url'] = 'http://';
525 $content['target_send'] = '{?order_min?}';
526 $content['subject'] = '';
527 $content['text'] = '';
531 SQL_FREERESULT($result);
533 if ((isPostRequestElementSet('data')) || ((getOrderMultiPage() != 'Y') && ((!isAdmin()) && (!isExtensionActive('html_mail'))))) {
534 // Pre-output categories
535 $content['category_selection'] = generateCategoryOptionsList(((isExtensionActive('html_mail')) && (isPostRequestElementSet('html'))) ? postRequestElement('html') : 'N', getMemberId());
538 $content['payment_selection'] = '';
539 foreach ($payments as $key => $value) {
540 if (is_array($value)) {
541 // Output option line
542 $content['payment_selection'] .= ' <option value="' . $payments[$key]['id'] . '"';
543 if (($OLD_ORDER) && ($content['payment_id'] == $payments[$key]['id'])) $content['payment_selection'] .= ' selected="selected"';
544 $content['payment_selection'] .= '>{%pipe,translateComma=' . $payments[$key]['price'] . '%} {--PER_MAIL--} - ' . $payments[$key]['mail_title'] . ' - ' . round($payments[$key]['payment']) . ' {--PAYMENT--}</option>';
548 // No content is default
549 $content['zip_content'] = '';
551 if (isPostRequestElementSet('zip')) {
552 // Output entered ZIP code
553 $content['zip_content'] = loadTemplate('member_order_zip2', TRUE, postRequestElement('zip'));
556 // No HTML extension installed by default
557 $content['html_extension'] = '<input type="hidden" name="html" value="N" />';
560 if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
561 // Extension is active so output valid HTML tags
562 $content['html_extension'] = loadTemplate('member_order_html_ext', TRUE);
565 // Output form for page 2
566 loadTemplate('member_order_page2', FALSE, $content);
568 // No HTML extension installed by default
569 $content['html_extension'] = '<input type="hidden" name="html" value="N" />';
571 // Remember maybe entered ZIP code in constant
572 if (isExtensionActive('html_mail')) {
573 // Add some content when html extension is active
574 $content['html_extension'] = loadTemplate('member_order_html_intro', TRUE);
577 // Default is no ZIP code
578 $content['zip_content'] = '';
580 // Is sending to ZIP code enabled? (logged-in admin can always send to it)
581 if ((isOrderMultiPageEnabled()) || (isAdmin())) {
583 if (postRequestElement('zip') > 0) {
585 'zip' => bigintval(postRequestElement('zip'))
592 $content['zip_content'] = loadTemplate('member_order_zip1', TRUE, $data);
595 // Output form for page 1 (ZIP code or HTML)
596 loadTemplate('member_order_page1', FALSE, $content);
599 // No mail types defined
600 displayMessage('<span class="bad">{--MEMBER_ORDER_NO_PAYMENTS--}</span>');
604 displayMessage('<span class="bad">{--MEMBER_ORDER_NO_POINTS--}</span>');
607 // No cateogries are defined yet
608 displayMessage('<span class="bad">{--MEMBER_NO_CATEGORIES--}</span>');
611 // Please confirm some mails first
612 displayMessage('<span class="notice">{%message,MEMBER_ORDER_LINKS_LEFT=' . getTotalUnconfirmedMails(getMemberId()) . '%}</span>');
615 if (!empty($data['url'])) {
616 // Redirect to requested URL
617 redirectToUrl($data['url']);