1<?php
2/**
3 * Copyright 2013-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file LICENSE for license information (ASL).  If you
6 * did not receive this file, see http://www.horde.org/licenses/apache.
7 *
8 * @author   Jan Schneider <jan@horde.org>
9 * @category Horde
10 * @license  http://www.horde.org/licenses/apache ASL
11 * @package  Ingo
12 */
13
14/**
15 * Ingo_Transport_Sql implements an Ingo transport driver using a SQL database.
16 *
17 * @author   Jan Schneider <jan@horde.org>
18 * @category Horde
19 * @license  http://www.horde.org/licenses/apache ASL
20 * @package  Ingo
21 */
22class Ingo_Transport_Sql extends Ingo_Transport_Base
23{
24    /**
25     * Database handle.
26     *
27     * @var Horde_Db_Adapter
28     */
29    protected $_db;
30
31    /**
32     * Constructor.
33     *
34     * @param array $params  A hash containing driver parameters.
35     */
36    public function __construct(array $params = array())
37    {
38        $this->_supportShares = true;
39        parent::__construct($params);
40    }
41
42    /**
43     * Sets a script running on the backend.
44     *
45     * @param array $script  The filter script information. Passed elements:
46     *                       - 'name': (string) the script name.
47     *                       - 'recipes': (array) the filter recipe objects.
48     *                       - 'script': (string) the filter script.
49     *
50     * @throws Ingo_Exception
51     */
52    public function setScriptActive($script)
53    {
54        $this->_connect();
55
56        try {
57            foreach ($script['recipes'] as $recipe) {
58                $this->_db->execute($recipe['object']->generate());
59            }
60        } catch (Horde_Db_Exception $e) {
61            throw new Ingo_Exception($e);
62        }
63    }
64
65    /**
66     * Quotes user input if supported by the transport driver.
67     *
68     * @param string $string  A string to quote.
69     *
70     * @return string  The quoted string.
71     */
72    public function quote($string)
73    {
74        $this->_connect();
75        return $this->_db->quote($string);
76    }
77
78    /**
79     * Connect to the SQL server.
80     *
81     * @throws Ingo_Exception
82     */
83    protected function _connect()
84    {
85        if ($this->_db) {
86            return;
87        }
88
89        try {
90            $this->_db = $GLOBALS['injector']
91                ->getInstance('Horde_Core_Factory_Db')
92                ->create('ingo', $this->_params);
93        } catch (Horde_Exception $e) {
94            throw new Ingo_Exception($e);
95        }
96    }
97}
98