For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > October 2006 > Is this a PHP script?? BASH?? Please review!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Is this a PHP script?? BASH?? Please review!
Synapse Syndrome

2006-10-30, 7:03 pm

Hi

I've been given what I am told is a PHP script to be used on my server. I
do not know any PHP.

I am trying to use a feature of a program called ArchiCAD. This feauture
allows CAD drawing files to be published on a webpage, and viewed and
commented on using a java applet.

The problem is that with ArchiCAD v10, it now makes spaces and uses other
URL unfriendly characters in the filenames, making this feature not
properly.

This is an example of the faulty output:
http://test404.damnserver.com


Following is what I have been told is a PHP script. It is meant for a
freebsd or MacOS server. It apparently unpacks a zip file, deletes some
temp files that MacOS makes, and renames the files to make them URL
friendly. It then edits the Content.xml file with the filename changes.

First of all, is this actually a PHP script? I need it to run on a Windows
server running IIS. But someone that I gave it to look at has said that he
thinks that it does some strange functions, and he thinks it's actually a
BASH file, and I have no hope of getting it to run on Windows.

I need something that does the same thing for a Windows server running the
website linked earlier. I also do not want the unzip function, and it needs
to be taken out.

I would be grateful for any thoughts or opinion on this.

Cheers.

________________________________________
_____________________________

function process_gpr($zip_file, $record_folder_location) #opens the zip,
makes it web safe
{
if(!ini_get('upload_tmp_dir')) #check php's upload_tmp_dir
{ #if it's not in the ini set it to this value
$upload_tmp_dir = '/var/tmp/';
}
else
{ #otherwise use the ini value
$upload_tmp_dir = ini_get('upload_tmp_dir');
}

$upload_tmp_dir = $upload_tmp_dir . "php/"; #make our own little folder for
unzipping
if(!file_exists($upload_tmp_dir)) { shell_exec("mkdir " .
$upload_tmp_dir); }
if(!shell_exec("rm -Rf " . $upload_tmp_dir . "*")) #clean it out; if that
went smoothly
{ #unzip the zip file
shell_exec("unzip -d " . $upload_tmp_dir . " " . $zip_file);

#zip's from osx's finder create this metadata folder
$macosx_only1 = $upload_tmp_dir . "__MACOSX"; #delete it if present
if(file_exists($macosx_only1)) { if(is_dir($macosx_only1)) {
shell_exec("rm -Rf " .
$macosx_only1); } }
$macosx_only2 = $upload_tmp_dir . ".DS_Store"; #and this file too
if(file_exists($macosx_only2)) { if(is_file($macosx_only2)) {
shell_exec("rm -Rf " .
$macosx_only2); } }

#find the Reviewer folder in the expanded tree
if(preg_match('/(\/.*\/)Reviewer/i', shell_exec("find " . $upload_tmp_dir .
" -type d -name
Reviewer"), $matches))
{ #find the enclosing folder
$gpr['path'] = $enclosing_folder = $matches[1];

if($dh = opendir($enclosing_folder)) #open the enclosing folder
{
while(($file = readdir($dh)) !== false) #read it out
{
if((filetype($enclosing_folder . $file) == "dir") && ($file !== "Reviewer")
&& ($file !==
".") && ($file !== ".."))
{ #find the data folder
$gprf = $file . "/";
}
if((filetype($enclosing_folder . $file) == "file") && (substr($file, -5) ==
".html") &&
($file !== ".") && ($file !== ".."))
{ #find the html file and rename it
preg_match("/(.+)\..*?$/i", $file, $name);
$gpr['title'] = $name[1]; #save that title for later
rename($enclosing_folder . $file, $enclosing_folder . 'gpr.html');
}
}
closedir($dh); #close the enclosing folder

#create the folder name
$gpr['file_name'] = $gpr['title'] . "-" . date("Y-m-d") . ".gpr";
#find the real filepath leading up to where we're placing it
preg_match("/(\/.*\/)rianachd\/.+\..+/i", $_SERVER['PATH_TRANSLATED'],
$project_folder_full_path);
$project_folder_full_path = $project_folder_full_path[1] .
substr($record_folder_location, 3)
.. "/" . $gpr['file_name'] . "/" . $gpr['title'] . "/";
#echo "<h6>project_folder_full_path: $project_folder_full_path</h6>";
$gprf = $enclosing_folder . $gprf; #set this to data folder
$catalog = $gprf . "Catalog.xml"; #path to catalog file
if($handle = fopen($catalog, "r"))
{ #read out contents of the catalog file
$contents = fread($handle, filesize($catalog));
#replace stupid characters in set, path, title and layout links & titles
#$contents = gpr_replacer($content, $regex, $character, $replacement);
$contents = gpr_replacer($contents, "/\<Set\>(.*?)\<\/Set\>/i", "", "_");
$contents = gpr_replacer($contents, "/\<Set\>(.*?)\<\/Set\>/i", "", "_");
$contents = preg_replace('/path="(.*?)"/i', 'path="' .
$project_folder_full_path . '"',
$contents);
# $contents = gpr_replacer($contents, "/title=\"(.*?)\"/i", "", "_");
$contents = gpr_replacer($contents, "/href=\"(.*?)\"/i", "", "_");
$contents = gpr_replacer($contents, "/\<Set\>(.*?)\<\/Set\>/i", "&",
"AND");
# $contents = gpr_replacer($contents, "/title=\"(.*?)\"/i", "&", "AND");
$contents = gpr_replacer($contents, "/href=\"(.*?)\"/i", "&", "AND");

if($handle = fopen($catalog, "w+"))
{ #write out contents and close file
fwrite($handle, $contents);
fclose($handle);

if($handle = opendir($gprf))
{ #open the data folder
#shell_exec("mkdir " . $gprf . "/redline");
while (($file = readdir($handle)) !== false)
{
if (($file !== ".") && ($file !== "..") && (substr($file, 0, 1) !== '.') &&
(substr($file,
-4) == '.DWF'))
{ #change the characters of .DWF's here too
$newfile = gpr_replacer($file, "/(.*)/i", "", "_");
#newfile is necessary, because the name has been changed after the first
rename
rename($gprf . $file, $gprf . $newfile);
rename($gprf . $newfile, $gprf . gpr_replacer($newfile, "/(.*)/i", "&",
"AND"));
#once everything has been done, add a value to the success var
$success++;
}
} #close the data folder
closedir($handle);
}
}
}
}
}
}
#return array of file_name, title, and path
if($success) { return $gpr; } else { return false; }
}

function gpr_replacer($contents, $regex, $character, $replacement) #
{ #if no replacement string is passed, use these
if(!$character) { $character = array("'", '"', ",", "=", "/", " "); }
#match the regular expression
preg_match_all ($regex, $contents, $contents_matches, PREG_SET_ORDER);
$contents_matches_cnt = count($contents_matches);
if($contents_matches_cnt) #if matches > 0
{
for($i=0; $i<$contents_matches_cnt; $i++) #do for each match
{ #replace the found string
$insert = str_replace($character, $replacement, $contents_matches[$i][1]);
$contents = str_replace($contents_matches[$i][1], $insert, $contents);
}
}
#return results
return $contents;
}



David T. Ashley

2006-10-30, 7:03 pm

"Synapse Syndrome" <synapse@NOSPAMgomez404.elitemail.org> wrote in message
news:wLudnUuBnvKmQt3YRVnyuA@bt.com...
> Hi
>
> I've been given what I am told is a PHP script to be used on my server. I
> do not know any PHP.


Based on reading the script you provided, it appears to be PHP.

You can find more information about PHP, including function definitions,
language syntax, etc. at:

http://www.php.net

I've left many questions unanswered. I'll leave that for other posters.

Dave.



David T. Ashley

2006-10-30, 7:03 pm

"Synapse Syndrome" <synapse@NOSPAMgomez404.elitemail.org> wrote in message
news:wLudnUuBnvKmQt3YRVnyuA@bt.com...
> Hi
>
> I've been given what I am told is a PHP script to be used on my server. I
> do not know any PHP.


As I mentioned in my previous post, it appears to be PHP.

> This is an example of the faulty output:
> http://test404.damnserver.com
>
> Following is what I have been told is a PHP script. It is meant for a
> freebsd or MacOS server. It apparently unpacks a zip file, deletes some
> temp files that MacOS makes, and renames the files to make them URL
> friendly. It then edits the Content.xml file with the filename changes.
>
> First of all, is this actually a PHP script?


Yes.

> I need it to run on a Windows server running IIS.


You should be able to do this. The script isn't that complicated. You
should be able to reimplement it using a M$-friendly language, such as .ASP.

PHP does run under Windows, but it might be easier to go with the M$ tool
chain.

> But someone that I gave it to look at has said that he thinks that it does
> some strange functions.


At first glance, the script looks rather standard. I'm not seeing any
"strange functions".

> and he thinks it's actually a BASH file.


It is not a BASH file. This is guaranteed PHP. I see many PHP functions in
the script ... it is PHP.

>and I have no hope of getting it to run on Windows.


The script is not that complicated. Just go through it line-by-line, look
up all the functions at http://www.php.net, and see what you can do on
Windows. You can either use PHP under Windows or reimplement some other
way, perhaps .ASP.

> I need something that does the same thing for a Windows server running the
> website linked earlier. I also do not want the unzip function, and it
> needs to be taken out.
>
> I would be grateful for any thoughts or opinion on this.


Reverse-engineer and reimplement.



Rik

2006-10-30, 7:03 pm

David T. Ashley wrote:
> "Synapse Syndrome" <synapse@NOSPAMgomez404.elitemail.org> wrote in
> message news:wLudnUuBnvKmQt3YRVnyuA@bt.com...
>
> As I mentioned in my previous post, it appears to be PHP.


It is indeed.

>
> You should be able to do this. The script isn't that complicated.
> You should be able to reimplement it using a M$-friendly language,
> such as .ASP.
>
> PHP does run under Windows, but it might be easier to go with the M$
> tool chain.


Well, in PHP it's just that easy.
The problem here is the script highly relies on shell_exec() for some
things (that needn't be shell functions, and mostly can be handled by
native PHP functions.

>
> At first glance, the script looks rather standard. I'm not seeing any
> "strange functions".


I think he's talking about the shell_exec()'s.

>
> The script is not that complicated. Just go through it line-by-line,
> look up all the functions at http://www.php.net, and see what you can
> do on Windows. You can either use PHP under Windows or reimplement
> some other way, perhaps .ASP.


Yup, it's a fairly easy script. I wouldn't do it for free though :-).
--
Rik Wasmus


Synapse Syndrome

2006-10-30, 7:03 pm


"David T. Ashley" <dta@e3ft.com> wrote in message
news:k260h.155798$882.46442@fe38.usenetserver.com...
> "Synapse Syndrome" <synapse@NOSPAMgomez404.elitemail.org> wrote in message
> news:wLudnUuBnvKmQt3YRVnyuA@bt.com...
>
> As I mentioned in my previous post, it appears to be PHP.
>
>
> Yes.
>
>
> You should be able to do this. The script isn't that complicated. You
> should be able to reimplement it using a M$-friendly language, such as
> .ASP.
>
> PHP does run under Windows, but it might be easier to go with the M$ tool
> chain.
>
>
> At first glance, the script looks rather standard. I'm not seeing any
> "strange functions".
>
>
> It is not a BASH file. This is guaranteed PHP. I see many PHP functions
> in the script ... it is PHP.
>
>
> The script is not that complicated. Just go through it line-by-line, look
> up all the functions at http://www.php.net, and see what you can do on
> Windows. You can either use PHP under Windows or reimplement some other
> way, perhaps .ASP.
>
>
> Reverse-engineer and reimplement.


Thanks for your comments. The only problem is that I have no real
programming skill. I just know some HTML/CSS coding, so adjusting this code
to run on Windows looks pretty difficult to me, let alone translating to
..ASP.

But, ideally, I wouild like it to run on .ASP, as my webserver is already
running that.

How long do you think it would take a beginner to go through this script and
get it running on Windows as PHP?

Cheers.

ss.


Synapse Syndrome

2006-10-30, 7:03 pm


"Rik" <luiheidsgoeroe@hotmail.com> wrote in message
news:28499$4540ee22$8259c69c$9848@news2.tudelft.nl...
>
> Yup, it's a fairly easy script. I wouldn't do it for free though :-).



Thanks for you comments. Do you think it it wouldn't take that long for a
beginner to get it running on Windows with PHP?

ss.


Rik

2006-10-30, 7:03 pm

Synapse Syndrome wrote:
> "Rik" <luiheidsgoeroe@hotmail.com> wrote in message
> news:28499$4540ee22$8259c69c$9848@news2.tudelft.nl...
>
>
> Thanks for you comments. Do you think it it wouldn't take that long
> for a beginner to get it running on Windows with PHP?


Well, this particular script? In my early starting days, it would have
taken me a couple of days perhaps. Certainly if you want the skip in
unzipping option, which is most of the work, it could even be done in a day
by a beginner. 'Beginner' in this is someone who is mildly familiar with
PHP, but hasn't really used it yet. If beginner ment 'never coded and never
looked at PHP code', it highly depends on who it is/how fast he/she picks
up things etc...
--
Rik Wasmus


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com