<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
$success=true;

set_time_limit(0);

Db::i()->query("UPDATE ?_tree t, ?_tree t1 SET t.path=t1.path WHERE t.symlink=t1.tree_id");
Db::i()->query("ALTER TABLE  ?_users ADD  `last_ip_tmp` VARBINARY( 16 )  NULL DEFAULT NULL AFTER `last_ip`");
if ($users=Db::i()->selectCol("SELECT user_id as ARRAY_KEY, INET_NTOA(last_ip) FROM ?_users")) {
	foreach($users as $user_id=>$ip) {
		Db::i()->query("UPDATE ?_users SET last_ip_tmp=? WHERE user_id=?", inet_pton($ip), $user_id);
	}
}
Db::i()->query("ALTER TABLE ?_users DROP `last_ip`");
Db::i()->query("ALTER TABLE ?_users CHANGE `last_ip_tmp` `last_ip` VARBINARY( 16 ) NULL DEFAULT NULL ");


Db::i()->query("ALTER TABLE  ?_comments ADD  `ip_tmp` VARBINARY( 16 )  NULL DEFAULT NULL AFTER  `ip`");
if ($comments=Db::i()->selectCol("SELECT comment_id as ARRAY_KEY, INET_NTOA(ip) FROM ?_comments")) {
	foreach($comments as $comment_id=>$ip) {
		Db::i()->query("UPDATE ?_comments SET ip_tmp=? WHERE comment_id=?", inet_pton($ip), $comment_id);
	}
}
Db::i()->query("ALTER TABLE ?_comments DROP `ip`");
Db::i()->query("ALTER TABLE ?_comments CHANGE `ip_tmp` `ip` VARBINARY( 16 ) NULL DEFAULT NULL ");

if ($module_id=Db::i()->selectCell("SELECT module_id FROM ?_modules WHERE module='forum'")) {
	Db::i()->query("ALTER TABLE  ?_forum_posts ADD  `ip_tmp` VARBINARY( 16 )  NULL DEFAULT NULL AFTER  `ip`");
	if ($posts=Db::i()->selectCol("SELECT post_id as ARRAY_KEY, INET_NTOA(ip) FROM ?_forum_posts")) {
		foreach($posts as $post_id=>$ip) {
			Db::i()->query("UPDATE ?_forum_posts SET ip_tmp=? WHERE post_id=?", inet_pton($ip), $post_id);
		}
	}
	Db::i()->query("ALTER TABLE ?_forum_posts DROP `ip`");
	Db::i()->query("ALTER TABLE ?_forum_posts CHANGE `ip_tmp` `ip` VARBINARY( 16 ) NULL DEFAULT NULL ");
	
	Db::i()->query("ALTER TABLE  ?_forum_hposts ADD  `ip_tmp` VARBINARY( 16 )  NULL DEFAULT NULL AFTER  `ip`");
	if ($posts=Db::i()->selectCol("SELECT post_id as ARRAY_KEY, INET_NTOA(ip) FROM ?_forum_hposts")) {
		foreach($posts as $post_id=>$ip) {
			Db::i()->query("UPDATE ?_forum_hposts SET ip_tmp=? WHERE post_id=?", inet_pton($ip), $post_id);
		}
	}
	Db::i()->query("ALTER TABLE ?_forum_hposts DROP `ip`");
	Db::i()->query("ALTER TABLE ?_forum_hposts CHANGE `ip_tmp` `ip` VARBINARY( 16 ) NULL DEFAULT NULL ");
}

if ($module_id=Db::i()->selectCell("SELECT module_id FROM ?_modules WHERE module='guest'")) {
	Db::i()->query("ALTER TABLE  ?_guest ADD  `ip_tmp` VARBINARY( 16 )  NULL DEFAULT NULL AFTER  `ip`");
	if ($items=Db::i()->selectCol("SELECT msg_id as ARRAY_KEY, INET_NTOA(ip) FROM ?_guest")) {
		foreach($items as $item_id=>$ip) {
			Db::i()->query("UPDATE ?_guest SET ip_tmp=? WHERE msg_id=?", inet_pton($ip), $item_id);
		}
	}
	Db::i()->query("ALTER TABLE ?_guest DROP `ip`");
	Db::i()->query("ALTER TABLE ?_guest CHANGE `ip_tmp` `ip` VARBINARY( 16 ) NULL DEFAULT NULL ");
}
