]> git.mxchange.org Git - friendica-addons.git/blob - opmlexport/opmlexport.php
ca65cbaab812b8c3076bd8930032272ef27c3f99
[friendica-addons.git] / opmlexport / opmlexport.php
1 <?php
2 /**
3  * Name: OPML Export
4  * Description: Export user's RSS/Atom contacts as OPML
5  * Version: 1.0
6  * Author: Fabio Comuni <https://social.gl-como.it/profile/fabrixxm>
7  * License: 3-clause BSD license
8  */
9
10 use Friendica\DI;
11 use Friendica\App;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Logger;
14 use Friendica\Network\HTTPException;
15 use Friendica\Database\DBA;
16 use Friendica\Core\Renderer;
17 use Friendica\Core\Protocol;
18 use Friendica\Model\Contact;
19
20 function opmlexport_install()
21 {
22         Hook::register('addon_settings',        __FILE__, 'opmlexport_addon_settings');
23         Hook::register('addon_settings_post',   __FILE__, 'opmlexport_addon_settings_post');
24         Logger::log('installed opmlexport Addon');
25 }
26
27
28 function opmlexport(App $a)
29 {
30         $condition = [
31                 'uid' => local_user(),
32                 'self' => false,
33                 'deleted' => false,
34                 'archive' => false,
35                 'blocked' => false,
36                 'pending' => false,
37                 'network' => Protocol::FEED
38         ];
39         $data = Contact::selectToArray([], $condition, ['order' => ['name']]);
40
41         $xml = new \DOMDocument( '1.0', 'utf-8' );
42         $opml = $xml->createElement('opml');
43         $head = $xml->createElement('head');
44         $body = $xml->createElement('body');
45         $outline = $xml->createElement('outline');
46         $outline->setAttribute('title', $a->user['username'] . '\'s RSS/Atom contacts');
47         $outline->setAttribute('text', $a->user['username'] . '\'s RSS/Atom contacts');
48
49         foreach($data as $c) {
50                 $entry = $xml->createElement('outline');
51                 $entry->setAttribute('title',  $c['name']);
52                 $entry->setAttribute('text',   $c['name']);
53                 $entry->setAttribute('xmlUrl', $c['url']);
54                 $outline->appendChild($entry);
55         }
56
57         $body->appendChild($outline);
58         $opml->appendChild($head);
59         $opml->appendChild($body);
60         $xml->appendChild($opml);
61         header('Content-Type: text/x-opml');
62         header('Content-Disposition: attachment; filename=feeds.opml');
63         $xml->formatOutput = true;
64         echo $xml->saveXML();
65         die();
66 }
67
68
69 function opmlexport_addon_settings(App $a, &$s)
70 {
71         if (!local_user()) {
72                 return;
73         }
74
75         $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/opmlexport/');
76         $s .= Renderer::replaceMacros($t, [
77                 '$title'   => DI::l10n()->t('OPML Export'),
78                 '$submit'  => DI::l10n()->t('Export RSS/Atom contacts'),
79         ]);
80 }
81
82
83 function opmlexport_addon_settings_post(App $a, &$b)
84 {
85         if (!local_user() || empty($_POST['opmlexport-submit'])) {
86                 return;
87         }
88         opmlexport($a);
89 }