Jan 8, 2013

PHP: shortest and easiest GeoIP (IP to Country)

Without using a database of IP addresses or an "external API" (not completely true), this is the shortest method of determining the country of an IP addresss.

The one-liner issues a shell_exec() to the whois command and parses the two letter country code from it.

if(preg_match('/^(\d+\.?){4}$/',$ip)) {
  $country = trim(shell_exec('whois '.$ip.' -H | grep country | awk \'{print $2}\''));
}

The preg_match() is of course optional, but for your own good, because as you may know, shell_exec() is VERY DANGEROUS.

Note: This does not work on all versions of WHOIS!

Also note that there is a daily query limit to the RIPE.NET database, so it's good to cache the results. It's not like that information gets changed often.

Jan 4, 2013

Sendmail :: X-Authentication-Warning: user set sender to email using -f

Solving the X-Authentication-Warning header in the emails you send requires quite a few steps.

An example of a typical warning:

X-Authentication-Warning: hostname: www-data set sender to info@example.com using -f






The -f flag in sendmail is used to set the envelope sender address. 
Usually the -f flag is passed in PHP's mail() function as an additional parameter argument. 

It comes in handy to set a valid bounce email address to catch undelivered mail. 





Even the PHP manual states that only the trusted users in the system (listed in /etc/mail/trusted-users) can set the -f flag. 
Each line in the file contains the trusted user name.






Trouble is, that's not enough
.




To tell sendmail to actually include the trusted-users file, you need to modify submit.mc, located in /etc/mail/submit.mc. Just add these two lines at the end of the file:


define(`_USE_CT_FILE_',`1')dnl

define(`confCT_FILE',`/etc/mail/trusted-users')dnl




The last thing you need to do is rebuild the sendmail configuration files by executing the command sendmailconfig as root and restart sendmail.

Read more about SPF records and DKIM, to increase the chances of mail being delivered and not getting flagged as spam.

Dec 13, 2012

Android developer settings (debug mode) in 4.2 Jelly Bean

With the recent update of the Android OS Jelly Bean to 4.2, the developer settings were nowhere to be found.

It turns out that this stupid move was deliberate and the developer settings screen is now hidden,
To get it back you need to do a kind of easter-egg hack - you go to "Settings" > "About phone" and literally tap the "Build number" 7 (seven!!) times, to get the developer settings back.


Way to go Google,
I guess you think all your users are stupid.

Thanks for that.


BTW:
Try tapping the other things on the list. Don't faint from the excitement.

Oct 6, 2012

Android Facebook SDK login error #1118578

The error is caused because the activity has set the launch mode to singleInstance.

From StackOverflow where I posted the question:


Everything was working fine, until I wanted to check what happens if the user clicks the Login with Facebook button with no internet connection available.
A toast showed up saying:
Login failed. Please contact the maker of this app and ask them to report issue #1118578 to Facebook.
In the logcat I have:
D/Facebook-authorize(2824): Login canceled by user.
I tried to get rid of this toast and display my own error message in the onCancel() method - but I can't find a way to disable that toast. When I enabled WiFi again, single sign on didn't work anymore!
What could be the cause of this?
    private void fbLogin() {
    facebook.authorize(this, new String[] { "email" }, new DialogListener() {
        @Override
        public void onComplete(Bundle values) {
            app.save("fb_token", facebook.getAccessToken());
            app.save("fb_expire", facebook.getAccessExpires());
            mAsyncRunner.request("me", new meRequestListener());
        }
        @Override
        public void onFacebookError(FacebookError error) {}
        @Override
        public void onError(DialogError e) {}
        @Override
        public void onCancel() {}
    });
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}
EDIT:
To answer my own question, removing android:launchMode="singleInstance" From the manifest in the  resolved the #1118578 issue.
However, I do need the android:launchMode="singleInstance" for the twitter OAuth login, as the browser passes the data back to the activity via new Intent. If I do not provideandroid:launchMode="singleInstance" in the manifest, a new activity is launched and it does not recieve the OAuth verifier.
Is there any way around this, to use Facebook and Twitter login in the same activity? The only solution I think of is to use a dummy activity for Twitter4j with singleInstance.


See original thread here:
http://stackoverflow.com/questions/12759261/android-facebook-sdk-login-error-1118578

Sep 15, 2012

Java :: calculating approximate sunset and sunrise

Here's a class for calculating an approximate time of the sunset, sunrise and day length on a given date, on a given location (latitude coordinate). It takes into account leap years and daylight saving time offset.
It's accurate to about 20 minutes (compared to results from WolframAlpha).

Also, because the constructor Date(int year, int month, int day) is marked as deprecated, GregorianCalendar class is used instead.

View the project on GitHub.


import java.util.*;

/*
 * Calculate sunset and sunrise on a given day on a given location
 * 
 * GPL, MIT License
 * 2012, Žan Kafol, Ana Grum
 */

public class Analemma {
 // Maximum axial tilt is 23°26'15"
 public static double MAX_DECLINATION = 23 + 26/60 + 15/3600;
 
 /*
  * Test - print out sunset for all days in this year
  */
 public static void main(String[] args) {
  Calendar calendar = Calendar.getInstance();
  
  // our chosen latitude coordinate
  double latitude = 45.772792;
  
  // for each month
  for (int m = 0; m < 12; m++) {
   calendar.set(Calendar.MONTH, m);
   
   // for each day in that month
   for (int d = 1; d <= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); d++) {
    
    System.out.println(sunset(latitude, calendar.get(Calendar.YEAR), m, d));
   }
  }
 }
 
 /*
  * Returns a Date object representing the time and date of the sunset for
  * the given parameters. The formula takes into account the daylight saving
  * time offset.
  * 
  * @param double latitude coordinate for calculating the sunset
  * @param int  the year for calculating the sunset
  * @param int  the month (0..January, 1..February, ...) for calculating the sunset
  * @param int  the day of month for calculating the sunset
  * @return   the time and date of the sunset
  * @see    dayLength
  */
 public static Date sunset(double latitude, int year, int month, int day) {
  double dayLength = dayLength(latitude, year, month, day);
  double hour = 12 + dayLength / 2 + daylightSaving(year, month, day);
  
  return getDate(year, month, day, hour);
 }
 
 /*
  * Returns a Date object representing the time and date of the sunrise for
  * the given parameters. The formula takes into account the daylight saving
  * time offset.
  * 
  * @param double latitude coordinate for calculating the sunrise
  * @param int  the year for calculating the sunrise
  * @param int  the month (0..January, 1..February, ...) for calculating the sunrise
  * @param int  the day of month for calculating the sunrise
  * @return   the time and date of the sunrise
  * @see    dayLength
  */
 public static Date sunrise(double latitude, int year, int month, int day) {
  double dayLength = dayLength(latitude, year, month, day);
  double hour = 12 - dayLength / 2 + daylightSaving(year, month, day);
  
  return getDate(year, month, day, hour);
 }
 
 /*
  * Returns the length of a day for the given parameters in hours. The
  * formula takes into account leap years.
  * 
  * @param double latitude coordinate for calculating the length of the day
  * @param int  the year for calculating the length of the day
  * @param int  the month (0..January, 1..February, ...) for calculating the length of the day
  * @param int  the day of month for calculating the length of the day
  * @return   the day length in fractional hours
  * @see    GregorianCalendar
  */
 public static double dayLength(double latitude, int year, int month, int day) {
  // Define the cardinal dates
  GregorianCalendar 
   today   = new GregorianCalendar(year,  month,    day), 
   prevWinter  = new GregorianCalendar(year - 1, Calendar.DECEMBER, 21), 
   nextSpting  = new GregorianCalendar(year + 1, Calendar.MARCH,  21), 
   springStart  = new GregorianCalendar(year,  Calendar.MARCH,  21), 
   summerStart  = new GregorianCalendar(year,  Calendar.JUNE,  21), 
   autumnStart  = new GregorianCalendar(year,  Calendar.SEPTEMBER, 23), 
   winterStart  = new GregorianCalendar(year,  Calendar.DECEMBER, 21);
  
  int  season;   // number of days in the season of the date
  int  cardinal;  // number of days passed since the start of the season
  
  boolean isWinter = false;
  boolean isAutumn = false;
  
  if (today.after(prevWinter) && today.before(springStart)) {
   season = daysBetween(prevWinter, springStart);
   cardinal = daysBetween(today, prevWinter);
   isWinter = true;
  } else if (today.equals(springStart) || today.after(springStart) && today.before(summerStart)) {
   season = daysBetween(springStart, summerStart);
   cardinal = daysBetween(today, springStart);
  } else if (today.equals(summerStart) || today.after(summerStart) && today.before(autumnStart)) {
   season = daysBetween(summerStart, autumnStart);
   cardinal = daysBetween(today, summerStart);
  } else if (today.equals(autumnStart) || today.after(autumnStart) && today.before(winterStart)) {
   season = daysBetween(autumnStart, winterStart);
   cardinal = daysBetween(today, autumnStart);
   isAutumn = true;
  } else {
   season = daysBetween(winterStart, nextSpting);
   cardinal = daysBetween(today, winterStart);
   isWinter = true;
  }
  
  // Calculate the Sun's declination
  double declination = (cardinal * MAX_DECLINATION / season);
  
  // During a solstice, the maximum axial tilt to the Sun is 23°26'15"
  // During an equinox, the axial tilt to the Sun is 0°
  if (today.after(summerStart) && today.before(autumnStart) || today.before(springStart) || today.after(winterStart)) {
   declination = MAX_DECLINATION - declination;
  }
  
  // Summer and winter solstice
  if (declination == 0 && !(today.equals(springStart) || today.equals(autumnStart))) {
   declination = MAX_DECLINATION;
  }
  
  // Use a negative declination between the summer's and next winter solstice
  if (isWinter || isAutumn) {
   declination *= -1;
  }
  
  // Calculate the day lenght from latitude and declination
  double cos_t = -Math.tan(Math.toRadians(latitude)) * Math.tan(Math.toRadians(declination));
  double t = Math.toDegrees(Math.acos(cos_t));
  double dayLength = 2 * t * 24 / 360;
  
  return dayLength;
 }

 /*
  * Returns the number of days passed between two dates
  * 
  * @param GregorianCalendar first date
  * @param GregorianCalendar  second date
  * @return      days passed
  * @see       GregorianCalendar
  */
 public static int daysBetween(GregorianCalendar d1, GregorianCalendar d2) {
  double millis = Math.abs(d1.getTimeInMillis() - d2.getTimeInMillis());
  return (int) (millis / (1000 * 3600 * 24));
 }
 
 /*
  * Returns 1 if the date is in daylight time or 0 if not
  * 
  * @param int  year of the date
  * @param int  month of the date
  * @param int  day of the month of the date
  * @return   1 for true, 0 for false
  * @see    TimeZone
  */
 public static int daylightSaving(int year, int month, int day) {
  return TimeZone.getDefault().inDaylightTime(getDate(year, month, day, 12, 0)) ? 1 : 0;
 }

 /*
  * Returns the Date object. This method is used as a replacement for the
  * deprecated constructor Date(year, month, day)
  * 
  * @param int  year of the date
  * @param int  month of the date
  * @param int  day of the month of the date
  * @return   the Date object for the given parameters
  * @see    Date
  */
 public static Date getDate(int year, int month, int day) {
  return new Date(new GregorianCalendar(year, month, day).getTimeInMillis());
 }
 
 /*
  * Returns the Date object. This method is used as a replacement for the
  * deprecated constructor Date(year, month, day, hour, minute)
  * 
  * @param int  year of the date
  * @param int  month of the date
  * @param int  day of the month of the date
  * @param  int  hour of day
  * @param  int  minute of hour
  * @return   the Date object for the given parameters
  * @see    Date
  */
 public static Date getDate(int year, int month, int day, int hour, int min) {
  return new Date(new GregorianCalendar(year, month, day, hour, min).getTimeInMillis());
 }
 
 /*
  * Returns the Date object. This method is used as a replacement for the
  * deprecated constructor Date(year, month, day, hour, minute, second)
  * 
  * @param int  year of the date
  * @param int  month of the date
  * @param int  day of the month of the date
  * @param  int  hour of day as double
  * @return   the Date object for the given parameters
  * @see    Date
  */
 public static Date getDate(int year, int month, int day, double hour) {
  double min = (hour - Math.floor(hour)) * 60;
  double sec = (min - Math.floor(min)) * 60;
  return new Date(new GregorianCalendar(year, month, day, (int) hour, (int) min, (int) sec).getTimeInMillis());
 }
}