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        'tplpatterns' => FALSE,
29        'checksummary' => TRUE
30    );
31
32    /**
33     *
34     */
35    public function __construct() {
36        $this->config = array();
37
38        $this->loadCookie();
39    }
40
41    /**
42     *
43     */
44    public function update($options) {
45        $this->load($options);
46    }
47
48    /**
49     *
50     */
51    public function getConfig() {
52        return array_merge(self::$defaults, $this->config);
53    }
54
55    /**
56     *
57     */
58    public function getConf($id) {
59        if (array_key_exists($id, $this->config)) {
60            return $this->config[$id];
61        }
62
63        if (array_key_exists($id, self::$defaults)) {
64            return self::$defaults[$id];
65        }
66
67        return '';
68    }
69
70    /**
71     *
72     */
73    public function serialize() {
74        return json_encode($this->config);
75    }
76
77    /**
78     *
79     */
80    private function loadCookie() {
81        if (!array_key_exists(self::COOKIE, $_COOKIE)) {
82            return;
83        }
84
85        $cookie = json_decode($_COOKIE[self::COOKIE], TRUE);
86
87        if (!is_array($cookie)) {
88            return;
89        }
90
91        $this->load($cookie);
92    }
93
94    /**
95     * Sanitize user-provided data
96     */
97    private function load($options) {
98        if (array_key_exists('searchmode', $options)) {
99            $this->config['searchmode'] = $options['searchmode'] == 'regexp' ? 'regexp' : 'text';
100        }
101
102        $this->loadBoolean($options, 'matchcase');
103        $this->loadBoolean($options, 'multiline');
104        $this->loadBoolean($options, 'advregexp');
105        $this->loadBoolean($options, 'matchctx');
106        $this->loadInteger($options, 'ctxchars');
107        $this->loadInteger($options, 'ctxlines');
108
109        if ($this->getConf('ctxchars') == 0) {
110            $this->config['matchctx'] = FALSE;
111        }
112
113        $this->loadBoolean($options, 'searchlimit');
114        $this->loadInteger($options, 'searchmax');
115
116        if ($this->getConf('searchmax') == 0) {
117            $this->config['searchlimit'] = FALSE;
118        }
119
120        $this->loadBoolean($options, 'keepmarks');
121        $this->loadInteger($options, 'markpolicy');
122        $this->loadBoolean($options, 'tplpatterns');
123        $this->loadBoolean($options, 'checksummary');
124        $this->loadInteger($options, 'searchheight');
125        $this->loadInteger($options, 'replaceheight');
126    }
127
128    /**
129     *
130     */
131    private function loadBoolean($options, $id) {
132        if (array_key_exists($id, $options)) {
133            $this->config[$id] = $options[$id] == TRUE;
134        }
135    }
136
137    /**
138     *
139     */
140    private function loadInteger($options, $id) {
141        if (array_key_exists($id, $options) && $options[$id] !== '') {
142            $this->config[$id] = max(intval($options[$id]), 0);
143        }
144    }
145}
146