About jontas

I like to make things that people find useful.

Magento redirect

Sometimes there is a need to do a redirect from inside a Magento page where you might not know if headers are sent or not – Or maybe you wish to make a new redirect function where you don’t have to worry about writing different code if the headers are sent or not.

This is a way to do a redirect where a javascript redirect is used if headers have been sent (and thus it is no longer possible to use a header redirect)

if (!headers_sent()) {
 
	Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl() . 'portal/');
 
}
 
else {
 
	$baseUrl = Mage::getBaseUrl() . 'portal/';
 
	print '<script type="text/javascript">';
 
	print "window.location.href = '{$baseUrl}';";
 
	print '</script>';
 
}

And the function headers_sent() is a php function so that can of course be used outside of Magento in any php scrip where a header location is nice but not always possible.

OpenX INSERT_RANDOM_NUMBER_HERE example in php

For iframe’s and noscript tags OpenX uses a random string in the image in order to prevent user caching of banners (as that would mean less impressions and possibly outdated ads).
However these tags needs to be added by the user.
An example way to make these random numbers using PHP would be to use

<?php echo rand();?>

So an complete example invocation code would be

<a href="http://www.example.com/ck.php?n=3798N2&cb=<?php echo rand();?>" _fcksavedurl="http://www.example.com/ck.php?n=3798N2&cb=<?php echo rand();?>" _fcksavedurl="http://www.example.com/ck.php?n=3798N2&cb=<?php echo rand();?>" 
<img src="http://www.example.com/avw.php?zoneid=10&n=3798N2&cb=<?php echo rand(); ?>"></a>

Merging personal fabrication and distributed manufacturing.

Another step of the open source 3D printing revolution might be to merge personal fabrication and distributed manufacturing.

Someone produces a model of a custom part that he/she needs produced and then asks the community to produce it (an example of this at thingiverse, a custom part made by Ponskaartje.

I think this is a very nice idea that might get the 3d printing/personal fabrication revolution rolling even faster.

Also I hope that we will start seeing more of this and perhaps even a marketplace that can make this happen (where a user asks for a part and sets some limits on it, and users might bid on making this part) like the sites where for instance programming tasks are put up and the developers have to bid on the task and the one asking chooses the best option.

Magento: Problem uploading images

When I was truing to upload images to products in the Magento admin panel I got the following error:
SSL Error: Invalid or self-signed certificate while uploading image from backend

The way I solved this issue was by setting the timezone on the computer that I was uploading from to the same timezone as the settings in Magento.

Other suggestions to try out if that don’t work:

  • If you are using certificate: Verify that it is valid, you have correct domains etc
  • Set use secure url in admin to false

PostgreSQL 8.3 adding uuid-ossp on Debian

UUID is a nice way to generate (almost) unique id’s.

However on PostgreSQL 8.3 the data type exists, but if you wish to be able to generate UUID’s you need to get a module (in this case uuid-ossp).

On Debian this module is in the contrib package, so if you have not installed this before do it now

user@server:~$ sudo apt-get install postgresql-contrib

Then you need to “load” the module into PostgreSQL; or more accurately you tell PostgreSQL about the new functions this module has, how to reach them etc.
Depending on you security settings (in pg_hba.conf) you may need to log in as the postgres user on your system.

postgres@sever:~$ psql -d MYDB -U postgres -f /usr/share/postgresql/8.3/contrib/uuid-ossp.sql

Ubuntu Hibernate/Suspend problem

First off, quick explanation.
Suspend saves a image to ram. It is faster to save and reload; but the computer uses a minimal amount of energy to keep this in ram. If the power is lost (battery is drained etc) then the image is lost.
Hibernate save a image to disk. It is slightly slower to save and reload; but there is no problem if the power is lost.

However I am not the only one that have noticed that hibernate/suspend on Ubuntu does not work on a default installation.
One solution that more than me have found to be working is to switch to uswsusp.

user@laptop$ sudo apt-get install uswsusp
user@laptop$ sudo s2disk

s2disk does a hibernation (s2ram does a suspend); and it is good to test it out before swapping the hibernate/suspend function.
When you know hibernate/suspend works, you can swap to using uswsusp by using:

user@laptop$ sudo dpkg-divert --rename --divert /usr/sbin/pmi-disabled /usr/sbin/pmi

If you later on would like to swap back you can use

user@laptop$ sudo dpkg-divert --rename --remove /usr/sbin/pmi

Magento: Redirecting from inside a view

To redirect before headers are started to be sent (for instance from inside a controller) the following works:

Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('portal'));

This will redirect to the page called portal.

However if you try to do this from a view then you will get an error message about headers already having been sent.
If you need to do a redirect form inside a view then using javascript is one way to do this:

echo 'Due to SOMEREASON you are being redirected <a herf="' . Mage::getUrl('portal') . '">Please click here if nothing happens</a>';
		echo '<script type="text/javascript">
		<!--
		window.location = "' . Mage::getUrl('portal') . '"
		//-->
		</script>';

However a tip is to reconsider if you really need to do the redirect from the view, doing it from the controller is a lot easier and quicker.

Magento: Loading a store by store_id

Sometimes there is a need to load a store from the store id, this is not hard to do once you find the right function.

Mage::getModel('core/store')->load($storeId); //a store object

A store object might look like this. Each of the data has a get and a set fucntion (so getIsActive() returns if the store is active or not)

 
 
Mage_Core_Model_Store Object
(
    [_cacheTag:protected] => 1
    [_eventPrefix:protected] => store
    [_eventObject:protected] => store
    [_priceFilter:protected] => 
    [_website:protected] => 
    [_group:protected] => 
    [_configCache:protected] => 
    [_configCacheBaseNodes:protected] => Array
        (
            [0] => catalog/price/scope
            [1] => web/secure/base_url
            [2] => web/secure/use_in_adminhtml
            [3] => web/secure/use_in_frontend
            [4] => web/url/use_store
            [5] => web/unsecure/base_url
            [6] => web/seo/use_rewrites
            [7] => web/unsecure/base_link_url
            [8] => web/secure/base_link_url
            [9] => general/locale/code
        )
 
    [_dirCache:protected] => Array()
 
    [_urlCache:protected] => Array()
 
    [_baseUrlCache:protected] => Array()
 
    [_session:protected] => 
    [_isAdminSecure:protected] => 
    [_isFrontSecure:protected] => 
    [_isReadOnly:private] => 
    [_resourceName:protected] => core/store
    [_resource:protected] => 
    [_resourceCollectionName:protected] => core/store_collection
    [_dataSaveAllowed:protected] => 1
    [_data:protected] => Array
        (
            [store_id] => 64
            [code] => mystore
            [website_id] => 1
            [group_id] => 25
            [name] => mystore
            [sort_order] => 0
            [is_active] => 1
        )
 
    [_origData:protected] => Array
        (
            [store_id] => 64
            [code] => mystore
            [website_id] => 1
            [group_id] => 25
            [name] => mystore
            [sort_order] => 0
            [is_active] => 1
        )
 
    [_idFieldName:protected] => 
    [_isDeleted:protected] => 
)

An an example code to find if the store with id 5 is active or not

if (Mage::getModel('core/store')->load($storeId)->getIsActive())
    //do something if we are active
else
    //do something else if the store is not active

.forward to several recipients

On *nix environments it is possible to forward the mail to one account to another user (or another email address if external mail is supported).
This is done by adding the recipient in a file called .forward in the home directory.

Example To simply forward to another address:

user@server$ echo recipient@mail.com > ~/.forward
user@server$ chmod go-w ~/.forward

In order to forward to several addresses coma (,) is used as a delimiter between the different recipients.

Example To forward to two addresses at the same time:

user@server$ echo recipient1@mail.com,recipient2@mail.com > ~/.forward
user@server$ chmod go-w ~/.forward

Executing rsync as sudo

A quick way to run rsync (via sudo on the remote server) is to use the –rsync-path option (asuming that sudo is configured to run without a password) also verify that you have !tty_tickets in your sudoers file.

rsync -avz --rsync-path='sudo rsync' user@server:/dir/to/backup /destination/directory/

To do this interactivly (without being able to do it password less)

user@bacup$ stty -echo;ssh user@server sudo -v; stty echo
user@bacup$ rsync -avz --rsync-path='sudo rsync' user@server:/dir/to/backup /destination/directory/

(Or as one line)

stty -echo; rsync -azv --rsync-path='sudo rsync' user@server:/dir/to/backup /destination/directory/; stty echo

“stty -echo” turns off input echo (so that your password will not be shown as you type it)
“stty echo” turns the input echo back on (so you can see what you are doing)