Skip to content

Instantly share code, notes, and snippets.

@loulou2u
Last active August 29, 2015 14:21
Show Gist options
  • Save loulou2u/cfd37b946e0cffbad72a to your computer and use it in GitHub Desktop.
Save loulou2u/cfd37b946e0cffbad72a to your computer and use it in GitHub Desktop.
Restrict Access to Files
//The custom app below redirects files tagged as "private" to a .htaccess protected directory
<?php
$_LW->REGISTERED_APPS['protect_files']=array(
'title'=>'Protect Files',
'handlers'=>array('onLoad')
);
class LiveWhaleApplicationProtectFiles {
public function onLoad() {
global $_LW;
// if this is a LiveURL files request
if (!empty($GLOBALS['LIVE_URL']['REQUEST_URI']) && strpos($GLOBALS['LIVE_URL']['REQUEST_URI'], '/live/files/')===0) {
// get request info
$request=explode('-', $GLOBALS['LIVE_URL']['REQUEST'][0]);
if (is_numeric($request[0])) {
//query database for files tagged "private"
if ($res2=$_LW->dbo->query('select', '1', 'livewhale_files', 'livewhale_files.id='.(int)$request[0])->innerJoin('livewhale_tags2any', 'livewhale_tags2any.id2=livewhale_files.id AND livewhale_tags2any.type="files"')->innerJoin('livewhale_tags', 'livewhale_tags.id=livewhale_tags2any.id1 AND livewhale_tags.title LIKE "%private%"')->firstRow()->run()) {
//redirect to authenticated directory
die(header('Location: /authenticated-directory/files/'.$request[0]));
};
};
};
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment