Хранилища Subversion ant

Редакция

Содержимое файла | Последнее изменение | Открыть журнал | RSS

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