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 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2009 by Roland Haeder *
21 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
41 } elseif (!isMember()) {
42 redirectToIndexMemberOnlyModule();
45 // Add description as navigation point
46 addMenuDescription('member', __FILE__);
48 if ((!isExtensionActive('order')) && (!isAdmin())) {
49 loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('order'));
55 // Count unconfirmed mails
56 $links = countSumTotalData(getMemberId(), 'user_links', 'id', 'userid', true);
58 $ALLOWED = getUserData('receive_mails') - getUserData('mail_orders');
59 if (getConfig('order_max_full') == 'MAX') $ALLOWED = getUserData('receive_mails');
61 // Now check his points amount
62 $total = getTotalPoints(getMemberId());
64 if ((isExtensionInstalledAndNewer('holiday', '0.1.3')) && (getUserData('holiday_active') == 'Y')) {
66 loadTemplate('admin_settings_saved', false, '{--MEMBER_HOLIDAY_ORDER_NOT_POSSIBLE--}');
67 } elseif ((isPostRequestParameterSet('frametester')) && ($ALLOWED > 0) && (postRequestParameter('receiver') > 0)) {
68 // Continue with the frametester, we first need to store the data temporary in the pool
70 // First we would like to store the data and get it's pool position back...
71 $result = SQL_QUERY_ESC("SELECT `id`, `data_type`
73 `{?_MYSQL_PREFIX?}_pool`
77 `timestamp` > (UNIX_TIMESTAMP() - {?url_tlock?})
81 postRequestParameter('url')
82 ), __FILE__, __LINE__);
84 $type = 'TEMP'; $id = '0';
85 if (SQL_NUMROWS($result) == 1) {
86 // Load id and mail type
87 list($id, $type) = SQL_FETCHROW($result);
91 SQL_FREERESULT($result);
93 if ($type == 'TEMP') {
94 // No entry found, so we need to check out the stats table as well... :)
95 // We have to add that suff here, now we continue WITHOUT checking and check the text and subject against some filters
97 if (getConfig('allow_url_in_text') == 'Y') {
98 // Test submitted text against some filters (length, URLs in text etc.)
99 if ((strpos(strtolower(postRequestParameter('text')), 'https://') > -1) || (strpos(strtolower(postRequestParameter('text')), 'http://') > -1) || (strpos(strtolower(postRequestParameter('text')), "www") > -1)) {
101 $url = 'modules.php?module=login&what=order&code=' . getCode('URL_FOUND');
104 // Remove new-line and carriage-return characters
105 $TEST = str_replace("\n", '', str_replace("\r", '', postRequestParameter('text')));
107 // Text length within allowed length?
108 if (strlen($TEST) > getConfig('max_tlength')) {
110 $url = 'modules.php?module=login&what=order&code=' . getCode('OVERLENGTH');
114 // Shall I test the subject line against URLs?
115 if (getConfig('allow_url_in_subject') == 'Y') {
116 // Check the subject line for issues
117 setPostRequestParameter('subject', str_replace("\\", '[nl]', substr(postRequestParameter('subject'), 0, 200)));
118 if ((strpos(strtolower(postRequestParameter('subject')), 'http://') > -1) || (strpos(strtolower(postRequestParameter('subject')), "www") > -1)) {
119 // URL in subject found
120 $url = 'modules.php?module=login&what=order&code=' . getCode('SUBJECT_URL');
124 // And shall I check that his URL is not in the black list?
125 if (getConfig('url_blacklist') == 'Y') {
126 // Ok, I do that for you know...
127 $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`timestamp`) AS tstamp FROM `{?_MYSQL_PREFIX?}_url_blacklist` WHERE `url`='%s' LIMIT 1",
128 array(postRequestParameter('url')), __FILE__, __LINE__);
130 if (SQL_NUMROWS($result) == 1) {
131 // Jupp, we got one listed
132 list($blist) = SQL_FETCHROW($result);
134 // Create redirect-URL
135 $url = 'modules.php?module=login&what=order&code=' . getCode('BLIST_URL') . '&blist=' . $blist;
139 SQL_FREERESULT($result);
142 // Enougth receivers entered?
143 if ((postRequestParameter('receiver') < getConfig('order_min')) && (!isAdmin())) {
144 // Less than allowed receivers entered!
145 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS3');
149 if (!isUrlValid(postRequestParameter('url'))) {
151 $url = 'modules.php?module=login&what=order&code=' . getCode('INVALID_URL');
154 // Probe for HTML extension
155 if (isExtensionActive('html_mail')) {
156 // HTML or regular text mail?
157 if (postRequestParameter('html') == 'Y') {
158 // Chek for valid HTML tags
159 setPostRequestParameter('text', checkHtmlTags(postRequestParameter('text')));
161 // Maybe invalid tags found?
162 if (!isPostRequestParameterSet('text')) $url = 'modules.php?module=login&what=order&code=' . getCode('INVALID_TAGS')."&id=".$id;
164 // Remove any HTML code
165 setPostRequestParameter('text', str_replace('<', '{OPEN_HTML}', str_replace('>', '{CLOSE_HTML}', postRequestParameter('text'))));
170 if ((!isPostRequestParameterSet('mail_type')) || (postRequestParameter('mail_type') < 1)) {
172 $url = 'modules.php?module=login&what=order&code=' . getCode('NO_MAIL_TYPE');
174 } elseif (!isAdmin()) {
175 // He has already sent a mail within a specific time
176 $url = 'modules.php?module=login&what=order&code=' . getCode('URL_TLOCK') . '&id=' . $id;
181 // Check if category and number of receivers is okay
183 if ((getConfig('order_multi_page') == 'Y') && (isPostRequestParameterSet('zip')) && (postRequestParameter('zip') != '')) {
184 // Choose recipients by ZIP code
185 $add = sprintf(" AND d.zip LIKE '%s%%'",
186 bigintval(postRequestParameter('zip'))
191 $result = SQL_QUERY_ESC("SELECT
194 `{?_MYSQL_PREFIX?}_user_cats` AS c
196 `{?_MYSQL_PREFIX?}_user_data` AS d
202 d.`status`='CONFIRMED' AND
208 bigintval(postRequestParameter('cat')),
210 getConfig('order_select'),
211 getConfig('order_mode'),
212 ), __FILE__, __LINE__);
214 // Do we enougth receivers left?
215 if (SQL_NUMROWS($result) >= postRequestParameter('receiver')) {
216 // Load receivers from database
217 $TEST = array(); $cnt = '0';
218 while ($holidayContent = SQL_FETCHARRAY($result)) {
219 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
220 // Check for his holiday status
221 $result_holiday = SQL_QUERY_ESC("SELECT
224 `{?_MYSQL_PREFIX?}_user_holidays`
226 `userid`=%s AND `holiday_start` < UNIX_TIMESTAMP() AND `holiday_end` > UNIX_TIMESTAMP()
228 array($holidayContent['userid']), __FILE__, __LINE__);
229 if (SQL_NUMROWS($result_holiday) == 1) $holidayContent['userid'] = '0'; // Exclude user who are in holiday
232 SQL_FREERESULT($result_holiday);
235 if ($holidayContent['userid'] > 0) {
237 $TEST[] = $holidayContent['userid'];
243 SQL_FREERESULT($result);
245 // Implode array into string for the sending pool
246 $receiver = implode($TEST, ';');
248 // Count array for maximum sent
249 $content['target_send'] = count($TEST);
251 // Update receiver list
252 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receive_mails`=`receive_mails`-1 WHERE `userid` IN (%s) LIMIT %s",
253 array(convertReceivers($receiver), $content['target_send']), __FILE__, __LINE__);
255 // Is calculated max receivers larger than wanted receivers then reset it
256 if ($content['target_send'] > postRequestParameter('receiver')) $content['target_send'] = bigintval(postRequestParameter('receiver'));
258 // Calculate used points
259 $USED = $content['target_send'] * getPaymentPoints(bigintval(postRequestParameter('mail_type')));
261 // Fix empty zip code
262 if (!isPostRequestParameterSet('zip')) setPostRequestParameter('zip', 0);
264 // Check if he has enougth points for this order and selected more than 0 receivers
265 if (($USED > 0) && ($USED <= $total) && ($content['target_send'] > 0)) {
266 // Gettings points is okay, so we can add $USED later from
267 if (($id == '0') || ($type != 'TEMP')) {
270 if (isExtensionActive('html_mail')) {
271 // HTML extension is active
272 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_pool` (`sender`, `subject`, `text`, `receivers`, `payment_id`, `data_type`, `timestamp`, `url`, `cat_id`, `target_send`, `zip`, `html_msg`)
273 VALUES (%s,'%s','%s','%s','%s','TEMP',UNIX_TIMESTAMP(),'%s',%s,%s,%s,'%s')",
276 postRequestParameter('subject'),
277 postRequestParameter('text'),
279 bigintval(postRequestParameter('mail_type')),
280 postRequestParameter('url'),
281 bigintval(postRequestParameter('cat')),
282 $content['target_send'],
283 bigintval(postRequestParameter('zip'), true, false),
284 postRequestParameter('html')
285 ), __FILE__, __LINE__);
287 // No HTML extension is active
288 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_pool` (`sender`, `subject`, `text`, `receivers`, `payment_id`, `data_type`, `timestamp`, `url`, `cat_id`, `target_send`, `zip`)
289 VALUES (%s,'%s','%s','%s','%s','TEMP',UNIX_TIMESTAMP(),'%s',%s,%s,%s)",
292 postRequestParameter('subject'),
293 postRequestParameter('text'),
295 bigintval(postRequestParameter('mail_type')),
296 postRequestParameter('url'),
297 bigintval(postRequestParameter('cat')),
298 $content['target_send'],
299 bigintval(postRequestParameter('zip'), true, false),
300 ), __FILE__, __LINE__);
303 // Change current order
304 if (isExtensionActive('html_mail')) {
305 // HTML extension is active
306 SQL_QUERY_ESC("UPDATE
307 `{?_MYSQL_PREFIX?}_pool`
313 `timestamp`=UNIX_TIMESTAMP(),
323 postRequestParameter('subject'),
324 postRequestParameter('text'),
326 bigintval(postRequestParameter('mail_type')),
327 postRequestParameter('url'),
328 bigintval(postRequestParameter('cat')),
329 $content['target_send'],
330 bigintval(postRequestParameter('zip')),
331 postRequestParameter('html'),
333 ), __FILE__, __LINE__);
335 // No HTML extension is active
336 SQL_QUERY_ESC("UPDATE
337 `{?_MYSQL_PREFIX?}_pool`
343 `timestamp`=UNIX_TIMESTAMP(),
352 postRequestParameter('subject'),
353 postRequestParameter('text'),
355 bigintval(postRequestParameter('mail_type')),
356 postRequestParameter('url'),
357 bigintval(postRequestParameter('cat')),
358 $content['target_send'],
359 bigintval(postRequestParameter('zip')),
361 ), __FILE__, __LINE__);
365 // Do we need to get the id number?
367 // Order is placed as temporary. We need to get it's id for the frametester
368 $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",
371 postRequestParameter('subject'),
372 bigintval(postRequestParameter('mail_type'))
373 ), __FILE__, __LINE__);
376 list($id) = SQL_FETCHROW($result);
379 SQL_FREERESULT($result);
382 // id is received so we can redirect the user, used points will be added when he send's out the mail
383 $url = 'modules.php?module=frametester&order=' . $id;
384 } elseif ($content['target_send'] == '0') {
385 // Not enougth receivers found which can receive mails
386 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS2');
388 // No enougth points left!
389 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_POINTS');
392 // Ordered more mails than he can send in this category
393 $url = 'modules.php?module=login&what=order&code=' . getCode('NO_RECS_LEFT');
396 } elseif (postRequestParameter('receiver') == '0') {
397 // Not enougth receivers selected
398 $url = 'modules.php?module=login&what=order&code=' . getCode('MORE_RECEIVERS1');
399 } elseif (($ALLOWED == '0') && (getConfig('order_max_full') == 'ORDER')) {
400 // No more mail orders allowed
401 loadTemplate('admin_settings_saved', false, '{--MEMBER_ORDER_ALLOWED_EXHAUSTED--}');
402 } elseif ($links < getConfig('unconfirmed')) {
403 // Show only enabled categories to the user ...
404 $whereStatement = " WHERE `visible`='Y'";
406 // ... but all to the admin
407 if (isAdmin()) $whereStatement = '';
409 // Display order form
410 $result_cats = SQL_QUERY("SELECT
413 `{?_MYSQL_PREFIX?}_cats`
416 `sort` ASC", __FILE__, __LINE__);
417 if (!SQL_HASZERONUMS($result_cats)) {
419 // Initialize array...
426 // Enable HTML checking
427 // @TODO Rewrite this to a filter
428 $HTML = ''; $HOL_STRING = '';
429 if ((isExtensionActive('html_mail')) && (postRequestParameter('html') == 'Y')) $HTML = " AND `html`='Y'";
430 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
431 // Extension's version is fine
432 $HOL_STRING = " AND `holiday_active`='N'";
435 // ... and begin loading stuff
436 while ($categoriesContent = SQL_FETCHARRAY($result_cats)) {
437 $categories['id'][] = bigintval($categoriesContent['id']);
438 $categories['name'][] = $categoriesContent['cat'];
440 // Select users in current category
441 $result_userids = SQL_QUERY_ESC("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s AND `userid` != '%s' ORDER BY `userid` ASC",
442 array(bigintval($categoriesContent['id']), getMemberId()), __FILE__, __LINE__);
445 while (list($ucat) = SQL_FETCHROW($result_userids)) {
446 // Check for holiday system
447 $isHolidayActive = false;
448 if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
449 // Check user's holiday status
450 $result_holiday = SQL_QUERY_ESC("SELECT
451 COUNT(d.userid) AS `cnt`
453 `{?_MYSQL_PREFIX?}_user_data` AS d
455 `{?_MYSQL_PREFIX?}_user_holidays` AS h
460 d.receive_mails > 0 AND
461 d.`status`='CONFIRMED' AND
462 d.`holiday_active`='Y' AND
463 h.holiday_start < UNIX_TIMESTAMP() AND
464 h.holiday_end > UNIX_TIMESTAMP()
466 array(bigintval($ucat)), __FILE__, __LINE__);
469 list($cnt) = SQL_FETCHROW($result);
472 SQL_FREERESULT($result_holiday);
474 // Is holiday is active?
475 $isHolidayActive = ($cont == 1);
478 if ($isHolidayActive === false) {
479 // Check if the user want's to receive mails?
480 $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",
481 array(bigintval($ucat)), __FILE__, __LINE__);
483 if ((SQL_NUMROWS($result_ver) == 1) && (isPostRequestParameterSet('zip')) && (getConfig('order_multi_page') == 'Y')) {
485 list($zip) = SQL_FETCHROW($result_ver);
486 if (substr($zip, 0, strlen(postRequestParameter('zip'))) == postRequestParameter('zip')) {
487 // Ok, ZIP part is found
492 $userid_cnt += SQL_NUMROWS($result_ver);
496 SQL_FREERESULT($result_ver);
501 SQL_FREERESULT($result_userids);
502 $categories['userids'][] = $userid_cnt;
506 SQL_FREERESULT($result_cats);
508 // Now we need to load the mail types...
509 $result = SQL_QUERY("SELECT `id`, `price`, `payment`, `mail_title` FROM `{?_MYSQL_PREFIX?}_payments` ORDER BY `payment` ASC", __FILE__, __LINE__);
512 if (!SQL_HASZERONUMS($result)) {
513 // Check for message id in URL
514 $message = getMessageFromErrorCode(getRequestParameter('code'));
516 if (!empty($message)) {
517 // We got system message so we drop it out to the user
518 loadTemplate('admin_settings_saved', false, $message);
521 // Load all email types...
522 while ($types[] = SQL_FETCHROW($result)) {
523 // Nothing to do here... ;-)
527 SQL_FREERESULT($result);
529 // Output user's points
530 $content['total'] = translateComma($total);
532 // Check how many mail orders he has placed today and how many he's allowed to send
533 switch (getConfig('order_max_full')) {
534 case 'MAX': // He is allowed to send as much as possible
535 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_MAX--}';
538 case 'ORDER': // He is allowed to send as much as he setup the receiving value
539 $content['order_max_full'] = sprintf(getMessage('MEMBER_ORDER_ALLOWED_RECEIVE'), $ALLOWED, getUserData('receive_mails'));
542 default: // Unknown/invalid
543 logDebugMessage(__FILE__, __LINE__, sprintf("Unknown order_mas_full config detected.", getConfig('order_max_full')));
544 $content['order_max_full'] = '{--MEMBER_ORDER_ALLOWED_UNKNOWN--}';
548 // Load final template
549 loadTemplate('member_order_points', false, $content);
557 // Check if we already have an order placed and make it editable
558 $result = SQL_QUERY_ESC("SELECT
559 `subject`, `text`, `payment_id`, `timestamp`, `url`, `target_send`, `cat_id`, `zip`
561 `{?_MYSQL_PREFIX?}_pool`
566 array(getMemberId()), __FILE__, __LINE__);
568 if (SQL_NUMROWS($result) == 1) {
570 $content = merge_array($content, SQL_FETCHARRAY($result));
572 // Fix max receivers when it is too much
573 if ((isset($categories['userids'][$content['cat_id']])) && ($content['target_send'] > $categories['userids'][$content['cat_id']])) {
575 $content['target_send'] = $categories['userids'][$content['cat_id']];
578 // Old order is grabbed
581 // Default output for that your members don't forget it...
582 $content['url'] = 'http://';
583 $content['target_send'] = getConfig('order_min');
584 $content['subject'] = '{--ORDER_DEFAULT_SUBJECT--}';
585 $content['text'] = '{--ORDER_DEFAULT_TEXT--}';
589 SQL_FREERESULT($result);
591 if ((isPostRequestParameterSet('data')) || ((getConfig('order_multi_page') != 'Y') && ((!isAdmin()) && (!isExtensionActive('html_mail'))))) {
592 // Pre-output categories
593 $content['category_selection'] = '';
594 foreach ($categories['id'] as $key => $value) {
595 $content['category_selection'] .= ' <option value="' . $value . '"';
596 if (($OLD_ORDER) && ($content['cat_id'] == $value)) $content['category_selection'] .= ' selected="selected"';
597 $content['category_selection'] .= '>' . $categories['name'][$key]." (".$categories['userids'][$key] . ' {--USER_IN_CAT--})</option>';
601 $content['type_selection'] = '';
602 foreach ($types as $key => $value) {
603 $P = translateComma($types[$key][1]);
604 if (is_array($value)) {
605 // Output option line
606 $content['type_selection'] .= ' <option value="' . $types[$key][0] . '"';
607 if (($OLD_ORDER) && ($content['payment_id'] == $types[$key][0])) $content['type_selection'] .= ' selected="selected"';
608 $content['type_selection'] .= '>' . $P . ' {--PER_MAIL--} - ' . $types[$key][3] . ' - ' . round($types[$key][2]) . ' {--PAYMENT--}</option>';
612 // No content is default
613 $content['zip_content'] = '';
615 if (isPostRequestParameterSet('zip')) {
616 // Output entered ZIP code
617 $content['zip_content'] = loadTemplate('member_order-zip2', true, postRequestParameter('zip'));
621 if ((isExtensionActive('html_mail')) && (postRequestParameter('html') == 'Y')) {
622 // Extension is active so output valid HTML tags
623 $content['html_extension'] = loadTemplate('member_order-html_ext', true, addValidHtmlTags());
625 // Extension not active and/or class not uploaded
626 $content['html_extension'] = '<tr><td colspan="3"><input type="hidden" name="html" value="N" /></td></tr>';
629 // Output form for page 2
630 loadTemplate('member_order_page2', false, $content);
632 // Remember maybe entered ZIP code in constant
633 if (isExtensionActive('html_mail')) {
634 // Add some content when html extension is active
635 $content['html_extension'] = loadTemplate('member_order-html_intro', true);
637 // No HTML extension installed
638 $content['html_extension'] = '<tr><td colspan="3"><input type="hidden" name="html" value="N" /></td></tr>';
641 // Default is no ZIP code
642 $content['zip_content'] = '';
644 // Do we want ZIP code or not?
645 if ((getConfig('order_multi_page') == 'Y') || (isAdmin())) {
647 if (postRequestParameter('zip') > 0) {
649 'zip' => bigintval(postRequestParameter('zip'))
656 $content['zip_content'] = loadTemplate('member_order-zip1', true, $data);
659 // Output form for page 1 (ZIP code or HTML)
660 loadTemplate('member_order_page1', false, $content);
663 // No mail types defined
664 loadTemplate('admin_settings_saved', false, '<span class="member_failed">{--MEMBER_NO_PAYMENTS--}</span>');
668 loadTemplate('admin_settings_saved', false, '<span class="member_failed">{--MEMBER_NO_POINTS--}</span>');
671 // No cateogries are defined yet
672 loadTemplate('admin_settings_saved', false, '<span class="member_failed">{--MEMBER_NO_CATEGORIES--}</span>');
675 // Please confirm some mails first
676 loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_LINKS_LEFT'), $links);
680 // Redirect to requested URL