Skip to content

Instantly share code, notes, and snippets.

@bwonur
Created January 7, 2020 07:26
Show Gist options
  • Save bwonur/5596a0efb20c3695e64ec815c9661236 to your computer and use it in GitHub Desktop.
Save bwonur/5596a0efb20c3695e64ec815c9661236 to your computer and use it in GitHub Desktop.
PHP .htaccess CRUD classes
<?php
/**
* .htaccess writer CRUD class
*
* @author Lawrence Cherone
* @version 1.00
*/
class htaccessCRUD
{
public $file;
private $handle;
/**
* construct loads [or creates] the .htaccess file
*
* @param string $file
*/
function __construct($file)
{
$this->file = $file;
try {
//create file if not exsist
if(!file_exists($this->file))
{
if (!$this->handle = @fopen($this->file, 'w'))
{
throw new Exception('Cannot create file');
}
flock($this->handle,LOCK_EX);
fwrite($this->handle,
'# .htaccess generated by htaccessCRUD '.
'Class by Lawrence Cherone'.PHP_EOL);
flock($this->handle,LOCK_UN);
fclose($this->handle);
}
//check file is readable
if (!is_readable($this->file))
{
throw new Exception('File is not readable');
}
//check file is writable
if (!is_writable($this->file))
{
throw new Exception('File is not writable');
}
} catch (Exception $e)
{
exit('Error: '.$e->getMessage());
}
}
/**
* Create or update an entry in the .htaccess file
*
* @param string $data
* @param string $delim
*/
function create($data = PHP_EOL, $delim = '#')
{
//first check for exsisting then update
if($this->read($delim))
{
$this->update($data, $delim);
} else
//create new entry
{
$data = PHP_EOL.'# '.$delim.PHP_EOL.$data.PHP_EOL.'# \\'.$delim;
file_put_contents($this->file, $data, FILE_APPEND);
}
}
/**
* Read entry from .htaccess file
*
* @param string $delim
* @return mixed (bool|string)
*/
function read($delim = '#')
{
$file = file_get_contents($this->file);
$delim = preg_quote($delim);
if (preg_match("/#\s$delim\s(.*?)\s#\s\\\\$delim/s", $file, $matches))
{
return trim($matches[1]);
} else
{
return false;
}
}
/**
* Update entry in .htaccess file
*
* @param string $data
* @param string $delim
*/
function update($data = PHP_EOL, $delim = '#')
{
$data = str_replace(array('$1','$2','$3','$4','$5'),
array('\$1','\$2','\$3','\$4','\$5'), $data);
$delim = preg_quote($delim);
file_put_contents($this->file,
trim(preg_replace("/#\s$delim\s(.*?)\s#\s\\\\$delim/s",
'# '.$delim.PHP_EOL.$data.PHP_EOL.'# \\'.$delim,
file_get_contents($this->file))));
}
/**
* Delete entry from .htaccess file
*
* @param string $delim
* @return bool
*/
function delete($delim = '#')
{
$file = file_get_contents($this->file);
$delim = preg_quote($delim);
if (preg_match("/#\s$delim\s(.*?)\s#\s\\\\$delim/s", $file, $matches))
{
file_put_contents($this->file,
str_replace(PHP_EOL.$matches[0].PHP_EOL, '', $file));
return true;
} else
{
return false;
}
}
}
$htaccess = new htaccessCRUD('./dummy.htaccess');
/**
* Usage example
*/
//$htaccess->create('Data to go in htaccess', 'the_key');
//$htaccess->read('the_key');
//$htaccess->update('Data to go in htaccess', 'the_key');
//$htaccess->delete('the_key');
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
foreach($_POST as $key => $value)
{
$htaccess->update($value, $key);
}
}
?>
<h1>.htaccess writer class</h1>
<form method="POST" action="">
<p>mod_rewrite</p>
<p><textarea rows="10" name="mod_rewrite" cols="55"><?php echo $htaccess->read('mod_rewrite'); ?></textarea></p>
<p>Blocked IP's</p>
<p><textarea rows="10" name="custom_blocked" cols="55"><?php echo $htaccess->read('custom_blocked'); ?></textarea></p>
<p>Test</p>
<p><textarea rows="10" name="test" cols="55"><?php echo $htaccess->read('test'); ?></textarea></p>
<p><input type="submit" value="Submit"></p>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment