summaryrefslogtreecommitdiffstats
path: root/app/classes/OpmlManager.php
blob: cd3d685554dcbe1ad8388d4e0b9e2c33cf030218 (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
<?php


class OpmlManager
{
    public static function load($file)
    {
        if (!file_exists($file)) {
            throw new Exception('OPML file not found!');
        }

        $opml = new Opml();

        //Remove BOM if needed
        $BOM = '/^/';
        $fileContent = file_get_contents($file);
        $fileContent = preg_replace($BOM, '', $fileContent, 1);

        //Parse
        $opml->parse($fileContent);

        return $opml;
    }

    /**
     * @param Opml   $opml
     * @param string $file
     */
    public static function save($opml, $file)
    {
        $out = '<?xml version="1.0"?>'."\n";
        $out.= '<opml version="2.0">'."\n";
        $out.= '<head>'."\n";
        $out.= '<title>'.htmlspecialchars($opml->getTitle()).'</title>'."\n";
        $out.= '<dateCreated>'.gmdate('c').'</dateCreated>'."\n";
        $out.= '<dateModified>'.gmdate('c').'</dateModified>'."\n";
        if ($opml->ownerName != '') {
            $out.= '<ownerName>'.htmlspecialchars($opml->ownerName).'</ownerName>'."\n";
        }
        if ($opml->ownerEmail != '') {
            $out.= '<ownerEmail>'.htmlspecialchars($opml->ownerEmail).'</ownerEmail>'."\n";
        }
        if ($opml->ownerId != '') {
            $out.= '<ownerId>'.htmlspecialchars($opml->ownerId).'</ownerId>'."\n";
        }
        $out.= '<docs>http://opml.org/spec2.opml</docs>'."\n";

        $out.= '</head>'."\n";
        $out.= '<body>'."\n";
        foreach ($opml->entries as $person) {
            $out .= sprintf(
                '<outline text="%s" htmlUrl="%s" xmlUrl="%s" isDown="%s" />',
                htmlspecialchars($person['name'], ENT_QUOTES),
                htmlspecialchars($person['website'], ENT_QUOTES),
                htmlspecialchars($person['feed'], ENT_QUOTES),
                htmlspecialchars($person['isDown'] ?? '', ENT_QUOTES)
            ) . "\n";
        }
        $out.= '</body>'."\n";
        $out.= '</opml>';

        file_put_contents($file, $out);
    }

    public static function backup($file)
    {
        copy($file, $file.'.bak');
    }
}