Skip to content

Instantly share code, notes, and snippets.

@Nosskirneh
Forked from kirb/.htaccess
Last active July 1, 2020 00:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nosskirneh/765a0eced64cfa16de0eb39ba9aa85b8 to your computer and use it in GitHub Desktop.
Save Nosskirneh/765a0eced64cfa16de0eb39ba9aa85b8 to your computer and use it in GitHub Desktop.
Self-hosted Cydia Repo Download Counter

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

This is a fork of kirb's repo download counter, only that this one works and that one doesn't.

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 nginx.conf in your favorite non-Notepad text editor.
  3. In counter.php, scroll down to the line that says $db = 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($db, "UTF8")) ... line to the appropriate value.
  5. Go down to the if (!mysqli_select_db($db, "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 debs in your repo folder.
  8. In nginx.conf: Replce the repo with the path to the folder that your repo is in, relative to your document root. Place the rewrite rules to the root server block in the existing /etc/nginx/nginx.conf file on your server.
  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);
}
$db = mysqli_connect("localhost", "user", "password"); //change parameters to what you use
if (!$db) {
errorout("Couldn't connect to the database. Please try your download later.");
}
if (!mysqli_set_charset($db, "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($db, "myrepodb")) { //change myrepodb to your database name
errorout("Couldn't open the database. Please try your download later.");
}
//Store the file name in a variable
$file = isset($_GET['filename']) ? mysqli_real_escape_string($db, $_GET['filename']) : "";
$filename = $file . '.deb';
//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('debs/' . $filename)) {
//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($db, "SELECT * FROM downloads WHERE filename='$file'");
if (!$query or mysqli_num_rows($query) == 0) {
//If it doesn't exist, create the row
mysqli_query($db, "INSERT INTO downloads SET filename='$file', count=1, dldate=NOW()");
} else {
//If it does, add one to the counter
mysqli_query($db, "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" : 'debs/' . $filename);
//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=\"$filename\"");
//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
rewrite ^/repo/debs(/?)$ /repo/counter.php?filename=Packages last;
rewrite ^/repo/Packages$ /repo/counter.php?filename=Packages last;
rewrite ^/repo/debs/(.*).deb$ /repo/counter.php?filename=;
@0bash
Copy link

0bash commented Jul 27, 2018

i have a trouble with counting can you help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment