in my forum, I'm trying to stop links from converting into clickbale links and place a custom text language instead that says Links Not Allowed.
This seems to be the code that creates the clickable links. My question, is it possible to convert the urls to non-clickable text that says "Spam - Links Not Allowed"
public function parseUrl($params) {
$url = $params['url'];
$text = $params['text'];
If I remove this last line, it seems to make the links disappear, however I would like to display a custom message instead. Sorry if this is a basic question, my code knowledge is at a beginner level.
It's not just server side action, for this reason you should call JS function, so, at first change your php code to this :
$url = $params['javascript:myfunction();'];
and then in client side for javascript write this :
function myfunction(){
alert('not allowed!');
}
$text = $params['text'];
replace "text" with "spam - links not allowed."
In terms of how to make it not clickable, I need to see where the link is actually used.
Related
I'm learning PHP and MySQL and I have a question. I've seen that a lot of sites use this, for example:
http://www.example.com/index.php?product_id=8
or
http://www.example.com/index.php?cattegory=headphones
If you change the values in the URL, the content the page displays changes, so I would like to know how to use that in my webpage, thanks!
You can check for the GET parameter included in the url...
if($_GET['product_id' == 8]) { ... }
I have built a custom CMS. Recently, I added the ability to create pages dynamically. I used CKEditor for the content of these pages.
I would also like to run some php functions that may be included in the content of the page stored in mysql.
I DO NOT want to store actual PHP code in the database, but rather function names perhaps. For example, in a page stored in the database I may have.
<?php //begin output
hello world!
check out this latest news article.
news($type, $id);
//end output
?>
What is the best way to find and execute this existing function without using EVAL if its found in the output? I was thinking along the lines of wordpress style short codes. Maybe [[news(latest, 71]] ? Then have a function to find and execute these functions if they exist in my functions.php file. Not really sure the best way to go about this.
I'm not searching for any code answers, but more of a best practice for this type of scenario, especially one that is safest against possible injections.
I found a solution from digging around and finding this thread
How to create a Wordpress shortcode-style function in PHP
I am able to pass short codes like this in CKEditor
[[utube 1 video_id]]
Then, in my page that renders the code:
print shortcodify($page_content);
using this function:
function shortcodify($string){
return preg_replace_callback('#\[\[(.*?)\]\]#', function ($matches) {
$whitespace_explode = explode(" ", $matches[1]);
$fnName = array_shift($whitespace_explode);
return function_exists($fnName) ? call_user_func_array($fnName,$whitespace_explode) : $matches[0];
}, $string);
}
If the function name exist (utube) it will fire the function.
Only problem Im having at the moment is not matter where I place the [[shortcode]] in my editor, it always executes first.
For example, in CKEditor I put:
Hello world! Check out my latest video
[[utube 1 video_id]]
It will always put the text under the video instead of where it is in the document. I need to figure a way to have the short code execute in the order it is placed.
How do i change a url in a string example :
This is a link http://google.com.au
To something like this
This a warning page link http://google.com.au
EDIT:
What i'm trying to do is take a description entered by the end user, They might enter links in the description, i want to change all the links to make them GOTO a warning page, the warning page is ./warn.php?site=link
The string might look like this
This is a awesome description Google and this another link Google images
Ok here's what i tried:
$descc = mysql_real_escape_string($_POST['description']);
$descc = preg_replace('"\b(http://\S+)"', '$1', $descc);
Check this, although im not sure if you are really refering to this, just let me know the case then ---
$mylink = "http://google.com.au";
This a warning page link http://google.com.au
EDIT version 1.0
Even it is on description box data you can fetch it via jquery or php like
$mylink = $_GET['desc_name_data'];
Please be more specific with the problem :)
EDIT Version 1.1
Check this and let me know then --
echo preg_replace('(<a href="http://\S+)', '<a href="./warn.php?site='.'google.com.au'.'">google.com.au', $descc);
I'm not sure I understand your question, but urlencode may help.
http://google.com.au
If this isn't it, then please be a bit more specific with what you're trying to achieve and what you have tried.
EDIT:
Ok, you could try a HTML parser to extract the href from the link, then rewrite appropriately. This is likely to be more reliable that a regex.
You should still add urlencode if you're passing a url as a querystring.
You could have a look at preg_replace().
I've been looking around for some way to either code up links using bbcode or manually convert a url in a specified message to a link. BBCodes to me are just getting a little old. Although, are still massively heavily used for such things as smileys etc.
I'd be looking to probably do a mixture of the two functionalities.
Can anyone advise on something they use or have used recently for prettifying a messaging system, so to speak.
As far as converting links, Codeigniter's got you covered with the url helper:
auto_link()
Automatically turns URLs and email addresses contained in a string
into links. Example: $string = auto_link($string);
The second parameter determines whether URLs and emails are converted
or just one or the other. Default behavior is both if the parameter is
not specified. Email links are encoded as safe_mailto() as shown
above.
As for smilies, that's covered as well. There is actually a smiley helper:
If you give up and want to parse bbcode, here's a helper written by Phil Sturgeon (a lead Codeigniter developer): https://github.com/bcit-ci/CodeIgniter/wiki/BBCode-Helper
If you wanted to go with something client side for BBCode interpritation, I've written an extendible BBCode parser in JavaScript.
It has all of the standard BBCode tags, but if your messaging system needed some new tags for certain kinds of URL manipulation they could be easily added. As an example, for a smilies tag you could extend it like this:
"smiley": {
openTag: function(params,content) {
if (content === ":)") {
return "<img src='smiley.png'/>";
} else if (content === ":(") {
return "<img src='frown.png'/>";
} else {
return "";
}
},
closeTag: function(params,content) {
return "";
}
}
And then the BBCode would look something like:
[smiley]:)[/smiley]
And the HTML code it would generate from that would look like this:
<img src='smiley.png'/>
This might be more work than what you want and you may not want your own custom tags for your messaging system, but I figured I'd mention it just in case.
So I have a site with a dozen pages on it using Drupal as a CMS. Wanted to know how to set/append all href links on any page with a dynamic attribute?
so here is an example URL:
http://www.site.com/?q=node/4
and I wanted to append to the URL like so:
http://www.site.com/?q=node/4&attr=1234
Now I have a nav bar on the site and when I hover over the link I see the url but I need to append the &attr=1234 string to the end of it. The string is dynamic so it might change from time to time.
I was thinking jQuery would be a good choice to do this but does Drupal have any functionality as well?
Now I've seen a couple of posts on Stack:
Post 1
Post 2
Problem is I'm learning my way around Drupal and have minimal experience with jQuery but getting better with both. I see the jQuery can replace a HREF but looks like they hard coded the HREF, could jQuery find all HREF's on a page and append the string to it? Also does Drupal have this functionality and what would be the best approach?
Also need this to work for clean or standard URL format, so I think Apache would handle this I just wanted to make sure.
Thanks for any help
EDIT:
Looks like the general consensus is the Drupal should handle this type of request. Just looking for the best implementation. Simple function call would be best but I would like it to dynamically add it to all existing href's as I want this to be dynamic instead of hard coding any url/href calls. So I could add/remove pages on the fly without the need to reconfigure/recode anything.
Thanks for the great tips though
EDIT #2:
Okay maybe I'm asking the wring question. Here is what I need and why it's not working for me yet.
I need to pass a value in the url that changes some of the look and feel of the site. I need it to be passed on just about every href tag on the page but not on User logout or Admin pages.
I see in my template code where the nav links get generated, so I though I could pass my code in the attributes array as the second parm to the function, but that is setting the tag attributes and not the URL attributes.
Now I see the bottom nav links use this Drupal function: menu_navigation_links() in menu.inc but the top nav uses a custom function.
This function in the template.php script looks to be the one creating the links
function lplus($text, $path, $options = array()) {
global $language;
// Merge in defaults.
$options += array(
'attributes' => array(),
'html' => FALSE,
);
// Append active class.
if (($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page())) &&
(empty($options['language']) || $options['language']->language == $language->language)) {
if (isset($options['attributes']['class'])) {
$options['attributes']['class'] .= ' active';
}
else {
$options['attributes']['class'] = 'active';
}
}
// Remove all HTML and PHP tags from a tooltip. For best performance, we act only
// if a quick strpos() pre-check gave a suspicion (because strip_tags() is expensive).
if (isset($options['attributes']['title']) && strpos($options['attributes']['title'], '<') !== FALSE) {
$options['attributes']['title'] = strip_tags($options['attributes']['title']);
}
return '<a href="'. check_url(url($path, $options)) .'"'. drupal_attributes($options['attributes']) .'><b>'. ($options['html'] ? $text : check_plain($text)) .'</b></a>';
}
not sure how to incorporate what I need into this function.
So on the home page the ?q=node/ is missing and if I append the ampersand it throws an error.
http://www.site.com/&attr=1234 // throws error
But if I mod it to the correct format it works fine
http://www.site.com/?attr=1234
Assuming that when you mean pages, you mean the content type of pages (will work for any other content type as long as it's really content and not something in a block or view).
You can easily replace the contents of any node that is about to be viewed by running a str_replace or with a regular expression. For instance, using str_replace:
function module_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch($op) {
case "view":
$node->body = str_replace($expected_link, $desired_link);
break;
}
}
where you define desired link somewhere else. Not an optimal solution, but non-Javascript browsers (yes, they still exist!) can't get around the forced, desired URLs if you try to change it with Javascript.
I think doing it in Drupal/PHP would be cleaner. Check out Pathauto module: http://drupal.org/node/17345
There is a good discussion on a related topic here:
http://drupal.org/node/249864
This wouldn't use jquery (instead you would overwrite a function using PHP) but you could get the same result. This assumes, however, that you are working with menu links.
I think you should consider exploring PURL ( http://drupal.org/project/purl ) and some of the modules it works with e.g. spaces and context.
I don't suggest you use jQuery to do this. It's a better practice to do this server side in PHP (Drupal).
You can overwrite the links dynamically into your preprocess page function.
On your template.php file:
function *yourtheme*_preprocess_page(&$vars, $hook){
//You can do it for the region that you need
$vars['content'] = preg_replace('/<a href="(.*?)"/i', '<a href="$1&attr=1234"', $vars['content']);
}
Note:
I did not try it, its only a hint.
It will add your parameters to the outside links too.