Archive for the ‘ DEVELOPMENT ’ Category

PHP Regular Expression to validate strings

Hi at all 😉

Since a long time i’m searching for a regex in PHP, that filter some letters as sample names. But the solutions that i’ve found could not filter all letters i need.
The most ones didn’t gets letters like à, ä, æ, and so on, without including the letters explicit in the regex.

These week Lars, my college from iscope 😉 , founds the following solution to take all Unicode characters:

<?php
class Validate {
	
	/**
	 * Checks a String
	 *
	 * @author     Thomas Deuling <tdeuling@gmail.com>
	 *             http://thomas.deuling.org
	 * @param      string $value String to check
	 * @return     boolean Is valid?!
	 */
	public static function lettersOnly($value="") {
		if (!preg_match("/^[\pL]*$/", $value)) return false;
		return true;
	}	
	
	...
	
}
?>

Here some test cases:

<?php
require_once("./validate.class.php");

$testvalue = "gfgsergds";
$function = "lettersOnly";
echo "Validate::".$function."(".$testvalue."): ".(string)Validate::$function($testvalue)."<br>";
// Validate::lettersOnly(gfgsergds): 1

$testvalue = "gfs3467rgds";
$function = "lettersOnly";
echo "Validate::".$function."(".$testvalue."): ".(string)Validate::$function($testvalue)."<br>";
// Validate::lettersOnly(gfs3467rgds):

$testvalue = "g%/)=gsgds";
$function = "lettersOnly";
echo "Validate::".$function."(".$testvalue."): ".(string)Validate::$function($testvalue)."<br>";
// Validate::lettersOnly(g%/)=gsgds):

$testvalue = utf8_decode("aäüöføæhgfd");
$function = "lettersOnly";
echo "Validate::".$function."(".$testvalue."): ".(string)Validate::$function($testvalue)."<br>";
// Validate::lettersOnly(aäüöføæhgfd): 1

?>

Update Subversion PlugIn to 1.6.x within Aptana Studio 3

Before starting the update of subversion, check your Aptana Updates and install them.
Do this in the main menu ‘Help’ -> ‘Check for Updates’. After finishing the update, restart aptana.

Now you are ready to start the update. First of all, add the new Subversion Update site. For that open ‘Help’ -> ‘Preferences’ and select within the Tree ‘Install/Update’->’Available Software Sites’, now add the following update site:
http://subclipse.tigris.org/update_1.6.x

After that, you have to do a second Aptana Update under ‘Help’ -> ‘Check for Updates’. Now you will find some new updates about subversion, select them all.
Agree the terms and do complete the update. After finishing these second update, restart aptana again and that’s it.

Take webcam snapshot with OpenLaszlo

Today i have played a bit with my webcam, and tried to make some pictures and save that as jpeg files.

Click here for a demo 😉

At first we must integrate the webcam within a base structure of openlaszlo code. It’s needed to include some ActionScript libraries to realize our little project.
It’s advisable to implement in the base strukture a switch tag for handle the runtime, and also a passthrough tag for including the ActionScript 3 libraries.
This base structure could be look like:

<?xml version="1.0" encoding="UTF-8" ?>
<canvas width="100%" height="100%">
	
	<switch>
		<unless property="$as3">
			<handler name="oninit">
			<![CDATA[
				Debug.error("ActionScript 3 required");
			]]>
			</handler>				
		</unless>
		<otherwise>
		
			<passthrough>				
				import flash.net.*;
				import com.adobe.images.JPGEncoder;
				import flash.utils.ByteArray;
				import flash.display.Bitmap;
				import flash.display.BitmapData;
				import flash.media.Camera;
				import flash.geom.Matrix;
			</passthrough>

		</otherwise>
	</switch>
		
</canvas>

After that we create some views and include a videoview and a camera-tag inside.

<simplelayout axis="y" spacing="0" />
<view name="videoView" width="$once{canvas.imageWidth*2}" height="$once{canvas.imageHeight}">
	<simplelayout axis="x" spacing="0" />
	<view name="videoView" width="$once{canvas.imageWidth}" height="$once{canvas.imageHeight}">
		<videoview name="video" oninit="this.stream.broadcast()" width="$once{canvas.imageWidth}" height="$once{canvas.imageHeight}">
			<camera name="webcam" show="true" width="$once{canvas.imageWidth}" height="$once{canvas.imageHeight}" bandwidth="0" picturequality="1.0" />
		</videoview>
	</view>
	
	<view name="snapshot" width="$once{canvas.imageWidth}" height="$once{canvas.imageHeight}" />
</view>
			
<view layout="axis: x; spacing:0">
	<button text="Take a snapshot" onclick="canvas.takeSnapshot();" width="$once{canvas.imageWidth}" />
	<button text="Save snapshot" onclick="canvas.saveImage();" width="$once{canvas.imageWidth}" />		
</view>

I don’t know what i do wrong, but i have the feeling that the bandwidth and quality changes does not work (using lps 4.9.0). So, i additional change these values within the oninit handler. If you have a tip, or know what’s the problem of that, please contact me 😉
Here now my oninit handler:

<handler name="oninit">
<![CDATA[
	
	// Get the webcam 
	var cam:Camera = Camera.getCamera();
	
	// Specifies the maximum amount of bandwidth that the current outgoing video feed can use, in bytes per second. To specify that Flash Player video can use as much bandwidth as needed to maintain the value of quality , pass 0 for bandwidth . The default value is 16384.
	var bandwidth:int = 0; 
	
	// This value is 0-100 with 1 being the lowest quality. 
	// Pass 0 if you want the quality to vary to keep better framerates
	var quality:int = 100; 
	
	// Set bandwidth and quality
	cam.setQuality(bandwidth, quality);
	
	// Set measurements and framerate
	cam.setMode(canvas.imageWidth, canvas.imageHeight, 30, false); 
	
]]>
</handler>

For taking a snapshot from the webcam, we write a small function:

<method name="takeSnapshot">
<![CDATA[
	// create a new bitmap object
	var tempSnapshot:BitmapData =new BitmapData(canvas.imageWidth, canvas.imageHeight, true, 0x00FFFFFF);
	tempSnapshot.draw(canvas.videoView.videoView.video.getDisplayObject(), new Matrix());
	
	// place the snapshop i a view
	var targetView = canvas.videoView.snapshot.getDisplayObject();
	targetView.addChild(new Bitmap(tempSnapshot));
	
	canvas.setAttribute("shotAvailable", true);	
]]>
</method>	

Further we want save the snapshot at a local machine. Therefore we must convert the snapshot into a image and send it to the client.

In order to convert the snapshot into a image, we need the JPGEncoder ActionScript library from Mike Chambers.
Some days ago i write about, how to integrate this in OpenLaszlo. You will find the article here.
Here now the function for converting the snapshot into a image:

<method name="saveImage">
<![CDATA[
	
	if(canvas.shotAvailable) {
		
		// Take the snapshot movieclip
		var sketch_mc = canvas.videoView.snapshot.getDisplayObject();
		
		// Get the image data from that
		var jpgSource:BitmapData = new BitmapData(sketch_mc.width, sketch_mc.height);
		jpgSource.draw(sketch_mc);
		
		// Create a Jpeg from the image data
		var jpgEncoder:JPGEncoder = new JPGEncoder(85);
		var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
		
		// Create an URLRequest with the image
		var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
		var jpgURLRequest:URLRequest = new URLRequest("./snapShotHandle.php?name=sketch.jpg");
		jpgURLRequest.requestHeaders.push(header);
		jpgURLRequest.method = URLRequestMethod.POST;
		jpgURLRequest.data = jpgStream;
		
		// Send the image for download
		navigateToURL(jpgURLRequest, "_blank");
	}
	
]]>
</method>	

For saving the image, we use use a small PHP script like:

<?php

// create filename
$filename = date("Y-m-d_H:i:s", time())."_".uniqid(mt_rand(), true).".jpg";

if(isset($GLOBALS["HTTP_RAW_POST_DATA"])) {
	
	// add headers for download dialog-box
	header('Content-Type: image/jpeg');
	header("Content-Disposition: attachment; filename=".$filename);
	echo $GLOBALS["HTTP_RAW_POST_DATA"];

}

?>

That’s all you need…
Click here for a demo 😉

Sourcecode: OpenLaszlo - SnapShot.zip (1053)

If you found some bugs or have some Tips, please contact me.

Flush DNS-Cache and change DNS-Server in Ubuntu

During i do today move a internet site from one server to another one, i’ve the problem that the DNS of this site isn’t up to date.
So i wanted to flush my local DNS cache, but how?

After a bit searching with google, i collect the needed informations for doing this in Ubuntu.
At first it’s needed to install nscd by using the following command:

sudo aptitude install nscd

After this, we are able to flush the DNS Cache with the following command:

sudo /etc/init.d/nscd restart

This should be enough, to become a current DNS dissolving. But in my case it shows after a few test, that our intranet router also caches the DNS. So, i must change my local DNS entry to another one, that is more up to date.

For this, we must editing the resolv.conf:

sudo nano /etc/resolv.conf 

Therein we must change the IP of the nameserver:

#nameserver 192.168.42.3
nameserver 208.67.222.222

After just a few seconds the system should be automatically changed, and your network requests goes to the new DNS server.

Use ActionScript 3 libraries within OpenLaszlo

Today i tried to integrate an ActionScript 3 within OpenLaszlo.
In detail i needed a JPEGEncoder that is integrated in the ActionScript 3 Library from Mike Chambers.

You can download the library here:
https://github.com/mikechambers/as3corelib

The integration is very easy. First of all navigate into your lps root folder, and create a folder named ‘flexlib’. Then download the ActionScript 3 Library and place the as3corelib.swc in it.

That’s all.
Now you are able to integrate as sample the JPGEncoder Library:

<?xml version="1.0" encoding="UTF-8" ?>
<canvas width="100%" height="100%">
	
	<switch>
		<unless property="$as3">
			<handler name="oninit">
			<![CDATA[
				Debug.error("ActionScript 3 required");
			]]>
			</handler>				
		</unless>
		<otherwise> 
		
			<passthrough>		
				import com.adobe.images.JPGEncoder;			
			</passthrough>

			...

		</otherwise>
	</switch>
		
</canvas>

OpenLaszlo SWF10/Flash Download with a fileReference

To download a file with script within a flash movie, you can use a FileReference.

Two points needed attention:
1. The download-event must be explicit fired, as sample by a onclick event.
2. The security requirements by the adobe flash player must be met. This mean that if the downloading file is a remote file on a separate server, the server needs a crossdomain.xml file with the needed security configuration.

Here’s a small sample for a download within a flash movie generated with OpenLaszlo:

<?xml version="1.0" encoding="UTF-8" ?>
<canvas name="URLRequestDownload" width="100%" height="100%">
		
	<switch>
		<unless property="$as3">
			<handler name="oninit">
			<![CDATA[
				Debug.error("ActionScript 3 required");
			]]>
			</handler>				
		</unless>
		<otherwise>
		
			<button text="download">
				<passthrough>
					import flash.net.FileReference;
					import flash.net.URLRequest;
				</passthrough>
					
				<handler name="onclick">
				<![CDATA[
					var fileref:FileReference = new FileReference();
					var request:URLRequest = new URLRequest("http://thomas.deuling.org/wp-content/plugins/download-monitor/download.php?id=1");
					fileref.download(request, "2011-05-04_openlaszlo_lineheight.zip");
				]]>
				</handler>
			</button>
	
		</otherwise>
	</switch>
		
</canvas>

Here’s to a sample for a crossdomain.xml that allows every access:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-access-from domain="*" to-ports="*" secure="false"/>
  <allow-http-request-headers-from domain="*" headers="*" secure="false" />
</cross-domain-policy>

Tip:
If you’re not sure if the flash player needs a crossdomain.xml, or where he is call the crossdomain.xml, use the firebug in your firefox and look at the network-panel. There you can see every request to the crossdomain.xml. 😉

As an extentions for this, you could catch the ProgressEvent for display the download progress and many more.

You will find all the other available events and many informations here.

laszlocode.com is online again

Just i’m surfed through the internet, visited a few old bookmarked sites, and what do i see 🙂
laszlocode.com is online again.
That’s very nice!

laszlocode.com is a small website, she provides that everyone can share his programmed laszlo code. The website was created in the beginning of 2008 by Nick and the guys from mobiledatanow, and after something a year it was stopped, because they didn’t have the time.

I browsed within the code, and see.. there is still a snippet of laszlo code that i did post there in july of 2008.
It’s just a little multiline tooltip, but it’s nice to find it still in the internet. 🙂

I’m pleased to find more smart laszlo scripts on that website in future. So stay tuned, and dont forget to visit the laszlocode site regular! 🙂

Change line-height in OpenLaszlo Flash/SWF10 Runtime

When you’re using OpenLaszlo by compiling to flash/swf10 Flash, the lineheight attribute within the text class is readonly. That’s sometimes problematic, if you getting strict specifications from your designer for create a website or GUI (graphic user interface). The designer wants that you do implement all the typographic specification that he gives you, pixel by pixel.

For this case, i’ve searched for a workaround to change the lineheight within text fields.
The result is the following class:

<?xml version="1.0" encoding="UTF-8" ?>
<library>
	<class name="lineHeightText" extends="text" multiline="true">
		
		<switch>
			<unless property="$as3">
				<method name="setLineHeight" args="newLineHeight:Number =0">
				<![CDATA[
					Debug.error("setLineHeight is not supported");
				]]>
				</method>				
			</unless>
		    <otherwise>
				<passthrough>
					import flash.text.*;
				</passthrough>
				
				<method name="setLineHeight" args="newLineHeight:Number =0, refreshHeight:Boolean =true">
				<![CDATA[
				
					// get an reference of this text
					var test = this.getDisplayObject();
					
					// set new line-height
					var format:TextFormat = new TextFormat();
					format.leading = newLineHeight-test.lineheight;
					
					// set line-height in the sprites
					test.lineheight = newLineHeight;
										
					// make this textfield bigger
					if(refreshHeight) this.setAttribute("height", newLineHeight*test.textfield.numLines);
					
					// set the new format
					test.textfield.setTextFormat(format);					
					test.textfield.defaultTextFormat = format;
								
				]]>
				</method>
				
			</otherwise>
		</switch>
		
	</class>
</library>

The usage is very simple:

<?xml version="1.0" encoding="UTF-8" ?>
<canvas name="lineHeight" width="100%" height="100%">
		
	<include href="./lineHeightText.lzx" />
	
	<lineHeightText fontsize="11" fgcolor="#000000" bgcolor="#abcdef" x="10" y="10" name="textarea1" width="200" selectable="true">
		[start]Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.[end]
	</lineHeightText>
	
	<lineHeightText fontsize="11" fgcolor="#000000" bgcolor="#abcdef" x="220" y="10" name="textarea2" width="200" selectable="true">
		<handler name="oninit">
		<![CDATA[
			this.setLineHeight(20);
		]]>
		</handler>
		[start]Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.[end]
	</lineHeightText>	

</canvas>

I hope this can help somebody.
If you find some errors, or have some questions or tips, write me.. 😉

Sourcecode: OpenLaszlo - LineHeight.zip (891)

Configure Aptana Studio 3

All descripted configurations are setted under ‘Window’ -> ‘Preferences’.

Font-Face within the Project-Explorer and Outline perspective:

The Project-Explorer and Outline perspective in Aptana Studio 3 uses a monotype font. That’s not so good, because the fixed-width of each letter let be the text often very long.
To change these font, go to ‘General’ -> ‘Appearance’ -> ‘Colors and Fonts’ within the Tree, and change the font at ‘Basics’ -> ‘Text Font’ -> ‘App Explorer/Outline View Font’ into your prefered proportional font.

Changing the color theme:

I don’t like the dark color and syntax hightlight theme, which seems to be standard in Aptana Studio 3. But we can switch back to the classic Eclipse one.
You will find it within the tree at ‘Aptana’ -> ‘Themes’, then select Eclipse as Theme.

Select Firefox as webbrowser:

To select the Firefox as the default Browser in Aptana Studio 3, navigate within the tree to ‘General’ -> ‘Web Browser’, then set the radiobutton to ‘Use external web browser’. Add a new Web Browser by typing name: ‘Firefox’ and in my case location: ‘/usr/lib/firefox’.

Notice:
If you are not sure which short cuts you can use within a perspective, just press Ctrl+Shift+L to see a full list of the currently available key bindings.

If you want to switch between different perspectives, just press ALT+SHIFT+Q to display a full list of perspectives. In this list you can use the up/down keys to navigate, or direct press the last short cut key, to switch in one other perspective.

Configure Subversion PlugIn within Aptana 3

In order to work more effective and faster with Aptana Studio, i will bind some short cuts to the most often used subversion functions.

Open the preferences under ‘Window’ -> ‘Preferences’. Select within the tree ‘General’ -> ‘Keys’ and use the quick search for searching the SVN entries.

To bind a short cut to a SVN function, select one entry as sample ‘cleanup’. Then press within the binding textfield the short cut that should use for it, and press apply.

I’m using the following short cuts:

cleanup ALT+C
update ALT+U
commit ALT+S

Notice:
With Aptana Studio 2 were these short cuts directly active. But now in case of Aptana Studio 3, it seems that the short cuts aren’t catched. I resolved this issue by going to ‘Window’ -> ‘Customize Perspective’. Then, under the ‘Command Groups Availability’ tab, enabled SVN. Do this steps for all perspectives, where you need the short cuts. Don’t forget to save the perspective.
Then all the keyboard shortcuts worked.