// constants
include dirname(__FILE__)."/include.constants.php";
include dirname(__FILE__)."/include.database.php";
// include dirname(__FILE__)."/include.properties.php";
// //////////////////////////////////////////////////////////////////////////
// open database connection
$connection = mysql_connect( DB_SERVER, DB_USER, DB_PASSWORD );
if (!$connection) die("internal error, no database access");
if (!mysql_select_db(DB_DATABASE, $connection)) die("internal error, database not found");
// //////////////////////////////////////////////////////////////////////////
// determinate domain name
$lastDotPosition = strrpos ( getenv("HTTP_HOST"), "." );
$domain = substr ( getenv("HTTP_HOST"), 0, $lastDotPosition );
if ( substr_count ( $domain , "." ) > 0 )
{
$domain = substr ( $domain, strrpos ( $domain, "." ) + 1, $lastDotPosition );
}
// read properties from database
$sql = "select * from willol_project where domain = '" . $domain . "'";
$resultSet = mysql_query($sql, $connection);
if (!$resultSet)
{
// print mysql_error();
IS_LOCALHOST ? die ($sql) : die("internal error, no result set");
}
if ( mysql_num_rows($resultSet) == 0 )
{
mysql_free_result($resultSet);
mysql_close($connection);
die("internal error, project not found");
}
// multiple loop
// while ($resultSetArray = mysql_fetch_array($resultSet))
// {
// }
// just one time loop
$resultSetArray = mysql_fetch_array($resultSet);
define("PROJECT_ID", $resultSetArray["id"]);
define("PROJECT_NAME", $resultSetArray["display"]);
define("HOST_SOURCE", $resultSetArray["source_host"]);
define("IS_CACHE", $resultSetArray["is_cache"] == "0" ? false : true);
define("IS_ADVERTISEMENT", $resultSetArray["is_ads"] == "0" ? false : true);
define("IS_KEYWORD_SAVING", $resultSetArray["is_keyword_saving"] == "0" ? false : true);
define("IS_KEYWORD_TESTING", $resultSetArray["is_keyword_testing"] == "0" ? false : true);
define("ID_ANALYTICS", $resultSetArray["analytics_id"]);
define("DIRECTORY_CACHE", $resultSetArray["cache_directory"]);
define("SEPARATOR_PATH", $resultSetArray["separator_path"]);
define("SEPARATOR_CONTENT", $resultSetArray["separator_content"]);
define("TRANSLATION_DIRECTION_CODE", $resultSetArray["translation_direction_code"]);
define("MAX_TRANSLATION_LENGTH", $resultSetArray["translation_max_length"]);
define("WORD_AND", $resultSetArray["translation_ampersand"]);
define("COPYRIGHT_GLOBAL_PRE", $resultSetArray["copyright_global_pre"]);
define("COPYRIGHT_GLOBAL_POST", $resultSetArray["copyright_global_post"]);
define("COPYRIGHT_CONTENT_PRE", $resultSetArray["copyright_content_pre"]);
define("COPYRIGHT_CONTENT_POST", $resultSetArray["copyright_content_post"]);
define("HOMEPAGE", $resultSetArray["content_homepage"]);
define("KEYWORD_LAST_UPDATE", $resultSetArray["keyword_last_update"]);
mysql_free_result($resultSet);
// //////////////////////////////////////////////////////////////////////////
// automatic keyword enabling
// 'YYYY-MM-DD'
$today = date("Y-m-d");
// compare
if ( KEYWORD_LAST_UPDATE != $today )
{
if ( KEYWORD_LAST_UPDATE != null && strlen ( KEYWORD_LAST_UPDATE ) > 0 )
{
// read keyword id's
$sql = "select count(id) from willol_keyword where willol_project_id = " . PROJECT_ID . " and is_online = 1";
$resultSet = mysql_query($sql, $connection);
if (!$resultSet)
{
// print mysql_error();
// die( $sql );
IS_LOCALHOST ? die ($sql) : die("internal error, no result set");
}
$resultSetArray = mysql_fetch_array($resultSet);
mysql_free_result($resultSet);
$activeKeywordCounter = $resultSetArray[0];
$newKeywordCounter = 1;
if ( $activeKeywordCounter > 100 )
{
$newKeywordCounter = round ( $activeKeywordCounter / 100 * 1 , 0 );
}
// read keyword id's
$sql = "select id from willol_keyword where willol_project_id = " . PROJECT_ID . " and is_online = 0 and is_allowed = 1 order by rand(now()) limit " . $newKeywordCounter;
$resultSet = mysql_query($sql, $connection);
if (!$resultSet)
{
// print mysql_error();
// die( $sql );
IS_LOCALHOST ? die ($sql) : die("internal error, no result set");
}
while ( $resultSetArray = mysql_fetch_array($resultSet) )
{
/// echo $resultSetArray ["id"] . "
";
// update keywords
$sql = "update willol_keyword set is_online = 1 where id = " . $resultSetArray ["id"];
$innerResultSet = mysql_query($sql, $connection);
if (!$innerResultSet)
{
// print mysql_error();
IS_LOCALHOST ? die ($sql) : die("internal error, no result set");
}
$sql = "select willol_url_id from willol_keyword_has_url where willol_keyword_id = " . $resultSetArray ["id"];
$innerResultSet = mysql_query($sql, $connection);
if (!$innerResultSet)
{
// print mysql_error();
IS_LOCALHOST ? die ($sql) : die("internal error, no result set");
}
// multiple loop
while ($innerResultSetArray = mysql_fetch_array($innerResultSet))
{
$sql = "update willol_url set is_rebuilding = 1 where id = " . $innerResultSetArray["willol_url_id"];
$innerInnerResultSet = mysql_query($sql, $connection);
if (!$innerInnerResultSet)
{
// print mysql_error();
IS_LOCALHOST ? die ($sql) : die("internal error, no result set");
}
}
}
}
// update date
$sql = "update willol_project set keyword_last_update = '" . $today . "' where id = '" . PROJECT_ID . "'";
$resultSet = mysql_query($sql, $connection);
if (!$resultSet)
{
// print mysql_error();
IS_LOCALHOST ? die ($sql) : die("internal error, no result set");
}
}
// //////////////////////////////////////////////////////////////////////////
$url = "";
$title = "";
if ( isset ($_SERVER["REDIRECT_STATUS"]) && $_SERVER["REDIRECT_STATUS"] == "404" )
{
/// die ($_SERVER["REDIRECT_URL"]);
/// plausibility
$parameters = $_SERVER["REDIRECT_URL"];
if ( $parameters == null || strlen ( $parameters ) == 0 )
{
homepage( $connection );
exit;
}
if ( substr_count($parameters, "/") != 2 )
{
homepage( $connection );
exit;
}
if ( substr ($parameters, 0, 1) != "/" )
{
homepage( $connection );
exit;
}
$urlId = substr ( $parameters, 1, strpos ( $parameters, "/", 1 ) - 1);
if ( !isNumber( $urlId ) )
{
homepage( $connection );
exit;
}
$title = urldecode ( substr ( $parameters, 1 + strpos ( $parameters, "/", 1 ) ));
///////////////////////////////////////////////////////////////////////////
$sql = "select original, is_rebuilding from willol_url where willol_project_id = " . PROJECT_ID . " and id = '" . $urlId . "'";
$resultSet = mysql_query($sql, $connection);
if (!$resultSet)
{
// print mysql_error();
IS_LOCALHOST ? die ($sql) : IS_LOCALHOST ? die ($sql) : die("internal error, no result set");
}
if ( mysql_num_rows($resultSet) == 0 )
{
mysql_free_result($resultSet);
homepage( $connection );
exit;
}
$resultSetArray = mysql_fetch_array($resultSet);
mysql_free_result($resultSet);
$realURL = $resultSetArray["original"];
$is_rebuilding = $resultSetArray["is_rebuilding"] == "0" ? false : true;
// inkrement keys and disable rebuilding
$sql = "update willol_url set hits = hits + 1, is_rebuilding = 0 where id = '" . $urlId . "'";
$resultSet = mysql_query($sql, $connection);
if (!$resultSet)
{
// print mysql_error();
IS_LOCALHOST ? die ($sql) : die("internal error, no result set");
}
///////////////////////////////////////////////////////////////////////////
if ( $realURL == null || strlen ( $realURL ) == 0 )
{
homepage( $connection );
exit;
}
$url = HOST_SOURCE . $realURL;
header('HTTP/1.1 200 OK');
}
else
{
homepage( $connection );
exit;
}
/////////////////////////////////////////////////////////////////////////////
$filename = "." . SEPARATOR_PATH . DIRECTORY_CACHE . SEPARATOR_PATH . md5 ($url);
$html_body_content = "";
if ( $is_rebuilding || !IS_CACHE || !file_exists($filename) )
{
// proxfy
require_once 'PHProxy.class.php';
$PHProxy = new PHProxy(isset($_GET['flags']) ? $_GET['flags'] : null);
$PHProxy->start_transfer($url);
$html_body_content = $PHProxy->return_response();
// cut interesting content
// begin @
$html_body_content = substr($html_body_content, 22 + strpos($html_body_content, ''));
// end @