forked from Business/AccountHub
Add RSS/ATOM notification feeds, close #12
parent
f43f986e25
commit
39ccaa2f2d
Binary file not shown.
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS `userkeys` (
|
||||||
|
`uid` INT(11) NOT NULL,
|
||||||
|
`key` VARCHAR(100) NOT NULL,
|
||||||
|
`created` DATETIME NULL DEFAULT NULL,
|
||||||
|
`typeid` INT(11) NOT NULL,
|
||||||
|
PRIMARY KEY (`uid`),
|
||||||
|
INDEX `fk_userkeys_userkeytypes1_idx` (`typeid` ASC),
|
||||||
|
CONSTRAINT `fk_userkeys_accounts1`
|
||||||
|
FOREIGN KEY (`uid`)
|
||||||
|
REFERENCES `accounthub`.`accounts` (`uid`)
|
||||||
|
ON DELETE NO ACTION
|
||||||
|
ON UPDATE NO ACTION,
|
||||||
|
CONSTRAINT `fk_userkeys_userkeytypes1`
|
||||||
|
FOREIGN KEY (`typeid`)
|
||||||
|
REFERENCES `accounthub`.`userkeytypes` (`typeid`)
|
||||||
|
ON DELETE NO ACTION
|
||||||
|
ON UPDATE NO ACTION)
|
||||||
|
ENGINE = InnoDB
|
||||||
|
DEFAULT CHARACTER SET = utf8;
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS `userkeytypes` (
|
||||||
|
`typeid` INT(11) NOT NULL,
|
||||||
|
`typename` VARCHAR(45) NOT NULL,
|
||||||
|
PRIMARY KEY (`typeid`, `typename`))
|
||||||
|
ENGINE = InnoDB
|
||||||
|
DEFAULT CHARACTER SET = utf8;
|
||||||
|
|
||||||
|
INSERT INTO `userkeytypes` (`typeid`, `typename`) VALUES (1, 'RSSAtomFeed');
|
||||||
|
INSERT INTO `userkeytypes` (`typeid`, `typename`) VALUES (2, 'Other');
|
@ -0,0 +1,89 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
require __DIR__ . "/required.php";
|
||||||
|
|
||||||
|
date_default_timezone_set('UTC');
|
||||||
|
|
||||||
|
use \FeedWriter\RSS1;
|
||||||
|
use \FeedWriter\RSS2;
|
||||||
|
use \FeedWriter\ATOM;
|
||||||
|
|
||||||
|
if (empty($_GET['key']) || empty($_GET['type'])) {
|
||||||
|
http_response_code(400);
|
||||||
|
die("400 Bad Request: please send a user key and a feed type");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$database->has('userkeys', ['key' => $_GET['key']])) {
|
||||||
|
http_response_code(403);
|
||||||
|
die("403 Forbidden: provide valid key");
|
||||||
|
}
|
||||||
|
|
||||||
|
$uid = $database->get('userkeys', 'uid', ['key' => $_GET['key']]);
|
||||||
|
$user = new User($uid);
|
||||||
|
switch ($user->getStatus()->get()) {
|
||||||
|
case AccountStatus::NORMAL:
|
||||||
|
case AccountStatus::CHANGE_PASSWORD:
|
||||||
|
case AccountStatus::ALERT_ON_ACCESS:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
http_response_code(403);
|
||||||
|
die("403 Forbidden: user account not active");
|
||||||
|
}
|
||||||
|
|
||||||
|
$notifications = Notifications::get($user);
|
||||||
|
|
||||||
|
switch ($_GET['type']) {
|
||||||
|
case "rss1":
|
||||||
|
$feed = new RSS1();
|
||||||
|
break;
|
||||||
|
case "rss":
|
||||||
|
case "rss2":
|
||||||
|
$feed = new RSS2();
|
||||||
|
break;
|
||||||
|
case "atom":
|
||||||
|
$feed = new ATOM();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
http_response_code(400);
|
||||||
|
die("400 Bad Request: feed parameter must have a value of \"rss\", \"rss1\", \"rss2\" or \"atom\".");
|
||||||
|
}
|
||||||
|
|
||||||
|
$feed->setTitle($Strings->build("Notifications from server for user", ['server' => SITE_TITLE, 'user' => $user->getName()], false));
|
||||||
|
|
||||||
|
if (strpos(URL, "http") === 0) {
|
||||||
|
$url = URL;
|
||||||
|
} else {
|
||||||
|
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . (($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) ? ":" . $_SERVER['SERVER_PORT'] : "") . URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
$feed->setLink($url);
|
||||||
|
|
||||||
|
foreach ($notifications as $n) {
|
||||||
|
$item = $feed->createNewItem();
|
||||||
|
$item->setTitle($n['title']);
|
||||||
|
if (empty($n['url'])) {
|
||||||
|
$item->setLink($url);
|
||||||
|
} else {
|
||||||
|
$item->setLink($n['url']);
|
||||||
|
}
|
||||||
|
$item->setDate(strtotime($n['timestamp']));
|
||||||
|
if ($n['sensitive']) {
|
||||||
|
$content = $Strings->get("Sensitive content hidden", false);
|
||||||
|
} else {
|
||||||
|
$content = $n['content'];
|
||||||
|
}
|
||||||
|
if ($_GET['type'] == "atom") {
|
||||||
|
$item->setContent($content);
|
||||||
|
} else {
|
||||||
|
$item->setDescription($content);
|
||||||
|
}
|
||||||
|
$feed->addItem($item);
|
||||||
|
}
|
||||||
|
|
||||||
|
$feed->printFeed();
|
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class RandomString {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random string, using a cryptographically secure
|
||||||
|
* pseudorandom number generator (random_int)
|
||||||
|
*
|
||||||
|
* From https://stackoverflow.com/a/31107425
|
||||||
|
*
|
||||||
|
* @param int $length How many characters do we want?
|
||||||
|
* @param string $keyspace A string of all possible characters
|
||||||
|
* to select from
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function generate(int $length, string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'): string {
|
||||||
|
$pieces = [];
|
||||||
|
$max = mb_strlen($keyspace, '8bit') - 1;
|
||||||
|
for ($i = 0; $i < $length; ++$i) {
|
||||||
|
$pieces [] = $keyspace[random_int(0, $max)];
|
||||||
|
}
|
||||||
|
return implode('', $pieces);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue