Exemple #1
0
function connect()
{
    $dsn = \str_replace('$ROOTDIR', \dirname(__DIR__), \bmtmgr\config\get('db_dsn'));
    $db = new \PDO($dsn);
    $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
    // Do we need to initialize?
    if (\bmtmgr\config\get('allow_init', false)) {
        if (\bmtmgr\config\get('test_force_init', false)) {
            _init($db);
            return $db;
        }
        $init_sql = \file_get_contents(dirname(__DIR__) . '/db_init.sql');
        if (!\preg_match('/INSERT INTO db_version.*VALUES\\s*\\(([0-9]+)\\)/', $init_sql, $matches)) {
            throw new \Exception('Cannot detect version number');
        }
        $newest_version = \intval($matches[1]);
        try {
            $vdata = $db->query('SELECT version FROM db_version');
        } catch (\PDOException $e) {
            _init($db);
            return $db;
        }
        $version = -1;
        foreach ($vdata as $row) {
            $version = $row['version'];
        }
        if ($version < $newest_version) {
            _init($db);
        }
    }
    return $db;
}
Exemple #2
0
function create_session($u)
{
    // Create and set up a session
    $s = $GLOBALS['db']->prepare('INSERT INTO login_cookie_token
		(token, user_id, request_time, expiry_time)
		VALUES(?, ?, ?, ?);');
    $token = \bmtmgr\utils\gen_token();
    $request_time = time();
    $session_length = \bmtmgr\config\get('session_token_timeout', 10 * 360 * 24 * 60 * 60);
    $expiry_time = $request_time + $session_length;
    $s->execute(array($token, $u->id, $request_time, $expiry_time));
    setcookie('login_token', $token, $expiry_time, '/', false, \bmtmgr\config\get('force_https', false), true);
}
Exemple #3
0
function send($to, $template, $data)
{
    $fulltext = \bmtmgr\get_rendered($template, $data);
    $firstline_end = strpos($fulltext, "\n");
    $subject = substr($fulltext, 0, $firstline_end);
    $body_html = trim(substr($fulltext, $firstline_end));
    $from = \bmtmgr\config\get('mail_from');
    $from_name = 'Badminton-Turnier-Manager';
    if (\bmtmgr\config\get('mail_debug', false)) {
        return array('from' => $from_name . ' <' . $from . '>', 'to' => $to, 'subject' => $subject, 'body_html' => $body_html);
    } else {
        $headers = array('From' => $from_name . ' <' . $from . '>', 'To' => $to, 'Subject' => $subject, 'Content-Type' => 'text/html; charset="utf-8"', 'Content-Transfer-Encoding' => 'quoted-printable');
        echo "TODO: Call mail();!!!";
        // mail();
        return null;
    }
}
Exemple #4
0
function get_rendered($template_id, &$data)
{
    $mustache = _get_engine();
    $data['csrf_field'] = '<input type="hidden" name="csrf_token" value="' . \htmlspecialchars(utils\csrf_token()) . '" />';
    $data['support_email_address'] = \bmtmgr\config\get('support_email_address');
    $data['root_path'] = \bmtmgr\utils\root_path();
    $data['icon_path'] = \bmtmgr\utils\root_path() . 'static/icons/';
    if (\array_key_exists('user', $data) && $data['user']) {
        $data['is_admin'] = $data['user']->can('admin');
    }
    if (\array_key_exists('disciplines', $data)) {
        $data['disciplines_present'] = true;
    }
    if (array_key_exists('disciplines', $data) && array_key_exists('discipline', $data)) {
        foreach ($data['disciplines'] as $d) {
            if ($d->id == $data['discipline']->id) {
                $d->_is_new = 'edited';
                $d->is_active = true;
            }
        }
    }
    return $mustache->render($template_id, $data);
}
Exemple #5
0
 public function testTestConfig()
 {
     $this->assertEquals(\bmtmgr\config\get('test_force_init', '(unset)'), true);
 }
Exemple #6
0
function absolute_url()
{
    $res = \bmtmgr\config\get('absolute_url_prefix', null);
    if ($res) {
        return $res;
    }
    $domain = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
    $port = $_SERVER['SERVER_PORT'];
    $https = \array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] == 'on';
    if ($https && $port != 443 || !$https && $port != 80) {
        $domain .= ':' . $port;
    }
    $root_path = root_path();
    if (!endswith($root_path, '/')) {
        $root_path .= '/';
    }
    return $domain . $root_path;
}
Exemple #7
0
<?php

namespace bmtmgr;

// All modules can assume access the following modules and the timezone being set correctly.
require_once \dirname(__DIR__) . '/src/utils.php';
require_once \dirname(__DIR__) . '/src/model.php';
require_once \dirname(__DIR__) . '/src/config.php';
\bmtmgr\config\Config::load(__DIR__ . '/testconfig.json');
require_once \dirname(__DIR__) . '/src/db.php';
require_once \dirname(__DIR__) . '/src/user.php';
// These should be autoloaded later
require_once \dirname(__DIR__) . '/models/discipline.php';
require_once \dirname(__DIR__) . '/models/entry.php';
require_once \dirname(__DIR__) . '/models/player.php';
require_once \dirname(__DIR__) . '/models/season.php';
require_once \dirname(__DIR__) . '/models/tournament.php';
require_once \dirname(__DIR__) . '/models/user.php';
\date_default_timezone_set(\bmtmgr\config\get('timezone'));
Exemple #8
0
<?php

namespace bmtmgr\install;

require_once __DIR__ . '/utils.php';
require_once __DIR__ . '/config.php';
\bmtmgr\config\Config::load();
define('LIB_ROOT', dirname(__DIR__) . '/libs/');
function get_libs()
{
    return [['url' => 'http://code.jquery.com/jquery-1.11.2.min.js', 'symlink' => 'jquery.min.js'], ['url' => 'http://cdn.craig.is/js/mousetrap/mousetrap.min.js'], ['url' => 'http://jqueryui.com/resources/download/jquery-ui-1.11.4.zip', 'symlink' => 'jquery-ui'], ['url' => 'https://github.com/bobthecow/mustache.php.git'], ['url' => 'https://github.com/mk-j/PHP_XLSXWriter.git']];
}
if (\bmtmgr\config\get('allow_install', false)) {
    if (isset($argv)) {
        if (count($argv) == 1 || $argv[1] == 'install') {
            install_libs();
        } elseif ($argv[1] == 'clean') {
            clean();
        } else {
            die('Usage: ' . $argv[0] . ' install|clean' . "\n");
        }
    } else {
        install_libs();
    }
} elseif (isset($argv)) {
    echo 'Configuration option allow_install unset!' . "\n";
}
function rm_rf($fn)
{
    if (!\bmtmgr\utils\startswith($fn, LIB_ROOT)) {
        throw new \Exception('Invalid rm of ' . $fn);