Jump to content

[SOLVED] Using PHP and URLdecode on multiple pages - starting from scratch


MrLarkins

Recommended Posts

Hello,

 

My problem:

I have many pages that have urlencoding.

 

I'd like to write a script that will open all the files in a folder and subsequent sub-folders that are .html extension and urldecode them and overwrite the original with the decoded.

 

Is this going to be hard for me?  Where do I start?

how about this? i was working on this while you were posting

 

<?php
$current_dir = "$DOCUMENT_ROOT"."algebra1/";
  $dir = opendir($current_dir);    

  while ($file = readdir($dir))
    {
    $parts = explode(".", $file);
    if (is_array($parts) && count($parts) > 1) { 
        $extension = end($parts);        
        if ($extension == "html")    
             $html_file=fopen("$file","r+");
              urldecode($html_file);
fclose($html_file);
        } 
    } 
  
  closedir($dir);
?>

Warning: fclose(): supplied argument is not a valid stream resource in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 15

Warning: fopen(footer.html) [function.fopen]: failed to open stream: No such file or directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 11

 

guess not...whats that stuff mean?

again, i was working while you were posting, lol...and it seems yours is shorter than mine.  I'm going to try yours, but please take a look at mine and tell me where i went wrong...b/c it makes logical sense to me but doesn't work.

 

<?php
$current_dir = "$DOCUMENT_ROOT"."algebra1/";
  $dir = opendir($current_dir);    

  while ($file = readdir($dir))
    {
    $parts = explode(".", $file);
    if (is_array($parts) && count($parts) > 1) { 
        $extension = end($parts);        
        if ($extension == "html")    
             $encoded=file_get_contents($file);
	$decoded=urldecode($encoded);
             file_put_contents($file,$decoded);

        } 
    } 
  
  closedir($dir);
?>

 

edit**

yours give an error

 

Parse error: syntax error, unexpected T_AS in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 2

 

Simple mistake.

 

foreach (glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) {
  file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/algebra1/$file", urldecode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/algebra1/$file")));
}

<?php

<pre>
print_r(glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html'));
</pre>

?>

 

produces

Parse error: syntax error, unexpected '<' in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 3

while

<pre>
print_r(glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html'));
</pre>

produces

print_r(glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html'));

 

 

ok, thanks.

here's the output

Array
(
    [0] => /home/oskgamin/public_html/MrLarkins.com/algebra1/footer.html
    [1] => /home/oskgamin/public_html/MrLarkins.com/algebra1/globalnavigation-cd.html
)

 

and yes, those are the only two files

 

but keep in mind, there are many other folders with many other html files in there that need to be urldecoded

 

and of course this is the full error message

 

Warning: file_get_contents(/home/oskgamin/public_html/MrLarkins.com/algebra1//home/oskgamin/public_html/MrLarkins.com/algebra1/footer.html) [function.file-get-contents]: failed to open stream: No such file or directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 4

 

Warning: file_put_contents(/home/oskgamin/public_html/MrLarkins.com/algebra1//home/oskgamin/public_html/MrLarkins.com/algebra1/footer.html) [function.file-put-contents]: failed to open stream: No such file or directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 4

 

Warning: file_get_contents(/home/oskgamin/public_html/MrLarkins.com/algebra1//home/oskgamin/public_html/MrLarkins.com/algebra1/globalnavigation-cd.html) [function.file-get-contents]: failed to open stream: No such file or directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 4

 

Warning: file_put_contents(/home/oskgamin/public_html/MrLarkins.com/algebra1//home/oskgamin/public_html/MrLarkins.com/algebra1/globalnavigation-cd.html) [function.file-put-contents]: failed to open stream: No such file or directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 4

i modified your code and it seemed to work

 


<?php

foreach (glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) {
  file_put_contents("$file", urldecode(file_get_contents("$file")));
}

?>

but how do i get it to work into the subdirectories as well?

I had to modify the code a bit to strip out some now un-needed scripting garbage

new code still does not did into the lower directories and do its magic!

<?php

foreach (glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) {
$decode=urldecode(file_get_contents("$file"));
$s1=str_replace('<SCRIPT LANGUAGE="Javascript"><!--
document.write(unescape("',"",$decode);
$s2=str_replace('"));//--></SCRIPT>',"",$s1);
$s3=str_replace('<SCRIPT LANGUAGE="Javascript"><!--
document.write(unescape("',"",$s2);
$s4=str_replace('<SCRIPT LANGUAGE="JavaScript"><!--
eval(unescape("document.write=null;',"",$s3);
  file_put_contents("$file","$s4");
}

?>

 

 

still working on it, still having problems

 

here's what i got so far

<?php

$current_dir = glob($_SERVER['$DOCUMENT_ROOT']."algebra1/");
  foreach($current_dir as $dir) {
  $dir1 = opendir($dir);    

  while ($file = readdir($dir1))
    {
    $parts = explode(".", $file);
    if (is_array($parts) && count($parts) > 1) { 
        $extension = end($parts);        
        if ($extension == "html")   
$decode = urldecode(file_get_contents($file));
	$s1 = str_replace('<SCRIPT LANGUAGE="Javascript"><!--
	             document.write(unescape("',"",$decode);
	$s2 = str_replace('"));//--></SCRIPT>',"",$s1);
	$s3 = str_replace('<SCRIPT LANGUAGE="Javascript"><!--
                          document.write(unescape("',"",$s2);
             $s4 = str_replace('<SCRIPT LANGUAGE="JavaScript"><!--
		eval(unescape("document.write=null;',"",$s3);
  	file_put_contents($file,$s4);

}
}
}
?>

I'm still having problems.  :confused:  My code gives the following:

Warning: file_put_contents(.) [function.file-put-contents]: failed to open stream: Is a directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 21

 

Warning: file_put_contents(..) [function.file-put-contents]: failed to open stream: Is a directory in /home/oskgamin/public_html/MrLarkins.com/urldecode.php on line 21

 

what is with the 'file_put_contents(.)' and 'file_put_contents(..)' ? 

(or use the simpler glob() exclusively).

 

i tried using the glob() first, but it didn't dig into the subdirectories...or did i do it wrong?  thorpe helped with it

again, this is what was used


foreach (glob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) {
  file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/algebra1/$file", urldecode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/algebra1/$file")));
}

but it only returned the files in the immediate folder, not all the other files in the subdirectories

You will need to call glob() recursively, to dig into subdirectories.

sorry, but I have no idea how to ...

 

There's a user function on the manual page.

:wtf: because that all looks like gibberish to me...i wish i were smarter. :facewall:

 

believe it or not, I am trying.

No worries. Here's an example using that user function:

 

<?php
function rglob($pattern, $flags = 0, $path = '') {
if (!$path && ($dir = dirname($pattern)) != '.') {
	if ($dir == '\\' || $dir == '/') {
		$dir = '';
	}
	return rglob(basename($pattern), $flags, $dir . '/');
}
$paths = glob($path . '*', GLOB_ONLYDIR | GLOB_NOSORT);
$files = glob($path . $pattern, $flags);
foreach ($paths as $p) {
	$files = array_merge($files, rglob($pattern, $flags, $p . '/'));
}
return $files;
}

foreach (rglob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) {
//file_put_contents($file, urldecode(file_get_contents($file)));
echo "$file<br />";
}
?>

 

I'm not too sure about the paths used in the foreach loop, so it prints them for now so you can check if they are right, before you modify the files.

wow!  that shows a lot of files!  i knew there would be a bunch, just didn't realize how many.  see for yourself. ( over a 1000 )

 

and now i'm wondering about the string replacement that i need to do on all these files.  can i do it like

<?php
function rglob($pattern, $flags = 0, $path = '') {
if (!$path && ($dir = dirname($pattern)) != '.') {
	if ($dir == '\\' || $dir == '/') {
		$dir = '';
	}
	return rglob(basename($pattern), $flags, $dir . '/');
}
$paths = glob($path . '*', GLOB_ONLYDIR | GLOB_NOSORT);
$files = glob($path . $pattern, $flags);
foreach ($paths as $p) {
	$files = array_merge($files, rglob($pattern, $flags, $p . '/'));
}
return $files;
}

foreach (rglob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) {
file_put_contents($file, urldecode(file_get_contents($file)));
file_put_contents($file, str_replace('<SCRIPT LANGUAGE="Javascript"><!--        
document.write(unescape("',"",file_get_contents($file)));

}
?>

You would do it like this:

 

<?php
$replace = array(
'<SCRIPT LANGUAGE="Javascript"><!--
                   document.write(unescape("',
'"));//--></SCRIPT>',
'<SCRIPT LANGUAGE="Javascript"><!--
                             document.write(unescape("',
'<SCRIPT LANGUAGE="JavaScript"><!--
         eval(unescape("document.write=null;'
);
foreach (rglob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) {
$data = urldecode(file_get_contents($file));
$data = str_replace($replace, '', $data);
file_put_contents($file, $data);
}
?>

If it's because of the many spaces, you could try with regular expressions:

 

<?php
$replace = array(
'~<SCRIPT LANGUAGE="Javascript"><!--\s*document\.write\(unescape\("~i',
'~"\)\);//--></SCRIPT>~i',
'~<SCRIPT LANGUAGE="Javascript"><!--\s*document\.write\(unescape\("~i',
'~<SCRIPT LANGUAGE="JavaScript"><!--\s*eval\(unescape\("document\.write=null;~i'
);
foreach (rglob($_SERVER['DOCUMENT_ROOT'] . '/algebra1/*.html') as $file) {
$data = urldecode(file_get_contents($file));
$data = preg_replace($replace, '', $data);
file_put_contents($file, $data);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.