mirror of
https://github.com/billz/raspap-webgui.git
synced 2023-10-10 13:37:24 +02:00
Add file utility functions
This commit is contained in:
parent
f1b1e96df3
commit
ef09dd0f60
@ -250,6 +250,47 @@ function file_prepend_data($filename, $dataToSave)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a meta value from a file
|
||||
*
|
||||
* @param string $filename
|
||||
* @param string $pattern
|
||||
* @return string
|
||||
*/
|
||||
function file_get_meta($filename, $pattern)
|
||||
{
|
||||
if(file_exists($filename)) {
|
||||
$context = stream_context_create();
|
||||
$file_data = file_get_contents($filename, false, $context);
|
||||
preg_match('/^'.$pattern.'/', $file_data, $matched);
|
||||
return $matched[1];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renames an openvpn client config with the 'filename' header comment
|
||||
*
|
||||
* @param string file
|
||||
* @return boolean
|
||||
*/
|
||||
function file_move_config($file)
|
||||
{
|
||||
if(file_exists($file)) {
|
||||
$file_data = file_get_contents($file);
|
||||
preg_match('/^#\sfilename\s(.*)/i', $file_data, $matched);
|
||||
$renamed = pathinfo($file, PATHINFO_DIRNAME).'/'.
|
||||
$matched[1] .'_'.pathinfo($file, PATHINFO_FILENAME).'.'.
|
||||
pathinfo($file, PATHINFO_EXTENSION);
|
||||
if (!file_exists($renamed)) {
|
||||
$return = system("sudo mv $file $renamed", $return);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function for array_filter
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user