<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program 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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
if (!defined('ADMIN_ALLOW') && !defined('ADMIN_MAX')) {
	die ("Access Denied");
}
if ($acces == 1) {
if($admintest && $adminsecu)  {

    if($view == 4) { $ingroups = implode("-",$groups); }
    if($view < 4) { $ingroups = ""; }

		if ($moretime != "") {
			$moretime = $moretime * 86400;
			$expire = $moretime + $expire;
		}
		if ($url != "") {
			$bkey = "";
			$btime = time();
			if (!ereg("http://",$url)) {
				$url = "http://$url";
			}
			$rdf = parse_url($url);
			$fp = fsockopen($rdf['host'], 80, $errno, $errstr, 15);
			if (!$fp) {
				rssfail();
				exit;
			}
			if ($fp) {
				fputs($fp, "GET " . $rdf['path'] . "?" . $rdf['query'] . " HTTP/1.0\r\n");
				fputs($fp, "HOST: " . $rdf['host'] . "\r\n\r\n");
				$string	= "";
				while(!feof($fp)) {
					$pagetext = fgets($fp,300);
					$string .= chop($pagetext);
				}
				fputs($fp,"Connection: close\r\n\r\n");
				fclose($fp);
				$items = explode("</item>",$string);
				$content = "<font class=\"content\">";
				for ($i=0;$i<10;$i++) {
					$link = ereg_replace(".*<link>","",$items[$i]);
					$link = ereg_replace("</link>.*","",$link);
					$title2 = ereg_replace(".*<title>","",$items[$i]);
					$title2 = ereg_replace("</title>.*","",$title2);
					if ($items[$i] == "" AND $cont != 1) {
						$content = "";
					} else {
						if (strcmp($link,$title2) AND $items[$i] != "") {
							$cont = 1;
							$content .= "<strong><big>&middot;</big></strong>&nbsp;<a href=\"$link\" target=\"new\">$title2</a><br />\n";
						}
					}
				}
			}
			if ($oldposition != $bposition) {
				$result = $db->sql_query("select bid from ".TABLE_BLOCKS." where weight>='$weight' AND bposition='$bposition'");
				$fweight = $weight;
				$oweight = $weight;
				while ($row = $db->sql_fetchrow($result)) {
					$nbid = $db->XMFnum($row['bid']);
					$weight++;
					$db->sql_query("update ".TABLE_BLOCKS." set weight='$weight' where bid='$nbid'");
				}
				$result2 = $db->sql_query("select bid from ".TABLE_BLOCKS." where weight>'$oweight' AND bposition='$oldposition'");
				while ($row2 = $db->sql_fetchrow($result2)) {
					$obid = $db->XMFnum($row2['bid']);
					$db->sql_query("update ".TABLE_BLOCKS." set weight='$oweight' where bid='$obid'");
					$oweight++;
				}
				$row3 = $db->sql_fetchrow($db->sql_query("select weight from ".TABLE_BLOCKS." where bposition='$bposition' order by weight DESC limit 0,1"));
				$lastw = $db->XMFnum($row3['weight']);
				if ($lastw <= $fweight) {
					$lastw++;
					$db->sql_query("update ".TABLE_BLOCKS." set title='$title', content='$content', bposition='$bposition', weight='$lastw', active='$active', refresh='$refresh', blanguage='$blanguage', blockfile='$blockfile', view='$view', groups='$ingroups', subscription='$subscription' where bid='$bid'");
	             } else {
	        	$db->sql_query("update ".TABLE_BLOCKS." set title='$title', content='$content', bposition='$bposition', weight='$fweight', active='$active', refresh='$refresh', blanguage='$blanguage', blockfile='$blockfile', view='$view', groups='$ingroups', subscription='$subscription' where bid='$bid'");
	             }
	         } else {
	    $db->sql_query("update ".TABLE_BLOCKS." set bkey='$bkey', title='$title', content='$content', url='$url', bposition='$bposition', weight='$weight', active='$active', refresh='$refresh', blanguage='$blanguage', blockfile='$blockfile', view='$view', groups='$ingroups', subscription='$subscription' where bid='$bid'");

			}
redirect_url("".$admin_file.".php?op=$page&wb_name=$wb_name");
		} else {
			$title = stripslashes(FixQuotes($title));
			$content = stripslashes(FixQuotes($content));
			if ($oldposition != $bposition) {
				$result5 = $db->sql_query("select bid from ".TABLE_BLOCKS." where weight>='$weight' AND bposition='$bposition'");
				$fweight = $weight;
				$oweight = $weight;
				while ($row5 = $db->sql_fetchrow($result5)) {
					$nbid = $db->XMFnum($row5['bid']);
					$weight++;
					$db->sql_query("update ".TABLE_BLOCKS." set weight='$weight' where bid='$nbid'");
				}
				$result6 = $db->sql_query("select bid from ".TABLE_BLOCKS." where weight>'$oweight' AND bposition='$oldposition'");
				while ($row6 = $db->sql_fetchrow($result6)) {
					$obid = $db->XMFnum($row6['bid']);
					$db->sql_query("update ".TABLE_BLOCKS." set weight='$oweight' where bid='$obid'");
					$oweight++;
				}
				$row7 = $db->sql_fetchrow($db->sql_query("select weight from ".TABLE_BLOCKS." where bposition='$bposition' order by weight DESC limit 0,1"));
				$lastw = $db->XMFnum($row7['weight']);
				if ($lastw <= $fweight) {
					$lastw++;
					$db->sql_query("update ".TABLE_BLOCKS." set title='$title', content='$content', bposition='$bposition', weight='$lastw', active='$active', refresh='$refresh', blanguage='$blanguage', blockfile='$blockfile', view='$view', groups='$ingroups', subscription='$subscription' where bid='$bid'");
	    } else {
		$db->sql_query("update ".TABLE_BLOCKS." set title='$title', content='$content', bposition='$bposition', weight='$fweight', active='$active', refresh='$refresh', blanguage='$blanguage', blockfile='$blockfile', view='$view', groups='$ingroups', subscription='$subscription' where bid='$bid'");
	    }
	} else {
        if ($expire == "") {
            $expire = 0;
        }
        if ($expire != 0 AND $expire <= 999) {
           $expire = time() + ($expire * 86400);
        }
	    $result8 = $db->sql_query("update ".TABLE_BLOCKS." set bkey='$bkey', title='$title', content='$content', url='$url', bposition='$bposition', weight='$weight', active='$active', refresh='$refresh', blanguage='$blanguage', blockfile='$blockfile', view='$view', groups='$ingroups', expire='$expire', action='$action', subscription='$subscription' where bid='$bid'");

			}
redirect_url("".$admin_file.".php?op=$page&wb_name=$wb_name");
		}

}
}else {
	echo "Access Denied";
}