Location: PHPKode > scripts > PHP 2 Install > php-2-install/php2install.class.php
<?PHP
	/*
	 *  Autor:        Helmut Kemper
	 *  Mail:         hide@address.com
	 *  Class:        php2install
	 *  License:      GNU
	 *  Description:
	 *                This class transforms all the archives of a site into an only archive of
	 *                installation in php and an archive sql
	 *
	 *
	 *  Sample:
	 *                include ("./php2install.class.php");
	 *
	 *                $vgoInstall	 =  new php2install ();
	 *
	 *                $vgoInstall	 -> setEncodeDbName ("DbName");
	 *                $vgoInstall	 -> setEncodeDbHost ("127.0.0.1");
	 *                $vgoInstall	 -> setEncodeDbUser ("DbUser");
	 *                $vgoInstall	 -> setEncodeDbPassword ("DbPassword");
	 *
	 *                $vgoInstall	 -> setDecodeDbPath ("./site_out/site.sql");
	 *
	 *                $vgoInstall	 -> encodeDataBase ();
	 *
	 *                $vgoInstall	 -> setEncodePath ("./site_in");
	 *
	 *                $vgoInstall	 -> setDecodePath ("./site_out/site_install.php");
	 *
	 *                $vgoInstall	 -> encodeTree ();
	 */
	 
	class php2install
	{
		var $vcsEncodePath;
		var $vcsEncodeDbHost;
		var $vcsEncodeDbUser;
		var $vcsEncodeDbPassword;
		var $vcsEncodeDbName;
		
		var $vcaDecodeReplace;
		var $vcsDecodePath;
		var $vcsDecodeDbPath;
		var $vcsDecodeDbHost;
		var $vcsDecodeDbUser;
		var $vcsDecodeDbPassword;
		var $vcsDecodeDbName;
		var $vcsDecodeNewPath;
		
		var $vcaSearch;
		var $vcaReplace;
		var $vcaFormHelp;
		
		/*
		 *  Name:			setReplaceString
		 *  Input:			$vlaSearch Array, $vlaReplace Array
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setReplaceString (Array ("search1", "search2"), Array ("replace1", "replace2"))
		 *  Description:	Search and replace strings in outfile
		 */
		function setReplaceString ($vlaSearch, $vlaReplace)
		{
			$this -> vcaSearch			 =  $vlaSearch;
			$this -> vcaReplace			 =  $vlaReplace;
		}
		
		/*
		 *  Name:			setReplaceForm
		 *  Input:			$vlaText Array, $vlaReplace Array, $vlaHelp Array
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setReplaceForm (Array ("User:"), Array ("user"), Array ("Enter user name for this server"))
		 *  Description:	This function generates a form for each field of the Array, looking for for each term,
		 *  				"<!--{" + field in $vlaReplace + "}-->",and changing it for the value typed for the user
		 */
		function setReplaceForm ($vlaText, $vlaReplace, $vlaHelp)
		{
			$this -> vcaFormText		 =  $vlaText;
			$this -> vcaFormReplace		 =  $vlaReplace;
			$this -> vcaFormHelp		 =  $vlaHelp;
		}
		
		/*
		 *  Name:			setEncodePath
		 *  Input:			$vlsPath String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setEncodePath (".")
		 *  Description:	Set path of the tree of directories
		 *  				In Unix based, use . (dot) for same dir
		 */
		function setEncodePath ($vlsPath)
		{
			$this -> vcsEncodePath			 =  $vlsPath;
		}
		
		/*
		 *  Name:			setEncodeDbName
		 *  Input:			$vlsDbName String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setEncodeDbName ("db_name")
		 *  Description:	Set name for local MySQL to encoded
		 */
		function setEncodeDbName ($vlsDbName)
		{
			$this -> vcsEncodeDbName		 =  $vlsDbName;
		}
		
		/*
		 *  Name:			setEncodeDbHost
		 *  Input:			$vlsHost String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setEncodeDbHost ("127.0.0.1")
		 *  Description:	Set host for local MySQL to encoded
		 */
		function setEncodeDbHost ($vlsHost)
		{
			$this -> vcsEncodeDbHost		 =  $vlsHost;
		}
		
		/*
		 *  Name:			setEncodeDbUser
		 *  Input:			$vlsUser String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setEncodeDbUser ("user_name")
		 *  Description:	Set user name for local MySQL to encoded
		 */
		function setEncodeDbUser ($vlsUser)
		{
			$this -> vcsEncodeDbUser		 =  $vlsUser;
		}
		
		/*
		 *  Name:			setEncodeDbPassword
		 *  Input:			$vlsPassword String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setEncodeDbPassword ("user_password")
		 *  Description:	Set password for local MySQL to encoded
		 */
		function setEncodeDbPassword ($vlsPassword)
		{
			$this -> vcsEncodeDbPassword	 =  $vlsPassword;
		}
		
		/*
		 *  Name:			setDecodeDbPath
		 *  Input:			$vlsPath String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setDecodeDbPath ("./output_dir/mySiteSQL.php")
		 *  Description:	Uses this function to indicate the path of output of the installing archive of the data base
		 */
		function setDecodeDbPath ($vlsPath)
		{
			$this -> vcsDecodeDbPath		 =  $vlsPath;
		}
		
		/*
		 *  Name:			setDecodeDbName
		 *  Input:			$vlsDbName String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setDecodeDbName ("remote_db")
		 *  Description:	Set name for remote db
		 */
		function setDecodeDbName ($vlsDbName)
		{
			$this -> vcsDecodeDbName		 =  $vlsDbName;
		}
		
		/*
		 *  Name:			Set hoste for remote MySQL
		 *  Input:			$vlsHost String
		 *  Output:			$vlsHost String
		 *  Use:			$ObjInsatall -> setDecodeDbHost ("mySite.MyProvider")
		 *  Description:	Set host for remote MySQL
		 */
		function setDecodeDbHost ($vlsHost)
		{
			$this -> vcsDecodeDbHost		 =  $vlsHost;
		}
		
		/*
		 *  Name:			setDecodeDbUser
		 *  Input:			$vlsUser String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setDecodeDbUser ("remote_user")
		 *  Description:	Set user for remote MySQL
		 */
		function setDecodeDbUser ($vlsUser)
		{
			$this -> vcsDecodeDbUser		 =  $vlsUser;
		}
		
		/*
		 *  Name:			setDecodeDbPassword
		 *  Input:			$vlsPassword String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setDecodeDbPassword ("password")
		 *  Description:	Ser password for remote MySQL
		 */
		function setDecodeDbPassword ($vlsPassword)
		{
			$this -> vcsDecodeDbPassword	 =  $vlsPassword;
		}
		
		/*
		 *  Name:			setDecodeNewPath
		 *  Input:			$vlsPath String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setDecodeNewPath (".")
		 *  Description:	Set path of the remote tree of directories
		 *  				In Unix based, use . (dot) for same dir
		 */
		function setDecodeNewPath ($vlsPath)
		{
			$this -> vcsDecodeNewPath		 =  $vlsPath;
		}
		
		/*
		 *  Name:			setDecodePath
		 *  Input:			$vlsPath String
		 *  Output:			void
		 *  Use:			$ObjInsatall -> setDecodePath ("./outputdir/outputlocalfile.php")
		 *  Description:	Set name and local for output file
		 */
		function setDecodePath ($vlsPath)
		{
			$this -> vcsDecodePath			 =  $vlsPath;
		}
		
		/*
		 *  Name:			encodeDataBase
		 *  Input:			void
		 *  Output:			file
		 *  Use:			$ObjInsatall -> encodeDataBase ();
		 *  Description:	Start db encode
		 */
		function encodeDataBase ()
		{
			$this -> decodePath ($this -> vcsDecodeDbPath);
			$vlsFileData	 =  "";
			
			@ $vlhDb = mysql_pconnect ($this -> vcsEncodeDbHost, $this -> vcsEncodeDbUser, $this -> vcsEncodeDbPassword) or die (sprintf ("Error [%d]: %s", mysql_errno (), mysql_error ()));
			
			mysql_select_db ($this -> vcsEncodeDbName) or die (sprintf ("Erro [%d]: %s", mysql_errno (), mysql_error ()));
			
			$vlsSql		 =  "SHOW TABLES FROM " . $this -> vcsEncodeDbName;
			
			$vlsResult	 =  mysql_query ($vlsSql);
			
			while ($vlaTables = mysql_fetch_array ($vlsResult, MYSQL_NUM))
			{
				$vlsSql2			 =  "SHOW CREATE TABLE " . $vlaTables[0];
				$vlsResult2			 =  mysql_query ($vlsSql2);
				
				while ($vlaCreate = mysql_fetch_array ($vlsResult2, MYSQL_NUM))
				{
					$vlsFileData	.=  "\n\r" . $vlaCreate[1] . ";/*Fim da SQL*/\r";
				}
				
				$vlsSql2			 =  "SELECT * FROM " . $vlaTables[0];
				
				$vlsResult2			 =  mysql_query ($vlsSql2);
				
				while ($vlaData = mysql_fetch_array ($vlsResult2, MYSQL_NUM))
				{
					foreach ($vlaData as $vlnKey => $vlsValue)
						$vlaData[$vlnKey]	 =  addslashes ($vlsValue);
					
					$vlsAcc			 =  "INSERT INTO " . $vlaTables[0] . " VALUES ('" . implode ("', '", $vlaData) . "');/*Fim da SQL*/\r";
					$vlsAcc			 =  str_replace ("''", "null", $vlsAcc);
					
					$vlsFileData	.=  $vlsAcc;
				}
			}
			
			$vlsFileData			 =  "<?PHP
			@ \$vlhDb = mysql_pconnect (\"" . $this -> vcsDecodeDbHost . "\", \"" . $this -> vcsDecodeDbUser . "\", \"" . $this -> vcsDecodeDbPassword . "\") or die (sprintf (\"Error [%d]: %s\", mysql_errno (), mysql_error ()));
			mysql_select_db (\"" . $this -> vcsDecodeDbName . "\") or die (sprintf (\"Erro [%d]: %s\", mysql_errno (), mysql_error ()));
			\$vlsSql		 =  \"" . $vlsFileData . "\";
			\$vlaSql		 =  explode (\";/*Fim da SQL*/\\r\", \$vlsSql);
			foreach (\$vlaSql as \$vlsSql)
			{
				if (\$vlsSql != \"\")
					\$vlsResult	 =  mysql_query (\$vlsSql) or die (sprintf (\"Erro [%d]: %s\", mysql_errno (), mysql_error ()));
			}
			?".">";
			
			$vlHandle		 =  fopen ($this -> vcsDecodeDbPath, "w");
			fwrite ($vlHandle, $vlsFileData);
			fclose ($vlHandle);
		}
		
		function php2install ()
		{
		
		}
		
		/*
		 *  Name:			encodeTree
		 *  Input:			void
		 *  Output:			void
		 *  Use:			$ObjInsatall -> encodeTree ()
		 *  Description:	Start tree encode
		 */
		function encodeTree ()
		{
			$vlsFileEncode					 =  "";
			
			do
			{
				$vlaReturn					 =  $this -> findFiles ($this -> vcsEncodePath, $vraTree, $vraFiles);
				extract ($vlaReturn);
				
				$this -> vcsEncodePath		 =  $vrbPath;
			}
			while ($vrbPath);
			
			foreach ($vraFiles as $vlsFile)
				$vlsFileEncode	.=  $this -> composeEncodeFile ($vlsFile, 100);
			
			if ($this -> vcaSearch)
			{
				$vlsSearchAndReplace		 =  "
				\$vlsSearch	 =  \"" . addslashes (serialize ($this -> vcaSearch)) . "\";
				\$vlsReplace	 =  \"" . addslashes (serialize ($this -> vcaReplace)) . "\";
				\$vlsFile =  str_replace (unserialize (stripslashes (\$vlsSearch)), unserialize (stripslashes (\$vlsReplace)), \$vlsFile);
				";
			}
			
			if ($this -> vcaFormText)
			{
				$vlsFormReplace		 =  "
				if (\$_REQUEST[\"vfControle\"] == null)
				{
					\$_REQUEST[\"vfControle\"] = true;
					print \"<HTML><BODY><CENTER><TABLE border='0' width='80%'><tr><td><FORM name='form' action=''><INPUT type='hidden' name='vfControle' value='0'>";
				
				foreach ($this -> vcaFormText as $vlnKey => $vlsValue)
				{
					$vlsFormReplace	.=  "<Font face='verdana' sixe='1'>\".criaRotulo(\"" . $this -> vcaFormText[$vlnKey] . "\", \"" . $this -> vcaFormHelp[$vlnKey] . "\").\"</Font><br />";
					$vlsFormReplace	.=  "<INPUT type='text' name='" . $this -> vcaFormReplace[$vlnKey] . "'><br /><br />";
				}
				
				$vlsFormReplace		.=  "<INPUT type='submit' value='Execute'></FORM></td></tr></TABLE></CENTER></BODY></HTML>\";\n\t\t\t\t\texit;\n\t\t\t\t}";
			}
			
			$vlsFileEncode			 =  "<?PHP\n" . $vlsFormReplace . $vlsFileEncode . "\n\t\t\tfunction composeFile (\$vlsPath, \$vlsFile)
			{
				\$vlsFile	 =  base64_decode (\$vlsFile);
				\$vlsFile	 =  gzinflate (\$vlsFile);
				$vlsSearchAndReplace
				if (\$_REQUEST[\"vfControle\"] == 0)
				{
					foreach (\$_REQUEST as \$vlnKey => \$vlsValue)
					{
						\$vlsFile	 =  str_replace (\"<!--{\".\$vlnKey.\"}-->\", \$vlsValue, \$vlsFile);
					}
				}
				
				\$vlVetor	 =  fopen (\$vlsPath, \"w\");
				fwrite (\$vlVetor, \$vlsFile) or die (\"Erro:<br>\\n<br>\\nYour can check dir permissions for \" . dirname (\$vlsPath) . \"!\");
				fclose (\$vlVetor) or die (\"Erro:<br>\\n<br>\\nYour can check dir permissions for \" . dirname (\$vlsPath) . \"!\");
			}
			
			function composeDir (\$vlsDir, \$vlsMenssageError = \"Error! Can\'t create dir!\")
			{
				\$vlsDir = dirname (\$vlsDir);
				
				\$vlsAccDir = \"\";
				\$vlaDir = explode (\"/\", \$vlsDir);
			
				foreach (\$vlaDir as \$vlnKey => \$vlsValue)
				{
					\$vlsAccDir .= \$vlsValue;
					
					if ((\$vlsValue == \".\") or (\$vlsValue == \"..\"))
					{
						\$vlsAccDir .= \"/\";
						continue;
					}
					
					if (!is_dir(\$vlsAccDir))
					{
						\$vlUmask = umask(0);
						
						@ mkdir (\$vlsAccDir, 0777) or die (\$vlsMenssageError);
						
						umask(\$vlUmask);
					}
				
					\$vlsAccDir .= \"/\";
				}
			}
			
			function criaRotulo (\$vlRotulo, \$vlAjuda)
			{
				global \$vgObjIdioma;
				
				\$vlModelo	 =  \"<HTML style='width: 400; height: 150'><HEAD><TITLE>{\$vlRotulo}</TITLE></HEAD><BODY bgcollor='#FFFFFF'><font style='COLOR: #000000; FONT: 11px geneva, verdana, arial, sans-serif; font-weight: normal'>{\$vlAjuda}</font></BODY></HTML>\";
							
				\$vlStringSaida			 =  \"
												<script language=\\\"JavaScript\\\">
													function criaAjuda\" . preg_replace (\"/([^A-Za-z]+)/\", \"\", \$vlRotulo) . \" ()
													{
														var vlJsNovaJanela = window.open(\\\"\\\", \\\"\\\", \\\"scrollbars=yes, alwaysRaised=yes, menubar=no, titlebar=no, title=no, toolbar=no, resizable=yes, status=no, height=\$vlHeight, width=\$vlWidth\\\")
														var vlJsJanela = \\\"\\\"
														vlJsNovaJanela.document.write (\\\"\$vlModelo\\\")
														vlJsNovaJanela.document.close ()
														vlJsNovaJanela.focus ()
													}
												</script>
												<a class='rotulo' href=\\\"javascript:criaAjuda\" . preg_replace (\"/([^A-Za-z]+)/\", \"\", \$vlRotulo) . \"()\\\" style=\\\"cursor: help\\\" title=\\\"{\$vrCriaSaidaIdiomaDocumento[ObterAjuda]}\\\" onmouseover=\\\"javascript: return 'false'\\\">\$vlRotulo</a>
											\";
				
				return \$vlStringSaida;
			}?".">";
			
			$vlsFileEncode	 =  str_replace ("\r\n", "\n", $vlsFileEncode);
			
			$this -> decodePath ($this -> vcsDecodePath);
			
			$vlHandle		 =  fopen ($this -> vcsDecodePath, "w");
			fwrite ($vlHandle, $vlsFileEncode);
			fclose ($vlHandle);
		}
		
		function decodePath ($vlsPath)
		{
			$vlsDecodeFileName	 =  basename ($vlsPath);
			$vlsDecodePathName	 =  str_replace ($vlsDecodeFileName, "", $vlsPath);
			
			$vlaDecodeDir		 = explode ("/", $vlsDecodePathName);
			$vlsAccDir			 =  "";
			
			foreach ($vlaDecodeDir as $vlnKey => $vlValue)
			{
				$vlsAccDir .= $vlValue;
				
				if (($vlValue == ".") or ($vlValue == ".."))
				{
					$vlsAccDir .= "/";
					continue;
				}
				
				if (!is_dir($vlsAccDir))
				{
					$vlUmask = umask(0);
					print $vlsAccDir . "<br>";
					@ mkdir ($vlsAccDir, 0777) or die ("Error: Can't crete dir");
					umask($vlUmask);
				}
			
				$vlsAccDir .= "/";
			}
		}
		
		function composeEncodeFile ($vlsPath, $vlnStringLength = null)
		{
			$vlsFile = fopen ($vlsPath, "r");
			$vlnLength = filesize ($vlsPath);
			$vlsLine = fread ($vlsFile, $vlnLength);
			fclose ($vlsFile);
			
			//$vlsLine = str_replace ("\\", "\\\\", $vlsLine);
			//$vlsLine = str_replace ("\"", "\\\"", $vlsLine);
			
			$vlsLine = gzdeflate ($vlsLine, 9);
			$vlsImegeCode = base64_encode ($vlsLine);
			
			if ($vlnStringLength)
			{
				$vlsImegeCode = chunk_split ($vlsImegeCode, $vlnStringLength, "\".\n\t\t\t\t\t\t\t\t\t\"");
				
				if ($this -> vcsDecodeNewPath)
				{
					$vlsPath	 =  str_replace (dirname ($vlsPath), $this -> vcsDecodeNewPath, $vlsPath);
				}
				
				$vlsImegeCode	 =  "
				\$vlsPathOut	 =  \"$vlsPath\";
				\$vlsFile		 =  \"$vlsImegeCode\";
				
				if (\$_REQUEST[\"vfControle\"] != true)
				{
					composeDir (\$vlsPathOut);
					composeFile (\$vlsPathOut, \$vlsFile);\n
				}
										";
			}
			
			return $vlsImegeCode;
		}
		
		function findFiles ($vlsPath = null, $vlaTree = array (), $vlaFiles = array ())
		{
			if (!is_array ($vlaTree))
				$vlaTree				 =  array ();
				
			if (!is_array ($vlaFiles))
				$vlaFiles				 =  array ();
			
			$vlaTmpFiles				 =  array ();
			$vlaTmpTree					 =  array ();
			
			if (is_string ($vlsPath))
			{
				extract ($this -> _findFiles_ ($vlsPath, $vlaTree, $vlaFiles));
				
				$vlaFiles				 =  array_merge ($vlaFiles, $vlaTmpFiles);
				$vlaTree				 =  array_merge ($vlaTree, $vlaTmpTree);
			}
			
			else
			{
				foreach ($vlaTree as $vlsPath)
				{
					extract ($this -> _findFiles_ ($vlsPath, $vlaTree, $vlaFiles));
					
					$vlaFiles			 =  array_merge ($vlaFiles, $vlaTmpFiles);
					$vlaTree			 =  array_merge ($vlaTree, $vlaTmpTree);
				}
			}
			
			if ((count($vlaTmpFiles)) or (count ($vlaTmpTree)))
				return array ("vrbPath" => true, "vraTree" => $vlaTree, "vraFiles" => $vlaFiles);
				
			$vlaFiles					 =  array_count_values ($vlaFiles);
			$vlaFiles					 =  array_keys ($vlaFiles);
			
			$vlaTree					 =  array_count_values ($vlaTree);
			$vlaTree					 =  array_keys ($vlaTree);
			
			return array ("vrbPath" => false, "vraTree" => $vlaTree, "vraFiles" => $vlaFiles);
		}
		
		function _findFiles_ ($vlsPath, $vlaTree, $vlaFiles)
		{
			$vlaTmpTree					 =  array ();
			$vlaTmpFiles				 =  array ();
			
			$vlHandle					 =  opendir ($vlsPath);
			
			while ($vlsContent = readdir ($vlHandle))
			{
				if (($vlsContent == ".") or ($vlsContent == ".."))
					continue;
				
				$vlsContent = $vlsPath . "/" . $vlsContent;
				
				if ((is_dir ($vlsContent)) and (!in_array ($vlsContent, $vlaTree)))
					array_push ($vlaTmpTree, $vlsContent);
					
				if ((is_file ($vlsContent)) and (!in_array ($vlsContent, $vlaFiles)))
					array_push ($vlaTmpFiles, $vlsContent);
			}
			
			closedir ($vlHandle);
			
			return array ("vlaTmpTree" => $vlaTmpTree, "vlaTmpFiles" => $vlaTmpFiles);
		}
	}
?>
Return current item: PHP 2 Install