how to apply custom sanitation on inputs in codeigniter - codeigniter

I want to remove all multiple spaces from inputs (get) using this code:
preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $search);
but I don't know where to put this code so that it would affect all $this->input->get('myfield'); ?
EDIT
maybe I wasn't clear enough.. what I needed is one code that can affect all inputs in all modules throughout my project. e.g. by creating a function in the libraries or editing MY_Controller.php

You can put as like
<?php
$search = $this->input->get('myfield');
$search = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $search);
print $search;
?>

Related

Mass Image Change - Magento

Is there anyway to change all images of products in a single category ?
I have a category with products that have no images, about 175 of them, and I wish to use the same image for all of them.
Is this possible without going in manually and doing this ?
Thanks !
Unfortunately, Magento's admin interface is not very advanced, and does not provide any automated way of assigning the same image to multiple products (other than going into each product one by one, of course).
The most straight forward way to solve this is through a small script:
$category = Mage::getModel('catalog/category')->load(123);
$products = $category->getProductCollection();
foreach ($products as $product)
{
$product->addImageToMediaGallery(
Mage::getBaseDir() . DS . 'images' . DS . 'image.jpg',
/* attribute */ 'image',
/* move */ false,
/* exclude */ false
);
}
Code is untested, but that's the general idea - grab the list of products, go through each one, and use addImageToMediaGallery() to add the image to the product.
This type of script can be easily implemented as a shell script (see scripts in the shell directory).

Sanitize input in joomla

I have code like this in my Joomla plugin:
$some_id = $_GET["someid"];
$db = JFactory::getDBO();
$db->setQuery("SELECT * FROM #__table WHERE id = '$some_id'");
$result = $db->loadRow();
Does Joomla sanitize this automatically, or i need to do something (and what) to sanitize this query ? Using Joomla 2.5.
There is no need to sanitize database queries when using Joomla. The information you are pulling down is the information that has put put there or already there, and thus you don't want to change. I would also recommend using Joomla 2.5 coding standards to make database queries, like so:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('*'))
->from($db->quoteName('#__table'))
->where($db->quoteName('id') . ' = ' . $db->quote($some_id));
$db->setQuery($query);
$rows = $db->loadRow(); //or loadResult()
The only time I have ever needed to sanitize (so to speak) something was when handling files, in which case I used JFile::makeSafe();.
Please take a look at you will get your answer
Secure coding guidelines
http://docs.joomla.org/Secure_coding_guidelines
Take a look at JInput and this corresponding documentation
Filter example:
$jinput = JFactory::getApplication()->input;
$some_id = $jinput->get('someid', '', 'string');

joomla virtuemart search slash error

i am currently developing one joomla site which contains virtuemart and my written component which is working quite well, but the problem shows up when i try to search for products using virtuemart search in example when i search for 12/4 name it replaces 12/4 name with 124 name and displays no results.
So the question is where is that sql-query that loads those items so i can modify it?
or is there another plug-in or something that works with / or is there somekind of fix to this problem?
So after a while i finnaly fixed this issue. My fix is not weary good beacose i didn't find where exactly virtuemart component removes slash from search query so i just did like this......
As frontend uses modules who are located at admin/components/com_virtuemart/modules i had to edit module named product. And in the function named sortSearchListQuery2 i added some code
if ($useCore) {
// if ( $this->keyword !== "0" and $group ===false) {
if (!empty($this->keyword) and $this->keyword !== '' and $group === FALSE) {
$this->setKeyWord($_GET['keyword']);//Added this line!
//$keyword = trim(preg_replace('/\s+/', '%', $keyword), '%');
$keyword = '"%' . $this->_db->getEscaped ($this->keyword, TRUE) . '%"';
//var_dump($keyword,$this->keyword,$_GET['keyword']); debug_zval_dump($keyword); debug_print_backtrace(); die();
And on frontend view named Category in view.html.php i replaced $keyword=vmRequest::uword('keyword', '', ' ') with $keyword = $_GET['keyword']
And that is my approach to fix this problem!

Joomla - show full text of only leading articles in Category Blog Layout

How can I show the full text of a leading article (even if it contains a read more) on a Category Blog Layout?
Is there a back-end setting I can change or which PHP file do I need to overwrite? I only want the leading article's fulltext to show, intro articles should still display the readmore with only intro text.
Thanks in advance.
You don't specify which version of Joomla! so this reply is based on 2.5.x
You will need to create a layout override for com_content, specifically for the category view tmpl file blog.php or blog_item.php.
If your template already has an override installed for com_content you will find it at this location:
/templates/your-template/html/com_content/category/
If not you can copy the original files from your Joomla! 2.5's component directory at /components/com_content/views/category/tmpl/
In that directory you will find a series of files starting with blog and ending with .php - you can override 1 or all of these by coping them to the first path listed above. N.B. Only copy the files you need to change Joomla! is smart enough to look in the default directory if it doesn't find a version in the overrides location.
The default Joomla! 2.5 installation has these blog files:
blog_children.php
blog_item.php
blog_links.php
blog.php
Basically blog.php is the main file and it includes the sub-layouts like blog_item.php as required.
If you're working from the default Joomla! 2.5 files, to achieve your goal you will probably have to override blog.php and blog_item.php and find a way to check whether you are in the leading item. Once you know that your in a leading item you will then want to echo out the full text of the item.
blog_item.php normally just outputs the intro text with a line like this:
<?php echo $this->item->introtext; ?>
You'll want to have the full text echo'd so after you've wrapped the line above in an if to check if you're doing the leading item your output line will look something like this:
<?php echo $this->item->introtext.$this->item->fulltext; ?>
Note: I've haven't check this works it's just speculation and relies on the $item having the full row from the #__content table.
So the articles full text isn't added to the article item in a standard install i.e. you can simply echo $this->item->fulltext.
There are two ways to get the full text.
The simplest & first is to modify the core file at /components/com_content/models/articles.php starting with the first $query->select() in the function getListQuery() which starts on/near line 153. Add ' a.fulltext,' after the a.introtext and before the single apostrophe, so the line looks like this:
function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query->select(
$this->getState(
'list.select',
'a.id, a.title, a.alias, a.title_alias, a.introtext, a.fulltext, ' .
'a.checked_out, a.checked_out_time, ' .
The second way, and the Joomla! update proof way (& therefore probably the better way) is to load the entire article in the override (ugly but it's not a hack) and concatenate the fulltext to the introtext of the first article.
To do this you simply use getTable to load the article using the $this->item->id in blog.php for the first leading item, so at about line 54, between the <?php and the $this->item = &$item; put:
if($leadingcount == 0) {
$contentTable = JTable::getInstance('Content');
if($contentTable->load($item->id)) {
$item->introtext .= $contentTable->fulltext;
}
}
N.B. The if just limits it to the first leading article, if you remove the if it will attach the full text for all leading articles.
This works on my dev installation of Joomla! 2.5.6 so you should be able to do it as well.
Considering the two answers above, there are three changes required to blog.php and blog_item.php. This works in Joomla 3.1 The best is to copy these files to the template override
copy blog.php and blog_item.php
from components/com_content/views/category/tmpl/
to [my template]/html/com_content/category/
In blog.php in the loop for the leading items add one single line:
<?php foreach ($this->lead_items as &$item) : ?>
<div class="leading-<?php echo ...?>">
<?php
$this->item = &$item;
$this->item->leadingItem = true; //ADD THIS LINE !!!
echo $this->loadTemplate('item');
?>
</div>
<div class="clearfix"></div>
<?php
$leadingcount++;
?>
<?php endforeach; ?>
In the blog_item.php replace "echo $this->item->introtext;" with
if ($this->item->leadingItem){
//this is a leading article - show full text and remove "Read more..." button
$itemID = $this->item->id;
$db =& JFactory::getDBO();
$query = "SELECT `fulltext` FROM `#__content` WHERE `id` =" . $itemID;
$db->setQuery($query);
$fulltext = $db->loadResult();
echo $this->item->introtext . $fulltext;
}else{
echo $this->item->introtext;
}
Finally in the blog_item.php do not to show the "Read more..." button at the bottom of each article
Edit line
if ($params->get('show_readmore') && $this->item->readmore)
into
if ($params->get('show_readmore') && $this->item->readmore &&
!$this->item->leadingItem)
Joomla 2.5.x, in your template blog_item.php:
$itemID = $this->item->id;
$db =& JFactory::getDBO();
$query = "
SELECT `fulltext`
FROM `#__content`
WHERE `id` = $itemID;
";
$db->setQuery($query);
$fulltext = $db->loadResult();
and you can use the $fulltext string variable.

Adding an 'All' option into a codeIgniter form_dropdown

I have an array that returns locations from the database, which I am trying to output into a form drop down using the following code:
<?php form_dropdown('idLocation', $queryLocations, set_value('idLocation'); ?>
The $queryLocations has the locationID and the locationName. And this above code works fine to display all locations, now I need to add an option called 'All' having idLocation as zero to appear at the top of location list.
Can someone please help me do this?
Just prepend the $queryLocations array with your values. Here's one way:
form_dropdown(
'idLocation',
array('0' => 'All') + $queryLocations,
set_value('idLocation')
);
You could probably do this earlier, when you actually create the $queryLocations array as well.
$options[0] = 'All';
foreach ($results as $r) $options[$r->idLocation] = $r->locationName;
Something like that...

Resources