Skip to content

Instantly share code, notes, and snippets.

@sharif0777
Forked from kirb/.htaccess
Created November 29, 2017 18:12
Show Gist options
  • Save sharif0777/5e05bdf75f1f4510a78dd59439051748 to your computer and use it in GitHub Desktop.
Save sharif0777/5e05bdf75f1f4510a78dd59439051748 to your computer and use it in GitHub Desktop.
Self-hosted Cydia Repo Download Counter
RewriteEngine On
RewriteBase /
RewriteRule ^repo/downloads(\/?)$ /repo/counter.php?filename=Packages [L]
RewriteRule ^repo/Packages$ /repo/counter.php?filename=Packages [L]
RewriteRule ^repo/downloads/(.*)\.deb$ /repo/counter.php?filename=$1 [QSA]

How to Add a Download Counter to A Self-Hosted Cydia Repo

This is the source code from this tutorial on my blog.

Installation

  1. Click the download link at the top of the gist page to save these files to your machine.
  2. Unzip them and open counter.php and .htaccess in your favorite non-Notepad text editor.
  3. In counter.php, scroll down to the line that says $link = mysqli_connect( ... and change the parameters to the ones you use to connect to MySQL.
  4. If your database doesn't use the UTF-8 charset, change the UTF8 in the if (!mysqli_set_charset($link, "UTF8")) ... line to the appropriate value.
  5. Go down to the if (!mysqli_select_db($link, "myrepodb")) ... line and change myrepodb to the name of your database.
  6. Run the downloads.sql file in your MySQL database, changing UTF8 if needed. In phpMyAdmin, you can use the import tab to do this.
  7. Move your debs to a folder called downloads in your repo folder.
  8. Open the .htaccess file and replace repo with the path to the folder that your repo is in, relative to your document root (usually htdocs or public_html)
  9. Make 100,000% sure there is not a byte order mark at the start of counter.php. On Windows, open counter.php in Notepad++ and choose Convert to UTF-8 without BOM from the encoding menu. On Linux and Mac, pull up a terminal, cd to the folder the code is in and type: awk '{if(NR==1)sub(/^\xef\xbb\xbf/,"");print}' counter.php > counter1.php && mv counter1.php counter.php (note that this will cause problems if the file isn't in the UTF-8 charset)
  10. Upload and enjoy :)
<?php
function errorout($message) {
//Makes dpkg know it's not a package
header("Content-Type: text/plain");
//Generates HTTP error 500
header($_SERVER["SERVER_PROTOCOL"] . " 500 Internal Server Error");
//Then print out the error.
//Because the content type is plain text, escaping is not necessary.
die($message);
}
$link = mysqli_connect("localhost", "myrepo", "mypassword"); //change parameters to what you use
if (!$link) {
errorout("Couldn't connect to the database. Please try your download later.");
}
if (!mysqli_set_charset($link, "UTF8")) { //change UTF8 if your database uses otherwise
errorout("Couldn't set the charset for the database. Please try your download later.");
}
if (!mysqli_select_db($link, "myrepodb")) { //change myrepodb to your database name
errorout("Couldn't open the database. Plrase try your download later.");
}
//Store the file name in a variable
$file = isset($_GET["filename"]) ? mysqli_real_escape_string($link, $_GET["filename"]) : "";
//The following code checks for attempts to read files that aren't debs.
//Not doing this lets attackers read PHP scripts and system files.
if (empty($file) or stristr("..", $file) or stristr("/", $file) or stristr("\\", $file) or !file_exists("downloads/$file")) {
//Reset the file name to the Cydia package listing
$file = "Packages";
}
//Count the download in the database
//A log will be saved if something goes wrong.
try {
//Check whether the package has already been downloaded
$query = mysqli_query($link, "SELECT * FROM downloads WHERE filename='$file'");
if (!$query or mysqli_num_rows($query) == 0) {
//If it doesn't exist, create the row
mysqli_query($link, "INSERT INTO downloads SET filename='$file', count=1, dldate=NOW()");
} else {
//If it does, add one to the counter
mysqli_query($link, "UPDATE downloads SET count=count+1, dldate=NOW() WHERE filename='$file'");
}
} catch (Exception $e) {
//Something went wrong, so save a log file below the document root
//(usually htdocs or public_html)
file_put_contents(dirname($_SERVER["DOCUMENT_ROOT"]) . "/cydiaerr.log", print_r($e, true));
}
//Finally, serve the user the file they requested
$cont = file_get_contents($file == "Packages" ? "Packages" : "downloads/$file");
//If it's more than 16 bytes long, it's ok to serve it
if (strlen($cont) > 16) {
//Force Cydia to download the file, not display it
header("Content-Type: application/octet-stream");
header("Content-Disposition: inline; filename=\"$file\"");
//Tell Cydia how big the file is so it can show the progress bar
header("Content-Length: " . strlen($cont));
//And finally, output the file!
echo $cont;
} else {
errorout("The file you requested either doesn't exist or isn't allowed to be downloaded.");
}
CREATE TABLE IF NOT EXISTS `downloads` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`filename` text,
`dldate` datetime DEFAULT NULL,
`count` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment