Fix for _GET instead of variable, thanks to monakoianar
[mailer.git] / inc / mysql-manager.php
index 3ef6deee4669c9f75d5b0843facf12450b3330e7..bf54a9f45b5b4dff9450c065025295a88bdff584 100644 (file)
@@ -50,7 +50,7 @@ function ADD_MODULE_TITLE($mod) {
                        $name = $cacheArray['modules']['title'][$mod];
 
                        // Update cache hits
-                       $_CONFIG['cache_hits']++;
+                       if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
                } else {
                        // Load from database
                        $result = SQL_QUERY_ESC("SELECT title FROM "._MYSQL_PREFIX."_mod_reg WHERE module='%s' LIMIT 1", array($mod), __FILE__, __LINE__);
@@ -115,7 +115,7 @@ function CHECK_MODULE($mod) {
                        $mem    = $cacheArray['modules']['mem_only'][$mod_chk];
 
                        // Update cache hits
-                       $_CONFIG['cache_hits']++;
+                       if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
                        $found = true;
                } else {
                        // No, then we have to update it!
@@ -191,7 +191,12 @@ function CHECK_MODULE($mod) {
 // Add menu description pending on given file name (without path!)
 function ADD_DESCR($ACC_LVL, $file, $return = false, $output = true) {
        global $DEPTH, $_CONFIG;
+       // Use only filename of the file ;)
+       $file = basename($file);
+
+       // Init variables
        $LINK_ADD = ""; $OUT = ""; $AND = "";
+
        // First we have to do some analysis...
        if (ereg("action-", $file)) {
                // This is an action file!
@@ -454,7 +459,7 @@ function IS_ADMIN($admin="")
                $valPass = $cacheArray['admin_hash'];
        } elseif ((!empty($passCookie)) && (isset($cacheArray['admins']['password'][$admin])) && (!empty($admin))) {
                // Count cache hits
-               $_CONFIG['cache_hits']++;
+               if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
 
                // Login data is valid or not?
                $valPass = generatePassString($cacheArray['admins']['password'][$admin]);
@@ -1070,16 +1075,16 @@ function REMOVE_RECEIVER(&$ARRAY, $key, $uid, $pool_id, $stats_id="", $bonus=fal
        return $ret;
 }
 //
-function GET_TOTAL_DATA($search, $tableName, $lookFor, $whereStatement="userid", $onlyRows=false) {
+function GET_TOTAL_DATA($search, $tableName, $lookFor, $whereStatement="userid", $onlyRows=false, $add="") {
        $ret = 0;
        if ($onlyRows) {
                // Count rows
-               $result = SQL_QUERY_ESC("SELECT COUNT(%s) FROM "._MYSQL_PREFIX."_%s WHERE %s='%s'",
-                array($lookFor, $tableName, $whereStatement, $search), __FILE__, __LINE__);
+               $result = SQL_QUERY_ESC("SELECT COUNT(%s) FROM "._MYSQL_PREFIX."_%s WHERE %s='%s'%s",
+                array($lookFor, $tableName, $whereStatement, $search, $add), __FILE__, __LINE__);
        } else {
                // Add all rows
-               $result = SQL_QUERY_ESC("SELECT SUM(%s) FROM "._MYSQL_PREFIX."_%s WHERE %s='%s'",
-                array($lookFor, $tableName, $whereStatement, $search), __FILE__, __LINE__);
+               $result = SQL_QUERY_ESC("SELECT SUM(%s) FROM "._MYSQL_PREFIX."_%s WHERE %s='%s'%s",
+                array($lookFor, $tableName, $whereStatement, $search, $add), __FILE__, __LINE__);
        }
 
        // Load row
@@ -1112,7 +1117,7 @@ function GET_REF_LEVEL_PERCENTS ($level) {
                        $per = $cacheArray['ref_depths']['percents'][$key];
 
                        // Count cache hit
-                       $_CONFIG['cache_hits']++;
+                       if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
                }
        } else {
                // Get referal data
@@ -1134,15 +1139,15 @@ function GET_REF_LEVEL_PERCENTS ($level) {
 }
 /**
  *
- * Dynamic referral system, can also send mails!
+ * Dynamic referal system, can also send mails!
  *
- * uid         = Referral ID wich should receive...
+ * uid         = Referal ID wich should receive...
  * points      = ... xxx points
- * send_notify = shall I send the referral an email or not?
+ * send_notify = shall I send the referal an email or not?
  * rid         = inc/modules/guest/what-confirm.php need this (DEPRECATED???)
  * locked      = Shall I pay it to normal (false) or locked (true) points ammount?
  * add_mode    = Add points only to $uid or also refs? (WARNING! Changing "ref" to "direct"
- *               for default value will cause no referral will get points ever!!!)
+ *               for default value will cause no referal will get points ever!!!)
  */
 function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $locked=false, $add_mode="ref") {
        //* DEBUG: */ print "----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n";
@@ -1158,12 +1163,12 @@ function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $lock
                return;
        } // END - if
 
-       // Count up referral depth
+       // Count up referal depth
        if (!isset($DEPTH)) {
-               // Initialialize referral system
+               // Initialialize referal system
                $DEPTH = 0;
        } else {
-               // Increase referral level
+               // Increase referal level
                $DEPTH++;
        }
 
@@ -1229,7 +1234,7 @@ function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $lock
                                );
 
                                // Load email template
-                               $msg = LOAD_EMAIL_TEMPLATE("confirm-referral", $content, bigintval($uid));
+                               $msg = LOAD_EMAIL_TEMPLATE("confirm-referal", $content, bigintval($uid));
 
                                SEND_EMAIL($email, THANX_REFERRAL_ONE, $msg);
                        } elseif (($send_notify) && ($ref == 0) && (!$locked) && ($add_mode == "direct") && (!defined('__POINTS_VALUE'))) {
@@ -1267,7 +1272,7 @@ function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $lock
 function UPDATE_REF_COUNTER ($uid) {
        global $cacheArray, $cacheInstance;
 
-       // Make it sure referral level zero (member him-/herself) is at least selected
+       // Make it sure referal level zero (member him-/herself) is at least selected
        if (empty($cacheArray['ref_level'][$uid])) $cacheArray['ref_level'][$uid] = 1;
        //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},level={$cacheArray['ref_level'][$uid]}<br />\n";
 
@@ -1284,7 +1289,7 @@ function UPDATE_REF_COUNTER ($uid) {
                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid}<br />\n";
        } // END - if
 
-       // Check for his referral
+       // Check for his referal
        $result = SQL_QUERY_ESC("SELECT refid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
                array(bigintval($uid)), __FILE__, __LINE__);
 
@@ -1295,15 +1300,15 @@ function UPDATE_REF_COUNTER ($uid) {
        SQL_FREERESULT($result);
        //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):uid={$uid},ref={$ref}<br />\n";
 
-       // When he has a referral...
+       // When he has a referal...
        if (($ref > 0) && ($ref != $uid)) {
-               // Move to next referral level and count his counter one up!
+               // Move to next referal level and count his counter one up!
                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):ref={$ref} - ADVANCE!<br />\n";
                $cacheArray['ref_level'][$uid]++; UPDATE_REF_COUNTER($ref);
        } elseif ((($ref == $uid) || ($ref == 0)) && (GET_EXT_VERSION("cache") >= "0.1.2")) {
                // Remove cache here
                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):ref={$ref} - CACHE!<br />\n";
-               if ($cacheInstance->cache_file("refsystem", true)) $cacheInstance->cache_destroy();
+               REBUILD_CACHE("refsystem", "refsystem");
        }
 
        // "Walk" back here
@@ -1406,7 +1411,7 @@ function GET_ADMIN_ID($login) {
                $ret = $cacheArray['admins']['aid'][$login];
 
                // Update cache hits
-               $_CONFIG['cache_hits']++;
+               if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
        } elseif (!EXT_IS_ACTIVE("cache")) {
                // Load from database
                $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
@@ -1431,7 +1436,7 @@ function GET_ADMIN_HASH($login)
                $ret = $cacheArray['admins']['password'][$login];
 
                // Update cache hits
-               $_CONFIG['cache_hits']++;
+               if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
        } elseif (!EXT_IS_ACTIVE("cache")) {
                // Load from database
                $result = SQL_QUERY_ESC("SELECT password FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
@@ -1458,7 +1463,7 @@ function GET_ADMIN_LOGIN ($aid) {
                $ret = $cacheArray['admins']['login'][$aid];
 
                // Update cache hits
-               $_CONFIG['cache_hits']++;
+               if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
        } elseif (!EXT_IS_ACTIVE("cache")) {
                // Load from database
                $result = SQL_QUERY_ESC("SELECT login FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
@@ -1486,7 +1491,7 @@ function GET_ADMIN_EMAIL ($aid) {
                $ret = $cacheArray['admins']['email'][$aid];
 
                // Update cache hits
-               $_CONFIG['cache_hits']++;
+               if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
        } elseif (!EXT_IS_ACTIVE("cache")) {
                // Load from database
                $result_aid = SQL_QUERY_ESC("SELECT email FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
@@ -1516,7 +1521,7 @@ function GET_ADMIN_DEFAULT_ACL ($aid) {
                $ret = $cacheArray['admins']['def_acl'][$aid];
 
                // Update cache hits
-               $_CONFIG['cache_hits']++;
+               if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
        } else {
                // Load from database
                $result_aid = SQL_QUERY_ESC("SELECT default_acl FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
@@ -1784,11 +1789,11 @@ function MODULE_HAS_MENU($mod, $forceDb = false)
                if (isset($cacheArray['modules']['has_menu'][$mod])) {
                        // Check module cache and count hit
                        $ret = ($cacheArray['modules']['has_menu'][$mod] == "Y");
-                       $_CONFIG['cache_hits']++;
+                       if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
                } elseif (isset($cacheArray['extensions']['ext_menu'][$mod])) {
                        // Check cache and count hit
                        $ret = ($cacheArray['extensions']['ext_menu'][$mod] == "Y");
-                       $_CONFIG['cache_hits']++;
+                       if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
                }
        } elseif ((GET_EXT_VERSION("sql_patches") >= "0.3.6") && ((!EXT_IS_ACTIVE("cache")) || ($forceDb === true))) {
                // Check database for entry
@@ -1920,5 +1925,227 @@ VALUES (%s,0,'NEW','EXTENSION','%s','%s',UNIX_TIMESTAMP())",
        // Free memory
        SQL_FREERESULT($result);
 }
+
+// Prepares an SQL statement part for HTML mail and/or holiday depency
+function PREPARE_SQL_HTML_HOLIDAY ($mode) {
+       // Exclude no users by default
+       $MORE = "";
+
+       // HTML mail?
+       if ($mode == "html") $MORE = " AND html='Y'";
+       if (GET_EXT_VERSION("holiday") >= "0.1.3") {
+               // Add something for the holiday extension
+               $MORE .= " AND holiday_active='N'";
+       } // END - if
+
+       // Return result
+       return $MORE;
+}
+
+// "Getter" for total available receivers
+function GET_TOTAL_RECEIVERS ($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),
+               __FILE__, __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 ADD_CATEGORY_OPTIONS ($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", __FILE__, __LINE__);
+       if (SQL_NUMROWS($result) > 0) {
+               // ... and begin loading stuff
+               while (list($id, $cat) = SQL_FETCHROW($result)) {
+                       // Transfer some data
+                       $CATS['id'][]   = $id;
+                       $CATS['name'][] = $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($id)), __FILE__, __LINE__);
+
+                       // Start adding all
+                       $uid_cnt = 0;
+                       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)), __FILE__, __LINE__);
+                               $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;
+               }
+
+               // 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>\n";
+               }
+       } else {
+               // No cateogries are defined yet
+               $OUT = "<option class=\"member_failed\">".MEMBER_NO_CATS."</option>\n";
+       }
+
+       // Return HTML code
+       return $OUT;
+}
+
+// Add bonus mail to queue
+function ADD_BONUS_MAIL_TO_QUEUE ($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 = SELECTION_COUNT(explode(";", $receiverList));
+
+       // Receiver is zero?
+       if ($receiver == 0) {
+               // Then auto-fix it
+               $receiver = $target;
+       } // END - if
+
+       // HTML extension active?
+       if (EXT_IS_ACTIVE("html")) {
+               // No HTML by default
+               $HTML = "N";
+
+               // HTML mode?
+               if ($mode == "html") $HTML = "Y";
+
+               // Add HTML mail
+               $result = 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,
+       addslashes($text),
+       $receiverList,
+       $points,
+       $seconds,
+       $url,
+       $cat,
+       $target,
+       bigintval($receiver),
+       $HTML
+), __FILE__, __LINE__);
+       } else {
+               // Add regular mail
+               $result = 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,
+       addslashes($text),
+       $receiverList,
+       $points,
+       $seconds,
+       $url,
+       $cat,
+       $target,
+       bigintval($receiver),
+), __FILE__, __LINE__);
+       }
+}
+
+// Generate a receiver list for given category and maximum receivers
+function GENERATE_RECEIVER_LIST ($cat, $receiver, $mode="") {
+       global $_CONFIG;
+
+       // Init variables
+       $CAT_TABS     = "%s";
+       $CAT_WHERE    = "";
+       $receiverList = "";
+
+       // 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 = GET_TOTAL_RECEIVERS($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, $_CONFIG['order_select'], $_CONFIG['order_mode'], $receiver), __FILE__, __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, $_CONFIG['order_select'], $_CONFIG['order_mode'], $receiver), __FILE__, __LINE__);
+       }
+
+       // Entries found?
+       if ((SQL_NUMROWS($result) >= $receiver) && ($receiver > 0)) {
+               // Load all entries
+               while (list($REC) = SQL_FETCHROW($result)) {
+                       // Add receiver when not empty
+                       if (!empty($REC)) $receiverList .= $REC.";";
+               } // END - while
+
+               // Free memory
+               SQL_FREERESULT($result);
+
+               // Remove trailing semicolon
+               $receiverList = substr($receiverList, 0, -1);
+       } // END - if
+
+       // Return list
+       return $receiverList;
+}
+
 //
 ?>