Skip to content

Instantly share code, notes, and snippets.

@litzinger
Created June 29, 2012 20:21
Show Gist options
  • Save litzinger/3020414 to your computer and use it in GitHub Desktop.
Save litzinger/3020414 to your computer and use it in GitHub Desktop.
ExpressionEngine file upload paths swapping method
<?php
/*
Usage
$mystring = $this->swap_file_tokens('{filedir_3}path/to/image.jpg');
echo $mystring;
returns: 'http://mysite.com/images/path/to/image.jpg';
$mystring = $this->swap_file_tokens('{filedir_3}path/to/image.jpg', 'server_path');
echo $mystring;
returns: '/var/www/mysite.com/images/path/to/image.jpg';
$mystring = $this->swap_file_tokens('http://mysite.com/images/path/to/image.jpg');
echo $mystring;
returns: '{filedir_3}path/to/image.jpg';
$mystring = $this->swap_file_tokens('/var/www/mysite.com/images/path/to/image.jpg');
echo $mystring;
returns: '{filedir_3}path/to/image.jpg';
*/
private function get_upload_prefs($group_id = NULL, $id = NULL)
{
if ( ! isset($this->EE->session->cache[__CLASS__]['upload_prefs']))
{
if (version_compare(APP_VER, '2.4', '>='))
{
$this->EE->load->model('file_upload_preferences_model');
return $this->EE->file_upload_preferences_model->get_file_upload_preferences($group_id, $id);
}
if (version_compare(APP_VER, '2.1.5', '>='))
{
$this->EE->load->model('file_upload_preferences_model');
$result = $this->EE->file_upload_preferences_model->get_upload_preferences($group_id, $id);
}
else
{
$this->EE->load->model('tools_model');
$result = $this->EE->tools_model->get_upload_preferences($group_id, $id);
}
$this->EE->session->cache[__CLASS__]['upload_prefs'] = $result->result_array();
}
return $this->EE->session->cache[__CLASS__]['upload_prefs'];
}
private function swap_file_tokens($str, $which = 'url')
{
$prefs = $this->get_upload_prefs();
$upload_paths = array();
if ( ! isset($this->EE->session->cache[__CLASS__]['upload_paths']))
{
foreach ($prefs as $dir => $data)
{
$upload_paths[$dir] = array(
'server_path' => $data['server_path'],
'url' => $data['url']
);
}
$this->EE->session->cache[__CLASS__]['upload_paths'] = $upload_paths;
}
$paths = $this->EE->session->cache[__CLASS__]['upload_paths'];
// Simple search for {filedir_N} tokens and replace with full url (default) or server_path
if (preg_match('/\{filedir_(\d+)\}/', $str, $matches))
{
if ($matches && is_numeric($matches[1]))
{
return preg_replace('/\{filedir_(\d+)\}/', $paths[$matches[1]][$which], $str);
}
}
// No tokens found? Then search for full url or server_path and swap back to a token
// See if its a server_path
if (substr($str, 1) == '/')
{
foreach ($paths as $dir => $path)
{
// If the string contains a server path
if (strstr($str, $path['server_path']))
{
// Replace the path with the token.
return str_replace($path['server_path'], '{filedir_'. $dir .'}', $str);
}
}
}
// We have a possible url
if (substr($str, 4) == 'http')
{
foreach ($paths as $dir => $path)
{
// If the string contains a server path
if (strstr($str, $path['url']))
{
// Replace the path with the token.
return str_replace($path['url'], '{filedir_'. $dir .'}', $str);
}
}
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment