alpha1 #8
| @ -118,10 +118,10 @@ class Data { | ||||
|      * | ||||
|      * @param string $url The URL for the RSS feed | ||||
|      * @param string $title The title of the RSS feed | ||||
|      * @param string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked) | ||||
|      * @param ?string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked) | ||||
|      * @return int The ID of the added feed | ||||
|      */ | ||||
|     public static function addFeed(string $url, string $title, string $updatedOn): int { | ||||
|     public static function addFeed(string $url, string $title, ?string $updatedOn): int { | ||||
|         $db = self::getConnection(); | ||||
|         $query = $db->prepare(<<<'SQL' | ||||
|             INSERT INTO feed ( | ||||
|  | ||||
| @ -5,10 +5,10 @@ | ||||
| class Feed { | ||||
| 
 | ||||
|     /** @var string The XML namespace for Atom feeds */ | ||||
|     public const ATOM_NS = 'http://www.w3.org/2005/Atom'; | ||||
|     public const string ATOM_NS = 'http://www.w3.org/2005/Atom'; | ||||
| 
 | ||||
|     /** @var string The XML namespace for the `<content>` tag that allows HTML content in a feed */ | ||||
|     public const CONTENT_NS = 'http://purl.org/rss/1.0/modules/content/'; | ||||
|     /** @var string The XML namespace for the `<content:encoded>` tag that allows HTML content in a feed */ | ||||
|     public const string CONTENT_NS = 'http://purl.org/rss/1.0/modules/content/'; | ||||
| 
 | ||||
|     /** | ||||
|      * When parsing XML into a DOMDocument, errors are presented as warnings; this creates an exception for them | ||||
| @ -142,8 +142,14 @@ class Feed { | ||||
|         $channel = $feed['ok']->getElementsByTagName('channel')->item(0); | ||||
|         if (!$channel instanceof DOMElement) return [ 'error' => "Channel element not found ($channel->nodeType)" ]; | ||||
| 
 | ||||
|         $feedId = Data::addFeed($feed['url'], self::eltValue($channel, 'title'), | ||||
|             self::eltValue($channel, 'lastBuildDate')); | ||||
|         // In Atom feeds, lastBuildDate contains the last time an item in the feed was updated; if that is not present,
 | ||||
|         // use the pubDate element instead
 | ||||
|         $updated = self::eltValue($channel, 'lastBuildDate'); | ||||
|         if ($updated == 'lastBuildDate not found') { | ||||
|             $updated = self::eltValue($channel, 'pubDate'); | ||||
|             if ($updated == 'pubDate not found') $updated = null; | ||||
|         } | ||||
|         $feedId = Data::addFeed($feed['url'], self::eltValue($channel, 'title'), $updated); | ||||
| 
 | ||||
|         $result = self::updateItems($feedId, $channel); | ||||
|         if (array_key_exists('error', $result)) return $result; | ||||
|  | ||||
| @ -12,7 +12,11 @@ Security::verifyUser(); | ||||
| if ($_SERVER['REQUEST_METHOD'] == 'POST') { | ||||
|     // TODO: get feed, add if new, reject if existing but not owned by this user, update otherwise
 | ||||
|     $result = Feed::add($_POST['url']); | ||||
|     echo '<pre>'; var_dump($result); echo '</pre>'; | ||||
|     if (array_key_exists('ok', $result)) { | ||||
|         add_message('INFO', 'Feed added successfully'); | ||||
|     } else { | ||||
|         add_message('ERROR', $result['error']); | ||||
|     } | ||||
|     $feed = [ 'id' => $_POST['id'], 'url' => $_POST['url'] ]; | ||||
|     $title = 'TODO'; | ||||
| } else { | ||||
| @ -20,9 +24,8 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { | ||||
|     $feed = [ 'id' => $_GET['id'], 'url' => '' ]; | ||||
|     $title = 'Add RSS Feed'; | ||||
| } | ||||
| page_head($title); | ||||
| 
 | ||||
| ?>
 | ||||
| page_head($title); ?>
 | ||||
| <h1><?=$title?></h1>
 | ||||
| <article> | ||||
|     <form method=POST action=/feed hx-post=/feed> | ||||
| @ -33,6 +36,5 @@ page_head($title); | ||||
|         </label><br> | ||||
|         <button type=submit>Save</button> | ||||
|     </form> | ||||
| </article> | ||||
| <?php | ||||
| </article><?php | ||||
| page_foot(); | ||||
|  | ||||
| @ -12,6 +12,17 @@ require 'user-config.php'; | ||||
| 
 | ||||
| Data::ensureDb(); | ||||
| 
 | ||||
| /** | ||||
|  * Add a message to be displayed at the top of the page | ||||
|  * | ||||
|  * @param string $level The level (type) of the message | ||||
|  * @param string $message The message itself | ||||
|  */ | ||||
| function add_message(string $level, string $message): void { | ||||
|     if (!array_key_exists('USER_MSG', $_REQUEST)) $_REQUEST['USER_MSG'] = array(); | ||||
|     $_REQUEST['USER_MSG'][] = ['level' => $level, 'message' => $message]; | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * Render the page title | ||||
|  * @param string $title The title of the page being displayed | ||||
| @ -29,14 +40,19 @@ function page_head(string $title): void { | ||||
|     <div><?php | ||||
|         if (array_key_exists('FRC_USER_ID', $_REQUEST)) { | ||||
|             echo '<a href=/feed?id=new>Add Feed</a>'; | ||||
|             if ($_REQUEST['FRC_USER_EMAIL'] != 'solouser@example.com') { | ||||
|                 echo " | {$_REQUEST['FRC_USER_EMAIL']}"; | ||||
|             } | ||||
|             if ($_REQUEST['FRC_USER_EMAIL'] != 'solouser@example.com') echo " | {$_REQUEST['FRC_USER_EMAIL']}"; | ||||
|         } ?>
 | ||||
|     </div> | ||||
| </header> | ||||
| <main hx-target=this> | ||||
| <?php | ||||
| <main hx-target=this><?php | ||||
|     if (array_key_exists('USER_MSG', $_REQUEST)) { | ||||
|         foreach ($_REQUEST['USER_MSG'] as $msg) { ?>
 | ||||
|             <div> | ||||
|                 <?=$msg['level'] == 'INFO' ? '' : "<strong>{$msg['level']}</strong><br>"?>
 | ||||
|                 <?=$msg['message']?>
 | ||||
|             </div><?php | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user