XSLTFORMS: refresh model from URL? - php

I have some dynamic XML - generated in a PHP script.
The script takes a single param 'p' - for 'pagenum'.
The idea is to show paginated data - allowing the user to select next/previous page of data.
What approach do I take to updating the data with new pages - is it possible that my screen will update with having to reload the page over HTTP?
Here's a snippet of my main page - basically I'm reloading the whole page with a new GET param. (p=1, p=2 etc). (As can be seen, my main page also happens to be PHP - but I'm really doing a whole lot except grabbing the param).
<?php
header('Content-Type: text/xml; charset=utf-8');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$p=1;
if (isset($_GET['p'])) {
try { $p=(int)$_GET['p']; } catch(Exception $ex) { ; }
}
if (isset($_GET['debug'])) { $debug="yes"; } else { $debug="no"; }
?>
[...]
<xf:model>
<xf:instance src="data.php?p=<?=$p?>"/>
</xf:model>
</head>
<body>
<xf:repeat ref="videos/video">
<details>
<summary>
<span class="title"><xf:output value="title"/></span>
</summary>
<p>
<xf:output value="description" mediatype="text/html"/>
</p>
</details>
</xf:repeat>
EDIT: adding dummy datasource for reference.
<?php
header('Content-Type: text/xml; charset=utf-8');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
if (isset($_GET['p'])) { $p=(int)$_GET['p']; } else { $p=1; }
echo "<data xmlns=''>\n";
echo "<videos>\n";
switch ($p) {
case 1:
echo "<video><title>Macbeth</title></video><video>Malformed</video>";
break;
case 2:
echo "<video><title>Hamlet</title></video><video>SPACE 2003</video>";
break;
case 3:
echo "<video><title>Romeo And Juliet</title></video><video>Back to the Feature</video>";
default:
echo "<video/>";
}
echo "</videos>\n";
echo "</data>\n";
?>

I got something working - not sure this is the right approach here or not.
Posting in case useful for others - and if anybody could advise improvements.
I ditched the idea of using URL params on the view itself - instead, altered the model to include 'pagenum' in a 'metadata' section in the instance itself.
XForm Code:
<?xml-stylesheet href="xsltforms/xsltforms.xsl" type="text/xsl"?>
<?xsltforms-options debug="no"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xf="http://www.w3.org/2002/xforms"
>
<head>
<model xmlns="http://www.w3.org/2002/xforms">
<instance src="data.php" id="default"/>
<submission id="nextpage" method="get" replace="instance" instance="default" serialization="none">
<resource value="concat('data.php?p=', metadata/pagenum + 1)"/>
<message ev:event="xforms-submit-error">Cannot load!</message>
</submission>
<submission id="prevpage" method="get" replace="instance" instance="default" serialization="none">
<resource value="concat('data.php?p=', metadata/pagenum - 1)"/>
<message ev:event="xforms-submit-error">Cannot load!</message>
</submission>
</model>
</head>
<body>
<p> Page : <xf:output ref="metadata/pagenum"/> </p>
<xf:submit submission="prevpage"><xf:label>prev</xf:label></xf:submit>
<xf:submit submission="nextpage"><xf:label>next</xf:label></xf:submit>
<xf:repeat ref="videos/video">
<details>
<summary>
<xf:output value="title"/>
</summary>
<p>
<xf:output value="description" mediatype="text/html"/>
</p>
</details>
</xf:repeat>
</body>
</html>
Data-source:
<?php
header('Content-Type: text/xml; charset=utf-8');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
if (isset($_GET['p'])) { $p=(int)$_GET['p']; } else { $p=0; }
function as_xml($title,$description) {
return sprintf("<video><title>%s</title><description>%s</description></video>",$title,$description);
}
echo "<data xmlns=''>\n";
echo "<metadata><pagenum>".$p."</pagenum></metadata>\n";
echo "<videos>\n";
switch ($p) {
case 0:
echo as_xml('Macbeth','The Scottish Play') . "\n";
echo as_xml('Malformed','<b>Sci Fi Thriller</b>') . "\n";
break;
case 1:
echo as_xml('Hamlet','Something is rotten in the state of Denmmark') . "\n";
echo as_xml('SPACE 2003','XML <i>in space</i>') . "\n";
break;
case 2:
echo as_xml('Romeo and Juliet','Early Rom Com') . "\n" ;
echo as_xml('Back to the Feature','Copyright avoidance.') . "\n";
break;
default:
echo "<video/>";
}
echo "</videos>\n";
echo "</data>\n";
?>
Screenshot:

Related

header("Content-Disposition: attachment) not working

I have tried to export the mysql data result to excel. After click the Export button, the form will send parameter's value into doexport.php.
The result was appeared in console.
<?php
session_start(); //Start the session
include('install_lang_japanese.php');
//connect to database
$dbc=mysqli_connect(_SRV,_ACCID,_PWD,"QPL");
if(!$dbc){
die('Connect Error: ' . mysqli_connect_error());
}
if(isset($_POST['action'])){
switch($_POST['action']){
case 'senddatacar':
$start = mysqli_real_escape_string($dbc,$_POST['startdate']);
$end = mysqli_real_escape_string($dbc,$_POST['enddate']);
$sqlex = "SELECT * FROM table";
$result =mysqli_query($dbc,$sqlex) or die(_ERROR30.":".mysqli_error($dbc));
$filename="cardata".date('ymd').".xls";
header("Content-type: application/vnd.ms-excel; name='excel'");
header(sprintf("Content-Disposition: attachment; filename=$filename"));
header("Pragma: no-cache");
header("Expires: 0");
//Then echo table
break;
}
mysqli_close($dbc);
}
My problem is : i don't see any file has downloaded as excel
Response Header at console :
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Disposition:attachment; filename=cardata170929.xls
Content-Length:2988
Content-Type:application/vnd.ms-excel; name='excel'
Date:Fri, 29 Sep 2017 05:02:19 GMT
Expires:0
Pragma:no-cache
Server:Apache/2.2.22 (Ubuntu)

PHP output_buffering works only with error_reporting(E_ALL) issue

I have a PHP script, this is a part of:
if($signature == $params_signature) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
ob_start();
echo 'OK'; // send the OK response
header('Connection: close');
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header("Content-Encoding: none");
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();
echo 'test';
// some further procesiing
exit;
}
So the issue is: ob_start() working properly only in there is error_reporting settings:
error_reporting(E_ALL);
ini_set('display_errors', 1);
In this case output is "OK", but if I remove error_reporting part output is "OKtest".
The problem is, that I can not leave error reporting on production site, and I can not figure out the reason why this is happenning.
Maybe some more information is needded?!
UPDATE: Expected behavior: sctipt sends responce with status 200 and body - "OK", closes connection, and proceedes doing some internal stuff.
The below code works without error_reporting:
<?php
ob_start();
echo 'OK'; // send the OK response
header('Connection: close');
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header("Content-Encoding: none");
header('Content-Length: '.ob_get_length());
echo 'test';
// some further procesiing
exit;
?>
I have removed this before your last echo,
ob_end_flush();
ob_flush();
flush();

PHP - Generate file on the fly for android devices

I have read some answers here but i cannot seem to make my php script to work.
I generate a text or csv file (it depends what the user has chosen) from a form that it is submitted back to the same page.
The script works fine on Chrome, IE, Mozilla on my desktop but when i try on the stock browser on Android i get an attachment.html file that has the source of my script.
If i try with NEXT browser, i get a file with the proper filename but again inside is the source of my script.
I have read the following link but i cannot make it work
http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
My http headers are the following
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($FileName) . "\"");
header("Content-Length: " . filesize($FileName));
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Connection: close");
print $Content;
exit();
-----Update 1----
I wrote the following code to test things out
<?php
$get = $_GET['get'];
if ($get == 1) {
$content = 'This is a line of text!';
$filename = 'superfile';
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($filename) . ".TXT\"");
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Connection: close");
echo $content;
exit;
}
echo '<html>
<head>
<title>Test</title>
</head>
<body>
<form method="get" action="index.php"
<label>Get</label>
<input type="text" name="get"/>
<input type="submit" name="ok" value="Send"/>
</form>
</body>
</html>';
?>
When i send the form with get method everything works, when i use post i get the html source that you see, same as the problem that i have with my original script.
-----Update 2-----
Okkkkk, now some more info, i think it is related to an Android bug, please check the following link
http://roscopeco.com/2013/04/android-and-the-form-post-download-problem/
Waiting for suggestions to work around this issue, i need method to me post because i pass too many varialbes and i don't want to have on big ugly url on the address bar.
Well, i didn't want to experiment too much so i get the user-agent and i change my post form to a get. Not a great fix but the job is done.

export and download as csv in php not working in chrome and IE but working in firefox

I have a simple web page which pulls some data from DB and displays it in a html table and stores the data in some session variables.
I have a button "Export to csv" which calls a page which exports the results to a csv file.
The code for ExportToCsv file is:
<?php
session_start();
include "conn/sqlserverConn.php";
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
$data = $_SESSION['data'];
$rows = $_SESSION['row'];
$header = $_SESSION['header'];
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
$file = fopen("php://output", 'w');
fputcsv($file,$header);
for($i=0; $i<$rows; ++$i)
{
$valuesArray=array();
foreach($data[$i] as $name => $value)
{
$valuesArray[]=$value;
}
fputcsv($file,$valuesArray);
}
fclose($file);
die();
?>
My code is working flawlessly in firefox but it is not working in chrome or IE. On chrome and IE it is showing error 404 (Object not found!). Please tell me what is the problem ?
As outlined in the blog article at: http://www.exchangecore.com/blog/php-output-array-csv-headers/, I tested the following and it worked in IE8-11, and in chrome version 34. I presume it will work in other browsers fine as well.
Headers to use:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=' . $fileName);

embedding flv and swf located outside webroot

I have a script that uploads outside of the webroot. Through the website I then link users to images documents etc.
So for an image the link would be:
media.php?file=nameoffile.jpg&user=userid&folder=images
This is then used to display the image:
<img src="media.php?file=nameoffile.jpg&user=userid&folder=images" width="100" border="0">
This works fine for images and providing a link to download a document.
The problem I face is embedding, I use ffmpeg to convert all allowed videos types to flv (these videos are tested and working great), but when I try to embed the flv video it never works (it works with the direct link of the file just not through media.php). If possible I would also like to embed .swf.
I am using jwplayer to embed (works with the direct link of the file just not through media.php)
<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->
<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="328" height="200">
<param name="movie" value="player.swf" />
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="flashvars" value="media.php?file=nameoffile.flv&user=userid&folder=videos" />
<embed
type="application/x-shockwave-flash"
id="player2"
name="player2"
src="player.swf"
width="328"
height="200"
allowscriptaccess="always"
allowfullscreen="true"
flashvars="file=media.php?file=nameoffile.flv&user=userid&folder=videos"
/>
</object>
<script type="text/javascript" src="jwplayer.js"></script>
<!-- END OF THE PLAYER EMBEDDING -->
Here is media.php:
$path_parts = pathinfo($_SERVER['REQUEST_URI']);
$file = basename(urldecode($_GET['file']));
$user = basename(urldecode($_GET['user']));
$folder = basename(urldecode($_GET['folder']));
$ext = pathinfo($file, PATHINFO_EXTENSION);
$fileDir = 'pathoutsidewebroot';
$filePath = $fileDir . $file;
switch(
$ext) {
case "flv": $ctype="video/x-flv"; break;
// adobe
case "pdf": $ctype="application/pdf"; break;
// ms office
case "doc": $ctype="application/msword"; break;
case "rtf": $ctype="application/rtf"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
// open office
case "odt": $ctype="application/vnd.oasis.opendocument.text"; break;
case "ods": $ctype="application/vnd.oasis.opendocument.spreadsheet"; break;
default: $ctype = "application/force-download"; break;
}
if(in_array($ext, $valid_formats_vid)){
if (file_exists($filePath)) {
header('Content-Type: ' . mime_content_type($filePath));
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
}
}
else if(in_array($ext, $valid_formats_img)) {
if (file_exists($filePath)) {
header('Content-Type: ' . mime_content_type($filePath));
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
}
}
else if(in_array($ext, $valid_formats_docs)) {
if (file_exists($filePath))
{
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filePath)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".#filesize($filePath));
set_time_limit(0);
#readfile($filePath) or die("File not found."); }
}
Header from embed that is through media.php
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Disposition:filename=encoded_2012-10-19_22.37.09_1359032866.flv
Content-Length:0
Content-Type:video/x-flv
Date:Thu, 24 Jan 2013 16:26:32 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=88
Pragma:no-cache
Server:Apache/2.2.20 (Ubuntu)
X-Powered-By:PHP/5.3.6-13ubuntu3.8
Header from direct link to file (the one that works)
Accept-Ranges:bytes
Connection:Keep-Alive
Content-Length:2428614
Content-Type:video/x-flv
Date:Thu, 24 Jan 2013 16:23:54 GMT
ETag:"26ca3d8-250ec6-4d4087c796500"
Keep-Alive:timeout=5, max=100
Last-Modified:Thu, 24 Jan 2013 13:07:00 GMT
Server:Apache/2.2.20 (Ubuntu)
Managed to change it to this through media.php (but still not working)
header("Content-Type: $ctype");
header('Content-Length: ' . filesize($filePath));
header('Accept-Ranges: bytes');
$now = time( );
$then = gmstrftime("%a, %d %b %Y %H:%M:%S GMT", $now + 365*86440);
header("Expires: $then");
ob_clean();
flush();
readfile($filePath);
Accept-Ranges:bytes
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:2428614
Content-Type:video/x-flv
Date:Thu, 24 Jan 2013 16:44:18 GMT
Expires:Fri, 24 Jan 2014 20:47:38 GMT
Keep-Alive:timeout=5, max=79
Pragma:no-cache
Server:Apache/2.2.20 (Ubuntu)
X-Powered-By:PHP/5.3.6-13ubuntu3.8
The problem is here:
flashvars="file=media.php?file=nameoffile.flv&user=userid&folder=videos"
flashvars receives a query string, so this is interpreted as
file : media.php?file=nameoffile.flv
user : userid
folder : videos
You need to urlencode the file parameter:
flashvars="file=media.php?file=nameoffile.flv&user=userid&folder=videos"
I managed to get it working with the following, I also included a thumb image for the player (this was taken on upload through ffmpeg):
header("Content-Type: $ctype");
header('Content-Length: ' . filesize($filePath));
header('Accept-Ranges: bytes');
$now = time( );
$then = gmstrftime("%a, %d %b %Y %H:%M:%S GMT", $now + 365*86440);
header("Expires: $then");
ob_clean();
flush();
readfile($filePath);
$flv_path = 'media.php?file='.$row['cur_image'].'&folder=videos&user='.$row["posted_by"];
$thumb = pathinfo($row['cur_image']);
$thumb_path = 'media.php?file='.$thumb['filename'].'.jpg&folder=videos&user='.$row["posted_by"];
?>
<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->
<div id="mediaplayer_<?php echo $row['p_id']; ?>">JW Player goes here</div>
<script type="text/javascript">
jwplayer("mediaplayer_<?php echo $row['p_id']; ?>").setup({
flashplayer: "jwplayer/jwplayer.flash.swf",
file: "<?php echo $flv_path; ?>",
image: "<?php echo $thumb_path; ?>",
controlbar: "bottom",
width: "380",
height: "200",
primary: "flash",
type: "mp4",
controls: true,
allowscriptaccess: 'always'
});
</script>
<!-- END OF THE PLAYER EMBEDDING -->
One of the file reading methods should work, please review this link: http://www.ibm.com/developerworks/library/os-php-readfiles/
Alter your code, this line: readfile($filePath); with other file reading function, I think streaming should work also with (PHP fread)
PHP Manual: fread : provides useful information, please also try to use first user note.
http://php.net/manual/en/function.fread.php

Categories