0. What is the PAD SDK?

The PAD SDK is a set of include files implementing some classes as an easy to integrate interface on PAD files. This part of the SDK includes PHP sample files in the samples sub-directory along with the include files in the include sub-directory.

1. Extracting Data from a PAD File

Extracting data from a PAD file with the PAD SDK is very simple. Authors may want to use this code on their web pages to extract data like the current version number directly from the program's PAD file. This obsoletes having to update the web page on program updates.

Here is the sample code for extracting the product name and version from a local PAD file:

<?php

// Includes
include_once("./include/padfile.php");

// Create PAD file object for the local file "pad_file.xml"
// This could also be an absolute local path or an URL!
$PAD = new PADFile("./samples/pad_file.xml");

// Load file
if ( !$PAD->Load() )
  echo 
"Error: Cannot load PAD file.<br>\n";

// Sample Output
echo "<b>Program Name:</b> " $PAD->XML->GetValue("XML_DIZ_INFO/Program_Info/Program_Name") . "<br>\n";
echo 
"<b>Program Version:</b> " $PAD->XML->GetValue("XML_DIZ_INFO/Program_Info/Program_Version") . "<br>\n";

?>

And this is how the output will look like:

Program Name: Sample Prog
Program Version: 1.0

2. Prefilling a Form With the Data From a PAD File

A common task when implementing PAD support for a download archive is to allow the authors to enter a PAD URL and prefill a form with the data from that PAD file. This enables both manual and PAD based submissions.

Look at the sample code:

<?php
                     
// Includes
include_once("./include/padfile.php");


// Read input
$URL = @$_POST["URL"];
if ( 
$URL == "" $URL "http://";

// Create PAD file object
$PAD = new PADFile($URL);

?>
<form method='POST' action='<?php echo $_SERVER["PHP_SELF"]; ?>#prefill'>
  <table>
    <tr>
      <td>PAD URL:</td>
      <td>
        <input type='text' name='URL' size='60' value='<?php echo $PAD->URL?>'>
        <input type='submit' value='Prefill Form!'><br>
        &nbsp;<br>
<?php

  
// If the form above has been posted, load the PAD file from the entered URL
  
if ( $URL != "http://" )
  {
    echo 
"Loading <i>" $PAD->URL "</i> ... ";
    
$PAD->Load();
    switch ( 
$PAD->LastError )
    {
      case 
ERR_NO_ERROR:
        echo 
"<font color='green'>OK</font>\n";
        break;
      case 
ERR_NO_URL_SPECIFIED:
        echo 
"<br><font color='red'>No URL specified.</font>";
        break;
      case 
ERR_READ_FROM_URL_FAILED:
        echo 
"<br><font color='red'>Cannot open URL.";
        if (
$PADFile->LastErrorMsg != "")
          echo 
" (" $PADFile->LastErrorMsg ")";
        echo 
"</font>";
        break;
      case 
ERR_PARSE_ERROR:
        echo 
"<br><font color='red'>Parse Error: " $PAD->ParseError "</font>";
        break;
    }
  }

  
// Now, we prefill every form field with the data from the PAD file - if available
  // For program descriptions, we will use the GetBestDescription() method that will
  // extract the description text from the PAD file that fits best to the given
  // length and language.

?>
      </td>
    </tr>
    <tr>
      <td>Program Name:</td>
      <td><input name='ProgramName' type='text' size='60'
                 value='<?php echo $PAD->XML->GetValue("XML_DIZ_INFO/Program_Info/Program_Name"); ?>'></td>
    </tr>
    <tr>
      <td>Program Version:</td>
      <td><input name='ProgramVersion' type='text' size='60'
                 value='<?php echo $PAD->XML->GetValue("XML_DIZ_INFO/Program_Info/Program_Version"); ?>'></td>
    </tr>
    <tr>
      <td>Description:</td>
      <td><textarea name='ProgramDescription' cols='42'><?php echo $PAD->GetBestDescription(250"English"); ?></textarea></td>
    </tr>
  </table>
</form>

And here is the life demo:
(For example, a valid PAD URL is: http://pad.asp-software.org/padsdk/php/samples/pad_file.xml)

PAD URL:
 
Program Name:
Program Version:
Description:

3. Validating a PAD File Against the PAD Specification

The PAD SDK can also be used to validate a PAD file against the PAD specification. Archive webmasters might want to use this functionality to display validation errors on submissions of PAD files and might want to reject PAD files with certain errors.

Again, here is the sample code:

<?php

// Includes
include_once("./include/padfile.php");
include_once(
"./include/padvalidator.php");

// Read input
$URL = @$_POST["ValidateURL"];
if ( 
$URL == "" $URL "http://";

// Print the form
?>
<form method='POST' action='<?php echo $_SERVER["PHP_SELF"]; ?>#validate'>
  PAD URL:
  <input type='text' name='ValidateURL' size='60' value='<?php echo $URL?>'>
  <input type='submit' value='Validate!'>
</form>
<p>
<?php

// Create PAD file object
$PAD = new PADFile($URL);

// If the form above has been posted, load the PAD file from the entered URL
if ( $URL != "http://" )
{
  echo 
"Loading <i>" $PAD->URL "</i> ... ";
  
$PAD->Load();
  switch ( 
$PAD->LastError )
  {
    case 
ERR_NO_ERROR:
      echo 
"<font color='green'>OK</font>\n";
      break;
    case 
ERR_NO_URL_SPECIFIED:
      echo 
"<br><font color='red'>No URL specified.</font>";
      break;
    case 
ERR_READ_FROM_URL_FAILED:
      echo 
"<br><font color='red'>Cannot open URL.";
      if (
$PADFile->LastErrorMsg != "")
        echo 
" (" $PADFile->LastErrorMsg ")";
      echo 
"</font>";
      break;
    case 
ERR_PARSE_ERROR:
      echo 
"<br><font color='red'>Parse Error: " $PAD->ParseError "</font>";
      break;
  }

  
// Output
  
echo "<h4>Validation Report for <i>" $PAD->URL "</i></h4>\n";

  
// Create validator
  
$PADValidator = new PADValidator("./include/pad_spec.xml");
  if ( !
$PADValidator->Load() )
    echo 
"Error loading Validator.";
  else
  {
    
// Validate
    
$nErrors $PADValidator->Validate($PAD);
    if ( 
$nErrors == )
      
// No errors
      
echo "<b><font color='green'>No Errors</font></b>";
    else
    {
      
// Print validation errors
      
echo "<b><font color='red'>" $nErrors " Errors</font></b>";
      foreach(
$PADValidator->ValidationErrors as $err)
      {
        echo 
"<p><b><font color='red'>Error:</font></b><br>";
        
$err->Dump();
        echo 
"<br>&nbsp;</p>";
      }
    }
  }
}

?>

And the life demo:
(Sample URL: http://pad.asp-software.org/padsdk/php/samples/pad_file.xml)

PAD URL: