Skip to content

Commit

Permalink
Added simple cache administration script
Browse files Browse the repository at this point in the history
Fixes in qgisproxy
Update phpFastCache to 3.0.11
  • Loading branch information
uprel committed Sep 21, 2015
1 parent a0ff9f0 commit 4652add
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 30 deletions.
54 changes: 54 additions & 0 deletions admin/cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

require '../vendor/autoload.php';
require_once("settings.php");

//TODO custom settings
//$config = array(
// //"htaccess" => false,
// //"path" => "/var/tmp", // default path for files
// //"securityKey" => "", // default will good. It will create a path by PATH/securityKey
// "fallback" => "files"
//);
//$cache = phpFastCache("files", $config);
$cache = phpFastCache("files");

$test = $cache->fallback;

if (isset($_GET["clear"])) {

$clear = $_GET["clear"];
if ($clear == 'all') {
$cache->clean();
}
else {
$cache->delete($clear);
}
header("Location:" . $_SERVER['SCRIPT_NAME']);
} else {
echo '<a href="' . $_SERVER['SCRIPT_NAME'] . '?clear=all">Clear all cache</a>';
}
//TODO
if (isset($_GET["view"])) {
$view = $_GET["view"];
}

// Stats
echo "<pre>";
$stats = $cache->stats();
$path = $cache->getPath();

print_r("Cache size in bytes: ".$stats["size"]); //size of cached objects in bytes
print_r("\nPath to store files: ".$path);
print_r("\nCache content (key, size, write time):");

foreach ($stats["data"] as $key => $el) {
$cmd_clear = '<a href="' . $_SERVER['SCRIPT_NAME'] . '?clear='.$key.'">clear </a>';
//$cmd_view = '<a href="' . $_SERVER['SCRIPT_NAME'] . '?view='.$key.'">view </a>';
print_r('</br>'.$cmd_clear.'<b>'.$key.'</b>,'.$el['size'].','.date('c',$el['write_time']));
}

//TODO bug, doesn't work on files
//print_r($cache->systemInfo());

echo "</pre>";
2 changes: 1 addition & 1 deletion admin/class.Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ private function showPageLoggedIn()


echo "<h3> PHP List All Session Variables</h3>";
echo session_id()."<br/>";
echo "sess_id: ".session_id()."<br/><br/>";
foreach ($_SESSION as $key => $val)
echo $key . ": " . $val . "<br/><br/>";

Expand Down
39 changes: 34 additions & 5 deletions admin/qgisproxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
$query_arr = array();
parse_str($query,$query_arr);

//we have to extend map parameter with path to projects
//we have to extend map parameter with path to projects, but first store it into own variable and remove .qgs
$map = "";
if(strpos($query_arr["map"],".") === false) {
$map = $query_arr["map"];
}
else {
$map = explode(".",$query_arr["map"])[0];
}
$query_arr["map"] = PROJECT_PATH . $query_arr["map"];

$client = new Client();
Expand All @@ -32,6 +39,22 @@ function QgisServerRequest($client, $request, $query_arr)
return $response;
}

//Taken from helpers.php
function normalize ($string) {
$table = array(
'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b',
'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r', '.'=>''
);

return strtr($string, $table);
}

try {

$new_request = new Request('GET', QGISSERVERURL);
Expand All @@ -49,21 +72,22 @@ function QgisServerRequest($client, $request, $query_arr)
$contentType = null;
$cacheKey = null;
$contentLength = 0;
$s = "_x_"; //separator for key generating
switch ($query_arr["REQUEST"]) {
case "GetProjectSettings":
$cacheKey = $query_arr["REQUEST"].".".$_SESSION['project'];
$cacheKey = $map.$s."XML".$s.$query_arr["REQUEST"];
$contentType = "text/xml";
break;
case "GetLegendGraphics":
$cacheKey = $query_arr["REQUEST"].".".$_SESSION['project']. "." . $_REQUEST['LAYERS'];
$cacheKey = $map. $s."PNG".$s .$query_arr["REQUEST"].$s. normalize($_REQUEST['LAYERS']);
$contentType = "image/png";
break;
case "GetFeatureInfo":
//only caching large responses (whole tables)
$count = $query_arr['FEATURE_COUNT'];
if(is_numeric($count)) {
if(intval($count)>100) {
$cacheKey=$query_arr["REQUEST"].".".$_SESSION['project']. "." . $_REQUEST['FILTER'];
$cacheKey= $map. $s."XML".$s .$query_arr["REQUEST"].$s. normalize($_REQUEST['FILTER']);
}
}
break;
Expand All @@ -80,7 +104,12 @@ function QgisServerRequest($client, $request, $query_arr)
$contentLength = $response->getHeaderLine('Content-Length');
$content = $response->getBody()->__toString();

$cache->set($cacheKey,$content);
if($response->getStatusCode() == 200) {
$cache->set($cacheKey, $content);
}
else {
throw new Exception\ClientException($content,$new_request);
}
}
}
else {
Expand Down
12 changes: 6 additions & 6 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,23 @@
},
{
"name": "phpfastcache/phpfastcache",
"version": "3.0.10",
"version_normalized": "3.0.10.0",
"version": "3.0.11",
"version_normalized": "3.0.11.0",
"source": {
"type": "git",
"url": "https://github.com/khoaofgod/phpfastcache.git",
"reference": "4fbc2a8a7b0a7589cd4ca0e750c711616ba03699"
"reference": "5ee8aadcf04921f1b6f5742d2610973a5d8e9fe0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/khoaofgod/phpfastcache/zipball/4fbc2a8a7b0a7589cd4ca0e750c711616ba03699",
"reference": "4fbc2a8a7b0a7589cd4ca0e750c711616ba03699",
"url": "https://api.github.com/repos/khoaofgod/phpfastcache/zipball/5ee8aadcf04921f1b6f5742d2610973a5d8e9fe0",
"reference": "5ee8aadcf04921f1b6f5742d2610973a5d8e9fe0",
"shasum": ""
},
"require": {
"php": ">=5.1.0"
},
"time": "2015-08-25 16:47:00",
"time": "2015-09-14 18:49:38",
"type": "library",
"installation-source": "dist",
"autoload": {
Expand Down
13 changes: 6 additions & 7 deletions vendor/phpfastcache/phpfastcache/phpfastcache/3.0.0/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function set($keyword, $value = "", $time = 0, $option = array() ) {
}
$object = array(
"value" => $value,
"write_time" => @date("U"),
"write_time" => time(),
"expired_in" => $time,
"expired_time" => @date("U") + (Int)$time,
"expired_time" => time() + (Int)$time,
);

return $this->driver_set($keyword,$object,$time,$option);
Expand Down Expand Up @@ -114,7 +114,7 @@ function increment($keyword, $step = 1 , $option = array()) {
return false;
} else {
$value = (Int)$object['value'] + (Int)$step;
$time = $object['expired_time'] - @date("U");
$time = $object['expired_time'] - time();
$this->set($keyword,$value, $time, $option);
return true;
}
Expand All @@ -126,7 +126,7 @@ function decrement($keyword, $step = 1 , $option = array()) {
return false;
} else {
$value = (Int)$object['value'] - (Int)$step;
$time = $object['expired_time'] - @date("U");
$time = $object['expired_time'] - time();
$this->set($keyword,$value, $time, $option);
return true;
}
Expand All @@ -140,7 +140,7 @@ function touch($keyword, $time = 300, $option = array()) {
return false;
} else {
$value = $object['value'];
$time = $object['expired_time'] - @date("U") + $time;
$time = $object['expired_time'] - time() + $time;
$this->set($keyword, $value,$time, $option);
return true;
}
Expand Down Expand Up @@ -249,8 +249,7 @@ function __set($name, $v) {
}

public function __call($name, $args) {
$str = implode(",",$args);
eval('return $this->instant->$name('.$str.');');
return call_user_func_array( array( $this->instant, $name ), $args );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private function getFilePath($keyword, $skip = false) {

} elseif(!is_writeable($path)) {
if(!chmod($path,$this->__setChmodAuto())) {
die("PLEASE CHMOD ".$this->getPath()." - 0777 OR ANY WRITABLE PERMISSION! MAKE SURE PHP/Apache/WebServer have Write Permission");
throw new Exception("PLEASE CHMOD ".$this->getPath()." - 0777 OR ANY WRITABLE PERMISSION!",92);
}
}
}
Expand Down Expand Up @@ -147,6 +147,7 @@ function driver_stats($option = array()) {

$total = 0;
$removed = 0;
$content = array();
while($file=@readdir($dir)) {
if($file!="." && $file!=".." && is_dir($path."/".$file)) {
// read sub dir
Expand All @@ -160,6 +161,14 @@ function driver_stats($option = array()) {
$file_path = $path."/".$file."/".$f;
$size = @filesize($file_path);
$object = $this->decode($this->readfile($file_path));

if(strpos($f,".") === false) {
$key = $f;
}
else {
$key = explode(".", $f)[0];
}
$content[$key] = array("size"=>$size,"write_time"=>$object["write_time"]);
if($this->isExpired($object)) {
@unlink($file_path);
$removed = $removed + $size;
Expand All @@ -176,6 +185,7 @@ function driver_stats($option = array()) {
"Removed" => $removed,
"Current" => $res['size'],
);
$res["data"] = $content;
return $res;
}

Expand Down Expand Up @@ -233,7 +243,7 @@ function driver_isExisting($keyword) {

function isExpired($object) {

if(isset($object['expired_time']) && @date("U") >= $object['expired_time']) {
if(isset($object['expired_time']) && time() >= $object['expired_time']) {
return true;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function driver_set($keyword, $value = "", $time = 300, $option = array() ) {
$stm->execute(array(
":keyword" => $keyword,
":object" => $this->encode($value),
":exp" => @date("U") + (Int)$time,
":exp" => time() + (Int)$time,
));

return true;
Expand All @@ -212,7 +212,7 @@ function driver_set($keyword, $value = "", $time = 300, $option = array() ) {
$stm->execute(array(
":keyword" => $keyword,
":object" => $this->encode($value),
":exp" => @date("U") + (Int)$time,
":exp" => time() + (Int)$time,
));
} catch (PDOException $e) {
return false;
Expand Down Expand Up @@ -268,7 +268,7 @@ function driver_get($keyword, $option = array()) {
}

function isExpired($row) {
if(isset($row['exp']) && @date("U") >= $row['exp']) {
if(isset($row['exp']) && time() >= $row['exp']) {
return true;
}

Expand All @@ -280,7 +280,7 @@ function deleteRow($row) {
$stm = $this->db($row['keyword'])->prepare("DELETE FROM `caching` WHERE (`id`=:id) OR (`exp` <= :U) ");
$stm->execute(array(
":id" => $row['id'],
":U" => @date("U"),
":U" => time(),
));
} catch (PDOException $e) {
return false;
Expand All @@ -292,7 +292,7 @@ function driver_delete($keyword, $option = array()) {
$stm = $this->db($keyword)->prepare("DELETE FROM `caching` WHERE (`keyword`=:keyword) OR (`exp` <= :U)");
$stm->execute(array(
":keyword" => $keyword,
":U" => @date("U"),
":U" => time(),
));
} catch (PDOException $e) {
return false;
Expand Down Expand Up @@ -324,7 +324,7 @@ function driver_stats($option = array()) {

$stm = $PDO->prepare("DELETE FROM `caching` WHERE `exp` <= :U");
$stm->execute(array(
":U" => @date("U"),
":U" => time(),
));

$PDO->exec("VACUUM;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class phpFastCache {
"timeout" => ""
),

"ssdb" => array(
"host" => "127.0.0.1",
"port" => 8888,
"password" => "",
"timeout" => ""
),

"extensions" => array(),
);

Expand Down Expand Up @@ -180,7 +187,7 @@ public static function getPath($skip_create_path = false, $config) {
@chmod($full_path,self::__setChmodAuto($config));
}
if(!@file_exists($full_path) || !@is_writable($full_path)) {
die("Sorry, Please create ".$full_path." and SET Mode 0777 or any Writable Permission!");
throw new Exception("PLEASE CREATE OR CHMOD ".$full_path." - 0777 OR ANY WRITABLE PERMISSION!",92);
}
}

Expand Down Expand Up @@ -231,7 +238,7 @@ protected static function htaccessGen($path, $create = true) {
chmod($path,0777);
}
catch(Exception $e) {
die(" NEED WRITEABLE ".$path);
throw new Exception("PLEASE CHMOD ".$path." - 0777 OR ANY WRITABLE PERMISSION!",92);
}
}
if(!@file_exists($path."/.htaccess")) {
Expand All @@ -242,7 +249,7 @@ protected static function htaccessGen($path, $create = true) {

$f = @fopen($path."/.htaccess","w+");
if(!$f) {
die(" CANT CREATE HTACCESS TO PROTECT FOLDER - PLZ CHMOD 0777 FOR ".$path);
throw new Exception("PLEASE CHMOD ".$path." - 0777 OR ANY WRITABLE PERMISSION!",92);
}
fwrite($f,$html);
fclose($f);
Expand Down
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0.beta2

0 comments on commit 4652add

Please sign in to comment.