summaryrefslogtreecommitdiffstats
path: root/library/X509/SqlFilter.php
blob: 40da0d9036a9e2e26275b291874d98b93e9f9091 (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
<?php
// Icinga Web 2 X.509 Module | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\X509;

use ReflectionClass;
use Icinga\Data\ConfigObject;
use Icinga\Data\Db\DbConnection;
use Icinga\Data\Filter\Filter;
use ipl\Sql\Select;

/**
 * @internal
 */
class Quoter
{
    public function quote($identifier, $quoteCharacter = '"')
    {
        if (strlen($quoteCharacter) === 1) {
            return $quoteCharacter . $identifier . $quoteCharacter;
        } else {
            return $quoteCharacter[0] . $identifier . $quoteCharacter[1];
        }
    }
}

/**
 * @internal
 */
class NoImplicitConnectDbConnection extends DbConnection
{
    protected $renderFilterCallback;

    public function __construct(ConfigObject $config = null)
    {
    }

    public function setRenderFilterCallback(callable $callback = null)
    {
        $this->renderFilterCallback = $callback;

        return $this;
    }

    protected function renderFilterExpression(Filter $filter)
    {
        $hit = false;

        if (isset($this->renderFilterCallback)) {
            $hit = call_user_func($this->renderFilterCallback, clone $filter);
        }

        if ($hit !== false) {
            $filter = $hit;
        }

        return parent::renderFilterExpression($filter);
    }
}

/**
 * @internal
 */
class SqlFilter
{
    public static function apply(Select $select, Filter $filter = null, callable $renderFilterCallback = null)
    {
        if ($filter === null || $filter->isEmpty()) {
            return;
        }

        if (! $filter->isEmpty()) {
            $conn = (new NoImplicitConnectDbConnection())->setRenderFilterCallback($renderFilterCallback);

            $reflection = new ReflectionClass('\Icinga\Data\Db\DbConnection');

            $dbAdapter = $reflection->getProperty('dbAdapter');
            $dbAdapter->setAccessible(true);
            $dbAdapter->setValue($conn, new Quoter());

            $select->where($conn->renderFilter($filter));
        }
    }
}