<?php
// $Id: banner_convert.php,v 1.3 2005/10/28 11:56:02 ohwada Exp $

//================================================================
// convert banner
// 2004-12-14 K.OHWADA
//================================================================

include '../../../mainfile.php';
include '../../../include/cp_header.php';

global $xoopsDB, $xoopsUser, $xoopsModule;
xoops_cp_header();

// check permission
$flag = false;
if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) 
{	$flag = true;	}

if (!$flag)
{
	echo "<font color=red>you are not admin</font><br>";
	xoops_cp_footer();
	exit();
}

// table name
$MODULE_DIRNAME = $xoopsModule->dirname();
$table_link     = $MODULE_DIRNAME."_link";
$table_link     = $xoopsDB->prefix($table_link);
$table_catlink  = $MODULE_DIRNAME."_catlink";
$table_catlink  = $xoopsDB->prefix($table_catlink);

// --- link table ---
echo "<h3>read link table</h3>";
$sql1 = "SELECT * FROM $table_link ORDER BY lid";
$result1 = sql_exec($sql1);

while ($row = $xoopsDB->fetchArray($result1))
{
	$lid    = $row['lid'];
	$banner = $row['banner'];
	$banner_old = $banner;

// banner 
	if (empty($banner)) continue;

	if ( !preg_match("/^https?:/", $banner) )
	{
		$banner = assume_banner_url( $banner );

		echo "$lid: convert $banner_old to $banner <br>\n";
		$sql2  = "UPDATE $table_link SET ";
		$sql2 .= "banner='$banner' ";
		$sql2 .= "WHERE lid=$lid";
		sql_exec($sql2);
	}

	$size = GetImageSize($banner);

	if ($size)
	{
		$width  = (int)$size[0];
		$height = (int)$size[1];

		echo "$lid: update banner size $width x $height <br>\n";

		$sql3  = "UPDATE $table_link SET ";
		$sql3 .= "width=$width, ";
		$sql3 .= "height=$height ";
		$sql3 .= "WHERE lid=$lid";
		sql_exec($sql3);
	}
	else
	{
		echo "<font color=red>$lid: connect error to $banner</font><br>";
	}

}

echo "<h3>end</h3>";
xoops_cp_footer();
exit();


function assume_banner_url( $banner )
{

// elliptical style
	$url = XOOPS_URL.$banner;
	$fp = fopen($url,"r");
	if ($fp)
  	{
		return $url;
  	}

// mylinks style
	$url = $this->module_url. "/images/shots/".$banner;
	return $url;

}

function sql_exec($sql)
{ 
	global $xoopsDB;

	$ret = $xoopsDB->queryF($sql);
	if ($ret != false ) { return $ret; }

	$error = $xoopsDB->error();
	echo "<font color=red>$sql<br>$error</font><br>";

	return false;
}

?>