<?php

include_once 'common.inc.php';
func_register(array('user' => array('callback' => 'deal_user'), 'profile' => array('callback' => 'profile_show')));
function theme_user($data)
{
    include_once "theme.inc.php";
    $content = '';
    foreach ($data as $r) {
        if (strstr($r['source'], '<')) {
            $source = str_replace("<a ", '<a class="left microblog-item-position"', $r['source']);
        } else {
            $source = '<a class="left microblog-item-position">' . $r['source'] . '</a>';
        }
        $content .= '<div class="item" id="' . $r['tweet_id'] . '">
                        <div class="item-delete">
                            <a class="right"></a>
                        </div>
                        <div class="left item-pic">
                            <img alt="" width="50" height="50" src="' . $r['profile_image_url'] . '">
                        </div>
                        <div class="left item-content">
                            <div class="item-blog">
                                <a class="item-blog-name" target="_blank" href="' . BASE_URL . 'profile/' . $r['post_screenname'] . '">' . $r['post_screenname'] . '</a>:' . $r['content'] . '
                            </div>
                            <div class="item-other">
                                <span class="left item-time">' . time_tran($r['post_datetime']) . '</span>' . $source . '
                                <a class="right item-control last delete">
                                    删除</a>
                            </div>
                        </div>
<?php

include_once 'common.inc.php';
func_register(array('hot' => array('callback' => 'deal_hot')));
function get_tags($id)
{
    include_once "common.inc.php";
    connect_db();
    $view = "SELECT tags.name from tags, (SELECT * FROM tag_relationship WHERE tweet_id='{$id}') as r WHERE r.tag_id=tags.tag_id ORDER BY tags.count DESC";
    $list = mysql_query($view);
    $result = array();
    $i = 0;
    while ($row = mysql_fetch_array($list)) {
        $result[$i++] = $row[0];
        if ($i == 7) {
            break;
        }
    }
    if ($i == 0) {
        return false;
    }
    return $result;
}
function get_hot($num)
{
    include_once "common.inc.php";
    connect_db();
    $view = "SELECT tags.name,tags.count,tags.tag_group,tg.tag_group_name from tags,(SELECT * FROM tag_group) AS tg WHERE tg.tag_group = tags.tag_group ORDER BY tags.count DESC";
    $list = mysql_query($view);
    $result = array();
    $i = 0;
<?php

include_once 'common.inc.php';
func_register(array('follow' => array('callback' => 'deal_following', 'security' => 'true')));
function get_followings($num)
{
    include_once 'login.inc.php';
    $id = get_current_user_id();
    connect_db();
    $view = "SELECT * FROM followings WHERE user_id='{$id}' AND deleted=0 ORDER BY add_time DESC";
    $list = mysql_query($view);
    $result = array();
    $i = 0;
    while ($row = mysql_fetch_array($list)) {
        $result[$i++] = $row;
        if ($i == $num) {
            break;
        }
    }
    return $result;
}
function following_show()
{
    $args = func_get_args();
    $key = intval($args[2]);
    $content = '';
    $follows = get_followings($key);
    foreach ($follows as $f) {
        $content .= '<div class="concern-item concern-item-normal" onmouseover="ConcernMouseOver(this)"
                    onmouseout="ConcernMouseOut(this)">
                    <a class="concern-item-delete left" onclick="ConcernDeleteClick(\'' . $f['following_id'] . '\',\'' . $f['search'] . '\',this)"></a>
<?php

include_once 'common.inc.php';
func_register(array('search' => array('callback' => 'deal_search'), 'history' => array('callback' => 'deal_search_history', 'security' => 'true')));
function theme_search($key, $content)
{
    /*if($title=='首页')
      {
          $GLOBALS['search'] = "淘宝";
          include_once("common.inc.php");
          $counts = get_counts();
          $pre = '<div id="radio">
                  本周新增职位'.$counts['tweets_thisweek'].'个,今日新增职位'.$counts['tweets_today'].'个</div>
                  <div id="microblogs">';
      }
      else
      {
          $GLOBALS['search'] = $key;
          $pre = '<div id="search-result-outer">
                      <div id="search-result">
                          <div class="left">
                              #<a class="keyword">'.$key.'</a>#的搜索结果</div>';
          #include_once("follow.inc.php");
          $pre .= '<a id="search-result-concern" class="left"></a>';
          $pre .= '<a id="search-result-rss" class="right">
                          </a>
                      </div>
                  </div>';
      }
      $content = $pre.$content;*/
    theme('page', $key, $content);
<?php

include_once 'common.inc.php';
func_register(array('like' => array('callback' => 'deal_like', 'security' => 'true')));
function like_count()
{
    include_once 'login.inc.php';
    $row = get_likes(0, 0, true);
    echo $row[0][0];
}
function get_likes($num, $page, $count = false)
{
    include_once 'login.inc.php';
    $id = get_current_user_id();
    if ($count) {
        $select = "COUNT(tweets.tweet_id)";
        $limit = "";
    } else {
        $select = "tweets.*";
        if (!$page) {
            $page = "0";
        }
        $page = intval($page) * $num;
        $limit = " LIMIT {$page} , {$num}";
    }
    connect_db();
    $view = "SELECT {$select} from tweets, (SELECT * FROM favorites WHERE user_id='{$id}' AND deleted=0) as favorites WHERE tweets.deleted=0 AND tweets.tweet_id=favorites.tweet_id ORDER BY tweets.post_datetime DESC{$limit}";
    $list = mysql_query($view);
    $result = array();
    $i = 0;
    while ($row = mysql_fetch_array($list)) {
<?php

include_once 'common.inc.php';
func_register(array('tweet' => array('callback' => 'deal_tweet', 'security' => 'true')));
function tweet_post()
{
    $args = func_get_args();
    $cate = $args[2];
    $content = format_str($_POST['text'], false);
    if (!$cate or !$content) {
        die("Invalid argument!");
    }
    include_once 'sinaoauth.inc.php';
    $c = new WeiboClient(SINA_AKEY, SINA_SKEY, $GLOBALS['user']['sinakey']['oauth_token'], $GLOBALS['user']['sinakey']['oauth_token_secret']);
    if ($_FILES['upload']['tmp_name']) {
        $msg = $c->upload($content, $_FILES['upload']['tmp_name']);
    } else {
        $msg = $c->update($content);
    }
    if ($msg === false || $msg === null) {
        echo "Error occured";
        return false;
    }
    if (isset($msg['error_code']) && isset($msg['error'])) {
        echo 'Error_code: ' . $msg['error_code'] . ';  Error: ' . $msg['error'];
        return false;
    }
    include_once "uuid.inc.php";
    $v4uuid = str_replace("-", "", UUID::v4());
    connect_db();
    $add = "INSERT INTO pending_tweets (\r\n                     site_id, tweet_id, user_site_id, content, post_datetime,\r\n                     type, tweet_site_id,\r\n                     post_screenname, profile_image_url, source)\r\n                     VALUES (1, '{$v4uuid}', '" . $msg['user']['id'] . "', '{$content}', '" . date("Y-m-d H:i:s", strtotime($msg["created_at"])) . "',\r\n                     {$cate}, '" . $msg['id'] . "', '" . $msg["user"]["name"] . "', '" . $msg["user"]["profile_image_url"] . "', '" . $msg["source"] . "')";
include_once 'common.inc.php';
include_once 'theme.inc.php';
include_once 'login.inc.php';
include_once 'search.inc.php';
include_once 'follow.inc.php';
include_once 'like.inc.php';
include_once 'hot.inc.php';
include_once 'user.inc.php';
include_once 'tweet.inc.php';
include_once 'resume.inc.php';
include_once 'apply_sent.inc.php';
include_once 'apply_received.inc.php';
include_once 'avatar.inc.php';
include_once 'feedback.inc.php';
include_once 'count.inc.php';
func_register(array('' => array('callback' => 'home_page'), 'home' => array('callback' => 'home_page'), 'manager' => array('callback' => 'manager_page', 'security' => 'true'), 'feedback' => array('callback' => 'feedback_page'), 'help' => array('callback' => 'help_page'), 'admin' => array('callback' => 'admin_page', 'admin' => 'true')));
function home_page()
{
    theme('template', 'default.html');
    #header("Location: ".BASE_URL."default.html");
    #$content = theme('result', get_newest_result(10));
    #theme('search', '首页', $content);
    #echo "502: Service Unavailable";
}
function manager_page()
{
    theme('template', 'manager.html');
}
function feedback_page($query)
{
    $key = (string) $query[1];
<?php

include_once 'common.inc.php';
func_register(array('count' => array('callback' => 'count_show')));
function count_show()
{
    $counts = get_counts();
    $r = $counts['tweets_thisweek'] . ',' . $counts['tweets_today'];
    echo $r;
}
<?php

include_once 'common.inc.php';
func_register(array('apply_sent' => array('callback' => 'deal_apply_sent', 'security' => 'true')));
function get_sent_applies($num, $page, $count = false)
{
    include_once 'login.inc.php';
    $id = get_current_user_id();
    if ($count) {
        $select = "COUNT(*)";
        $limit = "";
    } else {
        if (!$page) {
            $page = "0";
        }
        $select = "*";
        $page = intval($page) * $num;
        $limit = " LIMIT {$page} , {$num}";
    }
    connect_db();
    $view = "SELECT {$select} from tweets,(SELECT * FROM applications WHERE user_id='{$id}' AND deleted=0 GROUP BY tweet_id) AS ap WHERE ap.tweet_id = tweets.tweet_id AND tweets.deleted=0 ORDER BY tweets.post_datetime DESC{$limit}";
    $list = mysql_query($view);
    $result = array();
    $i = 0;
    while ($row = mysql_fetch_array($list)) {
        $result[$i++] = $row;
    }
    return $result;
}
function apply_sent_add()
{
<?php

include_once 'common.inc.php';
func_register(array('cate' => array('callback' => 'theme_cat')));
$current_theme = false;
function theme()
{
    global $current_theme;
    $args = func_get_args();
    $function = array_shift($args);
    $function = 'theme_' . $function;
    if ($current_theme) {
        $custom_function = $current_theme . '_' . $function;
        if (function_exists($custom_function)) {
            $function = $custom_function;
        }
    } elseif (!function_exists($function)) {
        return "<p>Error: theme function <b>{$function}</b> not found.</p>";
    }
    return call_user_func_array($function, $args);
}
function theme_template($template)
{
    $page = file_get_contents($template);
    #FIXME: Should use flexible theme functions here
    echo $page;
}
function theme_external_link($url, $content = null)
{
    if (!$content) {
        return "<a class='item-blog-link' href='" . long_url($url) . "' target='_blank'>" . $url . "</a>";
<?php

include_once 'common.inc.php';
func_register(array('avatar' => array('callback' => 'deal_avatar', 'security' => 'true')));
function resizeavatar($url, $size)
{
    if ($size == "big") {
        $url = str_replace("/50/", "/180/", $url);
    }
    return $url;
}
function get_avatar($id, $size)
{
    connect_db();
    $view = "SELECT avatar_url FROM userinfo WHERE user_id='{$id}'";
    $list = mysql_query($view);
    $row = mysql_fetch_array($list);
    if ($row) {
        if ($row[0]) {
            return resizeavatar($row[0], $size);
        }
    }
    $me = sina_get_credentials();
    $avatar = $me['profile_image_url'];
    $view = "UPDATE userinfo SET avatar_url='" . $avatar . "' WHERE user_id='{$id}'";
    $list = mysql_query($view);
    return resizeavatar($avatar, $size);
}
function avatar_show()
{
    include_once "login.inc.php";
<?php

include_once 'common.inc.php';
func_register(array('feedback' => array('callback' => 'deal_feedback')));
function feedback_post($query)
{
    include_once 'login.inc.php';
    if (user_is_authenticated()) {
        $id1 = ', user_id';
        $id2 = ', \'' . get_current_user_id() . '\'';
    } else {
        $id1 = $id2 = "";
    }
    $question = get_post('question');
    $description = get_post('description');
    $email = get_post('email');
    include_once "uuid.inc.php";
    $v4uuid = str_replace("-", "", UUID::v4());
    connect_db();
    $add = "INSERT INTO feedback (\r\n\t\t\t\t\t feedback_id, question, description, post_datetime, email{$id1})\r\n\t\t\t\t     VALUES ('{$v4uuid}', '{$question}', '{$description}', '" . date('Y-m-d H:i:s') . "', '{$email}'{$id2})";
    $added = mysql_query($add) or die("Could not add entry!");
    echo "0";
}
function get_feedbacks($num, $page, $count = false)
{
    if ($count) {
        $limit = "";
    } else {
        if (!$page) {
            $page = "0";
        }
<?php

include_once 'common.inc.php';
func_register(array('apply_received' => array('callback' => 'deal_apply_received', 'security' => 'true')));
function get_received_tweets($num, $page, $count = false)
{
    include_once 'login.inc.php';
    $id = get_current_user_id();
    if ($count) {
        $limit = "";
        $select = "COUNT(DISTINCT tweets.tweet_id)";
        $groupby = "";
    } else {
        if (!$page) {
            $page = "0";
        }
        $select = "*";
        $page = intval($page) * $num;
        $limit = " LIMIT {$page} , {$num}";
        $groupby = " GROUP BY tweets.tweet_id";
    }
    connect_db();
    $view = "SELECT {$select} from tweets, (SELECT * FROM applications WHERE deleted=0) AS ap, (SELECT * from accountbindings WHERE user_id = '{$id}') AS ab WHERE tweets.deleted=0 AND tweets.tweet_id=ap.tweet_id AND tweets.user_site_id = ab.user_site_id AND tweets.site_id = ab.site_id{$groupby} ORDER BY tweets.post_datetime DESC{$limit}";
    $list = mysql_query($view);
    $result = array();
    $i = 0;
    while ($row = mysql_fetch_array($list)) {
        $result[$i++] = $row;
    }
    return $result;
}
<?php

include_once 'common.inc.php';
func_register(array('login' => array('callback' => 'user_login'), 'relogin' => array('callback' => 'user_relogin'), 'sina_login' => array('callback' => 'oauth_sina'), 'sina_callback' => array('callback' => 'oauth_sina_callback'), 'logout' => array('callback' => 'user_logout', 'security' => 'true')));
function user_is_authenticated()
{
    if (!isset($GLOBALS['user'])) {
        if (array_key_exists('USER_AUTH', $_COOKIE)) {
            _user_decrypt_cookie($_COOKIE['USER_AUTH']);
            return true;
        } else {
            $GLOBALS['user'] = array();
            return false;
        }
    } elseif (!isset($GLOBALS['user']['id'])) {
        return false;
    } else {
        return true;
    }
}
function get_current_user_id()
{
    return $GLOBALS['user']['id'];
}
function get_current_user_nickname()
{
    return $GLOBALS['user']['nickname'];
}
function get_current_user_role()
{
    return $GLOBALS['user']['role'];