bakedproject.com Blog Not just another WordPress weblog

4Oct/101

Using jQuery UI with WordPress

I started developing with WordPress 3.0+ these days and figuring out a few things!.

WP loads a few jQuery libraries by default, so being a plugin developer you do not explicitly need to load those libraries, or that is what they have suggested. The original list is here http://codex.wordpress.org/Function_Reference/wp_enqueue_script.

I could not get the jQuery UI working for the widgets! The widget control does not load any UI component, the tabs are already "sortable" and "draggable". This probably is the reason. jQuery UI was not working on the actual widget display either. I tried a few things before remembering, a while ago I had to call the the jQuery UI init functions all the way to the bottom for a challenging project. We were using jQuery Light Box and some other components. The jQuery UI initi simply wont work if the call was made before

So this is my call

1
wp_enqueue_script('myscript', '/wp-content/plugins/bp-category-display/js/myscript.js', array('jquery', 'jquery-ui-core','jquery-ui-sortable'), '', true);

the important thing is, enqueuing the script to the end of the document, this the last 'true'.

Filed under: Uncategorized 1 Comment
15Sep/1010

User ID from Facebook application installed on profile tab

Facebook platform does not give direct access to the user ID through a (Ajax or http form)post from an application installed on a profile tab(mainly fan page, Facebook removing user's profile tab soon). So application installed on a profile tab can not record user activity, can not get the user to permit the application either.

But, using the new OAuth 2.0 for canvas pages, it is pretty easy and efficient. This feature could be activated from the applications advanced settings. Once activated the platform sends only one POST variable called signed_request. I found the following code on the developer's forum. This code can re-generate the viewing user ID. If the ID is not present, the user never provided(added) the application!

function parse_signed_request($signed_request, $secret) {
 list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
 
 // decode the data
 $sig = $this->base64_url_decode($encoded_sig);
 $data = json_decode($this->base64_url_decode($payload), true);
 
 if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
  error_log('Unknown algorithm. Expected HMAC-SHA256');
  return null;
 }
 
 // check sig
 $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
 if ($sig !== $expected_sig) {
  error_log('Bad Signed JSON signature!');
  return null;
 }
 
 return $data;
}
 
function base64_url_decode($input) {
 return base64_decode(strtr($input, '-_', '+/'));
}

9Sep/100

Facebook.streamPublish behaviour with permission

The Facebook.streamPublish FBJS method publishes a post into the stream on the Wall of a user or a Facebook Page, group, or event connected to the user (but not to the Wall of an application profile page). This method fires up FBJS dialog popup window, well that is when the application has permission(user added the application). If the user never added the application, this method actually fires up a browser popup, 90s style!

Filed under: FBJS, Facebook No Comments
5Aug/100

CodeIgniter – organizing controllers into sub-folders a wild card _remap function

For a "hello world" project it is really not important, but building a CMS using the most amazing framework requires an organized "controllers" set. Implementing sub-folder for organizing controllers is probably the most efficient way to do this.

Here is an example...

I am writing a new "users" controller. The controller has following methods
login
logout
register
profile

So, instead of writing all these methods in the "users.php" controller file, I am going to create a "users" folder and create corresponding php files those methods. So my "users" folder directory tree going to look something like this
users
->login.php
->logout.php
->register.php
->profile.php

My controllers usually have "index" method, which is the default method for the controller. The URL 'http://yourdomain.com/users/profile' will reach the 'index' method. By default that is how the most amazing framework works. But, the URL 'http://yourdomain.com/users/profile/' will cause a 404 error! If we have a method called "view", 'http://yourdomain.com/users/profile/view/' is going to work, but for some reason, we may absolutely need the URL to be 'http://yourdomain.com/users/profile/'.

This is doable via the awesome "_remap" function!

For 'wild card' function matching,

function _remap($method) {
 if(method_exists ($this, $method)) {
  $this->$method();
  } else {
  $this->index();
 }
}

13Jul/101

Codeigniter form validation “French” messages

Here are all the CodeIgniter form validation messages in French. Well I do not speak the language so I turned to the language expert! I am not sure about the grammar, so info for a better solution would be appreciated.


$lang['required'] = "Le champ %s est nècessaire.";
$lang['isset'] = "Le champ %s doit avoir une valeur.";
$lang['valid_email'] = "Le champ %s doit contenir une adresse email valide.";
$lang['valid_emails'] = "Le champ %s doit contenir toutes les adresses e-mail valide.";
$lang['valid_url'] = "Le champ %s doit contenir une URL valide.";
$lang['valid_ip'] = "Le champ %s doit contenir une adresse IP valide.";
$lang['min_length'] = "Le champ %s doit Ítre au moins %s caractères de longueur.";
$lang['max_length'] = "Le champ %s ne peut pas dÈpasser %s caractères.";
$lang['exact_length'] = "Le champ %s doit Ítre exactement de %s caractères.";
$lang['alpha'] = "Le champ %s ne peut contenir que des caractères alphabÈtiques.";
$lang['alpha_numeric'] = "Le champ %s ne peut contenir que des caractères alpha-numÈriques.";
$lang['alpha_dash'] = "Le champ %s ne peut contenir que des caractères alphanumÈriques, tirets et les tirets.";
$lang['numeric'] = "Le champ %s doit contenir uniquement des nombres.";
$lang['is_numeric'] = "Le champ %s ne doit contenir que des caractères numÈriques.";
$lang['integer'] = "Le champ %s doit contenir un nombre entier.";
$lang['matches'] = "Le champ %s ne correspond pas au champ %s.";
$lang['is_natural'] = "Le champ %s ne doit contenir que des nombres positifs.";
$lang['is_natural_no_zero'] = "Le champ %s doit contenir un nombre supÈrieur ‡ zÈro.";

6Jul/100

BUILDING FACEBOOK AND FACEBOOK CONNECT APPLICATION WITH CODEIGNITER – Mixing segment-based URL with query strings (GET)

After Facebook user "allows" the application, Facebook redirects users to the application canvas (or the calling controller) URL with Facebook generated authentication code.

For example, controller "invite", requires (Facebook) user authentication. The controller probably going to have something like this

$CI->load->library('Facebook'); // Facebook API library
$CI->facebook = new Facebook($this->fb_api_key, $this->fb_secret);//initialize facebook class
$this->fb_client = $CI->facebook->api_client;
$this->fb_user_id = $CI->facebook->require_login(); // get the FB user id

After the "require_login" call, Facebook will ask for the user to provide permission, and if the user allows the app to access user's personal data, facebook will redirect the user to

http://apps.facebook.com/facebook-application-name/invite/?installed=1&session={%22session_key%22:%222.besQb6tf33wpIJ7hQTnx_Q__.3600.1278370800-
1678407106%22,%22uid%22:1678407106,%22expires%22:1278370800,%22se
cret%22:%22KxSIVxKfjafJeJRdRa40aA__%22,%22sig%22:%2238a96471694e9c63
6192f93bfb2333cb%22}

By default, CodeIgniter would throw a 404 cause it is designed to work only with segmented URI. But it is absolutely impossible to build a Facebook application with such setup. Well, saying absolutely impossible is quite the thing,in fact it is. Application with regular setup may work but chances are, would break very frequently as Facebook keeps sending session variable(I saw entire file binary data once) over GET.

So we need to customize CodeIgniter to make sure the (most amazing) framework can accept such mixed input.

Step 1: Open application/config.php file, and search for $config['uri_protocol'] and change the value to "PATH_INFO". So that line would look like
$config['uri_protocol'] = "PATH_INFO";

Step 2: In same application/config.php file, search for $config['enable_query_strings'] and change the value to "TRUE". So that line would look like
$config['enable_query_strings'] = "TRUE";

Step 3: Now, in thesame application/config.php file, search for $config['permitted_uri_chars'] and change the value to a suitable combination.

"a-z 0-9~%.:_\-+?=!,${}"\'[]&" works really great. The session variable is technically a JSON dump from Facebook, so this combination has a few unusual characters.

So now you can access both the segmented URI variables and GET variables via "$this->uri->uri_to_assoc(n)" and "$_GET" respectively.

VERY IMPORTANT: Above mod(I could say "customization" or something else!!!) would make the (most amazing) framework vulnerable to SQL injection and many other unethical injections, so take necessary steps to secure it!

12Mar/100

Remove symbols and punctuations from a string

A very simple function to remove symbols and punctuations from a word.

function replace_symbols($word) {
 $symbols = "~!@#$%^&*()_+=:;?.,";
 
 for($i=0;$i<strlen($symbols);$i++) {
  $word = str_replace($symbols[$i],'', $word);
 }
 
 return $word;
} // end of replace_symbols

We can add more symbols to the symbols string.

15Jan/100

GET URL with Codeigniter

I copied the Codeigniter URI library to my application libraries folder. Keeping the same name ensures CI would read my custom URI library, not the default one.

Add custom functionality(whatever needs to be done to the GET variables) to

function _filter_uri($str)

I am parsing the string and returning CI friendly URI!

function _filter_uri($str)
{

5Jan/100

Building Facebook and Facebook Connect application with Codeigniter – Setup

Developing for Facebook is quite simple and easy if you have the right components.  I used Codeingniter as a base framework for a few Facebook applications and that works pretty well .  I had to do some little tweaking though.

This entry is not about how to build a Facebook application in Codeigniter, its more like the things that I have done to get it going...

I named my controller “fbook”. Very important as the Facebook library is “Facebook” under “application/libraries/”

The “Facebook” PHP client comes with 3 main files,
1. facebook_desktop.php
2. facebookapi_php5_restlib.php
3. facebook.php
We need to create a CI library from these 3 files and the “jsonwrapper”. Create a “facebook-clinet” folder under “application/libraries” folder. Copy paste all the files from the Facebook API library except “facebook.php” to this newly created folder. Rename the “facebook.php” file to “Facebook.php” and place it under “application/libraries” folder. Add the following line to the “Facebook.php” file
include_once 'facebook-client/facebookapi_php5_restlib.php';
This will include the REST library.

This is pretty much the initial setup. You can use the same “Facebook” library to for “Facebook Connect” functionality.

31Dec/091

memcache for a dull life

Life is very exciting with real time MySQL queries...but I prefer not to wait for my badly structured query to take more time than it is (static final)necessary...

memcached for all object manipulation...YES

Also, using sub-queries makes life quite dull instead a of JOIN(LEFT/RIGHT/MIDDLE/MESSEDUP any kind...). MySQL should have an "experience lock" on that, unless someone been using sub-queries for 3 years...oh wait, the old version never had sub-queries...well, like always, I am often wrong(but never in doubt).