Category Archives: Miscellaneous

Cannot Delete Files / Folders In Linux

A folder/file is not getting deleted? Have changed the permission and ownership but still cannot delete?

This can be due to the immutable flag set.
(Specially In ISPConfig when a folder is created for a site or when folder is mapped to a FTP User, ISPConfig itself sets the immutable flag)

To delete a folder or file which has the immutable flag set, try the following

chattr -i filename/foldername
rm -f filename / rm -f -r foldername

A folder still is not getting deleted? Try

cd foldername
chattr -i .
chattr -i ..
cd ..
rm -f -r foldername

 

 

Prevent CRON from overlapping

To solve situations where CRON starts another copy before the current one finishes.

At the top of the CRON job use:

//check if already running
$buff = file_get_contents("cron_stat.txt");
if($buff == 1)
{
    die();
}
//set a running flag;
file_put_contents("cron_stat.txt","1");
......
......
......

When the CRON will run for the first time “cron_stat.txt” will be blank so a 1 will be written in “cron_stat.txt” indicating that the CRON is running and the script will proceed with its work.
When the CRON will start again (second time onwards) it will check “cron_stat.txt” and if it finds there is a 1 in it the script it will exit. And if it finds a 0 then it will proceed.

At the end of the file use:

file_put_contents("cron_stat.txt","0");

This will write a 0 in the “cron_stat.txt” file indicating the CRON is not running.

So in short at the start of the script set 1 in a flag indicating the CRON is running and at the end of the script set the flag to 0 indicating CRON has finished.

There are ways to implement this through Linux commands, but didn’t work for me.

Distance between two Latitudes and Longitudes – Part 3

This series is about code in various languages that will calculate the distance between two latitudes and longitudes. This article is for the C code.

Code courtsey : GeoDataSource

/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::                                                             :*/
/*::  This routine calculates the distance between two           :*/
/*::  points (given the latitude/longitude of those points).     :*/
/*::  It is being used to calculate the distance between two     :*/
/*::  locations using GeoDataSource(TM) Products                 :*/
/*::                                                             :*/
/*::  Definitions:                                               :*/
/*::    South latitudes are negative,                            :*/
/*::    east longitudes are positive                             :*/
/*::                                                             :*/
/*::  Passed to function:                                        :*/
/*::    lat1, lon1 = Latitude and Longitude of point 1           :*/
/*::                 (in decimal degrees)                        :*/
/*::    lat2, lon2 = Latitude and Longitude of point 2           :*/
/*::                 (in decimal degrees)                        :*/
/*::    unit = the unit you desire for results                   :*/
/*::           where: 'M' is statute miles                       :*/
/*::                  'K' is kilometers (default)                :*/
/*::                  'N' is nautical miles                      :*/
/*::  Worldwide cities and other features databases with         :*/
/*::  latitude longitude are available at                        :*/
/*::  http://www.geodatasource.com                               :*/ 
/*::                                                             :*/
/*::  For enquiries, please contact [email protected]      :*/
/*::                                                             :*/
/*::  Official Web site: http://www.geodatasource.com            :*/
/*::                                                             :*/
/*::         GeoDataSource.com (C) All Rights Reserved 2014      :*/
/*::                                                             :*/
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/

#include 

#define pi 3.14159265358979323846

double distance(double lat1, double lon1, double lat2, double lon2, char unit) 
{
  double theta, dist;
  theta = lon1 - lon2;

  dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta));

  dist = acos(dist);
  dist = rad2deg(dist);
  dist = dist * 60 * 1.1515;
  switch(unit) 
 {
    case 'M':
      break;
    case 'K':
      dist = dist * 1.609344;
      break;
    case 'N':
      dist = dist * 0.8684;
      break;
  }
  return (dist);
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::  This function converts decimal degrees to radians             :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
double deg2rad(double deg) {
  return (deg * pi / 180);
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::  This function converts radians to decimal degrees             :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
double rad2deg(double rad) {
  return (rad * 180 / pi);
}

printf("%f",distance(32.9697, -96.80322, 29.46786, -98.53506, "K"));
puts(" Kilometers");

Distance between two Latitudes and Longitudes – Part 2

This series is about code in various languages that will calculate the distance between two latitudes and longitudes. This article is for the Javascript code.

Code courtsey : GeoDataSource

/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::                                                             :*/
/*::  This routine calculates the distance between two           :*/
/*::  points (given the latitude/longitude of those points).     :*/
/*::  It is being used to calculate the distance between two     :*/
/*::  locations using GeoDataSource(TM) Products                 :*/
/*::                                                             :*/
/*::  Definitions:                                               :*/
/*::    South latitudes are negative,                            :*/
/*::    east longitudes are positive                             :*/
/*::                                                             :*/
/*::  Passed to function:                                        :*/
/*::    lat1, lon1 = Latitude and Longitude of point 1           :*/
/*::                 (in decimal degrees)                        :*/
/*::    lat2, lon2 = Latitude and Longitude of point 2           :*/
/*::                 (in decimal degrees)                        :*/
/*::    unit = the unit you desire for results                   :*/
/*::           where: 'M' is statute miles                       :*/
/*::                  'K' is kilometers (default)                :*/
/*::                  'N' is nautical miles                      :*/
/*::  Worldwide cities and other features databases with         :*/
/*::  latitude longitude are available at                        :*/
/*::  http://www.geodatasource.com                               :*/ 
/*::                                                             :*/
/*::  For enquiries, please contact [email protected]      :*/
/*::                                                             :*/
/*::  Official Web site: http://www.geodatasource.com            :*/
/*::                                                             :*/
/*::         GeoDataSource.com (C) All Rights Reserved 2014      :*/
/*::                                                             :*/
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/

function distance(lat1, lon1, lat2, lon2, unit) 
{
	var radlat1 = Math.PI * lat1/180;
	var radlat2 = Math.PI * lat2/180;
	var radlon1 = Math.PI * lon1/180;
	var radlon2 = Math.PI * lon2/180;
	var theta = lon1-lon2;
	var radtheta = Math.PI * theta/180;

	var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);

	dist = Math.acos(dist);
	dist = dist * 180/Math.PI;
	dist = dist * 60 * 1.1515;
	if (unit=="K") { dist = dist * 1.609344 }
	if (unit=="N") { dist = dist * 0.8684 }
	return dist;
}        

document.write(distance(32.9697, -96.80322, 29.46786, -98.53506, "K") + " Kilometers");
          

Database of All Countries

Here is a MySQL dump of all the countries in one table. It is a pretty handy database with 2 char country codes, 3 char country codes and latitude and longitude (somewhat in the middle area) of each countries. Below are the columns of the table.
Country Table Structure

And below is a sample of the data stored in each row.
country-row-sample

Here is the link for downloading the dump file country.sql
Let me know if other formats like CSV or XML will be more helpful.

Secure Upload Folders

There are two ways to prevent execution of any (malicious) scripts uploaded to by users.
Method one – like described in this post.
Method two – add the below code to .htaccess file of the directory that needs to be protected.

RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo .sh .html .shtml .jsp

If it doesn’t work for PHP scripts on servers where suPHP is enabled, then see this post

Throw scripts as text instead of executing it

To throw PHP or other scripts as text instead of executing it, add the below code to .htacess file.

AddType text/plain .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo .sh .html .shtml .jsp

This method can be used to execute .html files as .php (or for executing any other type of file as some other type).

This requires mod_mime module of Apache to be installed and enabled.

There might be problem if su_php is enabled. In case the above doesn’t work for PHP scripts when suPHP is enabled then try adding the following to Apache Directives (main is the highlighted code).

<Directory /var/www/domain.com/files/images/>
        suPHP_Engine off
 </Directory>

It is not a good idea to disable suPHP for the whole site, instead disable only for the required directory.