summaryrefslogtreecommitdiff
path: root/models/config.php
blob: d854047dcaf5474b3d8564a2c846cb3c6eea3c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

namespace Model;

use \SimpleValidator\Validator;
use \SimpleValidator\Validators;

class Config extends Base
{
    const TABLE = 'config';

    public function getLanguages()
    {
        return array(
            'en_US' => t('English'),
            'fr_FR' => t('French'),
        );
    }

    public function get($name, $default_value = '')
    {
        if (! isset($_SESSION['config'][$name])) {
            $_SESSION['config'] = $this->getAll();
        }

        if (isset($_SESSION['config'][$name])) {
            return $_SESSION['config'][$name];
        }

        return $default_value;
    }

    public function getAll()
    {
        return $this->db->table(self::TABLE)->findOne();
    }

    public function save(array $values)
    {
        $_SESSION['config'] = $values;
        return $this->db->table(self::TABLE)->update($values);
    }

    public function reload()
    {
        $_SESSION['config'] = $this->getAll();

        $language = $this->get('language', 'en_US');
        if ($language !== 'en_US') \Translator\load($language);
    }

    public function validateModification(array $values)
    {
        $v = new Validator($values, array(
            new Validators\Required('language', t('The language is required')),
        ));

        return array(
            $v->execute(),
            $v->getErrors()
        );
    }

    public static function generateToken()
    {
        if (ini_get('open_basedir') === '') {
            return substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15);
        }
        else {
            return substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
        }
    }

    public function optimizeDatabase()
    {
        $this->db->getconnection()->exec("VACUUM");
    }

    public function downloadDatabase()
    {
        return gzencode(file_get_contents(self::DB_FILENAME));
    }

    public function getDatabaseSize()
    {
        return filesize(self::DB_FILENAME);
    }
}