<?php
/**
 * Parse the Google SoC 2008 data available to mentors at
 * http://code.google.com/soc/2008/drupal/open.html
 * in order to include it in a Drupal app
 *
 * @author FG Marand http://drupal.org/user/27985
 * @license GPL2
 *
 * Usage: log to the SoC2008 site as a mentor, save the pages to <your site>/files/soc.html for the list,
 * and to <your site>/files/details.html for a details page
 *
 * and go to <your site>/testcode/list_apps
 *   for the list
 * <your site>/testcode/app_details
 *   for an app details
 *
 * WARNING: this is just unsafe demo code.
 */

function testcode_menu($may_cache)
  {
  $items = array();
  if ($may_cache)
    {
    $items[] = array
      (
      'title'    => 'SoC 2008 - List',
      'path'     => 'testcode/list_apps',
      'access'   => user_access('administer nodes'),
      'callback' => 'testcode_list_apps',
      );
    $items[] = array
      (
      'title'    => 'SoC 2008 - App details',
      'path'     => 'testcode/app_details',
      'access'   => user_access('administer nodes'),
      'callback' => 'testcode_app_details',
      );
    }

  return $items;
  }

/**
 * Parse the page saved from
 * http://code.google.com/soc/2008/drupal/open.html
 * containing the applications list
 *
 * Link back to the individual app pages on Google
 *
 * @return string
 */
function testcode_list_apps()
  {
  $socBase = 'http://code.google.com/soc/2008/drupal/';
  $ret = '';

  $unsafeFile = file_get_contents('files/soc.html');
  @$htmlDom = DOMDocument::loadHTML($unsafeFile);
  $xml = simplexml_import_dom($htmlDom);

  $arApps = array();

  $appList = $xml->xpath('//table[@class="applist"]/tr');
  array_shift($appList);
  foreach($appList as $row)
    {
    /**
     * Note: trim() silently casts the SimpleXML elements to strings
     */

    $class = trim($row['class']) == 'listrequestapp' ? 'Request' : '&nbsp;';

    $link = $row->td[0]->table->tr[0]->td->a;
    $l  = l(trim($link[0]), $socBase . $link['href']);

    $ts = $row->td[0]->table->tr[1]->td;

    $student = trim($row->td[1]);
    $mentor  = trim(strip_tags($row->td[2]->asXml()));
    $score   = trim($row->td[3]);

    $arApps[] = array
      (
      'class'   => $class,
      'link'    => $l,
      'student' => $student,
      'mentor'  => $mentor,
      'score'   => $score,
      );
    }

  /**
   * Now we can format the table as wished
   */
  $header = array(t('Status'), t('Project'), t('Student'), t('Mentor'), t('Score'));

  $ret = theme('table', $header, $arApps);
  return $ret;
  }

/**
 * Display an individual app details, saved from
 * http://code.google.com/soc/2008/drupal/app.html?csaid=<some csaid>
 *
 *
 * @param string $csaid Code Summer App ID
 */
function testcode_app_details()
  {
  $csaid = 'PxIWBwNUVgECNRINH0VSXCxfPURUFg4GUXJfbkJQEQoBBiU%3D%0A'; // sample for the security scanner mentored by chx

  $ret = '';

  $unsafeFile = file_get_contents('files/details.html');
  @$htmlDom = DOMDocument::loadHTML($unsafeFile);
  $xml = simplexml_import_dom($htmlDom);

  $sxForm = $xml->xpath('//form[@action="app.do"]'); // this could provide access to the hidden fields, but we don't need them...

  $sxApplists = $xml->xpath('//table[@class="applist"]');
  $sxMeta        = $sxApplists[0]->tr;
  $sxDescription = $sxApplists[1]->tr;

  /**
   * Parse the meta
   */
  $arMeta = array();
  foreach($sxMeta as $sxMetaRow)
    {
    $key = trim($sxMetaRow->th);
    $val = trim($sxMetaRow->td);
    if (!empty($key))
      {
      $arMeta[$key] = $val;
      }
    }
  // dvr($arMeta);

  /**
   * Parse the description
   */
  $arDescription = array();
  $currentArea = '';
  foreach($sxDescription as $sxDescriptionRow)
    {
    if (isset($sxDescriptionRow->th)) // this is the title for a new area
      {
      $currentArea = trim($sxDescriptionRow->th);
      }
    else // this HAS to be a td
      {
      assert(!empty($currentArea));
      $arDescription[$currentArea] = trim($sxDescriptionRow->td->div); // Warning: this contains HTML
      }
    }

  $ret = array
    (
    'meta' => $arMeta,
    'description' => $arDescription
    );

  $ret = "<pre>" . print_r($ret, true) . "</pre>";
  return $ret;
  }