Linux commands

// to rename filename
mv old-filename new-filename

// current working directory
pwd

// running process
top

// switch user
sudo su

// restart apache
service apache2 restart

// folder size
du -csh /dir-name/
du -sh /dir-name/

// show only dir
ls -ld — */
ls -d */

// create zip file
tar -czvf zip_file_name.tgz public_html/

// unzip file
tar -zxvf zip_file_name.tgz

// backup database
mysqldump -uUSERNAME -pPASSWORD database_name > file_name.sql

// restore backup
mysql -uUSERNAME -pPASSWORD database_name < file_name.sql

// change file or dir permission recursively
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;

// Change file/dir owner
chown -hR root:root public_html/

// download file
wget http://domain.com/filename

// delete file
rm -rf var/cache/*
rm -rf var/session/*
rm -rf var/report/*

Magento get controller / module / action / cms page name

/* get Controller/module/action name */

$controller = Mage::app()->getRequest()->getControllerName();
$module = Mage::app()->getRequest()->getModuleName();
$action = Mage::app()->getRequest()->getActionName();
$layout = $this->getLayout()->getBlock('root')->getTemplate();

/* get page name/indentifier */

$pageTitle = Mage::getSingleton('cms/page')->getIdentifier(); 
$pageTitle = Mage::getModel('cms/page')->load('about-page', 'identifier')->getTitle();

/* Magento get front controller name */

$frontController = Mage::app()->getFrontController()->getRequest()->getRouteName();

Scan directory recursively and search text in files

<?php

die('-1');

if (!isset($_GET['code']) || $_GET['code']!='chkapmlp') {
 die('error');
}

if (isset($_POST['s']) && $_POST['s']!='' ) {
 
 define('DS', DIRECTORY_SEPARATOR);
 define('SEARCH', $_POST['s']);

//$base_path = 'D:\wamp\www\wp';
 $base_path = dirname(__FILE__);

function txtSrc($a, $b)
 {

//header('Content-Type: text/plain');
 //echo $a.'==='.$b; exit;
 $output = '';

$contents = file_get_contents($a);
 $pattern = preg_quote($b, '/');
 $pattern = "/^.*$pattern.*\$/m";
 if(preg_match_all($pattern, $contents, $matches)){
 $output .= "Found matches:\n";
 $output .= htmlentities(implode("\n", $matches[0]));
 }
 else{
 //echo "No matches found";
 } 
 return $output;
 }

function dirItr($cpath='')
 {
 $allow_ext = array('php','html');

$allow_dir = array('dir1','dir2');

$directoryIterator = new DirectoryIterator($cpath);
 foreach ($directoryIterator as $fileInfo) {
 if($fileInfo->isDot()) continue;

if($fileInfo->isFile()) {
 $fullpath = $fileInfo->getPath().DS.$fileInfo->getFilename();
 
 if (in_array($fileInfo->getExtension(), $allow_ext) &&
 '/chroot/home/augustap/augustapreciousmetals.com/html' != $fileInfo->getPath()
 ) {
 $output = txtSrc($fullpath, SEARCH);
 if ($output!='') {
 echo $output . "<br />";
 //echo "<b>".$fileInfo->getPathname() . "</b><br />";
 echo "<b>".$fullpath . "</b><br /><br />";
 } 
 }
 }
 elseif($fileInfo->isDir()) {
 if (in_array($fileInfo->getFilename(), $allow_dir)) {
 //echo '<b>';
 //echo $fileInfo->getPath().DS.$fileInfo->getFilename() . "<br>\n";
 //echo '</b>';
 dirItr($fileInfo->getPath().DS.$fileInfo->getFilename()); 
 }
 
 }
 }
 }
 dirItr($base_path);
 echo 'Done!<br />';

}
?>
<!DOCTYPE html>
<html>
<head>
 <title>Search Text</title>
</head>
<body>
 <form action="" method="post">
 <input type="text" name="s" value="<?php echo (isset($_POST['s'])) ? $_POST['s'] : '' ;?>">
 <!-- <input type="text" name=""> -->
 <input type="submit" name="submit" value="Submit">
 </form>
</body>
</html>

CURL – Get HTTP Status code

$baseUrl = 'https://www.domain.com/';

function checkHttpStatusCode($url='')
{
 global $baseUrl;
 $url = $baseUrl.$url.'/';
 //echo "{$url}<br>";


 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
 curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch, CURLOPT_TIMEOUT,10);
 $output = curl_exec($ch);
 $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 curl_close($ch);

//echo 'HTTP code: ' . $httpcode;
 return $httpcode;
}

$listDir = array('dir1','dir2');


foreach ($listDir as $key => $value) {
 $code = checkHttpStatusCode($value);
 if ($code == '200') {
 //echo $code.'=>'.$value.'<br>';
 echo $baseUrl.$value.'/<br>';
 }
}

Skype : Sorry, we couldn’t connect to skype

Recently in my laptop and i had an problem with skype. It shows following message,

Sorry, we couldn’t connect to skype

and here is the solution for this problem,

  • open internet explorer browser
  • click “tools” -> “internet options” (if you don’t see the menu bar, then right click near tab and check “menu bar”)
  • click “advance” then click “reset” button
  • save and close internet explorer.

that’s it, now completely quit from skype and start again, hope you can login now 🙂

 

 

PHP – generate random code

The following php function code will generate random code , you have to call the function name with length for the code 🙂

function generateRandomCode($length)
{
$chars = "234567890abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$i = 0;
$url = "";
while ($i <= $length) {
$url .= $chars{mt_rand(0,strlen($chars))};
$i++;
}
return $url;
}

and to view the out put just call the function like this

echo generateRandomCode(16);

that’s it.