Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / modules / member / what-order.php
index 4ee6eab5c19548cab986d991bae13c65cdfe0f25..f2822d0401d494caa5a735c4da1063fb6a3379ba 100644 (file)
@@ -16,7 +16,7 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -70,7 +70,7 @@ if ((isExtensionInstalledAndNewer('holiday', '0.1.3')) && (isUserDataEnabled('ho
        //
        // First we would like to store the data and get it's pool position back...
        // @TODO Try to move out url_tlock to a filter for extra SQL statements
-       $result = SQL_QUERY_ESC("SELECT
+       $result = sqlQueryEscaped("SELECT
        `id`,
        `data_type`
 FROM
@@ -85,88 +85,40 @@ LIMIT 1",
                        postRequestElement('url')
                ), __FILE__, __LINE__);
 
-       if (SQL_NUMROWS($result) == 1) {
+       if (sqlNumRows($result) == 1) {
                // Load id and mail type
-               $data = SQL_FETCHARRAY($result);
+               $data = sqlFetchArray($result);
        } // END - if
 
        // Free result
-       SQL_FREERESULT($result);
+       sqlFreeResult($result);
 
        if ($data['data_type'] == 'TEMP') {
-               /*
-                * No entry found, so we need to check out the stats table as well...
-                * :) We have to add that suff here, now we continue WITHOUT checking
-                * and check the text and subject against some filters
-                */
-               $data['url'] = '';
-               if (!isAllowUrlInTextEnabled()) {
-                       // Test submitted text against some filters (length, URLs in text etc.)
-                       if ((isInStringIgnoreCase('https://', postRequestElement('text'))) || (isInStringIgnoreCase('http://', postRequestElement('text'))) || (isInStringIgnoreCase('www', postRequestElement('text')))) {
-                               // URL found
-                               $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('URL_FOUND');
-                       } // END - if
-
-                       // Remove new-line and carriage-return characters
-                       $TEST = str_replace(array(PHP_EOL, chr(13)), array('', ''), postRequestElement('text'));
-
-                       // Text length within allowed length?
-                       if (strlen($TEST) > getMaxTextLength()) {
-                               // Text is too long!
-                               $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('OVERLENGTH');
-                       } // END - if
-               } // END - if
-
-               // Shall I test the subject line against URLs?
-               if (!isAllowUrlInSubjectEnabled()) {
-                       // Check the subject line for issues
-                       setPostRequestElement('subject', str_replace(chr(92), '[nl]', substr(postRequestElement('subject'), 0, 200)));
-                       if ((isInStringIgnoreCase('https://', postRequestElement('subject'))) || (isInStringIgnoreCase('http://', postRequestElement('subject'))) || (isInStringIgnoreCase('www', postRequestElement('subject')))) {
-                               // URL in subject found
-                               $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('SUBJECT_URL');
-                       } // END - if
-               } // END - if
-
-               // And shall I check that his URL is not in the black list?
-               if ((isExtensionActive('blacklist')) && (isUrlBlacklisted(postRequestElement('url')))) {
-                       // Create redirect-URL
-                       $data['url'] = 'modules.php?module=login&what=order&code=' . getCode('BLIST_URL') . '&blist=' . $GLOBALS['blacklist_data'][postRequestElement('url')]['timestamp'];
-               } // END - if
-
-               // Enougth receivers entered?
-               if ((postRequestElement('receiver') < getOrderMin()) && (!isAdmin())) {
-                       // Less than allowed receivers entered!
-                       $data['url'] = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('MORE_RECEIVERS3');
-               } // END - if
-
-               // Validate URL
-               if (!isUrlValid(postRequestElement('url'))) {
-                       // URL is invalid!
-                       $data['url'] = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('INVALID_URL');
-               } // END - if
-
-               // Probe for HTML extension
-               if (isExtensionActive('html_mail')) {
-                       // HTML or regular text mail?
-                       if (postRequestElement('html') == 'Y') {
-                               // Chek for valid HTML tags
-                               $checked = checkHtmlTags(postRequestElement('text')));
-
-                               // Maybe invalid tags found?
-                               if (empty($checked)) {
-                                       // Invalid HTML tags found
-                                       $data['url'] = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('INVALID_TAGS') . '&amp;id=' . $data['id'];
-                               } // END - if
-                       } else {
-                               // Remove any HTML code
-                               setPostRequestElement('text', str_replace(array('<', '>'), array('{OPEN_HTML}', '{CLOSE_HTML}'), postRequestElement('text')));
-                       }
-               } // END - if
-
-               // Is mail type set?
-               if ((!isPostRequestElementSet('mail_type')) || (postRequestElement('mail_type') < 1)) {
-                       // Not correctly set
-                       $data['url'] = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('NO_MAIL_TYPE');
+               // Prepare data for filter chain
+               $filterData = array(
+                       'url'       => postRequestElement('url'),
+                       'text'      => postRequestElement('text'),
+                       'subject'   => postRequestElement('subject'),
+                       'receiver'  => bigintval(postRequestElement('receiver')),
+                       'mail_type' => bigintval(postRequestElement('mail_type')),
+                       'html'      => postRequestElement('html'),
+                       'id'        => bigintval($data['id']),
+                       'redirect'  => '',
+               );
+
+               // Run the filter chain
+               $filterData = runFilterChain('pre_mail_order', $filterData);
+
+               // Copy field redirect->url
+               $data['url'] = $filterData['redirect'];
+
+               // Is there an error?
+               if (empty($data['url'])) {
+                       // Copy all filter data -> POST
+                       foreach ($filterData as $key => $value) {
+                               // Set it
+                               setPostRequestElement($key, $value);
+                       } // END - foreach
                } // END - if
        } elseif (!isAdmin()) {
                // He has already sent a mail within a specific time
@@ -176,7 +128,7 @@ LIMIT 1",
        // Still no error?
        if (empty($data['url'])) {
                // Check for userids
-               $result = SQL_QUERY_ESC("SELECT
+               $result = sqlQueryEscaped("SELECT
        `c`.`userid`
 FROM
        `{?_MYSQL_PREFIX?}_user_cats` AS `c`
@@ -198,51 +150,34 @@ ORDER BY
                        ), __FILE__, __LINE__);
 
                // Are there still receivers left?
-               if (SQL_NUMROWS($result) >= postRequestElement('receiver')) {
+               if (sqlNumRows($result) >= postRequestElement('receiver')) {
                        // Load receivers from database
-                       $TEST = array(); $count = '0';
-                       while ($holidayContent = SQL_FETCHARRAY($result)) {
-                               if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
-                                       // Check for his holiday status
-                                       $result_holiday = SQL_QUERY_ESC("SELECT
-       `id`
-FROM
-       `{?_MYSQL_PREFIX?}_user_holidays`
-WHERE
-       `userid`=%s AND
-       `holiday_start` < UNIX_TIMESTAMP() AND
-       `holiday_end` > UNIX_TIMESTAMP()
-LIMIT 1",
-                                               array($holidayContent['userid']), __FILE__, __LINE__);
-                                       if (SQL_NUMROWS($result_holiday) == 1) {
-                                               // Exclude user who are in holiday
-                                               $holidayContent['userid'] = '0';
-                                       } // END - if
-
-                                       // Free memory
-                                       SQL_FREERESULT($result_holiday);
-                               } // END - if
+                       $receiverArray = array(); $count = '0';
+                       while ($holidayContent = sqlFetchArray($result)) {
+                               // Run fetched data through pre filter chain
+                               $holidayContent = runFilterChain('pre_mail_recipient_check', $holidayContent);
 
+                               // Is still valid id?
                                if (isValidId($holidayContent['userid'])) {
-                                       // Add receiver
-                                       array_push($TEST, $holidayContent['userid']);
+                                       // Add receiver then
+                                       array_push($receiverArray, $holidayContent['userid']);
                                        $count++;
                                } // END - if
                        } // END - while
 
                        // Free memory
-                       SQL_FREERESULT($result);
+                       sqlFreeResult($result);
 
                        // Implode array into string for the sending pool
-                       $receiver = implode($TEST, ';');
+                       $receivers = implode($receiverArray, ';');
 
                        // Count array for maximum sent
-                       $content['target_send'] = count($TEST);
+                       $content['target_send'] = count($receiverArray);
 
                        // Update receiver list
-                       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receive_mails`=`receive_mails`-1 WHERE `userid` IN (%s) LIMIT %s",
+                       sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receive_mails`=`receive_mails`-1 WHERE `userid` IN (%s) LIMIT %s",
                                array(
-                                       convertReceivers($receiver),
+                                       convertReceivers($receivers),
                                        bigintval($content['target_send'])
                                ), __FILE__, __LINE__);
 
@@ -268,7 +203,7 @@ LIMIT 1",
                                                'sender'      => getMemberId(),
                                                'subject'     => postRequestElement('subject'),
                                                'text'        => postRequestElement('text'),
-                                               'receivers'   => $receiver
+                                               'receivers'   => $receivers,
                                                'payment_id'  => bigintval(postRequestElement('mail_type')),
                                                'data_type'   => 'TEMP',
                                                'timestamp'   => 'UNIX_TIMESTAMP()',
@@ -291,7 +226,7 @@ LIMIT 1",
                                        $data = array(
                                                'subject'     => postRequestElement('subject'),
                                                'text'        => postRequestElement('text'),
-                                               'receivers'   => $receiver
+                                               'receivers'   => $receivers,
                                                'payment_id'  => bigintval(postRequestElement('mail_type')),
                                                'timestamp'   => 'UNIX_TIMESTAMP()',
                                                'url'         => postRequestElement('url'),
@@ -311,7 +246,7 @@ LIMIT 1",
                                }
 
                                // Make sure only valid id numbers can pass
-                               assert(isValidId($data['id']));
+                               assert((isset($data['id'])) && (isValidId($data['id'])));
 
                                // Id is received so we can redirect the user, used points will be added when he send's out the mail
                                $data['url'] = 'modules.php?module=frametester&amp;order=' . bigintval($data['id']);
@@ -341,7 +276,7 @@ LIMIT 1",
        if (isAdmin()) $whereStatement = '';
 
        // Display order form
-       $result_cats = SQL_QUERY('SELECT
+       $result_cats = sqlQuery('SELECT
        `id`,
        `cat`
 FROM
@@ -351,7 +286,7 @@ ORDER BY
        `sort` ASC', __FILE__, __LINE__);
 
        // Some categories found?
-       if (!SQL_HASZERONUMS($result_cats)) {
+       if (!ifSqlHasZeroNumRows($result_cats)) {
                // Enought points left?
                if ($totalPoints > 0) {
                        // Initialize array...
@@ -363,93 +298,68 @@ ORDER BY
 
                        // Enable HTML checking
                        // @TODO Rewrite this to a filter
-                       $HTML = ''; $HOL_STRING = '';
+                       $HTML = '';
                        if ((isExtensionActive('html_mail')) && (postRequestElement('html') == 'Y')) {
                                $HTML = " AND `html`='Y'";
                        } // END - if
-                       if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
-                               // Extension's version is fine
-                               $HOL_STRING = " AND `holiday_active`='N'";
-                       } // END - if
 
                        // ... and begin loading stuff
-                       while ($categoriesContent = SQL_FETCHARRAY($result_cats)) {
+                       while ($categoriesContent = sqlFetchArray($result_cats)) {
                                $categories['id'][]   = bigintval($categoriesContent['id']);
                                array_push($categories['name'], $categoriesContent['cat']);
 
                                // Select users in current category
-                               $result_userids = SQL_QUERY_ESC('SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s AND `userid` != %s ORDER BY `userid` ASC',
+                               $result_userids = sqlQueryEscaped('SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s AND `userid` != %s ORDER BY `userid` ASC',
                                        array(bigintval($categoriesContent['id']), getMemberId()), __FILE__, __LINE__);
 
                                $userid_cnt = '0';
-                               while (list($userid) = SQL_FETCHROW($result_userids)) {
-                                       // Check for holiday system
-                                       $isHolidayActive = FALSE;
-                                       if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
-                                               // Check user's holiday status
-                                               $result_holiday = SQL_QUERY_ESC("SELECT
-       COUNT(`d`.`userid`) AS `cnt`
-FROM
-       `{?_MYSQL_PREFIX?}_user_data` AS `d`
-LEFT JOIN
-       `{?_MYSQL_PREFIX?}_user_holidays` AS `h`
-ON
-       `d`.`userid`=`h`.`userid`
-WHERE
-       `d`.`userid`=%s AND
-       `d`.`receive_mails` > 0 AND
-       `d`.`status`='CONFIRMED' AND
-       `d`.`holiday_active`='Y' AND
-       `h`.`holiday_start` < UNIX_TIMESTAMP() AND
-       `h`.`holiday_end` > UNIX_TIMESTAMP()
-LIMIT 1",
-                                                       array(bigintval($userid)), __FILE__, __LINE__);
-
-                                               // Fetch entry
-                                               list($count) = SQL_FETCHROW($result_holiday);
-
-                                               // Free memory
-                                               SQL_FREERESULT($result_holiday);
+                               while (list($userid) = sqlFetchRow($result_userids)) {
+                                       // Init filter data array
+                                       $filterData = array(
+                                               'userid'   => $userid,
+                                               'pre_okay' => TRUE,
+                                       );
 
-                                               // Is holiday is active?
-                                               $isHolidayActive = ($count == 1);
-                                       } // END - if
+                                       // Check for holiday system
+                                       $filterData = runFilterChain('pre_category_mail_order_check', $filterData);
 
-                                       if ($isHolidayActive === FALSE) {
+                                       // Still all fine?
+                                       if ($filterData['pre_okay'] === TRUE) {
                                                // Check if the user want's to receive mails?
-                                               $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",
+                                               $result_ver = sqlQueryEscaped("SELECT `zip` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s" . $HTML . " AND `receive_mails` > 0 AND `status`='CONFIRMED' LIMIT 1",
                                                        array(bigintval($userid)), __FILE__, __LINE__);
 
-                                               if ((SQL_NUMROWS($result_ver) == 1) && (isPostRequestElementSet('zip')) && (isOrderMultiPageEnabled())) {
+                                               // Is an entry found?
+                                               if ((sqlNumRows($result_ver) == 1) && (isPostRequestElementSet('zip')) && (isOrderMultiPageEnabled())) {
                                                        // Get zip code
-                                                       list($zip) = SQL_FETCHROW($result_ver);
+                                                       list($zip) = sqlFetchRow($result_ver);
                                                        if (substr($zip, 0, strlen(postRequestElement('zip'))) == postRequestElement('zip')) {
                                                                // Ok, ZIP code part is found
                                                                $userid_cnt++;
                                                        } // END - if
                                                } else {
                                                        // Count numbers up!
-                                                       $userid_cnt += SQL_NUMROWS($result_ver);
+                                                       $userid_cnt += sqlNumRows($result_ver);
                                                }
 
                                                // Free result
-                                               SQL_FREERESULT($result_ver);
+                                               sqlFreeResult($result_ver);
                                        } // END - if
                                } // END - while
 
                                // Free memory
-                               SQL_FREERESULT($result_userids);
+                               sqlFreeResult($result_userids);
                                array_push($categories['userids'], $userid_cnt);
                        } // END - while
 
                        // Free memory
-                       SQL_FREERESULT($result_cats);
+                       sqlFreeResult($result_cats);
 
                        // Now we need to load the mail types...
-                       $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__);
+                       $result = sqlQuery("SELECT `id`, `price`, `payment`, `mail_title` FROM `{?_MYSQL_PREFIX?}_payments` WHERE `price` > 0 AND `payment` > 0 ORDER BY `payment` ASC", __FILE__, __LINE__);
 
                        $payments = array();
-                       if (!SQL_HASZERONUMS($result)) {
+                       if (!ifSqlHasZeroNumRows($result)) {
                                // Is the error code set?
                                if (isGetRequestElementSet('code')) {
                                        // Display error message
@@ -457,12 +367,12 @@ LIMIT 1",
                                } // END - if
 
                                // Load all email types...
-                               while ($payment = SQL_FETCHARRAY($result)) {
+                               while ($payment = sqlFetchArray($result)) {
                                        array_push($payments, $payment);
                                } // END - while
 
                                // Free memory
-                               SQL_FREERESULT($result);
+                               sqlFreeResult($result);
 
                                // Output user's points
                                $content['total_points'] = $totalPoints;
@@ -493,7 +403,7 @@ LIMIT 1",
                                $target = '';
 
                                // Check if we already have an order placed and make it editable
-                               $result = SQL_QUERY_ESC("SELECT
+                               $result = sqlQueryEscaped("SELECT
        `subject`,
        `text`,
        `payment_id`,
@@ -510,9 +420,9 @@ WHERE
 LIMIT 1",
                                        array(getMemberId()), __FILE__, __LINE__);
 
-                               if (SQL_NUMROWS($result) == 1) {
+                               if (sqlNumRows($result) == 1) {
                                        // Old order found
-                                       $content = merge_array($content, SQL_FETCHARRAY($result));
+                                       $content = merge_array($content, sqlFetchArray($result));
 
                                        // Fix max receivers when it is too much
                                        if ((isset($categories['userids'][$content['cat_id']])) && ($content['target_send'] > $categories['userids'][$content['cat_id']])) {
@@ -531,7 +441,7 @@ LIMIT 1",
                                }
 
                                // Free result
-                               SQL_FREERESULT($result);
+                               sqlFreeResult($result);
 
                                if ((isPostRequestElementSet('data')) || ((getOrderMultiPage() != 'Y') && ((!isAdmin()) && (!isExtensionActive('html_mail'))))) {
                                        // Pre-output categories
@@ -604,7 +514,7 @@ LIMIT 1",
                        }
                } else {
                        // No points left
-                       displayMessage('<span class="bad">{--MEMBER_ORDER_NO_POINTS--}</span>');
+                       displayMessage('<span class="notice">{--MEMBER_ORDER_NO_POINTS--}</span>');
                }
        } else {
                // No cateogries are defined yet