Example #1
0
function get_listing_stats($id, $extended = false)
{
    require 'config.php';
    $db_link = mysql_connect($db_server, $db_user, $db_password) or die(DATABASE_CONNECT_ERROR . mysql_error());
    mysql_select_db($db_database) or die(DATABASE_CONNECT_ERROR . mysql_error());
    $query = "SELECT * FROM `{$db_owned}` WHERE `listingid` = '{$id}'";
    $result = mysql_query($query);
    if (!$result) {
        log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
        die(STANDARD_ERROR);
    }
    $info = mysql_fetch_array($result);
    $db_link_list = mysql_connect($info['dbserver'], $info['dbuser'], $info['dbpassword']);
    if ($db_link_list === false) {
        echo '<p class="error">' . DATABASE_CONNECT_ERROR . " Can't connect to MySQL server on {$info['dbserver']}</p>";
        return;
    }
    $dbselected = mysql_select_db($info['dbdatabase'], $db_link_list);
    if (!$dbselected) {
        echo '<p class="error">' . DATABASE_CONNECT_ERROR . " Can't connect to MySQL database '{$info['dbdatabase']}'</p>";
        return;
    }
    $table = $info['dbtable'];
    $afftable = $table . '_affiliates';
    $stats = array();
    // get added date in main table - make sure it is only approved members
    $query = "SELECT `added` FROM `{$table}` WHERE `pending` = 0 " . 'ORDER BY `added` DESC LIMIT 1';
    $result = mysql_query($query);
    if (!$result) {
        log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
        die(STANDARD_ERROR);
    }
    $row = mysql_fetch_array($result);
    $stats['lastupdated'] = $row['added'];
    // get most recent members
    $query = "SELECT * FROM `{$table}` WHERE `added` = '" . $stats['lastupdated'] . '\'';
    $result = mysql_query($query);
    if (!$result) {
        log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
        die(STANDARD_ERROR);
    }
    $new = array();
    while ($row = mysql_fetch_array($result)) {
        $new[] = $row;
    }
    // get added date in affiliates table if affiliates is present
    if ($info['affiliates'] == 1) {
        $query = "SELECT `added` FROM `{$afftable}` ORDER BY `added` " . 'DESC LIMIT 1';
        $result = mysql_query($query);
        if (!$result) {
            log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
            die(STANDARD_ERROR);
        }
        $row = mysql_fetch_array($result);
        if ($row['added'] && $row['added'] > $stats['lastupdated']) {
            $stats['lastupdated'] = $row['added'];
        }
        if ($extended) {
            // do this only if we're looking for "extended" stats
            // now we take the newest affiliates added
            $query = "SELECT * FROM `{$afftable}` WHERE `added` = '" . $row['added'] . "'";
            $result = mysql_query($query);
            if (!$result) {
                log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
                die(STANDARD_ERROR);
            }
            $affrows = array();
            while ($affrow = mysql_fetch_array($result)) {
                $affrows[] = $affrow;
            }
            // prep new affiliates
            require_once 'mod_affiliates.php';
            // require for f'n
            $newaffiliates = '';
            $newaffiliates_img = '';
            $i = 0;
            foreach ($affrows as $a) {
                if ($i == count($affrows) - 1 && count($affrows) != 1) {
                    $newaffiliates .= 'and ';
                }
                $newaffiliates .= '<a href="' . $a['url'];
                if ($info['linktarget']) {
                    $newaffiliates .= '" target="' . $info['linktarget'];
                }
                $newaffiliates .= '">' . $a['title'] . '</a>, ';
                $newaffiliates_img .= parse_affiliates_template($a['affiliateid'], $info['listingid']);
                $i++;
            }
            $stats['newaffiliates'] = rtrim($newaffiliates, ', ');
            $stats['newaffiliatesimg'] = $newaffiliates_img;
            // sigh, reconnect :p
            $db_link_list = mysql_connect($info['dbserver'], $info['dbuser'], $info['dbpassword']);
            if ($db_link_list === false) {
                echo '<p class="error">' . DATABASE_CONNECT_ERROR . " Can't connect to MySQL server on {$info['dbserver']}</p>";
                return;
            }
            $dbselected = mysql_select_db($info['dbdatabase']);
            if (!$dbselected) {
                echo '<p class="error">' . DATABASE_CONNECT_ERROR . " Can't connect to MySQL database '{$info['dbdatabase']}'</p>";
                return;
            }
            // get total affiliates
            $query = "SELECT COUNT(*) AS `count` FROM `{$afftable}`";
            $result = mysql_query($query);
            if (!$result) {
                log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
                die(STANDARD_ERROR);
            }
            $affnum = mysql_fetch_array($result);
            $stats['totalaffiliates'] = $affnum['count'];
            // random affiliate
            $rand = rand(1, $stats['totalaffiliates']) - 1;
            $query = "SELECT * FROM `{$afftable}` LIMIT {$rand}, 1";
            $result = mysql_query($query);
            if (!$result) {
                log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
                die(STANDARD_ERROR);
            }
            $randaff = mysql_fetch_array($result);
            $stats['randomaffiliate'] = '<a href="' . $randaff['url'];
            if ($info['linktarget']) {
                $stats['randomaffiliate'] .= '" target="' . $info['linktarget'];
            }
            $stats['randomaffiliates'] .= '">' . $randaff['title'] . '</a> ';
            $stats['randomaffiliateimg'] = parse_affiliates_template($randaff['affiliateid'], $info['listingid']);
            // sigh, reconnect :p
            $db_link_list = mysql_connect($info['dbserver'], $info['dbuser'], $info['dbpassword']);
            if ($db_link_list === false) {
                echo '<p class="error">' . DATABASE_CONNECT_ERROR . " Can't connect to MySQL server on {$info['dbserver']}</p>";
                return;
            }
            $dbselected = mysql_select_db($info['dbdatabase']);
            if (!$dbselected) {
                echo '<p class="error">' . DATABASE_CONNECT_ERROR . " Can't connect to MySQL database '{$info['dbdatabase']}'</p>";
                return;
            }
        }
    }
    // prepare new members format
    $newmembers = '';
    $i = 0;
    foreach ($new as $n) {
        if ($i == count($new) - 1 && count($new) != 1) {
            $newmembers .= 'and ';
        }
        if ($n['url'] != '' && $n['showurl'] == 1) {
            $newmembers .= '<a href="' . $n['url'];
            if ($info['linktarget']) {
                $newmembers .= '" target="' . $info['linktarget'];
            }
            $newmembers .= '">' . $n['name'] . '</a>, ';
        } else {
            $newmembers .= $n['name'] . ', ';
        }
        $i++;
    }
    $stats['new_members'] = rtrim($newmembers, ', ');
    // get total number of members
    $query = "SELECT COUNT(*) AS `count` FROM `{$table}` WHERE `pending` = 0";
    $result = mysql_query($query);
    if (!$result) {
        log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
        die(STANDARD_ERROR);
    }
    $row = mysql_fetch_array($result);
    $stats['total'] = $row['count'];
    // random member
    $rand = rand(1, $stats['total']) - 1;
    $query = "SELECT * FROM `{$table}` WHERE `pending` = 0 LIMIT {$rand}, 1";
    $result = mysql_query($query);
    if (!$result) {
        log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
        die(STANDARD_ERROR);
    }
    $randmem = mysql_fetch_array($result);
    $stats['randommember'] = $randmem['name'];
    if ($randmem['url'] && $randmem['showurl'] == 1) {
        $stats['randommember'] = '<a href="' . $randmem['url'];
        if ($info['linktarget']) {
            $stats['randommember'] .= '" target="' . $info['linktarget'];
        }
        $stats['randommember'] .= '">' . $stats['randommember'] . '</a>';
    }
    if (isset($randmem['country']) && $randmem['country']) {
        $stats['randommember'] .= 'from ' . $randmem['country'];
    }
    $stats['randommember_url'] = $randmem['url'];
    $stats['randommember_name'] = $randmem['name'];
    $stats['randommember_country'] = isset($randmem['country']) && $randmem['country'] ? $randmem['country'] : '';
    $stats['randommember_email'] = $randmem['email'];
    $afields = explode(',', $info['additional']);
    foreach ($afields as $field) {
        if ($field) {
            $stats['randommember_' . $field] = $randmem[$field];
        }
    }
    // get total number of PENDING members
    $query = "SELECT COUNT(*) AS `count` FROM `{$table}` WHERE `pending` = 1";
    $result = mysql_query($query);
    if (!$result) {
        log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
        die(STANDARD_ERROR);
    }
    $row = mysql_fetch_array($result);
    $stats['pending'] = $row['count'];
    // prepare average number of new fans a day
    $query = "SELECT YEAR( `added` ) AS `year`, MONTH( `added` ) AS " . "`month`, DAYOFMONTH( `added` ) AS `day` FROM `{$table}` WHERE " . "`pending` = 0 AND `added` != '0000-00-00' ORDER BY `added` ASC " . 'LIMIT 1';
    $result = mysql_query($query);
    if (!$result) {
        log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
        die(STANDARD_ERROR);
    }
    $row = mysql_fetch_array($result);
    $firstyear = $row['year'];
    $firstmonth = $row['month'];
    $firstday = $row['day'];
    $today = getdate();
    @($first = getdate(mktime(0, 0, 0, $firstmonth, $firstday, $firstyear)));
    $seconds = $today[0] - $first[0];
    $days = round($seconds / 86400);
    if ($days == 0) {
        $days = 1;
    }
    $stats['average'] = round($stats['total'] / $days, 2);
    // prepare number of countries
    if ($info['country'] == 1) {
        $query = 'SELECT COUNT( DISTINCT( `country` ) ) AS `countries` FROM ' . "`{$table}` WHERE `pending` = 0";
        $result = mysql_query($query);
        if (!$result) {
            log_error(__FILE__ . ':' . __LINE__, 'Error executing query: <i>' . mysql_error() . '</i>; Query is: <code>' . $query . '</code>');
            die(STANDARD_ERROR);
        }
        $row = mysql_fetch_array($result);
        $stats['countries'] = $row['countries'];
    } else {
        $stats['countries'] = '0';
    }
    @mysql_close($db_link_list);
    return $stats;
}
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 For more information please view the readme.txt file.
******************************************************************************/
require 'config.php';
require_once 'mod_errorlogs.php';
require_once 'mod_affiliates.php';
require_once 'mod_owned.php';
require_once 'mod_settings.php';
// make sure fanlisting is set to have affiliates
$info = get_listing_info($listing);
if ($info['affiliates'] == 0) {
    echo '<p>The fanlisting has not been set up to have affiliates.</p>';
    return;
}
// get affiliates and listing info
$affiliates = get_affiliates($listing);
foreach ($affiliates as $aff) {
    echo parse_affiliates_template($aff['affiliateid'], $listing);
}
 http://scripts.indisguise.org/

 Enthusiast is a tool for (fan)listing collective owners to easily
 maintain their listing collectives and listings under that collective.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 For more information please view the readme.txt file.
******************************************************************************/
require 'config.php';
require_once 'mod_errorlogs.php';
require_once 'mod_affiliates.php';
require_once 'mod_settings.php';
// get all affiliates
$affiliates = get_affiliates();
echo get_setting('affiliates_template_header');
foreach ($affiliates as $aff) {
    echo parse_affiliates_template($aff['affiliateid']);
}
echo get_setting('affiliates_template_footer');