Файл: adultscript-2.0.3-pro/files/install/migrate-vshare.php
Строк: 351
<?php
define('_VALID', true);
define('_INSTALL', true);
define('_CONSOLE', true);
require realpath('../libraries/bootstrap.php');
define('INSTALL_URL', BASE_URL.'/install');
error_reporting(E_ALL);
ini_set('display_errors', 1);
restore_error_handler();
restore_exception_handler();
while(ob_get_level()) {
ob_end_flush();
}
// we dont need a session opened here
session_write_close();
$dt_method = 'copy';
$tag_length = 3;
$errors = array();
$test = VF::factory('test');
$required = $test->get_required_results();
$optional = $test->get_optional_results();
$recommended = $test->get_recommended_results();
$config = array(
'm_path' => '',
'm_db_host' => '',
'm_db_username' => '',
'm_db_password' => '',
'm_db_name' => '',
'base_url' => BASE_URL,
'relative_url' => RELATIVE_URL,
);
$secret = VText::random();
function show($value)
{
if ($value == '2') {
return 'no';
}
return ($value == '0') ? 'all' : 'friends';
}
function update_progress($data)
{
echo $data,"n";
file_put_contents(TMP_DIR.'/logs/migration_progress.log', $data."n");
}
function show_prefs($value)
{
$show = 'all';
if ($value == '1') {
$show = 'friends';
}
if ($value == '0') {
$show = 'no';
}
return $show;
}
$_POST['submitted'] = true;
if (isset($_POST['submitted'])) {
$filter = VF::factory('filter');
$m_path = '/home/rockwind/public_html';
$m_db_host = 'localhost';
$m_db_username = 'rockwind_vshare';
$m_db_password = 'Gl_JN0UGQIDb';
$m_db_name = 'rockwind_vshare';
if ($m_path == '') {
$errors[] = 'You need to enter the Clipshare Installation path to continue!';
} else {
$dirs = array(
$m_path,
$m_path.'/flvideo',
$m_path.'/thumb',
$m_path.'/photo',
$m_path.'/chimg'
);
foreach ($dirs as $dir) {
if (!file_exists($dir) OR !is_dir($dir) OR !is_readable($dir)) {
$errors[] = 'Invalid AVS installation (folder: '.$dir.' not found)!';
}
}
}
if ($m_db_host == '' OR $m_db_username == '' OR
$m_db_password == '' OR $m_db_name == '') {
$errors[] = 'You need to enter the AVS database host, username, password and name to continue!';
} else {
require 'db.php';
$mdb = new VInstall_db($m_db_host, $m_db_name, $m_db_username, $m_db_password, '');
if ($mdb->is_error()) {
$errors[] = $mdb->get_error();
}
}
if ($errors) {
foreach ($errors as $error) {
echo $error,"n";
}
}
if (!$errors) {
update_progress('1^Migrating users....');
$db = VF::factory('database');
$db->query("SHOW COLUMNS FROM #__user LIKE 'old_user_id'");
if (!$db->affected_rows()) {
$db->query("ALTER TABLE #__user ADD `old_user_id` int(11) NOT NULL default '0'");
}
$mdb->query("SELECT u.*
FROM #__users AS u
ORDER BY u.user_id ASC");
$users = $mdb->fetch_rows();
foreach ($users as $user) {
if ($user['user_name'] == 'anonymous') {
$db->query("UPDATE #__user
SET old_user_id = ".(int) $user['user_id']."
WHERE username = 'anonymous'
LIMIT 1");
continue;
}
if ($user['user_name'] == 'admin') {
$db->query("UPDATE #__user
SET old_user_id = ".(int) $user['user_id']."
WHERE username = 'admin'
LIMIT 1");
continue;
}
$gender = ($user['user_gender'] != '') ? strtolower($user['user_gender']) : 'hidden';
$relation = ($user['user_relation'] != '') ? strtolower($user['user_relation']) : 'hidden';
$verified = ($user['user_email_verified'] == 'yes') ? 1 : 0;
$status = ($user['user_account_status'] == 'Active') ? 1 : 0;
$ip = (ip2long($user['user_ip'])) ? ip2long($user['user_ip']) : 0;
$db->query("INSERT INTO #__user
SET group_id = 5,
old_user_id = ".(int) $user['user_id'].",
username = '".$db->escape(str_replace(' ', '-', $user['user_name']))."',
password = '".$db->escape($user['user_password'])."',
email = '".$db->escape($user['user_email'])."',
name = '".$db->escape($user['user_first_name']).' '.$db->escape($user['user_last_name'])."',
gender = '".$db->escape($gender)."',
relation = '".$db->escape($relation)."',
birth_date = '".$db->escape($user['user_birth_date'])."',
city = '".$db->escape($user['user_city'])."',
country = '".$db->escape($user['user_country'])."',
join_date = '".$db->escape(date('Y-m-d h:i:s', (int) $user['user_join_time']))."',
login_date = '".$db->escape(date('Y-m-d h:i:s', (int) $user['user_last_login_time']))."',
login_ip = ".$ip.",
verified = '".$verified."',
status = '".$status."'");
if ($db->affected_rows()) {
$user_id = $db->get_last_insert_id('#__user');
$db->query("INSERT INTO #__user_activity
SET user_id = ".$user_id.",
total_video_views = ".(int) $user['user_video_viewed'].",
total_viewed_videos = ".(int) $user['user_watched_video'].",
total_profile_views = ".(int) $user['user_profile_viewed']);
$db->query("INSERT INTO #__user_profile
SET user_id = ".$user_id.",
about = '".$db->escape($user['user_about_me'])."',
website = '".$db->escape($user['user_website'])."',
hobbies = '".$db->escape($user['user_interest_hobby'])."',
school = '".$db->escape($user['user_school'])."',
occupation = '".$db->escape($user['user_occupation'])."',
movies = '".$db->escape($user['user_fav_movie_show'])."',
music = '".$db->escape($user['user_fav_music'])."',
books = '".$db->escape($user['user_fav_book'])."'");
$db->query("INSERT INTO #__user_notifications SET user_id = ".$user_id);
// not everything migrated here
$db->query("INSERT INTO #__user_preferences SET user_id = ".$user_id);
if ($user['user_photo'] == '1') {
$avatar = $m_path.'/photo/'.$user['user_id'].'.jpg';
if (file_exists($avatar) && is_file($avatar)) {
$ext = VFile::ext($avatar);
$dt_method($avatar, MEDIA_DIR.'/users/'.$user_id.'.'.$ext);
$db->query("UPDATE #__user SET avatar = '".$db->escape($ext)."' WHERE user_id = ".$user_id." LIMIT 1");
}
}
} else {
die('Failed to add user!');
}
}
// progress 15%, lets initiate our database connections again :-)
update_progress('15^Migrating friends...');
$mdb->close();
$mdb = new VInstall_db($m_db_host, $m_db_name, $m_db_username, $m_db_password, '');
$db = VF::factory_remove('database');
$db = VF::factory('database');
$mdb->query("SELECT * FROM friends");
$friends = $mdb->fetch_rows();
foreach ($friends as $friend) {
$user_id = FALSE;
$friend_id = FALSE;
$db->query("SELECT user_id
FROM #__user
WHERE old_user_id = ".(int) $friend['friend_user_id']."
LIMIT 1");
if ($db->affected_rows()) {
$user_id = (int) $db->fetch_field('user_id');
}
$db->query("SELECT user_id
FROM #__user
WHERE old_user_id = ".(int) $friend['friend_friend_id']."
LIMIT 1");
if ($db->affected_rows()) {
$friend_id = (int) $db->fetch_field('user_id');
}
$status = 'approved';
if ($friend['friend_status'] != 'Confirmed') {
$status = strtolower($friend['friend_status']);
}
if ($user_id && $friend_id) {
$add_date = $friend['friend_invite_date'].' 00:00:00';
$db->query("INSERT INTO #__user_friends
SET user_id = ".$user_id.",
friend_id = ".$friend_id.",
add_date = '".$add_date."',
status = '".$status."'");
if ($status == 'approved') {
$db->query("UPDATE #__user_activity
SET total_friends = total_friends
WHERE user_id = ".$friend_id." LIMIT 1");
}
}
}
// progress 15%, lets initiate our database connections again :-)
update_progress('20^Migrating user comments...');
$mdb->close();
$mdb = new VInstall_db($m_db_host, $m_db_name, $m_db_username, $m_db_password, '');
$db = VF::factory_remove('database');
$db = VF::factory('database');
$mdb->query("SELECT * FROM profile_comments");
$comments = $mdb->fetch_rows();
foreach ($comments as $comment) {
$user_id = FALSE;
$poster_id = FALSE;
$db->query("SELECT user_id
FROM #__user
WHERE old_user_id = ".(int) $comment['profile_comment_user_id']."
LIMIT 1");
if ($db->affected_rows()) {
$user_id = (int) $db->fetch_field('user_id');
}
$db->query("SELECT user_id
FROM #__user
WHERE old_user_id = ".(int) $comment['profile_comment_posted_by']."
LIMIT 1");
if ($db->affected_rows()) {
$poster_id = (int) $db->fetch_field('user_id');
}
if ($user_id && $poster_id) {
$db->query("INSERT INTO #__user_comments
SET user_id = ".$user_id.",
poster_id = ".$poster_id.",
comment = '".$db->escape($comment['profile_comment_text'])."',
add_date = '".$comment['profile_comment_date']."'");
}
}
// progress 19%, lets initiate our database connections again :-)
update_progress('20^Migrating video categories...');
$mdb->close();
$mdb = new VInstall_db($m_db_host, $m_db_name, $m_db_username, $m_db_password, '');
$db = VF::factory_remove('database');
$db = VF::factory('database');
$db->query("SHOW COLUMNS FROM #__video_categories LIKE 'old_cat_id'");
if (!$db->affected_rows()) {
$db->query("ALTER TABLE #__video_categories ADD `old_cat_id` int(11) NOT NULL default '0'");
}
$mdb->query("SELECT * FROM channels ORDER BY channel_name ASC");
$categories = $mdb->fetch_rows();
foreach ($categories as $category) {
$db->query("INSERT INTO #__video_categories
SET old_cat_id = ".(int) $category['channel_id'].",
name = '".$db->escape($category['channel_name'])."',
slug = '".$db->escape($category['channel_seo_name'])."',
description = '".$db->escape($category['channel_description'])."'");
if ($db->affected_rows()) {
$cat_id = $db->get_last_insert_id('#__video_categories');
$c_src = $m_path.'/chimg/'.$category['channel_id'].'.jpg';
$c_dst = MEDIA_DIR.'/videos/cat/'.$cat_id.'.jpg';
$dt_method($c_src, $c_dst);
} else {
die('Failed to add video category!');
}
}
// this is to write the required data for the video migration
// for each server
$files = array();
// progress 20%, lets initiate our database connections again :-)
update_progress('32^Migrating videos...');
$mdb->close();
$mdb = new VInstall_db($m_db_host, $m_db_name, $m_db_username, $m_db_password, '');
$db = VF::factory_remove('database');
$db = VF::factory('database');
$db->query("SHOW COLUMNS FROM #__video LIKE 'old_video_id'");
if (!$db->affected_rows()) {
$db->query("ALTER TABLE #__video ADD `old_video_id` int(11) NOT NULL default '0'");
}
$mdb->query("SELECT * FROM videos
ORDER BY video_id ASC");
$videos = $mdb->fetch_rows();
$percent = 68;
$count = count($videos);
$incr = round($percent/round($count/50, 0), 2);
$progress = 32;
$index = 0;
foreach ($videos as $video) {
$db->query("SELECT user_id
FROM #__user
WHERE old_user_id = ".(int) $video['video_user_id']."
LIMIT 1");
$user_id = (int) $db->fetch_field('user_id');
$allow_comment = ($video['video_allow_comment'] == 'yes') ? 1 : 0;
$allow_rating = ($video['video_allow_rated'] == 'yes') ? 1 : 0;
$allow_embed = ($video['video_allow_embed'] == 'enabled') ? 1 : 0;
$db->query("INSERT INTO #__video
SET old_video_id = ".(int) $video['video_id'].",
user_id = ".$user_id.",
title = '".$db->escape($video['video_title'])."',
slug = '".$db->escape(prepare_string($video['video_title'], true))."',
description = '".$db->escape($video['video_description'])."',
rating = ".$video['video_rate'].",
rated_by = ".$video['video_rated_by'].",
embed_code = '".$db->escape($video['video_embed_code'])."',
allow_comment = '".$allow_comment."',
allow_rating = '".$allow_rating."',
allow_embed = '".$allow_embed."',
duration = ".$video['video_duration'].",
thumb = 1,
thumbs = 3,
total_views = ".$video['video_view_number'].",
total_downloads = 0,
add_date = '".$db->escape($video['video_add_date'])."',
add_time = ".$video['video_add_time'].",
view_date = '".date('Y-m-d H:i:s', $video['video_view_time'])."',
view_time = ".$video['video_view_time'].",
status = 0");
if ($db->affected_rows()) {
$video_id = $db->get_last_insert_id('#__video');
$mdb->query("SELECT t.tag
FROM tag_video AS tv
LEFT JOIN tags AS t ON (t.id = tv.tag_id)
WHERE tv.vid = ".$video_id);
$rows = $mdb->fetch_rows();
$tags = array();
foreach ($rows as $row) {
$tags[] = prepare_tags($row['tag']);
}
foreach ($tags as $tag) {
if (strlen($tag) >= 3) {
$db->query("INSERT INTO #__video_tags SET video_id = ".$video_id.", name = '".$db->escape($tag)."'");
}
}
$category = $video['video_channels'];
if (!is_numeric($category)) {
$channels = explode('|', $category);
}
$categories = array();
foreach ($channels as $old_cat_id) {
$db->query("SELECT cat_id FROM #__video_categories WHERE old_cat_id = ".(int) $old_cat_id." LIMIT 1");
if ($db->affected_rows()) {
$categories[] = (int) $db->fetch_field('cat_id');
}
}
foreach ($categories as $cat_id) {
$db->query("INSERT INTO #__video_category
SET video_id = ".$video_id.",
cat_id = ".$cat_id);
}
$thumbs = 0;
$thumb_dir = MEDIA_DIR.'/videos/tmb/'.path($video_id);
VFolder::create($thumb_dir);
for ($i=1; $i<=3; $i++) {
$src = $m_path.'/thumb/'.$video['video_folder'].$i.'_'.$video['video_id'].'.jpg';
$dst = $thumb_dir.'/'.$i.'.jpg';
if (file_exists($src) && is_file($src)) {
$dt_method($src, $dst);
++$thumbs;
}
}
$ext = 'mp4';
$server = 0;
$mobile = 0;
$src_mp4 = $m_path.'/flvideo/'.$video['video_folder'].$video['video_flv_name'];
$dst_mp4 = MEDIA_DIR.'/videos/mp4/'.$video_id.'.mp4';
if (file_exists($src_mp4) && is_file($src_mp4)) {
$dt_method($src_mp4, $dst_mp4);
}
$db->query("UPDATE #__video
SET thumbs = ".$thumbs.",
ext = '".$db->escape($ext)."',
mobile = '".$mobile."',
server = ".$server.",
status = 1
WHERE video_id = ".$video_id."
LIMIT 1");
$files[$video_id] = array(
'old_video_id' => $video['video_id'],
'folder' => $video['video_folder'],
'filename' => $video['video_flv_name']
);
}
// doing the progress stuff here...not very accurate...enough for my eye
++$index;
if ($index === 50) {
$index = 0;
$progress = ($progress > 96) ? 96 : ($progress + $incr);
update_progress(floor($progress).'^Migrating videos...');
}
}
// progress 20%, lets initiate our database connections again :-)
update_progress('97^Migrating video comments...');
$mdb->close();
$mdb = new VInstall_db($m_db_host, $m_db_name, $m_db_username, $m_db_password, '');
$db = VF::factory_remove('database');
$db = VF::factory('database');
$mdb->query("SELECT * FROM comments");
$comments = $mdb->fetch_rows();
foreach ($comments as $comment) {
$video_id = FALSE;
$user_id = FALSE;
$db->query("SELECT video_id
FROM #__video
WHERE old_video_id = ".(int) $comment['comment_video_id']."
LIMIT 1");
if ($db->affected_rows()) {
$video_id = (int) $db->fetch_field('video_id');
}
$db->query("SELECT user_id
FROM #__user
WHERE old_user_id = ".(int) $comment['comment_user_id']."
LIMIT 1");
if ($db->affected_rows()) {
$user_id = (int) $db->fetch_field('user_id');
}
if ($video_id && $user_id) {
$db->query("INSERT INTO #__video_comments
SET video_id = ".$video_id.",
user_id = ".$user_id.",
comment = '".$db->escape($comment['comment_text'])."',
add_date = '".date('Y-m-d h:i:s', $comment['comment_add_time'])."'");
$db->query("UPDATE #__video
SET total_comments = total_comments+1
WHERE video_id = ".$video_id."
LIMIT 1");
}
}
// progress 20%, lets initiate our database connections again :-)
update_progress('99^Migrating video favorites...');
$mdb->close();
$mdb = new VInstall_db($m_db_host, $m_db_name, $m_db_username, $m_db_password, '');
$db = VF::factory_remove('database');
$db = VF::factory('database');
$mdb->query("SELECT * FROM favourite");
$favorites = $mdb->fetch_rows();
foreach ($favorites as $favorite) {
$video_id = FALSE;
$user_id = FALSE;
$db->query("SELECT video_id
FROM #__video
WHERE old_video_id = ".(int) $favorite['favourite_video_id']."
LIMIT 1");
if ($db->affected_rows()) {
$video_id = (int) $db->fetch_field('video_id');
}
$db->query("SELECT user_id
FROM #__user
WHERE old_user_id = ".(int) $favorite['favourite_user_id']."
LIMIT 1");
if ($db->affected_rows()) {
$user_id = (int) $db->fetch_field('user_id');
}
if ($video_id && $user_id) {
$db->query("INSERT INTO #__video_favorites
SET video_id = ".$video_id.",
user_id = ".$user_id);
}
}
file_put_contents(BASE_DIR.'/install/data/data_videos.php', serialize($files));
update_progress('100^Migration finished...');
$finished = TRUE;
}
}
?>