Stumped

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thom McGrath

    Stumped

    Alright, I've run into a little problem. I have an html file with some
    <?php ?> tags and such that need to be evaluated. Some of you probably
    already know what I'm getting at.

    I need to be able to open the file, make some changes, then execute the
    code. I need it to behave like include() or require(). eval() was a
    decent solution, but it only supports raw php code.

    So what I'm doing is saving the modified code back out to a temp file,
    calling include() on the temp file, and then deleting it. Seems like an
    awful lot of work.

    Is there a way to do this better? Basically an include() that I pass
    code to rather than a file pointer?

    --
    - Thom McGrath
    Head of the ZeeTox Project and lead programmer for The ZAZ
    Contact me with iChat or AOL Instant Messenger: zazTekcor

  • CountScubula

    #2
    Re: Stumped

    "Thom McGrath" <thom@thezaz.co m> wrote in message
    news:2004021319 433416807%thom@ thezazcom...[color=blue]
    > Alright, I've run into a little problem. I have an html file with some
    > <?php ?> tags and such that need to be evaluated. Some of you probably
    > already know what I'm getting at.
    >
    > I need to be able to open the file, make some changes, then execute the
    > code. I need it to behave like include() or require(). eval() was a
    > decent solution, but it only supports raw php code.
    >
    > So what I'm doing is saving the modified code back out to a temp file,
    > calling include() on the temp file, and then deleting it. Seems like an
    > awful lot of work.
    >
    > Is there a way to do this better? Basically an include() that I pass
    > code to rather than a file pointer?
    >
    > --
    > - Thom McGrath
    > Head of the ZeeTox Project and lead programmer for The ZAZ
    > Contact me with iChat or AOL Instant Messenger: zazTekcor
    >[/color]

    Ok, I am a little confused, why nit just rename the .html file to .php and
    do an include?


    If that does not work, I may have a solution for you, but need to understand
    you a bit better. can you give example?

    or do you just want to eval() html and code? (this is possible)

    --
    Mike Bradley
    http://www.gzentools.com -- free online php tools


    Comment

    • Thom McGrath

      #3
      Re: Stumped

      On 2004-02-13 20:03:54 -0500, "CountScubu la" <me@scantek.hot mail.com> said:
      [color=blue]
      > Ok, I am a little confused, why nit just rename the .html file to .php and
      > do an include?
      >
      >
      > If that does not work, I may have a solution for you, but need to understand
      > you a bit better. can you give example?
      >
      > or do you just want to eval() html and code? (this is possible)[/color]

      eval() html and code. I can't do an include because the code is a bit
      dynamic. It has HTML and PHP mixed in, like a web page file (as that's
      pretty much what it is).

      --
      - Thom McGrath
      Head of the ZeeTox Project and lead programmer for The ZAZ
      Contact me with iChat or AOL Instant Messenger: zazTekcor

      Comment

      • CountScubula

        #4
        Re: Stumped

        here is some code, I borrowed some pieces:
        is will do the eval on a string with/embeded php tags

        <?php

        $s = "hi <?php print 'john doe'; ?>\n";

        print gzenEval($s);

        function gzenEvalBuffer( $string){ob_sta rt();eval("$str ing[2];");$return =
        ob_get_contents ();
        ob_end_clean(); return $return;}
        function gzenEvalPrintBu ffer($string) {ob_start();eva l("print
        $string[2];");$return = ob_get_contents ();
        ob_end_clean(); return $return;}
        function gzenEval($strin g){$string =
        preg_replace_ca llback("/(<\?=)(.*?)\?>/si","gzenEvalPr intBuffer",$str ing);
        return
        preg_replace_ca llback("/(<\?php|<\?)(.* ?)\?>/si","gzenEvalBu ffer",$string); }

        ?>

        --
        Mike Bradley
        http://www.gzentools.com -- free online php tools
        "Thom McGrath" <thom@thezaz.co m> wrote in message
        news:2004021320 492516807%thom@ thezazcom...[color=blue]
        > On 2004-02-13 20:03:54 -0500, "CountScubu la" <me@scantek.hot mail.com>[/color]
        said:[color=blue]
        >[color=green]
        > > Ok, I am a little confused, why nit just rename the .html file to .php[/color][/color]
        and[color=blue][color=green]
        > > do an include?
        > >
        > >
        > > If that does not work, I may have a solution for you, but need to[/color][/color]
        understand[color=blue][color=green]
        > > you a bit better. can you give example?
        > >
        > > or do you just want to eval() html and code? (this is possible)[/color]
        >
        > eval() html and code. I can't do an include because the code is a bit
        > dynamic. It has HTML and PHP mixed in, like a web page file (as that's
        > pretty much what it is).
        >
        > --
        > - Thom McGrath
        > Head of the ZeeTox Project and lead programmer for The ZAZ
        > Contact me with iChat or AOL Instant Messenger: zazTekcor
        >[/color]


        Comment

        • Thom McGrath

          #5
          Re: Stumped

          On 2004-02-13 21:49:20 -0500, "CountScubu la" <me@scantek.hot mail.com> said:
          [color=blue]
          > here is some code, I borrowed some pieces:
          > is will do the eval on a string with/embeded php tags[/color]

          But wouldn't that screw up the scope, because the eval is being
          executed inside a function?

          for emaple:
          $name = 'john doe';
          $s = "hi <?php print $name; ?>\n";
          print gzenEval($s);

          wouldn't that simply output

          'hi '

          ?

          --
          - Thom McGrath
          Head of the ZeeTox Project and lead programmer for The ZAZ
          Contact me with iChat or AOL Instant Messenger: zazTekcor

          Comment

          • CountScubula

            #6
            Re: Stumped

            nope, I tested before I posted, it works ok, this use to be part of my
            encoders, now I moved all static html into encoded php, so I didn't need
            this stub anymore, but thats another story




            --
            Mike Bradley
            http://www.gzentools.com -- free online php tools
            "Thom McGrath" <thom@thezaz.co m> wrote in message
            news:2004021322 571616807%thom@ thezazcom...[color=blue]
            > On 2004-02-13 21:49:20 -0500, "CountScubu la" <me@scantek.hot mail.com>[/color]
            said:[color=blue]
            >[color=green]
            > > here is some code, I borrowed some pieces:
            > > is will do the eval on a string with/embeded php tags[/color]
            >
            > But wouldn't that screw up the scope, because the eval is being
            > executed inside a function?
            >
            > for emaple:
            > $name = 'john doe';
            > $s = "hi <?php print $name; ?>\n";
            > print gzenEval($s);
            >
            > wouldn't that simply output
            >
            > 'hi '
            >
            > ?
            >
            > --
            > - Thom McGrath
            > Head of the ZeeTox Project and lead programmer for The ZAZ
            > Contact me with iChat or AOL Instant Messenger: zazTekcor
            >[/color]


            Comment

            • CountScubula

              #7
              Re: Stumped

              "Thom McGrath" <thom@thezaz.co m> wrote in message
              news:2004021322 571616807%thom@ thezazcom...[color=blue]
              > On 2004-02-13 21:49:20 -0500, "CountScubu la" <me@scantek.hot mail.com>[/color]
              said:[color=blue]
              >
              > But wouldn't that screw up the scope, because the eval is being
              > executed inside a function?
              >
              > for emaple:
              > $name = 'john doe';
              > $s = "hi <?php print $name; ?>\n";
              > print gzenEval($s);
              >
              > wouldn't that simply output
              >
              > 'hi '
              >
              > --
              > - Thom McGrath
              > Head of the ZeeTox Project and lead programmer for The ZAZ
              > Contact me with iChat or AOL Instant Messenger: zazTekcor
              >[/color]

              oops, I just saw what you typed:[color=blue]
              > $name = 'john doe';
              > $s = "hi <?php print $name; ?>\n";[/color]

              $s will be "hi <?php print john doe; ?>\n"
              do a print $s; to verify

              you would have to do this:
              $name = 'john doe';
              $s = "hi <?php print \"$name\"; ?>\n";
              print gzenEval($s);

              now if you wanted to eval the $name durring the eval process this can be
              done, bu why, when you can do it before it hits it, since everything is in a
              string.

              Ok, but if you must, this too can be done, (I do it in my encoder, so an
              encoded file can included a non encoded file).

              Lets make things real simple, can you post a mini version of what you are
              doing, not theory, but bits-o-code?



              --
              Mike Bradley
              http://www.gzentools.com -- free online php tools


              Comment

              • Thom McGrath

                #8
                Re: Stumped

                On 2004-02-13 23:09:21 -0500, "CountScubu la" <me@scantek.hot mail.com> said:
                [color=blue]
                > nope, I tested before I posted, it works ok, this use to be part of my
                > encoders, now I moved all static html into encoded php, so I didn't need
                > this stub anymore, but thats another story[/color]

                One last question regarding this code. The regex looks too greedy. For example:

                $s = 'Hi <?php print 'John Doe'; ?>. How are you this <?php print
                date('l'); ?>?';

                Wouldn't that cause trouble? Unless I'm missing something, the regex
                would match from the first <?php to the second ?>.

                --
                - Thom McGrath
                Head of the ZeeTox Project and lead programmer for The ZAZ
                Contact me with iChat or AOL Instant Messenger: zazTekcor

                Comment

                • Thom McGrath

                  #9
                  Re: Stumped

                  On 2004-02-13 23:18:28 -0500, "CountScubu la" <me@scantek.hot mail.com> said:
                  [color=blue]
                  > you would have to do this:
                  > $name = 'john doe';
                  > $s = "hi <?php print \"$name\"; ?>\n";
                  > print gzenEval($s);
                  >
                  > now if you wanted to eval the $name durring the eval process this can be
                  > done, bu why, when you can do it before it hits it, since everything is in a
                  > string.
                  >
                  > Ok, but if you must, this too can be done, (I do it in my encoder, so an
                  > encoded file can included a non encoded file).
                  >
                  > Lets make things real simple, can you post a mini version of what you are
                  > doing, not theory, but bits-o-code?[/color]

                  No, I can't. It's a) rather protected and b) rather complex. What I can
                  tell you is that this is for a template engine. My users would have the
                  ability to do something like $theme->ReadTemplate(" Site Header"); to
                  load the site's header into some variable.

                  The header may contain php code embedded with plain html, so printing
                  the variable won't work. The other possibility is that the template
                  will contain pure php code which can be loaded a similar way. Although
                  a dumb possibility, I can't control what my users will want to do.

                  So I want to provide my users a function like

                  Execute($theme->ReadTemplate(" Site Header"));

                  to handle this. There is a good possiblity that the code in the
                  template will need variables not in the template. So what I am doing
                  now is rather messy:

                  include(MakeInc lude($theme->ReadTemplate(" Site Header")));

                  which takes care of the job, but every time the template is needed, a
                  temp file will be created/overwritten, then included.

                  I am under the impression what I'm doing now might be the only or best
                  option, but I'm hoping somebody will have something better.

                  --
                  - Thom McGrath
                  Head of the ZeeTox Project and lead programmer for The ZAZ
                  Contact me with iChat or AOL Instant Messenger: zazTekcor

                  Comment

                  • CountScubula

                    #10
                    Re: Stumped

                    "Thom McGrath" <thom@thezaz.co m> wrote in message
                    news:2004021323 282775249%thom@ thezazcom...[color=blue]
                    >
                    > No, I can't. It's a) rather protected and b) rather complex. What I can
                    > tell you is that this is for a template engine. My users would have the
                    > ability to do something like $theme->ReadTemplate(" Site Header"); to
                    > load the site's header into some variable.
                    >[/color]


                    OK, so, would it be fair to say this:

                    $name = "john doe";

                    $s = "<B><?php print \$name; ?></B>"; // only \$ needed for direct string
                    not from file()
                    or
                    $s = file("localfile .template");

                    magicEval($s);

                    and you would like to have $name evaled in the magicEval, thus including a
                    file would work?

                    if so, let me know, email me at: info at gzentools dot com


                    --
                    Mike Bradley
                    http://www.gzentools.com -- free online php tools


                    Comment

                    • Chung Leong

                      #11
                      Re: Stumped

                      The cleanest way is probably to do a
                      include("http://localhost/code.php?some_p arams...") where code.php outputs
                      the code. Makes it easier to debug since you can just call up the PHP code
                      in your browser.

                      Be sure to compare $_SERVER['SERVER_ADDR'] to $_SERVER['REMOTE_ADDR'] so
                      that you don't reveal your PHP code to the world!

                      Uzytkownik "Thom McGrath" <thom@thezaz.co m> napisal w wiadomosci
                      news:2004021319 433416807%thom@ thezazcom...[color=blue]
                      > Alright, I've run into a little problem. I have an html file with some
                      > <?php ?> tags and such that need to be evaluated. Some of you probably
                      > already know what I'm getting at.
                      >
                      > I need to be able to open the file, make some changes, then execute the
                      > code. I need it to behave like include() or require(). eval() was a
                      > decent solution, but it only supports raw php code.
                      >
                      > So what I'm doing is saving the modified code back out to a temp file,
                      > calling include() on the temp file, and then deleting it. Seems like an
                      > awful lot of work.
                      >
                      > Is there a way to do this better? Basically an include() that I pass
                      > code to rather than a file pointer?
                      >
                      > --
                      > - Thom McGrath
                      > Head of the ZeeTox Project and lead programmer for The ZAZ
                      > Contact me with iChat or AOL Instant Messenger: zazTekcor
                      >[/color]


                      Comment

                      Working...