summaryrefslogtreecommitdiffstats
path: root/appinfo/application.php
blob: 6b7ba43791c09b9a3d954d091a30e8d2328da086 (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
<?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\AppInfo;

use HTMLPurifier;
use HTMLPurifier_Config;

use PicoFeed\Config\Config as PicoFeedConfig;
use PicoFeed\Reader\Reader as PicoFeedReader;

use OCP\ILogger;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\AppFramework\App;
use OCP\Files\IRootFolder;

use OCA\News\Config\AppConfig;
use OCA\News\Config\Config;
use OCA\News\Service\FeedService;
use OCA\News\Db\MapperFactory;
use OCA\News\Db\ItemMapper;
use OCA\News\Fetcher\Fetcher;
use OCA\News\Fetcher\FeedFetcher;
use OCA\News\Fetcher\YoutubeFetcher;
use OCA\News\Explore\RecommendedSites;
use OCA\News\Utility\ProxyConfigParser;


class Application extends App {

    public function __construct(array $urlParams=[]) {
        parent::__construct('news', $urlParams);

        // files
        $this->registerFileContents('checksums', 'checksum.json');
        $this->registerFileContents('info', 'info.xml');

        // parameters
        $this->registerParameter('exploreDir', __DIR__ . '/../explore');

        // factories
        $this->registerFactory(ItemMapper::class, MapperFactory::class);


        /**
         * App config parser
         */
        $this->registerService(AppConfig::class, function($c) {
            $config = new AppConfig(
                $c->query(INavigationManager::class),
                $c->query(IURLGenerator::class)
            );

            $config->loadConfig($c->query('info'));

            return $config;
        });

        /**
         * Core
         */
        $this->registerService('LoggerParameters', function($c) {
            return ['app' => $c->query('AppName')];
        });

        $this->registerService('databaseType', function($c) {
            return $c->query(IConfig::class)->getSystemValue('dbtype');
        });

        $this->registerService('ConfigView', function($c) {
            $fs = $c->query(IRootFolder::class);
            $path = 'news/config';
            if ($fs->nodeExists($path)) {
                return $fs->get($path);
            } else {
                return $fs->newFolder($path);
            }
        });


        $this->registerService(Config::class, function($c) {
            $config = new Config(
                $c->query('ConfigView'),
                $c->query(ILogger::class),
                $c->query('LoggerParameters')
            );
            $config->read('config.ini', true);
            return $config;
        });

        $this->registerService(HTMLPurifier::class, function($c) {
            $directory = $c->query(IConfig::class)
                ->getSystemValue('datadirectory') . '/news/cache/purifier';

            if(!is_dir($directory)) {
                mkdir($directory, 0770, true);
            }

            $config = HTMLPurifier_Config::createDefault();
            $config->set('HTML.ForbiddenAttributes', 'class');
            $config->set('Cache.SerializerPath', $directory);
            $config->set('HTML.SafeIframe', true);
            $config->set('URI.SafeIframeRegexp',
                '%^https://(?:www\.)?(' .
                'youtube(?:-nocookie)?.com/embed/|' .
                'player.vimeo.com/video/)%'); //allow YouTube and Vimeo
            return new HTMLPurifier($config);
        });

        /**
         * Fetchers
         */
        $this->registerService(PicoFeedConfig::class, function($c) {
            // FIXME: move this into a separate class for testing?
            $config = $c->query(Config::class);
            $appConfig = $c->query(AppConfig::class);
            $proxy =  $c->query(ProxyConfigParser::class);

            $userAgent = 'ownCloud News/' . $appConfig->getConfig('version') .
                         ' (+https://owncloud.org/; 1 subscriber;)';

            $pico = new PicoFeedConfig();
            $pico->setClientUserAgent($userAgent)
                ->setClientTimeout($config->getFeedFetcherTimeout())
                ->setMaxRedirections($config->getMaxRedirects())
                ->setMaxBodySize($config->getMaxSize())
                ->setParserHashAlgo('md5');

            // proxy settings
            $proxySettings = $proxy->parse();
            $host = $proxySettings['host'];
            $port = $proxySettings['port'];
            $user = $proxySettings['user'];
            $password = $proxySettings['password'];

            if ($host) {
                $pico->setProxyHostname($host);

                if ($port) {
                    $pico->setProxyPort($port);
                }
            }

            if ($user) {
                $pico->setProxyUsername($user)
                    ->setProxyPassword($password);
            }

            return $pico;
        });

        $this->registerService(Fetcher::class, function($c) {
            $fetcher = new Fetcher();

            // register fetchers in order, the most generic fetcher should be
            // the last one
            $fetcher->registerFetcher($c->query(YoutubeFetcher::class));
            $fetcher->registerFetcher($c->query(FeedFetcher::class));

            return $fetcher;
        });


    }

    /**
     * Registers the content of a file under a key
     * @param string $key
     * @param string $file path relative to this file, __DIR__ will be prepended
     */
    private function registerFileContents($key, $file) {
        $this->registerService($key, function () {
            return file_get_contents(__DIR__ . '/' . $file);
        });
    }

    /**
     * Shortcut for registering a service
     * @param string $key
     * @param closure $factory
     * @param boolean $shared
     */
    private function registerService($key, $factory, $shared=true) {
        $this->getContainer()->registerService($key, $factory, $shared);
    }

    /**
     * Shortcut for registering a parameter
     * @param string $key
     * @param mixed $value
     */
    private function registerParameter($key, $value) {
        $this->getContainer()->registerParameter($key, $value);
    }

    /**
     * Register a class containing the app construction logic instead of the
     * inlining everything in this class to enhance testability
     * @param string $key fully qualified class name
     * @param string $factory fully qualified factory class name
     */
    private function registerFactory($key, $factory) {
        $this->registerService($key, function ($c) use ($factory) {
            return $c->query($factory)->build();
        });
    }

    /**
     * Register the additional config parameters found in the info.xml
     */
    public function registerConfig() {
        $this->getContainer()->query(AppConfig::class)->registerAll();
    }

}