Example #1
0
 public function execute()
 {
     if ($_SERVER["REQUEST_METHOD"] == "POST") {
         /* Update menu
          */
         if ($this->model->menu_oke($_POST["menu"]) == false) {
             $this->show_menu_form($_POST["menu"]);
         } else {
             if ($this->model->update_menu($_POST["menu"]) == false) {
                 $this->output->add_tag("result", "Error while updating menu.");
             } else {
                 $this->output->add_tag("result", "The menu has been updated.");
                 $this->user->log_action("menu updated");
                 header("X-Hiawatha-Cache-Remove: all");
                 $cache = new cache($this->db, "menu");
                 $cache->store("last_updated", time(), 365 * DAY);
             }
         }
     } else {
         /* Show menu
          */
         if (($menu = $this->model->get_menu()) === false) {
             $this->output->add_tag("result", "Error loading menu.");
         } else {
             $this->show_menu_form($menu);
         }
     }
 }
Example #2
0
 /**
  * Hent Facebook-cache
  */
 protected function get_facebook_cache()
 {
     // har vi cache?
     $data = cache::fetch("facebook_posts");
     if ($data) {
         return $data;
     }
     // authentiser
     $app_id = KOF_FB_APP_ID;
     $app_secret = KOF_FB_APP_SECRET;
     if (!$app_id || !$app_secret) {
         return null;
     }
     $ret = @file_get_contents("https://graph.facebook.com/oauth/access_token?client_id={$app_id}&client_secret={$app_secret}&grant_type=client_credentials");
     if ($ret === false) {
         // kunne ikke hente data
         putlog("CREWCHAN", "Henting av Facebook-data feilet.");
         cache::store("facebook_posts", array());
     }
     $info = null;
     parse_str($ret, $info);
     // hent JSON
     $json = @file_get_contents("https://graph.facebook.com/kofradia/posts?access_token={$info['access_token']}");
     $data = json_decode($json, true);
     cache::store("facebook_posts", $data);
     return $data;
 }
Example #3
0
 private function show_alert($index)
 {
     if (valid_input($index, VALIDATE_NUMBERS, VALIDATE_NONEMPTY) == false) {
         return;
     } else {
         if ($index >= count($this->alerts)) {
             return;
         }
     }
     list($title, $type, $column) = $this->alerts[(int) $index];
     $cache = new cache($this->db, "dashboard_" . $this->user->username);
     if (($list = $cache->{$column}) === NULL) {
         $function = "get_" . $type . "_statistics";
         $list = $this->model->{$function}($column);
         $cache->store($column, $list, $this->settings->dashboard_page_refresh * 60 - 1);
     }
     if ($list == false) {
         return;
     }
     $this->output->open_tag("list", array("title" => $title));
     foreach ($list as $name => $item) {
         $this->output->add_tag("item", $name, array("count" => $item["today"], "change" => $item["change"]));
     }
     $this->output->close_tag();
 }
Example #4
0
 /**
  * Load settings and save to cache
  */
 public static function reload()
 {
     $result = \Kofradia\DB::get()->query("SELECT id, name, value FROM settings");
     \game::$settings = array();
     while ($row = $result->fetch()) {
         \game::$settings[$row['name']] = array("id" => $row['id'], "value" => $row['value']);
     }
     // keep for 1 hour
     \cache::store("settings", \game::$settings, 3600);
 }
Example #5
0
 /**
  * Get list of ranks
  *
  * @return array(\Kofradia\Game\Rank\Base, ..)
  */
 public static function getRanks()
 {
     if (!static::$by_number) {
         // check cache first, then get from DB
         if ($cache = \cache::fetch("ranks-" . static::$type)) {
             static::$by_number = $cache;
             return $cache;
         }
         static::fetchRanks();
         \cache::store("ranks-" . static::$type, static::$by_number);
     }
     return static::$by_number;
 }
Example #6
0
 /**
  * Hent antall likes
  */
 public static function get_likes_num()
 {
     $d = cache::fetch("facebook_likes");
     if ($d !== false) {
         return $d;
     }
     $ttl = 900;
     // 15 min cache
     // hent data
     $json = @file_get_contents("https://graph.facebook.com/kofradia");
     if (!$json) {
         cache::store("facebook_likes", "ukjent", $ttl);
         return "ukjent";
     }
     $data = json_decode($json, true);
     cache::store("facebook_likes", $data['likes'], $ttl);
     return $data['likes'];
 }
Example #7
0
 /**
  * Hent, evt. generer, cache
  */
 public static function cache_load($reload = false)
 {
     if (!$reload) {
         $data = cache::fetch("hall_of_fame");
         if ($data) {
             self::$data = $data;
             return;
         }
     }
     $data = self::get_data_structure();
     // hent fra databasen
     $result = \Kofradia\DB::get()->query("\n\t\t\tSELECT hof_id, hof_name, hof_sub, hof_time, hof_data\n\t\t\tFROM hall_of_fame");
     while ($row = $result->fetch()) {
         $data[$row['hof_name']][$row['hof_sub'] ?: 0] = array($row['hof_time'], unserialize($row['hof_data']));
     }
     cache::store("hall_of_fame", $data, 900);
     self::$data = $data;
 }
Example #8
0
 /** Hent oppgaver */
 private static function load($skip_cache = false)
 {
     // forsøk å hent fra cache
     if (!$skip_cache && !self::$cache) {
         self::$cache = cache::fetch("tasks");
     }
     // hent fra databasen
     if ($skip_cache || !self::$cache) {
         global $_base;
         $result = \Kofradia\DB::get()->query("SELECT t_name, t_ant, t_last FROM tasks");
         // les data
         self::$cache = array();
         while ($row = $result->fetch()) {
             self::$cache[$row['t_name']] = $row;
         }
         // lagre til cache
         cache::store("tasks", self::$cache);
     }
 }
Example #9
0
 public function __construct($db)
 {
     $this->db = $db;
     /* Handle settings updates
      */
     $cache = new cache($this->db, "settings");
     if ($cache->last_updated === null) {
         $cache->store("last_updated", time(), 365 * DAY);
     }
     if (isset($_SESSION["settings_last_updated"]) == false) {
         $_SESSION["settings_last_updated"] = $cache->last_updated;
     } else {
         if ($cache->last_updated > $_SESSION["settings_last_updated"]) {
             $_SESSION["settings_cache"] = array();
             $_SESSION["settings_last_updated"] = $cache->last_updated;
         }
     }
     unset($cache);
     if (isset($_SESSION["settings_cache"]) == false) {
         $_SESSION["settings_cache"] = array();
     }
     $this->cache =& $_SESSION["settings_cache"];
 }
Example #10
0
File: db.php Project: hazardland/db
 function store($name, $value)
 {
     if (function_exists('\\apcu_store')) {
         if (is_bool($value)) {
             \apcu_delete($name);
         } else {
             \apcu_store($name, $value);
         }
     } else {
         if (function_exists('\\apc_store')) {
             if (is_bool($value)) {
                 \apc_delete($name);
             } else {
                 \apc_store($name, $value);
             }
         } else {
             parent::store($name, $value);
         }
     }
 }
Example #11
0
 public function to_output($current_url = null)
 {
     /* Handle menu updates
      */
     $cache = new cache($this->db, "menu");
     if ($cache->last_updated === null) {
         $cache->store("last_updated", time(), 365 * DAY);
     }
     if (isset($_SESSION["menu_last_updated"]) == false) {
         $_SESSION["menu_last_updated"] = $cache->last_updated;
     } else {
         if ($cache->last_updated > $_SESSION["menu_last_updated"]) {
             $_SESSION["menu_cache"] = array();
             $_SESSION["menu_last_updated"] = $cache->last_updated;
         }
     }
     unset($cache);
     /* Build menu
      */
     if (isset($_SESSION["menu_cache"]) == false) {
         $_SESSION["menu_cache"] = array();
     }
     $cache =& $_SESSION["menu_cache"];
     $username = $this->user !== null ? $this->user->username : "";
     $index = sha1(sprintf("%d-%d-%s-%s", $this->parent_id, $this->depth, $username, $current_url));
     if (isset($cache[$index]) == false) {
         if (($menu = $this->get_menu($this->parent_id, $this->depth, $current_url)) === false) {
             return false;
         }
         $cache[$index] = json_encode($menu);
     } else {
         $menu = json_decode($cache[$index], true);
     }
     $this->show_menu($menu);
     return true;
 }
Example #12
0
 protected function handle_submit()
 {
     parent::handle_submit();
     $cache = new cache($this->db, "settings");
     $cache->store("last_updated", time(), 365 * DAY);
 }
Example #13
0
 protected static function get_revision_info()
 {
     $branch = "";
     // hent informasjon fra Git
     $data = @file_get_contents(PATH_ROOT . "/.git/HEAD");
     if (!$data) {
         return null;
     }
     if (mb_substr($data, 0, 3) == "ref") {
         $ref = trim(mb_substr($data, 5));
         $commit = @file_get_contents(PATH_ROOT . "/.git/{$ref}");
         $branch = basename($ref);
     } else {
         $commit = trim($data);
         $branch = $commit;
     }
     if (!$commit) {
         return null;
     }
     // hent tidspunkt og melding
     $last_rev = cache::fetch("gitlog_last_rev");
     $last_info = cache::fetch("gitlog_last_info");
     if (!$last_rev || $last_rev != $commit) {
         $res = shell_exec("git log -1 --format=\"%ct %s\"");
         $last_info = sscanf($res, "%d %[^\$]s");
         cache::store("gitlog_last_info", $last_info);
     }
     $r = array("branch" => $branch, "commit" => $commit, "date" => $last_info[0], "message" => $last_info[1]);
     return $r;
 }
Example #14
0
<?php

// denne filen henter bydelene og lagrer til cache
$result = \Kofradia\DB::get()->query("SELECT * FROM bydeler ORDER BY name");
game::$bydeler = array();
while ($row = $result->fetch()) {
    game::$bydeler[$row['id']] = $row;
}
// lagre til cache
cache::store("bydeler", game::$bydeler, 0);
Example #15
0
 /**
  * Bygg opp cache over auksjoner for menyen
  */
 public static function update_cache()
 {
     // hent alle aktive og kommende auksjoner
     $result = \Kofradia\DB::get()->query("SELECT a_start, a_end FROM auksjoner WHERE a_end >= " . time() . " AND a_active != 0 AND a_completed = 0");
     $data = array();
     while ($row = $result->fetch(\PDO::FETCH_NUM)) {
         $data[] = $row;
     }
     cache::store("auksjoner_active", $data);
 }
Example #16
0
 /**
  * Hent cache over ikke-fullførte prestasjoner
  */
 public function load_cache($reload = null)
 {
     if ($this->cache && !$reload) {
         return;
     }
     $cache_key = "achievements_up_" . $this->up->id;
     // hent fra cache
     if (!$reload && ($data = cache::fetch($cache_key))) {
         $this->achievements = $data;
     }
     // hent frisk data
     $result = \Kofradia\DB::get()->query("\n\t\t\tSELECT upa_id, upa_ac_id, upa_time, upa_prize, upa_apoints, upa_params, upa_up_id, upa_complete\n\t\t\tFROM up_achievements\n\t\t\tWHERE upa_up_id = {$this->up->id} AND upa_complete = 0");
     $data = array();
     while ($row = $result->fetch()) {
         $data[$row['upa_ac_id']] = $row;
     }
     $this->cache = $data;
     // lagre til cache
     cache::store($cache_key, $data, 300);
 }
Example #17
0
 /**
  * Oppdater cache for menyen
  */
 public static function update_cache()
 {
     cache::store("poker_active", \Kofradia\DB::get()->query("SELECT COUNT(*) FROM poker WHERE poker_state = 2")->fetchColumn(0));
 }
Example #18
0
    /** Sjekk for IP-ban */
    private function check_ip_ban()
    {
        global $_base;
        // sjekk for IP-ban
        if ($_SERVER['REQUEST_METHOD'] == "CRON") {
            return;
        }
        // allerede sjekket og OK?
        if (cache::fetch("ip_ok_" . $_SERVER['REMOTE_ADDR'])) {
            return;
        }
        $ip = \Kofradia\DB::quote(to_float(ip2long($_SERVER['REMOTE_ADDR'])));
        $time = time();
        $result = \Kofradia\DB::get()->query("SELECT bi_ip_start, bi_ip_end, bi_time_start, bi_time_end, bi_reason FROM ban_ip WHERE {$ip} BETWEEN bi_ip_start AND bi_ip_end AND IF(ISNULL(bi_time_end), {$time} >= bi_time_start, {$time} BETWEEN bi_time_start AND bi_time_end) ORDER BY bi_ip_end - bi_ip_start");
        // fant ingen IP-ban oppføring
        if ($result->rowCount() == 0) {
            // sjekk om vi venter en kommende IP-ban
            $result = \Kofradia\DB::get()->query("SELECT bi_time_start FROM ban_ip WHERE {$ip} BETWEEN bi_ip_start AND bi_ip_end AND {$time} <= bi_time_start ORDER BY bi_time_start LIMIT 1");
            if ($result->rowCount() > 0) {
                $next = $result->fetchColumn(0);
                // marker som ok for tiden før IP-ban starter
                cache::store("ip_ok_" . $_SERVER['REMOTE_ADDR'], true, $next - $time);
                return;
            }
            // marker som ok
            cache::store("ip_ok_" . $_SERVER['REMOTE_ADDR'], true);
            return;
        }
        // utestengt via IP
        // mer enn 1 uke vil vise som ubestemt tid
        // sett opp grunner
        $ban_end = 0;
        $reasons = array();
        while ($row = $result->fetch()) {
            if ($ban_end !== false && empty($row['bi_time_end'])) {
                $ban_end = false;
            } elseif ($ban_end !== false && $row['bi_time_end'] > $ban_end) {
                $ban_end = $row['bi_time_end'];
            }
            // sett opp IP-adresse (range?)
            $ip = '<b>' . long2ip($row['bi_ip_start']);
            if ($row['bi_ip_start'] != $row['bi_ip_end']) {
                // range
                $ip .= ' - ' . long2ip($row['bi_ip_end']);
            }
            $ip .= '</b>';
            // grunn oppgitt?
            if (empty($row['bi_reason'])) {
                // nei
                $reason = 'Grunn ikke oppgitt.';
            } else {
                // ja
                $reason = game::bb_to_html($row['bi_reason']);
            }
            #$reasons[] = '<p>'.$ip.': '.$reason.'</p>';
            $reasons[] = '<fieldset><legend>' . $ip . '</legend><p>' . $reason . '</p></fieldset>';
        }
        // "jukse" til ubestemt tid?
        #if ($ban_end !== false && $ban_end > time()+604800) $ban_end = false;
        #$timeinfo = $ban_end === false ? '<p>Din IP-adresse er utestengt på ubestemt tid.</p>' : '<p>Din IP-adresse er utestengt til <b>'.$_base->date->get($ban_end)->format(date::FORMAT_SEC).'</b>.</p>';
        putlog("ABUSE", "%c8%bIP-Blokk:%b%c %u{$_SERVER['REMOTE_ADDR']}%u - {$_SERVER['HTTP_USER_AGENT']}");
        // send feilmelding etc
        header("HTTP/1.0 403 Forbidden");
        echo '<!DOCTYPE html>
<html lang="no">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="Henrik Steen; http://www.henrist.net" />
<title>IP-blokkering</title>
<style type="text/css">
<!--
body { font-family: tahoma; font-size: 14px; }
h1 { font-size: 23px; }
.hsws { color: #CCCCCC; font-size: 12px; }
.subtitle { font-size: 16px; font-weight: bold; }
fieldset { margin: 10px 0 }
fieldset p { margin: 8px 3px 5px; padding: 0 }
legend { color: #FFFFFF; background-color: #222222; padding: 3px 5px; border: 3px solid #FFFFFF; border-width: 0 3px }
-->
</style>
</head>
<body>
<h1>IP-blokkering</h1>
<p>Din IP-adresse er blokkert/utestengt.</p>
<p>IP-adresse og eventuelle grunner:</p>
' . implode("\n", $reasons) . '
<p class="hsws"><a href="http://hsw.no/">hsw.no</a></p>
</body>
</html>';
        die;
    }
Example #19
0
$result = \Kofradia\DB::get()->query("SELECT id, name, points, rank_max_health, rank_max_energy FROM ranks ORDER BY points");
// sett opp data
$i = 0;
$last_id = 0;
while ($row = $result->fetch()) {
    // oppdater need_points til den forrige raden
    if ($last_id) {
        game::$ranks['items'][$last_id]['need_points'] = $row['points'] - game::$ranks['items'][$last_id]['points'];
        game::$ranks['items_number'][$i]['need_points'] = $row['points'] - game::$ranks['items'][$last_id]['points'];
    }
    $row['number'] = ++$i;
    game::$ranks['items'][$row['id']] = $row;
    game::$ranks['items_number'][$i] = game::$ranks['items'][$row['id']];
    $last_id = $row['id'];
}
// oppdater need_points til den siste raden
if ($last_id) {
    game::$ranks['items'][$last_id]['need_points'] = 0;
    game::$ranks['items_number'][$i]['need_points'] = 0;
}
// hent rankene for posisjonene
$result = \Kofradia\DB::get()->query("SELECT pos, name FROM ranks_pos ORDER BY pos");
$i = 0;
while ($row = $result->fetch()) {
    $row['number'] = ++$i;
    game::$ranks['pos'][$row['pos']] = $row;
    game::$ranks['pos_max'] = max(game::$ranks['pos_max'], $row['pos']);
}
// lagre til cache
cache::store("ranks", game::$ranks);