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?

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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")));
}

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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")));
}

Link to comment
Share on other sites

<?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'));

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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");
}

?>

 

 

Link to comment
Share on other sites

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);

}
}
}
?>

Link to comment
Share on other sites

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(..)' ? 

Link to comment
Share on other sites

(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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)));

}
?>

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.