Skip to content

Commit

Permalink
clear cache link
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickael Desfrenes committed Apr 25, 2011
1 parent 89ce035 commit c884563
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
4 changes: 3 additions & 1 deletion applications/cms/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static function getRoutes()
'/admin/cms/savepage/' => 'cms/admin/savepage',
'/admin/cms/loadversion/:num/:num' => 'cms/admin/loadpageversion/$1/$2',
'/admin/cms/commentpublishing/' => 'cms/admin/togglecommentpublishing',
'/admin/cms/clearcache/' => 'cms/admin/clearcache',
'/cms/postcomment/' => 'cms/index/postcomment',
'/admin/cms/elfinderconnector/' => 'cms/admin/elfinderconnector',
'/admin/cms/elfinderstrings/' => 'cms/admin/elfinderstrings'
Expand Down Expand Up @@ -100,7 +101,8 @@ public static function getTranslations($lang_id)
'Display' => 'Afficher',
'Indexing' => 'Indexation',
'Analyzer' => 'Analyseur',
'Apply to descendants' => 'Appliquer aux descendants'
'Apply to descendants' => 'Appliquer aux descendants',
'Clear cache' => 'Vider le cache'
);
}
return array();
Expand Down
11 changes: 11 additions & 0 deletions applications/cms/controllers/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ public function renamepageAction()
}
}

public function clearcacheAction()
{
Auth::mustHave('cms.update');
foreach(Page::find() as $page)
{
$page->deleteAllCache();
}
header('content-type: application/json');
die(json_encode(array('status' => 'ok')));
}

public function savepageAction()
{
Auth::mustHave('cms.update');
Expand Down
2 changes: 2 additions & 0 deletions applications/cms/controllers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function norouteAction(\Exception $e)
$this->assignToLayout('author', $page->getAuthor() ?: '');

$output = $this->render($template_path, array('page' => $page));

if($page->getMake_static_file())
{
$file_path = $page->makeStaticFilePath();
Expand All @@ -55,6 +56,7 @@ public function norouteAction(\Exception $e)
file_put_contents($file_path, $output);
}
}

die($output);
}
else
Expand Down
42 changes: 28 additions & 14 deletions applications/cms/models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function setTableDefinition()
));
$this->addColumn('make_static_file', array(
'type' => 'boolean',
'default' => '0'
'default' => '1'
));
}

Expand Down Expand Up @@ -338,38 +338,48 @@ public function deleteAllCache()
public function deleteStaticFile()
{
$path = $this->makeStaticFilePath();
if(is_file($path))
if(is_file($path) && !$this->isForbiddenPath($path))
{
unlink($path);
}
if(substr($path, -11) == '/index.html')
{
if(is_dir(dirname($path)))
$dir = dirname($path);
if(is_dir($dir) && !$this->isForbiddenPath($dir))
{
rmdir(dirname($path));
rmdir($dir);
}
}
return true;
}

private function stripFinalSlash($in)
{
if(substr($in, -1) == '/')
{
$in = substr($in, 0, -1);
}
return $in;
}

public function makeStaticFilePath()

private function isForbiddenPath($path)
{
$path = $this->stripFinalSlash($path);
$s = \shozu\Shozu::getInstance();
$forbidden_paths = array(
$this->stripFinalSlash($s->document_root),
$s->document_root . 'index.php',
$s->document_root . 'static',
$s->document_root . 'themes',
$s->document_root . 'upload'
);
$file_path = $s->document_root . substr($this->getUrl(),1);
foreach ($forbidden_paths as $forbidden)
{
if(strpos($file_path,$forbidden))
{
throw new \Exception('forbidden path');
}
}
);
return in_array($path, $forbidden_paths);
}

public function makeStaticFilePath()
{
$file_path = \shozu\Shozu::getInstance()->document_root . substr($this->getUrl(),1);
if(substr($file_path, -1) == '/')
{
$file_path .= 'index.html';
Expand All @@ -378,6 +388,10 @@ public function makeStaticFilePath()
{
$file_path .= '/index.html';
}
if($this->isForbiddenPath($file_path))
{
throw new \Exception('forbidden path');
}
return $file_path;
}

Expand Down
3 changes: 2 additions & 1 deletion applications/cms/views/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<div style="position: fixed;
top:0px;
right:30px;
width:300px;
width:350px;
height:20px;
overflow: hidden;
border-bottom: 1px solid #cecece;
Expand All @@ -89,6 +89,7 @@
padding:5px;
">
<div style="font-size: 12px;">
<a id="clearcache" href="<?php echo $this->escape($this->url('cms/admin/clearcache')); ?>"><?php echo $this->T('Clear cache'); ?></a> -
<a href="<?php echo $this->escape($this->url('cms/admin/index')); ?>"><?php echo $this->T('Edit site'); ?></a> -
<a href="<?php echo $this->escape($this->url('cms/admin/resources')); ?>"><?php echo $this->T('Manage resources'); ?></a><!--
<a href="<?php echo $this->escape($this->url('cms/admin/users')); ?>"><?php echo $this->T('Manage users'); ?></a>-->
Expand Down
13 changes: 13 additions & 0 deletions applications/cms/views/edit_site.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@
$(this).fadeOut('fast');
$(document).css('cursor','auto');
});
$("a#clearcache").click(function(){
$.post("<?php echo $this->url('cms/admin/clearcache'); ?>", function(data){
if(data.status == 'ok')
{
alert("<?php echo $this->T('Cache cleared.'); ?>")
}
else
{
alert("error.");
}
});
return false;
});
Date.format = 'yyyy-mm-dd 00:00:00';
$.dpText = {
TEXT_PREV_YEAR : '<?php echo $this->T('Previous year'); ?>',
Expand Down

0 comments on commit c884563

Please sign in to comment.