Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Wednesday, January 2, 2008

Directory listing as podcast

Here is a quick and dirty (as always) php script to turn a directory listing into an rss 2.0 feed with enclosures, suitable for use with a podcast client. Just drop a copy of this script into the directory and point your feed reader at it. The script automatically hides itself in the listing.

PHP:
<?php
$link = 'http://'.$_SERVER['SERVER_NAME'].preg_replace('/\/[^\/]+\.[^\/]+$/', '/', $_SERVER['REQUEST_URI']);
$items = preg_replace('/\s+->\s.*$/','', explode("\n", `ls -FlopTt`));

$items = preg_grep('/('.preg_replace('/.*\//', '', $_SERVER['PHP_SELF']).')$|^$|^total/', $items, PREG_GREP_INVERT);

header('Content-type: application/rss+xml; charset=utf-8');
echo '<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>'.$_SERVER['SERVER_NAME'].preg_replace('/\/[^\/]+\.[^\/]+$/', '/', $_SERVER['REQUEST_URI']).'</title>
<link>'.$link.'</link>
<description>Contents of '.$link.'</description>
<language>en-us</language>
';
foreach ($items as $value) echo '<item>
<title>'.preg_replace('/^(\S+\s+){10}/', '', $value).'</title>
<description>'.$value.'</description>
<enclosure url="'.$link.rawurlencode(preg_replace('/^(\S+\s+){10}/', '', $value)).'" length="'.preg_replace('/^(\S+\s+){5}((\S+){1}).*/', '\2', $value).'" type="'.mime_content_type(preg_replace('/^(\S+\s+){10}/', '', $value)).'" />
<guid>'.$link.rawurlencode(preg_replace('/^(\S+\s+){10}/', '', $value)).'</guid>
<pubDate>'.gmdate('D, d M Y G:i:s', strtotime(preg_replace('/^(\S+\s+){6}((\S+\s+){4}).*/', '\2', $value))).' GMT</pubDate>
</item>
';
echo '</channel>
</rss>';
?>

Thursday, December 6, 2007

Start torrent downloads from an iPhone

I have a server that I use for, among other things, downloading files with bittorrent. I usually start these torrents by downloading them on my primary computer and then dropping them onto an alias to my server's torrents folder, which my bittorrent client (Transmission) monitors so that it can start the torrents automatically. I occasionally wanted to use my iPod Touch to start these torrents, but Mobile Safari refuses to download torrent files.

My solution was to use javascript to alter the torrent link to send its url to my server for download rather than trying to open it. A php script on the server could then download that url to the torrents folder. With both scripts in place and the permissions of the torrents folder modified to allow apache to write files, all I have to do is run the javascript as a bookmarklet before clicking the torrent link, and click the link to download the torrent file. The modified link will pass this url to the script to download it on the server.

Bookmarklet: torrent

PHP:
<?php
if (!($_SERVER['PHP_AUTH_USER'] == "username" && $_SERVER['PHP_AUTH_PW'] == "password")) {
header('WWW-Authenticate: Basic realm="torrent"');
header('HTTP/1.0 401 Unauthorized');
} else {
passthru('curl -H "Referer: '.$_SERVER['$HTTP_REFERER'].'" -H "User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10" -o /path/to/monitored/torrents/folder/remote.torrent "'.$_SERVER['QUERY_STRING'].'"');
}
?><script type="text/javascript">history.back()</script>


Modifications required:
  • script url in bookmarklet
  • username in php script
  • password in php script
  • torrents folder path in php script