Category Archive: Php

Subcategories: No categories

WebPasswd

webpasswdWebPasswd is a password app that I have been working on, its a self hosted PHP app, that stores usernames and password (along with notes) in an encrypted file.

I’ve released it under the MIT license and available on github at https://github.com/sam159/WebPasswd

 

PKCS#5 Padding with PHP

Here is a quick class for dealing with PKCS#5 style padding.

This padding is useful for encrypting data with DES/AES which have a multi-byte block size. MCrypt will by default pad out to the next block boundary with Null bytes which will show up in the decrypted data.

A good explanation can be found here.

class PKCS5Padding {
  public static function Pad($unpaddedString, $blockSize) {
    $additionalChars = $blockSize - strlen($unpaddedString) % $blockSize;

    $char = chr($additionalChars);

    return $unpaddedString.str_repeat($char, $additionalChars);
  }
  public static function UnPad($paddedString) {
    $additionalChars = ord(substr($paddedString, -1, 1));

    return substr($paddedString, 0, -$additionalChars);
  }
}

//Example use. $td is the resource returned by mcrypt_module_open
$paddedData = PKCS5Padding::Pad($data, mcrypt_enc_get_block_size($td));

PHP: Dpi of a png image

I recently needed to determine the DPI of a set of PNG files (so that I could convert the size to points), with the exception of imagemagick there is no way to do this.

So here is a pure-php method of extracting the DPI from a PNG file, it searches for the pHYs chunk (which may or may not exist).

function read_png_dpi($source)
{
  $fh = fopen($source, 'rb');
  if (!$fh)
    return false;
  
  $dpi = false;
  
  $buf = array();
  
  $x = 0;
  $y = 0;
  $units = 0;
  
  while(!feof($fh)) {
    array_push($buf, ord(fread($fh, 1)));
    if (count($buf) > 13)
      array_shift($buf);
    if (count($buf) < 13)
      continue;
    if ($buf[0] == ord('p') &&
        $buf[1] == ord('H') &&
        $buf[2] == ord('Y') &&
        $buf[3] == ord('s'))
    {
      $x = ($buf[4] << 24) + ($buf[5] << 16) + ($buf[6] << 8) + $buf[7];
      $y = ($buf[8] << 24) + ($buf[9] << 16) + ($buf[10] << 8) + $buf[11];
      $units = $buf[12];
      break;
    }
  }
  
  fclose($fh);
  
  if ($x == $y)
    $dpi = $x;
    
  if ($dpi != false && $units == 1) //meters
    $dpi = round($dpi * 0.0254);
  
  return $dpi;
}

Database (over) usage

(Rant about drupal not django btw)
Since creating this website (in django) ive been interested in how it uses the database, dajango doesnt provide an easy way to show this on a per page basis, so instead i chose to use the log feature in MYSQL.

To enable it add log=/var/log/mysqld.log to your my.cnf file in the [mysqld] section and then restart the database.
You may not want to leave this on indefinably, as it spits out every database query (leading to a large logfile)
(more…)

PHP Soap Server, Part 2: PHP Server and Client

This is in conclusion to Part 1

The example soap service that i will build for demonstration purposes will be a simple schedule service, that can be queried to find out what meeting is next and todays meeting schedule.

Sorry for taking so long to put this out, but better late than never, eh?

All the code/wsdl/xml is available here
And the demo client (Source Code) and server (Source Code)

(more…)

New Website (Software)

Welcome to the new sam.xnet.tk, running on my new blogging sotware using python+django!

Basic but fully functional.

Ill release more info soon

EDIT: when i say fully functional i mean missing trackbacks and rss… ;)

EDIT2: The RSS is now up and working at https://sam.xnet.tk/rss/

EDIT3: Now with a full rss feed for rss readers https://sam.xnet.tk/rss/full/

PHP Soap Server, Part 1: Introduction to WSDL

In this multi-part blog im going to talk through how to set up a php soap (Simple Object Access Protocol) server that can be added as a web-reference through visual studio. Im mainly doing this because it took me along time to figure out the finer details of wsdl and soap, and i’d like to try and save other people the same hassle.

The example soap service that i will build for demonstration purposes will be a simple schedule service, that can be queried to find out what meeting is next and todays meeting schedule.

All the code/wsdl/xml is available here
And the demo client and server

PHP Soap Server, Part 2: PHP Server and Client (more…)

Converting text files into a navigable collection with php

Say you have a collection of text files that you want to be accessible over the internet, but dont want to convert each one, add navigation etc…
This is where txt2dir comes in, this is a small php script I made that can take that collection of text files and convert them to html on demand including navigation and an index! (more…)

PHP and SQL injection

Ok, being someone who likes php, i get annoyed at the many sites trying to teach people the language who have example login scripts that use something like:

$result=mysql_query("select * from users where Username=$username and Password=$password");
if (mysql_num_rows($result) < 1)
    blah blah blah

Whats wrong with that you ask? Well imagine what would happen if someone were to use the username '' or 1=1 # (more…)

Blueprint

Blueprint is a php web application I made for the online game Eve-Online, a while back i was building and selling ships and i wanted to know how much materials would cost and to be able to find out what my profit was going to be, and this was the result. (more…)