Perl to PHP convesion

Started by Microcyb, December 21, 2004, 11:51:41 AM

Previous topic - Next topic

Microcyb

 :D
I folks!  Hope the holiday will be good to this year.

As for me, well I am stuck and was hoping some of your master perl/php people could help.

I need to convert the following from perl to php


#!/usr/bin/perl -w -T

# includes
use Imagine::DR_DB;
use Imagine::DR_DB_SELECT;
use Imagine::DR_ERROR;
use Imagine::DR_UTILS;
use strict;
#
#
#
#################################################################################################
# Constants
#my $CONST_LocalDreamRoot = "/usr/Imagine/Dreams/";
#my $CONST_LocalScreenshotRoot = "/usr/Imagine/Dreams/";
my $CONST_LocalDreamRoot = "/home/microcyb/public_html/dreams/data/";
my $CONST_LocalScreenshotRoot = "/home/microcyb/public_html/dreams/datab/";
#
#
#
#################################################################################################
# Main

      # Save the file
      my $sFileName = sprintf("W_%X", $iDream_ID);
      $sFileName =~ /(\w+)/ || die "Bad filename";
      $sFileName = $1;

      my $sScreenshot = $CONST_LocalScreenshotRoot . $sFileName. ".jpg";

      # - Screenshot
      open(fScreenshot, "+>$sScreenshot") || die "Failed to Open Error File";
      binmode(fScreenshot);
        print fScreenshot substr($sBuffer, 0, $Input{'CI_ScreenshotSize'});
      close fScreenshot;
      chmod(0664, $sScreenshot);

      # - Dream
      my $sDream = $CONST_LocalDreamRoot . $sFileName. ".Dream";
      open(fDream, "+>$sDream") || die "Failed to Open Error File";
      binmode(fDream);
        print fDream substr($sBuffer, $Input{'CI_ScreenshotSize'}, $Input{'CI_DreamSize'});
      close fDream;
      chmod(0664, $sDream);



my %Input;
my $hDB = Imagine::DR_DB->new();
my $hUtils = Imagine::DR_UTILS->new();
my $sBufferHeader;
my $sBuffer;
eval
{
   print "Content-type: text/html\n\n";

   # read in the content
   my $sBufferContent;
   binmode(STDIN);
   read (STDIN,$sBufferContent,$ENV{'CONTENT_LENGTH'});

   ($sBufferHeader, $sBuffer)  = split(/\n/, $sBufferContent);
   $sBuffer = substr($sBufferContent, length($sBufferHeader) + 1, $ENV{'CONTENT_LENGTH'} - length($sBufferHeader) - 1);

   my @Pairs = split(/&/, $sBufferHeader);
   my $sPair;
   my $sName;
   my $sValue = "";

   foreach $sPair (@Pairs)
   {
      ($sName, $sValue)=split(/=/, $sPair);

      # replace + and %-encoding
      $sName =~ tr/+/ /;
      $sValue =~ tr/+/ /;

      $sName =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
      $sValue =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;

      $sName =~ s/^\s//;
      $sName =~ s/\s$//;

      $Input{$sName} = $sValue;
   }

   #validate parameters
   exists $Input{'CI_Version'} || die 'Input Parameter Not Found CI_Version';
   exists $Input{'CI_LicenseID'} || die 'Input Parameter Not Found CI_LicenseID';
   exists $Input{'CI_Cypher'} || die 'Input Parameter Not Found CI_Cypher';
   exists $Input{'CI_DreamTitle'} || die 'Input Parameter Not Found CI_DreamTitle';
   exists $Input{'CI_DreamAuthor'} || die 'Input Parameter Not Found CI_DreamAuthor';
   exists $Input{'CI_ScreenshotSize'} || die 'Input Parameter Not Found CI_ScreenshotSize';
   exists $Input{'CI_DreamSize'} || die 'Input Parameter Not Found CI_DreamSize';

   $hUtils->UnTaint_Integer(\$Input{'CI_Version'});
   $hUtils->UnTaint_Integer(\$Input{'CI_LicenseID'});
   $hUtils->UnTaint_String(\$Input{'CI_Cypher'});
   $hUtils->UnTaint_String(\$Input{'CI_DreamAuthor'});
   $hUtils->UnTaint_String(\$Input{'CI_DreamTitle'});
   $hUtils->UnTaint_Integer(\$Input{'CI_ScreenshotSize'});
   $hUtils->UnTaint_Integer(\$Input{'CI_DreamSize'});

   # Publish
   Publish($hDB, $hUtils, %Input);
};
if($@)
{
   print "Error\n";
   print "Your Dream can not be published at this time";

   # format Error Message
     my $sError = "ERROR - DreamPublish - $@";
      my $hError = Imagine::DR_ERROR->new();
   $hError->Report_Error($sError);
}
#
#
#
#################################################################################################
sub Publish
{
    # read parms
   my($hDB, $hUtils, %Input) = @_;

   my $sStatus;
   eval
   {
      my $hDB_select = Imagine::DR_DB_SELECT->new($hDB);
      my $sQ_Cypher = $hDB->GetConnection()->quote( substr($Input{'CI_Cypher'}, 0, 5) );
      $hDB_select->Select("SELECT * FROM Licenses WHERE License_ID = $Input{'CI_LicenseID'} AND substr(Cypher, 1, 5) = $sQ_Cypher");
      my $Row = $hDB_select->GetRow();
      $sStatus = $Row->{'status'};
   };
   if ($@) # Not a valid License
   {
      print "Error\n";
      print "Only registered users may submit dreams.\r\n";
      print "Please check your License details.\r\n";
      return;
   }

   if ($sStatus eq 'D') # Inactive account
   {
      print "Error\n";
      print "Your account is disabled and cannot be used for publishing.\r\n";
      print "Please contact support\@DreamRender.com.\r\n";
      return;
   }

   my $iDream_ID;
   eval
   {
      # Get the next dream ID
      {
         my $hDB_select = Imagine::DR_DB_SELECT->new($hDB);
         $hDB_select->Select("SELECT nextval('Dreams_Dream_ID_seq') as nextval");
         my $Seq  = $hDB_select->GetRow();
         $iDream_ID = $Seq->{'nextval'};
      }

      # Save the file
      my $sFileName = sprintf("W_%X", $iDream_ID);
      $sFileName =~ /(\w+)/ || die "Bad filename";
      $sFileName = $1;

      my $sScreenshot = $CONST_LocalScreenshotRoot . $sFileName. ".jpg";

      # - Screenshot
      open(fScreenshot, "+>$sScreenshot") || die "Failed to Open Error File";
      binmode(fScreenshot);
        print fScreenshot substr($sBuffer, 0, $Input{'CI_ScreenshotSize'});
      close fScreenshot;
      chmod(0664, $sScreenshot);

      # - Dream
      my $sDream = $CONST_LocalDreamRoot . $sFileName. ".Dream";
      open(fDream, "+>$sDream") || die "Failed to Open Error File";
      binmode(fDream);
        print fDream substr($sBuffer, $Input{'CI_ScreenshotSize'}, $Input{'CI_DreamSize'});
      close fDream;
      chmod(0664, $sDream);

      # Add the entry into the database
      my $sQ_Title = $hDB->GetConnection()->quote($Input{'CI_DreamTitle'});
      my $sQ_Author = $hDB->GetConnection()->quote($Input{'CI_DreamAuthor'});
      my $sSQL = "";
      my $sFields = "(Dream_ID, License_ID, Title, Author, Date, State, Version, Size)";
      my $sValues = "VALUES($iDream_ID, $Input{'CI_LicenseID'}, $sQ_Title, $sQ_Author, now(), 'A', $Input{'CI_Version'}, $Input{'CI_DreamSize'})";
      $sSQL = "INSERT INTO Dreams $sFields $sValues";
      $hDB->Execute($sSQL);

      print "Success\n";
      print "$Input{'CI_DreamTitle'} has been published";
    };
   if ($@) #catch Complete failure
    {
      die $@;
    }
}
#
#
#


[Unknown]

Maybe if you could port parts of it, and leave comments on the sections you're not sure of?

-[Unknown]

Microcyb

Quote from: [Unknown] on December 22, 2004, 04:06:16 AM
Maybe if you could port parts of it, and leave comments on the sections you're not sure of?

-[Unknown]

The only part I do not care about is the DB parts,  I tried to shorten it before, and you helped me getting it to work, but the issue I have is that it seems to not generate the binary file does not seemto want to generate.

I tried running the .pl file itself but only get an error.

So in short just want to files to upload.


[Unknown]

Maybe you should read the PHP documentation on uploading files?  It really doesn't help you learn for me to do it for you :P.

-[Unknown]

Grudge

I love PHP or it's ease in uploding files. At the *very* basic level you can do: (Where "newfile" is the name of your file input box)


if (!empty(_FILES['newfile']['tmp_name']))
move_uploaded_file($_FILES['newfile']['tmp_name'], $destination);

Search php.net for $_FILES and they will teach you all :P
I'm only a half geek really...


Microcyb

Hey thanks for the link. 
Perl is much more complex than php in my opinion.

Advertisement: