PHP Accessing and loading video via samba - php

I got a pretty specific question. I'm accessing data via samba on distant server and I got no choice. To achieve that in php I use a php wrapper for smbclient from GitHub https://github.com/icewind1991/SMB. I got a piece of code that works to load the video and play it but the problem is that I want to be able to navigate through the timeline and for it to play as soon as there is data to be read.. I repeat that I can't access data any other way and I use that library for other parts of my website so I can't change it. Right now it reads all the data from the whole video before playing it and I can't use the slider to navigate through the timeline. There's my code sample with false authentication data for the samba access.
<?php
require('../../class/SMB-master/vendor/autoload.php');
$host = '0.0.0.0';
$user = 'user';
$workgroup = 'workgroup.lan';
$password = 'password';
$share = 'share/'.$_GET["filePath"].'';
$auth = new \Icewind\SMB\BasicAuth($user, $workgroup, $password);
$serverFactory = new \Icewind\SMB\ServerFactory();
$server = $serverFactory->createServer($host, $auth);
$share = $server->getShare($share);
//Reads PDF 4096 bytes by 4096 bytes until there is no more data to read if the format is pdf
if($_GET['openFile'] == 'PDF')
{
$charNumber = 20;
$result = bin2hex(random_bytes($charNumber));
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename=".$result.".pdf");
$fh = $share->read($_GET['name']);
while ($data = fread($fh, 4096))
echo $data;
fclose($fh);
}
else{
//Reads video 4096 bytes by 4096 bytes until there is no more data to read if the format is video mp4
header("Content-type:video/mp4");
header("Content-Disposition:inline;filename=".$_GET['name']."");
$fh = $share->read($_GET['name']);
while ($data = fread($fh, 4096)){
echo $data;
}
fclose($fh);
}

Related

Out of memory decrypting large files in PHP with phpseclib

I'm trying to write a program that decrypts AES files on the fly with phpseclib.
Files are large, so I get an out of memory error if I use file_get_contents($f) or fread(filesize($f)) to read the input file.
For some reason, a loop like this is creating corrupted output files. WHY!? =(
For example, an input file of size 296,155,408 bytes comes out with 18,805,826 bytes. NOTE: It works if the entire file can be read in one iteration of the loop.
define('MY_BUFFER_SIZE', 128 * 1024);
$sessionKey = '....';
$filenameIn = $argv[1];
$fileIn = fopen($filenameIn, 'rb');
$filenameOut = dirname($argv[1]) . DIRECTORY_SEPARATOR . basename($argv[1], '.tar.gz') . '-decrypted.tar.gz';
$fileOut = fopen($filenameOut, 'wb');
// Setup cipher for continuous buffering and copy between files.
$aesCipher = new Crypt_AES(CRYPT_AES_MODE_CBC);
$aesCipher->setKey($sessionKey);
$aesCipher->setIV("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
$aesCipher->enableContinuousBuffer();
while (!feof($fileIn)) {
$packet = fread($fileIn, MY_BUFFER_SIZE); // #TODO: Streaming not working.
fwrite($fileOut, $aesCipher->decrypt($packet));
}
fclose($fileIn);
fclose($fileOut);
Thanks to #neubert!
What was required was adding:
$aesCipher->disablePadding()
This works:
// Setup cipher for continuous buffering and copy between files.
$aesCipher = new Crypt_AES(CRYPT_AES_MODE_CBC);
$aesCipher->setKey($sessionKey);
$aesCipher->setIV("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
$aesCipher->enableContinuousBuffer();
$aesCipher->disablePadding();
while (!feof($fileIn)) {
fwrite($fileOut, $aesCipher->decrypt(fread($fileIn, MY_BUFFER_SIZE)));
}

How to get results using yelp api

Sorry, this might be pretty basic. I'm trying to use the Yelp API and am running a test search for McDonalds in Baltimore.
this is the code:
<?php
$AccountKey = "XXXX";
$restaurant = "McDonalds";
$city = "Baltimore";
$file = "test.txt";
$data = http_get("http://api.yelp.com/business_review_search?term=".$restaurant."&location=".$city."&ywsid=".$AccountKey);
file_put_contents($file, $data);
?>
I'm trying to store the results in test.txt which I can then parse but its not working. Any ideas?
Thanks in advance!
In your code you didn't open the text file.
// Open the file to get existing content
$data = file_get_contents($file);
$data. = http_get("http://api.yelp.com/business_review_search?term=".$restaurant."&location=".$city."&ywsid=".$AccountKey);
// Write the contents back to the file
file_put_contents($file, $data);
more details

How to write PrinceXML file to server using PHP

Trying to fwrite the pdf results from PrinceXML to the server (in a new pdf file) instead of exporting via headers to acrobat. The below code displays the results on the browser.
require_once("../library/Prince/prince.php");
$princeSettings = $this->getInvokeArg('bootstrap') >getOption('prince');
$prince = new Prince($princeSettings['path']);
$prince->setHTML(true);
$result = $prince->convert_string_to_passthru($this->htmlView);
$fp = fopen("./files/reports/report.pdf", "w");
fwrite($fp, $result);
fclose($fp);
Figured this out for anyone who want to know...
require_once("../library/Prince/prince.php");
$princeSettings =$this->getInvokeArg('bootstrap')->getOption('prince');
$prince = new Prince($princeSettings['path']); $prince->setHTML(true);
$pdfPath =realpath(APPLICATION_PATH . "/../public/files/reports/report.pdf");
$prince->convert_string_to_file($this->htmlView, $pdfPath);

Handle errors file_get_contents

I'm writing an application that uses a .php script to get tweets using the twitter search API.
See below code:
<?php
$hashtag = 'hashtag'; // We search Twitter for the hashtag
$show = 25; // And we want to get 25 tweets
// Local path
$cacheFile = '../../_data/tweets.json.cache'; // A cachefile will be placed in _data/
$json = file_get_contents("http://search.twitter.com/search.json?result_type=recent&rpp=$show&q=%23" . $hashtag. "%20-RT") or die("Could not get tweets");
$fp = fopen($cacheFile, 'w');
fwrite($fp, $json);
fclose($fp);
?>
My problem is that I want to make sure that this script runs without fail, or if it does fail at least doesn't keep looping.
The script is going to be run automatically every 1 minute.
Would anyone know a good way to handle errors here?
TL;DR: How do I handle errors on my code?
in simple, use '#' prefix for function. It suppresses errors from displaying. Read More Here
<?php
$hashtag = 'hashtag'; // We search Twitter for the hashtag
$show = 25; // And we want to get 25 tweets
$cacheFile = '../../_data/tweets.json.cache'; // A cachefile will be placed in _data/
$json = #file_get_contents("http://search.twitter.com/search.json?result_type=recent&rpp=$show&q=%23" . $hashtag . "%20-RT");
if (!empty($json)) {
$fp = fopen($cacheFile, 'w');
fwrite($fp, $json);
fclose($fp);
} else {
echo "Could not get tweets";
exit;
}
?>

php writing additional lines to encrypted files?

I'm trying to open an encrypted file that will store a list of information, then add a new ID with information, and save the file back as it was originally encrypted. I have xor/base64 functions that are working, but I am having trouble getting the file to retain old information.
here is what I am currently using:
$key = 'some key here';
$id = $_GET['id'];
$group = $_GET['group'];
$file = "groups.log";
$fp = fopen($file, "w+");
$fs = file_get_contents($file);
$filedec = xorstr(base64_decode($fs),$key);
$info = "$id: $group";
$filedec = $filedec . "$info\n";
$reencode = base64_encode(xorstr($filedec,$key));
fwrite($fp, $reencode);
fclose($fp);
function xorstr($str, $key) {
$outText = '';
for($i=0;$i<strlen($str);)
{
for($j=0;$j<strlen($key);$j++,$i++)
{
$outText .= $str[$i] ^ $key[$j];
}
}
return $outText;
}
?>
It should save an entire list of the ID's and their corresponding groups, but for some reason it's only showing the last input :(
I wouldn't call this encryption. "cereal box decoder ring", maybe. If you want encryption, then use the mcrypt functions. At best this is obfuscation.
The problem is that you're doing fopen() before doing file_get_contents. Using mode w+ truncates the file to 0-bytes as part of the fopen() call. So by the time file_get_contents comes up, you've deleted the original file.
$fs = file_get_contents(...);
$fh = fopen(..., 'w+');
in that order will fix the problem.

Categories