Хранилища Subversion ant

Редакция

Редакция 2 | К новейшей редакции | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS

Редакция 2 Редакция 3
1
<?php
1
<?php
2
/**
2
/**
3
 * Smarty plugin
3
 * Smarty plugin
4
 * @package Smarty
4
 * @package Smarty
5
 * @subpackage plugins
5
 * @subpackage plugins
6
 */
6
 */
7
7
8
8
9
/**
9
/**
10
 * Smarty {fetch} plugin
10
 * Smarty {fetch} plugin
11
 *
11
 *
12
 * Type:     function<br>
12
 * Type:     function<br>
13
 * Name:     fetch<br>
13
 * Name:     fetch<br>
14
 * Purpose:  fetch file, web or ftp data and display results
14
 * Purpose:  fetch file, web or ftp data and display results
15
 * @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
15
 * @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
16
 *       (Smarty online manual)
16
 *       (Smarty online manual)
17
 * @author Monte Ohrt <monte at ohrt dot com>
17
 * @author Monte Ohrt <monte at ohrt dot com>
18
 * @param array
18
 * @param array
19
 * @param Smarty
19
 * @param Smarty
20
 * @return string|null if the assign parameter is passed, Smarty assigns the
20
 * @return string|null if the assign parameter is passed, Smarty assigns the
21
 *                     result to a template variable
21
 *                     result to a template variable
22
 */
22
 */
23
function smarty_function_fetch($params, &$smarty)
23
function smarty_function_fetch($params, &$smarty)
24
{
24
{
25
    if (empty($params['file'])) {
25
    if (empty($params['file'])) {
26
        $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
26
        $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
27
        return;
27
        return;
28
    }
28
    }
29
29
30
    $content = '';
30
    $content = '';
31
    if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
31
    if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
32
        $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
32
        $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
33
        require_once(SMARTY_CORE_DIR . 'core.is_secure.php');
33
        require_once(SMARTY_CORE_DIR . 'core.is_secure.php');
34
        if(!smarty_core_is_secure($_params, $smarty)) {
34
        if(!smarty_core_is_secure($_params, $smarty)) {
35
            $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
35
            $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
36
            return;
36
            return;
37
        }
37
        }
38
       
38
       
39
        // fetch the file
39
        // fetch the file
40
        if($fp = @fopen($params['file'],'r')) {
40
        if($fp = @fopen($params['file'],'r')) {
41
            while(!feof($fp)) {
41
            while(!feof($fp)) {
42
                $content .= fgets ($fp,4096);
42
                $content .= fgets ($fp,4096);
43
            }
43
            }
44
            fclose($fp);
44
            fclose($fp);
45
        } else {
45
        } else {
46
            $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
46
            $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
47
            return;
47
            return;
48
        }
48
        }
49
    } else {
49
    } else {
50
        // not a local file
50
        // not a local file
51
        if(preg_match('!^http://!i',$params['file'])) {
51
        if(preg_match('!^http://!i',$params['file'])) {
52
            // http fetch
52
            // http fetch
53
            if($uri_parts = parse_url($params['file'])) {
53
            if($uri_parts = parse_url($params['file'])) {
54
                // set defaults
54
                // set defaults
55
                $host = $server_name = $uri_parts['host'];
55
                $host = $server_name = $uri_parts['host'];
56
                $timeout = 30;
56
                $timeout = 30;
57
                $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
57
                $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
58
                $agent = "Smarty Template Engine ".$smarty->_version;
58
                $agent = "Smarty Template Engine ".$smarty->_version;
59
                $referer = "";
59
                $referer = "";
60
                $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
60
                $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
61
                $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
61
                $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
62
                $_is_proxy = false;
62
                $_is_proxy = false;
63
                if(empty($uri_parts['port'])) {
63
                if(empty($uri_parts['port'])) {
64
                    $port = 80;
64
                    $port = 80;
65
                } else {
65
                } else {
66
                    $port = $uri_parts['port'];
66
                    $port = $uri_parts['port'];
67
                }
67
                }
68
                if(!empty($uri_parts['user'])) {
68
                if(!empty($uri_parts['user'])) {
69
                    $user = $uri_parts['user'];
69
                    $user = $uri_parts['user'];
70
                }
70
                }
71
                if(!empty($uri_parts['pass'])) {
71
                if(!empty($uri_parts['pass'])) {
72
                    $pass = $uri_parts['pass'];
72
                    $pass = $uri_parts['pass'];
73
                }
73
                }
74
                // loop through parameters, setup headers
74
                // loop through parameters, setup headers
75
                foreach($params as $param_key => $param_value) {
75
                foreach($params as $param_key => $param_value) {
76
                    switch($param_key) {
76
                    switch($param_key) {
77
                        case "file":
77
                        case "file":
78
                        case "assign":
78
                        case "assign":
79
                        case "assign_headers":
79
                        case "assign_headers":
80
                            break;
80
                            break;
81
                        case "user":
81
                        case "user":
82
                            if(!empty($param_value)) {
82
                            if(!empty($param_value)) {
83
                                $user = $param_value;
83
                                $user = $param_value;
84
                            }
84
                            }
85
                            break;
85
                            break;
86
                        case "pass":
86
                        case "pass":
87
                            if(!empty($param_value)) {
87
                            if(!empty($param_value)) {
88
                                $pass = $param_value;
88
                                $pass = $param_value;
89
                            }
89
                            }
90
                            break;
90
                            break;
91
                        case "accept":
91
                        case "accept":
92
                            if(!empty($param_value)) {
92
                            if(!empty($param_value)) {
93
                                $accept = $param_value;
93
                                $accept = $param_value;
94
                            }
94
                            }
95
                            break;
95
                            break;
96
                        case "header":
96
                        case "header":
97
                            if(!empty($param_value)) {
97
                            if(!empty($param_value)) {
98
                                if(!preg_match('![\w\d-]+: .+!',$param_value)) {
98
                                if(!preg_match('![\w\d-]+: .+!',$param_value)) {
99
                                    $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
99
                                    $smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'");
100
                                    return;
100
                                    return;
101
                                } else {
101
                                } else {
102
                                    $extra_headers[] = $param_value;
102
                                    $extra_headers[] = $param_value;
103
                                }
103
                                }
104
                            }
104
                            }
105
                            break;
105
                            break;
106
                        case "proxy_host":
106
                        case "proxy_host":
107
                            if(!empty($param_value)) {
107
                            if(!empty($param_value)) {
108
                                $proxy_host = $param_value;
108
                                $proxy_host = $param_value;
109
                            }
109
                            }
110
                            break;
110
                            break;
111
                        case "proxy_port":
111
                        case "proxy_port":
112
                            if(!preg_match('!\D!', $param_value)) {
112
                            if(!preg_match('!\D!', $param_value)) {
113
                                $proxy_port = (int) $param_value;
113
                                $proxy_port = (int) $param_value;
114
                            } else {
114
                            } else {
115
                                $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
115
                                $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
116
                                return;
116
                                return;
117
                            }
117
                            }
118
                            break;
118
                            break;
119
                        case "agent":
119
                        case "agent":
120
                            if(!empty($param_value)) {
120
                            if(!empty($param_value)) {
121
                                $agent = $param_value;
121
                                $agent = $param_value;
122
                            }
122
                            }
123
                            break;
123
                            break;
124
                        case "referer":
124
                        case "referer":
125
                            if(!empty($param_value)) {
125
                            if(!empty($param_value)) {
126
                                $referer = $param_value;
126
                                $referer = $param_value;
127
                            }
127
                            }
128
                            break;
128
                            break;
129
                        case "timeout":
129
                        case "timeout":
130
                            if(!preg_match('!\D!', $param_value)) {
130
                            if(!preg_match('!\D!', $param_value)) {
131
                                $timeout = (int) $param_value;
131
                                $timeout = (int) $param_value;
132
                            } else {
132
                            } else {
133
                                $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
133
                                $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'");
134
                                return;
134
                                return;
135
                            }
135
                            }
136
                            break;
136
                            break;
137
                        default:
137
                        default:
138
                            $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
138
                            $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'");
139
                            return;
139
                            return;
140
                    }
140
                    }
141
                }
141
                }
142
                if(!empty($proxy_host) && !empty($proxy_port)) {
142
                if(!empty($proxy_host) && !empty($proxy_port)) {
143
                    $_is_proxy = true;
143
                    $_is_proxy = true;
144
                    $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
144
                    $fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout);
145
                } else {
145
                } else {
146
                    $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
146
                    $fp = fsockopen($server_name,$port,$errno,$errstr,$timeout);
147
                }
147
                }
148
148
149
                if(!$fp) {
149
                if(!$fp) {
150
                    $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
150
                    $smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)");
151
                    return;
151
                    return;
152
                } else {
152
                } else {
153
                    if($_is_proxy) {
153
                    if($_is_proxy) {
154
                        fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
154
                        fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
155
                    } else {
155
                    } else {
156
                        fputs($fp, "GET $uri HTTP/1.0\r\n");
156
                        fputs($fp, "GET $uri HTTP/1.0\r\n");
157
                    }
157
                    }
158
                    if(!empty($host)) {
158
                    if(!empty($host)) {
159
                        fputs($fp, "Host: $host\r\n");
159
                        fputs($fp, "Host: $host\r\n");
160
                    }
160
                    }
161
                    if(!empty($accept)) {
161
                    if(!empty($accept)) {
162
                        fputs($fp, "Accept: $accept\r\n");
162
                        fputs($fp, "Accept: $accept\r\n");
163
                    }
163
                    }
164
                    if(!empty($agent)) {
164
                    if(!empty($agent)) {
165
                        fputs($fp, "User-Agent: $agent\r\n");
165
                        fputs($fp, "User-Agent: $agent\r\n");
166
                    }
166
                    }
167
                    if(!empty($referer)) {
167
                    if(!empty($referer)) {
168
                        fputs($fp, "Referer: $referer\r\n");
168
                        fputs($fp, "Referer: $referer\r\n");
169
                    }
169
                    }
170
                    if(isset($extra_headers) && is_array($extra_headers)) {
170
                    if(isset($extra_headers) && is_array($extra_headers)) {
171
                        foreach($extra_headers as $curr_header) {
171
                        foreach($extra_headers as $curr_header) {
172
                            fputs($fp, $curr_header."\r\n");
172
                            fputs($fp, $curr_header."\r\n");
173
                        }
173
                        }
174
                    }
174
                    }
175
                    if(!empty($user) && !empty($pass)) {
175
                    if(!empty($user) && !empty($pass)) {
176
                        fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
176
                        fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");
177
                    }
177
                    }
178
178
179
                    fputs($fp, "\r\n");
179
                    fputs($fp, "\r\n");
180
                    while(!feof($fp)) {
180
                    while(!feof($fp)) {
181
                        $content .= fgets($fp,4096);
181
                        $content .= fgets($fp,4096);
182
                    }
182
                    }
183
                    fclose($fp);
183
                    fclose($fp);
184
                    $csplit = split("\r\n\r\n",$content,2);
184
                    $csplit = split("\r\n\r\n",$content,2);
185
185
186
                    $content = $csplit[1];
186
                    $content = $csplit[1];
187
187
188
                    if(!empty($params['assign_headers'])) {
188
                    if(!empty($params['assign_headers'])) {
189
                        $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
189
                        $smarty->assign($params['assign_headers'],split("\r\n",$csplit[0]));
190
                    }
190
                    }
191
                }
191
                }
192
            } else {
192
            } else {
193
                $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
193
                $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
194
                return;
194
                return;
195
            }
195
            }
196
        } else {
196
        } else {
197
            // ftp fetch
197
            // ftp fetch
198
            if($fp = @fopen($params['file'],'r')) {
198
            if($fp = @fopen($params['file'],'r')) {
199
                while(!feof($fp)) {
199
                while(!feof($fp)) {
200
                    $content .= fgets ($fp,4096);
200
                    $content .= fgets ($fp,4096);
201
                }
201
                }
202
                fclose($fp);
202
                fclose($fp);
203
            } else {
203
            } else {
204
                $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
204
                $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
205
                return;
205
                return;
206
            }
206
            }
207
        }
207
        }
208
208
209
    }
209
    }
210
210
211
211
212
    if (!empty($params['assign'])) {
212
    if (!empty($params['assign'])) {
213
        $smarty->assign($params['assign'],$content);
213
        $smarty->assign($params['assign'],$content);
214
    } else {
214
    } else {
215
        return $content;
215
        return $content;
216
    }
216
    }
217
}
217
}
218
218
219
/* vim: set expandtab: */
219
/* vim: set expandtab: */
220
220
221
?>
221
?>
222
 
222