Exemplo n.º 1
0
 public function PostProcessSuccess($video_id)
 {
     // Adjust permissions and move directory
     $old_directory = $this->video_dir->GetBaseDir();
     @chmod($old_directory, 0777);
     $directory = Video_Dir::DirNameFromId($video_id);
     $this->video_dir->MoveTo($directory);
     // Add the embed code to the database
     DatabaseAdd('tbx_video_clip', array('video_id' => $video_id, 'clip' => $this->source[Video_Source::FIELD_EMBED], 'type' => 'Embed'));
     // Get the relative URL for each thumb and add to database
     $thumb_ids = array();
     foreach ($this->thumbs as $thumb) {
         $thumb = str_replace(array($old_directory, Config::Get('document_root')), array($directory, ''), $thumb);
         $thumb_ids[] = DatabaseAdd('tbx_video_thumbnail', array('video_id' => $video_id, 'thumbnail' => $thumb));
     }
     // Determine number of thumbnails and select random display thumbnail
     $num_thumbnails = count($this->thumbs);
     $display_thumbnail = null;
     if ($num_thumbnails > 0) {
         // Select display thumbnail randomly from the first 40%
         $display_thumbnail = $thumb_ids[rand(0, floor(0.4 * $num_thumbnails))];
     }
     $update = array('video_id' => $video_id, 'num_thumbnails' => $num_thumbnails, 'display_thumbnail' => $display_thumbnail, 'duration' => $this->duration);
     $DB = GetDB();
     $DB->Update('DELETE FROM `tbx_conversion_queue` WHERE `video_id`=?', array($video_id));
     DatabaseUpdate('tbx_video', $update);
 }
Exemplo n.º 2
0
 public static function Run()
 {
     chdir(realpath(dirname(__FILE__) . '/../'));
     require_once 'includes/global.php';
     $doc_root = Config::Get('document_root');
     $DB = GetDB();
     self::Log('Starting...');
     self::MarkRunning();
     while (true) {
         // See if we were requested to stop
         if (self::ShouldStop()) {
             self::Log('User requested stop...');
             break;
         }
         self::Ping();
         $DB->Connect();
         $queue_item = $DB->Row('SELECT * FROM `tbx_thumb_queue` ORDER BY `queued` LIMIT 1');
         if (!empty($queue_item)) {
             $video = $DB->Row('SELECT * FROM `tbx_video` WHERE `video_id`=?', array($queue_item['video_id']));
             if (!empty($video)) {
                 $DB->Update('UPDATE `tbx_thumb_queue` SET `date_started`=? WHERE `video_id`=?', array(Database_MySQL::Now(), $video['video_id']));
                 $clips = $DB->FetchAll('SELECT * FROM `tbx_video_clip` WHERE `video_id`=? AND `type`!=? ORDER BY `clip_id`', array($queue_item['video_id'], 'Embed'));
                 $dir = new Video_Dir(Video_Dir::DirNameFromId($video['video_id']));
                 Video_FrameGrabber::SetLogFile($dir->GetBaseDir() . '/thumbnailer.log');
                 $thumb_start = time();
                 try {
                     if (!empty($clips)) {
                         $thumbs = array();
                         $duration = 0;
                         // Number of thumbs to create per clip
                         $amount = round(Config::Get('thumb_amount') / count($clips));
                         // Move existing thumbnails
                         $dir->MoveFiles($dir->GetThumbsDir(), $dir->GetTempDir(), JPG_EXTENSION);
                         // Process each clip
                         foreach ($clips as $clip) {
                             self::Ping();
                             // Remote video
                             if (preg_match('~https?://~i', $clip['clip'])) {
                                 $http = new HTTP();
                                 if ($http->Get($clip['clip'], $clip['clip'])) {
                                     $video_file = $dir->AddOriginalFromVar($http->body, File::Extension($clip['clip']));
                                     $vi = new Video_Info($video_file);
                                     $vi->Extract();
                                     $duration += $vi->length;
                                     $temp_thumbs = Video_FrameGrabber::Grab($video_file, $dir->GetProcessingDir(), $amount, Config::Get('thumb_quality'), Config::Get('thumb_size'), $vi);
                                     // Move generated thumbs from the processing directory
                                     foreach ($temp_thumbs as $temp_thumb) {
                                         $thumbs[] = $dir->AddThumbFromFile($temp_thumb);
                                     }
                                     @unlink($video_file);
                                 }
                             } else {
                                 $temp_thumbs = Video_FrameGrabber::Grab($doc_root . '/' . $clip['clip'], $dir->GetProcessingDir(), $amount, Config::Get('thumb_quality'), Config::Get('thumb_size'));
                                 // Move generated thumbs from the processing directory
                                 foreach ($temp_thumbs as $temp_thumb) {
                                     $thumbs[] = $dir->AddThumbFromFile($temp_thumb);
                                 }
                             }
                         }
                         // Get the relative URL for each thumb and add to database
                         $thumb_ids = array();
                         foreach ($thumbs as $thumb) {
                             $thumb = str_replace($doc_root, '', $thumb);
                             $thumb_ids[] = DatabaseAdd('tbx_video_thumbnail', array('video_id' => $video['video_id'], 'thumbnail' => $thumb));
                         }
                         // Determine number of thumbnails and select random display thumbnail
                         $num_thumbnails = count($thumbs);
                         $display_thumbnail = null;
                         if ($num_thumbnails > 0) {
                             // Select display thumbnail randomly from the first 40%
                             $display_thumbnail = $thumb_ids[rand(0, floor(0.4 * $num_thumbnails))];
                         }
                         $update = array('video_id' => $video['video_id'], 'num_thumbnails' => $num_thumbnails, 'display_thumbnail' => $display_thumbnail);
                         if (empty($video['duration']) && !empty($duration)) {
                             $update['duration'] = $duration;
                         }
                         DatabaseUpdate('tbx_video', $update);
                         // Remove old thumbnails
                         $DB->Update('DELETE FROM `tbx_video_thumbnail` WHERE `video_id`=?' . (!empty($thumb_ids) ? ' AND`thumbnail_id` NOT IN (' . join(',', $thumb_ids) . ')' : ''), array($video['video_id']));
                         $dir->ClearTemp();
                     }
                 } catch (Exception $e) {
                     // Restore old thumbnails
                     $dir->MoveFiles($dir->GetTempDir(), $dir->GetThumbsDir(), JPG_EXTENSION);
                     Video_FrameGrabber::Log($e->getMessage() . (strtolower(get_class($e)) == 'baseexception' ? $e->getExtras() : '') . "\n" . $e->getTraceAsString());
                     self::UpdateStatsProcessed($thumb_start, $thumb_end, $queue_item['queued'], true);
                 }
                 $thumb_end = time();
                 $DB->Update('DELETE FROM `tbx_thumb_queue` WHERE `video_id`=?', array($queue_item['video_id']));
                 self::UpdateStatsProcessed($thumb_start, $thumb_end, $queue_item['queued']);
             }
         } else {
             break;
         }
     }
     self::MarkStopped();
     self::Log('Exiting...');
 }
Exemplo n.º 3
0
function tbxDisplayResetConfirm()
{
    global $t;
    $DB = GetDB();
    $v = Validator::Create();
    // Remove expired codes
    $DB->Update('DELETE FROM `tbx_user_reset_code` WHERE `timestamp` < ?', array(time() - 3600));
    $confirmation = $DB->Row('SELECT * FROM `tbx_user` JOIN `tbx_user_reset_code` USING (`username`) WHERE `reset_code`=?', array($_REQUEST['code']));
    $v->Register(empty($confirmation), Validator_Type::IS_FALSE, _T('Validation:Invalid confirmation code'));
    if (!$v->Validate()) {
        $t->Assign('g_errors', $v->GetErrors());
    } else {
        $DB->Update('DELETE FROM `tbx_user_reset_code` WHERE `username`=?', array($confirmation['username']));
        $user = $DB->Row('SELECT * FROM `tbx_user` JOIN `tbx_user_custom` USING (`username`) JOIN `tbx_user_stat` USING (`username`) WHERE `tbx_user`.`username`=?', array($confirmation['username']));
        $password = RandomPassword();
        DatabaseUpdate('tbx_user', array('username' => $user['username'], 'password' => sha1($password)));
        $t->AssignByRef('g_user', $user);
        $t->Assign('g_password', $password);
        $m = new Mailer();
        $m->Mail('email-user-reset.tpl', $t, $user['email'], $user['name']);
    }
    $t->Display('user-reset-confirmed.tpl');
}
Exemplo n.º 4
0
function tbxUploadStepTwo()
{
    global $t;
    $upload = $_FILES['video_file'];
    $v = Validator::Create();
    $DB = GetDB();
    $v->Register(sha1($_REQUEST['step_one_data'] . Config::Get('random_value')) == $_REQUEST['step_one_sig'], Validator_Type::IS_TRUE, _T('Validation:Video Data Altered'));
    $v->Register($upload['error'] == UPLOAD_ERR_OK, Validator_Type::IS_TRUE, Uploads::CodeToMessage($upload['error']));
    if (is_uploaded_file($upload['tmp_name'])) {
        $max_filesize = Format::StringToBytes(Config::Get('max_upload_size'));
        $max_duration = Format::DurationToSeconds(Config::Get('max_upload_duration'));
        $extensions = str_replace(',', '|', Config::Get('upload_extensions'));
        $v->Register($upload['size'], Validator_Type::IS_BETWEEN, _T('Validation:Video size too large'), '1,' . $max_filesize);
        $v->Register(File::Extension($upload['name']), Validator_Type::REGEX_MATCH, _T('Validation:Video file extension not allowed'), '~^(' . $extensions . ')$~');
        try {
            $vi = new Video_Info($upload['tmp_name']);
            $vi->Extract();
            $v->Register($vi->length, Validator_Type::LESS_EQ, _T('Validation:Video duration too long'), $max_duration);
        } catch (Exception $e) {
            $v->Register(false, Validator_Type::IS_TRUE, $e->getMessage());
        }
        $md5 = md5_file($upload['tmp_name']);
        if (Config::Get('flag_upload_reject_duplicates')) {
            $v->Register($DB->QueryCount('SELECT COUNT(*) FROM `tbx_video_md5sum` WHERE `md5`=?', array($md5)), Validator_Type::IS_ZERO, _T('Validation:Duplicate video'));
        }
    }
    // Validate input
    if (!$v->Validate()) {
        $t->Assign('g_errors', $v->GetErrors());
        $t->AssignByRef('g_form', $_REQUEST);
        if (isset($_REQUEST['flash'])) {
            $t->Display('upload-flash-errors.tpl');
        } else {
            $t->Assign('g_file_types', '*.' . str_replace(',', ';*.', Config::Get('upload_extensions')));
            $t->Assign('g_cookie', $_COOKIE[LOGIN_COOKIE]);
            $t->Display('upload-step-two.tpl');
        }
        return;
    }
    $_REQUEST = array_merge($_REQUEST, unserialize(base64_decode($_REQUEST['step_one_data'])));
    Form_Prepare::Standard('tbx_video');
    Form_Prepare::Standard('tbx_video_stat');
    Form_Prepare::Custom('tbx_video_custom_schema', 'on_submit');
    $_REQUEST['duration'] = $vi->length;
    $_REQUEST['date_added'] = Database_MySQL::Now();
    $_REQUEST['username'] = AuthenticateUser::GetUsername();
    $_REQUEST['is_private'] = Config::Get('flag_upload_allow_private') ? intval($_REQUEST['is_private']) : 0;
    $_REQUEST['allow_ratings'] = intval($_REQUEST['allow_ratings']);
    $_REQUEST['allow_embedding'] = intval($_REQUEST['allow_embedding']);
    $_REQUEST['allow_comments'] = intval($_REQUEST['allow_comments']) ? 'Yes - Add Immediately' : 'No';
    $_REQUEST['is_user_submitted'] = 1;
    if ($_REQUEST['recorded_day'] && $_REQUEST['recorded_month'] && $_REQUEST['recorded_year']) {
        $_REQUEST['date_recorded'] = $_REQUEST['recorded_year'] . '-' . $_REQUEST['recorded_month'] . '-' . $_REQUEST['recorded_day'];
    }
    // Strip HTML tags
    if (Config::Get('flag_video_strip_tags')) {
        $_REQUEST = String::StripTags($_REQUEST);
    }
    // Configure status
    $_REQUEST['status'] = STATUS_ACTIVE;
    if (Config::Get('flag_upload_convert')) {
        $_REQUEST['status'] = STATUS_QUEUED;
        $_REQUEST['next_status'] = Config::Get('flag_upload_review') ? STATUS_PENDING : STATUS_ACTIVE;
    } else {
        if (Config::Get('flag_upload_review')) {
            $_REQUEST['status'] = STATUS_PENDING;
        }
    }
    // Add to database
    $_REQUEST['video_id'] = DatabaseAdd('tbx_video', $_REQUEST);
    DatabaseAdd('tbx_video_custom', $_REQUEST);
    DatabaseAdd('tbx_video_stat', $_REQUEST);
    if ($_REQUEST['status'] == STATUS_ACTIVE && !$_REQUEST['is_private']) {
        Tags::AddToFrequency($_REQUEST['tags']);
    } else {
        if ($_REQUEST['status'] == STATUS_QUEUED) {
            DatabaseAdd('tbx_conversion_queue', array('video_id' => $_REQUEST['video_id'], 'queued' => time()));
        }
    }
    // Mark as private
    if ($_REQUEST['is_private']) {
        $_REQUEST['private_id'] = sha1(uniqid(rand(), true));
        DatabaseAdd('tbx_video_private', $_REQUEST);
    }
    // Setup video files and generate thumbnails
    $directory = Video_Dir::DirNameFromId($_REQUEST['video_id']);
    $vd = new Video_Dir($directory);
    $clip = $vd->AddClipFromFile($upload['tmp_name'], File::Extension($upload['name']));
    if (Video_FrameGrabber::CanGrab()) {
        Video_FrameGrabber::Grab($clip, $vd->GetThumbsDir(), Config::Get('thumb_amount'), Config::Get('thumb_quality'), Config::Get('thumb_size'), $vi);
    }
    foreach ($vd->GetClipURIs() as $clip) {
        $_REQUEST['clip'] = $clip;
        $_REQUEST['filesize'] = filesize(Config::Get('document_root') . $clip);
        DatabaseAdd('tbx_video_clip', $_REQUEST);
    }
    $thumb_ids = array();
    foreach ($vd->GetThumbURIs() as $thumb) {
        $_REQUEST['thumbnail'] = $thumb;
        $thumb_ids[] = DatabaseAdd('tbx_video_thumbnail', $_REQUEST);
    }
    // Select the display thumbnail
    $num_thumbnails = count($thumb_ids);
    $display_thumbnail = null;
    if ($num_thumbnails > 0) {
        $display_thumbnail = $thumb_ids[rand(0, floor(0.4 * $num_thumbnails))];
    }
    DatabaseUpdate('tbx_video', array('video_id' => $_REQUEST['video_id'], 'num_thumbnails' => $num_thumbnails, 'display_thumbnail' => $display_thumbnail));
    // Add MD5 sum for prevention of duplicates
    $DB->Update('REPLACE INTO `tbx_video_md5sum` VALUES (?)', array($md5));
    // Update user stats
    StatsRollover();
    $DB->Update('UPDATE `tbx_user_stat` SET ' . '`today_videos_uploaded`=`today_videos_uploaded`+1,' . '`week_videos_uploaded`=`week_videos_uploaded`+1,' . '`month_videos_uploaded`=`month_videos_uploaded`+1,' . '`total_videos_uploaded`=`total_videos_uploaded`+1 ' . 'WHERE `username`=?', array($_REQUEST['username']));
    $t->AssignByRef('g_form', $_REQUEST);
    $t->AssignByRef('g_video', $_REQUEST);
    $t->Display(isset($_REQUEST['flash']) ? 'upload-flash-complete.tpl' : 'upload-complete.tpl');
    UpdateCategoryStats($_REQUEST['category_id']);
    if (!Config::Get('flag_using_cron') && $_REQUEST['status'] == STATUS_QUEUED) {
        ConversionQueue::Start();
    }
}
Exemplo n.º 5
0
 public function PostProcessSuccess($video_id)
 {
     // Adjust permissions and move directory
     $doc_root = Config::Get('document_root');
     $old_directory = $this->video_dir->GetBaseDir();
     @chmod($old_directory, 0777);
     $directory = Video_Dir::DirNameFromId($video_id);
     $this->video_dir->MoveTo($directory);
     // Get the relative URL for each clip and add to database
     foreach ($this->clips as $clip) {
         $clip = str_replace(array($old_directory, $doc_root), array($directory, ''), $clip);
         DatabaseAdd('tbx_video_clip', array('video_id' => $video_id, 'filesize' => filesize($doc_root . $clip), 'clip' => $clip, 'type' => 'URL'));
     }
     // Get the relative URL for each thumb and add to database
     $thumb_ids = array();
     foreach ($this->thumbs as $thumb) {
         $thumb = str_replace(array($old_directory, $doc_root), array($directory, ''), $thumb);
         $thumb_ids[] = DatabaseAdd('tbx_video_thumbnail', array('video_id' => $video_id, 'thumbnail' => $thumb));
     }
     // Determine number of thumbnails and select random display thumbnail
     $num_thumbnails = count($this->thumbs);
     $display_thumbnail = null;
     if ($num_thumbnails > 0) {
         // Select display thumbnail randomly from the first 40%
         $display_thumbnail = $thumb_ids[rand(0, floor(0.4 * $num_thumbnails))];
     }
     $update = array('video_id' => $video_id, 'num_thumbnails' => $num_thumbnails, 'display_thumbnail' => $display_thumbnail, 'duration' => $this->duration);
     DatabaseUpdate('tbx_video', $update);
 }
Exemplo n.º 6
0
function tbxGenericEdit($type)
{
    Privileges::Check(Privileges::FromType($type));
    $schema = GetDBSchema();
    $xtable = $schema->el('//table[naming/type="' . $type . '"]');
    $table = $xtable->name->val();
    $function_name = 'tbx' . $xtable->naming->function . 'Edit';
    $function_exists = function_exists($function_name);
    $primary_key = $xtable->columns->primaryKey->val();
    $v = Validator::Create();
    $v->RegisterFromXml($xtable);
    if (!empty($xtable->custom)) {
        $xtable_custom = $schema->el('//table[name="' . $xtable->custom->val() . '"]');
        $v->RegisterFromXml($xtable_custom);
    }
    if ($function_exists) {
        call_user_func($function_name, Phase::PRE_VALIDATE);
    }
    if (!$v->Validate()) {
        if ($function_exists) {
            call_user_func($function_name, Phase::VALIDATION_FAILED);
        }
        $output = array('message' => $xtable->naming->textUpper . ' could not be updated; please fix the following items', 'errors' => $v->GetErrors());
        JSON::Failure($output);
    } else {
        if ($function_exists) {
            call_user_func($function_name, Phase::PRE_UPDATE);
        }
        $original = DatabaseUpdate($table, $_REQUEST);
        if (!empty($xtable->custom)) {
            DatabaseUpdate($xtable->custom->val(), $_REQUEST);
        }
        if ($function_exists) {
            call_user_func($function_name, Phase::POST_UPDATE);
        }
        $output = array('id' => $original[$primary_key], 'message' => $xtable->naming->textUpper . ' has been successfully updated', 'html' => SearchItemHtml($type, $original));
        JSON::Success($output);
    }
}
Exemplo n.º 7
0
 public static function Import($settings)
 {
     $DB = GetDB();
     ProgressBarShow('pb-import');
     $file = TEMP_DIR . '/' . File::Sanitize($settings['import_file']);
     $fp = fopen($file, 'r');
     $filesize = filesize($file);
     $line = $read = $imported = 0;
     $expected = count($settings['fields']);
     while (!feof($fp)) {
         $line++;
         $string = fgets($fp);
         $read += strlen($string);
         $data = explode($settings['delimiter'], trim($string));
         ProgressBarUpdate('pb-import', $read / $filesize * 100);
         // Line does not have the expected number of fields
         if (count($data) != $expected) {
             continue;
         }
         $video = array();
         $defaults = array('category_id' => $settings['category_id'], 'sponsor_id' => $settings['sponsor_id'], 'username' => $settings['username'], 'duration' => Format::DurationToSeconds($settings['duration']), 'status' => $settings['status'], 'next_status' => $settings['status'], 'allow_comments' => $settings['allow_comments'], 'allow_ratings' => $settings['allow_ratings'], 'allow_embedding' => $settings['allow_embedding'], 'is_private' => $settings['is_private'], 'date_added' => Database_MySQL::Now(), 'is_featured' => 0, 'is_user_submitted' => 0, 'conversion_failed' => 0, 'tags' => null, 'title' => null, 'description' => null);
         foreach ($settings['fields'] as $index => $field) {
             if (!empty($field)) {
                 $video[$field] = trim($data[$index]);
             }
         }
         // Setup clips
         $clips = array();
         $thumbs = array();
         $clip_type = 'URL';
         if (isset($video['embed_code'])) {
             // Cannot convert or thumbnail from embed code
             $settings['flag_convert'] = $settings['flag_thumb'] = false;
             $clips[] = $video['embed_code'];
             $clip_type = 'Embed';
         } else {
             if (isset($video['gallery_url'])) {
                 $http = new HTTP();
                 if (!$http->Get($video['gallery_url'])) {
                     // Broken gallery URL, continue
                     continue;
                 }
                 list($thumbs, $clips) = Video_Source_Gallery::ExtractUrls($http->url, $http->body);
             } else {
                 if (!isset($video['video_url']) && isset($video['base_video_url'])) {
                     if (!preg_match('~/$~', $video['base_video_url'])) {
                         $video['base_video_url'] .= '/';
                     }
                     foreach (explode(',', $video['video_filename']) as $filename) {
                         $clips[] = $video['base_video_url'] . $filename;
                     }
                 } else {
                     $clips[] = $video['video_url'];
                 }
             }
         }
         // Check for duplicate clips
         $duplicate = false;
         foreach ($clips as $clip) {
             if (!Request::Get('flag_skip_imported_check') && $DB->QueryCount('SELECT COUNT(*) FROM `tbx_imported` WHERE `video_url`=?', array($clip)) > 0) {
                 $duplicate = true;
             }
             $DB->Update('REPLACE INTO `tbx_imported` VALUES (?)', array($clip));
         }
         // Dupe found
         if ($duplicate) {
             continue;
         }
         // Setup thumbs
         if (!isset($video['gallery_url']) && !isset($video['thumbnail_url']) && isset($video['base_thumbnail_url'])) {
             if (!preg_match('~/$~', $video['base_thumbnail_url'])) {
                 $video['base_thumbnail_url'] .= '/';
             }
             foreach (explode(',', String::FormatCommaSeparated($video['thumbnail_filename'])) as $filename) {
                 $thumbs[] = $video['base_thumbnail_url'] . $filename;
             }
         } else {
             if (!isset($video['gallery_url']) && isset($video['thumbnail_url'])) {
                 $thumbs[] = $video['thumbnail_url'];
             }
         }
         // Setup duration
         if (isset($video['duration_seconds'])) {
             $video['duration'] = $video['duration_seconds'];
         } else {
             if (isset($video['duration_formatted'])) {
                 $video['duration'] = Format::DurationToSeconds($video['duration_formatted']);
             }
         }
         // Use description for title
         if (empty($video['title'])) {
             $video['title'] = isset($video['description']) ? $video['description'] : '';
         }
         // Use title for description
         if (empty($video['description'])) {
             $video['description'] = isset($video['title']) ? $video['title'] : '';
         }
         // Use title for tags
         if (empty($video['tags'])) {
             $video['tags'] = isset($video['title']) ? $video['title'] : '';
         }
         // Setup category
         if (isset($video['category']) && ($category_id = $DB->QuerySingleColumn('SELECT `category_id` FROM `tbx_category` WHERE `name` LIKE ?', array($video['category']))) !== false) {
             $video['category_id'] = $category_id;
         } else {
             if (($category_id = GetBestCategory($video['title'] . ' ' . $video['description'])) !== null) {
                 $video['category_id'] = $category_id;
             }
         }
         // Merge in the defaults
         $video = array_merge($defaults, $video);
         // Format tags and convert to UTF-8
         $video['tags'] = Tags::Format($video['tags']);
         $video = String::ToUTF8($video);
         if (Request::Get('flag_convert')) {
             $video['status'] = STATUS_QUEUED;
         }
         // Add to database
         $video['video_id'] = DatabaseAdd('tbx_video', $video);
         DatabaseAdd('tbx_video_custom', $video);
         DatabaseAdd('tbx_video_stat', $video);
         if ($video['is_private']) {
             $video['private_id'] = sha1(uniqid(mt_rand(), true));
             DatabaseAdd('tbx_video_private', $video);
         }
         if ($video['status'] == STATUS_QUEUED) {
             $video['queued'] = time();
             DatabaseAdd('tbx_conversion_queue', $video);
         }
         if (Request::Get('flag_thumb')) {
             $video['queued'] = time();
             DatabaseAdd('tbx_thumb_queue', $video);
         }
         if ($video['status'] == STATUS_ACTIVE && !$video['is_private']) {
             Tags::AddToFrequency($video['tags']);
         }
         // Add clips
         foreach ($clips as $clip) {
             DatabaseAdd('tbx_video_clip', array('video_id' => $video['video_id'], 'type' => $clip_type, 'clip' => $clip));
         }
         $dir = new Video_Dir(Video_Dir::DirNameFromId($video['video_id']));
         // Process thumbs
         $thumb_ids = array();
         foreach ($thumbs as $thumb) {
             $http = new HTTP();
             if ($http->Get($thumb, $thumb)) {
                 if (Video_Thumbnail::CanResize()) {
                     $thumb_temp = $dir->AddTempFromVar($http->body, 'jpg');
                     $thumb_file = Video_Thumbnail::Resize($thumb_temp, Config::Get('thumb_size'), Config::Get('thumb_quality'), $dir->GetThumbsDir());
                 } else {
                     $thumb_file = $dir->AddThumbFromVar($http->body);
                 }
                 if (!empty($thumb_file)) {
                     $thumb_ids[] = DatabaseAdd('tbx_video_thumbnail', array('video_id' => $video['video_id'], 'thumbnail' => str_replace(Config::Get('document_root'), '', $thumb_file)));
                 }
             }
         }
         // Determine number of thumbnails and select random display thumbnail
         $num_thumbnails = count($thumb_ids);
         $display_thumbnail = null;
         if ($num_thumbnails > 0) {
             // Select display thumbnail randomly from the first 40%
             $display_thumbnail = $thumb_ids[rand(0, floor(0.4 * $num_thumbnails))];
         }
         DatabaseUpdate('tbx_video', array('video_id' => $video['video_id'], 'num_thumbnails' => $num_thumbnails, 'display_thumbnail' => $display_thumbnail));
         $imported++;
     }
     fclose($fp);
     UpdateCategoryStats();
     UpdateSponsorStats($settings['sponsor_id']);
     $t = new Template();
     $t->ClearCache('categories.tpl');
     ProgressBarHide('pb-import', NumberFormatInteger($imported) . ' videos have been imported!');
     // Start up the thumbnail and converson queues if needed
     if (!Config::Get('flag_using_cron')) {
         if (Request::Get('flag_convert')) {
             ConversionQueue::Start();
         }
         if (Request::Get('flag_thumb')) {
             ThumbQueue::Start();
         }
     }
     File::Delete($file);
 }