Print a text file to PDF from CLI on Linux
It is nice how modern text editors have that feature to "print" a file to PDF. I wanted to do the same from CLI and after a bit of looking around i've found this solution:
sudo apt-get install enscript enscript lorem.txt -o - | ps2pdf - lorem.pdf
There is also the unoconv, which converts between any document format that OpenOffice understands. It uses OpenOffice's UNO bindings for non-interactive conversion of documents.
Supported document formats include Open Document Format (.odt), MS Word (.doc), MS Office Open/MS OOXML (.xml), Portable Document Format (.pdf), HTML, XHTML, RTF, Docbook (.xml), and more.
Features:
Converts between different document formats that OpenOffice understands
OpenOffice can export to about 100 different document formats
Can be used for batch processing
Combines with asciidoc and docbook2odf/xhtml2odt to create PDF or Word (.doc) files
Can apply custom style templates during conversion (to enforce corporate identity)
Autostarts OpenOffice for processing if necessary
Can be used in a client/server environment to process documents centrally
Can influence OpenOffice filters during import and export
Supports OpenOffice on Linux, Windows and MacOSX
How to: Replace OpenOffice.org with LibreOffice on Ubuntu
Here are a few easy steps to replace openoffice.org with libreoffice on ubuntu:
CLI instructions (these will take care of removing OpenOffice.org and installing LibreOffice):
Add the PPA and install LibreOffice:
sudo add-apt-repository ppa:libreoffice/ppa sudo apt-get update sudo apt-get install libreoffice
For GNOME integration (for people running default Ubuntu):
sudo apt-get install libreoffice-gnome
or for KDE Integration (for people running Kubuntu):
sudo apt-get install libreoffice-kde
As of Ubuntu 11.04, LibreOffice will replace by default OpenOffice.
Fix Firefox 4 beta not displaying images on certain sites.
I've switched for a while to FF4 beta and i've noticed that on certain sites the images don't load properly. They ofcourse load just fine on other browsers (chrome, opera).
Turns out it's a FF4 setting and here's how to fix it:
1. Type “about:config” in the location bar, and press ENTER.
2. In the filter box, type “referer” and press ENTER. This should
leave you with one preference, "network.http.sendRefererHeader". This
is probably set to 0.
3. Right click on network.http.sendRefererHeader and select “Modify”.
4. In the dialog that appears type “2″ and press OK.
5. Restart your browser.
Fix gitk ugly fonts in Ubuntu
I've recently started playing around with git and found out the power of gitk. Gitk is a small tool built using tcl/tk that helps the user visualize the changes he's done in a graphical, easy to follow manner. By default Ubuntu 10.10 ships with tk8.4 installed by default and that makes gitk look really ugly, as tk8.4 doesn't support antialiased fonts. To fix this problem simply install tk8.5 and make it default on your machine:
sudo apt-get install tk8.5 sudo update-alternatives --config wish # a list will show up and you need to type in the number that corresponds to tk8.5 (in my case was 3) then hit enter.
That's it. You can now reopen gitk and see the difference.
Find string in files and show line number with grep
Handy snippet to find some string inside the files in a directory:
grep -r --line-number "string_you_search" path/where/to/look
Note the -r tells grep also to look into all subfolders recursively.
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');