Mar 10 2010

PHP parse url, mailto, and also twitter’s usernames and arguments

Category: Phpadmin @ 9:41 pm

This small function receive a text as input and returns an html text with links if the source text contains urls (http://www… but also ftp://… and every other protocol), emails, twitter’s usernames (with @ at the beginning) and also twitter tags (with # at the beginning).
Those replaces are possible with the php preg_replace function:

function parse_twitter($t) {
	// link URLs
	$t = " ".preg_replace( "/(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]*)".
		"([[:alnum:]#?\/&=])/i", "<a href=\"\\1\\3\\4\" target=\"_blank\">".
		"\\1\\3\\4</a>", $t);

	// link mailtos
	$t = preg_replace( "/(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)".
		"([[:alnum:]-]))/i", "<a href=\"mailto:\\1\">\\1</a>", $t);

	//link twitter users
	$t = preg_replace( "/ +@([a-z0-9_]*) ?/i", " <a href=\"http://twitter.com/\\1\" target=\"_blank\">@\\1</a> ", $t);

	//link twitter arguments
	$t = preg_replace( "/ +#([a-z0-9_]*) ?/i", " <a href=\"http://twitter.com/search?q=%23\\1\" target=\"_blank\">#\\1</a> ", $t);

	// truncates long urls that can cause display problems (optional)
	$t = preg_replace("/>(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]".
		"{30,40})([^[:space:]]*)([^[:space:]]{10,20})([[:alnum:]#?\/&=])".
		"</", ">\\3...\\5\\6<", $t);
	return trim($t);
}
  • Share/Bookmark

Tags: , , , ,


Mar 10 2010

How to write a text description into html input type password

Category: Java ScriptSimone Lippolis @ 11:58 am

Sometime designers put form labels and instrucions into html inputs. One of the common uses is for login boxes when there’s not much space.

An example of login form design

An example of login form design

In this case, you have to choose between:

  1. Set the password field as type=”text”, thus not masking users’ passwords;
  2. Set the password field as type=”password”, thus not letting the user see what’s written inside the form field.

To avoid this problems, I wrote a small jQuery plugin which dynamically replaces selected <input type=”password” /> fields with <input type=”text” /> fields displaying a chosen value. The replace procedure works forth and back: when the user clicks on the input field it is transformed into a <input type=”password” />; if the user inserts some text, then the field remains masked, if he/she does not insert anything, the field is replaced again, letting the chosen value be visible again.

To use it, simply select the form element you want to replace.It works with any input type=”password” on the page, it can replace a single element, each element with a specific class, or property, or every password element.

// Replaces each &lt;input type="password" /&gt; element
$(document).ready(function () {
 $('input[type=password]').txReplaceFormPassword({
 show_text : 'Password'
 });
 });
// Replaces the &lt;input type="password" /&gt; element with id="pwd"
$(document).ready(function () {
 $('#pwd').txReplaceFormPassword({
 show_text : 'Password'
 });
 });
// Replaces each &lt;input type="password" /&gt; element with class="password"
$(document).ready(function () {
 $('.password').txReplaceFormPassword({
 show_text : 'Password'
 });
 });

Here is the complete code:

/***********************************************************
* txReplaceFormPassword.js
*
* Written by Simone Lippolis - http://simonelippolis.com
*
* This script is distributed under a Creative Commons - Commercial, Attribution licence
* http://creativecommons.org/licenses/by/2.5/it/
*
* Feel free to copy, edit, redistribute this script. Please, don't remove credits.
*
* If you find some bug or error, please let me know.
*
* How to use:
*
* 1. Include jquery:
* &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;
*
* 2. Include this script:
* &lt;script type="text/javascript" src="txReplaceFormPassword.js"&gt;&lt;/script&gt;
*
* 3. On document ready, invoke the plugin
*
* $(document).ready(function () {
*         $('#pwd').txReplaceFormPassword({
*            show_text : 'Password'
*        });
*     });
*
* consider '#pwd' as the object which needs to be replaced:
*
* &lt;form action="someaction" method="post" id="formid" name="formname"&gt;
*        &lt;input type="password" name="pwd" id="pwd" value="" /&gt;
*    &lt;/form&gt;
*
* The 'show_text' parameters contains the text to be shown in password's
* text field.
*
***********************************************************/

jQuery.fn.txReplaceFormPassword = function(options) {
 var options = jQuery.extend( {
 show_text: 'Password'
 },options);

 function trim(txt) {
 return txt.replace(/^\s+|\s+$/g, '');
 }

 return this.each(function() {
 if (jQuery(this).val().length == 0) {

 var $pos = jQuery(this).position();
 var $style = jQuery(this).attr('style');
 var $class = jQuery(this).attr('class');
 var $size = jQuery(this).attr('size');
 var $id = jQuery(this).attr('id');
 var $name = jQuery(this).attr('name');

 jQuery(this).hide();
 jQuery(this).after('&lt;input type="text" id="txgrpl-' + $id + '" name="txgrpl-' + $name + '" size="' + $size + '" style="' + $style + '" value="' + options.show_text + '" rel="' + $id + '" title="' + options.show_text + '" /&gt;');

 $('#txgrpl-' + $id).focus(function() {
 $(this).hide();
 var $id = $(this).attr('rel');
 $('#' + $id).show().focus();
 });
 }

 jQuery(this).blur(function() {
 if (trim(jQuery(this).val()) == '' || jQuery(this).val() == undefined) {
 var $id = jQuery(this).attr('id');
 jQuery(this).hide();
 $('#txgrpl-' + $id).show();
 }
 });
 });
};
  • Share/Bookmark

Tags: , ,


Mar 01 2010

PHP curl bot to update Facebook status

Category: Php, Spiders & webbotsadmin @ 10:30 pm

I’ve found this great mini bot from Alste blog, and I’ve decided to add it to the mini bot class. This bot uses curl to connect to facebook mobile (m.facebook.com) and perform the login. Then it saves the cookies received from mobile facebook and go to the facebook mobile homepage where it sets the status making a post.
I’ve tried to make the same thing with the normal facebook, but it didn’t work. I think that mobile facebook is simpler and easier to make bots working. I’ve added this bot to the Mini Bot PHP class.

	//
	// change Facebook status with curl
	// Thanks to Alste (curl stuff inspired by nexdot.net/blog)
	public function setFacebookStatus($status, $login_email, $login_pass) {
		$debug = false;
		//CURL stuff
		//This executes the login procedure
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&amp;next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
		curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=' . urlencode($login_email) . '&pass=' . urlencode($login_pass) . '&login=' . urlencode("Log in"));
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
		curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		//make sure you put a popular web browser here (signature for your web browser can be retrieved with 'echo $_SERVER['HTTP_USER_AGENT'];'
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
		curl_exec($ch);

		//This executes the status update
		curl_setopt($ch, CURLOPT_POST, 0);
		curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
		$page = curl_exec($ch);

		curl_setopt($ch, CURLOPT_POST, 1);
		//this gets the post_form_id value
		preg_match("/input type=\"hidden\" name=\"post_form_id\" value=\"(.*?)\"/", $page, $form_id);
		//we'll also need the exact name of the form processor page
		preg_match("/form action=\"(.*?)\"/", $page, $form_num);

		curl_setopt($ch, CURLOPT_POSTFIELDS, 'post_form_id=' . $form_id[1] . '&status=' . urlencode($status) . '&update=' . urlencode("Update status"));
		//set url to form processor page
		curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com' . $form_num[1]);
		curl_exec($ch);

		if ($debug) {
			//show information regarding the request
			print_r(curl_getinfo($ch));
			echo curl_errno($ch) . '-' . curl_error($ch);
			echo "<br><br>Your Facebook status seems to have been updated.";
		}
		//close the connection
		curl_close($ch);
	}
  • Share/Bookmark

Tags: , , , , ,


Next Page »