Location: PHPKode > projects > Rumba XML > narod.php
<?php
/*
            _______________________________________
   ________|                                       |_________
  \        |             Rumba Folk 1.0            |        /
   \       |         http://rumbacms.com           |       /
    \      |    Eduard Sesigin  © 2007 - 2010      |      /
    /      |_______________________________________|      \
   /___________)                               (___________\

The script is designed to create a static html site copy on the local
hosting and copy it to a free hosting.

hide@address.com
http://rumbacms.com
Rumba © 2007 - 2010
*/

#--------------------------------------------------------------------------------------------------
$server = 'http://'.$_SERVER[SERVER_NAME].str_replace('narod.php', '', $_SERVER[PHP_SELF]);
include 'data/config.php';
if(!isset($_GET['pass'])) {exit ('No admin!');}
if($_GET['pass']!=md5($adminpass.date("dym"))) {exit ('Wrong pass!');}
#========================
# Change URL
#========================
function ch_url($in_array, $server)
{
	/* Function appends at http:// need and removes external links */
	$out_array = array();
	foreach ($in_array as $item)
		{
		$item= str_replace("href='", '', $item);
		$item= str_replace('href="', '', $item);
		$item= str_replace('href=', '', $item);
		if(strpos($item, 'http:')===false)
			{$item=$server.$item;}
		if(strpos($item, $server)!==false)
			{$out_array[$item]=$item;} //print $item.'<br>';
		}
	sort($out_array);	
	return $out_array;
}
#========================
# Get URL
#========================
function get_url($in_url, $all_url, $server, $level=0)
{
	/* Function collects all the URLs map */
	$buffer = ''; $ogr = 10000; // Limit on the number of pages for parsing
	$handle = fopen($in_url, "r"); echo "Link ".$in_url." successfully recorded. ";flush(); 
	while (!feof($handle)) 
		{
			$buffer .= fgets($handle, 4096);
		}
	fclose($handle);
	preg_match_all("/href=+[^\.<>]+\.html/", $buffer, $matches);
	$match = ch_url($matches[0], $server);
	//echo '<pre>'.$in_url; print_r ($match); echo '</pre>';
	foreach ($match as $item)
		{
		if (in_array($item, $all_url)==false)
			{
			$all_url[]=$item;
			if($level<=$ogr)
				{
				$all_url=get_url($item, $all_url, $server, $level+1);
				}
			}
		}
	sort($all_url);
	return $all_url;
}
#========================
# Create Pages
#========================
function create_pages($dir, $all_url, $server, $start_url)
{
	/* Function adds the html files in $ dir */
	foreach ($all_url as $item)
		{
		// Download in the page buffer
		$buffer = '';
		$handle = fopen($item, "r");
		while (!feof($handle)) 
			{
				$buffer .= fgets($handle, 4096);
			}
		fclose($handle);
		// Change all URLs to short
		$buffer = str_replace($server, '', $buffer);
		$buffer = str_replace("''", "'./'", $buffer);
		$buffer = str_replace('""', '"./"', $buffer);
		// Write to disk options
		$temp_name = str_replace($server, '', $item);
		$handle = fopen($dir.$temp_name, "w+");
		fwrite($handle, UTF8toCP1251($buffer));
		fclose($handle);
		echo "File ".$dir.$temp_name." successfully recorded. ";flush();
		} 
	copy ($dir.$start_url, $dir.'index.html');
	return true;
}
#========================
# UTF8toCP1251
#========================
function UTF8toCP1251($str){ // by SiMM, $table from http://ru.wikipedia.org/wiki/CP1251
//$str = str_replace("\r\n", "\n", $str);
$str = str_replace('</title>', "</title>\r\n<META HTTP-EQUIV=\"Content-type\" CONTENT=\"text/html; charset=windows-1251\">\r\n", $str);
$str = str_replace('<meta http-equiv="content-type" content="text/html; charset=utf-8" />', "", $str);
  static $table = array("\xD0\x81" => "\xA8", // Ё
                        "\xD1\x91" => "\xB8", // ё
                        // Ukrainian symbols
                        "\xD0\x8E" => "\xA1", // Ў (У)
                        "\xD1\x9E" => "\xA2", // ў (у)
                        "\xD0\x84" => "\xAA", // Є (Э)
                        "\xD0\x87" => "\xAF", // Ї (I..)
                        "\xD0\x86" => "\xB2", // I (I)
                        "\xD1\x96" => "\xB3", // i (i)
                        "\xD1\x94" => "\xBA", // є (э)
                        "\xD1\x97" => "\xBF", // ї (i..)
                        // Chuvash characters
                        "\xD3\x90" => "\x8C", // &#1232; (А)
                        "\xD3\x96" => "\x8D", // &#1238; (Е)
                        "\xD2\xAA" => "\x8E", // &#1194; (С)
                        "\xD3\xB2" => "\x8F", // &#1266; (У)
                        "\xD3\x91" => "\x9C", // &#1233; (а)
                        "\xD3\x97" => "\x9D", // &#1239; (е)
                        "\xD2\xAB" => "\x9E", // &#1195; (с)
                        "\xD3\xB3" => "\x9F", // &#1267; (у)
                       );
  return preg_replace('#([\xD0-\xD1])([\x80-\xBF])#se',
                      'isset($table["$0"]) ? $table["$0"] :
                       chr(ord("$2")+("$1" == "\xD0" ? 0x30 : 0x70))
                      ',
                      $str
                     );
}
#========================
# Copy Pages
#========================
function copy_pages($dir, $all_url, $server, $ftp_server, $ftp_user_name, $ftp_user_pass)
{
	/* Copies the html files on the ftp */
	// установка соединения
	$conn_id = ftp_connect($ftp_server);  if (!$conn_id) {echo "Unable to connect!";}
	// Login with username and password
	$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
	// Check connection
	if ((!$conn_id) || (!$login_result)) { 
			echo "Unable to establish connection with FTP server!";
			echo "Trying to connect to $ftp_server under the name $ftp_user_name!";
			exit; 
		} else {
			echo "Connected to FTP server $ftp_server for user $ftp_user_nam";
		} flush();
		$all_url[]=$server.'index.html';
	foreach ($all_url as $item)
		{	
			$temp_name = str_replace($server, '', $item);
			$fp = fopen($dir.$temp_name, 'r'); 
			// file uploads
			$upload = ftp_put($conn_id, $temp_name, $dir.$temp_name, FTP_ASCII); // FTP_ASCII, либо FTP_BINARY. 
			// test result
			if (!$upload) { 
					echo "Unable to upload file - ".$temp_name." \r\n";
				} else {
					echo "File $temp_name load on $ftp_server ||\r\n";
				}
			fclose ($fp);flush();
		}

	// closing connection
	ftp_close($conn_id); 
	return true;
}
#========================
# CopyImages
#========================
function copy_images($all_pic, $server, $ftp_server, $ftp_user_name, $ftp_user_pass)
{
	/* function copies the html files on ftp */
	// Connect
	$conn_id = ftp_connect($ftp_server);  if (!$conn_id) {echo "Не удалось установить connect! ";}
	// login with username and password
	$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
	// check connection
	if ((!$conn_id) || (!$login_result)) { 
			echo "Unable to establish connection with FTP server! ";
			echo "Trying to connect to $ftp_server under the name $ftp_user_name! ";
			exit; 
		} else {
			echo "Connected to FTP server $ftp_server for user $ftp_user_name";
		} flush(); $core_pwd=ftp_pwd($conn_id); print '='.$core_pwd.'=';
		
						// attempt to create a directory css
						if (@ftp_mkdir($conn_id, 'css')) {
						 echo " A directory css \n ";
						} else {
						 echo " Unable to create directory css\n ";
						}
						// attempt to create a directory of images
						if (@ftp_mkdir($conn_id, 'images')) {
						 echo " Create directory images \n ";
						} else {
						 echo " Unable to create directory images\n ";
						}
						// attempt to create a directory pic
						if (@ftp_mkdir($conn_id, 'pic')) {
						 echo " Create directory pic \n ";
						} else {
						 echo " Unable to create directory pic\n ";
						}
	foreach ($all_pic as $item)
		{	
			$temp_name = str_replace($server, '', $item);
			$temp_dir = explode ('/', $temp_name);
			$fp = fopen($temp_name, 'r'); 
					// file uploads
					if (strpos($temp_name,".js")==true || strpos($temp_name,".css")==true)
						{$upload = ftp_put($conn_id, $temp_name, $temp_name, FTP_ASCII);}
					else{$upload = ftp_put($conn_id, $temp_name, $temp_name, FTP_BINARY);}
					// test result
					if (!$upload) { 
							echo "Unable to upload file - ".$temp_name." \r\n";
						} else {
							echo "File $temp_name injects in $ftp_server ||\r\n";
						}
			fclose ($fp);flush();
		}

	// closing connection
	ftp_close($conn_id); 
	return true;
}
#========================
# Get Images
#========================
function get_images($dir, $all_pic, $server)
{
	/* Function collects a list of files (images, etc) */
	$kat = opendir ($dir);
		 while ($file = readdir ($kat))
		 {
		 if($file!="." && $file!=".." && $file!="" && strpos($file,".php")==false)
			{
			$all_pic[]=$server.$dir.$file;
			echo "Link ".$server.$dir.$file." successfully recorded. ";flush(); 
			}
		 }
	return $all_pic;
}
##########################
######## M A I N #########
##########################

#--
print '<h1>Rumba Folk</h1>
Script logging static pages for publication in a free hosting narod.ru (or similar) in connection with the fact that the parsing of the site and download the pages on a hosting requires considerable time, often exceeding 30 seconds, are urged to file usr/local/php5/php . ini increase the value of max_execution_time to 30 (default) to 30000 (for example). Before the parsing to delete comments on pages and the search form, recent comments, a link to authorize the menu and link to the RSS feed in the template (ie, all interactive elements). Copy otparsennogo site in a static form is placed in data / cashe, where you can copy it yourself. <br>';flush();

#--
print '<br><br><h4>List pages</h4>';flush();
$temp_urlik = ($startpage=='none')? 'index.html':$startpage.'.html';
$all_url=array();
$all_url[0] = $server.$temp_urlik;
if($all_url=get_url($server.$temp_urlik, $all_url, $server))
	{print '<br><font color=green>Full list '.count($all_url).' static pages <i>'.$server.'</i> generated successfully.</font>';flush();}
else
	{print '<br><font color=red>Complete list of static pages <i>'.$server.'</i> not generated .</font>'; exit (' ERROR');flush();}
	
#--	
print '<br><br><h4>Creating pages</h4>';flush();
if(create_pages('./data/cashe/', $all_url, $server, $temp_urlik))
	{print '<br><font color=green>All pages <i>'.$server.'</i> generated successfully.</font>';flush();}
else
	{print '<br><font color=red>Site Pages <i>'.$server.'</i> not generate .<br></font>'; exit (' ERROR');flush();}

#--	
print '<br><br><h4>Copying pages</h4>';flush();
if($ftp_server!='ftp.test.com')
	{
		if(copy_pages('data/cashe/', $all_url, $server, $ftp_server, $ftp_user_name, $ftp_user_pass))
			{print '<br><font color=green>All pages <i>'.$server.'</i> uploaded to '.$ftp_server.' successfully.</font>';flush();}
		else
			{print '<br><font color=red>Site Pages <i>'.$server.'</i> not uploaded to the  '.$ftp_server.'.<br></font>'; exit (' ERROR');flush();}
	}
else {print '<br>In connection with the educational hosting '.$ftp_server.' procedures copy pages missing.';flush();}

#--
print '<br><br><h4>List of additional files</h4>';flush();
$all_pic = array();
$dir_mass = array('images/', 'pic/', 'css/');
foreach ($dir_mass as $item_pic)
	{
	if($all_pic=get_images($item_pic, $all_pic, $server))
		{print '<br><font color=green>Full list of additional files in the directory <i>'.$item_pic.'</i> generated successfully.</font><br>';flush();}
	else
		{print '<br><font color=red>Full list of additional files in the directory <i>'.$item_pic.'</i> not generated .<br></font><br>'; exit (' ERROR');flush();}
	}

#--	
print '<br><br><h4>Copying extra files</h4>';flush();
if($ftp_server!='ftp.test.com')
	{
		if(copy_images($all_pic, $server, $ftp_server, $ftp_user_name, $ftp_user_pass))
			{print '<br><font color=green>All additional files site <i>'.$server.'</i> uploaded to '.$ftp_server.' successfully.</font>';flush();}
		else
			{print '<br><font color=red>extra files <i>'.$server.'</i> not uploaded to the  '.$ftp_server.'.<br></font>'; exit (' ERROR');flush();}
	}
else {print '<br>In connection with the educational hosting '.$ftp_server.' procedures copy pages missing.';flush();}
	
#--	
print '<br><br><h4>Completion of works</h4>';flush();	
print '<br>All work on site <i>'.$server.'</i> completed!<br>';flush();

//echo '<pre>'; print_r ($all_pic); echo '</pre>';flush();
?>
Return current item: Rumba XML