Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Updated repo to match June 20th changes
Browse files Browse the repository at this point in the history
  • Loading branch information
waylaidwanderer committed Jul 22, 2014
1 parent fb4fca4 commit 7241a12
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 24 deletions.
5 changes: 5 additions & 0 deletions cron.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
if (php_sapi_name() != 'cli')
{
header("Location: http://ficsave.com");
exit(0);
}
chdir("tmp");
$files = glob("*");
$time = time();
Expand Down
67 changes: 56 additions & 11 deletions exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
$uniqid = $argv[1];
$story_url = $argv[2];
$format = $argv[3];
$email = isset($argv[4]) ? $argv[4] : "";

$debug = false;

Expand Down Expand Up @@ -136,16 +137,11 @@
}

$book->finalize(); // Finalize the book, and build the archive.

if ($format == "epub")
{
// Save the book to a temporary directory. ".epub" will be appended if missing.
$book->saveBook($uniqid . "_" . $story["title"] . " - " . $story["author"], "./tmp");
}
else
$filename = $uniqid . "_" . $story["title"] . " - " . $story["author"];
$book->saveBook($filename, "./tmp");
if ($format == "pdf")
{
$book->saveBook($uniqid . "_" . $story["title"] . " - " . $story["author"], "./tmp");
$storyPath = "./tmp/" . $uniqid . "_" . $story["title"] . " - " . $story["author"];
$storyPath = "./tmp/" . $filename;
if (file_exists("$storyPath.epub"))
{
$output = shell_exec("xvfb-run -a ebook-convert \"$storyPath.epub\" \"./tmp/$uniqid.pdf\"");
Expand All @@ -158,9 +154,14 @@
else
{
error_log($output);
exit(0);
}
}
}
if (!empty($email))
{
mailAttachment($filename.".".$format, "./tmp/", $email, $uniqid);
}
}
else if ($format == "mobi")
{
Expand Down Expand Up @@ -192,12 +193,56 @@
}

$mobi->setContentProvider($mobiContent);
$mobi->save("./tmp/" . $uniqid . "_" . $story["title"] . " - " . $story["author"] .".mobi");
$filename = $uniqid . "_" . $story["title"] . " - " . $story["author"] .".mobi";
$mobi->save("./tmp/" . $filename);
if (!empty($email))
{
mailAttachment($filename, "./tmp/", $email, $uniqid);
}
}

exit(0);
}

function mailAttachment($filename, $path, $mailto, $uniqid) {
$rename_explode = explode("{$uniqid}_", $filename);
$rename = $rename_explode[1];
copy($path.$filename, $path.$rename);
$filename = $rename;
$from_name = "FicSave";
$from_mail = "delivery@ficsave.com";
$replyto = "noreply@ficsave.com";
$subject = $filename;
$message = "Here's your ebook, courtesy of FicSave.com!\r\nFollow us on Twitter @FicSave and tell your friends about us!";
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
return true;
} else {
return false;
}
return $message;
}

function stripAccents($str) {
$unwanted_array = array('Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
Expand Down
42 changes: 32 additions & 10 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,46 @@
<div class="row" style="margin-top:50px">
<div class="large-12 columns">
<p>FicSave is an online fanfiction downloader that allows you to save stories from FanFiction.net (with more to come) for offline reading. Please be patient and only click the Download button once - it will take a while for longer stories to be ready for downloading.<br />
Problems? <a href="https://github.com/waylaidwanderer/FicSave/issues" target="_blank">Create an issue</a> on GitHub.</p>
<p>For questions/inquiries and keeping up with the latest news, follow me on Twitter <a href="https://twitter.com/FicSave" target="_blank">@FicSave</a>.<br>Something wrong with the generated file? Please let me know!</p>
Problems, or something wrong with the generated file? <a href="https://github.com/waylaidwanderer/FicSave/issues" target="_blank">Create an issue</a> on GitHub.</p>
<p>For questions/inquiries and keeping up with the latest news, follow me on Twitter <a href="https://twitter.com/FicSave" target="_blank">@FicSave</a>.
<br>If you are requesting the file via email, please whitelist <strong>delivery@ficsave.com</strong> otherwise you'll need to look in your Junk folder.</p>
<form action="#" method="POST" id="form" onsubmit="return false;">
<div class="row">
<div class="large-6 columns">
<div class="large-5 columns">
<label>Fanfic URL</label>
<input type="text" name="story_url" placeholder="https://www.fanfiction.net/s/<story_id>/" />
<input type="text" id="storyurl" name="story_url" placeholder="https://www.fanfiction.net/s/<story_id>/" />
</div>
<div class="large-4 columns">
<div class="large-2 columns">
<label>Format</label>
<select name="format" form="form">
<option value="epub">ePub</option>
<option value="pdf">PDF</option>
<option value="mobi">MOBI</option>
</select>
</div>
</div>
<div class="large-3 columns">
<label>Email (optional, Kindle only!)</label>
<input type="text" id="email" name="email" placeholder="user@free.kindle.com" />
</div>
<div class="large-2 columns" style="margin-top:22px">
<input id="download" type="submit" class="button postfix" value="Download">
</div>
</div>
</form>
<iframe id="file" width="0" height="0" scrolling="no" frameborder="0" src="" seamless="seamless"></iframe>
<div style="text-align:center">

<div style="text-align:center;width:100%">
<style>
.ficsave { width: 320px; height: 50px; }
@media(min-width: 500px) { .ficsave { width: 468px; height: 60px; } }
@media(min-width: 800px) { .ficsave { width: 728px; height: 90px; } }
</style>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ficsave"
style="display:inline-block"
data-ad-client="ca-pub-9751041753087569"
data-ad-slot="6792896931"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
</div>
</div>
Expand All @@ -72,6 +88,12 @@
<div class="large-12 columns">
<h3>Changelog</h3>
<ul>
<li>
<ul><strong>June 20th, 2014</strong>
<li>Added Kindle support.</li>
<li>It's my birthday!</li>
</ul>
</li>
<li>
<ul><strong>May 29th, 2014</strong>
<li>Fixed a compatibility issue with mobile browsers causing corrupted files to be sent</li>
Expand Down Expand Up @@ -140,7 +162,7 @@
ga('send', 'pageview');
</script>
<script src="js/foundation.min.js"></script>
<script src="js/ficsave.js"></script>
<script src="js/ficsave.0.0.4.7.js"></script>
<script>
$(document).foundation();
</script>
Expand Down
11 changes: 11 additions & 0 deletions js/ficsave.js → js/ficsave.0.0.3.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ $("#download").click(function() {
}
});

$('#email').keyup(function() {
if ($(this).val() != "")
{
$("#download").val("Send to Email");
}
else
{
$("#download").val("Download");
}
});

function checkDownload()
{
$.get("process.php", function(data) {
Expand Down
74 changes: 74 additions & 0 deletions js/ficsave.0.0.4.7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var start;
var timer;
var email;
$(document).ready(function()
{
if ($("#email").val() != "")
{
$("#download").val("Send to Email");
}
});

$("#download").click(function() {
if (!$(this).hasClass("disabled"))
{
$(this).addClass("disabled");
$.post("parser.php", $("#form").serialize())
.always(function() {
$(this).val("Submitting...");
})
.done(function(data) {
if (data == "done")
{
$(this).val("Waiting...");
email = $("#email").val();
start = Date.now();
timer = setInterval(checkDownload, 1000);
}
else
{
$("#download").val("Error!");
}
});
}
});

$('#email').keyup(function() {
if ($(this).val() != "")
{
$("#download").val("Send to Email");
}
else
{
$("#download").val("Download");
}
});

function checkDownload()
{
$.get("process.php", function(data) {
if (data == "waiting")
{
var seconds = Math.abs((Date.now() - start) / 1000);
$("#download").val("Waiting. " + Math.floor(seconds) + "s...");
}
else if (data == "done")
{
clearInterval(timer);
if (email === "")
{
window.location.replace("http://ficsave.com/process.php?get");
}
else
{
$("#download").val("Email Sent!");
}
$("#download").removeClass("disabled");
}
else
{
clearInterval(timer);
$("#download").val("Error!");
}
});
}
24 changes: 21 additions & 3 deletions parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@

if (!isset($_POST["story_url"]) || empty($_POST["story_url"]) || !isset($_POST["format"]) || empty($_POST["format"]) || strpos($_POST["story_url"], 'fanfiction.net') === FALSE)
{
header("Location: http://ficsave.com");
echo "error";
exit(0);
}

if (isset($_POST["email"]) && !empty($_POST["email"]))
{
$domain = substr(strrchr($_POST["email"], "@"), 1);
$valid = array('kindle.com', 'free.kindle.com');
if (!in_array($domain, $valid) || empty($domain))
{
echo "error";
exit(0);
}
}
$email = isset($_POST["email"]) ? $_POST["email"] : "";
$format = $_POST["format"];
$uniqid = uniqid();
session_start();
Expand All @@ -17,14 +27,22 @@
$story_id = getStoryID($story_url);
$story_url = "https://www.fanfiction.net/s/" . $story_id . "/";


$output = array();
$status = -1;
$retry = 0;
while ($status != 0) {
exec("php exec.php $uniqid $story_url $format > /dev/null &", &$output, &$status);
if ($retry == 5)
{
echo "error";
break;
}
exec("php exec.php $uniqid $story_url $format $email > /dev/null &", &$output, &$status);
if ($status == 0) {
echo "done";
break;
} else {
$retry++;
sleep(1);
}
}
Expand Down

0 comments on commit 7241a12

Please sign in to comment.