wordpress style shortcodes in Ckeditor / PHP - php

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.

Related

Smarty: How to read a {$var} stored in the database?

I use codeigniter with smarty.
I have a variable stored in the db called $serverName. I want to expand it to its actual value "Pedrosite". But when the page is loaded, it displays exactly {$serverName} and not the value.
So I found this solution on stackoverflow, using smarty's fetch function:
$data['content'] contains the text from the database.
$data['content'] = $this->CI->smarty->fetch('string:'.$data['content']);
With that, I can display the smarty vars, like: {$smarty.const.FCPATH}
But none of my custom $vars while they can be shown in a regular template (.tpl).
So I found this workaround that looks very hacky to me:
$this->CI->smarty->assign('serverName', $this->CI->config->item('server_name'));
I can put this in one of my __construct function and then it will affect the whole site, and then it loads properly. But I'm not sure it's the right way to proceed at all.
I don't really understand your question, but, if you have your variable $serverName and it content "Pedrosite" you can display it like that :
$this->smarty->assign('serverName' , $serverName);
$this->smarty->view('/modules/xxxxxx');
And display it in your .html file for example :
<p>{$serverName}</p>

How to make dynamic links in php without eval()

I am using wordpress for a web site. I am using snippets (my own custom php code) to fetch data from a database and echo that data onto my web site.
if($_GET['commentID'] && is_numeric($_GET['commentID'])){
$comment_id=$_GET['commentID'];
$sql="SELECT comments FROM database WHERE commentID=$comment_id";
$result=$database->get_results($sql);
echo "<dl><dt>Comments:</dt>";
foreach($result as $item):
echo "<dd>".$item->comment."</dd>";
endforeach;
echo "</dl>";
}
This specific page reads an ID from the URL and shows all comments related to that ID. In most cases, these comments are texts. But some comments should be able to point to other pages on my web site.
For example, I would like to be able to input into the comment-field in the database:
This is a magnificent comment. You should also check out this other section for more information
where getURLtoSectionPage() is a function I have declared in my functions.php to provide the static URLs to each section of my home page in order to prevent broken links if I change my URL pattern in the future.
I do not want to do this by using eval(), and I have not been able to accomplish this by using output buffers either. I would be grateful for any hints as to how I can get this working as safely and cleanly as possible. I do not wish to execute any custom php code, only make function calls to my already existing functions which validates input parameters.
Update:
Thanks for your replies. I have been thinking of this problem a lot, and spent the evening experimenting, and I have come up with the following solution.
My SQL "shortcode":
This is a magnificent comment. You should also check out this other section for more information
My php snippet in wordpress:
ob_start();
// All my code that echo content to my page comes here
// Retrieve ID from url
// Echo all page contents
// Finished generating page contents
$entire_page=ob_get_clean();
replaceInternalLinks($entire_page);
PHP function in my functions.php in wordpress
if(!function_exists("replaceInternalLinks")){
function replaceInternalLinks($reference){
mb_ereg_search_init($reference,"\[custom_func:([^\]]*):([^\]]*)\]");
if(mb_ereg_search()){
$matches = mb_ereg_search_getregs(); //get first result
do{
if($matches[1]=="getURLtoSectionPage" && is_numeric($matches[2])){
$reference=str_replace($matches[0],getURLtoSectionPage($matches[2]),$reference);
}else{
echo "Help! An unvalid function has been inserted into my tables. Have I been hacked?";
}
$matches = mb_ereg_search_regs();//get next result
}while($matches);
}
echo $reference;
}
}
This way I can decide which functions it is possible to call via the shortcode format and can validate that only integer references can be used.
I am safe now?
Don't store the code in the database, store the ID, then process it when you need to. BTW, I'm assuming you really need it to be dynamic, and you can't just store the final URL.
So, I'd change your example comment-field text to something like:
This is a magnificent comment. You should also check out this other section for more information
Then, when you need to display that text, do something like a regular expression search-replace on 'href="#comment-([0-9]+)"', calling your getURLtoSectionPage() function at that point.
Does that make sense?
I do not want to do this by using eval(), and I have not been able to accomplish this by using output buffers either. I would be grateful for any hints as to how I can get this working as safely and cleanly as possible. I do not wish to execute any custom php code, only make function calls to my already existing functions which validates input parameters.
Eval is a terrible approach, as is allowing people to submit raw PHP at all. It's highly error-prone and the results of an error could be catastrophic (and that's without even considering the possibly that code designed by a malicious attacker gets submitted).
You need to use something custom. Possibly something inspired by BBCode.

How to gather code to be used in a htmlentities() function without rendering beforehand?

To put my question into context, I'm working on an entirely static website where 'post' pages are created by myself manually - there's no CMS behind it. Each page will require a <pre> <code> block to display code as text in a styled block. This could be very few - several which is why I'm trying to do this for ease.
Here's what I've done -
function outputCode($code) {
return "<pre class='preBlock'><code class='codeBlock'>".htmlentities($code)."</code></pre>";
}
The code works as expected and produces an expected outcome when it's able to grab code. My idea is to somehow wrap the code for the code block with this function and echo it out for the effect, fewer lines and better readability.
As I'm literally just creating pages as they're needed, is there even a way to create the needed code blocks with such function to avoid having to manually repeat all the code for each code block? Cheers!
EDIT:
I was previously using this function and it was working great as I was pulling code from .txt documents in a directory and storing the code for code blocks in a variable with file_get_contents(). However, now, I'm trying to get the function to work by manually inputting the code into the function.
Well. Wrapping the function input in ' ' completely slipped my mind! It works just fine now!
If I understand correctly, you want to re-use your outputCode function in several different PHP files, corresponding to posts. If yes, you could put this 1 function in its own file, called outputcode.php for example, and then do
include "outputcode.php";
in every post/PHP file that needs to re-use this function. This will pull in the code, from the one common/shared file, for use in each post/PHP file that needs it. Or maybe I'm misreading your last paragraph :(

How to manually call MediaWiki to convert wiki text to HTML?

I have a MediaWiki installation and I'm writing a custom script that reads some database entries and produces a custom output for client.
However, the text are in wiki format, and I need to convert them to HTML. Is there some PHP API I could call -- well there must be, but what and how exactly?
What files to include and what to call?
You use the global object $wgParser to do this:
<?php
require(dirname(__FILE__) . '/includes/WebStart.php');
$output = $wgParser->parse(
"some ''wikitext''",
Title::newFromText('Some page title'),
new ParserOptions());
echo $output->getText();
?>
Although I have no idea whether doing it this way is a good practice, or whether there is some better way.
All I found is dumpHTML.php that will dump all your mediawiki ; or may be better API:Parser wiki text which tells :
If you are interested in simply getting the rendered content of a
page, you can bypass the api and simply add action=render to your url,
like so: /w/index.php?title=API:Parsing_wikitext&action=render
Once you add action=render it seems you can get the html page ; dont you think ?
hope this could help.
regards.

How can I save a template and download it from the database using smarty template engine

I'm having a hard time saving my template to a database and download the contents to view using Smarty.
I followed the tutorial on Smarty.net on how to make a custom retrieve type.
My original method was save the tpl markup to the db then download it and pass it as a variable to a smarty assigned variable and in my text.pl do something like {$source}
but this parses it as a string and smarty won't compile it.
the tutorial on smarty website didn't help and just leaves me with more questions...
this is what i tried based on the tutorial
function db_get_template($tpl_name, &$tpl_source, &$s)
{
$tpl_name = "index.tpl";
$tpl_source = nl2br($defaults->get_default_theme('theme_data'));
return true;
}
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty)
{
$tpl_timestamp = (int)time();
//$tpl_timestamp = (int)$defaults->get_default_theme('modified_on');
return true;
}
function db_get_secure($tpl_name, &$smarty)
{
// assume all templates are secure
return true;
}
function db_get_trusted($tpl_name, &$smarty)
{
// not used for templates
}
$s->registerResource("db", array(
"db_get_template",
"db_get_secure",
"db_get_trusted"
));
$s->display("db:index.tpl");
i'm not sure where db:index.tpl is being pulled from. I don't know where the markup is suppose to be loaded into...
I feel dirty even suggesting this, but:
Here's a more relevant manual page than the page you were referencing.
In particular, pay attention to the string: prefix to the "filename" passed in the display method call. You can simply pull the template data out of your database (a horrible idea, IMNSHO) and pass it to Smarty that way. No need to build your own custom resource thing unless you really want to do it that way.
I, for one, would urge you to keep it as stupid, ugly and obvious as possible, as to remind yourself about how much of a horrible, no-good idea this is. I'd like to remind you that Smarty compiles to PHP, and placing Smarty code in the database is an effective gateway to arbitrary code execution, a severe security vulnerability.

Categories