The Blog

Serve landing page while building a new wordpress site

05 Apr 12

If you’re a web developer you confronted by this at least once. If you don’t own a development webserver you’ll need to make your developments in the production webserver. This means that everyone could see your work-in-progress easily.

A common pattern is to create an index.html file ( which gets served – almost ever – before index.php ) which will contain a specific landing page with an “under construction” text.

How to serve the index.html file to “regular people” while you work on wordpress ( index.php ) ?

It’s quite easy using .htaccess and mod_rewrite of apache. The basic concepts are:

  • WordPress logged in people will see the “work in progress” website
  • Not logged in people will see the “site under construction” page

In order to achieve that, you’ve to prepend the following lines to your .htaccess file Β ( placed in your document root ) :

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} index.html$
  RewriteCond %{HTTP_COOKIE} ^.*wordpress_logged_in.*$ [NC]
  RewriteRule . /index.php [L]
</IfModule>

As you can see we only override the normal web-flow only if the browser is going to request the index.html file and the user has the wordpress_logged_in cookie setted.

Whenever you need to see your “real website” you just need to point your browser to http://example.com/wp-admin , login and then you’ll be able to see the website you’re creating and not the “under construction” page.

Note: This is only a partial solution and people could be smart enough to emulate the wordpress_logged_in cookie and see your work in progress anyway. In my case-scenarios this was more than enough.

 

Avoiding Xss injection without sacrifying some tags in PHP

23 Mar 12

Sooner or later I always remember I’ve a blog. This time the topic is XSS.

If you don’t have a clue ofΒ what XSS is maybe you should read this before reading this article.

Read More

onChange event on EditText in Android

09 Ott 10

Sooner or later you’ll have to deal with it. If you’re an html developer and you write also in javascript you’ll surely know the onchange event.

Unfortunately it’s a little bit tricky to find the same event on android.

The onChange event is helpful when you’ve to deal with the following things:

  • Let the user know (in realtime) how many characters he typed.
  • Let the user know (in realtime) how many remaining characters he is allowed to type.
  • Make realtime processing of the content ( like sending it online and fetch some partial results of the partial typed edittext )

You’ve to implement your own instance of TextWatcher and let the edittext know that you want to be notified at each change by calling the method EditText.addTextChangedListener.

Below i will give you a simple example ( it’s written on the fly but you’ll understand the idea )

[sourcecode lang=”java”]
((EditText)findViewById(R.id.et_testo)).addTextChangedListener(new TextWatcher() {

public void afterTextChanged(Editable s) {
((TextView)findViewById(R.id.numcaratteri)).setText(String.format(getString(R.string.caratteri), s.length()));

}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub

}

});

[/sourcecode]

Android Root Certification Authorities List

23 Set 10

Since it was a little hard for me finding it, here you can find the trusted CAs in Android 2.2 Froyo.

In order to get my result on each android device you’ve to download this file and place it on $JAVA_HOME/lib/ext . Plus, you should have $JAVA_HOME/bin in your $PATH

[sourcecode language=”bash”]
adb pull /system/etc/security/cacerts.bks cacerts.bks
keystore cacerts.bks -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider -storepass changeit -list -v >> certificates.txt
[/sourcecode]

After the break my certificates.txt.
Read More

Reading $_GET variable in CODEIGNITER

14 Ago 10

There are multiple solutions out there but i did create my own.

It’s simple as 1,2,3. Hope it helps.

[sourcecode lang=”php”]
// CODEIGNITER HACK
$tmp = explode(‘?’,$_SERVER[‘REQUEST_URI’]);
$tmp = explode(‘&’, $tmp[1]);

foreach($tmp as $keyval) {
$tmpAppoggio = explode(‘=’, $keyval);
$_GET[urldecode($tmpAppoggio[0])]=urldecode($tmpAppoggio[1]);
}
// end of codeigniter hack
[/sourcecode]

Write an SMS without sending it on Android 2.2 Froyo

12 Ago 10

I just figured out how to write an sms without sending it really on android froyo 2.2 using the content provider.

I’ll write only the snippet here. Hope it helps

[sourcecode language=”java”]
/**
* writes an sms on the contentprovider
* @param ctx Context
* @param mobNo mobile number
* @param msg text of the message
*/
private final static void storeMessage(Context ctx,String mobNo, String msg) {
ContentValues values = new ContentValues();
values.put("address", mobNo);
values.put("body", msg);
ctx.getContentResolver().insert(Uri.parse("content://sms/sent"), values);
}
[/sourcecode]

Obviously you should set the right permissions on the manifest ( Yes the following are all needed for this task ) :

[sourcecode language=”xml”]
<manifest>
….
….
<uses-permission android:name="android.permission.WRITE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
</manifest>
[/sourcecode]

Add Events on Google Calendar on Android Froyo and above

09 Ago 10

Since i started developing applications for android i noticed there were some undocumented apis. Google does reccomend to not use these apis but since there are no “other nice ways” to achieve some tasks sometimes they are useful ( but still unreccomended)

It’s the case of the Google Calendar Apis. Out there you can find a lot of docs about these undocumented & unsupported apis but you’ll get some troubles if google decides to change them.

For example if you want to add an “event” to the calendar programmatically you can follow the snippet below which is SDK proof. In fact i did ( It’s not refactored for better reading ) write some code that would work on Sdk from 1.5 to 2.2 ( aka Froyo ) solving the provider issue on froyo and above.


Hope it helps to solve the problem about google calendar in froyo πŸ™‚

Reference to the CP: Xda

Android Usefull Docs

27 Lug 10

Today i surfed the web searching about guidelines and best practices on writing android apps. In this in-depth search i found some useful slides and tips:

1. Android Ui Design Guidelines

A slideshare with some usefull tips about android ui design:

[slideshare id=4827211&doc=uidesigntips-100723230420-phpapp02]

2. Android Ui Stencil and PSDs

I also found a great resource where there are some other stencils and psds to download.

Link

3. Android icon templatepack

I also noticed the Google Android team released the icon template pack so you can easily design you’r own menus and icons using their psd with a lot of presets.

Link

How to use codeigniter with eclipse helios

24 Giu 10

Codeigniter is a great framework. Eclipse is a very nice IDE. Why not using them both when writing our own apps ?

After some googling i found a way to get auto-completion with codeigniter on eclipse.

Requirements:

  1. Xampp : I suggest using xampp lite.
  2. Codeigniter: Obviously
  3. Eclipse Helios for PHP Development : πŸ™‚

Read More

How to manage custom post type category template on wordpress 3.0

18 Giu 10

Today wordpress 3.0 has been released. Since i started to play with it almost immediately i found some troubles when i was trying to display all the post in the new wordpress custom type into one single page.

The goals:

  1. I wanted to create a custom template for these custom posts.
  2. I wanted to create a custom archive template for these custom posts

I found immediately the solution for the first “issue” . WordPress 3.0 templating system would search for a file named single-[custom post type name].php inside the theme directory so i simply copied and pasted the single.php into the new file and made some tiny modifications.

The second goal was a little bit difficult to achieve. I googled it and i found some articles talking about this issue but none of them gave Β ma an easy and working solution.

So i decided to take some parts from them and make my own solution.

Note: The custom post type name is “androidapps”

I did create a new useless plugin that fixed my issues. the source coude is the following:

[sourcecode language=”php”]/*
Plugin Name: Androidapss
Plugin URI: http://i love you.com
Version: 1.0.78
Description: A very useless plugin
Author: Baccega Andrea
Author URI: http://www.andreabaccega.com
Tags: seo,google,link,optimization
*/
add_filter(‘rewrite_rules_array’,’wp_insertMyRewriteRules’);
add_filter(‘query_vars’,’wp_insertMyRewriteQueryVars’);
add_filter(‘init’,’flushRules’);
add_action("template_redirect",’buch_template_redirect’);
// Remember to flush_rules() when adding rules
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
$newrules = array();
$newrules[‘androidapps/feed/(feed|rdf|rss|rss2|atom)/?$’] = ‘index.php?&feed=$matches[1]&post_type=androidapps’;
$newrules[‘androidapps/(feed|rdf|rss|rss2|atom)/?$’] = ‘index.php?&feed=$matches[1]&post_type=androidapps’;
$newrules[‘androidapps/page/?([0-9]{1,})/?$’] = ‘index.php?&paged=$matches[1]&post_type=androidapps’;
$newrules[‘androidapps/?$’] = ‘index.php?paged=1&post_type=androidapps’;
return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
array_push($vars, ‘id’);
return $vars;
}

function buch_template_redirect()
{
global $wp;

$muley_custom_types = array("androidapps");

if (in_array($wp->query_vars["post_type"], $muley_custom_types))
{
if ( is_robots() ) :
do_action(‘do_robots’);
return;
elseif ( is_feed() ) :
do_feed();
return;
elseif ( is_trackback() ) :
include( ABSPATH . ‘wp-trackback.php’ );
return;
elseif($wp->query_vars["name"]):
include(TEMPLATEPATH . "/single-".$wp->query_vars["post_type"].".php");
die();
else:
include(TEMPLATEPATH . "/".$wp->query_vars["post_type"].".php");
die();
endif;

}
}[/sourcecode]

Well all the trick is done by two functions

  • buch_template_redirect
  • wp_insertMyRewriteRules

The first one buch_template_redirect would hack the template functions and would tell wordpress to search for

  • single-androidapps.php for single posts
  • androidapps.php for archive

The second wp_insertMyRewriteRules which is copied from the wordpress codex website would add some rewrite rules to wordpress so he will knows how to route the requests :).

Now if you navigate through http://www.andreabaccega.com/androidapps you’ll see the archive template dedicated to the posts inside the “androidapps” type.

How would you use my code for your meanings?

Simply create a new plugin with the code above, then replace all those androidapps instances with your custom post type name.

Enjoy πŸ™‚