TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-25-2008, 12:49 AM   #1 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Angry help in a templating class

Hi guys, this is my first actual php post here and it's about a problem with a line in the simple templating class that i have. the class it's this:

PHP Code:
<?php
class View {
    public 
$template;
    public 
$d='%'//delimiter
    
    
function load($f) {
        
$f=WEB_HOST.'components/'.$f;
        if(!
file_exists($f)) return FALSE;
        
$this->template file_get_contents($f);
    }
    
    function 
delimit($delimeter) {
        
$this->d=$delimeter;
    }
    
    function 
replace($var$content) {
        
$this->template str_replace($this->d.$var.$this->d$content$this->template);
    }
    
    function 
publish() {
        eval(
"?>".$this->template."<?");
    }
}
?>
the problem seems to be in the function file_get_contents() because nothing seems to be saved in the template variable. I have tried change it by using file, fopen, fread functions and also even using curl, but nothing seems to save the text of the file in the variable. I am lost with this. Any help would help me, and thanks in advance

Also I would like your opinion in the publish function, i feel not very secure using the eval() function.
tony is offline  
Reply With Quote
Old 10-25-2008, 02:35 AM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

The only reason file_get_content wouldn't work that I know of is because the file doesn't exists. As for publish, just use a tmp php file
Enfernikus is offline  
Reply With Quote
Old 10-25-2008, 02:23 PM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

is there some permission problems with the file, i.e. does PHP have privileges to read it.

Eval is evil xD.

Try using output buffering to get the template file:

PHP Code:
ob_start();
include(
'template file');
$template ob_get_clean(); 
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 10-25-2008, 03:17 PM   #4 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

sketchMedia and Enfernikus thanks for the quick reply. I went back to the code and the reason it wasn't getting the content was because the WEB_HOST constant wasn't saving the right string.

Although I would appreciate if I can get another option for the publish method. It just bugs me that it's using eval and can't find another way
tony is offline  
Reply With Quote
Old 10-25-2008, 04:40 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I don't think there will be another way apart from eval. You should be using <?php though, as opposed to the shortened version, as I believe the shortened version can often be disabled in the PHP configuration.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
tony (10-26-2008)
Old 10-25-2008, 04:45 PM   #6 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

In order to make a more reliable form of publishing I included a semicache system that includes the cache'd file and exit all processing after that. The class below is just an example because you'd really need to get rid of ALL the white space before taking and comparing md5 as FTP transfer tends to add white spaces.

PHP Code:
<?php

class View
{
    private 
$template;
    private 
$d;
    private 
$md5;
    private 
$key;

    public function 
__construct()
    {
        
//Check if the cache folder exists
        
if ( !is_dir(WEB_HOST 'components/cache') )
        {
            @
mkdirWEB_HOST 'components/cache' );
        }

        
//Now establish default delimiter and key array
        
$this->'%';
        
$this->key = array();
    }

    public function 
load$f )
    {
        if ( 
file_exists(WEB_HOST 'compnents/' $f) )
        {
            
$this->template $f;
            return 
$this;
        }
    }

    public function 
add$key$item )
    {
        
$this->key[$key] = $item;
        return 
$this;
    }

    public function 
delimit$delimeter )
    {
        
$this->$delimeter;
    }

    public function 
publish()
    {
        
//Check the file's MD5
        
$file WEB_HOST 'components/' $this->template;
        
$storage file_get_contents$file );
        
$md5 md5_file$file );

        
//Check cache md5
        
$cacheFile WEB_HOST 'components/cache/' $this->template;
        
$cacheMd5 '';
        if( 
file_exists($cacheFile) )
        {
            
$cacheStorage file_get_contents$cacheFile );
               
$cacheMd5 substr$cacheStorage032 );
        }    

        
//now check the md5 sums
        
if ( $md5 !== $cacheMd5 )
        {
            
//It's not the same so send it through the loop
            
foreach ( $this->key as $key => $val )
            {
                
$storage str_replace$this->$key $this->d$val$storage );
            }

            
//now put it into the cache
            
$storage $md5 $storage;
            
file_put_contents$cacheFile$storage );
        }

        
//include the file and kill any PHP after that as it should be it
        
include ( $cacheFile );
        exit;

    }

}
?>
Enfernikus is offline  
Reply With Quote
The Following User Says Thank You to Enfernikus For This Useful Post:
tony (10-26-2008)
Old 10-26-2008, 06:54 PM   #7 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

thanks WildHoney I'll change that. Enfernikus thanks! i like that idea with cache files checked by the hash. i'll implement it see how it works.
although, would it be more effecient to use include or eval?
tony is offline  
Reply With Quote
Old 10-26-2008, 09:42 PM   #8 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

I'm tempted to believe include would be far faster then eval, but I'm just guessing I haven't actually seen or done any benchmarks.

Also I forgot to mentioned, if you're going to set scopes(P/P/P) for your variables then you should do the same thing for your functions even if they're just going to be public.
Enfernikus is offline  
Reply With Quote
Old 10-26-2008, 10:43 PM   #9 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

Yeah thought so too. I have search for any benchmark about it. I am going with include for this one. I don't know what you mean by function scope, though. I can see it in Java or c++ but function scope in php. then again i am not a pro in php.
tony is offline  
Reply With Quote
Old 10-26-2008, 11:05 PM   #10 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Sorry for the disgusting grammar errors in the example...Wrote it rather quickly.

PHP Code:

<?php

class Example
{
        
//This can be access by anyone
        
public function doThis() {}
        
        
//This can be access only the class and
        //the classes's children
        
protected function doThat() {}
        
        
//This can ONLY be used the class Example
        
private function doThese() {}
}

?>
Enfernikus is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:19 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design