summaryrefslogtreecommitdiffstats
path: root/config/config.php
blob: d9fe1569fd758cd97a1ada17e39c28e966254ad3 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Alessandro Cosentino <cosenal@gmail.com>
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Alessandro Cosentino 2012
 * @copyright Bernhard Posselt 2012, 2014
 */

namespace OCA\News\Config;

use OCP\ILogger;
use OCP\Files\Folder;


class Config {

    private $fileSystem;
    private $autoPurgeMinimumInterval;  // seconds, used to define how
                                        // long deleted folders and feeds
                                        // should still be kept for an
                                        // undo actions
    private $autoPurgeCount;  // number of allowed unread articles per feed
    private $maxRedirects;  // seconds
    private $feedFetcherTimeout;  // seconds
    private $useCronUpdates;  // turn off updates run by owncloud cronjob
    private $logger;
    private $loggerParams;
    private $maxSize;
    private $exploreUrl;
    private $proxyHost;
    private $proxyPort;
    private $proxyUser;
    private $proxyPassword;


    public function __construct(Folder $fileSystem,
                                ILogger $logger,
                                $LoggerParameters) {
        $this->fileSystem = $fileSystem;
        $this->autoPurgeMinimumInterval = 60;
        $this->autoPurgeCount = 200;
        $this->maxRedirects = 10;
        $this->maxSize = 100*1024*1024; // 100Mb
        $this->feedFetcherTimeout = 60;
        $this->useCronUpdates = true;
        $this->logger = $logger;
        $this->proxyHost = '';
        $this->proxyPort = 8080;
        $this->proxyUser = '';
        $this->proxyPassword = '';
        $this->exploreUrl = '';
        $this->loggerParams = $LoggerParameters;
    }

    public function getProxyPort() {
        return $this->proxyPort;
    }

    public function getProxyHost() {
        return $this->proxyHost;
    }

    public function getProxyAuth() {
        if($this->proxyUser === '') {
            return null;
        } else {
            return $this->proxyUser . ':' . $this->proxyPassword;
        }
    }

    public function getProxyUser() {
        return $this->proxyUser;
    }

    public function getProxyPassword() {
        return $this->proxyPassword;
    }

    public function getAutoPurgeMinimumInterval() {
        if ($this->autoPurgeMinimumInterval > 60) {
            return $this->autoPurgeMinimumInterval;
        } else {
            return 60;
        }
    }


    public function getAutoPurgeCount() {
        return $this->autoPurgeCount;
    }


    public function getMaxRedirects() {
        return $this->maxRedirects;
    }


    public function getFeedFetcherTimeout() {
        return $this->feedFetcherTimeout;
    }


    public function getUseCronUpdates() {
        return $this->useCronUpdates;
    }


    public function getMaxSize() {
        return $this->maxSize;
    }


    public function getExploreUrl() {
        return $this->exploreUrl;
    }


    public function setAutoPurgeMinimumInterval($value) {
        $this->autoPurgeMinimumInterval = $value;
    }


    public function setAutoPurgeCount($value) {
        $this->autoPurgeCount = $value;
    }


    public function setMaxRedirects($value) {
        $this->maxRedirects = $value;
    }


    public function setFeedFetcherTimeout($value) {
        $this->feedFetcherTimeout = $value;
    }


    public function setUseCronUpdates($value) {
        $this->useCronUpdates = $value;
    }


    public function setProxyPort($value) {
        $this->proxyPort = $value;
    }


    public function setProxyHost($value) {
        $this->proxyHost = $value;
    }


    public function setProxyUser($value) {
        $this->proxyUser = $value;
    }


    public function setProxyPassword($value) {
        $this->proxyPassword = $value;
    }


    public function setMaxSize($value) {
        $this->maxSize = $value;
    }


    public function setExploreUrl($value) {
        $this->exploreUrl = $value;
    }


    public function read($configPath, $createIfNotExists=false) {
        if($createIfNotExists && !$this->fileSystem->nodeExists($configPath)) {
            $this->fileSystem->newFile($configPath);
            $this->write($configPath);

        } else {

            $content = $this->fileSystem->get($configPath)->getContent();
            $configValues = parse_ini_string($content);

            if($configValues === false || count($configValues) === 0) {
                $this->logger->warning(
                    'Configuration invalid. Ignoring values.',
                    $this->loggerParams
                );
            } else {

                foreach($configValues as $key => $value) {
                    if(property_exists($this, $key)) {
                        $type = gettype($this->$key);
                        settype($value, $type);
                        $this->$key = $value;
                    } else {
                        $this->logger->warning(
                            'Configuration value "' . $key .
                            '" does not exist. Ignored value.' ,
                            $this->loggerParams
                        );
                    }
                }

            }
        }
    }


    public function write($configPath) {
        $ini =
            'autoPurgeMinimumInterval = ' .
                $this->autoPurgeMinimumInterval . "\n" .
            'autoPurgeCount = ' .
                $this->autoPurgeCount . "\n" .
            'maxRedirects = ' .
                $this->maxRedirects . "\n" .
            'maxSize = ' .
                $this->maxSize . "\n" .
            'exploreUrl = ' .
                $this->exploreUrl . "\n" .
            'feedFetcherTimeout = ' .
                $this->feedFetcherTimeout . "\n" .
            'useCronUpdates = ' .
                var_export($this->useCronUpdates, true);
        ;

        $this->fileSystem->get($configPath)->putContent($ini);
    }


}