Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokanfar/39e9bbca3bb39fbd560c to your computer and use it in GitHub Desktop.
Save mokanfar/39e9bbca3bb39fbd560c to your computer and use it in GitHub Desktop.
<?php
require_once('PhpCopy.php');
function rip_tags($file) {
$string = file_get_contents($file);
$regex = <<<'END'
/
(
(?: [\x00-\x7F] # single-byte sequences 0xxxxxxx
| [\xC0-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
| [\xE0-\xEF][\x80-\xBF]{2} # triple-byte sequences 1110xxxx 10xxxxxx * 2
| [\xF0-\xF7][\x80-\xBF]{3} # quadruple-byte sequence 11110xxx 10xxxxxx * 3
){1,100} # ...one or more times
)
| . # anything else
/x
END;
$string = preg_replace($regex, '$1', $string);
$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '#', $string);
$string = utf8_decode($string);
// ----- remove control characters -----
$string = str_replace("\r", ' ', $string); // --- replace with empty space
$string = str_replace("\n", '#', $string); // --- replace with space
$string = str_replace("\t", ' ', $string);
$string = preg_replace ("/<script[^>]*>(.*?)<\/script>/", '', $string); // --- replace with space
$string = preg_replace ('/<[^>]*>/', '', $string);
$string = preg_replace ('/&laquo;Back...Home/', '', $string);
$string = preg_replace ('/&nbsp;/', ' ', $string);
$string = preg_replace ('/&rsquo;/', '\'', $string);
$string = preg_replace ('/&quot;/', '\"', $string);
$string = trim(preg_replace('/ {2,}/', ' ', $string));
$string = preg_replace('/#(#+|\s?#)+/', '##', $string);
$string = str_replace("#", "\n", $string);
$string = str_replace(" \n", "\n", $string);
$string = preg_replace('/Dislcosure.Notices.*Family.Based.Immigration/', '', $string);
$string = str_replace(" | U.S. Citizenship | Renewal of Green Card | Certification of Citizenship | Marriage Over the Internet ", "", $string);
$string = str_replace(" Services, 555 Blvd. ", "", $string);
$string = str_replace("7, phone:", "", $string);
// ----- remove multiple spaces -----
put_in_file($string,$file);
}
function put_in_file ($text,$filename) {
$fileContents = file_get_contents($filename);
$tags = get_meta_tags($filename, $use_include_path = true);
preg_match('/<title>(.+)<\/title>/',$fileContents,$matches);
$title = $matches[1];
$description = $tags['description'];
$keywords = $tags['keywords'];
$prepend = "---\nlayout: default\nTitle: " . $title . "\nMetaDescription: " . $description . "\nMetaKeywords: " . $keywords . "\n---\n\n";
$text = $prepend . $text;
file_put_contents($filename, $text);
return 0;
}
// --------------------------------------------------------------
$source = "./template";
$dest = "./Done";
$dir = "./Done/*/**";
if (!is_dir($dest)) {
echo "Copying over template folder.\n";
}
else {
system('rm -rf ' . escapeshellarg($dest));
echo "deleted existing done folder.\n";
}
smartCopy($source, $dest);
foreach(glob($dir) as $file)
{
rip_tags($file);
}
echo "done";
exit(0);
?>
<?php
/**
* Copy file or folder from source to destination, it can do
* recursive copy as well and is very smart
* It recursively creates the dest file or directory path if there weren't exists
* Situtaions :
* - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination
* - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it
* - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest
* - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest
* - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name
* - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name
* @todo
* - Should have rollback technique so it can undo the copy when it wasn't successful
* - Auto destination technique should be possible to turn off
* - Supporting callback function
* - May prevent some issues on shared enviroments : http://us3.php.net/umask
* @param $source //file or folder
* @param $dest ///file or folder
* @param $options //folderPermission,filePermission
* @return boolean
*/
function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755))
{
$result=false;
if (is_file($source)) {
if ($dest[strlen($dest)-1]=='/') {
if (!file_exists($dest)) {
cmfcDirectory::makeAll($dest,$options['folderPermission'],true);
}
$__dest=$dest."/".basename($source);
} else {
$__dest=$dest;
}
$result=copy($source, $__dest);
chmod($__dest,$options['filePermission']);
} elseif(is_dir($source)) {
if ($dest[strlen($dest)-1]=='/') {
if ($source[strlen($source)-1]=='/') {
//Copy only contents
} else {
//Change parent itself and its contents
$dest=$dest.basename($source);
@mkdir($dest);
chmod($dest,$options['filePermission']);
}
} else {
if ($source[strlen($source)-1]=='/') {
//Copy parent directory with new name and all its content
@mkdir($dest,$options['folderPermission']);
chmod($dest,$options['filePermission']);
} else {
//Copy parent directory with new name and all its content
@mkdir($dest,$options['folderPermission']);
chmod($dest,$options['filePermission']);
}
}
$dirHandle=opendir($source);
while($file=readdir($dirHandle))
{
if($file!="." && $file!="..")
{
if(!is_dir($source."/".$file)) {
$__dest=$dest."/".$file;
} else {
$__dest=$dest."/".$file;
}
//echo "$source/$file ||| $__dest<br />";
$result=smartCopy($source."/".$file, $__dest, $options);
}
}
closedir($dirHandle);
} else {
$result=false;
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment