Sunday, November 30, 2008

How to have Undelete on your Linux command line.

Almost all distro now has a Trash to undo your current deleted files when using the GUI file browsers.

When you spend most of your time on the shell you usually use the command "rm" to delete a file and this command doesn't support undelete once you issued the command.

First we need to learn not to use the command 'rm' to delete our files.

In your ~/.bashrc file add the following line:
alias del='mv -t ~/.local/share/Trash/files --backup=t'
Deleting files now is done via
del filename.txt
.

Then we need a script to clean up our trash folder occasionally.
Create a script and name it whatever you want, I named mine "cleantrash" and have the code below inside it.



#!/bin/bash
#
# This will delete all files in the trash directory
# that is older than the specified KEEPDAYS
#
# It can be run using a cron job or manually
#
TRASHDIR=~/.local/share/Trash/files/
KEEPDAYS=14
find $TRASHDIR -mtime +${KEEPDAYS} -exec /bin/rm -f {} \;


Make sure you do a chmod +x cleantrash so we can execute it.

Sunday, November 23, 2008

CakePHP Ajax Login using Auth Component.

Need to update a login controller/view to have AJAX login capability without too much changes to the view and controllers.

Controller

app/controllers/users_controller.php
class UsersController extends AppController {
 var $name = 'Users';
 var $helpers = array('Html', 'Form');
 var $components = array('Auth');

 function beforeFilter() {
   parent::beforeFilter();
   Auth->allow('logout');
 }


 function login(){
   if ($this->Auth->user()) {
     $this->User->id = $this->Auth->user('id');
     $this->User->saveField('last_login', date('Y-m-d H:i:s'));
 
     $this->Session->setFlash(sprintf("Welcome %s!", $this->Auth->user('username')));
     $this->redirect('/');
   }
 }

 function logout() {
   $this->Session->destroy();
   $this->redirect('/');
 }
}

View

views/users/login.ctp
  <div id="login-dialog"> 
  <?php echo $form->create('User', array('action'=>'login'))?>
  <?php flash('Auth.login'); ?>
  <?php echo $form->input('username');?>
  <?php echo $form->input('password', array('type' => 'password'));?>
  <?php echo $form->submit('Login');?>
  <?php echo $html->link('Forgot my password', array('action' => 'forgot_password'));?>
  <?php echo $form->end();?>
  <?php echo $html->link('Register', array('admin' => false, 'action' => 'register'));?>
  </div>


Above code are typical Users controller and view files.
What we need:

  1. Latest Jquery to make life easy. Save it to your app/webroot/js folder.

  2. Form plugin to make dealing with form easy. Save it to your app/webroot/js folder.

  3. JQuery Helper to make using JQuery with CakePHP easy.


Changes we need to implement:

  1. Add the RequestHandler component to our app_controller.php or to the UsersController.
        var $components = array('Auth', 'RequestHandler');


  2. Add the JQuery helper to our helpers.
        var $helpers = array('Html', 'Form', 'Jquery');

    Make sure to download the jquery helper from the link above and put it in your "app/views/helpers/" folder.

  3. Add the code below:

Controller changes:

 function login(){
   if ($this->Auth->user()) {
     $this->User->id = $this->Auth->user('id');
     $this->User->saveField('last_login', date('Y-m-d H:i:s'));
 
     $this->Session->setFlash(sprintf("Welcome %s!", $this->Auth->user('username')));
     $this->redirect('/');
   }

   if ($this->RequestHandler->isAjax()) {
      header('HTTP/1.0 401 Unauthorized');
      die();
   }
 }

View file changes

  <div id="login-dialog"> 
  <?php echo $form->create('User', array('action'=>'login'))?>
  <?php flash('Auth.login'); ?>
  <?php echo $form->input('username');?>
  <?php echo $form->input('password', array('type' => 'password'));?>
  <?php echo $form->submit('Login');?>
  <?php echo $html->link('Forgot my password', array('action' => 'forgot_password'));?>
  <?php echo $form->end();?>
  <?php echo $html->link('Register', array('admin' => false, 'action' => 'register'));?>
  </div>

<?php
 $loginURL = Router::url(array('controller' => 'users', 'action' => 'login'));
 $jquery->uses('jquery.form');
 $jquery->addScript(
 '  var options = {',
 '   url: "'.$loginURL.'",',
 '   success: function(data, statusText){',
 '     window.location = "/";',
 '    },',
 '   error: function(XMLHttpRequest, textStatus, errorThrown){',
 '     alert("Invalid Username and Password");',
 '    },',
 '  };',
 '  $("form#UserLoginForm").ajaxForm(options);'
 );
?>


There is no need to make changes to the default layout since the JQuery helper already includes the jquery library in the head of the layout.

The jquery library being loaded by the helper is hard coded so you need to make sure you got the correct jquery version in your /app/webroot/js folder.

The code also redirects to the root folder on successful authentication.


Need proxy? Lowest priced starting at $0.71/proxy. Insane!

Buy Instagram Followers

 TheFollowerShop - Buy Instagram Followers, Instagram Likes, Twitter Followers, Twitter Favorites

Sunday, November 02, 2008

RPM for Everybody

1. Listing content of an RPM file:

rpm -qpl package-version.rpm

2. Unpacking of an RPM archive:

rpm2cpio < myfile.rpm | cpio -i --make-directories

all contents of the RPM package will be extracted into the current directory creating relative path..

3. Installing an RPM package.

rpm -ivh package-version.rpm

4. Upgradinga an RPM package.

rpm -Uvh package-version.rpm

5. Listing installed packaged on your system:

rpm -qai

This will list all packages installed via the RPM system.

6. Uninstalling RPM packages:

rpm -e package-version.rpm

7. Building RPM from source:

@todo