Exemple #1
0
/**
 * Allows creation of topics, stuck or closed, and posts
 * @global array
 * @global array
 * @param string $topic post subject
 * @param string $content post content
 * @param integer $reply id of topic we are replying to
 * @param boolean $sticky are we sticking it to the top?
 * @param boolean $closed are we closing it?
 * @return string|int
 */
function post($topic, $content, $reply = false, $sticky = false, $closed = false)
{
    global $config, $user_data;
    // The time. milliseconds / seconds may change.
    $time = time();
    // Its new right now.
    $new = true;
    // Pre-Parse
    $topic = clean_input(strip_repeat($topic));
    $content = htmlentities($content);
    $content = field_clean(stripslashes($content), true);
    if ($_SESSION['logged_in']) {
        if (!$reply) {
            if ($topic == "") {
                return lang_parse('error_no_given', array(lang('subject')));
            }
        } else {
            if ($topic == "") {
                $topic = "re:";
            }
        }
        if (!alpha($topic, 'alpha-extra')) {
            return lang_parse('error_invalid_chars', array(lang('subject')));
        }
        if (is_string(length($content, $config['message_minimum_length'], $config['message_max_length']))) {
            return lang_parse('error_subject_length', array($config['subject_max_length'], $config['subject_minimum_length']));
        }
        if ($content != "") {
            if (!is_string(length($content, $config['message_minimum_length'], $config['message_max_length']))) {
                // Are we replying or is it new?
                if ($reply) {
                    if (is_numeric($reply)) {
                        if (topic($reply, 'id')) {
                            $new = false;
                            // topic data
                            $topic_data = topic($reply, '*');
                            // is it closed?
                            if ($topic_data['closed'] && !$user_data['admin']) {
                                return lang('error_topic_closed');
                            }
                        } else {
                            return lang('error_topic_missing');
                        }
                    } else {
                        return lang_parse('error_invalid_given', array(lang('topic') . " " . lang('id')));
                    }
                }
                // Sticky
                $sticky = $sticky ? '1' : '0';
                // Closed
                $closed = $closed ? '1' : '0';
                // Parsing
                $content = htmlentities($content);
                // Time Lapse
                if (!$user_data['admin']) {
                    if (!$new) {
                        $time_between = time() - $config['post_reply_time_limit'];
                    } else {
                        $time_between = time() - $config['post_topic_time_limit'];
                    }
                    // Last post by this user?
                    $query = "SELECT `time` FROM `forum` WHERE `starter_id` = '{$user_data['id']}' AND `time` > {$time_between}";
                    // Fetch users last post
                    $result = mysql_query($query);
                    // is there a result?
                    if (mysql_num_rows($result) > 0) {
                        return lang('error_flood_detection');
                    }
                }
                // So we don't have leftovers.
                unset($query, $result);
                // Guess we can go ahead and add you~
                $query = "INSERT INTO `forum` (`subject`,`message`,`reply`,`starter_id`,`host`,`time`,`updated`,`sticky`,`closed`) VALUES ('%s','%s',%d,%d,'%s','%s','%s','%s','%s')";
                $query = sprintf($query, mysql_clean($topic), mysql_clean($content), $new ? 0 : $reply, $user_data['id'], mysql_clean(gethostname()), $time, $time, $sticky, $closed);
                // Insert into mysql and retrieve id.
                $result = mysql_query($query);
                echo mysql_error();
                if ($result) {
                    // the id from the previous query
                    $id = mysql_insert_id();
                    // users new post count
                    $new_post_count = $user_data['posts'] + 1;
                    // update user post count
                    update_user($user_data['id'], false, 'posts', $new_post_count);
                    // Start sending back information
                    if ($new) {
                        return $id;
                    } else {
                        // How many replies?
                        $replies = intval(get_replies($reply));
                        // Lets update it
                        $replies = $replies + 1;
                        // Woooo~ Last id for redirecting~
                        $page_numbers = $replies / 20 - 1;
                        $n = ceil($page_numbers);
                        if ($n == -1) {
                            $n = 0;
                        } else {
                            $n = abs($n);
                        }
                        // Update
                        $query = "UPDATE `forum` SET `updated`='{$time}', `replies`='{$replies}' WHERE id = '{$reply}'";
                        // Update
                        $result = mysql_query($query);
                        // Return last page number for redirect!
                        return $n;
                    }
                } else {
                    return lang('error_unknown');
                }
            } else {
                return lang_parse('error_message_length', array($config['message_max_length'], $config['message_minimum_length']));
            }
        } else {
            return lang_parse('error_no_given', array(lang('message')));
        }
    } else {
        return lang('error_not_logged');
    }
}
Exemple #2
0
?>
</td>
					<td colspan="2" class="item key"><?php 
echo lang('actions');
?>
</td>
				</tr>
<?php 
if (is_array($posts)) {
    foreach ($posts as $row) {
        // reset
        $status = "";
        // Trim subject
        $content = substru(trim(stripslashes($row['message'])), 0, 25) . "&#8230;";
        // How many replies?
        $replies = intval(get_replies($row['reply']));
        // Lets update it
        $replies = $replies + 1;
        // Woooo~ Last id for redirecting~
        $page_numbers = $replies / 20 - 1;
        $n = ceil($page_numbers);
        if ($n == -1) {
            $n = 0;
        } else {
            $n = abs($n);
        }
        // Build topic url
        $post_url = "{$config['url_path']}/read.php?id={$row['reply']}&page={$n}";
        // Topic starter data
        $post_author = user_data($row['starter_id']);
        ?>
Exemple #3
0
function generate($what, $filename)
{
    global $tsv, $network, $esc, $titles, $database, $interval, $outputformat;
    require_once __DIR__ . '/CSV.class.php';
    // initialize variables
    $tweets = $times = $from_user_names = $results = $urls = $urls_expanded = $hosts = $hashtags = array();
    $csv = new CSV($filename, $outputformat);
    $collation = current_collation();
    // determine interval
    $sql = "SELECT MIN(t.created_at) AS min, MAX(t.created_at) AS max FROM " . $esc['mysql']['dataset'] . "_tweets t ";
    $sql .= sqlSubset();
    //print $sql . "<bR>";
    $rec = mysql_query($sql);
    $res = mysql_fetch_assoc($rec);
    // get frequencies
    if ($what == "hashtag") {
        $results = frequencyTable("hashtags", "text");
    } elseif ($what == "urls") {
        $results = frequencyTable("urls", "url_followed");
    } elseif ($what == "hosts") {
        $results = frequencyTable("urls", "domain");
    } elseif ($what == "mention") {
        $results = frequencyTable("mentions", "to_user");
        // get other things
    } else {
        // @todo, this could also use database grouping
        $sql = "SELECT id,text COLLATE {$collation} as text,created_at,from_user_name COLLATE {$collation} as from_user_name FROM " . $esc['mysql']['dataset'] . "_tweets t ";
        $sql .= sqlSubset();
        // get slice and its min and max time
        $rec = mysql_query($sql);
        if ($rec && mysql_num_rows($rec) > 0) {
            while ($res = mysql_fetch_assoc($rec)) {
                $tweets[] = $res['text'];
                $ids[] = $res['id'];
                $times[] = $res['created_at'];
                $from_user_names[] = strtolower($res['from_user_name']);
            }
        }
        // extract desired things ($what) and group per interval
        foreach ($tweets as $key => $tweet) {
            $time = $times[$key];
            switch ($interval) {
                case "hourly":
                    $group = strftime("%Y-%m-%d %Hh", strtotime($time));
                    break;
                case "weekly":
                    $group = strftime("%Y %u", strtotime($time));
                    break;
                case "monthly":
                    $group = strftime("%Y-%m", strtotime($time));
                    break;
                case "yearly":
                    $group = strftime("%Y-%m", strtotime($time));
                    break;
                case "overall":
                    $group = "overall";
                    break;
                case "custom":
                    $group = groupByInterval(strftime("%Y-%m-%d", strtotime($time)));
                    break;
                default:
                    $group = strftime("%Y-%m-%d", strtotime($time));
                    // default daily
            }
            switch ($what) {
                //case "hashtag":
                //    foreach ($hashtags as $hashtag)
                //        $results[$group][] = $hashtag;
                //    break;
                //case "mention": // @todo, mentions might be taken from own table
                //    $stuff = get_replies($tweet);
                //    foreach ($stuff as $thing)
                //        $results[$group][] = $thing;
                //    break;
                case "user":
                    $results[$group][] = $from_user_names[$key];
                    break;
                case "user-mention":
                    $stuff = get_replies($tweet);
                    foreach ($stuff as $thing) {
                        $results[$group]['mentions'][] = $thing;
                    }
                    $results[$group]['users'][] = $from_user_names[$key];
                    //var_dump($results);
                    break;
                case "retweet":
                    $results[$group][] = $tweet;
                    // TODO, write stemming function
                    break;
                    //case "urls":
                    //    if (isset($urls_expanded[$ids[$key]]))
                    //        $results[$group][] = $urls_expanded[$ids[$key]];
                    //    break;
                    //case "hosts":
                    //    if (isset($urls_expanded[$ids[$key]]))
                    //        $results[$group][] = $hosts[$ids[$key]];
                    //    break;
                //case "urls":
                //    if (isset($urls_expanded[$ids[$key]]))
                //        $results[$group][] = $urls_expanded[$ids[$key]];
                //    break;
                //case "hosts":
                //    if (isset($urls_expanded[$ids[$key]]))
                //        $results[$group][] = $hosts[$ids[$key]];
                //    break;
                default:
                    break;
            }
        }
        // count frequency of occurence of thing, per interval
        if ($what != "user-mention") {
            foreach ($results as $group => $things) {
                $counted_things = array_count_values($things);
                arsort($counted_things);
                $results[$group] = $counted_things;
            }
        }
    }
    // network output for users
    if ($what == "user-mention") {
        foreach ($results as $group => $things) {
            $tmp_mentions = array_count_values($things['mentions']);
            $tmp_users = array_count_values($things['users']);
            $counted_things = array();
            // add all from_user_names
            foreach ($tmp_users as $user => $count) {
                if (isset($tmp_mentions["@" . $user])) {
                    $counted_things[$user] = $tmp_mentions["@" . $user] . "," . $count;
                } else {
                    $counted_things[$user] = "0," . $count;
                }
            }
            // add all users which were replied but not in the set
            foreach ($tmp_mentions as $user => $count) {
                $user = str_replace("@", "", $user);
                if (!isset($counted_things[$user])) {
                    $counted_things[$user] = $count . ",0";
                }
            }
            ksort($counted_things);
            $results[$group] = $counted_things;
        }
        if (isset($titles[$what])) {
            if (!empty($esc['shell']['query'])) {
                $q = " with search " . $esc['shell']['query'];
            } else {
                $q = "";
            }
            $csv->writeheader(array($titles[$what] . $q . " from " . $esc['date']["startdate"] . " to " . $esc['date']["enddate"]));
        }
        $csv->writeheader(array("date", "user", "mentions", "tweets"));
        foreach ($results as $group => $things) {
            foreach ($things as $thing => $count) {
                $csv->newrow();
                $csv->addfield($group);
                $csv->addfield($thing);
                $exp = explode(",", $count);
                // unpack what we packed
                $csv->addfield($exp[0]);
                $csv->addfield($exp[1]);
                $csv->writerow();
            }
        }
        // write tsv output
    } elseif (in_array($what, $tsv) !== false) {
        ksort($results);
        // construct file
        if (isset($titles[$what])) {
            if (!empty($esc['shell']['query'])) {
                $q = " with search " . $esc['shell']['query'];
            } else {
                $q = "";
            }
            $csv->writeheader(array($titles[$what] . " for " . $esc['shell']['datasetname'] . $q . " from " . $esc['date']["startdate"] . " to " . $esc['date']["enddate"]));
        }
        if ($what == "urls") {
            $csv->writeheader(array("date", "frequency", "tweetedurl"));
        } elseif ($what == "hosts") {
            $csv->writeheader(array("date", "frequency", "domain", "name"));
        } else {
            $csv->writeheader(array("date", "frequency", $what));
        }
        foreach ($results as $group => $things) {
            arsort($things);
            foreach ($things as $thing => $count) {
                if (empty($thing)) {
                    continue;
                }
                if ($count < $esc['shell']['minf']) {
                    continue;
                }
                $csv->newrow();
                $csv->addfield($group);
                $csv->addfield($count);
                $csv->addfield($thing);
                $csv->writerow();
            }
        }
    } else {
        die('no valid output format found');
    }
    $csv->close();
}
Exemple #4
0
<?php

include 'get_connection.php';
try {
    if (PHP_SAPI === 'cli') {
        $post = $argv[1];
    } else {
        $post = $_GET['post'];
    }
    get_replies($conn, $post);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
    return;
}
$conn = null;
function get_replies($conn, $post)
{
    $query = "SELECT users.username, posts.title, posts.content, posts.ts \r\n\t\t\t\tFROM posts \r\n\t\t\t\tJOIN users \r\n\t\t\t\tON posts.author = users.id WHERE posts.id = :post";
    $post_results = $conn->prepare($query);
    $post_results->bindparam(':post', $post);
    $post_results->execute();
    $post_results = $post_results->fetchAll();
    $query = "SELECT users.username, replies.content, replies.ts \r\n\t\t\t\tFROM replies \r\n\t\t\t\tJOIN posts \r\n\t\t\t\tON replies.post_id = posts.id \r\n\t\t\t\tJOIN users \r\n\t\t\t\tON replies.author = users.id \r\n\t\t\t\tWHERE posts.id = :post \r\n\t\t\t\tORDER BY replies.ts DESC ";
    $results = $conn->prepare($query);
    $results->bindparam(':post', $post);
    $results->execute();
    $results = $results->fetchAll();
    $final_results = array_merge($post_results, $results);
    echo json_encode($final_results);
}