Skip to content

Commit

Permalink
Merge branch 'save-for-later'
Browse files Browse the repository at this point in the history
  • Loading branch information
eheikes committed Oct 20, 2016
2 parents d29be15 + 6470239 commit 0126cf6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This originally began as a single PHP script, but was then rewritten in Angular
## What You Can Do

* Add a link for later reading
* View a (rough) snapshot of the page
* Add a link *with description* to save for reference
* Add "tags" to categorize your bookmarks
* See how many items are in your list
Expand Down Expand Up @@ -38,15 +39,17 @@ This software requires a server running PHP and a MySQL (or compatible) database
## File Manifest
* `toread.html` -- HTML partial for Angular
* `toreadcontrols.html` -- HTML partial for Angular
* `toread.ini` -- configuration file
* `toread.js` -- Angular app
* `toread.php` -- App boilerplate
* `toread.sql` -- database schema
* `toreadapi.php` -- API backend
* `toreadcontrols.html` -- HTML partial for Angular
* `toreadsnapshot.php` -- snapshot view for a link
## Legal
Copyright 2015 Eric Heikes.
Copyright 2015-2016 Eric Heikes.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this software except in compliance with the License. You may obtain a copy of the License at [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0).
Expand Down
1 change: 1 addition & 0 deletions toread.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<li ng-repeat="item in entries" ng-class="{highlight: item.highlighted}">
<input type="checkbox" name="id[]" checklist-model="selectedItems" value="{{item.id}}" checklist-value="item.id" ng-disabled="actionInProgress">
<a ng-href="{{item.link}}" ng-class="{strike: item.deleted}" target="_blank">{{item.title}}</a>
<a ng-if="item.hasSnapshot" href="toreadsnapshot.php?id={{item.id}}"" class="snapshot" target="_blank" title="View snapshot"><span class="glyphicon glyphicon-sunglasses" aria-label="View snapshot"></span></a>
<span class="date">
{{moment(item.time).format("M/D/YY")}}
</span>
Expand Down
1 change: 1 addition & 0 deletions toread.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
.all-tags { margin-bottom: 0.5em; }
.all-tags:after { content: ""; display: table; clear: both; }
.all-tags .label { float: left; margin-bottom: 0.5em; }
.snapshot { margin-left: 0.25em; }
.description { white-space: pre-line; margin: 0 0 0 1.2em; }
.label { margin-right: 0.25em; }
.faded { opacity: 0.5; }
Expand Down
1 change: 1 addition & 0 deletions toread.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ CREATE TABLE `links` (
`title` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`deleted` datetime DEFAULT NULL,
`keywords` text COLLATE utf8mb4_unicode_ci,
`snapshot` mediumtext COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
Expand Down
6 changes: 6 additions & 0 deletions toreadapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ function getEntry() {
'title' => html_entity_decode($item['title']),
'link' => $item['link'],
'description' => $item['keywords'],
'hasSnapshot' => !is_null($item['snapshot']),
'time' => date('c', $item['time']),
'created' => $item['created'],
'deleted' => isset($_GET['since']) ? $item['deleted'] : !is_null($item['deleted']),
Expand Down Expand Up @@ -228,6 +229,7 @@ function postEntry() {
$response = array();

// Retrieve the page title.
$html = NULL; // default
$title = htmlentities($url); // default
if (function_exists("curl_init"))
{
Expand All @@ -240,11 +242,14 @@ function postEntry() {
$result = curl_exec($ch);
if ($result !== false)
{
// Save the <title>.
if (preg_match("#<title[^>]*>(.*)</title>#iU", $result, $matches)
and $matches[1] != "")
{
$title = $matches[1];
}
// Save the page.
$html = $result;
}
}

Expand All @@ -253,6 +258,7 @@ function postEntry() {
. " SET created = NOW()"
. " , url = " . $dbh->quote($url)
. " , title = " . $dbh->quote($title)
. " , snapshot = " . (is_null($html) ? "NULL" : $dbh->quote($html))
. " , keywords = " . ($keywords == '' ? "NULL" : $dbh->quote($keywords));
$success = $dbh->exec($sql);
$response['success'] = (bool)$success;
Expand Down
34 changes: 34 additions & 0 deletions toreadsnapshot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
$config = parse_ini_file('toread.ini');

if (!isset($_GET['id'])) {
header("HTTP/1.0 400 Bad Request");
echo "Please specify a link ID.";
exit;
}

try {
$dbh = new PDO(
"mysql:dbname=" . $config['db_name'] . ";host=" . $config['db_host'],
$config['db_user'], $config['db_pass']);
} catch (Exception $e) {
header("HTTP/1.0 500 Internal Server Error");
echo "Could not connect to database: " . $e->getMessage();
exit;
}

$sql = "SELECT snapshot"
. " FROM links"
. " WHERE id=" . $dbh->quote($_GET['id'])
. " LIMIT 1";
$result = $dbh->query($sql);
$snapshots = $result->fetchAll();
if (empty($snapshots)) {
header("HTTP/1.0 404 Not Found");
echo "Could not find link with ID of " . $_GET['id'];
exit;
}

header('Content-Type: text/html');
echo $snapshots[0]['snapshot'] ? $snapshots[0]['snapshot'] : '<p>No snapshot found.</p>';
?>

0 comments on commit 0126cf6

Please sign in to comment.