<?php
# ------------------------------------------------------------------------------
#
# Contains some holeper functions
#
# function http_request ($host, $port, $timeout,$page)
# function http_headers ($host, $port, $timeout,$page) {
# function exit_error($error)
# function recurse_dir($dir,$ext) {
# ------------------------------------------------------------------------------
#
# Copyright (C) 2003 Christian Eheim and Alex Pachikov
#
# This file is part of TVEz (tvez.sourceforge.net).
#
# TVEz is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# TVEz is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TVEz; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ------------------------------------------------------------------------------
#
# Created on 12/07/2003 by Christian Eheim (hide@address.com)
#
# LAST MODIFIED:
# $Date: 2004/01/19 23:05:26 $
# $Revision: 1.3 $
# $Author: eheim $
#
# ------------------------------------------------------------------------------
################################################################################
# Makes an HTTP request to a given URL and return the page source
################################################################################
function http_request ($host, $port, $timeout,$page) {
$host = preg_replace("/http:\/\//i","",$host);
$request = "GET $page HTTP/1.0\r\n";
$request .= "Host: $host\r\n";
$request .= "User-Agent: Mozilla/3.0\r\n";
$request .= "\r\n";
$socket = fsockopen( $host, $port, $errno, $errstr, $timeout );
if ($socket) {
fputs( $socket, $request );
$body = '';
$headersDone = false;
while ( !feof( $socket ) && $socket) {
$line = fgets( $socket, 4096 );
# If the headers are done, this line is part of the body
if ($headersDone)
$body .= $line;
# If we hit the first blank line, the headers are done
else if ( preg_match("/^\r\n$/", $line) )
$headersDone = true;
# otherwise, we are in the headers
else {
if ( preg_match("/^HTTP\/... ([0-9]*) .*$/", $line, $sts) )
$status = $sts[1];
else if ( preg_match("/^(.*): (.*)$/", $line, $matches) ) {
$headers[$matches[1]] = $matches[2];
# We are being redirected
if ( $matches[1] == "Location" ) {
$redirect = $matches[2];
# We have a full url in the redirect
if ( preg_match("/http/i", $redirect) ) {
$ahost = preg_replace("/http:\/\//i","",$redirect);
$host = preg_replace("/\/.*$/i","",$ahost);
$page = preg_replace("/$host/i","",$ahost);
}
else {
# The redirect is off the root
if ( preg_match("/^\//", $redirect) )
$page = $redirect;
# The redirect is relative to the current page
else
$page .= $redirect;
}
fclose( $socket );
return http_request($host, $port, $timeout, $page);
}
}
}
}
fclose( $socket );
# Follow redirects
return $body;
}
else { echo $errstr; return false; }
}
################################################################################
# Makes an HTTP request to a given URL and return the page source
################################################################################
function http_headers ($host, $port, $timeout,$page) {
$host = preg_replace("/http:\/\//i","",$host);
$request = "GET $page HTTP/1.0\r\n";
$request .= "Host: $host\r\n";
$request .= "User-Agent: Mozilla/3.0\r\n";
$request .= "\r\n";
$socket = fsockopen( $host, $port, $errno, $errstr, $timeout );
if ($socket) {
fputs( $socket, $request );
$body = '';
$headersDone = false;
while ( !feof( $socket ) && $socket) {
$line = fgets( $socket, 4096 );
# If the headers are done, this line is part of the body
if ($headersDone)
return $headers;
# If we hit the first blank line, the headers are done
else if ( preg_match("/^\r\n$/", $line) )
$headersDone = true;
# otherwise, we are in the headers
else {
if ( preg_match("/^HTTP\/... ([0-9]*) .*$/", $line, $sts) )
$status = $sts[1];
else if ( preg_match("/^(.*): (.*)$/", $line, $matches) ) {
$headers[$matches[1]] = $matches[2];
}
}
}
fclose( $socket );
# Follow redirects
return $headers;
}
else { echo $errstr; return false; }
}
################################################################################
# Prints an error message and a back button and exits
################################################################################
function exit_error($error) {
global $MYTHEME;
echo "
<p><p><p>
<blockquote><p>
<span class=\"warning\">
<b>$error</b>
</span>
<p><a href=\"javascript:history.back();\">".localize_string("Back")."</a>
</blockquote>
</body>
</html>
";
exit;
}
################################################################################
# Recursive directory search
#
# Fills the global hash movies with the movies
# and stores doubles in the global doubles
################################################################################
function recurse_dir($dir,$ext) {
global $movies, $doubles;
if ($dh = opendir($dir)) {
while ($file = readdir($dh)){
# Exclude all dot file, CVS and lost+found directories
# and exclude multi parts
if ( preg_match("/(^\.|^CVS$|^lost\+|\([2-9]of[2-9]\))/i",$file) )
continue;
# If the file is a movie, we add it to the list
if ( preg_match("/\.($ext)$/",$file) )
if (isset($movies[$file])) {
array_push($doubles,$movies[$file]);
array_push($doubles,"$dir/$file");
}
else
$movies[$file] = "$dir/$file";
# If this is a directory we search it
else if (is_dir("$dir/$file"))
recurse_dir("$dir/$file",$ext);
}
closedir($dh);
}
return false;
}
?>