Sunday, February 24, 2013

Magento : Changing the Admin URL


We can improve security by changing the default URL from 'admin' to something more obscure, such as 'bdadmin'. This will decrease the probability that a malicious user will hit upon your admin log-in page by guessing that the admin site is located at domain.com/admin/, or that automated scripts scanning for Magento admin pages will find it.
To change the admin address you first need to stop Apache and clear the cache:
root# /etc/init.d/apache2 stop
root# rm -rf /var/www/var/cache/*
root# rm -rf /var/www/var/session/*
Then open up the /var/www/app/etc/local.xml file, locate the <frontName> tag, and change the 'admin' part it to something a lot more random,
eg:  <frontName><![CDATA[gJpf2VK5]]></frontName>
Then set Apache running again (bearing in mind your site will be down for the duration of this operation!):
root# /etc/init.d/apache2 start

Saturday, February 23, 2013

Magento: Get the list of events from magento


Use following code in action of magento
$eventAreas = array('global','frontend','adminhtml');
foreach ($eventAreas as $eventArea) {
    $eventConfig = Mage::app()->getConfig()
                       ->getNode(sprintf('%s/events', $eventArea));
    foreach($eventConfig->children() as $key=>$value)
    {
        foreach($value->observers->children() as $key1=>$value1)
        {
            $observer_method = array((string)$eventArea,
            (string)$key,
            (string)$key1,
            (string)Mage::app()->getConfig()
                        ->getModelClassName($value1->class),
            (string)$value1->method);
             echo '<pre>';
             print_r($observer_method);
             echo '</pre>';
        }
    }
}

Magento : Add SEO content in footer

There are following unique pages as category, product and cms pages.
Here, we will create an attribute for these pages as
1.CMS Page

<?php
$installer = $this;
$installer->startSetup();
$installer->run('
    ALTER TABLE '. $this->getTable("cms_page") .
         ' ADD column seo_footer_cms_content text NULL;
');
$installer->endSetup();
?>
2.Category Page


<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_category', 'seo_footer_category_content', array(
                        'type'              => 'text',
                        'backend'           => '',
                        'frontend'          => '',
                        'label'             => 'SEO Footer Content',
                        'input'             => 'textarea',
                        'class'             => '',
                        'source'            => '',
                        'global'            => 0,
                        'visible'           => 1,
                        'required'          => 0,
                        'user_defined'      => 1,
                        'default'           => '',
                        'searchable'        => 0,
                        'filterable'        => 0,
                        'comparable'        => 0,
                        'visible_on_front'  => 0,
                        'unique'            => 0,
                        'position'          => 1,
                    ));
$installer->endSetup();
?>
3.Product page

<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'seo_footer_product_content', array(
                        'type'              => 'text',
                        'backend'           => '',
                        'frontend'          => '',
                        'label'             => 'SEO Footer Content',
                        'input'             => 'textarea',
                        'class'             => '',
                        'source'            => '',
                        'global'            => 0,
                        'visible'           => 1,
                        'required'          => 0,
                        'user_defined'      => 1,
                        'default'           => '',
                        'searchable'        => 0,
                        'filterable'        => 0,
                        'comparable'        => 0,
                        'visible_on_front'  => 0,
                        'unique'            => 0,
                        'position'          => 1,
                    ));                
$installer->endSetup();
?>

Now you can see corresponding attribute of product and category in admin.
But for cms page we have to add following code (use adminhtml_cms_page_edit_tab_meta_prepare_form event). Lets add below code in config.xml

<adminhtml>
    <events>
        <adminhtml_cms_page_edit_tab_meta_prepare_form>
            <observers>
                <bd_cms_page_edit_tab_meta>
                    <type>singleton</type>
                    <class>Bd_Attributes_Model_Observer</class>
                    <method>createCMSAttribute</method>
                </bd_cms_page_edit_tab_meta>
            </observers>
        </adminhtml_cms_page_edit_tab_meta_prepare_form>
    </events>
</adminhtml>
now create createCMSAttribute function in Bd/Attribures/Model/Observer.php file

public function createCMSAttribue($observer)
{
    $form = $observer->getEvent()->getForm();
    $fieldset = $form->getElement('meta_fieldset');
    $fieldset->addField('seo_footer_cms_conten', 'editor', array(
        'name' => 'seo_footer_cms_content',
        'label' => Mage::helper('cms')->__('SEO Footer Content'),
        'title' => Mage::helper('cms')->__('SEO Footer Content'),
        'style'     => '',
        'wysiwyg'   => false,
        'disabled'  => 0
    ));
}

To show the content in the footer we need to add the following code in footer.phtml

<?php
   $routeName=Mage::app()->getFrontController()->getRequest()->getRouteName();
   $currentCategory = Mage::registry('current_category');
   $currentProduct = Mage::registry('current_product');
   if ($currentProduct):
    echo $currentProduct->getSeoFooterProductContent();
   elseif ($currentCategory):
    echo $currentCategory->getSeoFooterCategoryContent();
   elseif( $routeName == 'cms'):
    $page = Mage::getSingleton('cms/page');  
    echo $page->getSeoFooterCmsContent();
  endif;
  ?>










Tuesday, February 12, 2013

How to completely uninstall and reinstall Firefox?

You can reinstall firefox with the following command:

sudo apt-get install --reinstall firefox

Sunday, January 27, 2013

How to Create CAPTCHA Protection using PHP

there are following stpes
1. html code
<img src="/captcha.php/?rand=0.608989860869691" id="captcha_image"/>
<a href="javascript:void(0);" onClick="refreshcaptcha();" >Refresh</a>
<input type="text" name="norobot" id="norobot" value="Enter Image Code" />

2. js
function refreshcaptcha(){
    var capimage = document.getElementById('captcha_image').src;  
    var full_img=capimage.split('rand=');  
    document.getElementById('captcha_image').setAttribute('src', full_img[0]+'rand='+Math.random());
}

3.captcha.php
$randomnr = rand(1000, 9999);
$session["randomnr"] = md5($randomnr));
   
   
    $im = imagecreatetruecolor(100, 38);

    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 150, 150, 150);
    $black = imagecolorallocate($im, 0, 0, 0);

    imagefilledrectangle($im, 0, 0, 200, 35, $black);

    //path to font - this is just an example you can use any font you like:
   
    $font =  '/font/karate/Karate.ttf';

    imagettftext($im, 20, 4, 22, 30, $grey, $font, $randomnr);

    imagettftext($im, 20, 4, 15, 32, $white, $font, $randomnr);
    
    //die(111);
    //prevent caching on client side:
    header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");

    header ("Content-type: image/gif");
    imagegif($im);
    imagedestroy($im);
    exit;

4. On post action file
if (md5($data['norobot']) == $session["randomnr"])    {
                // here you  place code to be executed if the captcha test passes
 }    else { 
                // here you  place code to be executed if the captcha test fails
                    echo "Please enter correct code.";
                    return;
}


Thursday, January 24, 2013

Magento : Create an attribute for customer mobile number

Consider package : Bd_Customermobile

1. Bd_Customermobile.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Bd_Customermobile>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Bd_Customermobile>
  </modules>
</config>

2. config.xml
<?xml version="1.0"?>
<config>
  <modules>
    <Bd_Customermobile>
      <version>0.1.0</version>
    </Bd_Customermobile>
  </modules>
  <global>
    <helpers>
      <customermobile>
        <class>Bd_Customermobile_Helper</class>
      </customermobile>
    </helpers>
    <models>
      <customermobile>
        <class>Bd_Customermobile_Model</class>
        <resourceModel>customermobile_mysql4</resourceModel>
      </customermobile>
    </models>
    <resources>
      <customerattribute_setup>
        <setup>
          <module>Bd_Customermobile</module>
          <class>Mage_Customer_Model_Entity_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </customerattribute_setup>
      <customerattribute_write>
        <connection>
          <use>core_write</use>
        </connection>
      </customerattribute_write>
      <customerattribute_read>
        <connection>
          <use>core_read</use>
        </connection>
      </customerattribute_read>
    </resources>
  </global>
</config>

3. mysql4-install-0.1.0.php
<?php
$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "bd_customer_mobile",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Customer Mobile",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => "Additional attribute for mobile number"

    ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "bd_customer_mobile");

       
$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
        $attribute->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100)
        ;
        $attribute->save();
   
   
   
$installer->endSetup();
    

Tuesday, January 15, 2013

Magento: Mobile number validation using custom class

HTML code

<input type="text" name="telephone" id="telephone" value="<?php echo $this->htmlEscape($this->getFormData()->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text validate-custommobile" />



Js code

<script type="text/javascript">
    //<![CDATA[

if(Validation) {
        Validation.addAllThese([
        ['validate-custommobile','Enter correct mobile number',
        function(v){
        var timePat ="^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}9[0-9](\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$";
        // var matchArray = v.match(timePat);
        if(v.length > 0){
        if(v.length !=10){
            return false;
           }else if(v[0] != 9 || v[1] != 1 ){

            //return false;
           }else if(v[2]!=9 && v[2]!=8 && v[2]!=7){

            return false;
           }


        return true;

        }else {
        return false;
        }

        }
        ]])};
        var dataForm = new VarienForm('form-id', true);

//]]>

</script>