1<?php
2
3/**
4 * Plugin BatchEdit: Configuration
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Mykola Ostrovskyy <dwpforge@gmail.com>
8 */
9
10class BatcheditConfig {
11
12    const COOKIE = 'BatchEditConfig';
13
14    private $config;
15
16    private static $defaults = array(
17        'searchmode' => 'text',
18        'matchcase' => FALSE,
19        'multiline' => FALSE,
20        'advregexp' => FALSE,
21        'matchctx' => TRUE,
22        'ctxchars' => 50,
23        'ctxlines' => 3,
24        'searchlimit' => TRUE,
25        'searchmax' => 100,
26        'keepmarks' => FALSE,
27        'markpolicy' => 1,
28        'checksummary' => TRUE
29    );
30
31    /**
32     *
33     */
34    public function __construct() {
35        $this->config = array();
36
37        $this->loadCookie();
38    }
39
40    /**
41     *
42     */
43    public function update($options) {
44        $this->load($options);
45    }
46
47    /**
48     *
49     */
50    public function getConfig() {
51        return array_merge(self::$defaults, $this->config);
52    }
53
54    /**
55     *
56     */
57    public function getConf($id) {
58        if (array_key_exists($id, $this->config)) {
59            return $this->config[$id];
60        }
61
62        if (array_key_exists($id, self::$defaults)) {
63            return self::$defaults[$id];
64        }
65
66        return '';
67    }
68
69    /**
70     *
71     */
72    public function serialize() {
73        return json_encode($this->config);
74    }
75
76    /**
77     *
78     */
79    private function loadCookie() {
80        if (!array_key_exists(self::COOKIE, $_COOKIE)) {
81            return;
82        }
83
84        $cookie = json_decode($_COOKIE[self::COOKIE], TRUE);
85
86        if (!is_array($cookie)) {
87            return;
88        }
89
90        $this->load($cookie);
91    }
92
93    /**
94     * Sanitize user-provided data
95     */
96    private function load($options) {
97        if (array_key_exists('searchmode', $options)) {
98            $this->config['searchmode'] = $options['searchmode'] == 'regexp' ? 'regexp' : 'text';
99        }
100
101        $this->loadBoolean($options, 'matchcase');
102        $this->loadBoolean($options, 'multiline');
103        $this->loadBoolean($options, 'advregexp');
104        $this->loadBoolean($options, 'matchctx');
105        $this->loadInteger($options, 'ctxchars');
106        $this->loadInteger($options, 'ctxlines');
107
108        if ($this->getConf('ctxchars') == 0) {
109            $this->config['matchctx'] = FALSE;
110        }
111
112        $this->loadBoolean($options, 'searchlimit');
113        $this->loadInteger($options, 'searchmax');
114
115        if ($this->getConf('searchmax') == 0) {
116            $this->config['searchlimit'] = FALSE;
117        }
118
119        $this->loadBoolean($options, 'keepmarks');
120        $this->loadInteger($options, 'markpolicy');
121        $this->loadBoolean($options, 'checksummary');
122        $this->loadInteger($options, 'searchheight');
123        $this->loadInteger($options, 'replaceheight');
124    }
125
126    /**
127     *
128     */
129    private function loadBoolean($options, $id) {
130        if (array_key_exists($id, $options)) {
131            $this->config[$id] = $options[$id] == TRUE;
132        }
133    }
134
135    /**
136     *
137     */
138    private function loadInteger($options, $id) {
139        if (array_key_exists($id, $options) && $options[$id] !== '') {
140            $this->config[$id] = max(intval($options[$id]), 0);
141        }
142    }
143}
144