<commit_before><?php
/* SuccessWhale, by <NAME>
 * A lightweight, standards-compliant Twitter client written in PHP
 * and JavaScript.
 * Based on Abraham's Twitter OAuth PHP example: http://twitter.abrah.am/
 * See http://onlydreaming.net/software/successwhale for details.

 * 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
 * (at your option) 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/>.*/

//////// START OF CODE ////////

date_default_timezone_set('UTC');

require_once('common.php');
session_start();

// DB connection
mysql_connect(DB_SERVER,DB_USER,DB_PASS);
@mysql_select_db(DB_NAME) or die( "Unable to select database");

// Check for conditions that are POSTed to index.php
checkAndSetTheme();
$colsperscreen = checkAndSetColsPerScreen();

// Check cookies, log in the user if the cookies exist and the secret matches,
// but the user is not logged in already.
if (!isset($_SESSION['sw_uid']) && isset($_COOKIE['sw_uid']) && isset($_COOKIE['secret'])) {
    $query = "SELECT secret FROM sw_users WHERE sw_uid='" . mysql_real_escape_string($_COOKIE['sw_uid']) . "';";
    $result = mysql_query($query) or die (mysql_error());
    $row = mysql_fetch_assoc($result);
    if ($_COOKIE['secret'] == $row['secret']) {
        logInUser($_COOKIE['sw_uid']);
    }
}
// If the user isn't logged in by this point, they don't have a session or a
// cookie, so kick out to the login page.
if (!isset($_SESSION['sw_uid'])) {
    header('Location: ./clearsessions.php');
}

// Grab appropriate auth tokens from the database, and build objects with them.
if (TWITTER_ENABLED) {
    $query = "SELECT access_token FROM twitter_users WHERE sw_uid='" . mysql_real_escape_string($_SESSION['sw_uid']) . "';";
    $result = mysql_query($query) or die (mysql_error());
    if (mysql_num_rows($result) > 0) {
        $row = mysql_fetch_assoc($result);
        $twitter_access_token = unserialize($row['access_token']);
        $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $twitter_access_token['oauth_token'], $twitter_access_token['oauth_token_secret']);
        $_SESSION['twitter'] = $twitter;
    }
}
if (FACEBOOK_ENABLED) {
    $query = "SELECT access_token FROM facebook_users WHERE sw_uid='" . mysql_real_escape_string($_SESSION['sw_uid']) . "';";
    $result = mysql_query($query) or die (mysql_error());
    $row = mysql_fetch_row($result);
    if (mysql_num_rows($result) > 0) {
        $facebook = new Facebook(array(
          'appId' => FACEBOOK_APP_ID,
          'secret' => FACEBOOK_SECRET,
          'cookie' => true,
        ));
        try {
         $attachment =  array('access_token' => $row[0],);
         $ret_code=$facebook->api('/me', 'GET', $attachment);
         $_SESSION['facebook'] = $facebook;
        }
         catch (Exception $e) {
           # We don't have a good session so
           die("FB connect failed.");
          $res = mysql_query('DELETE FROM facebook_users WHERE expires=0');
          return;
        }
    }
}

// Session-global the stuff that doesn't depend on which column we're rendering
$auth = $twitter->get('account/verify_credentials', array());
$_SESSION['thisUser'] = $auth['screen_name'];
$_SESSION['utcOffset'] = $auth['utc_offset'];

// Base column options, e.g. home timeline, mentions<commit_msg>Automatically fetch a new FB session if the known one has expired.<commit_after><?php
/* SuccessWhale, by <NAME>
 * A lightweight, standards-compliant Twitter client written in PHP
 * and JavaScript.
 * Based on Abraham's Twitter OAuth PHP example: http://twitter.abrah.am/
 * See http://onlydreaming.net/software/successwhale for details.

 * 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
 * (at your option) 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/>.*/

//////// START OF CODE ////////

date_default_timezone_set('UTC');

require_once('common.php');
session_start();

// DB connection
mysql_connect(DB_SERVER,DB_USER,DB_PASS);
@mysql_select_db(DB_NAME) or die( "Unable to select database");

// Check for conditions that are POSTed to index.php
checkAndSetTheme();
$colsperscreen = checkAndSetColsPerScreen();

// Check cookies, log in the user if the cookies exist and the secret matches,
// but the user is not logged in already.
if (!isset($_SESSION['sw_uid']) && isset($_COOKIE['sw_uid']) && isset($_COOKIE['secret'])) {
    $query = "SELECT secret FROM sw_users WHERE sw_uid='" . mysql_real_escape_string($_COOKIE['sw_uid']) . "';";
    $result = mysql_query($query) or die (mysql_error());
    $row = mysql_fetch_assoc($result);
    if ($_COOKIE['secret'] == $row['secret']) {
        logInUser($_COOKIE['sw_uid']);
    }
}
// If the user isn't logged in by this point, they don't have a session or a
// cookie, so kick out to the login page.
if (!isset($_SESSION['sw_uid'])) {
    header('Location: ./clearsessions.php');
}

// Grab appropriate auth tokens from the database, and build objects with them.
if (TWITTER_ENABLED) {
    $query = "SELECT access_token FROM twitter_users WHERE sw_uid='" . mysql_real_escape_string($_SESSION['sw_uid']) . "';";
    $result = mysql_query($query) or die (mysql_error());
    if (mysql_num_rows($result) > 0) {
        $row = mysql_fetch_assoc($result);
        $twitter_access_token = unserialize($row['access_token']);
        $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $twitter_access_token['oauth_token'], $twitter_access_token['oauth_token_secret']);
        $_SESSION['twitter'] = $twitter;
    }
}
if (FACEBOOK_ENABLED) {
    $query = "SELECT access_token FROM facebook_users WHERE sw_uid='" . mysql_real_escape_string($_SESSION['sw_uid']) . "';";
    $result = mysql_query($query) or die (mysql_error());
    $row = mysql_fetch_row($result);
    if (mysql_num_rows($result) > 0) {
        $facebook = new Facebook(array(
          'appId' => FACEBOOK_APP_ID,
          'secret' => FACEBOOK_SECRET,
          'cookie' => true,
        ));
        try {
         $attachment =  array('access_token' => $row[0],);
         $ret_code=$facebook->api('/me', 'GET', $attachment);
         $_SESSION['facebook'] = $facebook;
        }
         catch (Exception $e) {
          $res = mysql_query('DELETE FROM facebook_users WHERE expires=0');
          // We don't have a good session, so let's get one!
          header('Location: ./facebook-callback/');
        }
    }
}

// Session-global the stuff that doesn't depend on which column we're rendering
$auth = $twitter->get('account/verify_credentials', array());
$_SESSION['thisUser'] = $auth['screen_name'];
$_SESSION['utcOffset'] = $auth['utc_offset'];

// Base column options, e.g. home timeline, mentions
{'fraction_non_alphanumeric': 0.13652412710867007, 'fraction_numerical': 0.0013077023669412842, 'mean_word_length': 3.801004394224733, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1106802', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2262, 'n_words': 711}