| 
									
										
										
										
											2002-01-31 20:49:40 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * Smarty plugin | 
					
						
							|  |  |  |  * ------------------------------------------------------------- | 
					
						
							|  |  |  |  * Type:     function
 | 
					
						
							|  |  |  |  * Name:     fetch | 
					
						
							|  |  |  |  * Purpose:  fetch file, web or ftp data and display results | 
					
						
							|  |  |  |  * ------------------------------------------------------------- | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2002-02-20 22:24:32 +00:00
										 |  |  | function smarty_function_fetch($params, &$smarty) | 
					
						
							| 
									
										
										
										
											2002-01-31 20:49:40 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  | 	$file = $params['file']; | 
					
						
							| 
									
										
										
										
											2002-01-31 20:49:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (empty($file)) { | 
					
						
							| 
									
										
										
										
											2003-01-28 14:33:50 +00:00
										 |  |  |         $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty"); | 
					
						
							| 
									
										
										
										
											2002-01-31 20:49:40 +00:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  |     if ($smarty->security && !preg_match('!^(http|ftp)://!i', $file)) { | 
					
						
							|  |  |  |         // fetching file, make sure it comes from secure directory
 | 
					
						
							| 
									
										
										
										
											2002-02-20 22:24:32 +00:00
										 |  |  |         foreach ($smarty->secure_dir as $curr_dir) { | 
					
						
							| 
									
										
										
										
											2002-01-31 20:49:40 +00:00
										 |  |  |             if (substr(realpath($file), 0, strlen(realpath($curr_dir))) == realpath($curr_dir)) { | 
					
						
							|  |  |  |                 $resource_is_secure = true; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!$resource_is_secure) { | 
					
						
							| 
									
										
										
										
											2003-01-28 14:33:50 +00:00
										 |  |  |             $smarty->_trigger_fatal_error("[plugin] (secure mode) fetch '$file' is not allowed"); | 
					
						
							| 
									
										
										
										
											2002-01-31 20:49:40 +00:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  | 		// fetch the file
 | 
					
						
							| 
									
										
										
										
											2002-05-06 14:50:37 +00:00
										 |  |  | 		if($fp = @fopen($file,'r')) { | 
					
						
							|  |  |  | 			while(!feof($fp)) { | 
					
						
							|  |  |  | 				$content .= fgets ($fp,4096); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			fclose($fp); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2003-01-28 14:33:50 +00:00
										 |  |  |             $smarty->_trigger_fatal_error("[plugin] fetch cannot read file '$file'"); | 
					
						
							| 
									
										
										
										
											2002-05-06 14:50:37 +00:00
										 |  |  |             return;			 | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  | 		// not a local file
 | 
					
						
							|  |  |  | 		if(preg_match('!^http://!i',$file)) { | 
					
						
							|  |  |  | 			// http fetch
 | 
					
						
							|  |  |  | 			if($uri_parts = parse_url($file)) { | 
					
						
							|  |  |  | 				// set defaults
 | 
					
						
							|  |  |  | 				$host = $server_name = $uri_parts['host']; | 
					
						
							|  |  |  | 				$timeout = 30; | 
					
						
							|  |  |  | 				$accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*"; | 
					
						
							|  |  |  | 				$agent = "Smarty Template Engine ".$smarty->_version; | 
					
						
							| 
									
										
										
										
											2002-03-13 23:30:56 +00:00
										 |  |  | 				$referer = ""; | 
					
						
							| 
									
										
										
										
											2002-12-08 23:02:59 +00:00
										 |  |  | 				$uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/'; | 
					
						
							|  |  |  | 				$uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : ''; | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  | 				$_is_proxy = false; | 
					
						
							|  |  |  | 				if(empty($uri_parts['port'])) { | 
					
						
							|  |  |  | 					$port = 80; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$port = $uri_parts['port']; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if(empty($uri_parts['user'])) { | 
					
						
							| 
									
										
										
										
											2003-02-24 14:23:39 +00:00
										 |  |  | 					$user = ''; | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  | 				}				 | 
					
						
							|  |  |  | 				// loop through parameters, setup headers
 | 
					
						
							|  |  |  | 				foreach($params as $param_key => $param_value) {			 | 
					
						
							|  |  |  | 					switch($param_key) { | 
					
						
							|  |  |  | 						case "file": | 
					
						
							|  |  |  | 						case "assign": | 
					
						
							|  |  |  | 						case "assign_headers": | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						case "user": | 
					
						
							|  |  |  | 							if(!empty($param_value)) { | 
					
						
							|  |  |  | 								$user = $param_value; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						case "pass": | 
					
						
							|  |  |  | 							if(!empty($param_value)) { | 
					
						
							|  |  |  | 								$pass = $param_value; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						case "accept": | 
					
						
							|  |  |  | 							if(!empty($param_value)) { | 
					
						
							|  |  |  | 								$accept = $param_value; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						case "header": | 
					
						
							|  |  |  | 							if(!empty($param_value)) { | 
					
						
							|  |  |  | 								if(!preg_match('![\w\d-]+: .+!',$param_value)) { | 
					
						
							| 
									
										
										
										
											2003-01-28 14:33:50 +00:00
										 |  |  |             						$smarty->_trigger_fatal_error("[plugin] invalid header format '".$param_value."'"); | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  |             						return;									 | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									$extra_headers[] = $param_value; | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						case "proxy_host": | 
					
						
							|  |  |  | 							if(!empty($param_value)) { | 
					
						
							|  |  |  | 								$proxy_host = $param_value; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						case "proxy_port": | 
					
						
							|  |  |  | 							if(!preg_match('!\D!', $param_value)) { | 
					
						
							|  |  |  | 								$proxy_port = (int) $param_value; | 
					
						
							|  |  |  | 							} else { | 
					
						
							| 
									
										
										
										
											2003-01-28 14:33:50 +00:00
										 |  |  |             					$smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'"); | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  |             					return;									 | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						case "agent": | 
					
						
							|  |  |  | 							if(!empty($param_value)) { | 
					
						
							|  |  |  | 								$agent = $param_value; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						case "referer": | 
					
						
							|  |  |  | 							if(!empty($param_value)) { | 
					
						
							|  |  |  | 								$referer = $param_value; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						case "timeout": | 
					
						
							|  |  |  | 							if(!preg_match('!\D!', $param_value)) { | 
					
						
							|  |  |  | 								$timeout = (int) $param_value; | 
					
						
							|  |  |  | 							} else { | 
					
						
							| 
									
										
										
										
											2003-01-28 14:33:50 +00:00
										 |  |  |             					$smarty->_trigger_fatal_error("[plugin] invalid value for attribute '".$param_key."'"); | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  |             					return;									 | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						default: | 
					
						
							| 
									
										
										
										
											2003-01-28 14:33:50 +00:00
										 |  |  |             				$smarty->_trigger_fatal_error("[plugin] unrecognized attribute '".$param_key."'"); | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  |             				return; | 
					
						
							|  |  |  | 					}			 | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if(!empty($proxy_host) && !empty($proxy_port)) { | 
					
						
							|  |  |  | 					$_is_proxy = true; | 
					
						
							|  |  |  | 					$fp = fsockopen($proxy_host,$proxy_port,$errno,$errstr,$timeout); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					$fp = fsockopen($server_name,$port,$errno,$errstr,$timeout); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if(!$fp) { | 
					
						
							| 
									
										
										
										
											2003-01-28 14:33:50 +00:00
										 |  |  |             		$smarty->_trigger_fatal_error("[plugin] unable to fetch: $errstr ($errno)"); | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  |             		return;				 | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					if($_is_proxy) { | 
					
						
							|  |  |  | 						fputs($fp, "GET $file HTTP/1.0\r\n");						 | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						fputs($fp, "GET $uri HTTP/1.0\r\n"); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if(!empty($host)) { | 
					
						
							|  |  |  | 						fputs($fp, "Host: $host\r\n"); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if(!empty($accept)) { | 
					
						
							|  |  |  | 						fputs($fp, "Accept: $accept\r\n"); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if(!empty($agent)) { | 
					
						
							|  |  |  | 						fputs($fp, "User-Agent: $agent\r\n"); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if(!empty($referer)) { | 
					
						
							|  |  |  | 						fputs($fp, "Referer: $referer\r\n"); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2003-02-24 14:23:39 +00:00
										 |  |  | 					if(isset($extra_headers) && is_array($extra_headers)) { | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  | 						foreach($extra_headers as $curr_header) { | 
					
						
							|  |  |  | 							fputs($fp, $curr_header."\r\n"); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if(!empty($user) && !empty($pass)) { | 
					
						
							|  |  |  | 						fputs($fp, "Authorization: BASIC ".base64_encode("$user:$pass")."\r\n");						 | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2003-02-24 14:23:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					$content = '';					 | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  | 					fputs($fp, "\r\n"); | 
					
						
							|  |  |  | 					while(!feof($fp)) { | 
					
						
							|  |  |  | 						$content .= fgets($fp,4096); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					fclose($fp);					 | 
					
						
							|  |  |  | 					$csplit = split("\r\n\r\n",$content,2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					$content = $csplit[1]; | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					if(!empty($params['assign_headers'])) { | 
					
						
							|  |  |  | 						$smarty->assign($params['assign_headers'],split("\r\n",$csplit[0])); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2003-01-28 14:33:50 +00:00
										 |  |  |             		$smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax"); | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  |             		return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			// ftp fetch
 | 
					
						
							| 
									
										
										
										
											2002-05-06 14:50:37 +00:00
										 |  |  | 			if($fp = @fopen($file,'r')) { | 
					
						
							|  |  |  | 				while(!feof($fp)) { | 
					
						
							|  |  |  | 					$content .= fgets ($fp,4096); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				fclose($fp); | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2003-01-28 14:33:50 +00:00
										 |  |  |             	$smarty->_trigger_fatal_error("[plugin] fetch cannot read file '$file'"); | 
					
						
							| 
									
										
										
										
											2002-05-06 14:50:37 +00:00
										 |  |  |             	return;			 | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-01-31 20:49:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-13 23:14:26 +00:00
										 |  |  |     if (!empty($params['assign'])) { | 
					
						
							|  |  |  |         $smarty->assign($params['assign'],$content); | 
					
						
							| 
									
										
										
										
											2002-01-31 20:49:40 +00:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2003-01-08 17:34:45 +00:00
										 |  |  |         return $content; | 
					
						
							| 
									
										
										
										
											2002-01-31 20:49:40 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* vim: set expandtab: */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ?>
 |