Ejemplo n.º 1
0
function punish_comments($hours = 6)
{
    global $globals, $db;
    $log = new Annotation('punish-comment');
    if ($log->read() && $log->time > time() - 3600 * $hours) {
        echo "Comments already verified at: " . get_date_time($log->time) . "\n";
        return false;
    }
    if ($globals['min_karma_for_comments'] > 0) {
        $min_karma = $globals['min_karma_for_comments'];
    } else {
        $min_karma = 4.5;
    }
    $votes_from = time() - $hours * 3600;
    // 'date_sub(now(), interval 6 hour)';
    $comments_from = time() - 2 * $hours * 3600;
    //'date_sub(now(), interval 12 hour)';
    echo "Starting karma_comments...\n";
    $users = "SELECT SQL_NO_CACHE distinct comment_user_id as user_id from comments, users where comment_date > from_unixtime({$comments_from}) and comment_karma < -50 and comment_user_id = user_id and user_level != 'disabled' and user_karma >= {$min_karma}";
    $result = $db->get_results($users);
    $log->store();
    if (!$result) {
        return;
    }
    foreach ($result as $dbuser) {
        $user = new User();
        $user->id = $dbuser->user_id;
        $user->read();
        printf("%07d  %s\n", $user->id, $user->username);
        $punish = 0;
        $comment_votes_count = (int) $db->get_var("SELECT SQL_NO_CACHE count(*) from votes, comments where comment_user_id = {$user->id} and comment_date > from_unixtime({$comments_from}) and vote_type='comments' and vote_link_id = comment_id and  vote_date > from_unixtime({$votes_from}) and vote_user_id != {$user->id}");
        if ($comment_votes_count > 5) {
            $votes_karma = (int) $db->get_var("SELECT SQL_NO_CACHE sum(vote_value) from votes, comments where comment_user_id = {$user->id} and comment_date > from_unixtime({$comments_from}) and vote_type='comments' and vote_link_id = comment_id and vote_date > from_unixtime({$votes_from}) and vote_user_id != {$user->id}");
            if ($votes_karma < 50) {
                $distinct_votes_count = (int) $db->get_var("SELECT SQL_NO_CACHE count(distinct comment_id) from votes, comments where comment_user_id = {$user->id} and comment_date > from_unixtime({$comments_from}) and vote_type='comments' and vote_link_id = comment_id and  vote_date > from_unixtime({$votes_from}) and vote_user_id != {$user->id}");
                $comments_count = (int) $db->get_var("SELECT SQL_NO_CACHE count(*) from comments where comment_user_id = {$user->id} and comment_date > from_unixtime({$comments_from})");
                $comment_coeff = min($comments_count / 10, 1) * min($distinct_votes_count / ($comments_count * 0.75), 1);
                $punish = max(-2, round($votes_karma * $comment_coeff * 1 / 1000, 2));
            }
        }
        if ($punish < -0.1) {
            echo "comments: {$comments_count} votes distinct: {$distinct_votes_count} karma: {$votes_karma} coef: {$comment_coeff} -> {$punish}\n";
            $user->karma += $punish;
            //$user->store();
            $annotation = new Annotation("karma-{$user->id}");
            //$annotation->append(_('Penalización por comentarios').": $punish, nuevo karma: $user->karma\n");
            echo _('Penalización por comentarios') . ": {$punish}, nuevo karma: {$user->karma}\n";
            $log->append(_('Penalización') . " {$user->username}: {$punish}, nuevo karma: {$user->karma}\n");
        }
        $db->barrier();
    }
}
Ejemplo n.º 2
0
 static function insert($type, $ref_id, $user_id = 0, $annotation = false)
 {
     global $db, $globals;
     $ip = $globals['user_ip'];
     $ip_int = $globals['user_ip_int'];
     $sub = SitesMgr::my_id();
     // Get this subsite's parent id (or itself if it's a parent)
     $res = $db->query("insert into logs (log_sub, log_date, log_type, log_ref_id, log_user_id, log_ip_int, log_ip) values ({$sub}, now(), '{$type}', {$ref_id}, {$user_id}, {$ip_int}, '{$ip}')");
     if ($res && $annotation) {
         $a = new Annotation('log-' . $db->insert_id);
         $a->text = $annotation;
         $a->store(time() + 86400 * 30);
         // Valid for one month
     }
     return $res;
 }
Ejemplo n.º 3
0
 static function insert($type, $ref_id, $user_id = 0, $annotation = false)
 {
     global $db, $globals;
     if ($globals['behind_load_balancer'] && $globals['form_user_ip']) {
         // If the page stored the "real IP" in a form
         $ip = $globals['form_user_ip'];
         $ip_int = inet_ptod($globals['form_user_ip']);
     } else {
         $ip = $globals['user_ip'];
         $ip_int = $globals['user_ip_int'];
     }
     $sub = SitesMgr::my_parent();
     // Get this subsite's parent id (or itself if it's a parent)
     $res = $db->query("insert into logs (log_sub, log_date, log_type, log_ref_id, log_user_id, log_ip_int, log_ip) values ({$sub}, now(), '{$type}', {$ref_id}, {$user_id}, {$ip_int}, '{$ip}')");
     if ($res && $annotation) {
         $a = new Annotation('log-' . $db->insert_id);
         $a->text = $annotation;
         $a->store(time() + 86400 * 30);
         // Valid for one month
     }
     return $res;
 }
Ejemplo n.º 4
0
 public static function store_extended_properties($id = false, &$prefs)
 {
     if ($id == false) {
         $id = self::my_id();
     }
     $dict = array();
     $defaults = self::$extended_properties;
     foreach ($prefs as $k => $v) {
         if ($v !== '' && isset($defaults[$k]) && $defaults[$k] != $v) {
             switch ($k) {
                 case 'rules':
                 case 'message':
                     $dict[$k] = clean_text_with_tags($v, 0, false, 300);
                     break;
                 default:
                     $dict[$k] = mb_substr(clean_input_string($v), 0, 100);
             }
         }
     }
     $key = self::PREFERENCES_KEY . $id;
     $a = new Annotation($key);
     if (!empty($dict)) {
         $json = json_encode($dict);
         $a->text = $json;
         return $a->store();
     }
     return $a->delete();
 }
Ejemplo n.º 5
0
}
$tops = explode(',', $a_tops->text);
$a_history = new Annotation('top-link-history-' . $site_name);
if ($a_history->read()) {
    $history = explode(',', $a_history->text);
} else {
    $history = array();
}
if (!in_array($tops[0], $history)) {
    if (!($link = Link::from_db($tops[0]))) {
        echo "Error reading link " . $tops[0] . "\n";
        exit;
    }
    $url = $link->get_permalink($info->sub);
    if ($globals['url_shortener']) {
        $short_url = $link->get_short_permalink();
    } else {
        $short_url = $url;
    }
    $intro = '#' . _('destacada');
    $text = "{$intro} {$link->title}";
    // Save the history
    array_push($history, intval($tops[0]));
    while (count($history) > 10) {
        array_shift($history);
    }
    $a_history->text = implode(',', $history);
    $a_history->store();
    twitter_post($properties, $text, $url);
    facebook_post($properties, $link, $intro);
}
Ejemplo n.º 6
0
	function save_annotation($key) {
		global $globals;

		$key .= "-$this->id";
		$log = new Annotation($key);
		if ($log->read()) $array = unserialize($log->text);
		if (!$array || ! is_array($array)) $array = array();
		$dict = array();
		$dict['time'] = time();
		$dict['positives'] = $this->votes;
		$dict['negatives'] = $this->negatives;
		$dict['anonymous'] = $this->anonymous;
		$dict['old_karma'] = $this->old_karma;
		$dict['karma'] = $this->karma;
		$dict['coef'] = sprintf("%.2f",$this->coef);
		$dict['annotation'] = $this->annotation;
		array_unshift($array, $dict);
		$log->text = serialize($array);
		$log->store();
		$this->annotation = '';
	}
Ejemplo n.º 7
0
 static function store_text($key, $text, $expire = false)
 {
     $annotation = new Annotation($key);
     $annotation->text = $text;
     return $annotation->store($expire);
 }
Ejemplo n.º 8
0
 function all_stats()
 {
     global $db, $globals, $current_user;
     if ($this->stats) {
         return;
     }
     if (!$this->read) {
         $this->read();
     }
     $do_cache = $this->date < $globals['now'] - 86400;
     // Don't cache for new users
     $cache_time = 7200;
     $stats = new Annotation("user_stats-{$this->id}");
     if ($do_cache && $stats->read() && ($stats->time > $globals['now'] - $cache_time || $globals['bot'] || $current_user->user_id == 0 || $stats->time > intval($db->get_var("select unix_timestamp(max(vote_date)) from votes where vote_user_id = {$this->id} and vote_type in ('links', 'posts', 'comments')")))) {
         $obj = unserialize($stats->text);
     } else {
         if ($globals['bot'] && $current_user->user_id == 0) {
             return;
         }
         // Don't calculate stats por bots
         $obj = new stdClass();
         $obj->total_votes = (int) $db->get_var("SELECT count(*) FROM votes WHERE vote_type='links' and vote_user_id = {$this->id}");
         $obj->total_links = (int) $db->get_var("SELECT count(*) FROM links WHERE link_author = {$this->id} and link_votes > 0");
         $obj->published_links = (int) $db->get_var("SELECT count(*) FROM links WHERE link_author = {$this->id} AND link_status = 'published'");
         $obj->total_comments = (int) $db->get_var("SELECT count(*) FROM comments WHERE comment_user_id = {$this->id}");
         $obj->total_posts = (int) $db->get_var("SELECT count(*) FROM posts WHERE post_user_id = {$this->id}");
         $obj->total_friends = (int) $db->get_var("select count(*) from friends where friend_to = {$this->id}");
         $obj->total_images = Upload::user_uploads($this->id) - Upload::user_uploads($this->id, false, 'private');
         if ($do_cache) {
             $stats->text = serialize($obj);
             $stats->store($globals['now'] + 86400 * 90);
             // Expires in 90 days
         }
     }
     foreach (get_object_vars($obj) as $var => $value) {
         $this->{$var} = $value;
     }
     $this->stats = true;
 }
Ejemplo n.º 9
0
	static function calculate_affinity($uid, $min_karma = 200) {
		global $globals, $db;

		$affinity = array();
		$log = new Annotation("affinity-$uid");
		if ($log->read() && $log->time > time() - 3600*4) {
			return unserialize($log->text);
		}

		// Check vote-to-links affinity
		$link_ids = $db->get_col("SELECT SQL_NO_CACHE link_id FROM links WHERE link_date > date_sub(now(), interval 30 day) and link_author = $uid and link_karma > $min_karma");
		$nlinks = count($link_ids);
		if ($nlinks > 4) {
			$links = implode(',', $link_ids);
			$votes = $db->get_results("select SQL_NO_CACHE vote_user_id as id, sum(vote_value/abs(vote_value)) as count from votes where vote_link_id in ($links) and vote_type='links' group by vote_user_id");
			if ($votes) {
				foreach ($votes as $vote) {
					if ($vote->id > 0 && $vote->id != $uid && abs($vote->count) > max(1, $nlinks/10) ) {
						$w = min(1, $nlinks/10);
						$w = max(0.7, $w);
						$c = $vote->count/$nlinks * $w;
						if ($vote->count > 0) {
							$affinity[$vote->id] = round((1 - $c)*100);  // store as int (percent) to save space,
						} else {
							$affinity[$vote->id] = round((-1 - $c)*100);  // store as int (percent) to save space,
						}
					}
				}
			}
		}

		// Check vote-to-comments affinity
		$comment_ids = $db->get_col("SELECT SQL_NO_CACHE comment_id FROM comments WHERE comment_date > date_sub(now(), interval 3 day) and comment_user_id = $uid and comment_votes > 2");
		$ncomments = count($comment_ids);
		if ($ncomments > 4) {
			$comments = implode(',', $comment_ids);
			$votes = $db->get_results("select SQL_NO_CACHE vote_user_id as id, sum(vote_value/abs(vote_value)) as count from votes where vote_link_id in ($comments) and vote_type='comments' group by vote_user_id");
			if ($votes) {
				foreach ($votes as $vote) {
					if ($vote->id > 0 && $vote->id != $uid && abs($vote->count) > max(1, $ncomments/10) ) {
						$w = min(1, $ncomments/15);
						$w = max(0.5, $w);
						$c = $vote->count/$ncomments * $w;
						if ($vote->count > 0) {
							$a = round((1 - $c)*100);
							if (!isset($affinity[$vote->id]) || $a < $affinity[$vote->id]) {
								$affinity[$vote->id] = $a;
							}
						} else {
							$a = round((-1 - $c)*100); 
							if (!isset($affinity[$vote->id]) || ($affinity[$vote->id] < 0 && $a > $affinity[$vote->id]) ) {
								$affinity[$vote->id] = $a;
							}
						}
					}
				}
			}
		}


		if (count($affinity) > 0) {
			$log->text = serialize($affinity);
		} else {
			$affinity = false;
			$log->text = '';
		}
		$log->store(time() + 86400*15); // Expire in 15 days
		return $affinity;
	}
Ejemplo n.º 10
0
            // Recheck for images, some sites add images after the article has been published
            if ($link->thumb_status != 'local' && $link->thumb_status != 'remote' && $link->thumb_status != 'deleted' && !in_array($link->id, $thumbs_queue)) {
                echo "Adding {$link->id} to thumb queue\n";
                array_push($thumbs_queue, $link->id);
            }
        }
    }
    //////////
}
$output .= "</table>\n";
echo $output;
echo "</body></html>\n";
if (!DEBUG) {
    $annotation = new Annotation('promote');
    $annotation->text = $output;
    $annotation->store();
} else {
    echo "OUTPUT:\n{$output}\n";
}
// Get THUMBS
foreach ($thumbs_queue as $id) {
    $link = new Link();
    $link->id = $id;
    $link->read();
    echo "GETTING THUMB {$link->id}\n";
    $link->get_thumb(true);
    echo "DONE GETTING THUMB\n";
}
function print_row($link, $changes, $log = '')
{
    global $globals, $output;
Ejemplo n.º 11
0
            $user->karma = 0.8 * $user->karma + 0.2 * $karma;
        }
        if ($user->karma > $special_karma_gain && $user->level == 'normal') {
            $user->level = 'special';
        } else {
            if ($user->level == 'special' && $user->karma < $special_karma_loss) {
                $user->level = 'normal';
            }
        }
        $output .= sprintf(_('Karma final') . ": %4.2f,  " . 'cálculo actual' . ": %4.2f, " . 'karma anterior' . ": %4.2f\n", $user->karma, $karma, $old_karma);
        $user->karma_log = $output;
        $user->karma_calculated = time();
        $user->store();
        // If we run in the same server as the database master, wait few milliseconds
        if (!$db->dbmaster) {
            usleep(5000);
            // wait 1/200 seconds
        }
    }
    if (!empty($output)) {
        $annotation = new Annotation("karma-{$user->id}");
        $annotation->text = $output;
        $annotation->store(time() + 86400 * 60);
    }
    $db->barrier();
    echo $output;
}
if ($annotation) {
    $annotation->optimize();
}
echo "Calculated: {$calculated}, Ignored: {$no_calculated}\n";
Ejemplo n.º 12
0
function get_subs_coef($site_id, $days = 3)
{
    global $globals, $db, $output;
    if (empty($globals['sub_balance_metas']) || !in_array(SitesMgr::my_id(), $globals['sub_balance_metas'])) {
        return array();
    }
    /// Coeficients to balance metacategories
    $imported = $db->get_col("select src from subs_copy where dst = {$site_id}");
    if (empty($imported)) {
        return array();
    }
    $imported = implode(',', $imported);
    $totals = $db->get_results("select SQL_NO_CACHE origen, status, subs.name as name from sub_statuses, subs where sub_statuses.id = {$site_id} and status in ('queued', 'published') and date > date_sub(now(), interval {$days} day) and origen in ({$imported}) and origen = subs.id");
    $totals_sent = array();
    $totals_published = array();
    $names = array();
    $subs = array();
    $total_published = 0;
    $total_sent = 0;
    foreach ($totals as $sub) {
        $names[$sub->origen] = $sub->name;
        switch ($sub->status) {
            case 'published':
                $total_published++;
                $totals_published[$sub->origen]++;
            default:
                $total_sent++;
                $totals_sent[$sub->origen]++;
        }
        if (!in_array($sub->origen, $subs)) {
            $subs[] = $sub->origen;
        }
    }
    $average = $total_published / $total_sent;
    $subs_coef = array();
    foreach ($subs as $s) {
        $x = $totals_published[$s];
        $y = $totals_sent[$s];
        if ($y == 0) {
            $y = 1;
        }
        $c = $x / $y;
        $subs_coef[$s] = 0.7 * $c + 0.3 * $x / $total_published / count($subs);
        $output .= "{$days} days stats for <b>{$names[$s]}</b> (queued/published/total): {$y}/{$x}/{$total_published} -> {$subs_coef[$s]}<br/>";
    }
    foreach ($subs_coef as $s => $v) {
        $subs_coef[$s] = max(min($average / $v, 1.5), 0.7);
        $output .= "Karma coefficient for <b>{$names[$s]}</b>: {$subs_coef[$s]}<br/>";
    }
    // Store current coef in DB
    $log = new Annotation("subs-coef-{$site_id}");
    $log->text = serialize($subs_coef);
    $log->store();
    echo "DONE META SUBS\n";
    return $subs_coef;
}
Ejemplo n.º 13
0
$cache = new Annotation("sphinx-{$indices}");
if ($cache->read()) {
    /* If totals' cache is valid, just load the array */
    $totals = json_decode($cache->text, true);
} else {
    /* Otherwise, query to Sphinx and fills $totals */
    $totals = array();
    $res = $sp->get_results("select yearmonth(date) as yymm, count(*) as _count from {$indices} group by yymm limit 2000 option ranker = none");
    if ($res) {
        foreach ($res as $o) {
            $a = (array) $o;
            $totals[$o->yymm] = intval($a["_count"]);
        }
    }
    $cache->text = json_encode($totals);
    $cache->store(time() + 86400);
}
$sql = '';
$s = 0;
/* Build de Sphinx SQL query for each word or phrase, each one is a "serie" */
foreach ($q as $words) {
    $words = trim($words);
    $series[$s] = array();
    /* Common attributes for each serie */
    $series[$s]['words'] = $words;
    $series[$s]['objects'] = array();
    $series[$s]['sort'] = $sort;
    $sql .= "select yearmonth(date) as yymm, {$sort}, date, count(*) as _count from {$indices} where match('\"{$words}\"') group by yymm within group order by {$sort} desc limit 1000;";
    $s++;
}
$s = 0;
Ejemplo n.º 14
0
        $id = $_GET['id'];
        if (!ctype_digit($id)) {
            syslog(LOG_INFO, "Missing id {$id}");
            exit;
        }
        $url = "https://api.twitter.com/1/statuses/oembed.json?id={$id}&align=center&maxwidth=550";
        break;
    default:
        die;
}
$key = "json_cache_{$service}-{$id}";
$cache = Annotation::from_db($key);
if ($cache) {
    echo $cache->text;
    exit(0);
}
// Get the url if not cached
$cache = new Annotation($key);
$res = get_url($url);
if (!$res || !$res['content'] || $res['http_code'] != 200) {
    $cache->time = time() + 3600;
    // if it failed, cache for one hour
    $cache->text = '{}';
    // Return empty object
} else {
    $cache->time = time() + 86400 * 7;
    // 7 days in cache
    $cache->text = $res['content'];
}
$cache->store();
echo $cache->text;
Ejemplo n.º 15
0
 public static function store_extended_properties($id = false, &$prefs)
 {
     if ($id == false) {
         $id = self::my_id();
     }
     $dict = array();
     $defaults = array_merge(self::$extended_properties, self::$extra_extended_properties);
     foreach ($prefs as $k => $v) {
         if ($v !== '' && isset($defaults[$k]) && $defaults[$k] != $v) {
             switch ($k) {
                 case 'rules':
                 case 'message':
                     $dict[$k] = clean_text_with_tags($v, 0, false, 3000);
                     break;
                 case 'post_html':
                     // TODO: validate the HTML
                     $dict[$k] = $v;
                     break;
                 default:
                     if (isset($defaults[$k]) && is_int($defaults[$k])) {
                         $dict[$k] = intval($v);
                     } else {
                         $dict[$k] = mb_substr(clean_input_string($v), 0, 140);
                     }
             }
         }
     }
     $key = self::PREFERENCES_KEY . $id;
     $a = new Annotation($key);
     if (!empty($dict)) {
         $json = json_encode($dict);
         $a->text = $json;
         return $a->store();
     }
     return $a->delete();
 }