Skip to content

Instantly share code, notes, and snippets.

@zllovesuki
Created June 13, 2014 19:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zllovesuki/b22ed4427c02a5ad033e to your computer and use it in GitHub Desktop.
Save zllovesuki/b22ed4427c02a5ad033e to your computer and use it in GitHub Desktop.
Cache URL Generator
<?php
$retina = true;
$key = 'kokenwtf'; //ENTER YOUR OWN KEY
if ($_GET['key'] != $key) die;
$ds = DIRECTORY_SEPARATOR;
$root = dirname(__FILE__);
$content = $root . $ds . 'storage';
$base = '/storage/cache/images';
require($content . $ds . 'configuration' . $ds . 'database.php');
$interface = $KOKEN_DATABASE['driver'];
$query = "SELECT id, filename, file_modified_on FROM {$KOKEN_DATABASE['prefix']}content";
if ($interface == 'mysqli')
{
$db_link = mysqli_connect($KOKEN_DATABASE['hostname'], $KOKEN_DATABASE['username'], $KOKEN_DATABASE['password'], null, (int) $KOKEN_DATABASE['port'], $KOKEN_DATABASE['socket']);
$db_link->select_db($KOKEN_DATABASE['database']);
if ($query)
{
$result = $db_link->query($query);
if ($result)
{
while ($row = $result->fetch_assoc()) {
$results[] = $row;
}
$result->close();
}
}
$db_link->close();
}
else
{
mysql_connect($KOKEN_DATABASE['hostname'], $KOKEN_DATABASE['username'], $KOKEN_DATABASE['password']);
mysql_select_db($KOKEN_DATABASE['database']);
if ($query)
{
$result = mysql_query($query);
if ($result)
{
while ($row = mysql_fetch_assoc($result)) {
$results[] = $row;
}
}
}
mysql_close();
}
$requests = array();
foreach($results as $image) {
$full_id = str_pad($image['id'], 6, 0, STR_PAD_LEFT);
$parts = str_split($full_id, $split_length = 3);
$url = $base.'/'.$parts[0].'/'.$parts[1];
list($name, $ext) = explode('.', $image['filename']);
$size = array(
'tiny',
'small',
'medium',
'medium_large',
'large',
'xlarge',
'huge'
);
foreach($size as $s) {
$requests[] = $url.'/'.$name.','.$s.'.'.$image['file_modified_on'].'.'.$ext;
if ($retina) {
$requests[] = $url.'/'.$name.','.$s.'.2x.'.$image['file_modified_on'].'.'.$ext;
}
}
}
echo json_encode($requests);
@MHerbst
Copy link

MHerbst commented Jun 14, 2014

Thanks for sharing this script. Works really good. In my installation I had to modify the mysqli_connect statement in line 21 because port and socket where not stored in KOKEN_DATABASE. This caused a php error and request.php received an invalid json buffer. After commenting out the last two parameters it worked fine.

@laklare
Copy link

laklare commented Feb 23, 2015

I was forced to modify one line to work with the latest version of Koken, which doesn't set $KOKEN_DATABASE in the database.php file. Instead, I changed the line with the require command in cache.url.php as follows:

$KOKEN_DATABASE=require($content . $ds . 'configuration' . $ds . 'database.php');

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