-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.php
127 lines (111 loc) · 2.73 KB
/
API.php
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
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik_Plugins
* @package Piwik_API
*/
namespace Piwik\Plugins\FeedAnnotation;
use Piwik\Piwik;
use Piwik\Common;
/**
* FeedAnnotation API is used to request the configured Feed Annotation URLs.
*
* @package Piwik_FeedAnnotation
*/
class API {
protected static $instance;
/**
* Gets or creates the FeedAnnotation API singleton.
*/
public static function getInstance()
{
if (self::$instance == null)
{
self::$instance = new self;
}
return self::$instance;
}
/**
* Returns all configured feeds for all idSites.
* User needs to have admin access to a site.
*
* @param array $idSites
* @return array
*/
public function getFeeds($idSites = array()) {
if (count($idSites)) {
Piwik::checkUserHasViewAccess($idSites);
} else {
Piwik::checkUserHasSomeViewAccess();
$idSites = Piwik\Plugins\SitesManager\API::getInstance()->getSitesIdWithAtLeastViewAccess();
}
$query = sprintf("SELECT * FROM %s WHERE idsite IN (%s)",
Common::prefixTable("feedannotation"),
implode(",", $idSites)
);
$feeds = \Piwik\Db::fetchAll($query);
return $feeds;
}
/**
* Fetches a single feed by id.
*
* @param int $idFeed
* @throws InvalidFeedException
*/
public function getFeed($idFeed)
{
$query = sprintf("SELECT * FROM %s WHERE idfeed = %d",
Common::prefixTable("feedannotation"), $idFeed
);
$feed = \Piwik\Db::fetchRow($query);
if($feed)
{
Piwik::isUserHasViewAccess(array($feed['idsite']));
return $feed;
} else {
throw new InvalidFeedException(sprintf("Feed ID not valid: %d", $idFeed));
}
}
/**
* Adds a new feed to idSite.
*
* @param $idSite
* @param $url
* @throws InvalidFeedException
*/
public function addFeed($idSite, $url) {
Piwik::checkUserHasAdminAccess(array($idSite));
if($this->isValidFeedUrl($url))
{
$query = sprintf("INSERT INTO %s (idsite, feed_url) VALUES (?, ?)",
Common::prefixTable("feedannotation")
);
\Piwik\Db::query($query, array($idSite, $url));
} else {
throw new InvalidFeedException(sprintf("Feed URL not valid: %s", $url));
}
}
/**
* Checks whether the URL returns a parsable feed (RSS/Atom)
*
* @param $url
* @return bool
*/
public function isValidFeedUrl($url) {
try {
\Zend_Feed::import($url);
return true;
} catch (\Zend_Feed_Exception $e) {
return false;
}
}
}
/**
* Custom exception that is thrown when an invalid feed URL is specified.
*/
class InvalidFeedException extends \Exception {
}