Ver Fonte

OGP Update System Rewrite (#543)

* See php.ini important information:  https://opengamepanel.org/forum/viewthread.php?thread_id=7590

* Use ZipArchive Class for Update Process

* Use Unscoped Variable for Zip Loop

* Faster Update Test

* Faster Update Test

* Faster Update Test

* Faster Update Test

* Faster Update Test

* Faster Update Test

* Faster Update Test

* Faster Update Test

* Added Function Exists Check

* Add Exist Check

* Update and Unzip Fix

* Update and Unzip Fix

* Extras Install Faster Testing

* Extras Install Faster Testing

* Extras Install Faster Testing

* Extras Install Faster Testing

* Extras Install Faster Testing

* Increase Repo API Count

* Faster Extra Module Updates / Install Process

* Longer Refresh Just In case

* Back to 0 for Refresh
OwN-3m-All há 5 anos atrás
pai
commit
58684b8c14
4 ficheiros alterados com 223 adições e 129 exclusões
  1. 63 40
      modules/extras/extras.php
  2. 100 44
      modules/update/unzip.php
  3. 3 2
      modules/update/update.php
  4. 57 43
      modules/update/updating.php

+ 63 - 40
modules/extras/extras.php

@@ -65,7 +65,6 @@ function installUpdate($info, $base_dir, $current_blacklist = array())
 	$not_overwritten = 0;
 	$new = 0;
 	$all_writable = TRUE;
-	$filelist = "";
 	$overwritten_files = "";
 	$not_overwritten_files = "";
 	$new_files = "";
@@ -74,7 +73,9 @@ function installUpdate($info, $base_dir, $current_blacklist = array())
 	if( !file_exists($temp_dir) )
 		mkdir($temp_dir, 0775);
 	
-	$result = extractZip( $temp_dwl, $temp_dir . DIRECTORY_SEPARATOR, $info['remove_path'] );
+	$result = extractZipGitUpdateFile($temp_dwl, $temp_dir);
+	
+	$newResult = array('ignored_files' => array(), 'extracted_files' => array());
 		
 	if ( is_array($result['extracted_files']) and count($result['extracted_files']) > 0 )
 	{
@@ -83,8 +84,9 @@ function installUpdate($info, $base_dir, $current_blacklist = array())
 		// Check file by file if already exists, if it matches, compares both files 
 		// looking for changes determining if the file needs to be updated.
 		// Also determines if the file is writable
-		$filelist = array();
 		$i = 0;
+		$i2 = 0;
+		
 		foreach( $result['extracted_files'] as $file )
 		{
 			if( DIRECTORY_SEPARATOR == '\\')
@@ -92,53 +94,63 @@ function installUpdate($info, $base_dir, $current_blacklist = array())
 			else
 				$filename = $file['filename'];
 			
+			$fullFilename = $filename;
 			$filename = preg_replace( "/".preg_quote($info['remove_path'])."/", "", $filename);
 			$install_nfo .= realpath($base_dir) . $filename . "\n";
-			$temp_file = $temp_dir . DIRECTORY_SEPARATOR . $filename;
+			$temp_file = $temp_dir . DIRECTORY_SEPARATOR . $fullFilename;
 			$web_file = $base_dir . $filename;
 			
-			if( file_exists( $web_file ) )
-			{
-				if(!in_array($filename, $current_blacklist)){
-					$temp = file_get_contents($temp_file);
-					$web = file_get_contents($web_file);
-					
-					if( $temp != $web )
-					{
-						if( !is_writable( $web_file ) )
+			if(file_exists($temp_file)){
+				if(file_exists($web_file))
+				{
+					if(!in_array($filename, $current_blacklist)){
+						$temp = file_get_contents($temp_file);
+						$web = file_get_contents($web_file);
+						
+						if( $temp != $web )
 						{
-							if ( ! @chmod( $web_file, 0644 ) )
+							if( !is_writable( $web_file ) )
 							{
-								$all_writable = FALSE;
-								$not_writable .= $web_file."\n";
+								if ( ! @chmod( $web_file, 0644 ) )
+								{
+									$all_writable = FALSE;
+									$not_writable .= $web_file."\n";
+								}
+								else
+								{
+									$newResult["extracted_files"][$i]["filename"] = $filename;
+									copy($temp_file, $web_file);
+									$i++;
+									$overwritten_files .= $filename . "\n";
+									$overwritten++;
+								}
 							}
 							else
 							{
-								$filelist[$i] = $file['filename'];
+								$newResult["extracted_files"][$i]["filename"] = $filename;
+								copy($temp_file, $web_file);
 								$i++;
 								$overwritten_files .= $filename . "\n";
 								$overwritten++;
 							}
 						}
-						else
-						{
-							$filelist[$i] = $file['filename'];
-							$i++;
-							$overwritten_files .= $filename . "\n";
-							$overwritten++;
-						}
+					}else{
+						$newResult["ignored_files"][$i2] = $filename;
+						$i2++;
+						$not_overwritten_files .= $filename . "\n";
+						$not_overwritten++;
 					}
-				}else{
-					$not_overwritten_files .= $filename . "\n";
-					$not_overwritten++;
 				}
-			}
-			else
-			{	
-				$filelist[$i] = $file['filename'];
-				$i++;
-				$new_files .= $filename . "\n";
-				$new++;
+				else
+				{	
+					$newResult["extracted_files"][$i]["filename"] = $filename;
+					$webDir = dirname($web_file);
+					@mkdir($webDir, 0775, true);
+					copy($temp_file, $web_file);
+					$i++;
+					$new_files .= $filename . "\n";
+					$new++;
+				}
 			}
 		}
 	}
@@ -159,10 +171,7 @@ function installUpdate($info, $base_dir, $current_blacklist = array())
 	
 	if( $all_writable )
 	{
-		// Extract the files that are set in $filelist, to the folder at $base_dir.
-		$result = extractZip( $temp_dwl, $base_dir, $info['remove_path'], '', $filelist );
-		
-		if( is_array( $result['extracted_files'] ) )
+		if( is_array( $newResult['extracted_files'] ) )
 		{
 			// Updated files
 			if ( $overwritten > 0 )
@@ -307,7 +316,7 @@ function exec_ogp_module()
 	}
 	#return;
 	define('REPO_FILE', DATA_PATH . "repos" . "_" . strtolower($gitHubOrganization));
-	define('URL', 'https://api.github.com/' . $gitAPICont . '/' . $gitHubOrganization . '/repos?per_page=50'); // Returns detailed information of all repositories, and urls for more detailed informations about. Nice API GitHub! :)
+	define('URL', 'https://api.github.com/' . $gitAPICont . '/' . $gitHubOrganization . '/repos?per_page=100'); // Returns detailed information of all repositories, and urls for more detailed informations about. Nice API GitHub! :)
 	if(!file_exists(REPO_FILE) || isset($_GET['searchForUpdates']) || isset($_POST['update']) || filesize(REPO_FILE) == 0 || filesize(REPO_FILE) == 1 || (time() - filemtime(REPO_FILE)) >= 86400)
 	{
 		# Without this $context the file_get_contents function was returning HTTP/1.0 403 Forbidden
@@ -393,12 +402,26 @@ function exec_ogp_module()
 
 	foreach($repos_info_array as $key => $repository)
 	{
+		$isTheme = false;
+		$isModule = false;
+		
 		if(preg_match('/^(OGP-Website|OGP-Agent-Linux|OGP-Agent-Windows)$/',$repository['name']))
 			continue;
+			
+		if(!preg_match('/^(Module-|Theme-).*$/',$repository['name']))
+			continue;
+			
+		if(preg_match('/^(Module-).*$/',$repository['name'])){
+			$isModule = true;
+		}else if(preg_match('/^(Theme-).*$/',$repository['name'])){
+			$isTheme = true;
+		}
 		
 		$REMOTE_REPO_FILE = $gitHubURL . $repository['name'] . '/commits/master.atom';
 		$LOCAL_REPO_FILE = DATA_PATH . $repository['name'] . '.atom';
-		if(!file_exists($LOCAL_REPO_FILE) OR (isset($_GET['searchForUpdates']) and $_GET['searchForUpdates'] == $repository['name']) OR isset($_POST['update']))
+		if(!file_exists($LOCAL_REPO_FILE) 
+			OR (isset($_GET['searchForUpdates']) and $_GET['searchForUpdates'] == $repository['name']) 
+			OR ( isset($_POST['update']) && ( (in_array($m, $_POST["module"]) && $isModule) || (in_array($t, $_POST["theme"]) && $isTheme) ) ) )
 		{
 			$used_file = $REMOTE_REPO_FILE;
 			$contents = file_get_contents($used_file);

+ 100 - 44
modules/update/unzip.php

@@ -41,73 +41,76 @@ function extractZip( $zipFile, $extract_path, $remove_path = '', $blacklist = ''
 		return "Invalid arguments.\n";
 	if( ! file_exists( $zipFile ) )
 		return "Unable to read ${zipFile}.\n";
-	$zip = zip_open($zipFile);
 	$remove_path = addcslashes($remove_path,"/");
 
-	if (is_resource($zip))
+	$zip = new ZipArchive; 
+	if ($zip)
 	{
 		$i=0;
 		$i2=0;
 		$extracted_files = array();
 		$ignored_files = array();
-		while ($zip_entry = zip_read($zip))
-		{
-			$filename = zip_entry_name( $zip_entry );
-			$file_path = preg_replace( "/$remove_path/", "", $filename  );
-			$dir_path = preg_replace( "/$remove_path/", "", dirname( $filename ) );
+		if ($zip->open($zipFile) === TRUE) 
+		{ 
+			for($j = 0; $j < $zip->numFiles; $j++) 
+			{ 
+				$filename = $zip->getNameIndex($j);
+				$file_path = preg_replace( "/$remove_path/", "", $filename  );
+				$dir_path = preg_replace( "/$remove_path/", "", dirname( $filename ) );
 
-			if( isset( $blacklist ) and is_array( $blacklist ) and in_array( $file_path , $blacklist  ) )
-			{
-				if( isset( $whitelist ) and is_array( $whitelist ) and in_array( $filename , $whitelist  ) )
+				if( isset( $blacklist ) and is_array( $blacklist ) and in_array( $file_path , $blacklist  ) )
 				{
-					$ignored_files[$i2] = $file_path;
-					$i2++;
+					if( isset( $whitelist ) and is_array( $whitelist ) and in_array( $filename , $whitelist  ) )
+					{
+						$ignored_files[$i2] = $file_path;
+						$i2++;
+					}
+					continue;
 				}
-				continue;
-			}
-			if( isset( $whitelist ) and is_array( $whitelist ) and !in_array( $filename , $whitelist  ) )
-				continue;
+				if( isset( $whitelist ) and is_array( $whitelist ) and !in_array( $filename , $whitelist  ) )
+					continue;
 
-			$completePath = $extract_path . $dir_path;
-			$completeName = $extract_path . $file_path;
-			$escaped_temp_path = str_replace('\\', '\\\\', $temp_path);// For Windows paths backslashes
-			$root = preg_match("#^$escaped_temp_path#", $completePath)?$temp_path:$base_path;
-			$escaped_root = str_replace('\\', '\\\\', $root);
-			$relative_path = preg_replace("#^$escaped_root(.*)$#","$1",$completePath);
+				$completePath = $extract_path . $dir_path;
+				$completeName = $extract_path . $file_path;
+				$escaped_temp_path = str_replace('\\', '\\\\', $temp_path);// For Windows paths backslashes
+				$root = preg_match("#^$escaped_temp_path#", $completePath)?$temp_path:$base_path;
+				$escaped_root = str_replace('\\', '\\\\', $root);
+				$relative_path = preg_replace("#^$escaped_root(.*)$#","$1",$completePath);
 
-			// Walk through path to create non existing directories
-			// This won't apply to empty directories ! They are created further below
-			if(!file_exists($completePath) && preg_match( '/^' . $remove_path .'/', dirname(zip_entry_name($zip_entry)) ) )
-			{
-				$tmp = $root;
-				foreach(preg_split('/(\/|\\\\)/',$relative_path) AS $k)
+				// Walk through path to create non existing directories
+				// This won't apply to empty directories ! They are created further below
+				if(!file_exists($completePath) && preg_match( '/^' . $remove_path .'/', dirname($filename) ) )
 				{
-					if( $k != "" )
+					$tmp = $root;
+					foreach(preg_split('/(\/|\\\\)/',$relative_path) AS $k)
 					{
-						$tmp .= $k.DIRECTORY_SEPARATOR;
-						if( !file_exists($tmp) )
+						if( $k != "" )
 						{
-							if(!mkdir($tmp, 0777))
+							$tmp .= $k.DIRECTORY_SEPARATOR;
+							if( !file_exists($tmp) )
 							{
-								return "Unable to write folder ${tmp}.\n";
+								if(!mkdir($tmp, 0777))
+								{
+									return "Unable to write folder ${tmp}.\n";
+								}
 							}
 						}
 					}
 				}
-			}
-
-			if (zip_entry_open($zip, $zip_entry, "r"))
-			{
-				if( preg_match( '/^' . $remove_path .'/', dirname(zip_entry_name($zip_entry)) ) )
+				
+				if( preg_match( '/^' . $remove_path .'/', dirname($filename) ) )
 				{
 					if ( ! preg_match( "/\/$/", $completeName) )
 					{
 						if ( $fd = fopen($completeName, 'w+'))
 						{
-							fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
+							$fp = $zip->getStream($filename);
+							if($fp){
+								fwrite($fd, stream_get_contents($fp));
+								$extracted_files[$i]['filename'] = $filename;
+								$i++;
+							}
 							fclose($fd);
-							$extracted_files[$i]['filename'] = zip_entry_name($zip_entry);
-							$i++;
 						}
 						else
 						{
@@ -115,12 +118,65 @@ function extractZip( $zipFile, $extract_path, $remove_path = '', $blacklist = ''
 						}
 					}
 				}
-				zip_entry_close($zip_entry);
 			}
+			$zip->close();
+		}
+		return array('ignored_files' => $ignored_files, 'extracted_files' => $extracted_files);
+	}
+	return "${zipFile} is corrupt.\n";
+}
+
+function extractZipGitUpdateFile( $zipFile, $extract_path)
+{
+
+	if(!file_exists($extract_path))
+	{
+		return "Destination path (${extract_path}) does not exists.\n";
+	}
+
+	if(!is_writable($extract_path))
+	{
+		return "Can't extract to ${extract_path}, not writable.\n";
+	}
+
+	if($zipFile == '' or $extract_path == '')
+		return "Invalid arguments.\n";
+	if( ! file_exists( $zipFile ) )
+		return "Unable to read ${zipFile}.\n";
+
+	$zip = new ZipArchive; 
+	if ($zip)
+	{
+		if ($zip->open($zipFile) === TRUE) 
+		{ 
+			$zip->extractTo($extract_path);
+			$zip->close();
+		}
+	
+		$extracted_files = array();
+		$ignored_files = array();
+		
+		// Construct the iterator
+		$it = new RecursiveDirectoryIterator($extract_path);
+
+		// Loop through files
+		$i = 0;
+		foreach(new RecursiveIteratorIterator($it) as $file) {
+			$filename = $file->getPathname();
+			$filenameRelative = $file->getFilename();
+			
+			if($filenameRelative == ".." || $filenameRelative == "."){
+				continue;
+			}
+				
+			$file_path_no_extract = preg_replace( "#$extract_path/#", "", $filename  );
+			
+			$extracted_files[$i]['filename'] = $file_path_no_extract;
+			$i++;
 		}
-		zip_close($zip);
+		
 		return array('ignored_files' => $ignored_files, 'extracted_files' => $extracted_files);
 	}
 	return "${zipFile} is corrupt.\n";
 }
-?>
+?>

+ 3 - 2
modules/update/update.php

@@ -30,7 +30,7 @@ function check_file($local_path, $remote_url)
 	$remote_file = file_get_contents($remote_url);
 	// Load local file contents in to variable
 	$local_file = file_get_contents($local_path);
-	if( $remote_file != $local_file and preg_match("/exec_ogp_module/", $remote_file) )
+	if( $remote_file != $local_file )
 	{
 		// The file have changes, save them:
 		if( ! file_put_contents($local_path, $remote_file) )
@@ -106,7 +106,8 @@ function exec_ogp_module()
 	{
 		/// Checking for changes in the main update files:
 		$main_update_files = array( 'modules/update/update.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/modules/update/update.php',
-									'modules/update/updating.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/modules/update/updating.php' );
+									'modules/update/updating.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/modules/update/updating.php',
+									'modules/update/unzip.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/modules/update/unzip.php');
 		$refresh = False;
 		foreach($main_update_files as $local_path => $remote_url)
 		{

+ 57 - 43
modules/update/updating.php

@@ -121,7 +121,6 @@ function exec_ogp_module()
 		$overwritten = 0;
 		$new = 0;
 		$all_writable = TRUE;
-		$filelist = "";
 		$overwritten_files = "";
 		$new_files = "";
 		
@@ -145,64 +144,82 @@ function exec_ogp_module()
 		}
 		
 		include ( 'unzip.php' );     // array|false extractZip( string $zipFile, string $extract_path [, string $remove_path, array $blacklist, array $whitelist] )
-		$result = extractZip( $temp_dwl, $extract_path, $unwanted_path, '', '' );
+		$result = extractZipGitUpdateFile($temp_dwl, $extract_path);
+		
 		if ( is_array( $result['extracted_files'] ) and count($result['extracted_files']) > 0 )
 		{
 			// Check file by file if already exists, if it matches, compares both files 
 			// looking for changes determining if the file needs to be updated.
 			// Also determines if the file is writable
-			$filelist = array();
 			$i = 0;
+			$i2 = 0;
+			
+			$newResult = array('ignored_files' => array(), 'extracted_files' => array());
+			
 			foreach( $result['extracted_files'] as $file )
 			{
-				$filename = str_replace( $unwanted_path, "" , $file['filename'] );				
+				$filename = $file['filename'];	
+				$filenameLocal = str_replace( $unwanted_path, "" , $filename );
 				$temp_file = $extract_path . DIRECTORY_SEPARATOR . $filename;
-				$web_file = $baseDir . $filename;
+				$web_file = $baseDir . $filenameLocal;
 
-				if( file_exists( $web_file ) )
-				{
-					$temp = file_get_contents($temp_file);
-					$web = file_get_contents($web_file);
-					
-					if( $temp != $web )
+				if(file_exists($temp_file)){
+					if(file_exists($web_file))
 					{
-						if( !is_writable( $web_file ) )
+						$temp = file_get_contents($temp_file);
+						$web = file_get_contents($web_file);
+						
+						if( $temp != $web )
 						{
-							if ( ! @chmod( $web_file, 0775 ) )
+							if( !is_writable( $web_file ) )
 							{
-								$all_writable = FALSE;
-								$not_writable .= $web_file."<br>";
+								if ( ! @chmod( $web_file, 0775 ) )
+								{
+									$all_writable = FALSE;
+									$not_writable .= $web_file."<br>";
+								}
+								else
+								{
+									$newResult["extracted_files"][$i]["filename"] = $filenameLocal;
+									copy($temp_file, $web_file);
+									$i++;
+									$overwritten_files .= $filenameLocal . "<br>";
+									$overwritten++;
+								}
 							}
 							else
 							{
-								$filelist[$i] = $file['filename'];
-								$i++;
-								$overwritten_files .= $filename . "<br>";
-								$overwritten++;
+								if( !in_array( $filenameLocal, $blacklist  ) )
+								{
+									$newResult["extracted_files"][$i]["filename"] = $filenameLocal;
+									copy($temp_file, $web_file);
+									$i++;
+									$overwritten_files .= $filenameLocal . "<br>";
+									$overwritten++;
+								}else{
+									$newResult["ignored_files"][$i2] = $filenameLocal;
+									$i2++;
+								}
 							}
 						}
-						else
+					}
+					else
+					{	
+						if( !in_array( $filenameLocal, $blacklist  ) )
 						{
-							$filelist[$i] = $file['filename'];
+							$newResult["extracted_files"][$i]["filename"] = $filenameLocal;
+							$webDir = dirname($web_file);
+							@mkdir($webDir, 0775, true);
+							copy($temp_file, $web_file);
 							$i++;
-							if( !in_array( $filename, $blacklist  ) )
-							{
-								$overwritten_files .= $filename . "<br>";
-								$overwritten++;
-							}
+							$new_files .= $filenameLocal . "<br>";
+							$new++;
+						}else{
+							$newResult["ignored_files"][$i2] = $filenameLocal;
+							$i2++;
 						}
 					}
 				}
-				else
-				{	
-					$filelist[$i] = $file['filename'];
-					$i++;
-					if( !in_array( $filename, $blacklist  ) )
-					{
-						$new_files .= $filename . "<br>";
-						$new++;
-					}
-				}
 			}
 		}
 		else
@@ -219,16 +236,13 @@ function exec_ogp_module()
 		
 		if( $all_writable )
 		{
-			// Extract the files that are set in $filelist, to the folder at $baseDir and removes 'upload' from the beginning of the path.
-
-			$result = extractZip( $temp_dwl, preg_replace("/\/$/","",$baseDir), $unwanted_path, $blacklist, $filelist );
-			if( is_array( $result['ignored_files'] ) and !empty( $result['ignored_files'] ) )
+			if( is_array( $newResult['ignored_files'] ) and !empty( $newResult['ignored_files'] ) )
 			{
-				print_failure(get_lang_f('ignored_files',count($result['ignored_files'])));
-				echo get_lang_f("not_updated_files_blacklisted", implode("<br>", $result['ignored_files']) );
+				print_failure(get_lang_f('ignored_files',count($newResult['ignored_files'])));
+				echo get_lang_f("not_updated_files_blacklisted", implode("<br>", $newResult['ignored_files']) );
 				echo "<br><br><br>";
 			}
-			if( is_array( $result['extracted_files'] ) )
+			if( is_array( $newResult['extracted_files'] ) )
 			{
 				// Updated files
 				if ( $overwritten > 0 )