<?php

define('IN_SUPESITE', TRUE);
define('S_ROOT', '');

error_reporting(E_ERROR | E_WARNING | E_PARSE);
set_magic_quotes_runtime(0);

$perpage = 50;
$theurl = './update1to2.php';

require_once './config.php';
require_once './update/inc/func.php';
require_once './function/common.func.php';
require_once './function/main.func.php';
require_once './function/cache.func.php';
require_once './function/oldtpl.func.php';

if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
	exit('數據庫連接錯誤，請將config.php參數手動設置正確後，然後再執行進行升級');
}
$version = mysql_get_server_info();
if($version > '4.1') {
	//選擇字符集
	if(empty($dbcharset) && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
		$dbcharset = str_replace('-', '', $charset);
	}
	if(!empty($dbcharset)) mysql_query("SET NAMES '$dbcharset'");
}
if($version > '5.0') {
	mysql_query("SET sql_mode=''");
}
//選擇數據表
if($dbname) {
	mysql_select_db($dbname);
}
$page = 1;
if(!empty($_GET['page'])) $page = intval($_GET['page']);
if($page < 1) $page = 1;
$step = $_GET['step'];
$grade = $_GET['grade'];
$start = ($page-1)*$perpage;
$end = $start + $perpage;

tplheader();
echo '<strong>SupeSite/X-Space升級程序(適用於X-Space 1.x 升級到X-Space 2.0)</strong>';
if (empty($step)) {
	print <<<END
	<br><br>
	<div style="border:dotted 1px #996600;width:80%;">
		<span style="color: #993300;font-weight: bold;font-size: 14px;"><br/>升級注意事項：<br/></span>
		<pre>
	1、介於升級過程中，對數據表結構的改變操作，請在升級前暫時關閉論壇
	2、盡量在訪問人數較少的時候執行升級(午夜)
	3、為了升確保升級過程中，避免網絡之類的因素造成升級失敗，請在升級之間先備份數據庫
	
	在執行本升級文件之前，請您保證已經進行了以下操作：

	在升級之前請務必先備份數據庫
	數據庫備份方法:
	Discuz! 5.x 用戶可以登錄論壇後台系統設置--系統工具--資料備份--自定義備份,將supesite的表全選上備份
	Discuz! 4.x 用戶請自行使用phpmyadmin備份數據,備份方法參照http://www.discuz.net/thread-292525-1-1.html

	<strong>第一步，將原來X-Space路徑下的除</strong>

	./attachments
	./cache
	./html
	./log
	
	以外的其它全部文件、文件夾保存到一個備份文件夾中，如 ./old
	並刪除以下目錄中的所有文件
	./cache/js
	./cache/tpl
	
	<strong>第二步，將程序文件包中 /upload 目錄中的除了</strong>
	
	./attachments
	./cache
	./html
	./install
	./log
	./install.php
	
	以外的其他全部文件二進制方式上傳到服務器X-Space路徑下，將服務器上以下目錄、以及目錄下面的所有文件的屬性為777，
	win主機請設置internet來賓帳戶可讀寫屬性
	./templates
	./data
	./data/robot
	./data/temp
	./data/system
	./templates/
	./templates/default
	./templates/default/*.*
	./user
	
	<strong>第三步,修改config.php,請對照論壇數據庫信息填寫config.php裡面的數據庫信息，其中下列的內容必須填寫：</strong>
	
	\$dbhost = 'localhost';		這個一般為localhost
	\$dbuser = '';			數據庫用戶名
	\$dbpw = '';			數據庫密碼
	\$dbname = '';			數據庫名
					以上4條空間商都會給出信息
	\$cookiedomain = '';			cookie域，與論壇設置相同，以保證能夠論壇和SupeSite/X-Space能同步登錄
					同時，以上信息在論壇的config.inc.php裡面可以查看到
	\$tablepre = '';			論壇的表名前綴，默認沒有修改為cdb_
	\$stablepre = '';			SupeSite/X-Space表名前綴，默認為supe_
	\$siteurl = '';			SupeSite/X-Space訪問URL地址
	\$bbsurl = '';			論壇URL地址
	\$dbcharset = '';			此處填寫數據庫中看到的論壇表的字符集，非常重要！
	\$bbsver = '';			此處填寫論壇的版本號,是D5就填寫5,D4就填寫4	
	</pre>
	</div>
	<p><span style="color: #990000"><strong>請選擇升級前的版本號：</strong></span></p>
	<p><a href="$theurl?grade=1&step=1">X-Space 1.0正式版(20060520)</a><br>&nbsp;&nbsp;&nbsp;&nbsp;如果您是從 http://x-space.discuz.net 首頁上下載的X-Space V1.0正式版，並且之後沒有進行過升級，那麼請點擊該鏈接進行升級</p>
	<p><a href="$theurl?grade=2&step=1">X-Space 1.x版本/SupeSite 5.0 測試版(20060520之後版本)</a><br>&nbsp;&nbsp;&nbsp;&nbsp;如果您安裝了X-Space V1.0正式版之後，進行過版本升級，那麼請點擊該鏈接進行升級</p>
END;
}


if ($step == 1) {

	$htmlroot = './user/';
	if(!is_dir($htmlroot)) mkdir($htmlroot);
	if (empty($_GET['total'])) {
		$total = mysql_result(mysql_query('SELECT COUNT(*) FROM '.$stablepre.'userspaces'), 0);
	} else {
		$total = intval($_GET['total']);
	}
	print <<<END
	<div style="border:dotted 1px #996600;width:80%;">
	<br>
	<strong>第一步(共十步)，重構模板</strong><br>
		共找到 $total 條記錄，正在將數據庫中的模版生成緩存文件。正在處理 $start 至 $end 條記錄……
	</div>
END;

	//檢查目錄是否可寫
	if ($total > $start) {
		$query = mysql_query('SELECT t.tplid, t.uid, t.css, t.mainhtml, t.listhtml FROM '.$stablepre.'userspaces u, '.$stablepre.'templatecontents t WHERE t.tplid=u.tplid LIMIT '.$start.', '.$perpage);
		while ($value = mysql_fetch_array($query, MYSQL_ASSOC)) {
			changetpl($value);
		}
	}
	if ($total > ($start + $perpage)) {
		redirect($theurl.'?grade='.$grade.'&step='.$step.'&total='.$total.'&page='.($page + 1));
	} else {
		redirect($theurl.'?grade='.$grade.'&step='.($step + 1));
	}

} elseif ($step == 2) {

	print <<<END
	<div style="border:dotted 1px #996600;width:80%;">
	<br>
	<strong>第二步(共十步)，升級數據庫表結構</strong><br>
		正在升級數據庫表結構，請稍後……
	</div>
END;

	$sqlfile = ($grade == 1) ? './update/sql/1to2.sql' : './update/sql/0610_1010.sql';
	dbupdate();
	$channel =  array
		(
		'index' => array
			(
			'action' => 'index',
			'name' => '首頁',
			'tpl' => 'index',
			'url' => "{$siteurl}",
			'default' => 1,
			'system' => 1,
			'show' => 1
			),
		'news' => array
			(
			'action' => 'news',
			'name' => '資訊',
			'tpl' => 'news_index',
			'url' => "{$siteurl}?action_news.html",
			'default' => 0,
			'system' => 1,
			'show' => 1
			),
		'blog' => array
			(
			'action' => 'blog',
			'name' => '日誌',
			'tpl' => 'blog_index',
			'url' => "{$siteurl}?action_blog.html",
			'default' => 0,
			'system' => 1,
			'show' => 1
			),
		'image' => array
			(
			'action' => 'image',
			'name' => '圖片',
			'tpl' => 'image_index',
			'url' => "{$siteurl}?action_image.html",
			'default' => 0,
			'system' => 1,
			'show' => 1
			),
		'goods' => array
			(
			'action' => 'goods',
			'name' => '商品',
			'tpl' => 'goods_index',
			'url' => "{$siteurl}?action_goods.html",
			'default' => 0,
			'system' => 1,
			'show' => 1
			),
		'file' => array
			(
			'action' => 'file',
			'name' => '文件',
			'tpl' => 'file_index',
			'url' => "{$siteurl}?action_file.html",
			'default' => 0,
			'system' => 1,
			'show' => 1
			),
		'link' => array
			(
			'action' => 'link',
			'name' => '書籤',
			'tpl' => 'link_index',
			'url' => "{$siteurl}?action_link.html",
			'default' => 0,
			'system' => 1,
			'show' => 1
			),
		'bbs' => array
			(
			'action' => 'bbs',
			'name' => '論壇',
			'tpl' => 'bbs_index',
			'url' => "{$siteurl}?action_bbs.html",
			'default' => 0,
			'system' => 1,
			'show' => 1
			)
		);
	mysql_query('REPLACE INTO '.$stablepre.'settings VALUES (\'channel\', \''.serialize($channel).'\')');
	if (mysql_error()) {
		echo '<br>數據庫SQL文執行錯誤: REPLACE INTO '.$stablepre.'settings VALUES (\'channel\', \''.serialize($channel).'\') <br>';
		echo '數據庫升級出現錯誤: '.mysql_error().'<br>';
		exit;
	}
	dbconnect();
	updatesettingcache();	//防止config.cache.php被刪除無法取到附件地址

	redirect($theurl.'?grade='.$grade.'&step='.($step + 1));

} elseif($step == 3) {
	
	if(file_exists('./data/system/config.cache.php')) {
		require_once './data/system/config.cache.php';
	} elseif(file_exists('./cache/system/config.cache.php')) {
		require_once './cache/system/config.cache.php';
	} else {
		exit('請檢查cache/system目錄下面是否存在config.cache.php文件，如果沒有，請進入站點後台更新緩存來生成');
	}
	if (empty($_GET['total'])) {
		$total = mysql_result(mysql_query('SELECT COUNT(*) FROM '.$stablepre.'userspaces WHERE hash!=\'\''), 0);
	} else {
		$total = intval($_GET['total']);
	}
	
	print <<<END
	<div style="border:dotted 1px #996600;width:80%;">
	<br>
	<strong>第三步(共十步)，處理用戶頭像</strong><br>
		共找到 $total 個用戶，正在對 $start 至 $end 用戶頭像進行操作，請稍後……
	</div>
END;
	if ($total > $start) {
		$query = mysql_query('SELECT u.uid, u.hash, a.filepath, a.thumbpath FROM '.$stablepre.'userspaces u LEFT JOIN '.$stablepre.'attachments a ON a.hash=u.hash WHERE u.hash!=\'\' LIMIT '.$start.', '.$perpage);
		while ($item = mysql_fetch_array($query, MYSQL_ASSOC)) {
			if(empty($item['thumbpath']) && !empty($item['filepath'])) {
				$item['thumbpath'] = $item['filepath'];
			} elseif(empty($item['thumbpath']) && empty($item['filepath'])) { continue; }
			$oldphotopath = $_SSCONFIG['attachmentdir'].'/'.$item['thumbpath'];
			if(!file_exists($oldphotopath)) {
				continue;
			}
		
			$photoext = fileext($oldphotopath);	//獲取後綴名
			$photopath = getuserfile($item['uid'], $photoext);
			//移動頭像
			if(@copy($oldphotopath, $photopath)) {
			} elseif((function_exists('move_uploaded_file') && @move_uploaded_file($oldphotopath, $photopath))) {
			} elseif(@rename($oldphotopath, $photopath)) {
			}
			@unlink($oldphotopath);		//刪除附件
			$oldbigpoto = $_SSCONFIG['attachmentdir'].'/'.$item['filepath'];
			@unlink($oldbigpoto);
			mysql_query('UPDATE '.$stablepre.'userspaces SET photo=\''.$photoext.'\' WHERE uid=\''.$item['uid'].'\'');
		}
	}
	if ($total > ($start + $perpage)) {
		redirect($theurl.'?grade='.$grade.'&step='.$step.'&total='.$total.'&page='.($page + 1));
	} else {
		redirect($theurl.'?grade='.$grade.'&step='.($step + 1));
	}
	
} elseif ($step == 4) {
	if (empty($_GET['total'])) {
		$total = mysql_result(mysql_query('SELECT COUNT(*) FROM '.$stablepre.'userspaces'), 0);
	} else {
		$total = intval($_GET['total']);
	}
	print <<<END
	<div style="border:dotted 1px #996600;width:80%;">
	<br>
	<strong>第四步(共十步)，正在拆分userspaces表結構</strong><br>
		正在拆分處理userspaces表結構，請稍後……
		共找到 $total 條記錄，正在對 $start 至 $end 條記錄進行操作！
	</div>
END;
	
	if ($total > $start) {
		$query = mysql_query('SELECT uid, perpage, announcement, showblocks, blocknames, summarylen, attachsize, jammer, blogmod, blognum, flash, music FROM '.$stablepre.'userspaces LIMIT '.$start.', '.$perpage);
		while ($value = mysql_fetch_array($query, MYSQL_ASSOC)) {
			$value = saddslashes($value);
			$sql = '';
			$insert = implode('\', \'', $value);
			$sql .= '(\''.$insert.'\')';
			mysql_query('REPLACE INTO '.$stablepre.'userspacefields (uid, perpage, announcement, showblocks, blocknames, summarylen, attachsize, jammer, blogmod, blognum, flash, music) VALUES '.$sql);
			if (mysql_errno()) {
				mysql_query('REPLACE INTO `supe_userspacefields` ( `uid` , `perpage` , `announcement` , `showblocks` , `blocknames` , `summarylen` , `attachsize` , `jammer` , `blogmod` , `blognum` , `imagenum` , `goodsnum` , `filenum` , `linknum` , `threadnum` , `othernum` , `flash` , `music` , `choiceblockleft` , `choiceblockright` , `choiceblockmain` , `myblock1` , `myblock2` , `myblock3` , `myblock4` , `myblock5` , `layout` , `showside` , `diy` ) VALUES (\''.$item['uid'].'\', \'0\', \'\', \'\', \'\', \'0\', \'0\', \'0\', \'0\', \'0\', \'0\', \'0\', \'0\', \'0\', \'0\', \'0\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'1\', \'0\', \'\')');
				echo '<br>數據庫SQL文執行錯誤: REPLACE INTO '.$stablepre.'userspacefields (uid, perpage, announcement, showblocks, blocknames, summarylen, attachsize, jammer, blogmod, blognum, flash, music) VALUES '.$sql.' <br>';
				echo '數據庫升級出現錯誤: '.mysql_error().'<br>';
				exit;
			}
		}
	}
	if ($total > ($start + $perpage)) {
		redirect($theurl.'?grade='.$grade.'&step='.$step.'&total='.$total.'&page='.($page + 1));
	} else {
		mysql_query('ALTER TABLE '.$stablepre.'userspaces DROP tplid, DROP perpage, DROP announcement, DROP showblocks, DROP blocknames, DROP summarylen, DROP attachsize, DROP jammer, DROP blogmod, DROP blognum, DROP flash, DROP music, DROP hash');
		if (mysql_error()) {
			echo '<br>數據庫SQL文執行錯誤: ALTER TABLE '.$stablepre.'userspaces DROP tplid, DROP perpage, DROP announcement, DROP showblocks, DROP blocknames, DROP summarylen, DROP attachsize, DROP jammer, DROP blogmod, DROP blognum, DROP flash, DROP music, DROP hash <br>';
			echo '數據庫升級出現錯誤: '.mysql_error().'<br>';
			exit;
		}
		redirect($theurl.'?grade='.$grade.'&step='.($step + 1));
	}
} elseif ($step == 5) {

	if (empty($_GET['total'])) {
		$total = mysql_result(mysql_query('SELECT COUNT(*) FROM '.$stablepre.'spaceimages'), 0);
	} else {
		$total = intval($_GET['total']);
	}
	print <<<END
	<div style="border:dotted 1px #996600;width:80%;">
	<br>
	<strong>第五步(共十步)，正在處理相冊附件</strong><br>
		共找到 $total 個相冊附件,正在處理 $start 至 $end 個附件中,請稍後……
	</div>
END;
	if ($total > $start) {
		$itemidarr = array();
		$image = array();
		$query = mysql_query('SELECT itemid, COUNT(aid) AS num FROM '.$stablepre.'attachments WHERE type=\'image\' GROUP BY itemid LIMIT '.$start.', '.$perpage);
		while ($value = mysql_fetch_array($query, MYSQL_ASSOC)) {
			$itemidarr[] = $value['itemid'];
			$image[$value['itemid']]['imgnum'] = $value['num'];
			$image[$value['itemid']]['hasimg'] = 0;
		}
		$query = mysql_query('SELECT itemid, attachtype, thumbpath FROM '.$stablepre.'attachments WHERE itemid IN ('.implode(', ', $itemidarr).')');
		while ($value = mysql_fetch_array($query, MYSQL_ASSOC)) {
			if (!$image[$value['itemid']]['hasimg']) {
				$image[$value['itemid']]['img'] = $value['thumbpath'];
				$image[$value['itemid']]['hasimg'] = 1;
			}
			if (($image[$value['itemid']]['hasimg'] != 2) && $value['attachtype'] == 'jpg') {
				$image[$value['itemid']]['img'] = $value['thumbpath'];
				$image[$value['itemid']]['hasimg'] = 2;
			}
		}
		foreach ($image as $id => $val) {
			mysql_query('UPDATE '.$stablepre.'spaceimages SET image=\''.$val['img'].'\', imagenum=\''.$val['imgnum'].'\' WHERE itemid=\''.$id.'\'');
		}
	}
	if ($total > ($start + $perpage)) {
		redirect($theurl.'?grade='.$grade.'&step='.$step.'&total='.$total.'&page='.($page + 1));
	} else {
		redirect($theurl.'?grade='.$grade.'&step='.($step + 1));
	}
} elseif ($step == 6) {

	if (empty($_GET['total'])) {
		$total = mysql_result(mysql_query('SELECT COUNT(*) FROM '.$stablepre.'spacegoods'), 0);
	} else {
		$total = intval($_GET['total']);
	}
	print <<<END
	<div style="border:dotted 1px #996600;width:80%;">
	<br>
	<strong>第六步(共十步)，正在處理商品附件</strong><br>
		共找到 $total 件商品,正在處理 $start 至 $end 件商品中,請稍後……
	</div>
END;
	if ($total > $start) {
		$query = mysql_query('SELECT g.itemid, a.filepath, a.thumbpath FROM '.$stablepre.'spacegoods g LEFT JOIN '.$stablepre.'attachments a ON a.itemid=g.itemid LIMIT '.$start.', '.$perpage);
		while ($value = mysql_fetch_array($query, MYSQL_ASSOC)) {
			if ($value['filepath']) {
				mysql_query('UPDATE '.$stablepre.'spacegoods SET image=\''.$value['filepath'].'\', thumb=\''.$value['thumbpath'].'\' WHERE itemid=\''.$value['itemid'].'\'');
			}
		}
	}
	if ($total > ($start + $perpage)) {
		redirect($theurl.'?grade='.$grade.'&step='.$step.'&total='.$total.'&page='.($page + 1));
	} else {
		redirect($theurl.'?grade='.$grade.'&step='.($step + 1));
	}
} elseif ($step == 7) {
	if (empty($_GET['total'])) {
		$total = mysql_result(mysql_query('SELECT COUNT(*) FROM '.$stablepre.'spacelinks'), 0);
	} else {
		$total = intval($_GET['total']);
	}
	print <<<END
	<div style="border:dotted 1px #996600;width:80%;">
	<br>
	<strong>第七步(共十步)，正在處理書籤</strong><br>
		共找到 $total 條書籤,正在處理 $start 至 $end 條書籤中,請稍後……
	</div>
END;
	if ($total > $start) {
		$query = mysql_query('SELECT itemid, url FROM '.$stablepre.'spacelinks LIMIT '.$start.', '.$perpage);
		while ($value = mysql_fetch_array($query, MYSQL_ASSOC)) {
			mysql_query('UPDATE '.$stablepre.'spacelinks SET domain=\''.getdomain($value['url']).'\' WHERE itemid=\''.$value['itemid'].'\'');
		}
	}
	if ($total > ($start + $perpage)) {
		redirect($theurl.'?grade='.$grade.'&step='.$step.'&total='.$total.'&page='.($page + 1));
	} else {
		redirect($theurl.'?grade='.$grade.'&step='.($step + 1));
	}
} elseif ($step == 8) {
	if (empty($_GET['total'])) {
		$total = mysql_result(mysql_query('SELECT COUNT(*) FROM '.$stablepre.'categories'), 0);
	} else {
		$total = intval($_GET['total']);
	}
	print <<<END
	<div style="border:dotted 1px #996600;width:80%;">
	<br>
	<strong>第八步(共十步)，正在處理分類圖標</strong><br>
		共找到 $total 條分類圖標,正在處理 $start 至 $end 條分類圖標中,請稍後……
	</div>
END;
	if ($total > $start) {
		$query = mysql_query('SELECT c.catid, a.filepath, a.thumbpath FROM '.$stablepre.'categories c LEFT JOIN '.$stablepre.'attachments a ON a.hash=c.hash LIMIT '.$start.', '.$perpage);
		while ($value = mysql_fetch_array($query, MYSQL_ASSOC)) {
			if ($value['filepath']) {
				mysql_query('UPDATE '.$stablepre.'categories SET thumb=\''.$value['thumbpath'].'\', image=\''.$value['filepath'].'\' WHERE catid=\''.$value['catid'].'\'');
			}
		}
	}
	if ($total > ($start + $perpage)) {
		redirect($theurl.'?grade='.$grade.'&step='.$step.'&total='.$total.'&page='.($page + 1));
	} else {
		mysql_query('ALTER TABLE '.$stablepre.'categories DROP hash');
		if (mysql_error()) {
			echo mysql_error();
		}
		redirect($theurl.'?grade='.$grade.'&step='.($step + 1));
	}
} elseif ($step == 9) {
	print <<<END
	<div style="border:dotted 1px #996600;width:80%;">
	<br>
	<strong>第九步(共十步)，正在更新緩存</strong><br>
		正在更新緩存中,請稍後……
	</div>
END;
	dbconnect();
	updateadcache();
	updatecategorycache();
	updatesettingcache();
	updatecronscache();
	updateeffectcache();
	updategroupcache();
	updatebbssetting();
	updatebbsbbcode();
	updatebbscensor();
	updatebbsstyle();
	redirect($theurl.'?grade='.$grade.'&step='.($step + 1));
} elseif ($step == 10) {
	mysql_query('UPDATE `supe_attachments` SET hash = substring(hash, 8, 16)');
	mysql_query('ALTER TABLE `supe_attachments` CHANGE `hash` `hash` char(16) NOT NULL default \'\'');
	mysql_query('UPDATE `supe_cache` SET cachekey = substring(cachekey, 8, 16)');
	mysql_query('ALTER TABLE `supe_cache` CHANGE `cachekey` `cachekey` varchar(16) NOT NULL default \'\',CHANGE `value` `value` mediumtext NOT NULL');
	mysql_query('UPDATE `supe_spacecache` SET cachekey = substring(cachekey, 8, 16)');
	mysql_query('ALTER TABLE `supe_spacecache` CHANGE `cachekey` `cachekey` varchar(16) NOT NULL default \'\' , CHANGE `value` `value` mediumtext NOT NULL');
	mysql_query('UPDATE `supe_spaceitems` SET hash = substring(hash, 8, 16)');
	mysql_query('ALTER TABLE `supe_spaceitems` CHANGE `hash` `hash` char(16) NOT NULL default \'\'');
	mysql_query('UPDATE `supe_tagcache` SET cachekey = substring(cachekey, 8, 16)');
	mysql_query('ALTER TABLE `supe_tagcache` CHANGE `cachekey` `cachekey` varchar(16) NOT NULL default \'\' , CHANGE `value` `value` mediumtext NOT NULL default \'\'');

print <<<END
	<div style="border:dotted 1px #996600;width:80%;">
	<br>
	<strong>第十步(共十步)，正在更新緩存</strong><br>
		恭喜您！升級成功\！請刪除根目錄下的update1to2.php以及./update文件夾，並記得重新開放論壇。
	</div>
END;
}

tplfooter();

function redirect($url) {
	echo "<script>",
	"function redirect() {window.location.replace('$url');}\n",
	"setTimeout('redirect();', 1000);\n",
	"</script>",
	"<br><br>下一頁面處理中...<br>瀏覽器會自動跳轉頁面，無需人工干預。<br><a href=\"$url\">除非當您的瀏覽器沒有自動跳轉時，請點擊這裡</a>";
}

function getdomain($url) {

	$domain = '';
	$resuts = explode('://', $url);
	if(!empty($resuts[1])) {
		$domains = explode('/', $resuts[1]);
		$domain = $domains[0];
	}
	return $domain;

}

function saddslashes($string) {
	if(is_array($string)) {
		foreach($string as $key => $val) {
			$string[$key] = saddslashes($val);
		}
	} else {
		$string = addslashes($string);
	}
	return $string;
}


?>