Help with creating a temporary link

I’m trying to modify a php cms script. The code below is for the page that shows all the video file thumbnail images selected by the logged-in User for downloading. It was modified so that instead of downloading those video files, upon selecting the corresponding thumbnail image, the files are played instead, in a modal video player, on that page (the modification is from approx line 261 thru 334). I’d to get help to generate temporary links for the video files, so that upon the User session ending, all the files disappear, and must be searched for again upon the next session. I look forward to any guidance, thanks.

<?php
$site = "profile_downloads";

include ( "../admin/function/db.php" );

if ( ! isset( $_SESSION["people_id"] ) ) {
	header( "location:login.php" );
	exit();
}

include ( "../inc/header.php" );
include ( "profile_top.php" );?>

<div class="btn-group" role="group" style="float:right">
  <button type="button" class="btn btn-primary" onClick="location.href='profile_downloads.php'"><i class="glyphicon glyphicon-th"></i> <?php echo pvs_word_lang( "grid" );?></button>
  <button type="button" class="btn btn-default" onClick="location.href='profile_downloads_table.php'"><i class="glyphicon glyphicon-list"></i> <?php echo pvs_word_lang( "Table" );?></button>
</div>

<h1><?php echo pvs_word_lang( "my downloads" );?></h1>

<?php
$sql = "select id_parent,link,tlimit,ulimit,publication_id,collection_id from " .
	PVS_DB_PREFIX . "downloads where user_id=" . ( int )$_SESSION["people_id"] .
	" and tlimit<ulimit and data>" . pvs_get_time( date( "H" ), date( "i" ), date( "s" ),
	date( "m" ), date( "d" ), date( "Y" ) ) . " order by data desc";
$rs->open( $sql );

if ( ! $rs->eof ) {
	while ( ! $rs->eof ) {
		if ((int)$rs->row["collection_id"] == 0) {
			$preview = "";
			$preview_size = "";

			$sql = "select server1,title from " . PVS_DB_PREFIX . "photos where id_parent=" . ( int )
				$rs->row["publication_id"];
			$ds->open( $sql );
			if ( ! $ds->eof ) {
				$preview = pvs_show_preview( $rs->row["publication_id"], "photo", 1, 1, $ds->
					row["server1"], $rs->row["publication_id"] );
				$translate_results = pvs_translate_publication( $rs->row["publication_id"], $ds->
					row["title"], "", "" );
				$preview_title = $translate_results["title"];
				$preview_class = 1;

				$image_width = 0;
				$image_height = 0;
				$image_filesize = 0;
				$flag_storage = false;

				if ( pvs_is_remote_storage() )
				{
					$sql = "select url,filename1,filename2,width,height,item_id,filesize from " .
						PVS_DB_PREFIX . "filestorage_files where id_parent=" . $rs->row["publication_id"];
					$ds->open( $sql );
					while ( ! $ds->eof )
					{
						if ( $ds->row["item_id"] != 0 )
						{
							$image_width = $ds->row["width"];
							$image_height = $ds->row["height"];
							$image_filesize = $ds->row["filesize"];
						}

						$flag_storage = true;
						$ds->movenext();
					}
				}

				$sql = "select url,price_id from " . PVS_DB_PREFIX . "items where id=" . $rs->
					row["id_parent"];
				$dr->open( $sql );
				if ( ! $dr->eof )
				{
					if ( ! $flag_storage )
					{
						if ( file_exists( $DOCUMENT_ROOT . pvs_server_url( $ds->row["server1"] ) . "/" .
							$rs->row["publication_id"] . "/" . $dr->row["url"] ) )
						{
							$size = @getimagesize( $DOCUMENT_ROOT . pvs_server_url( $ds->row["server1"] ) .
								"/" . $rs->row["publication_id"] . "/" . $dr->row["url"] );
							$image_width = $size[0];
							$image_height = $size[1];
							$image_filesize = filesize( $DOCUMENT_ROOT . pvs_server_url( $ds->row["server1"] ) .
								"/" . $rs->row["publication_id"] . "/" . $dr->row["url"] );
						}
					}

					$sql = "select size from " . PVS_DB_PREFIX . "sizes where id_parent=" . $dr->
						row["price_id"];
					$dn->open( $sql );
					if ( ! $dn->eof )
					{
						if ( $dn->row["size"] != 0 and $image_width != 0 and $image_height != 0 )
						{
							if ( $image_width > $image_height )
							{
								$image_height = round( $image_height * $dn->row["size"] / $image_width );
								$image_width = $dn->row["size"];
							} else
							{
								$image_width = round( $image_width * $dn->row["size"] / $image_height );
								$image_height = $dn->row["size"];
							}
							$image_filesize = 0;
						}
					}
				}

				$preview_size = "<br>" . $image_width . "x" . $image_height;
				if ( $image_filesize != 0 )
				{
					$preview_size .= " " . strval( pvs_price_format( $image_filesize / ( 1024 * 1024 ),
						3 ) ) . " Mb.";
				}
			}

			$sql = "select server1,title from " . PVS_DB_PREFIX . "videos where id_parent=" . ( int )
				$rs->row["publication_id"];
			$ds->open( $sql );
			if ( ! $ds->eof ) {
				$preview = pvs_show_preview( $rs->row["publication_id"], "video", 1, 1, $ds->
					row["server1"], $rs->row["publication_id"] );
				$translate_results = pvs_translate_publication( $rs->row["publication_id"], $ds->
					row["title"], "", "" );
				$preview_title = $translate_results["title"];
				$preview_class = 2;

				$flag_storage = false;
				$file_filesize = 0;

				if ( pvs_is_remote_storage() )
				{
					$sql = "select filename1,filename2,url,item_id,filesize from " . PVS_DB_PREFIX .
						"filestorage_files where id_parent=" . $rs->row["publication_id"] .
						" and item_id=" . $rs->row["id_parent"];
					$dr->open( $sql );
					if ( ! $dr->eof )
					{
						$file_filesize = $dr->row["filesize"];
						$flag_storage = true;
					}
				}

				if ( ! $flag_storage )
				{
					$sql = "select url,price_id from " . PVS_DB_PREFIX . "items where id=" . $rs->
						row["id_parent"];
					$dr->open( $sql );
					if ( ! $dr->eof )
					{
						if ( file_exists( $DOCUMENT_ROOT . pvs_server_url( $ds->row["server1"] ) . "/" .
							$rs->row["publication_id"] . "/" . $dr->row["url"] ) )
						{
							$file_filesize = filesize( $DOCUMENT_ROOT . pvs_server_url( $ds->row["server1"] ) .
								"/" . $rs->row["publication_id"] . "/" . $dr->row["url"] );
						}
					}
				}

				$preview_size .= "<br>" . strval( pvs_price_format( $file_filesize / ( 1024 *
					1024 ), 3 ) ) . " Mb.";
			}

			$sql = "select server1,title from " . PVS_DB_PREFIX . "audio where id_parent=" . ( int )
				$rs->row["publication_id"];
			$ds->open( $sql );
			if ( ! $ds->eof ) {
				$preview = pvs_show_preview( $rs->row["publication_id"], "audio", 1, 1, $ds->
					row["server1"], $rs->row["publication_id"] );
				$translate_results = pvs_translate_publication( $rs->row["publication_id"], $ds->
					row["title"], "", "" );
				$preview_title = $translate_results["title"];
				$preview_class = 3;

				$flag_storage = false;
				$file_filesize = 0;

				if ( pvs_is_remote_storage() )
				{
					$sql = "select filename1,filename2,url,item_id,filesize from " . PVS_DB_PREFIX .
						"filestorage_files where id_parent=" . $rs->row["publication_id"] .
						" and item_id=" . $rs->row["id_parent"];
					$dr->open( $sql );
					if ( ! $dr->eof )
					{
						$file_filesize = $dr->row["filesize"];
						$flag_storage = true;
					}
				}

				if ( ! $flag_storage )
				{
					$sql = "select url,price_id from " . PVS_DB_PREFIX . "items where id=" . $rs->
						row["id_parent"];
					$dr->open( $sql );
					if ( ! $dr->eof )
					{
						if ( file_exists( $DOCUMENT_ROOT . pvs_server_url( $ds->row["server1"] ) . "/" .
							$rs->row["publication_id"] . "/" . $dr->row["url"] ) )
						{
							$file_filesize = filesize( $DOCUMENT_ROOT . pvs_server_url( $ds->row["server1"] ) .
								"/" . $rs->row["publication_id"] . "/" . $dr->row["url"] );
						}
					}
				}

				$preview_size .= "<br>" . strval( pvs_price_format( $file_filesize / ( 1024 *
					1024 ), 3 ) ) . " Mb.";
			}

			$sql = "select server1,title from " . PVS_DB_PREFIX . "vector where id_parent=" . ( int )
				$rs->row["publication_id"];
			$ds->open( $sql );
			if ( ! $ds->eof ) {
				$preview = pvs_show_preview( $rs->row["publication_id"], "vector", 1, 1, $ds->
					row["server1"], $rs->row["publication_id"] );
				$translate_results = pvs_translate_publication( $rs->row["publication_id"], $ds->
					row["title"], "", "" );
				$preview_title = $translate_results["title"];
				$preview_class = 4;

				$flag_storage = false;
				$file_filesize = 0;

				if ( pvs_is_remote_storage() )
				{
					$sql = "select filename1,filename2,url,item_id,filesize from " . PVS_DB_PREFIX .
						"filestorage_files where id_parent=" . $rs->row["publication_id"] .
						" and item_id=" . $rs->row["id_parent"];
					$dr->open( $sql );
					if ( ! $dr->eof )
					{
						$file_filesize = $dr->row["filesize"];
						$flag_storage = true;
					}
				}

				if ( ! $flag_storage )
				{
					$sql = "select url,price_id from " . PVS_DB_PREFIX . "items where id=" . $rs->
						row["id_parent"];
					$dr->open( $sql );
					if ( ! $dr->eof )
					{
						if ( file_exists( $DOCUMENT_ROOT . pvs_server_url( $ds->row["server1"] ) . "/" .
							$rs->row["publication_id"] . "/" . $dr->row["url"] ) )
						{
							$file_filesize = filesize( $DOCUMENT_ROOT . pvs_server_url( $ds->row["server1"] ) .
								"/" . $rs->row["publication_id"] . "/" . $dr->row["url"] );
						}
					}
				}

				$preview_size .= "<br>" . strval( pvs_price_format( $file_filesize / ( 1024 *
					1024 ), 3 ) ) . " Mb.";
			}

			$item_name = "";
			$sql = "select name from " . PVS_DB_PREFIX . "items where id=" . ( int )$rs->
				row["id_parent"];
			$ds->open( $sql );
			
			$sql2 = "select o.id,oc.item,i.url,i.id_parent,(select url from `filestorage` where activ=1) as folder from `downloads` d left join orders o on d.order_id=o.id left join orders_content oc on oc.id_parent=o.id left join items i on oc.item=i.id where d.link='".$rs->row["link"]."'";
			$dd->open( $sql2 );
			// echo '<pre>';
			// print_r($dr->row);
			$fileType = strtolower(pathinfo($dd->row['url'])['extension']);
			$videoUrl =  $dd->row['folder'].'/'.$dd->row['id_parent'].'/'.$dd->row['url'];
			// $videoUrl =  '/content2/8257/thumb.mp4';
			// echo '</pre>';
			
			if ( ! $ds->eof ) {
				$item_name = "<br>[" . $ds->row["name"] . "]";
			}
			$linkL = site_root.'/members/download.php?f='.$rs->row["link"];
			if ( $preview != "" ) {
	?>
				<div class="item_list video-<?php echo $fileType; ?>">
					<div  class="item_list_img">
						<div  class="item_list_img2">
					<a href="<?php echo ($fileType=='mp4' ? 'javascript:void(0);' : $linkL); ?>" data-toggle="modal" data-target="#myModal-<?php echo $rs->row["link"]; ?>" id="<?php echo ($fileType=='mp4' ? $videoUrl : ''); ?>"><img src="<?php echo $preview; ?>"></a>
						</div>
					</div>
					<div  class="item_list_text<?php echo $preview_class; ?>">
						<div><a href="<?php echo ($fileType=='mp4' ? 'javascript:void(0);' : $linkL); ?>" data-toggle="modal" data-target="#myModal-<?php echo $fileType; ?>" id="<?php echo ($fileType=='mp4' ? $videoUrl : ''); ?>"><?php echo $preview_title; ?></a><small><?php echo $item_name; ?><?php echo $preview_size; ?></small></div>
						<div class="idownloaded"><?php echo pvs_word_lang( "downloads" );?>: <?php echo $rs->row["tlimit"]; ?>(<?php echo $rs->row["ulimit"]; ?>)</div>
					</div>
				</div>
				<?php
			}
			$title = $preview_title;
		} else {
			$sql = "select id, title, price, description from " . PVS_DB_PREFIX . "collections where active = 1 and id = " . (int)$rs->row["collection_id"];
			$dd->open( $sql );
			if ( ! $dd->eof ) {
				$collection_result = pvs_show_collection_preview($dd->row["id"]);
				$title = $dd->row["title"];
				$fileType= '';
				$linkL = site_root.'/members/download.php?f='.$rs->row["link"];
			?>
				<div class="item_list video-<?php echo $fileType; ?>">
					<div  class="item_list_img">
						<div  class="item_list_img2">
						<a href="<?php echo ($fileType=='mp4' ? 'javascript:void(0);' : $linkL); ?>" data-toggle="modal" data-target="#myModal-<?php echo $fileType; ?>" id="<?php echo ($fileType=='mp4' ? $videoUrl : ''); ?>"><img src="<?php echo $collection_result["photo"]; ?>" style="max-width:<?php echo $pvs_global_settings["thumb_width"]; ?>px;max-height:<?php echo $pvs_global_settings["thumb_width"]; ?>px"></a>
						</div>
					</div>
					<div  class="item_list_text">
						<div><a href="<?php echo ($fileType=='mp4' ? 'javascript:void(0);' : $linkL); ?>" data-toggle="modal" data-target="#myModal-<?php echo $fileType; ?>" id="<?php echo ($fileType=='mp4' ? $videoUrl : ''); ?>"><?php echo pvs_word_lang("Collection"); ?></a><br><small><?php echo $dd->row["title"]; ?><br><?php echo pvs_word_lang("items") . ': ' . pvs_count_files_in_collection($dd->row["id"]); ?></small></div>
						<div class="idownloaded"><?php echo pvs_word_lang( "downloads" );?>: <?php echo $rs->row["tlimit"]; ?>(<?php echo $rs->row["ulimit"]; ?>)</div>
					</div>
				</div>
			<?php
			}
		}
		echo '<div class="modal fade" id="myModal-'.$rs->row["link"].'" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
				  <div class="modal-dialog" role="document">
					<div class="modal-content">
					  <div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						<h4 class="modal-title" id="myModalLabel">'.$title.'</h4>
					  </div>
					  <div class="modal-body">
						<video controls id="video1" style="width: 100%; height: auto; margin:0 auto; frameborder:0;">
						  <source src="'.($fileType=='mp4' ? $videoUrl : '').'" type="video/mp4">
						  <source src="'.($fileType=='mp4' ? $videoUrl : '').'" type="video/ogg">
						  Your browser doesn\'t support HTML5 video tag.
						</video>
					  </div>
					  <div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
					  </div>
					</div>
				  </div>
				</div>';
		$rs->movenext();
	}
} else
{
	echo ( pvs_word_lang( "not found" ) );
}
?>
<script>
$('#myModal-mp4').on('shown.bs.modal', function (event) {
	var triggerElement = event.relatedTarget;
	// var video = (triggerElement.id);
  // $('#video1 source').attr('src',video);
  $('#video1')[0].play();
})
$('#myModal-mp4').on('hidden.bs.modal', function () {
  $('#video1')[0].pause();
})
</script>
<?php
include ( "profile_bottom.php" );
include ( "../inc/footer.php" );?>

Stop mixing up business logic, database queries and HTML stuff. Seperate and comment them properly so that somebody is able to understand the code by reading it.

Are you talking about $videoURL video link ? Provided code is completely mess, please post only relevant code here. It would help to understand your requirement.

For temporary link, i think you can use this logic:

if( $videoURL != NULL )
{
    $oldURL = $videoURL;
    $newURL = md5(time()).".txt";

    if( rename($oldURL, $newURL) ) { // rename link to just once
         $newURL; // access video link
    } else {
        echo 'this link is not exist';
    }
}

Thanks for your replies.
This is a web script I’m using, I did not write it.
My requirement is I’d to get help to generate temporary links for the video files

For temporary link, i think you can use this logic:…etc. Can you tell me where I would place that code?
Thanks again

Why? Do you like to annoy your users?

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.