<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Web Dude</title>
	<atom:link href="http://thewebdude.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://thewebdude.co.uk</link>
	<description>Thoughts &#38; Ideas from Scott Mokler</description>
	<lastBuildDate>Mon, 26 Mar 2012 16:02:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>2012 &#8211; To Do</title>
		<link>http://thewebdude.co.uk/todo/</link>
		<comments>http://thewebdude.co.uk/todo/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 14:37:02 +0000</pubDate>
		<dc:creator>Lawsy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://thewebdude.co.uk/?p=37</guid>
		<description><![CDATA[So I thought I would build a to-do list for 2012, here it is Learn Gain more knowledge in the Zend Framework &#8211; ongoing Learn jQuery Mobile Learn Ruby Do]]></description>
			<content:encoded><![CDATA[<p>So I thought I would build a to-do list for 2012, here it is</p>
<p>Learn</p>
<ol>
<li>Gain more knowledge in the Zend Framework &#8211; ongoing</li>
<li>Learn jQuery Mobile</li>
<li>Learn Ruby</li>
</ol>
<p>Do</p>
<ol>
<li>Upgrade Media-Bugs E-commerce / CMS Platform to v0.5 (currently on v0.1)</li>
<li>Design / Build Musiko</li>
</ol>
<p>Read</p>
<ol>
<li>Read at least 1 book this year (100% increase on last year)</li>
<li>Read e-books on web dev</li>
</ol>
<p>Personal</p>
<ol>
<li>Attend Focus on imaging conference in March at the Birmingham NEC &#8211; (UPDATE  &#8211; booked for 7th March) <b>Fail</b></li>
<li>Start martial arts style fitness programme once a week</li>
<li>Try to go climbing a few times this year</li>
<li>Do more photography</li>
<li>Compete in <a href="http://toughmudder.com/" target="_blank">Tough Mudder</a></li>
</ol>
<p>Games</p>
<ol>
<li>Complete Final Fantasy 13</li>
<li>Complete Battlefield 3</li>
<li>Buy &amp; complete Final Fantasy 13-2</li>
<li style="text-decoration:line-through">Complete Super Mario World (snes)</li>
<li style="text-decoration: line-through">Complete Zelda &#8211; The Phantom Hourglass</li>
</ol>
<p>Will build on this list &amp; repost as and when it is updated</p>
]]></content:encoded>
			<wfw:commentRss>http://thewebdude.co.uk/todo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Authenticate with Facebook</title>
		<link>http://thewebdude.co.uk/authenticate-with-facebook/</link>
		<comments>http://thewebdude.co.uk/authenticate-with-facebook/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 00:12:30 +0000</pubDate>
		<dc:creator>Lawsy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://thewebdude.co.uk/?p=24</guid>
		<description><![CDATA[Lately, there’s been quite a fuzz about lazy registration. It turns out that the less the user has to think, the higher the conversion rates are! What a thought! If]]></description>
			<content:encoded><![CDATA[<p>Lately, there’s been quite a fuzz about lazy registration. It turns out that the less the user has to think, the higher the conversion rates are! What a thought! If everybody seems to have a Facebook profile, why not add a one-click user registration? I’ll show you how to do that today.</p>
<h2>Step 1 &#8211; The setup</h2>
<p><strong>Mysql Table</strong><br />
Lets start by creating the users table</p>
<pre>
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`oauth_provider` varchar(10),
`oauth_uid` text,
`username` text,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
</pre>
<p>Quite simple: we will be setting up a table for user information with id, username, first and last name, the URL to the user’s picture, and registered date. Also, we’re adding both an oauth_provider and oauth_uid fields, to distinguish between different third party open authentication protocols and their identifiers. For example, let’s say that, next week, you decide that it’s a good idea to also let Twitter users in. Easy; you just set another value to the oauthprovider, and avoid duplicating oauthuid values.</p>
<h2>The Facebook App</h2>
<p>Let’s begin by creating a <a href="https://developers.facebook.com/apps" target="_blank">new application</a>. Give it a name and agree to the terms and conditions. Next, grab both the API Key and Secret in the basic tab as shown below.</p>
<p><a href="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_basic.png"><img class="alignnone size-medium wp-image-26" title="fb_basic" src="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_basic-300x117.png" alt="Authenticate with Facebook" width="300" height="117" /></a></p>
<p>On the canvas tab, set both the Canvas URL and Post-Authorize Redirect URL to your localhost and path that the script will process — something like http://localhost.com/login_facebook.php?. Note the question mark at the end and the domain; both are required by Facebook. Simply set your hosts file to a valid domain name.</p>
<p><a href="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_canvas.png"><img class="alignnone size-medium wp-image-27" title="fb_canvas" src="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_canvas-300x80.png" alt="Authenticate with Facebook" width="300" height="80" /></a></p>
<p>On the connect tab, set the Connect URL to the same value and set localhost.com (or the one you are using) as the Base Domain.</p>
<p><a href="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_connect.png"><img class="alignnone size-medium wp-image-28" title="fb_connect" src="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_connect-300x90.png" alt="Authenticate with Facebook" width="300" height="90" /></a></p>
<p>Now save, download the client library, and unzip facebook.php in the srcdir to a new directory created in the root.</p>
<h2>Step 2. The Callback</h2>
<p>The authentication flow has three steps:</p>
<ol>
<li>The local script generates a URL asking the user for permission</li>
<li>Facebook returns to the Canvas URL specified with a GET parameter</li>
<li>The GET parameter authenticates the session</li>
</ol>
<p>Let’s make a quick test before registering and login.</p>
<pre>
# We require the library
require("facebook.php");

# Creating the facebook object
$facebook = new Facebook(array(
'appId' =&gt; 'YOUR_APP_ID',
'secret' =&gt; 'YOUR_APP_SECRET',
'cookie' =&gt; true
));

# Let's see if we have an active session
$session = $facebook-&gt;getSession();

if(!empty($session)) {
# Active session, let's try getting the user id (getUser()) and user info (api-&gt;('/me'))
try{
$uid = $facebook-&gt;getUser();
$user = $facebook-&gt;api('/me');
} catch (Exception $e){}

if(!empty($user)){
# User info ok? Let's print it (Here we will be adding the login and registering routines)
print_r($user);
} else {
# For testing purposes, if there was an error, let's kill the script
die("There was an error.");
}
} else {
# There's no active session, let's generate one
$login_url = $facebook-&gt;getLoginUrl();
header("Location: ".$login_url);
}
</pre>
<p>Now, go to http://localhost.com/login_facebook.php, and let’s see what happens. If you are redirected to Facebook and requested for permission, we are on the right track.</p>
<p><a href="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_permission.png"><img class="alignnone size-medium wp-image-29" title="fb_permission" src="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_permission-300x146.png" alt="Authenticate with Facebook" width="300" height="146" /></a></p>
<p>However, there might be two issues. The first one: if you’re redirected to Facebook, but it shows an error, there might be a missing value in the configuration. Go back to your application settings and check both the Connect and Canvas tabs and make sure the fields are ok as described above.</p>
<p>There might be another issue, where you see an error, like “Uncaught CurlException: 60: SSL certificate problem, verify that the CA cert is OK.” This happens because of the CURL settings. You’ll have to open facebook.php, find the makeRequest() method, and, inside the function, find this line:</p>
<pre> $opts = self::$CURL_OPTS; </pre>
<p>Immediately following it, add:</p>
<pre>$opts[CURLOPT_SSL_VERIFYPEER] = false;</pre>
<p>hate hacking libraries, but I haven’t found another way. Well, let’s continue with user registration. I’ve also added a try/catch statement, because, if there’s an old session keys in the GET params in the URL, the script will die with a horrible error.</p>
<h2>Step 3. Registration and Authentication</h2>
<p>We’ll next be working with MySQL. Please note that I will not implement a data sanitizer, since I want to keep the code as short and on task as possible. Please keep this in mind: always sanitize your data.</p>
<p>First, let’s connect to the database.</p>
<pre> mysql_connect('localhost', 'YOUR_USERNAME', 'YOUR_PASSWORD');
mysql_select_db('YOUR_DATABASE'); </pre>
<p>Now, let’s work on the $session conditional, in case we have a session.</p>
<pre>
# We have an active session; let's check if we've already registered the user
$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user['id']);
$result = mysql_fetch_array($query);

# If not, let's add it to the database
if(empty($result)){
$query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user['id']}, '{$user['name']}')");
$query = msyql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
$result = mysql_fetch_array($query);
}
</pre>
<p>Note that I’m querying the database, looking for facebook as a oauth_provider; it’s generally a good idea, if you want to accept other OAuth providers (as twitter, Google Accounts, Open ID, etc.) and a oauth_uid, since it’s the identifier the provider gives to its user accounts.</p>
<p>The oauth_provider field could potentially lead to bad performance if we leave it as a text field type. As such, the best option is setting it to an ENUM type.</p>
<p>We have now a $result var with the values queried from the database. Let’s next add some sessions. Add this line at the beginning of your script.</p>
<pre> session_start(); </pre>
<p>After the empty($result) conditional, append the following:</p>
<pre>
if(!empty($user)){
# ...

if(empty($result)){
# ...
}

# let's set session values
$_SESSION['id'] = $result['id'];
$_SESSION['oauth_uid'] = $result['oauth_uid'];
$_SESSION['oauth_provider'] = $result['oauth_provider'];
$_SESSION['username'] = $result['username'];
}
</pre>
<p>As it makes little sense to authenticate a user who is already logged in, just below the session_start() line, add:</p>
<pre>
if(!empty($_SESSION)){
header("Location: home.php");
}
</pre>
<p>And in the scripts which require authentication, just add:</p>
<pre>
session_start();
if(!empty($_SESSION)){
header("Location: login_facebook.php");
}
</pre>
<p>And if you want to display the username, access it as an array.</p>
<pre>
echo 'Welcome ' . $_SESSION['username'];
# or..
echo 'Welcome ' . !empty($_SESSION) ? $_SESSION['username'] : 'guest';
</pre>
<h2>Step 4. Additional Methods</h2>
<p>Facebook has a ton of connect features, but here are four that I’ve found to be the most useful.</p>
<h3>Legacy Methods</h3>
<p>I might be missing something, but the FQL seems more flexible and easy than the Graph API. Fortunately, Facebook still lets developers use it, altough with the new library, it has changed a bit.</p>
<p>If you want the user id, first name, last name, squared thumbnail for the user picture, the biggest user picture available, and his or her gender, you can use the users.getInfo method.</p>
<pre>
$uid = $facebook-&gt;getUser();
$api_call = array(
'method' =&gt; 'users.getinfo',
'uids' =&gt; $uid,
'fields' =&gt; 'uid, first_name, last_name, pic_square, pic_big, sex'
);
$users_getinfo = $facebook-&gt;api($api_call);
</pre>
<p>It is possible to achieve the same result, using FQL.</p>
<pre>
$uid = $facebook-&gt;getUser();
$fql_query = array(
'method' =&gt; 'fql.query',
'query' =&gt; 'SELECT uid, first_name, last_name, pic_square, pic_big, sex FROM user WHERE uid = ' . $uid
);
$fql_info = $facebook-&gt;api($fql_query);
</pre>
<p>Here’s the <a href="http://developers.facebook.com/docs/reference/fql/" target="_blank">list of tables</a></p>
<p>which can be accessed with FQL, as well as the fields available for <a href="http://developers.facebook.com/docs/reference/fql/user/" target="_blank">the table users</a>.</p>
<h2>Extended Permissions</h2>
<p>Facebook provides applications with some interaction with the user’s data – just as long as it’s authorized. With the old API, the authorization for additional permissions was exclusively available for the Javascript SDK (altough I’m not quite sure). With the new API, we can easily redirect the user to an authorization dialog in Facebook, and return to our site after the access is either granted or denied.</p>
<p>In the following example, we will be redirecting a user to authorize posts status updates, photos, videos and notes, the user’s real email address, birthday and access to photos and videos.</p>
<pre>
$uid = $facebook-&gt;getUser();

# req_perms is a comma separated list of the permissions needed
$url = $facebook-&gt;getLoginUrl(array(
'req_perms' =&gt; 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'
));
header("Location: {$url} ");
</pre>
<p>Here’s <a href="http://developers.facebook.com/docs/reference/api/permissions/" target="_blank">a full list of permissions</a>. Note that you can specify both the url to direct to if the user accepts and the url to be redirected to if the user denies. The key for these array elements are next and cancel_url, respectively. Here’s a quick example:</p>
<pre>
$url = $facebook-&gt;getLoginUrl(array(
'req_perms' =&gt; 'email',
'next' =&gt; 'http://localhost.com/thanks.php',
'cancel_url' =&gt; 'http://localhost.com/sorry.php'
));
</pre>
<p>If not specified, the default is the requesting script’s location.</p>
<p><a href="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_additional_permissions.png"><img class="alignnone size-medium wp-image-25" title="fb_additional_permissions" src="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_additional_permissions-300x239.png" alt="Authenticate with Facebook" width="300" height="239" /></a></p>
<h2>Checking for Extended Permissions</h2>
<p>Since the user can easily revoke permissions, the application should always check if a given permission is granted before using it, specially if it’s about publishing something.</p>
<pre>
$uid = $facebook-&gt;getUser();

# users.hasAppPermission
$api_call = array(
'method' =&gt; 'users.hasAppPermission',
'uid' =&gt; $uid,
'ext_perm' =&gt; 'publish_stream'
);
$users_hasapppermission = $facebook-&gt;api($api_call);
print_r($users_hasapppermission);
</pre>
<p>The ext_perm will only support <a href="http://developers.facebook.com/docs/reference/rest/users.hasAppPermission/" target="_blank">the old list of available permissions</a>.</p>
<h2>Publishing to the Wall</h2>
<p>Let’s post something to the wall after verifying the user has the publish_stream permission.</p>
<pre>
# let's check if the user has granted access to posting in the wall
$api_call = array(
'method' =&gt; 'users.hasAppPermission',
'uid' =&gt; $uid,
'ext_perm' =&gt; 'publish_stream'
);
$can_post = $facebook-&gt;api($api_call);
if($can_post){
# post it!
$facebook-&gt;api('/'.$uid.'/feed', 'post', array('message' =&gt; 'Saying hello from my Facebook app!'));
echo 'Posted!';
} else {
die('Permissions required!');
}
</pre>
<p>Essentially, we are making an API call to /{user_id}/feed, using the POST method (second argument) and an array as a third argument for the data to be sent. In this case, this third argument supports <code>message, link, picture, caption, name </code>and <code>description</code>. Here’s the code:</p>
<pre>
$facebook-&gt;api('/'.$uid.'/feed', 'post', array(
'message' =&gt; 'The message',
'name' =&gt; 'The name',
'description' =&gt; 'The description',
'caption' =&gt; 'The caption',
'picture' =&gt; 'The Image',
'link' =&gt; 'http://thewebdude.co.uk/'
));
</pre>
<p>Here’s how it is posted.</p>
<p><a href="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_wall.png"><img class="alignnone size-medium wp-image-30" title="fb_wall" src="http://thewebdude.co.uk/wp-content/uploads/2011/12/fb_wall-300x70.png" alt="Authenticate with Facebook" width="300" height="70" /></a></p>
<h2>Conclusion</h2>
<p>While Facebook’s authentication capabilities are indeed useful, since so many people are on Facebook these days, using it as the only method of authentication in a site is not recommended. What about those who don’t have Facebook accounts? Are they not allowed to access your application? Thanks for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://thewebdude.co.uk/authenticate-with-facebook/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Injection</title>
		<link>http://thewebdude.co.uk/sql-injection/</link>
		<comments>http://thewebdude.co.uk/sql-injection/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 18:35:06 +0000</pubDate>
		<dc:creator>Lawsy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://thewebdude.co.uk/?p=9</guid>
		<description><![CDATA[It seems that every day we are hearing new stories in the media of big sites being hacked. With this in mind I thought that I would offer some basic]]></description>
			<content:encoded><![CDATA[<p>It seems that every day we are hearing new stories in the media of big sites being hacked. With this in mind I thought that I would offer some basic coverage of protection against SQL Injection.</p>
<p>SQL Injection is one of the most common methods used to gain access to a web application and take control. For example, it is done by hackers entering sql into dynamic parameters to gain access to the database and displaying all of the information from the database or to force a login to a system where the user credentials don&#8217;t exist.</p>
<p>To protect against this it is important that we check the data that is passed into the system from forms and url strings.</p>
<h2>Using PHP Data Objects (PDO)</h2>
<p>PDO is a PHP extension that work like class letting you access database more fast and secure.</p>
<pre>
$PSH=$db-&gt;prepare('SELECT * FROM table WHERE id = :id');
$PSH-&gt;execute(array(':id' =&gt; $_GET['id']));
$rows = $PSH -&gt;fetchAll();
</pre>
<p>where $db is a success PDO connection</p>
<h2>mysql_real_escape_string()</h2>
<p>mysql_real_escape_string() is a function that escape the unsafe character before sending the MySQL query.</p>
<pre>
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * FROM table where id = "' .$id. '");
</pre>
<h2>preg_replace()</h2>
<p>preg_replace() is a function that find and replace character in string. Here we will use it to find anything but numbers and remove it.</p>
<p>This method is good if you are selecting from MySQL using WHERE id = INT;</p>
<pre>
$id = preg_replace("/[^0-9]/","", $_GET['id']);
$query = mysql_query("SELECT * FROM table where id = "' .$id. '");
</pre>
<h2>Conclusion</h2>
<p>Make sure that all data that is entered by a user is striped of anything that can be malicious. If you don&#8217;t you could end up loosing your application.</p>
<p>Always keep a backup of everything to!</p>
]]></content:encoded>
			<wfw:commentRss>http://thewebdude.co.uk/sql-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>So it begins</title>
		<link>http://thewebdude.co.uk/so-it-begins/</link>
		<comments>http://thewebdude.co.uk/so-it-begins/#comments</comments>
		<pubDate>Fri, 02 Dec 2011 23:57:35 +0000</pubDate>
		<dc:creator>Lawsy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://thewebdude.co.uk/?p=7</guid>
		<description><![CDATA[I will keep a blog. I will, I will! &#160; Will update another day though.]]></description>
			<content:encoded><![CDATA[<p>I will keep a blog. I will, I will!</p>
<p>&nbsp;</p>
<p>Will update another day though.</p>
]]></content:encoded>
			<wfw:commentRss>http://thewebdude.co.uk/so-it-begins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

