SimpleXML_append
So far the best function to append an xml element from another SimpleXML object.
/***	SimpleXML Class extension
	to easily append an XML as a child to an existing node
***/
function simplexml_append(SimpleXMLElement $to, SimpleXMLElement $from) {
	$toDom = dom_import_simplexml($to);
	$fromDom = dom_import_simplexml($from);
	$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}
PHP , simplexml
Niels posted this in 11-03-2013 @ 23:05
Generate UUID's with PHP
I found this very useful, fast and complete class to generate UUID's.
By Marius Karthaus
class uuid { 
    /** 
     * This class enables you to get real uuids using the OSSP library. 
     * Note you need php-uuid installed. 
     * On my system 1000 UUIDs are created in 0.0064 seconds. 
     * 
     * @author Marius Karthaus 
     * 
     */ 
    
    protected $uuidobject; 
    
    /** 
     * On long running deamons i've seen a lost resource. This checks the resource and creates it if needed. 
     * 
     */ 
    protected function create() { 
        if (! is_resource ( $this->uuidobject )) { 
            uuid_create ( &$this->uuidobject ); 
        } 
    } 
    
    /** 
     * Return a type 1 (MAC address and time based) uuid 
     * 
     * @return String 
     */ 
    public function v1() { 
        $this->create (); 
        uuid_make ( $this->uuidobject, UUID_MAKE_V1 ); 
        uuid_export ( $this->uuidobject, UUID_FMT_STR, &$uuidstring ); 
        return trim ( $uuidstring ); 
    } 
    
    /** 
     * Return a type 4 (random) uuid 
     * 
     * @return String 
     */ 
    public function v4() { 
        $this->create (); 
        uuid_make ( $this->uuidobject, UUID_MAKE_V4 ); 
        uuid_export ( $this->uuidobject, UUID_FMT_STR, &$uuidstring ); 
        return trim ( $uuidstring ); 
    } 
    
    /** 
     * Return a type 5 (SHA-1 hash) uuid 
     * 
     * @return String 
     */ 
    public function v5() { 
        $this->create (); 
        uuid_make ( $this->uuidobject, UUID_MAKE_V5 ); 
        uuid_export ( $this->uuidobject, UUID_FMT_STR, &$uuidstring ); 
        return trim ( $uuidstring ); 
    } 
} 
You need php-uuid installed, which might cause you to not be able to use this class in some cases.
So here is another v4 UUID generator function I found by Andrew Moore.
function create_uuid() {
	return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
		// 32 bits for "time_low"
		mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
		
		// 16 bits for "time_mid"
		mt_rand( 0, 0xffff ),
		
		// 16 bits for "time_hi_and_version",
		// four most significant bits holds version number 4
		mt_rand( 0, 0x0fff ) | 0x4000,
		
		// 16 bits, 8 bits for "clk_seq_hi_res",
		// 8 bits for "clk_seq_low",
		// two most significant bits holds zero and one for variant DCE1.1
		mt_rand( 0, 0x3fff ) | 0x8000,
		
		// 48 bits for "node"
		mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
	);
}
PHP , UUID
Niels posted this in 23-10-2012 @ 23:05
Excel manipulation through Powershell
Everybody is able to provide workbooks full of data, and somebody has got to process that data.
We all know who Somebody is ..

To get started we need to get a Sheet open from the Workbook.
$xl = new-object -com Excel.Application
$xl.visible = $false
$Workbook = $xl.Workbooks.open("c:\excel.xslx")
$ws = $Workbook.Worksheets.item("sheet 1")
$ws.Activate()
You can set visible to $true, but i personally don't like it.

If you are going to loop through rows you need to set the start end rows.
Get the last row in the sheet like this
($ws.usedRange).Rows.Count
And a function to Get and to Set the Value of a cell will come in handy one day.
function getCellValue{
	param([int]$Row
		,[int]$Column
		,[Object]$Sheet)
	
	return $Sheet.Cells.Item($Row,$column).Value()
}

function setCellValue{
	param([int]$Row
		,[int]$Column
		,[Object]$Sheet
		,$Value)
	
	return $Sheet.Cells.Item($Row,$column) = $Value
}
Closing the application after you are done with Excel is not alwyas too subtle.
$wb.Close($false) # close source workbook without saving
$xl.Quit()
That should have done the trick, but often it doesn`t do so.

Close the process by forcing it, just make sure you set a variable with the current time at the start of the script.
$scriptStart = (Get-Date)
#
# script executing part
#
$xlProcess = Get-Process -Name "*excel*" |?{	$scriptStart -gt $_.StartTime	}
if($xlProcess){
	$xlProcess |%{  Stop-Process -Id $_.Id -Force   }
}
Powershell , Excel , rows , rowcount , cellvalue
Niels posted this in 22-10-2012 @ 23:45
No Invoke-Sqlcmd workaround in Powershell
Invoke-Sqlcmd is a SQL Server cmdlet which might not be available on the system that on which your script will run.
With the following function you can execute any query against a server without the Invoke-Sqlcmd cmdlet installed on the device.
function Invoke-Query{
	param([string]$Server
		,[string]$Query
		,[string]$Database
		,[int]$Timeout = 20)
	$return = $null
	$conn = New-Object System.Data.SqlClient.SqlConnection
	if (!$conn){
		Throw "SqlConnection could not be created!"
		return
	}
	
	$conn.ConnectionString = "Server = $($server); Database = $($Database); Integrated Security = True"
	$conn.Open()
	if ($conn.State -eq 1){
		$cmd = New-Object System.Data.SqlClient.SqlCommand $Query, $conn
		$cmd.CommandTimeout = $Timeout
		$data = New-Object System.Data.SqlClient.SqlDataAdapter
		$ds = New-Object System.Data.DataSet
		$data.SelectCommand = $cmd
		$data.Fill($ds) | Out-Null
		
		$return = $ds.Tables[0]
		
		$ds.Dispose()
		$data.Dispose()
		$cmd.Dispose()
		$conn.Close()
	}

	return $return
}
Set CommandTimeout to 0 for infinite.

Powershell , SQL , Invoke/SQL , Timeout
Niels posted this in 22-10-2012 @ 23:05
mSata to 1.8" Micro Sata
Today I found the adapter that i was looking for, since i bought a Crucial m4 256GB mSata SSD foolishly assuming mSata equals Micro-Sata.
Obviously it does not, an adapter was needed, but the internet choose to not provide much choice in 50mm mSata to 1.8" MicroSata adapters.

The SSD is to replace the 120GB HDD that came with my Samsung X360 ultrabook which needed an upgrade after a while.

Because i spent quite some time looking up this adapter I hope to help someone who is looking for the same.
This is the one : Addonics 1.8" mSATA adapter

mSata , Micro-Sata , 1.8" , SSD , adapter , converter , ultrabook
Niels posted this in 16-08-2012 @ 13:05
HTTP-Authentication through powershell
I came across a project where i had to parse an html page that was accesable only through a webserver that requires authentication.
Using HTTP-Authentication is surprisingly easy with powershell :
$httpCred = New-Object net.NetworkCredential
$httpCred.UserName = "superuser"
$httpCred.Password = "superpass"

$http = New-Object net.WebClient
$http.Credentials = $httpCred

$response = $http.DownloadString("http://secure.domain.tld")
powershell , http , authentication , http-auth , security
Niels posted this in 14-08-2012 @ 23:55
Girlfriend promotion mode
www.katrienligt.nl

withneweyes.katrienligt.nl





Niels posted this in 13-10-2011 @ 01:03
Currently working on getting the hosting page up
After that there are some scripts i want to share on the project page, and some games i want to make.

And all must get phpified for to ensure this website will be used by me.

Soon Gadget!! Soooonnnnn ....
Niels posted this in 13-10-2011 @ 01:03
Any information goes here





http://findicons.com !

Niels posted this in 13-10-2011 @ 01:03