Skip to content

Instantly share code, notes, and snippets.

@frostbitten
Created April 18, 2016 16:52
Show Gist options
  • Save frostbitten/1f58c32f18d39caddaa8f460c43ea703 to your computer and use it in GitHub Desktop.
Save frostbitten/1f58c32f18d39caddaa8f460c43ea703 to your computer and use it in GitHub Desktop.
custom uri and file externalizer for 0.3.2 dev
# i should be placed within "public/user"
php_flag engine off
add these config vars:
in defines.php:
define('UserFrosting\USERFILES_DIR', realpath(__DIR__ . '/../userfiles'));
you'll also want to create that directory and seed it with some test files.
in app/config/development.php
'common_salt' => "type a random string of text here",
in your public folder, create a "user" folder and add the .htaccess file from this gist
{
"name": "userfrosting-plugin-externalizer",
"type": "project",
"description": "Integrates a custom URI handler for externalizing non-public resources",
"license" : "MIT",
"require-dev": {
},
"require": {
"sabre/uri": "~1"
}
}
<?php
namespace UserFrosting;
global $app;
class simpleURI {
private $_app;
private $internal_schemes;
public function __construct($app) {
$this->_app = $app;
$this->internal_schemes = [
"theme" => [
'internal' => APP_DIR . DIRECTORY_SEPARATOR . TEMPLATE_DIR_NAME. DIRECTORY_SEPARATOR .$this->_app->user->getTheme(),
'external' => '/theme/'.$this->_app->user->getTheme()
],
"user" => [
'internal' => USERFILES_DIR . DIRECTORY_SEPARATOR . $this->_app->user->id,
'external' => '/user/'.$this->_app->user->id.hash('adler32',$this->_app->config('common_salt').$this->_app->user->username)
]
];
}
public function externalize($uri){
$parsed = $this->parse($uri);
$parsed['schemeless'] = explode('//',$uri)[1];
if(!file_exists('./' . $this->_app->config('uri')['public_relative'].$this->internal_schemes[$parsed['scheme']]['external'] . '/' . $parsed['schemeless'] )){
error_log('unable to find external uri: '.'./' . $this->_app->config('uri')['public_relative'].$this->internal_schemes[$parsed['scheme']]['external'] . '/' . $parsed['schemeless'] );
if(!file_exists($this->internal_schemes[$parsed['scheme']]['internal'] . '/' . $parsed['schemeless'] )){
error_log('unable to find internal uri: '.$this->internal_schemes[$parsed['scheme']]['internal'] . '/' . $parsed['schemeless'] );
}else{
error_log('found internal url:'.$this->internal_schemes[$parsed['scheme']]['internal'] . '/' . $parsed['schemeless'] );
$structure = explode('/', $this->internal_schemes[$parsed['scheme']]['external'] . '/' .$parsed['schemeless']);
$depth = 0;
$place = './' . $this->_app->config('uri')['public_relative'];
foreach($structure as $folder){
$depth++;
if($depth != count($structure)){
$place .= '/' . $folder;
if(!is_dir($place))
mkdir($place);
}
}
copy(
$this->internal_schemes[$parsed['scheme']]['internal'] . '/' . $parsed['schemeless'],
'./' . $this->_app->config('uri')['public_relative'] .$this->internal_schemes[$parsed['scheme']]['external'] . '/' . $parsed['schemeless']
);
}
}else{
error_log('found external url:'.$this->internal_schemes[$parsed['scheme']]['external'] . '/' . $parsed['schemeless'] );
// error_log('externalize test: '.print_r(['uri'=>$uri,'scheme'=>$this->internal_schemes[$parsed['scheme']]],1));
}
}
public function resolve($basePath, $newPath) { return \Sabre\Uri\resolve($basePath, $newPath);}
public function normalize($uri) { return \Sabre\Uri\normalize($uri);}
public function parse($uri) { return \Sabre\Uri\parse($uri);}
public function build(array $parts) { return \Sabre\Uri\build($parts) ;}
public function split($path) { return \Sabre\Uri\split($path) ;}
}
//test below
$app->hook('slim.after', function() use ($app){
$go = new simpleURI($app);
$go->externalize("user://cat.jpg");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment