Zend_Auth and subdomains
I was working today on an user authentication system that redirects the users to their own sub-domain after a successful login. It should work like this: user comes to www.exmple.com, fills in his credentials and after successful login he/she should be redirected to username.example.com. All fine and dandy except the session is not persistent between sub-domains of example.com. After a bit of google and some help from eXcuvator on #zftalk, i found the solution:
Just put the following in your Bootstrap.php and everything should just work:
protected function _initSession()
{
Zend_Session::setOptions( array('cookie_domain' => '.example.com', 'use_only_cookies' => true, 'name' => 'yourSessionName'));
}
First day timestamp of week number in PHP
function mondayTimestamp($weekNumber, $year)
{
$time = strtotime($year . '0104 +' . ($weekNumber - 1) . ' weeks');
// Get the time of the first day of the week
$mondayTime = strtotime('-' . (date('w', $time) - 1) . ' days', $time);
return $mondayTime;
}
Test it as follows:
$mondayTimestamp = mondayTimestamp(46, 2010);
echo date('j F Y', $modayTimestamp);
Setup VPN server and client using OpenVPN
The good thing during the last years is that net access is almost everywhere, and is pretty cheap or even free. A lot of bars, hotels, restaurants offer a free wifi connection, but most of the times that connection is not secured. The biggest security risk with unencrypted connections is the man-in-the-middle type of attack. You are sitting on a bar, checking email, browsing and all your traffic is not encrypted. That means that an attacker can tap into your connection and "listen", intercept everything you are typing. Like that the attacker can get sensitive data (accounts, passwords, credit card numbers etc.)
Since you don't have any control over the connection, what one can do to protect himself is to use a VPN. With a VPN, you are creating a secure (encrypted) point-to-point connection between your PC and the VPN server. Translated, that means that all the Internet traffic you are doing, is going through that secure channel, which can't be hacked that easily.
Here us how to setup a vpn server using openVPN
The server
1. Install openvpn and openssl
sudo apt-get install openvpn libssl-dev openssl
2. Configurations
cd /etc/openvpn/ cp -r /usr/share/doc/openvpn/examples/easy-rsa/2.0/* /etc/openvpn/easy-rsa/
3. Create server certificates
cd /etc/openvpn/easy-rsa/ source vars ./clean-all ./build-dh ./pkitool --initca ./pkitool --server server cd keys openvpn --genkey --secret ta.key cp server.crt server.key ca.crt dh1024.pem ta.key /etc/openvpn/
4. Create client certificates
cd /etc/openvpn/easy-rsa/ source vars ./pkitool hostname
For each new client that connects to the VPN you'll need to create new client certificates using step 4
5. Create server configuration file:
cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/ gzip -d /etc/openvpn/server.conf.gz
After editing your file should look like this:
dev tun proto tcp port 1194 ca ca.crt cert server.crt key server.key dh dh1024.pem user nobody group nogroup server 10.10.0.0 255.255.255.0 persist-key persist-tun client-to-client push "redirect-gateway def1" push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 208.67.220.220"
6. Enable routing and MASQUERADE for your VPN by placing the following in your /etc/rc.local
echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -s 10.10.0.0/24 -o eth0 -j MASQUERADE
7. Start the server
/etc/rc.local /etc/init.d/openvpn restart
The Client
1. Ubuntu (all modern linux?)
I'm using an Ubuntu machine as a client. To use openvpn in Ubuntu just install the openvpn plugin for NetworkManager:
sudo apt-get install network-manager-openvpn
A reboot is recommended.
You can now go and add your connection in Network Manager
2. Windows - to come
3. Mac OS X - to come
Fix Zend Framework 404 routes on HTTPS
I've been working on a web project that required some of the pages to be loaded via HTTPS.
I've got the certificate, installed accessed homepage via HTTPS and seems to work. That was not the case for the rest of the routes. After a lot of searching i was able to find a post which proposes a fix for this. Namely, you have to add what's on your app's main .htaccess into your SSL virtualhost.
Add the following just before tag closes and you should be fine:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -s [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -l [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
</IfModule>
Another one of my requirements was to redirect some pages to HTTPS (ie a login page). To do that add the following to your .htaccess:
RewriteCond %{HTTPS} !=on
RewriteRule ^login(.*) https://%{SERVER_NAME}/login$1 [R,L]
Wipe all categories from magento database
Below is a small sql snippet that will delete all the categories from a magento database:
TRUNCATE TABLE `catalog_category_entity`; TRUNCATE TABLE `catalog_category_entity_datetime`; TRUNCATE TABLE `catalog_category_entity_decimal`; TRUNCATE TABLE `catalog_category_entity_int`; TRUNCATE TABLE `catalog_category_entity_text`; TRUNCATE TABLE `catalog_category_entity_varchar`; TRUNCATE TABLE `catalog_category_product`; TRUNCATE TABLE `catalog_category_product_index`; insert into `catalog_category_entity`(`entity_id`,`entity_type_id`,`attribute_set_id`,`parent_id`,`created_at`,`updated_at`,`path`,`position`,`level`,`children_count`) values (1,3,0,0,'0000-00-00 00:00:00','2009-02-20 00:25:34','1',1,0,1),(2,3,3,0,'2009-02-20 00:25:34','2009-02-20 00:25:34','1/2',1,1,0); insert into `catalog_category_entity_int`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) values (1,3,32,0,2,1),(2,3,32,1,2,1); insert into `catalog_category_entity_varchar`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) values (1,3,31,0,1,'Root Catalog'),(2,3,33,0,1,'root-catalog'),(3,3,31,0,2,'Default Category'),(4,3,39,0,2,'PRODUCTS'),(5,3,33,0,2,'default-category');
Wipe all products from magento database
Here's a sql snippet that deletes all the products from a mysql database:
TRUNCATE TABLE `catalog_product_bundle_option`; TRUNCATE TABLE `catalog_product_bundle_option_value`; TRUNCATE TABLE `catalog_product_bundle_selection`; TRUNCATE TABLE `catalog_product_entity_datetime`; TRUNCATE TABLE `catalog_product_entity_decimal`; TRUNCATE TABLE `catalog_product_entity_gallery`; TRUNCATE TABLE `catalog_product_entity_int`; TRUNCATE TABLE `catalog_product_entity_media_gallery`; TRUNCATE TABLE `catalog_product_entity_media_gallery_value`; TRUNCATE TABLE `catalog_product_entity_text`; TRUNCATE TABLE `catalog_product_entity_tier_price`; TRUNCATE TABLE `catalog_product_entity_varchar`; TRUNCATE TABLE `catalog_product_link`; TRUNCATE TABLE `catalog_product_link_attribute`; TRUNCATE TABLE `catalog_product_link_attribute_decimal`; TRUNCATE TABLE `catalog_product_link_attribute_int`; TRUNCATE TABLE `catalog_product_link_attribute_varchar`; TRUNCATE TABLE `catalog_product_link_type`; TRUNCATE TABLE `catalog_product_option`; TRUNCATE TABLE `catalog_product_option_price`; TRUNCATE TABLE `catalog_product_option_title`; TRUNCATE TABLE `catalog_product_option_type_price`; TRUNCATE TABLE `catalog_product_option_type_title`; TRUNCATE TABLE `catalog_product_option_type_value`; TRUNCATE TABLE `catalog_product_super_attribute`; TRUNCATE TABLE `catalog_product_super_attribute_label`; TRUNCATE TABLE `catalog_product_super_attribute_pricing`; TRUNCATE TABLE `catalog_product_super_link`; TRUNCATE TABLE `catalog_product_enabled_index`; TRUNCATE TABLE `catalog_product_website`; TRUNCATE TABLE `catalog_product_entity`; TRUNCATE TABLE `cataloginventory_stock`; TRUNCATE TABLE `cataloginventory_stock_item`; TRUNCATE TABLE `cataloginventory_stock_status`; insert into `catalog_product_link_type`(`link_type_id`,`code`) values (1,'relation'),(2,'bundle'),(3,'super'),(4,'up_sell'),(5,'cross_sell'); insert into `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) values (1,2,'qty','decimal'),(2,1,'position','int'),(3,4,'position','int'),(4,5,'position','int'),(6,1,'qty','decimal'),(7,3,'position','int'),(8,3,'qty','decimal'); insert into `cataloginventory_stock`(`stock_id`,`stock_name`) values (1,'Default');
CD Emulation in Linux
In Linux, most of the time you don't need any special tool to mount an image. You can just mount an .iso file using the mount tool:
sudo mount -oloop Image.iso /mnt
Unfortunately if you want to play a game that needs the CD inside the drive, mounting using the mount tool won't make the CD available for the game (unless it didn't worked for me). So i looked around and found that CDEmu can do the job. To install on Ubuntu add the repos from ppa:cdemu/ppa them install:
sudo add-apt-repository ppa:cdemu/ppa sudo apt-get update sudo apt-get install gcdemu
This will install and applet which you can use with your Gnome panel, and all the other libs that it needs. In order for the applet to work you need to start the cdemud daemon, which depends on the vhba kernel module. To start those run:
sudo modprobe vhba; cdemud &
Now you can mount images using your CDEmu applet
Fix php base64_decode error
If the base64_decode() returns weird results, you might try to decode a string which is not base64 valid. The most common problem seems to be + being replaced with a space (" ") when sending the encoded string via $_POST or $_GET.
To fix that simply do a str_replace on the encoded string, then try to decode it:
$encodedMessages = str_replace(' ', '+', $encodedMessages);
echo base64_decode($encodedMessages);
Hope this will help someone, it took me several hours to figure it out.
Install Adobe® Flash® Player “Square” on Linux
Adobe® Flash® Player "Square" is a preview release that enables native 64-bit support on Linux, Mac OS, and Windows operating systems, as well as enhanced support for Microsoft Internet Explorer 9 beta.
I'm glad to see Adobe has is finally embracing the way it should the 64 bit Platform. This release is a milestone as far as i know, as is the first one that's released in the same time for win, mac and linux. I've put together a small tutorial on
how to install adobe flash player square on linux:
#!/bin/bash # Script created by # Romeo-Adrian Cioaba romeo.cioaba@spotonearth.com echo "Stopping any Firefox that might be running" sudo killall -9 firefox echo "Removing any other flash plugin previously installed:" sudo apt-get remove -y --purge flashplugin-nonfree gnash gnash-common mozilla-plugin-gnash swfdec-mozilla libflashsupport nspluginwrapper sudo rm -f /usr/lib/mozilla/plugins/*flash* sudo rm -f ~/.mozilla/plugins/*flash* sudo rm -f /usr/lib/firefox/plugins/*flash* sudo rm -f /usr/lib/firefox-addons/plugins/*flash* sudo rm -rfd /usr/lib/nspluginwrapper echo "Installing Flash Player Square" cd ~ # 64 bit wget http://download.macromedia.com/pub/labs/flashplayer10/flashplayer_square_p1_64bit_linux_091510.tar.gz tar zxvf flashplayer_square_p1_64bit_linux_091510.tar.gz # 32 bit # wget http://download.macromedia.com/pub/labs/flashplayer10/flashplayer_square_p1_32bit_linux_091510.tar.gz # tar zxvf flashplayer_square_p1_32bit_linux_091510.tar.gz sudo cp libflashplayer.so /usr/lib/mozilla/plugins/ echo "Linking the libraries so Firefox and apps depending on XULRunner (vuze, liferea, rsswol) can find it." sudo ln -sf /usr/lib/mozilla/plugins/libflashplayer.so /usr/lib/firefox-addons/plugins/ sudo ln -sf /usr/lib/mozilla/plugins/libflashplayer.so /usr/lib/xulrunner-addons/plugins/ # now doing some cleaning up: sudo rm -rf libflashplayer.so sudo rm -rf flashplayer_square_p1_64bit_linux_091510.tar.gz
To check if the install did it's job, you need to check about:plugins in your address bar. It should show you this for flash player:
Shockwave Flash
- File: libflashplayer.so
- Version:
- Shockwave Flash 10.2 d161
| MIME Type | Description | Suffixes | Enabled |
|---|---|---|---|
| application/x-shockwave-flash | Shockwave Flash | swf | Yes |
| application/futuresplash | FutureSplash Player | spl | Yes |
As usual, I've wrapped everything mentioned before into a script to easily install flash player square on Linux
If this version doesn't work out that well for you, you can still check my other tutorial on how to install native 64bit flash player on Linux
Happy browsing!
Magento create custom order statuses
This post is just a reminder for myself. It's taken from http://www.magentocommerce.com/boards/viewthread/9976/P30/ . The code below was tested and works just fine on magento 1.4.0.1
So to recapitulate, I did the following (citing the other guys, thanks to atlasit, ajayksh and rune00):
1) write a list of the additional statuses that you want
2) download and open the file Magento\app\code\core\Mage\Sales\etc\config.xml
(do not upload it again as soon as you’re done, you need to place it into a different folder, see below)3) find this code:
<pending translate="label"><label>Pending</label></pending>and after that add your own new custom status
<my_new_custom_status translate="label"><label>My New Custom Status</label></my_new_custom_status>4) Now find this code, a few lines below:
<states> <new translate="label"> <label>New</label> <statuses> <pending/> </statuses> </new>and under or above this block create a new block with your own custom status:
<states> <new translate="label"> <label>New</label> <statuses> <pending/> </statuses> </new> <my_new_custom_status translate="label"> <label>My New Custom Status</label> <statuses> <my_new_custom_status/> </statuses> </my_new_custom_status>repeat this (step 3 and 4) for each new custom status
5) So that you can change your status from the admin interface back and forth
from any current status, you need to edit again each of the status code block
as follows.change this
<new translate="label"> <label>New</label> <statuses> <pending/> </statuses> </new>to this
<new translate="label"> <label>New</label> <statuses> <my_new_custom_status/> <pending/> <processing/> <holded/> <complete/> <closed/> <canceled/> </statuses> <visible_on_front/> </new>you need to do this with all of the existing statuses and the new custom statuses that you’ve just created
so that these new statuses do not only become available and visible to the admin, but also to the customer
on his “my account” page (order history, current status), you need to make the new statues visible (thanks to rune00)
with the tag, as described above. 6) After you saved your changes, upload the new config.xml file to
/app/code/local/Mage/Sales/etc/ (you will need to create that folder structure)7)Create a file named Mage_Sales.xml with the following content (thanks to atlasit)
<?xml version="1.0"?> <config> <modules> <Mage_Sales> <active>true</active> <codePool>local</codePool> </Mage_Sales> </modules> </config>Upload it to /app/etc/modules/
that’s pretty much it.
If you got it running successfully as well, let us know so that it will help others as well.