+ // Return result
+ return $MORE;
+}
+
+// "Getter" for total available receivers
+function getTotalReceivers ($mode='normal') {
+ // Query database
+ $result_all = SQL_QUERY("SELECT userid
+FROM `{!_MYSQL_PREFIX!}_user_data`
+WHERE `status`='CONFIRMED' AND receive_mails > 0 ".PREPARE_SQL_HTML_HOLIDAY($mode),
+ __FUNCTION__, __LINE__);
+
+ // Get num rows
+ $numRows = SQL_NUMROWS($result_all);
+
+ // Free result
+ SQL_FREERESULT($result_all);
+
+ // Return value
+ return $numRows;
+}
+
+// Returns HTML code with an "<option> list" of all categories
+function generateCategoryOptionsList ($mode) {
+ // Prepare WHERE statement
+ $whereStatement = " WHERE `visible`='Y'";
+ if (IS_ADMIN()) $whereStatement = '';
+
+ // Initialize array...
+ $CATS = array(
+ 'id' => array(),
+ 'name' => array(),
+ 'uids' => array()
+ );
+
+ // Get categories
+ $result = SQL_QUERY("SELECT id, cat FROM `{!_MYSQL_PREFIX!}_cats`".$whereStatement." ORDER BY `sort`", __FUNCTION__, __LINE__);
+ if (SQL_NUMROWS($result) > 0) {
+ // ... and begin loading stuff
+ while ($content = SQL_FETCHARRAY($result)) {
+ // Transfer some data
+ $CATS['id'][] = $content['id'];
+ $CATS['name'][] = $content['cat'];
+
+ // Check which users are in this category
+ $result_uids = SQL_QUERY_ESC("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_cats` WHERE cat_id=%s",
+ array(bigintval($content['id'])), __FUNCTION__, __LINE__);
+
+ // Start adding all
+ $uid_cnt = 0;
+ // @TODO Rewrite this to $content = SQL_FETCHARRAY()
+ while (list($ucat) = SQL_FETCHROW($result_uids)) {
+ $result_ver = SQL_QUERY_ESC("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_data`
+WHERE userid=%s AND `status`='CONFIRMED' AND receive_mails > 0".PREPARE_SQL_HTML_HOLIDAY($mode)." LIMIT 1",
+ array(bigintval($ucat)), __FUNCTION__, __LINE__);
+
+ // Add user count
+ $uid_cnt += SQL_NUMROWS($result_ver);
+
+ // Free memory
+ SQL_FREERESULT($result_ver);
+ } // END - while
+
+ // Free memory
+ SQL_FREERESULT($result_uids);
+
+ // Add counter
+ $CATS['uids'][] = $uid_cnt;
+ } // END - while
+
+ // Free memory
+ SQL_FREERESULT($result);
+
+ // Generate options
+ $OUT = '';
+ foreach ($CATS['id'] as $key => $value) {
+ if (strlen($CATS['name'][$key]) > 20) $CATS['name'][$key] = substr($CATS['name'][$key], 0, 17)."...";
+ $OUT .= ' <option value="' . $value . '">' . $CATS['name'][$key] . ' (' . $CATS['uids'][$key] . ' {--USER_IN_CAT--})</option>';
+ } // END - foreach
+ } else {
+ // No cateogries are defined yet
+ $OUT = '<option class="member_failed">{!MEMBER_NO_CATS!}</option>';
+ }
+
+ // Return HTML code
+ return $OUT;
+}
+
+// Add bonus mail to queue
+function addBonusMailToQueue ($subject, $text, $receiverList, $points, $seconds, $url, $cat, $mode='normal', $receiver=0) {
+ // Is admin or bonus extension there?
+ if (!IS_ADMIN()) {
+ // Abort here
+ return false;
+ } elseif (!EXT_IS_ACTIVE('bonus')) {
+ // Abort here
+ return false;
+ }
+
+ // Calculcate target sent
+ $target = countSelection(explode(';', $receiverList));
+
+ // Receiver is zero?
+ if ($receiver == 0) {
+ // Then auto-fix it
+ $receiver = $target;
+ } // END - if
+
+ // HTML extension active?
+ if (EXT_IS_ACTIVE('html_mail')) {
+ // No HTML by default
+ $HTML = 'N';
+
+ // HTML mode?
+ if ($mode == 'html') $HTML = 'Y';
+
+ // Add HTML mail
+ SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_bonus`
+(subject, text, receivers, points, time, data_type, timestamp, url, cat_id, target_send, mails_sent, html_msg)
+VALUES ('%s','%s','%s','%s','%s','NEW', UNIX_TIMESTAMP(),'%s','%s','%s','%s','%s')",
+ array(
+ $subject,
+ $text,
+ $receiverList,
+ $points,
+ $seconds,
+ $url,
+ $cat,
+ $target,
+ bigintval($receiver),
+ $HTML
+ ), __FUNCTION__, __LINE__);
+ } else {
+ // Add regular mail
+ SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_bonus`
+(subject, text, receivers, points, time, data_type, timestamp, url, cat_id, target_send, mails_sent)
+VALUES ('%s','%s','%s','%s','%s','NEW', UNIX_TIMESTAMP(),'%s','%s','%s','%s')",
+ array(
+ $subject,
+ $text,
+ $receiverList,
+ $points,
+ $seconds,
+ $url,
+ $cat,
+ $target,
+ bigintval($receiver),
+ ), __FUNCTION__, __LINE__);
+ }
+}
+
+// Generate a receiver list for given category and maximum receivers
+function generateReceiverList ($cat, $receiver, $mode = '') {
+ // Init variables
+ $CAT_TABS = "%s";
+ $CAT_WHERE = '';
+ $receiverList = '';
+ $result = false;
+
+ // Secure data
+ $cat = bigintval($cat);
+ $receiver = bigintval($receiver);
+
+ // Is the receiver zero and mode set?
+ if (($receiver == 0) && (!empty($mode))) {
+ // Auto-fix receiver maximum
+ $receiver = getTotalReceivers($mode);
+ } // END - if
+
+ // Category given?
+ if ($cat > 0) {
+ // Select category
+ $CAT_TABS = "LEFT JOIN `{!_MYSQL_PREFIX!}_user_cats` AS c ON d.userid=c.userid";
+ $CAT_WHERE = " AND c.cat_id=%s";
+ } // END - if
+
+ // Exclude users in holiday?
+ if (GET_EXT_VERSION('holiday') >= '0.1.3') {
+ // Add something for the holiday extension
+ $CAT_WHERE .= " AND d.`holiday_active`='N'";
+ } // END - if
+
+ if ((EXT_IS_ACTIVE('html_mail')) && ($mode == 'html')) {
+ // Only include HTML receivers
+ $result = SQL_QUERY_ESC("SELECT d.userid FROM `{!_MYSQL_PREFIX!}_user_data` AS d ".$CAT_TABS." WHERE d.`status`='CONFIRMED' AND d.`html`='Y'".$CAT_WHERE." ORDER BY d.%s %s LIMIT %s",
+ array($cat, getConfig('order_select'), getConfig('order_mode'), $receiver), __FUNCTION__, __LINE__);
+ } else {
+ // Include all
+ $result = SQL_QUERY_ESC("SELECT d.userid FROM `{!_MYSQL_PREFIX!}_user_data` AS d ".$CAT_TABS." WHERE d.`status`='CONFIRMED'".$CAT_WHERE." ORDER BY d.%s %s LIMIT %s",
+ array($cat, getConfig('order_select'), getConfig('order_mode'), $receiver), __FUNCTION__, __LINE__);
+ }
+
+ // Entries found?
+ if ((SQL_NUMROWS($result) >= $receiver) && ($receiver > 0)) {
+ // Load all entries
+ while ($content = SQL_FETCHARRAY($result)) {
+ // Add receiver when not empty
+ if (!empty($content['userid'])) $receiverList .= $content['userid'] . ';';
+ } // END - while
+
+ // Free memory
+ SQL_FREERESULT($result);
+
+ // Remove trailing semicolon
+ $receiverList = substr($receiverList, 0, -1);
+ } // END - if
+
+ // Return list
+ return $receiverList;
+}
+
+// Get timestamp for given stats type and data
+function getTimestampFromUserStats ($type, $data, $uid = 0) {
+ // Default timestamp is zero
+ $stamp = 0;
+
+ // User id set?
+ if ((isUserIdSet()) && ($uid == 0)) {
+ $uid = getUserId();
+ } // END - if
+
+ // Is the extension installed and updated?
+ if ((!EXT_IS_ACTIVE('sql_patches')) || (EXT_VERSION_IS_OLDER('sql_patches', '0.5.6'))) {
+ // Return zero here
+ return $stamp;
+ } // END - if
+
+ // Try to find the entry
+ $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`inserted`) AS `stamp`
+FROM `{!_MYSQL_PREFIX!}_user_stats_data`
+WHERE userid=%s AND stats_type='%s' AND stats_data='%s'
+LIMIT 1",
+ array(bigintval($uid), $type, $data), __FUNCTION__, __LINE__);
+
+ // Is the entry there?
+ if (SQL_NUMROWS($result) == 1) {
+ // Get this stamp
+ list($stamp) = SQL_FETCHROW($result);
+ } // END - if
+
+ // Free result