Process Tags in Plugin - php

I have an Expression Engine plugin that has a file parameter for example:
{exp:my_plugin file='/css/css.js'}
I can get the parameter in the plugin using
$file = $this->EE->TMPL->fetch_param('file');
Is there a way to process $file to replace any tags, i.e. global variables and snippets, so that I could do something like:
{exp:my_plugin file='{global_path}/css.js'}
And have {global_path} be replaced with the value of global path?

In your plugin, you can parse the parameter to match global variables:
$value = $this->_ee->TMPL->fetch_param('value', '');
$value = $this->_ee->TMPL->parse_globals($value);
You can find an example in https://github.com/pvledoux/Pvl_checkif/zipball/master

Related

How can I sanitise the explode() function to extract only the marker I require?

I have some php code that extracts a web address. The object I have extracted is of the form:
WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults
Now in PHP I have called this object $linkHREF
I want to extract the id element only and put it into an array (I'm bootstrapping this process to get multiple id's)
So the command is:
$detailPagePathArray = explode("id=",$linkHREF); #Array
Now the problem is the output of this includes what comes after the id tag, so the output looks like:
echo $detailPagePathArray[0] = WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&w
echo $detailPagePathArray[1] = bf&page=1&
echo $detailPagePathArray[2] = 16123012&source=searchresults
Now the problem is obvious, where it'd firstly picking up the "id" in the "wid" marker and cutting it there, however the secondary problem is it's also picking up all the material after the actual "id". I'm just interested in picking up "16123012".
Can you please explain how I can modify my explode command to point it to the particular marker I'm interested in?
Thanks.
Use the built-in functions provided for the purpose.
For example:
<?php
$url = 'http://www.example.com?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults';
$qs = parse_url($url);
parse_str($qs['query'], $vars);
$id = $vars['id'];
echo $id; // 16123012
?>
References:
parse_url()
parse_str()
if you are sure that you are getting &id=123456 only once in your object, then below
$linkHREF = "WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults";
$str = current(explode('&',end(explode('&id', $linkHREF,2))));
echo "id" .$str; //output id = 16123012

Create URL with only A-Z characters that includes variable and extension

I am trying to create file links based a variable which has a "prefix" and an extension at the end.
Here's what I have:
$url = "http://www.example.com/mods/" . ereg("^[A-Za-z_\-]+$", $title) . ".php";
Example output of what I wish to have outputted (assuming $title = testing;):
http://www.example.com/mods/testing.php
What it currently outputs:
http://www.example.com/mods/.php
Thanks in advance!
Perhaps this is what you need:
$title = "testing";
if(preg_match("/^[A-Za-z_\-]+$/", $title, $match)){
$url = "http://www.example.com/mods/".$match[0].".php";
}
else{
// Think of something to do here...
}
Now $url is http://www.example.com/mods/testing.php.
Do you want to keep letters and remove all other chars in the URL?
In this case the following should work:
$title = ...
$fixedtitle=preg_replace("/[^A-Za-z_-]/", "", $title);
$url = "http://www.example.com/mods/".$fixedtitle.".php";
the inverted character class will remove everything you do not want.
OK first it's important for you to realize that ereg() is deprecated and will eventually not be available as a command for php, so to prevent an error down the road you should use preg_match instead.
Secondly, both ereg() and preg_match output the status of the match, not the match itself. So
ereg("^[A-Za-z_\-]+$", $title)
will output an integer equal to the length of the string in $title, 0 if there's no match and 1 if there's a match but you didn't pass it another variable to store the matches in.
I'm not sure why it's displaying
http://www.example.com/mods/.php
It should actually be outputting
http://www.example.com/mods/1.php
if everything was working correctly. So there is something going on there, and it's definitely not doing what you want it to. You need to pass another variable to the function that will store all the matches found. If the match is successful (which you can check using the return value of the function) then that variable will be an array of all matches.
Note that with preg_match by default only the first match will be returned. but it will still generate an array (which can be used to get isolated portions of the match) whereas preg_match_all will match multiple things.
See http://www.php.net/manual/en/function.preg-match.php for more details.
Your regex looks more or less correct
So the proper code should look something like:
$title = 'testing'; //making sure that $title is what we think it is
if (preg_match('/^[A-Za-z_\-]+$/',$title,$matches)) {
$url = "http://www.example.com/mods/" . $matches[0] . ".php";
} else {
//match failed, put error code in here
}

Joomla 2.5 get component configuration in config.xml ALWAYS return null

Please help me with this
I am creating a component. There is a config.xml in my component
I write my custom JFormFieldUserCheck
in userCheck.php I want to load parameter or field from config.xml
I used
$param = JComponentHelper::getParams('com_my-component');
var_dump($param);
Resul is
object(stdClass)#214 (0) { }
But when I change com_my-component to com_content (Joomla default component).
then var_dump, result is fine.
Thanks in advance
I have added an excerpt from the blog entry:
Plugin parameters from inside a plugin
$param = $this->params->get('paramName', 'defaultValue');
Plugin parameters from outside a plugin
$plugin = &JPluginHelper::getPlugin('exampleType', 'example');
$pluginParams = new JParameter($plugin->params);
$param = $pluginParams->get('paramName', 'defaultValue');
Module parameters from inside a module
$param = $params->get('paramName', 'defaultValue');
Module parameters from outside a module
$module = &JModuleHelper::getModule('example');
$moduleParams = new JParameter($module->params);
$param = $moduleParams->get('paramName', 'defaultValue');
Component parameters from inside a component
$componentParams = &JComponentHelper::getParams('com_example');
$param = $componentParams->get('paramName', 'defaultValue');
Component parameters from outside a component
$componentParams = &JComponentHelper::getParams('com_example');
$param = $componentParams->get('paramName', 'defaultValue');
Template parameters from inside a template
$param = $this->params->get('paramName');
Template parameters from outside a template
jimport('joomla.filesystem.file');
$mainframe = &JFactory::getApplication();
$params = $mainframe->getParams(JFile::read(JURI::root() .'/templates/template_name/params.ini'));
$param = $params->get('paramName', 'defaultValue');
Template parameters from an included file outside the Joomla framework
// Get params.ini relative to the current file location (use your own relative path here)
$paramsFile = dirname(__FILE__) . '/../../params.ini';
// Only continue if the file exists
if(file_exists($paramsFile)) {
// Get contents from params.ini file
$iniString = file_get_contents($paramsFile);
// Escape double quotes in values and then double-quote all values (because Joomla doesn't do that for us..)
$iniQuoted = preg_replace('/=(.*)\\n/', "=\"$1\"\n", addcslashes($iniString, '"'));
// Parse the ini string to an associative array
$iniParsed = parse_ini_string($iniQuoted);
} else {
$iniParsed = '';
}
// Set params to obtained values or empty array
$params = (!empty($iniParsed)) ? $iniParsed : array();
// Get param value from array
$param = $params['paramName'];
Clearly, Joomla isn't understanding your component. Insure that it's properly installed in Joomla, and that the xml file for the component is accurate and formed properly. If Joomla does find your component, or is unable to load the XML, then the parameters cannot be made available to your PHP. These steps are done with the valid entries in the database, and the XML, both of which are typically done with the component installation, but can be done manually as long as you get them all correct.
Had the same problem. The result was empty until i went to the configuration of my component and saved it (though there were default values printed in the textfields).

PHP Wordpress gallery not finding the XMLl file

The code is below - it uses a wordpress shortcode which is [my_hmg=widget.xml] but if you try change the xml file like this [my_hmg=example_gallery.xml] it just always reverts to the default widget.xml
The problem is in the function my_hmg_filter_Callback in particular these 2 lines;
#$my_hmg_file = #$output['filename'];
if($my_hmg_file==""){$my_hmg_file = "widget.xml";}
For some reason it always thinks the file name is blank so always reverts to widget.xml.
The files can be downloaded from here - http://www.gopiplus.com/work/2010/07/18/horizontal-motion-gallery/
function my_hmg_show_filter($content){
return preg_replace_callback('/\[my_hmg=(.*?)\]/sim','my_hmg_filter_Callback',$content);
}
function my_hmg_filter_Callback($matches)
{
$my_hmg_package = "";
$var = $matches[1];
parse_str($var, $output);
#$my_hmg_file = #$output['filename'];
if($my_hmg_file==""){$my_hmg_file = "widget.xml";
}
Firstly change the short code to [my_hmg file='file.xml']
Then if you have a quick read of Wordpress's short code API you'll see that the first argument in the callback function are the attributes of the short code.
This way you can the reference the attribute 'file' in the array and get the proper url.

Textmate: HTML syntax: embedded in PHP single and double quotes

Is there anyway to highlight HTML embedded in PHP single and double quotes — which has no scope defined, within Textmate?
Example:
printf( 'This is some <strong>Text</strong>', 'foobar' );
Everything within the single quotes belong to the same scope. Its annoying. Has anyone tried to fix this somehow? I'd rather not alter the language files (without guidance), im not fluent in regex.
Nevermind, i found out how to do it. After reading on how scopes work in Textmate. I opened up the PHP languages panel under the Bundle Editor and pasted an include of the following scope name into the string-single-quoted scope:
text.html.basic
Heres the include:
{ include = 'text.html.basic'; },
and heres how the entire string-single-quoted section looks like now:
string-single-quoted = {
name = 'string.quoted.single.php';
contentName = 'meta.string-contents.quoted.single.php';
begin = "'";
end = "'";
beginCaptures = { 0 = { name = 'punctuation.definition.string.begin.php'; }; };
endCaptures = { 0 = { name = 'punctuation.definition.string.end.php'; }; };
patterns = (
{ include = 'text.html.basic'; },
{ name = 'constant.character.escape.php';
match = '\\[\\'']';
},
);
};
You would probably have to do some rather complicated editing of the scope definitions for HTML in order to get this to work as you describe. PHP files are HTML files by default in TextMate, so you're looking to define a scope where you have an HTML file with embedded PHP with strings that might be HTML. Not every string in PHP is going to be HTML, so you'd need to figure out a way to differentiate non-HTML strings from HTML strings, and if you're not fluent with regular expressions, it would probably take quite a bit of research and trial and error to get it right.
As an alternative, if you're actually using printf rather than simply using it as a generic example, consider using the function to format only what you need formatted and doing something like this.
<?php
$var = sprintf( '%d', $num );
?>
Here's the <strong><?php echo $var; ?></strong>.

Categories