A lot has been rewritten, ext-teams added, ext-forced continued:
[mailer.git] / inc / libs / user_functions.php
index 73bb07cf4995347e25e46702d976bf3a223fafad..029ab472ff5fbdb2c3f837c63051bc233ed405b2 100644 (file)
@@ -41,12 +41,9 @@ if (!defined('__SECURITY')) {
 } // END - if
 
 // Add links for selecting some users
-function alpha ($sortby, $colspan, $return=false) {
-       if (!isGetRequestParameterSet('offset')) {
-               setGetRequestParameter('offset', 0);
-       } // END - if
-       $add = '&page=' . getRequestParameter('page').'&offset=' . getRequestParameter('offset');
-       foreach (array('mode','status') as $param) {
+function alpha ($sortby) {
+       $add = '';
+       foreach (array('page','offset','mode','status') as $param) {
                if (isGetRequestParameterSet($param)) {
                        $add .= '&' . $param . '=' . getRequestParameter($param);
                } // END - if
@@ -62,13 +59,7 @@ function alpha ($sortby, $colspan, $return=false) {
                        $OUT .= '<strong>' . $ltr . '</strong>';
                } else {
                        // Output link to letter
-                       $OUT .= '<a href="{%url=modules.php?module=admin&amp;what=' . getWhat();
-                       foreach (array('mode','status') as $param) {
-                               if (isGetRequestParameterSet($param)) {
-                                       $OUT .= '&amp;' . $param . '=' . getRequestParameter($param);
-                               } // END - if
-                       } // END - foreach
-                       $OUT .= '&amp;letter=' . $ltr . '&amp;sortby=' . $sortby . $add . '%}">' . $ltr . '</a>';
+                       $OUT .= '<a href="{%url=modules.php?module=admin&amp;what=' . getWhat() . '&amp;letter=' . $ltr . '&amp;sortby=' . $sortby . $add . '%}">' . $ltr . '</a>';
                }
 
                if ((($counter / getConfig('user_alpha')) == round($counter / getConfig('user_alpha'))) && ($counter > 0)) {
@@ -80,23 +71,18 @@ function alpha ($sortby, $colspan, $return=false) {
 
        // Prepare content
        $content = array (
-               'colspan2'        => $colspan,
                'alpha_selection' => $OUT
        );
 
        // Load template
        $OUT = loadTemplate('admin_list_user_alpha', true, $content);
-       if ($return === true) {
-               // Return generated code
-               return $OUT;
-       } else {
-               // Output generated code
-               outputHtml($OUT);
-       }
+
+       // Return generated code
+       return $OUT;
 }
 
 // Add links for sorting
-function addSortLinks ($letter, $sortby, $colspan, $return=false) {
+function addSortLinks ($letter, $sortby) {
        $OUT = '';
        if (!isGetRequestParameterSet('offset')) setGetRequestParameter('offset', 0);
        if (!isGetRequestParameterSet('page'))   setGetRequestParameter('page'  , 0);
@@ -137,82 +123,67 @@ function addSortLinks ($letter, $sortby, $colspan, $return=false) {
                }
        } // END - foreach
 
-       // Add list and colspan
+       // Add output
        $content['list'] = substr($OUT, 0, -1);
-       $content['colspan2'] = $colspan;
 
        // Load template
        $OUT = loadTemplate('admin_list_user_sort', true, $content);
 
-       // Should we return or output?
-       if ($return === true) {
-               // Return code
-               return $OUT;
-       } else {
-               // Output code
-               outputHtml($OUT);
-       }
+       // Return code
+       return $OUT;
 }
 
 // Add page navigation
-function addPageNavigation ($numPages, $offset, $showForm, $colspan, $return=false) {
-       // @TODO These two constants are no longer used, maybe we reactivate this code?
-       //if ($showForm === true) {
-       //      // Load form for changing number of lines
-       //      define('__FORM_HEADER', loadTemplate('admin_list_user_sort_form', true));
-       //      define('__FORM_FOOTER', '<tr><td colspan="'.$colspan.'" class="seperator bottom">&nbsp;</td></tr>');
-       //} else {
-       //      // Empty row
-       //      define('__FORM_HEADER', '<tr><td colspan="' . $colspan . '" class="seperator">&nbsp;</td></tr>');
-       //      define('__FORM_FOOTER', '<tr><td colspan="' . $colspan . '" class="seperator bottom">&nbsp;</td></tr>');
-       //}
-
+function addPageNavigation ($numPages) {
+       // Start with empty content
        $OUT = '';
-       for ($page = 1; $page <= $numPages; $page++) {
-               if (($page == getRequestParameter('page')) || ((!isGetRequestParameterSet('page')) && ($page == 1))) {
-                       $OUT .= '<strong>-';
-               } else {
-                       if (!isGetRequestParameterSet('letter')) setGetRequestParameter('letter', '');
-                       if (!isGetRequestParameterSet('sortby')) setGetRequestParameter('sortby', 'userid');
 
-                       // Base link
-                       $OUT .= '<a href="{%url=modules.php?module=admin&amp;what=' . getWhat();
+       // Create only the navigation if page count > 1
+       if ($numPages > 1) {
+               // Create navigation links for every page
+               for ($page = 1; $page <= $numPages; $page++) {
+                       if (($page == getRequestParameter('page')) || ((!isGetRequestParameterSet('page')) && ($page == 1))) {
+                               $OUT .= '<strong>-';
+                       } else {
+                               if (!isGetRequestParameterSet('letter')) setGetRequestParameter('letter', '');
+                               if (!isGetRequestParameterSet('sortby')) setGetRequestParameter('sortby', 'userid');
 
-                       // Add status/mode
-                       foreach (array('mode','status') as $param) {
-                               if (isGetRequestParameterSet($param)) {
-                                       $OUT .= '&amp;' . $param . '=' . getRequestParameter($param);
-                               } // END - if
-                       } // END - foreach
+                               // Base link
+                               $OUT .= '<a href="{%url=modules.php?module=admin&amp;what=' . getWhat();
 
-                       // Letter and so on
-                       $OUT .= '&amp;letter=' . getRequestParameter('letter') . '&amp;sortby=' . getRequestParameter('sortby') . '&amp;page=' . $page . '&amp;offset=' . $offset . '%}">';
-               }
+                               // Add status/mode
+                               foreach (array('mode','status') as $param) {
+                                       if (isGetRequestParameterSet($param)) {
+                                               $OUT .= '&amp;' . $param . '=' . getRequestParameter($param);
+                                       } // END - if
+                               } // END - foreach
 
-               $OUT .= $page;
+                               // Letter and so on
+                               $OUT .= '&amp;letter=' . getRequestParameter('letter') . '&amp;sortby=' . getRequestParameter('sortby') . '&amp;page=' . $page . '&amp;offset=' . getConfig('user_limit') . '%}">';
+                       }
 
-               if (($page == getRequestParameter('page')) || ((!isGetRequestParameterSet('page')) && ($page == 1))) {
-                       $OUT .= '-</strong>';
-               } else  {
-                       $OUT .= '</a>';
-               }
+                       $OUT .= $page;
 
-               if ($page < $numPages) $OUT .= '|';
-       } // END - for
+                       if (($page == getRequestParameter('page')) || ((!isGetRequestParameterSet('page')) && ($page == 1))) {
+                               $OUT .= '-</strong>';
+                       } else  {
+                               $OUT .= '</a>';
+                       }
 
-       // Remember the list and colspan
-       $content['list']     = $OUT;
-       $content['colspan2'] = $colspan;
+                       if ($page < $numPages) {
+                               $OUT .= '|';
+                       } // END - if
+               } // END - for
 
-       // Load template
-       $OUT = loadTemplate('admin_list_user_pagenav', true, $content);
-       if ($return === true) {
-               // Return code
-               return $OUT;
-       } else {
-               // Output code
-               outputHtml($OUT);
-       }
+               // Add list output
+               $content['list'] = $OUT;
+
+               // Load template
+               $OUT = loadTemplate('admin_list_user_pagenav', true, $content);
+       } // END - if
+
+       // Return code
+       return $OUT;
 }
 
 // Create email link to user's account
@@ -250,7 +221,7 @@ LIMIT 1",
 // @TODO Double-check configuration entry here
 function determineRandomReferalId () {
        // Default is zero refid
-       $refid = null;
+       $refid = NULL;
 
        // Is the extension version fine?
        if (isExtensionInstalledAndNewer('user', '0.3.4')) {
@@ -270,10 +241,6 @@ function determineRandomReferalId () {
                        if (SQL_NUMROWS($result) == 1) {
                                // Use that userid as new referal id
                                list($refid) = SQL_FETCHROW($result);
-
-                               // Reset all users, this makes this random referal id more challenging
-                               SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `rand_confirmed`=0",
-                                       array($refid), __FUNCTION__, __LINE__);
                        } // END - if
 
                        // Free result
@@ -505,7 +472,7 @@ function doNewUserPassword ($email, $userid) {
 }
 
 // Get timestamp for given stats type and data
-function getEpocheTimeFromUserStats ($statsType, $statsData, $userid = '0') {
+function getEpocheTimeFromUserStats ($statsType, $statsData, $userid = NULL) {
        // Default timestamp is zero
        $data['inserted'] = '0';
 
@@ -560,7 +527,7 @@ function insertUserStatsRecord ($userid, $statsType, $statsData) {
        // Does it exist?
        if ((!getEpocheTimeFromUserStats($statsType, $statsData, $userid)) && (!is_array($statsData))) {
                // Then insert it!
-               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_stats_data` (`userid`, `stats_type`, `stats_data`) VALUES (%s,'%s','%s')",
+               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_stats_data` (`userid`,`stats_type`,`stats_data`) VALUES (%s,'%s','%s')",
                        array(
                                bigintval($userid),
                                $statsType,
@@ -581,10 +548,10 @@ function doConfirmUserAccount ($hash) {
        );
 
        // Initialize the user id
-       $userid = '0';
+       $userid = NULL;
 
        // Search for an unconfirmed or confirmed account
-       $result = SQL_QUERY_ESC("SELECT `userid`, `refid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `user_hash`='%s' AND (`status`='UNCONFIRMED' OR `status`='CONFIRMED') LIMIT 1",
+       $result = SQL_QUERY_ESC("SELECT `userid`,`refid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `user_hash`='%s' AND (`status`='UNCONFIRMED' OR `status`='CONFIRMED') LIMIT 1",
                array($hash), __FILE__, __LINE__);
        if (SQL_NUMROWS($result) == 1) {
                // Ok, he want's to confirm now so we load some data
@@ -620,7 +587,7 @@ LIMIT 1",
                        sendEmail($userid, '{--GUEST_THANX_CONFIRM--}', $message);
 
                        // Maybe he got "referaled"?
-                       if (($refid > 0) && ($refid != $userid)) {
+                       if ((isValidUserId($refid)) && ($refid != $userid)) {
                                // Select the referal userid
                                if (fetchUserData($refid)) {
                                        // Update ref counter...
@@ -742,11 +709,17 @@ function doExpressionUser ($data) {
        // User-related data, so is there a userid?
        if (!empty($data['matches'][4][$data['key']])) {
                // Do we have a userid or $userid?
-               if ($data['matches'][4][$data['key']] == '$userid') {
+               if (substr($data['matches'][4][$data['key']], 0, 1) == '$') {
                        // Use dynamic call
-                       $functionName = "getFetchedUserData('userid', \$userid, '" . $data['callback'] . "')";
+                       $functionName = "getFetchedUserData('userid', " . $data['matches'][4][$data['key']] . ", '" . $data['callback'] . "')";
                } elseif (!empty($data['matches'][4][$data['key']])) {
-                       // User data found
+                       // Do we have a number or a dollar sign in front of it?
+                       if (preg_replace('/[^0123456789]/', '', $data['matches'][4][$data['key']]) != $data['matches'][4][$data['key']]) {
+                               // Possible database column, so get it again
+                               $data['matches'][4][$data['key']] = "getFetchedUserData('userid', getMemberId(), '" . $data['matches'][4][$data['key']] . "')";
+                       } // END - if
+
+                       // Fix all together
                        $functionName = "getFetchedUserData('userid', " . $data['matches'][4][$data['key']] . ", '" . $data['callback'] . "')";
                }
        } elseif ((!empty($data['callback'])) && (isUserDataValid())) {
@@ -759,6 +732,7 @@ function doExpressionUser ($data) {
                // Surround the original function call with it
                $functionName = $data['extra_func'] . '(' . $functionName . ')';
        } // END - if
+       //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'functionName=' . $functionName);
 
        // Generate replacer
        $replacer = '{DQUOTE} . ' . $functionName . ' . {DQUOTE}';
@@ -771,7 +745,7 @@ function doExpressionUser ($data) {
 }
 
 // Template call-back function for list_user admin function
-function doTemplateAdminListUserTitle ($template, $dummy = false) {
+function doTemplateAdminListUserTitle ($template, $clear = false) {
        // Init title with "all accounts"
        $code = '{--ADMIN_LIST_ALL_ACCOUNTS--}';