Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 PHP in Templates
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #1
PHP in Templates
Well, this seems to be requested a bit, and being a rather short plugin, I decided to make it.

Simply, this allows you to enter PHP code in templates, by enclosing it in <?php ?> tags.

For example:

HTML Code
1
2
3
4
5
6
7
8
<!-- template stuff -->
<td>
<?php
if($mybb->user['uid']) echo 'You are logged in.';
else echo 'You are not logged in.';
?>
</td>
<!-- more template stuff -->


Changes in v1.1

  • PHP will now inherit the correct scope of where the code is being eval'd.
  • Also added conditionals in templates
  • Added some function shortcuts

Examples of the above - you might use this in your postbit:

HTML Code
<if $post['fid5'] then>
Your game tag is <func htmlspecialchars_uni>{$post['fid5']}</func>
<elseif $post['fid6'] and $mybb->user['cancp'] then>
This user's lucky number is <func intval>{$post['fid6']}</func>
<else />{$post['fid7']}</if>


Tips:
- PHP will execute slower than template conditionals and function shortcuts, so I recommend using the former unless you need the latter



Attached File(s)
.php  phptpl.php (Size: 2.69 KB / Downloads: 861)

My Blog
(This post was last modified: 05-27-2008 09:01 PM by ZiNgA BuRgA.)
05-03-2008 03:41 PM
Find all posts by this user
ancraz Offline
Junior Member
**
Posts: 12
Joined: Feb 2008
Post: #2
RE: PHP in Templates
thz for the great mod.i have been looking for a mod that can do this thz

i want to place this on the template.when i do iam getting a error

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
//<div style="font-size: 16px;">If you see this, your server does not support PHP. Ask your web-hosting provider where you can add the PHP code on your website.</div>
/*ERROR<!--*/
class TNX_l
{
        var $_login = 'ancraz'; // your TNX login
        var $_timeout_connect = 5; // timeout - maximum time for the links to load (in seconds)
        var $_connect_using = 'fsock'; // curl or fsock - you may choose the way to connect
        var $_html_delimiter = '<br>'; // link devider - symbol between links (you may choose any symbol)
        var $_encoding = ''; // your website encoding. You may also use: KOI8-U, UTF-8, iso-8859-1 (iconv module must be installed on your server)
        var $_exceptions = 'PHPSESSID'; // here you can list URL's that you don't want to be indexed by TNX. Basically, those are URLs that are not indexed by search engines, or links to pages that don't exist. Don't change this value after it has been indexed by TNX system.
        /*******************************/
        var $_return_point = 0;
        var $_content = '';

        function TNX_l()
        {
                if($this->_connect_using == 'fsock' AND !function_exists('fsockopen')){echo 'fsock function is disabled on your server, contact your provider or try to use the CURL version of the TNX code.'; return false;}
                if($this->_connect_using == 'curl' AND !function_exists('curl_init')){echo 'Error, CURL is not supported, try using fsock.'; return false;}
                if(!empty($this->_encoding) AND !function_exists("iconv")){echo 'CURL function is disabled on your server, contact your provider or try to use the fsock version of the TNX code.'; return false;}

                if ($_SERVER['REQUEST_URI'] == '') $_SERVER['REQUEST_URI'] = '/';
                if (strlen($_SERVER['REQUEST_URI']) > 180) return false;

                if(!empty($this->_exceptions))
                {
                        $exceptions = explode(' ', $this->_exceptions);
                        for ($i=0; $i<sizeof($exceptions); $i++)
                        {
                                if($_SERVER['REQUEST_URI'] == $exceptions[$i]) return false;
                                if($exceptions[$i] == '/' AND preg_match("#^\/index\.\w{1,5}$#", $_SERVER['REQUEST_URI'])) return false;
                                if(strpos($_SERVER['REQUEST_URI'], $exceptions[$i]) !== false) return false;
                        }
                }

                $this->_login = strtolower($this->_login); $this->_host = $this->_login . '.tnx.net'; $file = base64_encode($_SERVER['REQUEST_URI']);
                $user_pref = substr($this->_login, 0, 2); $md5 = md5($file); $index = substr($md5, 0, 2);
                $site = str_replace('www.', '', $_SERVER['HTTP_HOST']);
                $this->_path = '/users/' . $user_pref . '/' . $this->_login . '/' . $site. '/' . substr($md5, 0, 1) . '/' . substr($md5, 1, 2) . '/' . $file . '.txt';
                $this->_url = 'http://' . $this->_host . $this->_path;
                $this->_content = $this->get_content();
                if($this->_content !== false)
                {
                        $this->_content_array = explode('<br>', $this->_content);
                        for ($i=0; $i<sizeof($this->_content_array); $i++)
                        {
                                $this->_content_array[$i] = trim($this->_content_array[$i]);
                        }
                }
        }
        /*!!!*/
        function show_link($num = false)
        {
                if(!isset($this->_content_array)) return false;
                $links = '';
                if(!isset($this->_content_array_count)){$this->_content_array_count = sizeof($this->_content_array);}
                if($this->_return_point >= $this->_content_array_count) return false;

                if($num === false OR $num >= $this->_content_array_count)
                {
                        for ($i = $this->_return_point; $i < $this->_content_array_count; $i++)
                        {
                                $links .= $this->_content_array[$i] . $this->_html_delimiter;
                        }
                        $this->_return_point += $this->_content_array_count;
                }
                else
                {
                        if($this->_return_point + $num > $this->_content_array_count) return false;
                        for ($i = $this->_return_point; $i < $num + $this->_return_point; $i++)
                        {
                                $links .= $this->_content_array[$i] . $this->_html_delimiter;
                        }
                        $this->_return_point += $num;
                }
                return (!empty($this->_encoding)) ? iconv("windows-1251", $this->_encoding, $links) : $links;
        }
        function get_content()
        {
                $user_agent = 'TNX_l ip: ' . $_SERVER['REMOTE_ADDR'];
                $page = '';
                if ($this->_connect_using == 'curl' OR ($this->_connect_using == '' AND function_exists('curl_init')))
                {
                        $c = curl_init($this->_url);
                        curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $this->_timeout_connect);
                        curl_setopt($c, CURLOPT_HEADER, false);
                        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($c, CURLOPT_TIMEOUT, $this->_timeout_connect);
                        curl_setopt($c, CURLOPT_USERAGENT, $user_agent);
                        $page = curl_exec($c);
                        if(curl_error($c) OR (curl_getinfo($c, CURLINFO_HTTP_CODE) != '200' AND curl_getinfo($c, CURLINFO_HTTP_CODE) != '404') OR strpos($page, 'fsockopen') !== false)
                        {
                                curl_close($c);
                                return false;
                        }
                        curl_close($c);
                }
                elseif($this->_connect_using == 'fsock')
                {
                        $buff = '';
                        $fp = @fsockopen($this->_host, 80, $errno, $errstr, $this->_timeout_connect);
                        if ($fp)
                        {
                                fputs($fp, "GET " . $this->_path . " HTTP/1.0\r\n");
                                fputs($fp, "Host: " . $this->_host . "\r\n");
                                fputs($fp, "User-Agent: " . $user_agent . "\r\n");
                                fputs($fp, "Connection: Close\r\n\r\n");

                                stream_set_blocking($fp, true);
                                stream_set_timeout($fp, $this->_timeout_connect);
                                $info = stream_get_meta_data($fp);

                                while ((!feof($fp)) AND (!$info['timed_out']))
                                {
                                        $buff .= fgets($fp, 4096);
                                        $info = stream_get_meta_data($fp);
                                }
                                fclose($fp);

                                if ($info['timed_out']) return false;

                                $page = explode("\r\n\r\n", $buff);
                                $page = $page[1];
                                if((!preg_match("#^HTTP/1\.\d 200$#", substr($buff, 0, 12)) AND !preg_match("#^HTTP/1\.\d 404$#", substr($buff, 0, 12))) OR $errno!=0 OR strpos($page, 'fsockopen') !== false) return false;
                        }
                }
                if(strpos($page, '404 Not Found')) return '';
                return $page;
        }
}
$tnx = new TNX_l();
echo $tnx->show_link(1); // first link
echo $tnx->show_link(1); // second link
echo $tnx->show_link(1); // third link
echo $tnx->show_link(); // remaining links
/*-->*/
?>


We can't say what is right & what is wrong but all we can do is to do the thing what we believe is right
[Image: tirrathcopygc2fy8.png]
[Image: 14613em4.gif]
For free hosting http://www.hostarchive.uni.cc
(This post was last modified: 05-08-2008 01:53 PM by ancraz.)
05-04-2008 11:15 PM
Find all posts by this user
ancraz Offline
Junior Member
**
Posts: 12
Joined: Feb 2008
Post: #3
RE: PHP in Templates
sorry for the double post when i use PHP in Templates i get the following error

PHP Code:
Parse error: syntax error, unexpected T_STRING in /home/underarc/public_html/inc/functions_post.php(380) : eval()'d code on line 1


We can't say what is right & what is wrong but all we can do is to do the thing what we believe is right
[Image: tirrathcopygc2fy8.png]
[Image: 14613em4.gif]
For free hosting http://www.hostarchive.uni.cc
05-18-2008 07:33 PM
Find all posts by this user
Guest
Unregistered
 
Post: #4
RE: PHP in Templates
(05-18-2008 07:33 PM)ancraz Wrote:  sorry for the double post when i use PHP in Templates i get the following error

PHP Code:
Parse error: syntax error, unexpected T_STRING in /home/underarc/public_html/inc/functions_post.php(380) : eval()'d code on line 1


I received the same thing.

Code:
Parse error: syntax error, unexpected T_STRING in /home/grindcor/public_html/inc/functions_post.php(380) : eval()'d code on line 1

05-18-2008 09:21 PM
grindcore Offline
Junior Member
**
Posts: 1
Joined: May 2008
Post: #5
RE: PHP in Templates
I received the same thing.

Code:
Parse error: syntax error, unexpected T_STRING in /home/grindcor/public_html/inc/functions_post.php(380) : eval()'d code on line 1

[/quote]

Sorry for the double post, wanted to subscribe to thread & receive email notification.

Thanks for your help ZingaBurga!

05-18-2008 09:27 PM
Find all posts by this user
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #6
RE: PHP in Templates
Are you sure the PHP code is valid in the first place?
Also, I highly suggest against putting that code in any postbit related templates - it's going to get executed like 10 times per page - bad for server load.

My Blog
05-20-2008 10:20 AM
Find all posts by this user
ancraz Offline
Junior Member
**
Posts: 12
Joined: Feb 2008
Post: #7
RE: RE: PHP in Templates
i think i wont uses code
anyway iam getting the error even if i dont place any code ?

We can't say what is right & what is wrong but all we can do is to do the thing what we believe is right
[Image: tirrathcopygc2fy8.png]
[Image: 14613em4.gif]
For free hosting http://www.hostarchive.uni.cc
05-20-2008 10:33 AM
Find all posts by this user
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #8
RE: PHP in Templates
Urgh, my bad - I see I made a mistake somewhere (I thought I fixed that, maybe I uploaded/overwrote the wrong file or something >_>).

Retry with the updated plugin in the first post.

My Blog
05-20-2008 12:23 PM
Find all posts by this user
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #9
RE: PHP in Templates
Updated this plugin to include template conditionals.

My Blog
05-27-2008 09:00 PM
Find all posts by this user
ancraz Offline
Junior Member
**
Posts: 12
Joined: Feb 2008
Post: #10
RE: PHP in Templates
I get the following error when i use the plugin
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/underarc/public_html/portal.php(579) : eval()'d code(1) : eval()'d code on line 1

Parse error: syntax error, unexpected T_STRING in /home/underarc/public_html/portal.php(579) : eval()'d code(1) : eval()'d code on line 1
Frown

We can't say what is right & what is wrong but all we can do is to do the thing what we believe is right
[Image: tirrathcopygc2fy8.png]
[Image: 14613em4.gif]
For free hosting http://www.hostarchive.uni.cc
07-20-2008 04:05 PM
Find all posts by this user

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: