<?php
//saving??
class vBArcade_ipa
{
var $imports = array('convert', 'upload');
var $title = 'ibProArcade';//also pnFlash
var $scoring = 2;//all
function output($game)
{
global $vbulletin, $template_hook;
foreach (array('HTTP_HOST', 'SERVER_NAME') AS $varname)
{ //multiple domain variables in multiple locations
foreach (array(@$_SERVER[$varname], @$_ENV[$varname], getenv($varname), @$GLOBALS[$varname]) AS $domain) { if ($domain) break; }
}
$template_hook['dbtech_vbarcade_game_prefix'] = '<script type="text/javascript">refreshScores = function(){};</script>';
$checksum = md5(file_get_contents(DIR . '/dbtech/vbarcade/media/' . $game['gameid'] . '/' . $game['flashfile'])); $flashvar = 'location=./&gamename=' . $game['gameid'] . '&hash=' . $checksum;
$template_hook['dbtech_vbarcade_game_vars'] = '?tgame=0&pn_gid=' . $game['gameid'] . '&pn_license=' . $game['license'] . '&pn_checksum=' . $checksum . '&pn_domain=' . str_replace('www.', '', $domain) . '&pn_uname=' . $vbulletin->userinfo['username'];
$template_hook['dbtech_vbarcade_game_params'] = '<param name="flashvars" value="' . $flashvar . '" />'; $template_hook['dbtech_vbarcade_game_attrs'] = ' flashvars="' . $flashvar . '"';
}
function input($sessionid)
{ //handle game communication
global $vbulletin, $db;
$insecure = false; $end = '';
if ($session = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "dbtech_vbarcade_session WHERE userid = " . $vbulletin->userinfo['userid'] . " AND sessionid = $sessionid"))
{
if ($_REQUEST['module'] == 'pnFlashGames' AND $_REQUEST['func'] == 'storeScore')
{ //pngflash
if (strpos($_REQUEST['score'], ':') !== false)
{ //convert times to seconds and update game
$score = array_map('intval', explode(':', $_REQUEST['score'])); $_REQUEST['score'] = ($score[0] * 3600) + ($score[1] * 60) + $score[2];
$db->query_write("UPDATE " . TABLE_PREFIX . "dbtech_vbarcade_game SET decimals = -1 WHERE gameid = '" . $db->escape_string($session['gameid']) . "'");
}
$insecure = array('score' => TYPE_NUM, 'gid' => TYPE_NOHTML);
$end = '&opSuccess=true&endvar=1';
} //legacy ipa
else if ($_REQUEST['act'] == 'Arcade' AND $_REQUEST['do'] == 'newscore')
{
$insecure = array('gscore' => TYPE_NUM, 'gname' => TYPE_NOHTML);
vbsetcookie('vbarcade_session', false);
}
if ($insecure)
{ //insecure
$input = array_keys($insecure);
$vbulletin->input->clean_array_gpc('r', $insecure);
$score = $vbulletin->GPC[$input[0]];
if ($session['gameid'] == $vbulletin->GPC[$input[1]])
{ //scores approved, finish session
if ($session['status'])
{ //create incremental session
if ($session['chain']) $session = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "dbtech_vbarcade_session WHERE sessionid = " . intval($session['chain']) . " AND gameid = '" . $db->escape_string($session['gameid']) . "' AND userid = " . intval($session['userid']));
if ($session['score'] != $score)
{ //only way to guess if multiple vs duplicate
$matchid = $vbulletin->input->clean_gpc('c', COOKIE_PREFIX . 'vbarcade_match', TYPE_UINT);
$db->query_write("INSERT INTO " . TABLE_PREFIX . "dbtech_vbarcade_session (gameid, userid, ipaddress, timestart, status, timefinish, score, matchid) VALUES ('" . $db->escape_string($session['gameid']) . "', " . intval($session['userid']) . ", '" . $db->escape_string($session['ipaddress']) . "', " . $session['timefinish'] . ", 2, " . TIMENOW . ", $score, $matchid)");
$db->query_write("UPDATE " . TABLE_PREFIX . "dbtech_vbarcade_session SET chain = '" . $db->insert_id() . "' WHERE sessionid = $sessionid");
}
} //update original row only
else $db->query_write("UPDATE " . TABLE_PREFIX . "dbtech_vbarcade_session SET status = 2, timefinish = " . TIMENOW . ", score = $score WHERE sessionid = $sessionid");
}
print_output($end);
}
if ($_REQUEST['autocom'] == 'arcade')
{ //secure
if ($_REQUEST['do'] == 'verifyscore' AND !$session['status'])//ie7?
{ //make sure its a living session
$verify = array(rand(1, 25), rand(1, 25));
$db->query_write("UPDATE " . TABLE_PREFIX . "dbtech_vbarcade_session SET status = 1, verify = '" . implode(',', $verify) . "' WHERE sessionid = " . $session['sessionid']);
print_output('&randchar=' . $verify[0] . '&randchar2=' . $verify[1] . '&savescore=1&blah=OK');
}
if ($_REQUEST['do'] == 'savescore' AND $session['status'] == 1)
{
$vbulletin->input->clean_array_gpc('p', array(
'gscore' => TYPE_NUM,
'arcadegid' => TYPE_NUM,
'enscore' => TYPE_NUM,
'gname' => TYPE_NOHTML
));
if ($session['gameid'] == $vbulletin->GPC['gname'] AND $verify = explode(',', $session['verify']) AND $vbulletin->GPC['enscore'] == ($vbulletin->GPC['gscore'] * $verify[0] ^ $verify[1]))
{ //scores approved, finish session - check time limit?
$db->query_write("UPDATE " . TABLE_PREFIX . "dbtech_vbarcade_session SET status = 2, timefinish = " . TIMENOW . ", score = " . $vbulletin->GPC['gscore'] . ", verify = '' WHERE sessionid = " . $session['sessionid']);
vbsetcookie('vbarcade_session', false);
}
}
print_output('');
}
}
}
function import($which, $perform = 0, $perpage = 20, $tags = array())
{
global $vbulletin, $db;
$cats = array(); foreach ($vbulletin->vbarcade['category'] AS $cid => $cat) $cats[$cid] = $cat['title'];
switch ($which)
{
case 'convert':
{
$good = $db->query_first("SHOW TABLES LIKE '" . TABLE_PREFIX . "games_list'");
if (!$perform)
{ //just see if installed
if ($good)
{ //send current categories
$_cats = $db->query_read("SELECT c_id, cat_name FROM " . TABLE_PREFIX . "games_cats ORDER BY pos ASC, cat_name ASC");
$cats = array(); while ($cat = $db->fetch_array($_cats)) $cats['cat-' . $cat['c_id'] . ':' . $cat['cat_name'] . ';'] = $cat['cat_name'];
$db->free_result($_cats); return $cats;
} //missing setting
else return false;
}
else $start = (($perform - 1) * $perpage);
if ($good)
{
$result = $search = $v32games = array();
$cols = array('cat' => 'gcat', 'sec' => 'game_type');
foreach ($tags AS $column => $value)
{ //prep for query use
if ($value AND array_key_exists($column, $cols)) $search[] = 'g.' . $cols[$column] . ' = ' . intval($value);
}
if ($vbulletin->GPC['initial'] AND $games = arcade_scandir($v32dir = DIR . '/arcade/gamedata', ''))
{ //only fix ibproarcade's secure settings once
foreach ($games AS $game)
{ //should have this file if its secured
if (is_file("$v32dir/$game/$game.txt")) $v32games[] = "'" . $db->escape_string($game) . "'";
}
}
if (sizeof($v32games)) $db->query_write("UPDATE " . TABLE_PREFIX . "games_list SET game_type = 1 WHERE gname IN (" . implode(', ', $v32games) . ")");
if ($vbulletin->GPC['search']) $search[] = "CONCAT(g.gtitle, g.gwords) LIKE '%" . $db->escape_string_like($vbulletin->GPC['search']) . "%'";
$search[] = 'd.gameid IS ' . ( $vbulletin->GPC['inactive'] ? 'NOT ' : '' ) . 'NULL';
$games = $db->query_read("SELECT g.*, c.cat_name FROM " . TABLE_PREFIX . "games_list AS g LEFT JOIN " . TABLE_PREFIX . "games_cats AS c ON (c.c_id = g.gcat) LEFT JOIN " . TABLE_PREFIX . "dbtech_vbarcade_game AS d ON (d.gameid = g.gname) WHERE " . implode(' AND ', $search) . " ORDER BY g.position ASC, g.gtitle ASC LIMIT $start, $perpage");
while ($game = $db->fetch_array($games))
{
$info = array(
'title' => $game['gtitle'],
'description' => nl2br($game['gwords']),
'badge' => 'arcade/images/' . $game['gname'] . '1.gif',
'scoring' => ( ($game['highscore_type'] == 'high') ? 2 : 1 ),
'play' => '../arcade/' . $game['gname'] . '.swf',
'timestamp' => $game['added'],
'tags' => array()
);
if (in_array($game['gname'], $vbulletin->GPC['select']))
{ //complete insert info
$info = array_merge($info, array(
'enabled' => $game['active'],
'displayorder' => $game['position'],
'instruction' => $game['object'],
'controls' => $game['gkeys'],
'width' => $game['gwidth'],
'height' => $game['gheight'],
'decimals' => $game['decpoints'],
'system' => 'ipa', //might be wrong
'license' => $game['license'],
'tourns' => $game['tourney_use'],
'flashfile' => 'arcade/' . $game['gname'] . '.swf',
'icon' => 'arcade/images/' . $game['gname'] . '2.gif'
));
if (is_dir($v32dir = DIR . '/arcade/gamedata/' . $game['gname']))
{ //copy all the junk out of the gamedata folder
foreach (arcade_scandir($v32dir, array('html'), array('v3game.txt', 'v32game.txt'), true) AS $file)
{
$info['copyfile'][$file] = $subdir = "$v32dir/$file";
if (is_dir($subdir))
{ //add required files from subdirectory
foreach (arcade_scandir($subdir, array('html', 'txt'), array(), true) AS $subfile) $info['copyfile']["$file/$subfile"] = "$subdir/$subfile";
}
}
}
if ($tag = trim($game['cat_name']))
{ //try to reuse the category
if (!$catid = array_search($tag, $cats))
{
if (in_array('cats', $vbulletin->GPC['extras']))
{ //add it and get catid
if (file_exists($icon = DIR . '/arcade/cat_imgs/' . ($caticon = $game['gcat'] . '.gif'))) copy($icon, DIR . '/dbtech/vbarcade/images/' . $caticon);
else $caticon = ''; //not using an icon
$cat = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "games_cats WHERE c_id = " . $game['gcat']);
$db->query_write("INSERT INTO " . TABLE_PREFIX . "dbtech_vbarcade_category (enabled, title, displayorder, password, description, icon) VALUES (" . $cat['active'] . ", '" . $db->escape_string($tag) . "', " . $cat['pos'] . ", '" . $db->escape_string($cat['password']) . "', '" . $db->escape_string($cat['description']) . "', '" . $db->escape_string($caticon) . "')");
$vbulletin->vbarcade['category'][$catid = $db->insert_id()]['title'] = $cats[$catid] = $tag;
} //we arent adding missing cats
else $tag = '';
}
if ($tag) $db->query_write("REPLACE INTO " . TABLE_PREFIX . "dbtech_vbarcade_relation (gameid, categoryid) VALUES ('" . $db->escape_string($game['gname']) . "', $catid)");
}
if (in_array('sess', $vbulletin->GPC['extras']))
{ //get whatever scores remain that arent copied already
$scores = $db->query_read("SELECT i.* FROM " . TABLE_PREFIX . "games_scores AS i LEFT JOIN " . TABLE_PREFIX . "games_list AS g ON (g.gid = i.gid) LEFT JOIN " . TABLE_PREFIX . "dbtech_vbarcade_session AS s ON (s.gameid = g.gname AND s.userid = i.mid AND s.timestart = i.datescored) WHERE s.sessionid IS NULL AND g.gname = '" . $db->escape_string($game['gname']) . "'");
while ($score = $db->fetch_array($scores)) $db->query_write("INSERT INTO " . TABLE_PREFIX . "dbtech_vbarcade_session (gameid, userid, ipaddress, timestart, timefinish, status, record, score, comment) VALUES ('" . $db->escape_string($game['gname']) . "', " . $score['mid'] . ", '" . $db->escape_string($score['ip']) . "', " . $score['datescored'] . ", " . ($score['datescored'] + $score['timespent']) . ", 3, 4, " . $score['score'] . ", '" . $db->escape_string($score['comment']) . "')");
$db->free_result($scores);
}
if (in_array('opts', $vbulletin->GPC['extras']) AND !function_exists('save_settings'))
{ //if doing settings and not defined yet, do it once
require_once(DIR . '/includes/adminfunctions_options.php');
$settings = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "games_settings LIMIT 1");
$sort = array('gtitle' => 'a', 'gcount' => 'm', 'added' => 'n', 'position' => 's');
$note = array('none' => 0, 'pm' => 1, 'mail' => 2, 'pm+mail' => 3);
$show = ( $settings['show_active'] ? unserialize($settings['show_active']) : array() );
save_settings(array( //only equivalents
'dbtech_vbarcade_default_sort' => $sort[$settings['g_display_sort']],
'dbtech_vbarcade_perpage' => $settings['games_pp'],
'dbtech_vbarcade_score_perpage' => $settings['scores_amount'],
'dbtech_vbarcade_allscores' => intval($settings['score_type'] == 'all'),
'dbtech_vbarcade_alert_types' => ( $settings['msgsys_hscore'] ? $note[$settings['notification']] : 0 ),
'dbtech_vbarcade_tourn_alert' => ( $settings['msgsys_tstart'] ? $note[$settings['notification']] : 0 ),
'dbtech_vbarcade_showplayers' => ( ($show['glist'] OR $show['play']) ? 4 : 0 ),
'dbtech_vbarcade_bannedips' => $settings['banned_ips'],
'dbtech_vbarcade_timer' => $settings['scoretimeout'],
'dbtech_vbarcade_tourn_limit' => ($settings['tourney_limit2'] * 24),
'dbtech_vbarcade_reminder' => ( $settings['tourney_limit1'] ? ($settings['tourney_limit2'] - $settings['tourney_limit1']) * 24 : 0 )
));
}
}
if ($game['gcat']) $info['tags']['cat-' . $game['gcat']] = $game['cat_name'];
if ($game['game_type']) $info['tags']['sec-1'] = 'Secure';
$result[$game['gname']] = $info;
}
$db->free_result($games);
return $result;
}
} break;
case 'upload':
{
$rawgid = end($path = explode('/', $perpage));
$path = implode('/', $path); //renamed
$ini = $php = false;
if ($good = (file_exists($ini = "$path/$rawgid.ini") OR ($php = $path . '/' . array_pop(arcade_scandir($path, 'php')) AND strpos(file_get_contents($php), '$config') !== false)))
{
if ($php)
{ //parse for config
include($php);
$game = $config;
$gameid = $game['gname'];
}
else
{ //fix, parse, replace the ini file
file_put_contents($ini, preg_replace('/^(\w+)=(.*)$/mU', '$1="$2"', $orig = file_get_contents($ini)));
$game = array_map('trim', parse_ini_file($ini)); file_put_contents($ini, $orig);
$gameid = $rawgid;
}
$author = ( !$php ? $game['author'] : '' );
$active = ( $php ? $game['active'] : $game['status'] );
foreach (array("$path/gamedata/$gameid", "$path/gamedata") AS $v32dir)
{ //figure out possible gamedata folder
if (file_exists($v32game = "$v32dir/$gameid.txt")) break;
else $v32game = '';//remove it
}
$there = $db->query_first("SELECT gameid FROM " . TABLE_PREFIX . "dbtech_vbarcade_game WHERE gameid = '" . $db->escape_string($gameid) . "'");
if (($tags['sec'] AND !$v32game) OR ($tags['dev'] AND $tags['dev'] != $author) OR ($vbulletin->GPC['inactive'] XOR $there) OR ($vbulletin->GPC['search'] AND strpos(strtolower($game['gtitle'] . ' ' . $game['name'] . ' ' . $game['gwords'] . ' ' . $game['description']), strtolower($vbulletin->GPC['search'])) === false)) $good = false;
}
if (!$perform) return $good; //just see if matches
if ($good)
{
$badge = $path . '/' . $gameid . ( $php ? '1' : '' );
$info = array(
'title' => ( $php ? $game['gtitle'] : $game['name'] ),
'description' => nl2br( $php ? $game['gwords'] : $game['description'] ),
'badge' => str_replace(DIR . '/', '', ( file_exists($badge . '.jpg') ? $badge . '.jpg' : $badge . '.gif' )),
'scoring' => ( (($php AND (!array_key_exists('highscore_type', $game) OR $game['highscore_type'] == 'high')) OR $game['gameType'] == 1) ? 2 : 1 ),
'play' => str_replace(DIR, '..', $path) . '/' . $gameid . '.swf',
'timestamp' => 0,//unknown
'tags' => array()
);
if (in_array($gameid, $vbulletin->GPC['select']))
{ //if this is a v32 game copy that into media
$info = array_merge($info, array(
'enabled' => $active,
'author' => $author,
'width' => ( $php ? $game['gwidth'] : $game['width'] ),
'height' => ( $php ? $game['gheight'] : $game['height'] ),
'system' => 'ipa',
'flashfile' => str_replace(DIR . '/', '', $path . '/' . $gameid . '.swf'),
'icon' => ( $php ? str_replace(DIR . '/', '', $path . '/' . $gameid . '2.gif') : $info['badge'] ),
'instruction' => ( $php ? $game['object'] : '' ),
'controls' => ( $php ? $game['gkeys'] : '' )
));
if (is_dir($v32dir))
{ //copy all the junk out of the gamedata folder
foreach (arcade_scandir($v32dir, array('html'), array('v3game.txt', 'v32game.txt'), true) AS $file)
{
$info['copyfile'][$file] = $subdir = "$v32dir/$file";
if (is_dir($subdir))
{ //add required files from subdirectory
foreach (arcade_scandir($subdir, array('html', 'txt'), array(), true) AS $subfile) $info['copyfile']["$file/$subfile"] = "$subdir/$subfile";
}
}
}
}
if ($v32game) $info['tags']['sec-1'] = 'Secure';
if ($author) $info['tags']['dev-' . $author] = $author;
return array($gameid => $info);
}
} break;
}
}
}
?>