]> git.mxchange.org Git - friendica-addons.git/blob - opmlexport/opmlexport.php
Initial commit of url_replace addon.
[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 use Friendica\Model\User;
20
21 function opmlexport_install()
22 {
23         Hook::register('addon_settings',        __FILE__, 'opmlexport_addon_settings');
24         Hook::register('addon_settings_post',   __FILE__, 'opmlexport_addon_settings_post');
25         Logger::notice('installed opmlexport Addon');
26 }
27
28
29 function opmlexport()
30 {
31         $condition = [
32                 'uid' => DI::userSession()->getLocalUserId(),
33                 'self' => false,
34                 'deleted' => false,
35                 'archive' => false,
36                 'blocked' => false,
37                 'pending' => false,
38                 'network' => Protocol::FEED
39         ];
40         $data = Contact::selectToArray([], $condition, ['order' => ['name']]);
41         $user = User::getById(DI::userSession()->getLocalUserId());
42
43         $xml = new \DOMDocument( '1.0', 'utf-8' );
44         $opml = $xml->createElement('opml');
45         $head = $xml->createElement('head');
46         $body = $xml->createElement('body');
47         $outline = $xml->createElement('outline');
48         $outline->setAttribute('title', $user['username'] . '\'s RSS/Atom contacts');
49         $outline->setAttribute('text', $user['username'] . '\'s RSS/Atom contacts');
50
51         foreach($data as $c) {
52                 $entry = $xml->createElement('outline');
53                 $entry->setAttribute('title',  $c['name']);
54                 $entry->setAttribute('text',   $c['name']);
55                 $entry->setAttribute('xmlUrl', $c['url']);
56                 $outline->appendChild($entry);
57         }
58
59         $body->appendChild($outline);
60         $opml->appendChild($head);
61         $opml->appendChild($body);
62         $xml->appendChild($opml);
63         header('Content-Type: text/x-opml');
64         header('Content-Disposition: attachment; filename=feeds.opml');
65         $xml->formatOutput = true;
66         echo $xml->saveXML();
67         die();
68 }
69
70
71 function opmlexport_addon_settings(array &$data)
72 {
73         if (!DI::userSession()->getLocalUserId()) {
74                 return;
75         }
76
77         $data = [
78                 'addon'  => 'opmlexport',
79                 'title'  => DI::l10n()->t('OPML Export'),
80                 'submit' => DI::l10n()->t('Export RSS/Atom contacts'),
81         ];
82 }
83
84
85 function opmlexport_addon_settings_post(array &$b)
86 {
87         if (!DI::userSession()->getLocalUserId() || empty($_POST['opmlexport-submit'])) {
88                 return;
89         }
90
91         opmlexport();
92 }