WELCOME TO THE SICKBIKE SMS SERVICE (S3)

Here lies the dead code that formerly powered S3. More information can be found back at sick.bike.


<?php
error_reporting(-1);
ini_set('display_errors', 1);

$request_body = file_get_contents('php://input');
$xml = simplexml_load_string($request_body);
if (!isset($xml->methodName))
  failure('Must make a request.');

switch($xml->methodName) {
  # Wordpress blog verification
  case 'mt.supportedMethods':
    success('metaWeblog.getRecentPosts');
    break;

  # First authentication request from ifttt
  case 'metaWeblog.getRecentPosts':
    # Send a blank blog response
    # This also makes sure that the channel is never triggered
    success('<array><data></data></array>');
    break;

  case 'metaWeblog.newPost':
    # Required static parameters
    $payload = array('action'=>'edit', 'post'=>'1', 'prepend'=>'1');
    # Login information
    # see http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost
    $payload['authid'] = (string)$xml->params->param[1]->value->string;
    $payload['authpw'] = (string)$xml->params->param[2]->value->string;
    $content = $xml->params->param[3]->value->struct->member;

    foreach($content as $data) {
      switch((string)$data->name) {
        # Unused sections
        case 'post_status':
        case 'mt_keywords':
        case 'categories':
          break;

        # Post body is text to prepend
        case 'description':
          $payload['text'] = (string)$data->value->string;
          break;

        # Post title is the page name
        default:
          $payload['n'] = (string)$data->value->string;
      }
    }

    # Make the webrequest
    $url = 'http://sick.bike/pmwiki/pmwiki.php';
    $options = array(
       'http' => array(
          'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
          'method' => 'POST',
          'content' => http_build_query($payload),),);
    $context = stream_context_create($options);
    $response = file_get_contents($url, false, $context);

    if ($response)
      success('<string>Request was successful.</string>');
    else
      failure($response);
    break;
}

function output($xml) {
  $length = strlen($xml);
  header('Connection: close');
  header('Content-Length: ' . $length);
  header('Content-Type: text/xml');
  header('Date: ' . date('r'));
  echo $xml;
  exit;
}

function success($innerXML) {
  $xml = <<<EOD
<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
      <value>
      $innerXML
      </value>
    </param>
  </params>
</methodResponse>
EOD;

  output($xml);
}

function failure($status) {
  $xml = <<<EOD
<?xml version="1.0"?>
<methodResponse>
  <fault>
    <value>
      <struct>
        <member>
          <name>faultCode</name>
          <value><int>$status</int></value>
        </member>
        <member>
          <name>faultString</name>
          <value><string>Request was not successful.</string></value>
        </member>
      </struct>
    </value>
  </fault>
</methodResponse>
EOD;

  output($xml);
}