How do you add dynamic anchor text in wordpress links. - javascript

We would like to have links in a word press site that have the current meta description of the target site as the anchor text of the link.
I understand this requires either javascript or php and am not sure which is the appropriate approach and which is most easily supported within word press.

If you have Wordpress then you should have cURL installed and activated (or find the way). Also, there is a PHP function called get_meta_tags(). So, you could do something like this assuming you have an array of links with each URL called $links_array:
foreach($links_array as $link){
$tags = get_meta_tags($link);
$description = #$tags['description'];
//Printing each link
echo "<a href='$link'>$description</a>";
}

Interesting question and yes it is possible. You can't do it with javascript or AJAX because the browsers' cross-domain policy won't allow you to do this. I think it has to be a combination of both.
The first solution that i can think of is creating some kind of proxy with PHP, that returns the contents of the targeted URL (the one you link to):
<?php
$url=$_POST['url'];
if($url!="")
echo file_get_contents($url);
?>
Lets say we call this little script "getit.php". Now you can get a AJAX call going, that sends the target url to your .php file and the .php file returns the content of the targeted page. Then you are going to extract the description meta-tag from the returned data.
Of course you could get it in the PHP file and only return the meta description, because that would even be a better solution. You could try something like this in the PHP:
<?php
$url=$_POST['url'];
$tags = get_meta_tags($url);
return $tags['description'];
?>
PS. Apologies for my bad English, it's not my native language.

Related

Get downloaded file content by code

The following url
https://planning.univ-st-etienne.fr/jsp/custom/modules/plannings/anonymous_cal.jsp?resources=3797&projectId=1&calType=ical&firstDate=2017-08-22&lastDate=2018-08-20
automatically downloads a file for me (.ics) which I need its text content. I would like to automatically get this text by code for my website so I don't need to update it manually everyday.
How could I manage that?
If you use client-side language(Javascript), it will be so difficult Follow question about it.
Then if you use server-side like PHP, you could code like this:
<?php
$urlICS = 'https://planning.univ-st-etienne.fr/jsp/custom/modules/plannings/anonymous_cal.jsp?resources=3797&projectId=1&calType=ical&firstDate=2017-08-22&lastDate=2018-08-20';
$contentICS = file_get_contents($urlICS);
echo $contentICS;
?>

Load all <script> tags from a different page

I'm hooking into a separate page (same domain) and pulling it into the current page using $.load, which would be fine, if I was doing it for the whole page, but I'm not, I'm just doing it for the JavaScript code in that page. I was wondering if it's possible to load all the script tags from said page into the current page?
I'm currently using the below code:
var newMessageURL = $('#lnkCompose a').attr('href');
$('#hiddenScriptLoad').load(newMessageURL);
Is said page on the same domain or do you have access to it? You will run into trouble with the cross domain origin policy otherwise.
If you do have access, the only way is to parse the html using a regex statement or html parser and pull the scripts manually.
This sounds like a very hacky approach though and I'm not really sure why you'd want to do this.
If you have access, get the page contents and then use the below to get the script tag sources.
text.match( /<script src="scripts\/(.*?)\.scripts\.js"><\/script>/g )
Credit Javascript regex to get src url between script tag
var newMessageURL = $('#lnkCompose a').attr('href');
$('#hiddenScriptLoad').load(newMessageURL);
The above code will load all the contents you have in the second file and it will also import any javascript codes you have there. But the codes you'll have in second file will not work and wont get into action unless you call to them from first page using a function call or in any other manner.
If you just want to separate your js codings and html and have them in two separate files, it would be better to use PHP to import the second file into the first one and in this way, when the page is loaded in the client browser, it will render it as it was just a single file containing both contents. Ex..
<?php
include("script_file.js");
?>
And also if you want get only the js part of the second file use something like this
<?php
$Vdata = file_get_contents('path/to/YOUR/FILE.php');
preg_match_all("'<script(.*?)</script>'si", $Vdata, $match);
foreach($match[1] as $val)
{
echo $val;
}
?>

How to run PHP editable in an iframe with JS?

I created an HTML/JSS editor using this tool, which, when editing displays the result in an <iframe> (preview).
editor.getSession().addEventListener('change', function () {
iframe.contentWindow.document.body.innerHTML = editor.getSession().getValue(); // ex: '<div>Hi!</div>'
});
This is easy, the problem is with the PHP editor... obviously if I insert a PHP code in the innerHTML this can not be executed on the client side.
If value is:
<?php
$name= 'John Doe';
echo $name;
?>
This shows:
<!--?php $name= 'John Doe'; echo $name; ?-->
What is the best way to save this temporary code and run instantly on the iframe (preview)?
What do you mean by
run instantly on the iframe (preview)
If you wanted the user edited php code to run and show output completely in the client side browser,
then there is no pragmatic way to do this.
Because, to run php codes,
you need a php runtime at the first place.
Now, whether the clients have php installed in their system(PC) or not,
you have no access to that via the browser.
Now if you are desperate to run the code( on your server then ) and send output to the clients browser,
then
WARNING:: It's highly dangerous,
taking string's from unknown(and hence untrusted) sources and running them as php code on the server, you can get your server hacked(and whacked) easily at a short time :p
because,you don't know what code they are writing(unless you explicitly moderate it before running :p )
(Now If you don't know what you are doing)
you can use the eval construct as described here:
http://php.net/manual/en/function.eval.php
eval("?> $str <?php ");

Remove script tags using javascript or php

The title may not be very clear but I will clearly explain my problem here.
I am designing a website for my institute in which I had to provide a editor where user can create html pages and save them to certain folder (user wouldn't know the exact folder, it's created using php while registration). I have decided to use ck-editor for the editor purpose. To save the data I send a post request using ajax to a php script which simply uses
file_put_contents("folder/file_name.html",$_POST['data'])
To show the pages view_page.php accepts the file name as a get variable and then includes the html file e.g.
URL:
view_post.php?file_path=user/good.html
PHP CODE:
<?php
try{
#include_once("ed423eba62af16d6ab38cbfd2295b304/".$_GET['file_name']);
}
catch(Exception $e){
echo "Can't find the requested file.";
}
?>
Now the problem I am facing is that if user submits data that contains some script tags I have to make sure that the script tags doesn't get saved or doesn't run when the page loads. How can I do that?
You can try strip_tags() - http://www.php.net/strip_tags or HTML Purifier - http://htmlpurifier.org/
I would suggest HTML Purifier.

Hide Links in Source code

I want to hide file links generated by php function in source code. I know its impossible to hide source code but i think there should be a way to hide php generated links in php code.
Here is the part of my code which used to generate links.
<?php foreach($tracks as $track){ ?>
<tr class="track"
data-track_order="<?php echo $track['menu_order']; ?>
"data-track_src=" <?php echo $track['audio_file']; ?>">
OUTPUT IN SOURCE
<tr class="track" data-track_order="2" data-track_src="http://domain.com/spins.mp3">
Is there any way in javascipt or in php vulnerability to make this hidden in source?
Well, From the above code, i tried so many encryptions methods but none of them worked.
I need any solution to make it hidden in source.
There are ways to try to get around this topic BUT the browser NEEDS to see the plain html code in order to render the webpage. Because of this current methods can be easily circumnavigated and they client will still be able to get hold of the link. So you can never fully stop the client getting your links BUT you can make it harder for them to get at it by using some techniques like javascript Obfuscation.
I presume that you want to hide the location so that people can't just retrieve the file without going through your site?
Instead of serving the file directly, have a php file serve the file. This file can then check that you are logged into the system, or have a time limited auth key that was generated from the page, whatever you think may limit the ability to copy and paste the link.
If the client accessing the file doesn't pass the checks, you serve them an authorisation failure header instead of the file contents.
What is the purpose of hiding the link? To stop people from being able to see the file location, or to stop anything other than your application from accessing the MP3's?
You can program the link into your JavaScript and obfuscate it which will make it hard for the end user to see the link but ultimately its impossible to complete hide it if you are sending the end user to that page.
If you want to simply stop people from accessing the MP3 location(s) you might be better off looking at putting a .htaccess / mod rewite on the directory that they are residing in, or, have a single .php page to load in the MP3's that will authenticate the referrer and/or server IP address before loading the required file.

Categories

Resources