]> git.mxchange.org Git - friendica-addons.git/blob - opmlexport/opmlexport.php
Code standards
[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     $stmt = DBA::p("SELECT *
29             FROM `contact`
30             WHERE `uid` = ?
31                 AND `self` = 0
32                 AND NOT `deleted`
33                 AND NOT `archive` AND NOT `blocked` AND NOT `pending`
34                 AND network = ?
35             ORDER BY `name` ASC",
36             [local_user(), "feed"]
37     );
38
39 function opmlexport(App $a)
40 {
41
42     $xml = new \DOMDocument( '1.0', 'utf-8' );
43     $opml = $xml->createElement('opml');
44     $head = $xml->createElement('head');
45     $body = $xml->createElement('body');
46     $outline = $xml->createElement('outline');
47     $outline->setAttribute('title', $a->user['username'] . '\'s RSS/Atom contacts');
48     $outline->setAttribute('text', $a->user['username'] . '\'s RSS/Atom contacts');
49
50     while ($c = DBA::fetch($stmt)) {
51         $entry = $xml->createElement('outline');
52         $entry->setAttribute('title',  $c['name']);
53         $entry->setAttribute('text',   $c['name']);
54         $entry->setAttribute('xmlUrl', $c['url']);
55         $outline->appendChild($entry);
56     }
57     DBA::close($stmt);
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(App $a, &$s)
72 {
73     if (!local_user()) {
74         return;
75     }
76
77     $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/opmlexport/');
78     $s .= Renderer::replaceMacros($t, [
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(App $a, &$b)
86 {
87     if (!local_user() || empty($_POST['opmlexport-submit'])) {
88         return;
89     }
90     opmlexport($a);
91 }