How to know if a url is audio or video? - php

I get some urls from xml rss feed. The rss makes no distinction to audio or video files, they all come from the same tag. How can I distinguish if it is a audio or video url to provide the right to play it?
I have the url stored in a var
var url = "http://www.someWeb/someFile.mp3";
I tried to make a search with jQuery:
var search = url.search(/.mp3|.m4a/i);
if (search > -1){
$('#check').text("it is audio");
}else {
$('#check').text("it is video");
}
it works most of the cases but it could be more types of audio files, there could always be some types of audio, without extension...
Is there a better way to know if a url is a audio or file?

You might be able to use the php function get_headers(). You can test this out by using it to grab one of your links and doing a print_r() to see what you get under the "Content-type" key.
If that works, just go ahead and create an switch case for all your file types and you should be good to go
http://php.net/manual/en/function.get-headers.php

This is a shot in the dark, but if you need to know the content type before loading it, you can try a HEAD request:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4
This is only if the content service supports it though. Of course if you wrote the service then you can implement this yourself. Simply send the headers, but omit the actual content.
On the receiving end the response headers should include 'Content-Type'.
From this you can extract the type of content being delivered. It typically looks like this:
text/plain
image/jpeg
video/mp4
audio/mp3
It is simple enough to determine the general media from the prefix.

You must get HTTP header fields (Content-Type for this case). Look here: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

This is a bit of a dificult one.
I faced the same issue, loading content from an RSS feed, that could contain either video or audio.
There is no way to know what is the content without actually requesting it, and as I'm consuming content from external websites, I can't know for sure if they support the HEAD method.
what I'm currently doing is assuming the URL ends in a filename, taking the extension and using the browser ready version of https://github.com/broofa/node-mime to get the corresponding MIME type.
After getting the MIME type I check if it starts with either 'video' or 'audio'.
Here is the code:
function isVideoByURL(url) {
var filename = url.split('/').pop().split('#')[0].split('?')[0];
if(filename) {
var ext = filename.split('.').pop();
var mimeType = mime.getType(ext);
return mimeType.startsWith('video');
}
return false;
}

Related

How to set PDF page title from Symfony Controller?

Here is my method in a Controller:
/**
* #Route("/bilgi/agr", name="user_agreement")
*/
public function agr(): Response
{
$response = new BinaryFileResponse(__DIR__ . '/../../public/docs/User_Agreement.pdf');
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $response->getFile()->getFileName());
return $response;
}
I'm expecting to see Page title as User_Agreement.pdf, but instead it is agr. Which is not appropriate. I can't change the route because it is being used on several other classes/files.
Is there any way I can set a custom title or at least the file name? When I save the file I see the file name as User_Agreement.pdf so the file name is also correct.
If that is not possible is there workaround to show it in twig/html?
You can't. <title>" is an HTML element, not a PDF one.
If a browser renders a PDF directly (as many/most do nowadays), the only thing they could use for a "title" is the URL for the request. agr in your case.
It's basically something the server side has no control of. It just sends the response, and the browser decides how to show it, and what to show in the space usually reserved for the <title> element.
With the Content-Disposition header you can hint the browser about what name they should suggest for the file to the end-user, but that's all.
If you absolutely need this, yes, you could send a regular HTML response and somehow show the PDF inlined/embedded on the page.

Hide MP3 full url

I have a music player that links to a song using the following syntax:
<li>title</li>
Is there any way that I could have that executed server side and then be displayed like (see below) for the user?
While searching, I ran across this...I like the idea behind having an external file that has the data...like:
<?php
// get-file.php
// call with: http://yoururl.com/path/get-file.php?id=1
$id = (isset($_GET["id"])) ? strval($_GET["id"]) : "1";
// lookup
$url[1] = 'link.mp3';
$url[2] = 'link2.mp3';
header("Location: $url[$id]");
exit;
?>
then using: http://yoururl.com/path/get-file.php?id=1 as the link...the only problem is that when you type http://yoururl.com/path/get-file.php?id=1 the user goes straight to the file...is there any way to disable that ability...maybe some code on get-file.php itself?
Ok, so I did a combination of things that I am satisfied with...although not completely secure, it definitely helped me obscure it quite a bit.
First of all, I am using the AudioJS player to play music - which can be found: http://kolber.github.com/audiojs/
Basically what I did was:
Instead of using "data-src" as the path to my songs I called it "key", that way people wouldn't necessarily think it was a path.
Instead of using "my-song-title" as the name of the songs, I changed it to a number like 7364920, that way people couldn't look for that in the source and find the url that way.
I added + "mp3" to the javascript code after all of the "key" variables, that way I would not have to declare it in obfusticated link.
I used a relative path like "./8273019283/" instead of "your-domain.com/8273019283/", that way it would be harder to tell that I was displaying a url.
Added an iTunes link to the href, that way people might get confused as to how I was pulling the file.
So, now my inline javascript looks like:
<script type="text/javascript">
$(function() {
// Play entire album
var a = audiojs.createAll({
trackEnded: function() {
var next = $("ul li.playing").next();
if (!next.length) next = $("ul li").first();
next.addClass("playing").siblings().removeClass("playing");
audio.load($("a", next).attr("key") + "mp3");
audio.play();
}
});
// Load the first song
var audio = a[0];
first = $("ul a").attr("key") + "mp3";
$("ul li").first().addClass("playing");
audio.load(first);
// Load when clicked
$("ul li").click(function(e) {
e.preventDefault();
$(this).addClass("playing").siblings().removeClass("playing");
audio.load($('a', this).attr('key') + "mp3");
audio.play();
});
});
</script>
My link looks like:
Falling
When you load it up in the browser and you view the source you'll see:
Falling
Then when you use Web Inspector or Firebug you'll see:
Falling - *which doesn't completely give the url away
Basically what I did was make the link look like it's an api-key of some-kind. The cool thing is that you can't just copy the link straight from view source or straight from Web Inspector/Firebug. It's not fool-proof, and can definitely be broken, but the user would have to know what they're doing. It keeps most people away, yet still allows the player to get the url it needs to play the song :)
*also, I got the php obfusticate script from somewhere on Stack Exchange, just not sure where.
Instead of doing a header redirect, add proper headers and include the audio file in your PHP code. Then, in your .htaccess file, you can disallow access to the directory where your audio files live.
If you are using amazon s3 service you can use signed url for your files. It will be more safe as you have to be signed user and also url can be expired. Read this.
No. This is not possible since it is the browser that interprets the HTML to make the page work properly. So if the client (browser) does not know where the mp3 is coming from then it will not be there to use.
On the other hand if you want to have the music switch songs by clicking a link then i suggest you look into some tools like http://jplayer.org/
EDIT: The only way to probably prevent direct access to the file itself would be to read the file in instead of linking to it from the script. For instance on my image hosting site http://www.tinyuploads.com/images/CVN5Qm.jpg and if you were to look at the actual file path on my server, the file CVN5Qm.jpg is out of view from the public_html folder. There is no way to directly access the file. I use databases to take the image id, look up where it is stored, and then readfile() it into the script and display the proper headers to output the image.
Hope this helps
I use http_referer and I can controll the procedence of the link
<?php
// key.php
// call with: http://yoururl.com/path/key.php?id=1
$page_refer=$_SERVER['HTTP_REFERER'];
if ($page_refer=="http://www.yourdomine.com/path/page.html")
{
$id = (isset($_GET["id"])) ? strval($_GET["id"]) : "1";
// lookup
$url[1] = 'link1.mp3';
$url[2] = 'link2.mp3';
header("Location: $url[$id]");
exit;
}
else
{
exit;
}
?>

Display content based on remote url in php?

I currently have two php files (header and footer) on my server that is used as a template and is retrieved on another server that wraps the template files around their software.
Is it possible to display different content based on their url in my template files in php? If so, how?
I don't know if this matters, but the other server uses coldfusion and not php.
The php file could check a parameter in the url, like template.php?url=stackoverflow , so in the php file you could check
if ($_GET['url']=='stackoverflow'){
echo "Stack Overflow template";
}else if ($_GET['url']=='lol'){
echo "Another template";
}else{
echo "error";
}
Edit:
Now the server getting the content, just needs to add that parameter to the url and it gets the template that it wants. You could set a default template in case no parameter is specified.
Would it be possible for their url to contain a get variable like www.theirwebsite.com/?chrome=red? then your file could read that and parse out different themes based on what the variable's value is.
NOt quite sure if I'm understanding you, but you can certainly display different content based on a url.
$remote_content = file_get_contents($someurl);
switch $someurl
case 'www.google.com':
display_google_content();
break;
case 'www.microsoft.com':
throw(BSOD);
break;
default:
display_standard_content();
}
There are two obvious possibilities for how the remote server is attaching your code that spring to mind. The first is using JavaScript to instruct the client to go out and get your content, then write it to the appropriate locations. This should be rather obvious when looking at the HTML source code generated by their application.
The more likely scenario, in my opinion, is that they use CFHTTP to retrieve the content and inject it directly. CFHTTP mimics a broser call -- it's a standard HTTP 1.1 request. It's not going to contain a reference to the url requested on their server. Unless you can convince them to add identifying information to the request, all you'll be able to tell on your server is that the request came from CF (by examining the remote agent).

How to detect if a page is an RSS or ATOM feed

I'm currently building a new online Feed Reader in PHP. One of the features I'm working on is feed auto-discovery. If a user enters a website URL, the script will detect that its not a feed and look for the real feed URL by parsing the HTML for the proper <link> tag.
The problem is, the way I'm currently detecting if the URL is a feed or a website only works part of the time, and I know it can't be the best solution. Right now I'm taking the CURL response and running it through simplexml_load_string, if it can't parse it I treat it as a website. Here is the code.
$xml = #simplexml_load_string( $site_found['content'] );
if( !$xml ) // this is a website, not a feed
{
// handle website
}
else
{
// parse feed
}
Obviously, this isn't ideal. Also, when it runs into an HTML website that it can parse, it thinks its a feed.
Any suggestions on a good way of detecting the difference between a feed or non-feed in PHP?
I would sniff for the various unique identifiers those formats have:
Atom: Source
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
RSS 0.90: Source
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://my.netscape.com/rdf/simple/0.9/">
Netscape RSS 0.91
<rss version="0.91">
etc. etc. (See the 2nd source link for a full overview).
As far as I can see, separating Atom and RSS should be pretty easy by looking for <feed> and <rss> tags, respectively. Plus you won't find those in a valid HTML document.
You could make an initial check to tell HTML and feeds apart by looking for <html> and <body> elements first. To avoid problems with invalid input, this may be a case where using regular expressions (over a parser) is finally justified for once :)
If it doesn't match the HTML test, run the Atom / RSS tests on it. If it is not recognized as a feed, or the XML parser chokes on invalid input, fall back to HTML again.
what that looks like in the wild - whether feed providers always conform to those rules - is a different question, but you should already be able to recognize a lot this way.
I think your best choice is getting the Content-Type header as I assume that's the way firefox (or any other browser) does it. Besides, if you think about it, the Content-Type is indeed the way server tells user agents how to process the response content. Almost any decent HTTP server sends a correct Content-Type header.
Nevertheless you could try to identify rss/atom in the content as a second choice if the first one "fails"(this criteria is up to you).
An additional benefit is that you only need to request the header instead of the entire document, thus saving you bandwidth, time, etc. You can do this with curl like this:
<?php
$ch = curl_init("http://sample.com/feed");
curl_setopt($ch, CURLOPT_NOBODY, true); // this set the HTTP Request Method to HEAD instead GET(default) and the server only sends HTTP Header(no content).
curl_exec($ch);
$conType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if (is_rss($conType)){ // You need to implement is_rss($conType) function
// TODO
}elseif(is_html($conType)) { // You need to implement is_html($conType) function
// Search a rss in html
}else{
// Error : Page has no rss/atom feed
}
?>
Why not try to parse your data with a component built specifically to parse RSS/ATOM Feed, like Zend_Feed_Reader ?
With that, if the parsing succeeds, you'll be pretty sure that the URL you used is indeed a valid RSS/ATOM feed.
And I should add that you could use such a component to parse feed in order to extract their informations, too : no need to re-invent the wheel, parsing the XML "by hand", and dealing with special cases yourself.
Use the Content-Type HTTP response header to dispatch to the right handler.

Mediawiki + AJAX + IE = broken

I have created an extension for mediawiki that works in all major browsers other than IE (any version it appears). The extension relies on mediawiki's ajax wrapper to send an xmlhttprequest with parameters that essentially build a database query to a php script. This script will run a query based on the parameters and then create an XML object (using php's simplexml class) which then returns the XML to javascript for display in the browser (just a table, mostly).
Now with all that information, IE seems to be working up until the point at which it tries to parse the returned XML. I have set the mime type to application/xml and I have tried loading it with various different techniques found via google (none worked).
It is trivial to load the XML for parsing when using non-IE browsers:
function callbackHCL(response){
if (response.readyState == 4) {
var xmlObj = response.responseXML;
if (response.status == '200'){
if (xmlObj !== undefined){
//etc...
Now I can start using dom functions to get at data.
My Question: Does anybody have any suggestions on how to parse xml in IE based on my current scenario?
If you would like to email me at tccroninv#gmail.com, I can provide longer code snippets, they are longer and I don't believe they would help the situation. If you would like me to post more code, just ask as well.
Thanks in advance,
Tim
I think this might be what you want: http://dean.edwards.name/weblog/2006/04/easy-xml/. Basically, IE doesn't return an XML document like the other guys. Need to do a little fancy footwork to make it work correctly. I'm sure there's a library out there that wraps this all up so you don't have to worry about it if you don't want to.

Categories