<?php
/**
 * Class to handle the form fields
 *
 * This is having class FormHandler to handle the validation of form fields, to
 * set or get values for form fields, to handle page blocks, to handle CSS for
 * form fields and to populate the common static array file.
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		VideoUploadLib
 * @author		senthil_52ag05
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id : $
 * @since 		2008-11-05
 */
if(class_exists('AnovaHandler'))
	{
		$parent_class = 2;
	}
else
	{
		$parent_class = 1;
	}

switch($parent_class)
	{
		case 2:
			class VideoAudioHandler extends AnovaHandler{}
			break;
		case 1:
		default:
			class VideoAudioHandler extends FormHandler{}
			break;
	}

class VideoUploadLib extends VideoAudioHandler
	{
		public $___error_msg;
		/**
		 * VideoUpload::setIHObject()
		 *
		 * @param $imObj
		 * @return
		 **/
		public function setIHObject($imObj)
			{
				$this->imageObj = $imObj;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function videoToFrame($source_filename, $temp_dir, $total_frames, $scale)
			{
				if (substr($temp_dir,strlen($temp_dir)-1) == '/')
					$t_dir = substr($temp_dir, 0, strlen($temp_dir)-1);
				else
					$t_dir = $temp_dir;

				$total_frames = $total_frames+1;
				for($i=1;$i<=27;$i+=8)
					{
						if($this->CFG['debug']['store_log'])
							{
								$log_str = 'Video to Frame Conversion:'."\r\n";
								$log_str .= $this->CFG['admin']['video']['mplayer_path']."\""." ".$source_filename." -nosound -vo jpeg:outdir=".$t_dir." -ss ".$i." -frames ".$total_frames."\r\n";
								$this->writetoTempFile($log_str);
							}
						exec("\"".$this->CFG['admin']['video']['mplayer_path']."\""." ".$source_filename." -nosound -vf scale=".$scale." -vo jpeg:outdir=".$t_dir." -ss ".$i." -frames ".$total_frames);
					}
				if($this->CFG['debug']['store_log'])
					{
						if ($handle = opendir($temp_dir)) {
						    $log_str .= "Directory ".$temp_dir." in which frames created: $handle\r\n";
						    $log_str .= "Files:\r\n";

						    /* This is the correct way to loop over the directory. */
						    while (false !== ($file = readdir($handle))) {
						        $log_str .= "$file\r\n";
						    }
						    closedir($handle);
						}
						$this->writetoTempFile($log_str);
					}

				for($k=1;$k<$total_frames;$k++)
					{
						$from = $temp_dir.str_pad($k+1, 8, '0', STR_PAD_LEFT).'.'.$this->CFG['video']['image']['extensions'];
						$to = $temp_dir.str_pad($k, 8, '0', STR_PAD_LEFT).'.'.$this->CFG['video']['image']['extensions'];
						if(is_file($from))
							{
								if($this->CFG['debug']['store_log'])
									{
										$log_str = 'Video to Frame Conversion Success:'."\r\n";
										$this->writetoTempFile($log_str);
									}
								if(is_file($to))
									unlink($to);
								copy($from, $to);
							}
						else
							{
								if($this->CFG['debug']['store_log'])
									{
										$log_str = 'Video to Frame Conversion Failed:'."\r\n";
										$log_str .= 'Expected file: '.$from.' not created:'."\r\n";
										$this->writetoTempFile($log_str);
									}
							}
					}
				$file = $temp_dir.str_pad($total_frames, 8, '0', STR_PAD_LEFT).'.'.$this->CFG['video']['image']['extensions'];
				if(is_file($file))
					unlink($file);
			}

		/**
		 * VideoUpload::getServerDetails()
		 *
		 * @return
		 **/
		public function getServerDetails($server_for = 'Ans_video')
			{
				$sql = 'SELECT server_url, ftp_server, ftp_folder, ftp_usrename, ftp_password FROM'.
						' '.$this->CFG['db']['tbl']['server_settings'].
						' WHERE server_for='.$this->dbObj->Param('Ans_video').
						' AND server_status=\'Yes\' LIMIT 0,1';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($server_for));
				    if (!$rs)
					    trigger_db_error($this->dbObj);

				if(!$rs->PO_RecordCount())
					return false;

				$this->FTP_SERVER = '';
				$this->FTP_FOLDER = '';
				$this->FTP_USERNAME = '';
				$this->FTP_PASSWORD = '';
				$this->FTP_SERVER_URL = '';

				while($row = $rs->FetchRow())
					{
						$this->FTP_SERVER = $row['ftp_server'];
						$this->FTP_FOLDER = $row['ftp_folder'];
						$this->FTP_USERNAME = $row['ftp_usrename'];
						$this->FTP_PASSWORD = $row['ftp_password'];
						$this->FTP_SERVER_URL = $row['server_url'];
						return true;
					}
				if(isset($this->FTP_SERVER) and $this->FTP_SERVER)
					return true;
				return false;
			}

		/**
		 * VideoActivate::activateVideoFile()
		 *
		 * @return
		 **/
		public function activateVideoFile($video_type = 'video_id')
			{
				$dir_thumb = $this->_dir_path.$this->CFG['admin']['ans_videos']['thumbnail_folder'];
				$dir_video = $this->_dir_path.$this->CFG['admin']['ans_videos']['video_folder'];

				$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];
				$tempurl =  $temp_dir.$this->VIDEO_NAME;

				$local_upload = true;
				if($this->getServerDetails())
					{
						if($FtpObj = new FtpHandler($this->FTP_SERVER,$this->FTP_USERNAME,$this->FTP_PASSWORD))
							{
								if($this->FTP_FOLDER)
									$FtpObj->changeDirectory($this->FTP_FOLDER);

								$dir_thumb = $this->_dir_path.$this->CFG['admin']['ans_videos']['thumbnail_folder'];
								$dir_video = $this->_dir_path.$this->CFG['admin']['ans_videos']['video_folder'];

								$FtpObj->makeDirectory($dir_thumb);
								$FtpObj->makeDirectory($dir_video);

								if(is_file($tempurl.'T.'.$this->CFG['video']['image']['extensions']))
									{
										$FtpObj->moveTo($tempurl.'T.'.$this->CFG['video']['image']['extensions'], $dir_thumb.$this->VIDEO_NAME.'T.'.$this->CFG['video']['image']['extensions']);
										unlink($tempurl.'T.'.$this->CFG['video']['image']['extensions']);
									}

								if(is_file($tempurl.'L.'.$this->CFG['video']['image']['extensions']))
									{
										$FtpObj->moveTo($tempurl.'L.'.$this->CFG['video']['image']['extensions'], $dir_thumb.$this->VIDEO_NAME.'L.'.$this->CFG['video']['image']['extensions']);
										unlink($tempurl.'L.'.$this->CFG['video']['image']['extensions']);
									}

								if(is_file($tempurl.'.flv'))
									{
										$FtpObj->moveTo($tempurl.'.flv', $dir_video.$this->VIDEO_NAME.'.flv');
										unlink($tempurl.'.flv');
									}
								if(is_file($tempurl.'.'.$this->VIDEO_EXT))
									{
										unlink($tempurl.'.'.$this->VIDEO_EXT);
									}
								$FtpObj->ftpClose();
								$SERVER_URL = $this->FTP_SERVER_URL;
								$local_upload = false;
							}
					}
				if($local_upload)
					{
						$upload_dir_thumb = $dir_thumb;
						$upload_dir_video = $dir_video;

						$this->chkAndCreateFolder($upload_dir_thumb);
						$this->chkAndCreateFolder($upload_dir_video);

						$uploadUrlThumb = $upload_dir_thumb.$this->VIDEO_NAME;
						$uploadUrlVideo = $upload_dir_video.$this->VIDEO_NAME;

						if(is_file($tempurl.'T.'.$this->CFG['video']['image']['extensions']))
							{
								copy($tempurl.'T.'.$this->CFG['video']['image']['extensions'], $uploadUrlThumb.'T.'.$this->CFG['video']['image']['extensions']);
								unlink($tempurl.'T.'.$this->CFG['video']['image']['extensions']);
							}

						if(is_file($tempurl.'L.'.$this->CFG['video']['image']['extensions']))
							{
								copy($tempurl.'L.'.$this->CFG['video']['image']['extensions'], $uploadUrlThumb.'L.'.$this->CFG['video']['image']['extensions']);
								unlink($tempurl.'L.'.$this->CFG['video']['image']['extensions']);
							}

						if(is_file($tempurl.'.flv'))
							{
								copy($tempurl.'.flv', $uploadUrlVideo.'.flv');
								unlink($tempurl.'.flv');
							}
						if(is_file($tempurl.'.'.$this->VIDEO_EXT))
							{
								unlink($tempurl.'.'.$this->VIDEO_EXT);
							}
						$SERVER_URL = $this->CFG['site']['url'];
					}
				if(isset($SERVER_URL))
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video'].' SET'.
								' video_server_url='.$this->dbObj->Param('video_server_url').', video_status=\'Ok\' WHERE'.
								' video_id='.$this->dbObj->Param('video_id');

						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($SERVER_URL, $this->VIDEO_ID));
						    if (!$rs)
							    trigger_db_error($this->dbObj);

						if($this->VIDEO_FOR == 'Question')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['questions'].' SET'.
										' '.$video_type.'='.$this->dbObj->Param('video_id').' WHERE'.
										' ques_id='.$this->dbObj->Param('ques_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->VIDEO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						else if($this->VIDEO_FOR == 'Answer')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['answers'].' SET'.
										' '.$video_type.'='.$this->dbObj->Param('video_id').' WHERE'.
										' ans_id='.$this->dbObj->Param('ans_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->VIDEO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);

								if($this->dbObj->Affected_Rows())
									{
										$sql = 'SELECT ques_id FROM '.$this->CFG['db']['tbl']['answers'].' WHERE'.
												' ans_id='.$this->dbObj->Param('ans_id');

										$stmt = $this->dbObj->Prepare($sql);
										$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
										    if (!$rs)
											    trigger_db_error($this->dbObj);

										$row = $rs->FetchRow();

										$sql = 'UPDATE '.$this->CFG['db']['tbl']['questions'].' SET'.
												' total_videos=total_videos+1 WHERE'.
												' ques_id='.$this->dbObj->Param('ques_id');

										$stmt = $this->dbObj->Prepare($sql);
										$rs = $this->dbObj->Execute($stmt, array($row['ques_id']));
										    if (!$rs)
											    trigger_db_error($this->dbObj);
									}
							}
						else if($this->VIDEO_FOR == 'Forumresponse')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_response'].' SET'.
										' '.$video_type.'='.$this->dbObj->Param('video_id').' WHERE'.
										' response_id='.$this->dbObj->Param('ques_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->VIDEO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						else if($this->VIDEO_FOR == 'Blog')
							{
								//die($this->CONTENT_ID);
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['blogs'].' SET'.
										' '.$video_type.'='.$this->dbObj->Param('video_id').' WHERE'.
										' blog_id='.$this->dbObj->Param('ques_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->VIDEO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						else if($this->VIDEO_FOR == 'Blogcomment')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['blog_comment'].' SET'.
										' '.$video_type.'='.$this->dbObj->Param('video_id').' WHERE'.
										' comment_id='.$this->dbObj->Param('comment_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->VIDEO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						elseif($this->VIDEO_FOR == 'Debate')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['debates'].' SET'.
										' video_id='.$this->dbObj->Param('video_id').' WHERE'.
										' debate_id='.$this->dbObj->Param('debate_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->VIDEO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						else if($this->VIDEO_FOR == 'DebateResponse')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['debate_responses'].' SET'.
										' video_id='.$this->dbObj->Param('video_id').' WHERE'.
										' response_id='.$this->dbObj->Param('response_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->VIDEO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);

								if($this->dbObj->Affected_Rows())
									{
										$sql = 'SELECT debate_id FROM '.$this->CFG['db']['tbl']['debates'].' WHERE'.
												' response_id='.$this->dbObj->Param('response_id');

										$stmt = $this->dbObj->Prepare($sql);
										$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
										    if (!$rs)
											    trigger_db_error($this->dbObj);

										$row = $rs->FetchRow();

										$sql = 'UPDATE '.$this->CFG['db']['tbl']['debates'].' SET'.
												' total_videos=total_videos+1 WHERE'.
												' debate_id='.$this->dbObj->Param('debate_id');

										$stmt = $this->dbObj->Prepare($sql);
										$rs = $this->dbObj->Execute($stmt, array($row['debate_id']));
										    if (!$rs)
											    trigger_db_error($this->dbObj);
									}
							}
						return true;
					}
				return false;
			}

		/**
		 * VideoActivate::populateVideoDetails()
		 *
		 * @return
		 **/
		public function populateVideoDetails($video_id, $status = 'No')
			{
				$sql = 'SELECT video_id, video_ext, t_width, t_height, content_id, video_for FROM'.
						' '.$this->CFG['db']['tbl']['ans_video'].' WHERE'.
						' video_id='.$this->dbObj->Param('video_id').' AND'.
						' video_encoded_status='.$this->dbObj->Param('video_encoded_status').' LIMIT 0,1';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($video_id, $status));
				    if (!$rs)
					    trigger_db_error($this->dbObj);

				if($row = $rs->FetchRow())
					{
						$this->VIDEO_WIDTH = $row['t_width'];
						$this->VIDEO_HEIGHT = $row['t_height'];
						$this->VIDEO_ID = $row['video_id'];
						$this->VIDEO_NAME = getImageName($row['video_id']);
						$this->VIDEO_EXT = $row['video_ext'];
						$this->CONTENT_ID = $row['content_id'];
						$this->VIDEO_FOR = $row['video_for'];
						return true;
					}
				return false;

			}

		/**
		 * VideoUpload::storeImagesTempServer()
		 *
		 * @param $uploadUrl
		 * @param $extern
		 * @return
		 **/
		public function storeImagesTempServer($uploadUrl, $extern)
			{
				$this->imageObj->resize($this->CFG['admin']['ans_videos']['large_width'], $this->CFG['admin']['ans_videos']['large_height'], '-');
				$this->imageObj->output_resized($uploadUrl.'L.'.$extern, strtoupper($extern));

                $this->imageObj->resize($this->CFG['admin']['ans_videos']['thumb_width'], $this->CFG['admin']['ans_videos']['thumb_height'], '-');
				$this->imageObj->output_resized($uploadUrl.'T.'.$extern, strtoupper($extern));
				$image_info = getImageSize($uploadUrl.'T.'.$extern);
				$this->T_WIDTH = $image_info[0];
				$this->T_HEIGHT = $image_info[1];
			}

		/**
		 * VideoUpload::getPlayingTime()
		 *
		 * @param $text
		 * @return
		 **/
		public function getPlayingTime($text_arr)
			{
				$vdoname = $text_arr;
				exec($this->CFG['admin']['video']['mplayer_path'].' -vo null -ao null -frames 0 -identify '.$vdoname, $p);
				$length='';
				while(list($k,$v)=each($p))
					{
						if($length=strstr($v,'ID_LENGTH='))
				        	break;
				    }
				$lx = explode("=",$length);
				if(isset($lx[1]))
				$duration = $lx[1];
				else
					$duration = 0;
				$min = floor($duration/60);
				$sec = round($duration-($min*60));
				$min = str_pad($min, 2, '0', STR_PAD_LEFT);
				$sec = str_pad($sec, 2, '0', STR_PAD_LEFT);
				return $min.':'.$sec;
			}

		/**
		 * VideoUpload::videoEncode()
		 *
		 * @param $video_id
		 * @return
		 **/
		public function videoEncode($video_id)
			{
				if($this->populateVideoDetails($video_id))
					{
						$source_filename = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'].$this->VIDEO_NAME.'.'.$this->VIDEO_EXT;
						$store_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];

						if($this->CFG['debug']['store_log'])
							{
								$log_str = "\n Before Uploading \n".$source_filename."\n";
								$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
								$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
								$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
								$this->writetoTempFile($log_str);
							}

						if(is_file($source_filename))
							{
								$this->changeEncodedStatus('Partial');
								$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'].$this->VIDEO_NAME.'/';
								$this->chkAndCreateFolder($temp_dir);
								$temp_file = $temp_dir.'00000001.'.$this->CFG['video']['image']['extensions'];
								$final_name = $store_dir.$this->VIDEO_NAME.'T.'.$this->CFG['video']['image']['extensions'];

								$result = exec("\"".$this->CFG['admin']['video']['flvtool2_path']."\" -UP ". $source_filename);
								if($this->CFG['debug']['store_log'])
									{
										$log_str = "\n Flvtool2 start \n Flvtool2 execution Result \n".@implode("\n", $result);
										$log_str .= "\n Execute Query : "."\"".$this->CFG['admin']['video']['flvtool2_path']."\" -UP ". $source_filename;
										$log_str .= "\n Flvtool2 execution Output \n".@implode("\n", $p)."\n Flvtool2 End \n";
										$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
										$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
										$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
										$this->writetoTempFile($log_str);
									}

								$this->videoToFrame($source_filename, $temp_dir, 1, $this->CFG['admin']['ans_videos']['large_width'].':'.$this->CFG['admin']['ans_videos']['large_height']);
								if($this->CFG['debug']['store_log'])
									{
										$log_str = "\n videoToFrame created successfully \n".$source_filename."\n";
										$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
										$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
										$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
										$this->writetoTempFile($log_str);
									}

								if(is_file(sprintf($temp_file,1)))									{
										copy($temp_file, $final_name);
										unlink($temp_file);
										$this->removeDirectory($temp_dir);
										//get Video

										$this->setFormField('playing_time', $this->getPlayingTime($source_filename));
										if($this->CFG['debug']['store_log'])
											{
												$log_str = "\n Playing time calculated successfully \n".$source_filename."\n";
												$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
												$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
												$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
												$log_str .= "\n Playing Time: ".$this->fields_arr['playing_time']."\n";
												$log_str .= "\n $temp_file ".(is_file($temp_file)?' is a file':'NOT a file')."\n";
												$this->writetoTempFile($log_str);
											}

										$temp_file = $store_dir.$this->VIDEO_NAME;

                                                  //resize the image
										$imageObj = new ImageHandler($temp_file.'T.'.$this->CFG['video']['image']['extensions']);
										$this->setIHObject($imageObj);

										$this->storeImagesTempServer($temp_file, $this->CFG['video']['image']['extensions']);
										if($this->CFG['debug']['store_log'])
											{
												$log_str = "\n Created images successfully\n".$source_filename."\n";
												$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
												$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
												$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
												$this->writetoTempFile($log_str);
											}

										$this->updateVideoTable($this->VIDEO_ID);

										$this->activateVideoFile();
										if($this->CFG['debug']['store_log'])
											{
												$log_str = "\n Activated the video successfully\n".$source_filename."\n";
												$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
												$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
												$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
												$this->writetoTempFile($log_str);
											}
									}
								else
									{
										$this->removeDirectory($temp_dir);
										$this->video_uploaded_success = false;
										if(is_file($source_filename))
											unlink($source_filename);
										$this->deleteVideoTable();
									}
							}
					}
			}

		/**
		 * VideoUpload::updateVideoTable()
		 *
		 * @param $video_id
		 * @return
		 **/
		public function updateVideoTable($video_id)
			{
				//Update video table
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video'].' SET'.
						' t_width='.$this->dbObj->Param('t_width').', t_height='.$this->dbObj->Param('t_height').','.
						' video_encoded_status=\'Yes\','.
						' playing_time='.$this->dbObj->Param('playing_time').
						' WHERE video_id='.$this->dbObj->Param('video_id');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->T_WIDTH, $this->T_HEIGHT, $this->fields_arr['playing_time'], $this->VIDEO_ID));
				    if (!$rs)
					    trigger_db_error($this->dbObj);
			}

		/**
		 * VideoUpload::changeEncodedStatus()
		 *
		 * @return
		 **/
		public function changeEncodedStatus($status)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video'].' SET'.
						' video_encoded_status='.$this->dbObj->Param('video_encoded_status').
						' WHERE video_id='.$this->dbObj->Param('video_id');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($status, $this->VIDEO_ID));
				    if (!$rs)
					    trigger_db_error($this->dbObj);
			}

		/**
		 * VideoUpload::deleteVideoTable()
		 *
		 * @return
		 **/
		public function deleteVideoTable($video_type='video_id')
			{
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['ans_video'].' WHERE'.
						' video_id='.$this->dbObj->Param('video_id');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->VIDEO_ID));
				    if (!$rs)
					    trigger_db_error($this->dbObj);

				if ($this->CONTENT_ID AND $this->VIDEO_FOR)
					{
						if($this->VIDEO_FOR == 'Question')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['questions'].
										' SET '.$video_type.' = 0'.
										' WHERE ques_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						if($this->VIDEO_FOR == 'Answer')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['answers'].
										' SET '.$video_type.' = 0'.
										' WHERE ans_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);

								$sql = 'UPDATE '.$this->CFG['db']['tbl']['questions'].
										' SET total_videos=total_videos-1'.
										' WHERE ques_id IN (SELECT ques_id FROM '.$this->CFG['db']['tbl']['answer'].' WHERE ans_id='.$this->dbObj->Param($this->CONTENT_ID).')';
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						if($this->VIDEO_FOR == 'Blog')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['blogs'].
										' SET '.$video_type.' = 0'.
										' WHERE blog_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						if($this->VIDEO_FOR == 'Blogcomment')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['blog_comment'].
										' SET '.$video_type.' = 0'.
										' WHERE comment_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						if($this->VIDEO_FOR == 'Forumresponse')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_response'].
										' SET '.$video_type.' = 0'.
										' WHERE response_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									   trigger_db_error($this->dbObj);
							}
						if($this->VIDEO_FOR == 'Debate')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['debates'].
										' SET '.$video_type.' = 0'.
										' WHERE debate_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						if($this->VIDEO_FOR == 'DebateResponse')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['debate_responses'].
										' SET '.$video_type.' = 0'.
										' WHERE response_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);
							}
					}
			}

		/**
		 * AudioActivate::activateAudioFile()
		 *
		 * @return
		 **/
		public function activateAudioFile()
			{
				$dir_audio = $this->_dir_path.$this->CFG['admin']['ans_audios']['audio_folder'];
				$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_audios']['temp_folder'];
				$tempurl =  $temp_dir.$this->AUDIO_NAME;

				$local_upload = true;
				if($this->getServerDetails('Ans_audio'))
					{
						$dir_audio = $this->_dir_path.$this->CFG['admin']['ans_audios']['audio_folder'];
						if($FtpObj = new FtpHandler($this->FTP_SERVER,$this->FTP_USERNAME,$this->FTP_PASSWORD))
							{
								if($this->FTP_FOLDER)
									$FtpObj->changeDirectory($this->FTP_FOLDER);

								$FtpObj->makeDirectory($dir_audio);
								if(is_file($tempurl.'.'.$this->AUDIO_EXT))
									{
										$FtpObj->moveTo($tempurl.'.'.$this->AUDIO_EXT, $dir_audio.$this->AUDIO_NAME.'.'.$this->AUDIO_EXT);
										unlink($tempurl.'.'.$this->AUDIO_EXT);
									}
								$FtpObj->ftpClose();
								$SERVER_URL = $this->FTP_SERVER_URL;
								$local_upload = false;
							}
					}
				if($local_upload)
					{
						$upload_dir_audio = $dir_audio;
						$this->chkAndCreateFolder($upload_dir_audio);
						$uploadUrlAudio = $upload_dir_audio.$this->AUDIO_NAME;

						if(is_file($tempurl.'.'.$this->AUDIO_EXT))
							{
								copy($tempurl.'.'.$this->AUDIO_EXT, $uploadUrlAudio.'.'.$this->AUDIO_EXT);
								unlink($tempurl.'.'.$this->AUDIO_EXT);
							}
						$SERVER_URL = $this->CFG['site']['url'];
					}
				if(isset($SERVER_URL))
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_audio'].' SET'.
								' audio_ext=\'flv\', audio_server_url='.$this->dbObj->Param('audio_server_url').', audio_status=\'Ok\' WHERE'.
								' audio_id='.$this->dbObj->Param('audio_id');

						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($SERVER_URL, $this->AUDIO_ID));
						    if (!$rs)
							    trigger_db_error($this->dbObj);

						if($this->AUDIO_FOR == 'Question')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['questions'].' SET'.
										' audio_id='.$this->dbObj->Param('audio_id').' WHERE'.
										' ques_id='.$this->dbObj->Param('ques_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						else if($this->AUDIO_FOR == 'Answer')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['answers'].' SET'.
										' audio_id='.$this->dbObj->Param('audio_id').' WHERE'.
										' ans_id='.$this->dbObj->Param('ans_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);

								if($this->dbObj->Affected_Rows())
									{
										$sql = 'SELECT ques_id FROM '.$this->CFG['db']['tbl']['answers'].' WHERE'.
												' ans_id='.$this->dbObj->Param('ans_id');

										$stmt = $this->dbObj->Prepare($sql);
										$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
										    if (!$rs)
											    trigger_db_error($this->dbObj);

										$row = $rs->FetchRow();

										$sql = 'UPDATE '.$this->CFG['db']['tbl']['questions'].' SET'.
												' total_audios=total_audios+1 WHERE'.
												' ques_id='.$this->dbObj->Param('ques_id');

										$stmt = $this->dbObj->Prepare($sql);
										$rs = $this->dbObj->Execute($stmt, array($row['ques_id']));
										    if (!$rs)
											    trigger_db_error($this->dbObj);
									}
							}
						else if($this->AUDIO_FOR == 'Forumresponse')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_response'].' SET'.
										' audio_id='.$this->dbObj->Param('audio_id').' WHERE'.
										' response_id='.$this->dbObj->Param('ques_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						else if($this->AUDIO_FOR == 'Blog')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['blogs'].' SET'.
										' audio_id='.$this->dbObj->Param('audio_id').' WHERE'.
										' blog_id='.$this->dbObj->Param('ques_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									   trigger_db_error($this->dbObj);
							}
						else if($this->AUDIO_FOR == 'Blogcomment')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['blog_comment'].' SET'.
										' audio_id='.$this->dbObj->Param('audio_id').' WHERE'.
										' comment_id='.$this->dbObj->Param('comment_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						elseif($this->AUDIO_FOR == 'Debate')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['debates'].' SET'.
										' audio_id='.$this->dbObj->Param('audio_id').' WHERE'.
										' debate_id='.$this->dbObj->Param('debate_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						else if($this->AUDIO_FOR == 'DebateResponse')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['debate_responses'].' SET'.
										' external_audio_id='.$this->dbObj->Param('audio_id').' WHERE'.
										' response_id='.$this->dbObj->Param('response_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);

								if($this->dbObj->Affected_Rows())
									{
										$sql = 'SELECT debate_id FROM '.$this->CFG['db']['tbl']['debate_responses'].' WHERE'.
												' response_id='.$this->dbObj->Param('response_id');

										$stmt = $this->dbObj->Prepare($sql);
										$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
										    if (!$rs)
											    trigger_db_error($this->dbObj);

										$row = $rs->FetchRow();

										$sql = 'UPDATE '.$this->CFG['db']['tbl']['debates'].' SET'.
												' total_audios=total_audios+1 WHERE'.
												' ques_id='.$this->dbObj->Param('debate_id');

										$stmt = $this->dbObj->Prepare($sql);
										$rs = $this->dbObj->Execute($stmt, array($row['debate_id']));
										    if (!$rs)
											    trigger_db_error($this->dbObj);
									}
							}

						return true;
					}
				return false;
			}

		/**
		 * AudioActivate::populateAudioDetails()
		 *
		 * @return
		 **/
		public function populateAudioDetails($audio_id, $status = 'No')
			{
				$sql = 'SELECT audio_id, audio_ext, content_id, audio_for FROM'.
						' '.$this->CFG['db']['tbl']['ans_audio'].' WHERE'.
						' audio_id='.$this->dbObj->Param('audio_id').' AND'.
						' audio_encoded_status='.$this->dbObj->Param('audio_encoded_status').' LIMIT 0,1';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($audio_id, $status));
				    if (!$rs)
					    trigger_db_error($this->dbObj);

				if($row = $rs->FetchRow())
					{
						$this->AUDIO_ID = $row['audio_id'];
						$this->AUDIO_NAME = getImageName($row['audio_id']);
						$this->AUDIO_EXT = $row['audio_ext'];
						$this->CONTENT_ID = $row['content_id'];
						$this->AUDIO_FOR = $row['audio_for'];
						return true;
					}
				return false;
			}

		/**
		 * AudioUpload::audioEncode()
		 *
		 * @param $audio_id
		 * @return
		 **/
		public function audioEncode($audio_id)
			{
				if($this->populateAudioDetails($audio_id))
					{
						$source = $this->_dir_path.$this->CFG['admin']['ans_audios']['temp_folder'].$this->AUDIO_NAME;
						$source_filename = $source.'.'.$this->AUDIO_EXT;
						$store_dir = $this->_dir_path.$this->CFG['admin']['ans_audios']['temp_folder'];

						if($this->CFG['debug']['store_log'])
							{
								$log_str = "\n Before Uploading \n".$source_filename."\n";
								$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
								$log_str .= "\n Audio Name: ".$this->AUDIO_NAME."\n";
								$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
								$this->writetoTempFile($log_str);
							}

						if(is_file($source_filename))
							{
								$result = exec("\"".$this->CFG['admin']['video']['flvtool2_path']."\" -UP ". $source_filename);
								if($this->CFG['debug']['store_log'])
									{
										$log_str = "\n Flvtool2 start \n Flvtool2 execution Result \n".@implode("\n", $result);
										$log_str .= "\n Execute Query : "."\"".$this->CFG['admin']['video']['flvtool2_path']."\" -UP ". $source_filename;
										$log_str .= "\n Flvtool2 execution Output \n".@implode("\n", $p)."\n Flvtool2 End \n";
										$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
										$log_str .= "\n Audio Name: ".$this->AUDIO_NAME."\n";
										$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
										$this->writetoTempFile($log_str);
									}

								$this->AUDIO_EXT = 'flv';
								$this->changeAudioEncodedStatus('Yes');
								$this->activateAudioFile();
								if($this->CFG['debug']['store_log'])
									{
										$log_str = "\n Activated audio successfully \n".$source_filename."\n";
										$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
										$log_str .= "\n Audio Name: ".$this->AUDIO_NAME."\n";
										$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
										$this->writetoTempFile($log_str);
									}
							}
						else
							{
								$this->audio_uploaded_success = false;
								if(is_file($source_filename))
									unlink($source_filename);
								$this->deleteAudioTable();
							}
					}
			}

		/**
		 * AudioUpload::changeEncodedStatus()
		 *
		 * @return
		 **/
		public function changeAudioEncodedStatus($status)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_audio'].' SET'.
						' audio_encoded_status='.$this->dbObj->Param('audio_encoded_status').
						' WHERE audio_id='.$this->dbObj->Param('audio_id');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($status, $this->AUDIO_ID));
				    if (!$rs)
					    trigger_db_error($this->dbObj);
			}

		/**
		 * AudioUpload::deleteAudioTable()
		 *
		 * @return
		 **/
		public function deleteAudioTable($audio_type = 'audio_id')
			{
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['ans_audio'].' WHERE'.
						' audio_id='.$this->dbObj->Param('audio_id');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID));
				    if (!$rs)
					    trigger_db_error($this->dbObj);
				if ($this->CONTENT_ID AND $this->AUDIO_FOR)
					{
						if($this->AUDIO_FOR == 'Question')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['questions'].
										' SET '.$audio_type.' = 0'.
										' WHERE ques_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						if($this->AUDIO_FOR == 'Answer')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['answers'].
										' SET '.$audio_type.' = 0'.
										' WHERE ans_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);

								$sql = 'UPDATE '.$this->CFG['db']['tbl']['questions'].
										' SET total_audios=total_audios-1'.
										' WHERE ques_id IN (SELECT ques_id FROM '.$this->CFG['db']['tbl']['answer'].' WHERE ans_id='.$this->dbObj->Param($this->CONTENT_ID).')';
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						if($this->AUDIO_FOR == 'Blog')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['blogs'].
										' SET '.$audio_type.' = 0'.
										' WHERE blog_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						if($this->AUDIO_FOR == 'Blogcomment')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['blog_comment'].
										' SET '.$audio_type.' = 0'.
										' WHERE comment_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									   trigger_db_error($this->dbObj);
							}
						if($this->AUDIO_FOR == 'Forumresponse')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_response'].
										' SET '.$audio_type.' = 0'.
										' WHERE response_id='.$this->dbObj->Param($this->CONTENT_ID);
								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
								if (!$rs)
									    trigger_db_error($this->dbObj);
							}
					}
			}

		/**
		 * VideoUpload::selectVideoIdFromTable()
		 *
		 * @return
		 **/
		public function selectVideoIdFromTable($video_id)
			{
				$sql = 'SELECT user_id, video_id, video_ext, flv_upload_type, video_flv_url FROM '.$this->CFG['db']['tbl']['ans_video'].
						' WHERE video_encoded_status=\'No\' AND video_id='.$this->dbObj->Param('video_id').
						' ORDER BY video_id LIMIT 0,1';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($video_id));
				    if (!$rs)
					    trigger_db_error($this->dbObj);

				if($row = $rs->FetchRow())
					{
						$this->VIDEO_ID = $row['video_id'];
						$this->VIDEO_NAME = getImageName($this->VIDEO_ID);
						$this->VIDEO_EXT = $row['video_ext'];
						$this->FLV_UPLOAD_TYPE = $row['flv_upload_type'];
						$this->VIDEO_FLV_URL = $row['video_flv_url'];

						$sql = 'SELECT name, display_name, email FROM '.$this->CFG['db']['tbl']['users'].
								' WHERE user_id='.$this->dbObj->Param('user_id').' LIMIT 0,1';
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($row['user_id']));
						    if (!$rs)
							    trigger_db_error($this->dbObj);

						if($row = $rs->FetchRow())
							{
								$this->VIDEO_USER_NAME = $row['display_name'];
								$this->VIDEO_USER_EMAIL = $row['email'];
							}
						return true;
					}
				return false;
			}

		public function getFileContents($file)
			{
				$buffer='';
				$file=html_entity_decode($file);
				if($handle = @fopen("$file", "r"))
					{
						while (!feof($handle))
					    	$buffer .= fgets($handle, 4096);
					}
				return $buffer;
			}

		/**
		 * VideoUpload::insertVideoTable()
		 *
		 * @param $fields_arr
		 * @param string $err_tip
		 * @return
		 **/
		public function insertVideoTable($fields_arr, $err_tip='')
			{
				$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['ans_video'].' SET ';
				foreach($fields_arr as $fieldname)
					{
						if($this->fields_arr[$fieldname]!='NOW()')
							$sql .= $fieldname.'=\''.addslashes($this->fields_arr[$fieldname]).'\', ';
						else
							$sql .= $fieldname.'='.$this->fields_arr[$fieldname].', ';
					}
				$sql = substr($sql, 0, strrpos($sql, ','));

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				    if (!$rs)
					    trigger_db_error($this->dbObj);

				$return = $this->dbObj->Insert_ID();
				return $return;
			}

		/**
		 * MusicUpload::insertAudioTable()
		 *
		 * @param $fields_arr
		 * @param string $err_tip
		 * @return
		 **/
		public function insertAudioTable($fields_arr, $err_tip='')
			{
				$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['ans_audio'].' SET ';
				foreach($fields_arr as $fieldname)
					{
						if($this->fields_arr[$fieldname]!='NOW()')
							$sql .= $fieldname.'=\''.addslashes($this->fields_arr[$fieldname]).'\', ';
						else
							$sql .= $fieldname.'='.$this->fields_arr[$fieldname].', ';
					}
				$sql = substr($sql, 0, strrpos($sql, ','));

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				    if (!$rs)
					    trigger_db_error($this->dbObj);

				$return = $this->dbObj->Insert_ID();
				return $return;
			}

		/**
		 * VideoUpload::changeVideoStatus()
		 *
		 * @param $video_id
		 * @return
		 **/
		public function changeVideoStatus($video_id)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video'].' SET video_encoded_status=\'No\''.
						' WHERE video_id='.$this->dbObj->Param('video_id');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($video_id));
				    if (!$rs)
					    trigger_db_error($this->dbObj);
			}

		/**
		 * VideoUpload::sendMailToUserForActivate()
		 *
		 * @return
		 **/
		public function sendMailToUserForActivate($type)
			{
				$activatation_subject = $this->LANG['video_activate_subject'];
				if ($type == 'Audio')
					$activatation_subject = $this->LANG['audio_activate_subject'];

				$activatation_content_for_question = $this->LANG['video_activate_content_question'];
				$activatation_content_for_answer = $this->LANG['video_activate_content_answer'];
				$activatation_content_for_blog = $this->LANG['video_activate_content_blog'];
				$activatation_content_for_blogcomment = $this->LANG['video_activate_content_blogcomment'];
				$activatation_content_for_forumresponse = $this->LANG['video_activate_content_forumresponse'];

				if ($type == 'Audio')
					{
						$activatation_content_for_question = $this->LANG['audio_activate_content_question'];
						$activatation_content_for_answer = $this->LANG['audio_activate_content_answer'];
						$activatation_content_for_blog = $this->LANG['audio_activate_content_blog'];
						$activatation_content_for_blogcomment = $this->LANG['audio_activate_content_blogcomment'];
						$activatation_content_for_forumresponse = $this->LANG['audio_activate_content_forumresponse'];
					}
				$video_for = 'Question';
				if ((isset($this->VIDEO_FOR) AND $this->VIDEO_FOR == 'Blog') OR (isset($this->AUDIO_FOR) AND $this->AUDIO_FOR == 'Blog'))
					$video_for = 'Blog';
				else if ((isset($this->VIDEO_FOR) AND $this->VIDEO_FOR == 'Blogcomment') OR (isset($this->AUDIO_FOR) AND $this->AUDIO_FOR == 'Blogcomment'))
					$video_for = 'Blogcomment';
				else if ((isset($this->VIDEO_FOR) AND $this->VIDEO_FOR == 'Forumresponse') OR (isset($this->AUDIO_FOR) AND $this->AUDIO_FOR == 'Forumresponse'))
					$video_for = 'Forumresponse';
				else if ((isset($this->VIDEO_FOR) AND $this->VIDEO_FOR == 'Answer') OR (isset($this->AUDIO_FOR) AND $this->AUDIO_FOR == 'Answer'))
					$video_for = 'Answer';

				if (isset($this->VIDEO_USER_NAME))
					$USER_NAME = $this->VIDEO_USER_NAME;
				if (isset($this->AUDIO_USER_NAME))
					$USER_NAME = $this->AUDIO_USER_NAME;

				if (isset($this->VIDEO_USER_EMAIL))
					$USER_EMAIL = $this->VIDEO_USER_EMAIL;
				if (isset($this->AUDIO_USER_EMAIL))
					$USER_EMAIL = $this->AUDIO_USER_EMAIL;

				$subject = str_replace('VAR_SITENAME', $this->CFG['site']['name'], $activatation_subject);

				$body = $activatation_content_for_question;
				if($video_for == 'Blog')
					$body = $activatation_content_for_blog;
				elseif($video_for == 'Blogcomment')
					$body = $activatation_content_for_blogcomment;
				elseif($video_for == 'Forumresponse')
					$body = $activatation_content_for_forumresponse;
				else if ($video_for == 'Answer')
					$body = $activatation_content_for_answer;

				$body = str_replace('VAR_LINK', '<a href=\''.$this->CFG['site']['url'].'\'>'.$this->CFG['site']['url'].'</a>', $body);
				$body = str_replace('VAR_RECEIVER_NAME', $USER_NAME, $body);
				$body = str_replace('VAR_SITENAME', $this->CFG['site']['name'], $body);

				if ($video_for == 'Question')
					{
						$sql = 'SELECT question FROM '.$this->CFG['db']['tbl']['questions'].
								' WHERE ques_id = '.$this->dbObj->Param($this->CONTENT_ID);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_QUESTION', '<strong>'.nl2br($row['question']).'</strong>', $body);

					}
				if ($video_for == 'Answer')
					{
						$sql = 'SELECT answer, ques_id FROM '.$this->CFG['db']['tbl']['answers'].
								' WHERE ans_id = '.$this->dbObj->Param($this->CONTENT_ID);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_ANSWER', nl2br($row['answer']), $body);

						$sql = 'SELECT question FROM '.$this->CFG['db']['tbl']['questions'].
								' WHERE ques_id = '.$this->dbObj->Param($row['ques_id']);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($row['ques_id']));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();

						$body = str_replace('VAR_QUESTION', '<strong>'.nl2br($row['question']).'</strong>', $body);
					}
				if ($video_for == 'Blog')
					{
						$sql = 'SELECT subject FROM '.$this->CFG['db']['tbl']['blogs'].
								' WHERE blog_id = '.$this->dbObj->Param($this->CONTENT_ID);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_BLOG', '<strong>'.nl2br($row['subject']).'</strong>', $body);

					}
				if ($video_for == 'Blogcomment')
					{
						$sql = 'SELECT comment, blog_id FROM '.$this->CFG['db']['tbl']['blog_comment'].
								' WHERE comment_id = '.$this->dbObj->Param($this->CONTENT_ID);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_BLOGCOMMENT', '<strong>'.nl2br($row['comment']).'</strong>', $body);

						$sql = 'SELECT subject FROM '.$this->CFG['db']['tbl']['blogs'].
								' WHERE blog_id = '.$this->dbObj->Param('blog_id');
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($row['blog_id']));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_BLOG', '<strong>'.nl2br($row['subject']).'</strong>', $body);

					}
				if ($video_for == 'Forumresponse')
					{
						$sql = 'SELECT forum_response, response_id, topic_id FROM '.$this->CFG['db']['tbl']['forum_response'].
								' WHERE response_id = '.$this->dbObj->Param($this->CONTENT_ID);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_FORUMRESPONSE', '<strong>'.nl2br($row['forum_response']).'</strong>', $body);

						$sql = 'SELECT forum_topic FROM '.$this->CFG['db']['tbl']['forum_topics'].
								' WHERE topic_id = '.$this->dbObj->Param('topic_id');
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($row['topic_id']));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_FORUMTOPIC', '<strong>'.nl2br($row['forum_topic']).'</strong>', $body);

					}

				$to_email = $USER_EMAIL;
				$sender_name = $this->CFG['site']['noreply_name'];
				$sender_email = $this->CFG['site']['noreply_email'];
				$this->buildEmailTemplate($subject, $body, false, true);
				$EasySwift = new EasySwift($this->getSwiftConnection());
				$EasySwift->flush();
				//$EasySwift->addCc($CC_ADDRESS);
				$EasySwift->addPart($this->getEmailContent(), "text/html");
				$from_address = $sender_name.'<'.$sender_email.'>';
				return $EasySwift->send($to_email, $from_address, $this->getEmailSubject());

			}

		/**
		 * VideoUpload::sendMailToUserForInvalidContent()
		 *
		 * @return
		 **/
		public function sendMailToUserForInvalidContent($type)
			{
				$invalid_upload_subject = $this->LANG['video_invalid_upload_subject'];
				if ($type == 'Audio')
					$invalid_upload_subject = $this->LANG['audio_invalid_upload_subject'];

				$invalid_upload_content_for_question = $this->LANG['video_invalid_upload_content_question'];
				$invalid_upload_content_for_answer = $this->LANG['video_invalid_upload_content_answer'];
				$invalid_upload_content_for_blog = $this->LANG['video_invalid_upload_content_blog'];
				$invalid_upload_content_for_blogcomment = $this->LANG['video_invalid_upload_content_blogcomment'];
				$invalid_upload_content_for_forumresponse = $this->LANG['video_invalid_upload_content_forumresponse'];
				if ($type == 'Audio')
					{
						$invalid_upload_content_for_question = $this->LANG['audio_invalid_upload_content_question'];
						$invalid_upload_content_for_answer = $this->LANG['audio_invalid_upload_content_answer'];
						$invalid_upload_content_for_blog = $this->LANG['audio_invalid_upload_content_blog'];
						$invalid_upload_content_for_blogcomment = $this->LANG['audio_invalid_upload_content_blogcomment'];
						$invalid_upload_content_for_forumresponse = $this->LANG['audio_invalid_upload_content_forumresponse'];
					}

				$video_for = 'Question';
				if ((isset($this->VIDEO_FOR) AND $this->VIDEO_FOR == 'Blog') OR (isset($this->AUDIO_FOR) AND $this->AUDIO_FOR == 'Blog'))
					$video_for = 'Blog';
				else if ((isset($this->VIDEO_FOR) AND $this->VIDEO_FOR == 'Blogcomment') OR (isset($this->AUDIO_FOR) AND $this->AUDIO_FOR == 'Blogcomment'))
					$video_for = 'Blogcomment';
				else if ((isset($this->VIDEO_FOR) AND $this->VIDEO_FOR == 'Forumresponse') OR (isset($this->AUDIO_FOR) AND $this->AUDIO_FOR == 'Forumresponse'))
					$video_for = 'Forumresponse';
				else if ((isset($this->VIDEO_FOR) AND $this->VIDEO_FOR == 'Answer') OR (isset($this->AUDIO_FOR) AND $this->AUDIO_FOR == 'Answer'))
					$video_for = 'Answer';

				if (isset($this->VIDEO_USER_NAME))
					$USER_NAME = $this->VIDEO_USER_NAME;
				if (isset($this->AUDIO_USER_NAME))
					$USER_NAME = $this->AUDIO_USER_NAME;

				if (isset($this->VIDEO_USER_EMAIL))
					$USER_EMAIL = $this->VIDEO_USER_EMAIL;
				if (isset($this->AUDIO_USER_EMAIL))
					$USER_EMAIL = $this->AUDIO_USER_EMAIL;

				$subject = str_replace('VAR_SITENAME', $this->CFG['site']['name'], $invalid_upload_subject);

				$body = $invalid_upload_content_for_question;
				if ($video_for == 'Blog')
					$body = $invalid_upload_content_for_blog;
				else if ($video_for == 'Blogcomment')
					$body = $invalid_upload_content_for_blogcomment;
				else if ($video_for == 'Forumresponse')
					$body = $invalid_upload_content_for_forumresponse;
				else if ($video_for == 'Answer')
					$body = $invalid_upload_content_for_answer;

				$body = str_replace('VAR_LINK', '<a href=\''.$this->CFG['site']['url'].'\'>'.$this->CFG['site']['url'].'</a>', $body);
				$body = str_replace('VAR_RECEIVER_NAME', $USER_NAME, $body);
				$body = str_replace('VAR_SITENAME', $this->CFG['site']['name'], $body);

				if ($video_for == 'Question')
					{
						$sql = 'SELECT question FROM '.$this->CFG['db']['tbl']['questions'].
								' WHERE ques_id = '.$this->dbObj->Param($this->CONTENT_ID);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_QUESTION', '<strong>'.nl2br($row['question']).'</strong>', $body);
					}
				if ($video_for == 'Answer')
					{
						$sql = 'SELECT answer, ques_id FROM '.$this->CFG['db']['tbl']['answers'].
								' WHERE ans_id = '.$this->dbObj->Param($this->CONTENT_ID);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_ANSWER', nl2br($row['answer']), $body);

						$sql = 'SELECT question FROM '.$this->CFG['db']['tbl']['questions'].
								' WHERE ques_id = '.$this->dbObj->Param($row['ques_id']);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($row['ques_id']));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();

						$body = str_replace('VAR_QUESTION', '<strong>'.nl2br($row['question']).'</strong>', $body);
					}
				if ($video_for == 'Blog')
					{
						$sql = 'SELECT subject FROM '.$this->CFG['db']['tbl']['blogs'].
								' WHERE blog_id = '.$this->dbObj->Param($this->CONTENT_ID);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
						if (!$rs)
							    trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_BLOG', '<strong>'.nl2br($row['subject']).'</strong>', $body);
					}
				if ($video_for == 'Blogcomment')
					{
						$sql = 'SELECT comment, blog_id FROM '.$this->CFG['db']['tbl']['blog_comment'].
								' WHERE comment_id = '.$this->dbObj->Param($this->CONTENT_ID);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
						if (!$rs)
							   trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_BLOGCOMMENT', '<strong>'.nl2br($row['comment']).'</strong>', $body);

						$sql = 'SELECT subject FROM '.$this->CFG['db']['tbl']['blogs'].
								' WHERE blog_id = '.$this->dbObj->Param('blog_id');
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($row['blog_id']));
						if (!$rs)
							  trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_BLOG', '<strong>'.nl2br($row['subject']).'</strong>', $body);
					}
				if ($video_for == 'Forumresponse')
					{
						$sql = 'SELECT forum_response, response_id, topic_id FROM '.$this->CFG['db']['tbl']['forum_response'].
								' WHERE response_id = '.$this->dbObj->Param($this->CONTENT_ID);
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
						if (!$rs)
							  trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_FORUMRESPONSE', '<strong>'.nl2br($row['forum_response']).'</strong>', $body);

						$sql = 'SELECT forum_topic FROM '.$this->CFG['db']['tbl']['forum_topics'].
								' WHERE topic_id = '.$this->dbObj->Param('topic_id');
						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($row['topic_id']));
						if (!$rs)
							 trigger_db_error($this->dbObj);
						$row = $rs->FetchRow();
						$body = str_replace('VAR_FORUMTOPIC', '<strong>'.nl2br($row['forum_topic']).'</strong>', $body);
					}

				$to_email = $USER_EMAIL;
				$sender_name = $this->CFG['site']['noreply_name'];
				$sender_email = $this->CFG['site']['noreply_email'];
				$this->buildEmailTemplate($subject, $body, false, true);
				$EasySwift = new EasySwift($this->getSwiftConnection());
				$EasySwift->flush();
				//$EasySwift->addCc($CC_ADDRESS);
				$EasySwift->addPart($this->getEmailContent(), "text/html");
				$from_address = $sender_name.'<'.$sender_email.'>';
				return $EasySwift->send($to_email, $from_address, $this->getEmailSubject());


			}

		public function chkIsFile()
			{
				if(isset($this->fields_arr['file_extern']))
					{
						$this->fields_arr['file_extern'] = strtolower($this->fields_arr['file_extern']);
						$extern = $this->fields_arr['file_extern'];
						$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];
						$temp_file = $temp_dir.$this->fields_arr['rid'].'.'.$extern;
						if(is_file($temp_file))
							{
								return true;
							}
					}
				return false;
			}

		public function chkIsAudioFile()
			{
				if(isset($this->fields_arr['audio_extern']))
					{
						$this->fields_arr['audio_extern'] = strtolower($this->fields_arr['audio_extern']);
						$extern = $this->fields_arr['audio_extern'];
						$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];
						$temp_file = $temp_dir.$this->fields_arr['rid'].'.'.$extern;

						if(is_file($temp_file))
							{
								return true;
							}
					}
				return false;
			}

		public function chkVideoUploadEnvironment()
			{
				if(!$this->CFG['admin']['ans_videos']['env_details_checking'])
					return true;

				$this->VIDEO_UPLOAD_ENV_ERR='';
				$video_encoder_arr=array('MENCODER'=>$this->CFG['admin']['video']['mencoder_path'],
										'FLVTOOL2'=>$this->CFG['admin']['video']['flvtool2_path'],
										'MPALYER'=>$this->CFG['admin']['video']['mplayer_path']
										);

				foreach($video_encoder_arr as $index=>$file)
					$this->VIDEO_UPLOAD_ENV_ERR.=$this->getFilePermisions($index,$file);
				return (!trim($this->VIDEO_UPLOAD_ENV_ERR));
			}

		public function getFilePermisions($file_type, $file)
			{
				$env_file=($this->CFG['admin']['ans_videos']['env_details_list'])?$file:$file_type;
				if(file_exists($file))
					{
						$err='';
						if((($perm=intval((substr(sprintf('%o', fileperms($file)), -3)))) < 755))
							{
								$err.= ' EXECUTE PERMISSION NOT PROVIDED FOR THE FILE: '.$env_file;
								$err.=($this->CFG['admin']['ans_videos']['env_details_list'])?'. IT IS HAVING PERMISSION: '.$perm:'';
								$err.='<br />';
							}
					}
					else
						$err= $env_file.' NOT EXISTS.<br />';
				return $err;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function addNewExternalVideo($type)
			{
				$extern = $this->fields_arr['file_extern'];
				$this->setFormField('video_ext',$extern);
				$this->setFormField('user_id', $this->CFG['user']['user_id']);
				$this->setFormField('date_added','NOW()');
				$this->setFormField('video_encoded_status', 'Partial');
				$this->setFormField('video_for', $type);
				$this->setFormField('content_id', $this->fields_arr['content_id']);
				$this->fields_arr['video_flv_url'] = '';
				$this->fields_arr['flv_upload_type'] = 'Normal';


				$video_id = $this->insertVideoTable(array('user_id', 'content_id', 'video_for','video_ext','date_added','video_encoded_status','flv_upload_type', 'video_flv_url'));
				//added code for logging
				if($this->CFG['debug']['store_log'])
					{
						$log_str = ' External Video  Record created : '.$video_id."\r\n";
						$this->writetoTempFile($log_str);
					}

				$video_name = getImageName($video_id);
				$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];
				$this->chkAndCreateFolder($temp_dir);

				$temp_file = $temp_dir.$video_name.'.'.$extern;
				$this->storeVideosTempServer($temp_file);

				$this->changeVideoStatus($video_id);
				//die('addNewExternalVideo_out');
				if($this->CFG['admin']['ans_videos']['video_auto_encode'] or strtolower($extern)=='flv')
					{
						//added code for logging
						//die('addNewExternalVideo');
						if($this->CFG['debug']['store_log'])
							{
								$log_str = ' Calling External Video Encode \r\n';
								$this->writetoTempFile($log_str);
							}
						//added code for logging
						$this->externalVideoEncode($video_id);
					}
			}

		/**
		 * VideoUpload::storeVideosTempServer()
		 *
		 * @param $uploadUrl
		 * @param $file_name
		 * @return
		 **/
		public function storeVideosTempServer($uploadUrl)
			{
				$extern = $this->fields_arr['file_extern'];
				$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];
				$temp_file = $temp_dir.$this->fields_arr['rid'].'.'.$extern;
				copy($temp_file, $uploadUrl);
				unlink($temp_file);

				//added code for logging
				if($this->CFG['debug']['store_log'])
					{
						$log_str = ' External Video stored in temp server : '.$uploadUrl."\r\n";
						$this->writetoTempFile($log_str);
					}
				//added code for logging
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function externalVideoEncode($video_id)
			{
				if($this->selectVideoIdFromTable($video_id))
					{

						$source_filename = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'].$this->VIDEO_NAME.'.'.$this->VIDEO_EXT;
						$store_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];

						if($this->CFG['debug']['store_log'])
							{

								$log_str = "\n Before Uploading \n".$source_filename."\n";
								$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
								$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
								$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
								$this->writetoTempFile($log_str);

							}


						if(is_file($source_filename))
							{
								$this->changeEncodedStatus('Partial');
								$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'].$this->VIDEO_NAME.'/';

								$this->chkAndCreateFolder($temp_dir);
								$temp_file = $temp_dir.'00000001.'.$this->CFG['video']['image']['extensions'];
								$final_name = $store_dir.$this->VIDEO_NAME.'T.'.$this->CFG['video']['image']['extensions'];

								if(strtolower($this->VIDEO_EXT)!='flv')
									{
										if($this->CFG['debug']['store_log'])
											{
												//------Writing to the log file
												$log_str = 'CALLING MENCODER FOR FLV CONVERSION:'."\r\n";
												$log_str .= $this->CFG['admin']['video']['mencoder_path']."\" ".$source_filename." -o ".$store_dir.$this->VIDEO_NAME.".flv -of lavf -ovc lavc -oac lavc -lavcopts vcodec=flv:vbitrate=".$this->CFG['admin']['video']['vbitrate'].":autoaspect:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:predia=2:dia=2:precmp=2:cmp=2:subcmp=2:preme=2:turbo:acodec=mp3:abitrate=56 -vf scale=320:240 -srate 22050 -af lavcresample=22050 -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames";
												$this->writetoTempFile($log_str);
												//------Writing to the log file

											}
										$result = exec("\"".$this->CFG['admin']['video']['mencoder_path']."\" ".$source_filename." -o ".$store_dir.$this->VIDEO_NAME.".flv -of lavf -ovc lavc -oac lavc -lavcopts vcodec=flv:vbitrate=".$this->CFG['admin']['video']['vbitrate'].":autoaspect:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:predia=2:dia=2:precmp=2:cmp=2:subcmp=2:preme=2:turbo:acodec=mp3:abitrate=56 -vf scale=320:240 -srate 22050 -af lavcresample=22050 -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames", $p);

										if(count($p))
											{
												foreach($p as $key=>$val)
													{
														if(isset($log_str))
													$log_str .= $key.': '.$val."\n\r";
													}
											}
										$result = exec("\"".$this->CFG['admin']['video']['flvtool2_path']."\" -UP ". $store_dir.$this->VIDEO_NAME.".flv");
										if($this->CFG['debug']['store_log'])
											{
												$log_str = "\n Flvtool2 start \n Flvtool2 execution Result \n".@implode("\n", $result);
												$log_str .= "\n Execute Query : "."\"".$this->CFG['admin']['video']['flvtool2_path']."\" -UP ". $store_dir.$this->VIDEO_NAME.".flv";
												$log_str .= "\n Flvtool2 execution Output \n".@implode("\n", $p)."\n Flvtool2 End \n";
												$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
												$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
												$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
												$this->writetoTempFile($log_str);
											}
									}
								else
									{
										$result = exec("\"".$this->CFG['admin']['video']['flvtool2_path']."\" -UP ". $source_filename);
										if($this->CFG['debug']['store_log'])
											{
												$log_str = "\n Flvtool2 start \n Flvtool2 execution Result \n".@implode("\n", $result);
												$log_str .= "\n Execute Query : "."\"".$this->CFG['admin']['video']['flvtool2_path']."\" -UP ". $source_filename;
												$log_str .= "\n Flvtool2 execution Output \n".@implode("\n", $p)."\n Flvtool2 End \n";
												$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
												$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
												$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
												$this->writetoTempFile($log_str);
												//die('stopped flvtool');
											}
									}


								$this->videoToFrame($source_filename, $temp_dir, 1, $this->CFG['admin']['ans_videos']['large_width'].':'.$this->CFG['admin']['ans_videos']['large_height']);

								if($this->CFG['debug']['store_log'])
									{
										$log_str = "\n videoToFrame created successfully \n".$source_filename."\n";
										$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
										$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
										$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
										$this->writetoTempFile($log_str);
									}

								if(is_file(sprintf($temp_file,1)))
									{
										copy($temp_file, $final_name);
										unlink($temp_file);
										$this->removeDirectory($temp_dir);
										//get Video

										$this->setFormField('playing_time', $this->getPlayingTime($source_filename));
										if($this->CFG['debug']['store_log'])
											{
												$log_str = "\n Playing time calculated successfully \n".$source_filename."\n";
												$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
												$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
												$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
												$log_str .= "\n Playing Time: ".$this->fields_arr['playing_time']."\n";
												$log_str .= "\n $temp_file ".(is_file($temp_file)?' is a file':'NOT a file')."\n";
												$this->writetoTempFile($log_str);
											}

										$temp_file = $store_dir.$this->VIDEO_NAME;

										//resiz the image
										$imageObj = new ImageHandler($temp_file.'T.'.$this->CFG['video']['image']['extensions']);
										$this->setIHObject($imageObj);

										$this->storeImagesTempServer($temp_file, $this->CFG['video']['image']['extensions']);
										if($this->CFG['debug']['store_log'])
											{
												$log_str = "\n Created images successfully\n".$source_filename."\n";
												$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
												$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
												$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
												$this->writetoTempFile($log_str);
											}

										$this->updateVideoTable($this->VIDEO_ID);

										$this->changeEncodedStatus('Yes');

										if($this->CFG['admin']['ans_videos']['video_auto_activate'])
											{
												if($this->populateVideoDetails($this->VIDEO_ID, 'Yes'))
													{
														if($this->activateVideoFile('external_video_id'))
															{
																$this->sendMailToUserForActivate('Video');
															}
													}
											}
									}
								else
									{
										$this->removeDirectory($temp_dir);
										$this->video_uploaded_success = false;
										if(is_file($source_filename))
											unlink($source_filename);
										$this->deleteVideoTable('external_video_id');
										$this->sendMailToUserForInvalidContent('Video');
									}
							}
					}
			}

		public function addNewAudioExternal($type)
			{
				if($this->fields_arr['audio_upload_type']=='Youtube')
					$extern = 'mp3';
				else
					$extern = $this->fields_arr['audio_extern'];

				$this->setFormField('user_id',$this->CFG['user']['user_id']);
				$this->setFormField('audio_ext',$extern);
				$this->setFormField('date_added','NOW()');
				$this->setFormField('audio_encoded_status','Partial');
				$this->setFormField('audio_for', $type);
				$this->setFormField('content_id', $this->fields_arr['content_id']);

				$this->fields_arr['audio_url'] = '';
				if($this->fields_arr['audio_upload_type']=='Youtube')
					{
						$this->fields_arr['audio_url'] = $this->audio_url;
					}

				$this->AUDIO_ID = $audio_id = $this->insertAudioTable(array('user_id', 'content_id', 'audio_for', 'audio_ext','date_added','audio_encoded_status','audio_upload_type', 'audio_url'));


				$audio_name = getImageName($audio_id);
				$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];
				$this->chkAndCreateFolder($temp_dir);

				if($this->fields_arr['audio_upload_type']=='Normal')
					{
						$temp_file = $temp_dir.$audio_name.'.'.$extern;
						$this->storeAudioTempServer($temp_file);
					}

				$this->changeAudioStatus($audio_id);

				if($this->CFG['admin']['ans_audios']['audio_auto_encode'])
					$this->externalAudioEncode($audio_id);
			}

		/**
		 * MusicUpload::storeMusicsTempServer()
		 *
		 * @param $uploadUrl
		 * @param $file_name
		 * @return
		 **/
		public function storeAudioTempServer($uploadUrl)
			{
				$extern = $this->fields_arr['audio_extern'];
				$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];
				$temp_file = $temp_dir.$this->fields_arr['rid'].'.'.$extern;
				copy($temp_file, $uploadUrl);
				unlink($temp_file);

				//added code for logging
				if($this->CFG['debug']['store_log'])
					{
						$log_str = ' External Audio stored in temp server : '.$uploadUrl."\r\n";
						$this->writetoTempFile($log_str);
					}
			}

		/**
		 * VideoUpload::changeVideoStatus()
		 *
		 * @param $video_id
		 * @return
		 **/
		public function changeAudioStatus($audio_id)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_audio'].' SET audio_encoded_status=\'No\''.
						' WHERE audio_id='.$this->dbObj->Param('audio_id');
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($audio_id));
				    if (!$rs)
					    trigger_db_error($this->dbObj);
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function selectAudioIdFromTable($audio_id)
			{
				$sql = 'SELECT user_id, audio_id, audio_ext, audio_url, content_id, audio_for,'.
						' audio_upload_type FROM '.$this->CFG['db']['tbl']['ans_audio'].
						' WHERE audio_encoded_status=\'No\' AND audio_id='.$this->dbObj->Param('audio_id').
						' ORDER BY audio_id LIMIT 0,1';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($audio_id));
				    if (!$rs)
					    trigger_db_error($this->dbObj);

				if($row = $rs->FetchRow())
					{
						$this->AUDIO_ID = $row['audio_id'];
						$this->AUDIO_NAME = getImageName($this->AUDIO_ID);
						$this->AUDIO_EXT = $row['audio_ext'];
						$this->CONTENT_ID = $row['content_id'];
						$this->AUDIO_FOR = $row['audio_for'];
						$this->AUDIO_URL = $row['audio_url'];
						$this->AUDIO_UPLOAD_TYPE = $row['audio_upload_type'];

						$sql = 'SELECT name, display_name, email FROM '.$this->CFG['db']['tbl']['users'].
								' WHERE user_id='.$this->dbObj->Param('user_id').' LIMIT 0,1';

						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array($row['user_id']));
						    if (!$rs)
							    trigger_db_error($this->dbObj);

						if($row = $rs->FetchRow())
							{
								$this->AUDIO_USER_NAME = $row['display_name'];
								$this->AUDIO_USER_EMAIL = $row['email'];
							}
						return true;
					}
				return false;
			}

		/**
		 * AudioUpload::audioEncode()
		 *
		 * @param $audio_id
		 * @return
		 **/
		public function externalAudioEncode($audio_id)
			{
				if($this->selectAudioIdFromTable($audio_id))
					{
						$source = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'].$this->AUDIO_NAME;
						$source_filename = $source.'.'.$this->AUDIO_EXT;
						$store_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];

						$AUDIO_EXT = $this->AUDIO_EXT;

						if(($this->AUDIO_UPLOAD_TYPE!='Normal') OR (is_file($source_filename)))
							{
								if(strtolower($this->AUDIO_EXT)!='mp3')
									{
										exec($this->CFG['admin']['video']['mplayer_path'].' -vo null -vc dummy -af resample=44100:0:1 -ao pcm:file='.$source.'.wav '.$source_filename);
										exec($this->CFG['admin']['video']['lame_path'].' --quiet -m s '.$source.'.wav -o '.$store_dir.$this->AUDIO_NAME.'.mp3');
										if(is_file($source))
											unlink($source);
										if(is_file($source_filename))
											unlink($source_filename);
										$this->AUDIO_EXT = 'mp3';
										$AUDIO_EXT = 'mp3';
									}
								$this->changeAudioEncodedStatus('Yes');
								$this->updatePlayingTime($store_dir.$this->AUDIO_NAME.'.mp3');

								if($this->CFG['admin']['ans_audios']['audio_auto_activate'])
									{
										if($this->populateAudioDetails($audio_id, 'Yes'))
											{
												$this->AUDIO_EXT = $AUDIO_EXT;
												if($this->activateExternalAudioFile())
													{
														$this->sendMailToUserForActivate('Audio');
													}
											}
									}
							}
						else
							{
								$this->audio_uploaded_success = false;
								if(is_file($source_filename))
									unlink($source_filename);
								$this->deleteAudioTable('external_audio_id');
								$this->sendMailToUserForInvalidContent('Audio');
							}
					}
			}

		/**
		 * activateExternalAudioFile()
		 *
		 * @return
		 **/
		public function activateExternalAudioFile()
			{
				$SERVER_URL = $this->CFG['site']['url'];
				if($this->AUDIO_UPLOAD_TYPE=='Normal' || $this->AUDIO_UPLOAD_TYPE=='Othersite')
					{
						$dir_audio = $this->_dir_path.$this->CFG['admin']['ans_audios']['audio_folder'];
						$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];
						$tempurl =  $temp_dir.$this->AUDIO_NAME;

						$local_upload = true;
						if($this->getServerDetails('Ans_audio'))
							{
								$dir_audio = $this->_dir_path.$this->CFG['admin']['ans_audios']['audio_folder'];
								if($FtpObj = new FtpHandler($this->FTP_SERVER,$this->FTP_USERNAME,$this->FTP_PASSWORD))
									{
										if($this->FTP_FOLDER)
											$FtpObj->changeDirectory($this->FTP_FOLDER);

										$FtpObj->makeDirectory($dir_audio);
										if(is_file($tempurl.'.'.$this->AUDIO_EXT))
											{
												$FtpObj->moveTo($tempurl.'.'.$this->AUDIO_EXT, $dir_audio.$this->AUDIO_NAME.'.'.$this->AUDIO_EXT);
												unlink($tempurl.'.'.$this->AUDIO_EXT);
											}
										$FtpObj->ftpClose();
										$SERVER_URL = $this->FTP_SERVER_URL;
										$local_upload = false;
									}
							}
						if($local_upload)
							{
								$upload_dir_audio = $dir_audio;
								$this->chkAndCreateFolder($upload_dir_audio);
								$uploadUrlAudio = $upload_dir_audio.$this->AUDIO_NAME;

								if(is_file($tempurl.'.'.$this->AUDIO_EXT))
									{
										copy($tempurl.'.'.$this->AUDIO_EXT, $uploadUrlAudio.'.'.$this->AUDIO_EXT);
										unlink($tempurl.'.'.$this->AUDIO_EXT);
									}
								$SERVER_URL = $this->CFG['site']['url'];
							}
					}

				if(isset($SERVER_URL))
					{
						if($this->AUDIO_UPLOAD_TYPE=='Normal')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_audio'].' SET'.
										' audio_ext=\''.$this->AUDIO_EXT.'\', audio_server_url='.$this->dbObj->Param('audio_server_url').', audio_status=\'Ok\' WHERE'.
										' audio_id='.$this->dbObj->Param('audio_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($SERVER_URL, $this->AUDIO_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}

						if($this->AUDIO_FOR == 'Question')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['questions'].' SET'.
										' external_audio_id='.$this->dbObj->Param('external_audio_id').' WHERE'.
										' ques_id='.$this->dbObj->Param('ques_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						else if($this->AUDIO_FOR == 'Answer')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['answers'].' SET'.
										' external_audio_id='.$this->dbObj->Param('external_audio_id').' WHERE'.
										' ans_id='.$this->dbObj->Param('ans_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);

								if($this->dbObj->Affected_Rows())
									{
										$sql = 'SELECT ques_id FROM '.$this->CFG['db']['tbl']['answers'].' WHERE'.
												' ans_id='.$this->dbObj->Param('ans_id');

										$stmt = $this->dbObj->Prepare($sql);
										$rs = $this->dbObj->Execute($stmt, array($this->CONTENT_ID));
										    if (!$rs)
											    trigger_db_error($this->dbObj);

										$row = $rs->FetchRow();

										$sql = 'UPDATE '.$this->CFG['db']['tbl']['questions'].' SET'.
												' total_audios=total_audios+1 WHERE'.
												' ques_id='.$this->dbObj->Param('ques_id');

										$stmt = $this->dbObj->Prepare($sql);
										$rs = $this->dbObj->Execute($stmt, array($row['ques_id']));
										    if (!$rs)
											    trigger_db_error($this->dbObj);
									}
							}
						else if($this->AUDIO_FOR == 'Forumresponse')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['forum_response'].' SET'.
										' external_audio_id='.$this->dbObj->Param('external_audio_id').' WHERE'.
										' response_id='.$this->dbObj->Param('ques_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						else if($this->AUDIO_FOR == 'Blog')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['blogs'].' SET'.
										' external_audio_id='.$this->dbObj->Param('external_audio_id').' WHERE'.
										' blog_id='.$this->dbObj->Param('ques_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}
						else if($this->AUDIO_FOR == 'Blogcomment')
							{
								$sql = 'UPDATE '.$this->CFG['db']['tbl']['blog_comment'].' SET'.
										' external_audio_id='.$this->dbObj->Param('external_audio_id').' WHERE'.
										' comment_id='.$this->dbObj->Param('ques_id');

								$stmt = $this->dbObj->Prepare($sql);
								$rs = $this->dbObj->Execute($stmt, array($this->AUDIO_ID, $this->CONTENT_ID));
								    if (!$rs)
									    trigger_db_error($this->dbObj);
							}

						return true;
					}
				return false;
			}

		public function updatePlayingTime($source_filename)
			{
				$source_filename=($this->AUDIO_UPLOAD_TYPE=='Normal' and is_file($source_filename))?$source_filename:'';
				$source_filename=(!$source_filename and $this->AUDIO_URL)?$this->AUDIO_URL:$source_filename;
				if(!$source_filename)
					return;

				$playing_time=$this->getPlayingTime($source_filename);
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_audio'].' SET '.
						' playing_time=\''.$playing_time.'\' WHERE '.
						' audio_id=\''.$this->AUDIO_ID.'\' ';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				    if (!$rs)
					    trigger_db_error($this->dbObj);
			}

		public function getVideoFlvPath($external_upload_type)
			{
				$GrapUrl = new GrapUrl();

				if( $external_upload_type == 'Metacafe' )
					{
						$url = $GrapUrl->getMetaCafeVideoURL($this->fields_arr['video_external_flv_url']);
					}
				else if( $external_upload_type == 'Youtube' )
					{
						$url = $GrapUrl->getYouTubeVideoURL($this->fields_arr['video_external_flv_url']);
					}
				else if( $external_upload_type == 'Dailymotion' )
					{
						$url = $GrapUrl->getDailymotionVideoURL($this->fields_arr['video_external_flv_url']);
					}
				else if( $external_upload_type == 'Google' )
					{
						$url = $GrapUrl->getGoogleVideoURL($this->fields_arr['video_external_flv_url']);
					}
				else if( $external_upload_type == 'Blip' )
					{
						$url = $GrapUrl->getBlipVideoURL($this->fields_arr['video_external_flv_url']);
					}
				else if( $external_upload_type == 'Myspace' )
					{
						$url = $GrapUrl->getMySpaceVideoUrl($this->fields_arr['video_external_flv_url']);
					}
				return $url;
			}

		public function fileWrite($file_path, $text)
			{
				$file = fopen($file_path, 'w');
				fwrite($file, $text);
				fclose($file);
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function addExternalVideo($type)
			{
				if ($this->fields_arr['video_external_flv_url'])
					{
						$this->setFormField('video_ext', 'flv');
						$this->setFormField('user_id', $this->CFG['user']['user_id']);
						$this->setFormField('date_added', 'NOW()');
						$this->setFormField('video_encoded_status', 'No');
						$this->setFormField('video_for', $type);
						$this->setFormField('content_id', $this->fields_arr['content_id']);
						$external_upload_type = $this->chkIsValidExternalUrl($this->fields_arr['video_external_flv_url']);
						$this->setFormField('flv_upload_type', $external_upload_type);
						$this->setFormField('external_url', $this->fields_arr['video_external_flv_url']);
						$this->fields_arr['video_flv_url'] = '';
						$this->fields_arr['playing_time'] = '';

						$video_id = $this->insertVideoTable(array('user_id', 'content_id', 'video_for', 'video_ext', 'playing_time', 'date_added', 'video_encoded_status', 'flv_upload_type', 'video_flv_url', 'external_url'));

						$video_name = getImageName($video_id);

						//store the external video in temp folder
						$videoPian = new Videopian();
						$videoDetails = $videoPian->get($this->fields_arr['video_external_flv_url']);
/*echo "<pre>";
print_r($videoDetails);
echo "</pre>";
echo "<br>--->";
echo "<br>-->".$videoDetails->url;
echo "<br>-->".$videoDetails->duration;
echo "<pre>";
print_r($videoDetails->thumbnails);
echo "</pre>";
echo "<br>-->".$videoDetails->thumbnails[0]->url;
die();*/
						if($external_upload_type == 'Youtube')
							{
								if($this->selectVideoIdFromTable($video_id))
									{
										$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'].$this->VIDEO_NAME.'/';
										$store_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];

										$this->chkAndCreateFolder($temp_dir);
										$temp_file = $temp_dir.'00000001.'.$this->CFG['video']['image']['extensions'];
										copy($videoDetails->thumbnails[3]->url, $temp_file);

										$final_name = $store_dir.$this->VIDEO_NAME.'T.'.$this->CFG['video']['image']['extensions'];
										if(is_file(sprintf($temp_file,1)))
											{
												copy($temp_file, $final_name);
												unlink($temp_file);
												$this->removeDirectory($temp_dir);
											}

										$temp_file = $store_dir.$this->VIDEO_NAME;

										//resiz the image
										$imageObj = new ImageHandler($temp_file.'T.'.$this->CFG['video']['image']['extensions']);
										$this->setIHObject($imageObj);

										$this->storeImagesTempServer($temp_file, $this->CFG['video']['image']['extensions']);
										if($this->CFG['debug']['store_log'])
											{
												$log_str = "\n Created images successfully\n".$source_filename."\n";
												$log_str .= "\n User Name: ".$this->CFG['user']['name']."\n";
												$log_str .= "\n Video Name: ".$this->VIDEO_NAME."\n";
												$log_str .= "\n Date: ".date("F j, Y, g:i a")."\n";
												$log_str .= "\n YOutube thumbnail path: ".$videoDetails->thumbnails[0]->url."\n";
												$log_str .= "\n Temp upload path: ".$temp_file."\n";
												$log_str .= "\n Temp upload path1: ".$temp_file1."\n";
												$this->writetoTempFile($log_str);
											}

										//$this->videoToFrame($this->fields_arr['video_external_flv_url'], $temp_dir, 1, $this->CFG['admin']['ans_videos']['large_width'].':'.$this->CFG['admin']['ans_videos']['large_height']);

										$duration = $videoDetails->duration;
										$min = floor($duration/60);
										$sec = round($duration-($min*60));
										$min = str_pad($min, 2, '0', STR_PAD_LEFT);
										$sec = str_pad($sec, 2, '0', STR_PAD_LEFT);
										$playing_time = $min.':'.$sec;
										$this->setFormField('playing_time', $playing_time);

										$this->updateVideoTable($video_id);

										$this->changeEncodedStatus('Yes');

										if($this->CFG['admin']['ans_videos']['video_auto_activate'])
											{
												if($this->populateVideoDetails($this->VIDEO_ID, 'Yes'))
													{
														if($this->activateVideoFile('external_video_id'))
															{
																$this->sendMailToUserForActivate('Video');
															}
													}
											}
									}
							}
						else
							{
								$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];
								$this->chkAndCreateFolder($temp_dir);
								$extern = 'flv';
								$temp_file = $temp_dir.$video_name.'.'.$extern;
								$this->changeVideoStatus($video_id);

								$url = $this->getVideoFlvPath($external_upload_type);
								$page_source = fileGetContentsManual($url);
								if($page_source)
									{
										$this->fileWrite($temp_file, $page_source);
									}
								else
									{
										return false;
									}
								$this->externalVideoEncode($video_id);
							}

					}
				elseif ($this->fields_arr['file_extern'] AND $this->chkIsFile())
				//elseif ($this->fields_arr['file_extern'])
					{
						#die('coming here<br />');
						$this->fields_arr['flv_upload_type'] = 'Normal';
						$this->addNewExternalVideo($type);
					}
				#die('something wrong<br />');
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function addExternalAudio($type)
			{
				if ($this->fields_arr['audio_external_url'])
					{
						$extern = 'mp3';
						$this->setFormField('user_id',$this->CFG['user']['user_id']);
						$this->setFormField('audio_ext',$extern);
						$this->setFormField('date_added','NOW()');
						$this->setFormField('audio_encoded_status','Partial');
						$this->setFormField('audio_for', $type);
						$this->setFormField('content_id', $this->fields_arr['content_id']);
						$this->setFormField('external_url', $this->fields_arr['audio_external_url']);
						$this->setFormField('audio_upload_type', 'Othersite');
						$this->fields_arr['audio_url'] = '';

						$this->AUDIO_ID = $audio_id = $this->insertAudioTable(array('user_id', 'content_id', 'audio_for', 'audio_ext','date_added','audio_encoded_status','audio_upload_type', 'audio_url', 'external_url'));

						$audio_name = getImageName($audio_id);
						$temp_dir = $this->_dir_path.$this->CFG['admin']['ans_videos']['temp_folder'];
						$this->chkAndCreateFolder($temp_dir);

						$temp_file = $temp_dir.$audio_name.'.'.$extern;
						$page_source = getContents($this->fields_arr['audio_external_url']);

						if($page_source)
							{
								$this->fileWrite($temp_file, $page_source);
							}
						else
							{
								return false;
							}

						$this->changeAudioStatus($audio_id);

						if($this->CFG['admin']['ans_audios']['audio_auto_encode'])
							$this->externalAudioEncode($audio_id);
					}
				elseif ($this->fields_arr['audio_extern'] AND $this->chkIsAudioFile())
					{
						$this->fields_arr['audio_upload_type'] = 'Normal';
						$this->addNewAudioExternal($type);
					}
			}

		/**
		 * VideoUploadLib::chkIsValidCaptureVideoFile()
		 *
		 * @return
		 */
		public function chkIsValidCaptureVideoFile()
			{
				$file_name = $this->CFG['admin']['video']['red5_flv_path'].$this->fields_arr['rid'].'.flv';

				if($this->fields_arr['video']==$this->fields_arr['rid'])
					{
						if(is_file($file_name))
							return true;

						$this->fields_arr['video'] = '';
						return false;
					}
				if(is_file($file_name))
					unlink($file_name);
			}

		/**
		 * VideoUploadLib::chkIsValidCaptureAudioFile()
		 *
		 * @return
		 */
		public function chkIsValidCaptureAudioFile()
			{
				$file_name = $this->CFG['admin']['video']['red5_flv_path'].$this->fields_arr['rid'].'AUD.flv';

				if($this->fields_arr['audio']==$this->fields_arr['rid'])
					{
						if(is_file($file_name))
							{return true;}
						$this->fields_arr['audio'] = '';
						return false;
					}
				if(is_file($file_name))
					unlink($file_name);
			}
	}
?>