view createdb.php @ 79:b75f99a8973c

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 17 Oct 2013 18:44:16 +0300
parents 02de5197a232
children e51205f2bdd0
line wrap: on
line source

#!/usr/bin/php
<?
require "mconfig.inc.php";
require "msite.inc.php";

//
// CLI utility helper functions
//
function stCArg($index)
{
  global $argc, $argv;
  if ($index < $argc)
    return $argv[$index];
  else
    return FALSE;
}

function stCArgLC($index)
{
  global $argc, $argv;
  if ($index < $argc)
    return strtolower($argv[$index]);
  else
    return FALSE;
}


function stCSQLError($sql)
{
  global $db;
  die("Error executing SQL query: ".implode("; ", $db->errorInfo())." in statement \"".$sql."\"\n");
  exit;
}


// Check if we are running from commandline or not
if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"]))
{
  header("Status: 404 Not Found");
  die();
}



// The defaults we put in
$siteDefaults = array(
  "maxAttendees"     => array(VT_INT, 60, "Maximum attendees (soft limit)"),

  "userTimeout"      => array(VT_INT, 120, "User pages (voting, entry submission) timeout in minutes"),
  "admTimeout"       => array(VT_INT, 15, "Administration interface timeout in minutes"),

  "showAdmin"        => array(VT_BOOL, false, "Always show administration interface link on the menu"),
  "showAttendees"    => array(VT_BOOL, false, "Show attendees list"),
  "allowRegister"    => array(VT_BOOL, false, "Enable event registration"),
  "allowVoting"      => array(VT_BOOL, false, "Enable voting (individual compos must be enabled as well)"),

  "showResults"      => array(VT_BOOL, false, "Enable results page"),
  "showResultsASCII" => array(VT_BOOL, true,  "Show results as ASCII instead of HTML table"),
  "showResAuthors"   => array(VT_BOOL, false, "Show entry authors on results page"),

  "requireEMail"     => array(VT_BOOL, false, "Require e-mail address in registrations"),

  "registerInfoText" => array(VT_TEXT, "<p>
Only your <b>handle</b> and the answer to the botcheck are strictly required.
If you plan on joining the IRC channel
(<a href=\"irc://#fap2013@ircnet\">#fap2013 @ IRCNet</a>) or staying up to date by other means,
<b>e-mail</b> is not required either.
</p>", "Registration page info text"),

  "registerPostText" => array(VT_TEXT, "<p>Now go make a demo about it!</p>", "Successful post-registration note text"),

  "registerPostNoEmail" => array(VT_TEXT, "
<h2>By the way ...</h2>
<p>As you did not specify an e-mail contact address, you'll have to get updates
and information about the location (if you don't already know it) by 
some other means (IRC, for example.)</p>
", "No e-mail address registration note"),

  "eventDescription" => array(VT_TEXT, "
<h1>Event program &amp; schedule</h1>
<ul>
 <li><b>Aegis</b> of DSS and FAG will be performing a DJ gig.</li>
 <li>.. and possible additional live acts. More info to come.</li>
</ul>

<h2>Friday 30.11.</h2>
<ul>
 <li><b>18:00</b> - <i>Doors open</i>.</li>
 <li><b>22:00</b> - DJ set by Aegis.</li>
</ul>

<h2>Saturday 1.12.</h2>
<ul>
 <li><b>14:00</b> - Deadline for remote entries.</li>
 <li><b>18:00</b> - Deadline for the entries delivered on location.</li>
 <li><b>20:00</b> - Competitions start.</li>
</ul>

Competition schedule and voting deadline will depend on number of entries.

<h2>Sunday 2.12.</h2>
<ul>
 <li><b>12:00</b> - Party over?</li>
</ul>
", "Event general description / timetables etc."),


  "compoDescription" => array(VT_TEXT, "
<h1>General</h1>
<p class=\"notice\">
YOU <b>MUST</b> HAVE AT LEAST ONE ENTRY TO COMPETITIONS IF YOU COME TO THE PARTY.
</p>

<p class=\"note\">
If there are enough entries, then AGA/OCS/ECS demos will be run in separate compos.
<br />
Remote entries are welcome!
</p>

<p>
The compo machine will be an <b>A1200 with an 060/50 and lots of
RAM</b>. An <b>A500 1.3 512k/512k</b> will also be available if your
prod is not AGA compatible.
</p>

<h1>Compos</h1>
", "Compo general description"),


  "siteInfoText"     => array(VT_TEXT, "
<a href=\"news\">
<img src=\"img/fapsm.png\" alt=\"Finnish Amiga Party 2013\" class=\"logo\" /></a>
<div id=\"date\">
5.-8.12.2013<br />
Helsinki, Finland<br />
@ old location<br />
<span class=\"notice\">Entry 15 EUR + prod</span>
</div>", "Site header text"),


  "newsDescription"  => array(VT_TEXT, "
<div style=\"text-align: center;\">
<img src=\"img/fap.png\" alt=\"FAP\" />
<p>
Pure Amiga demoscene party, all traditional Amiga compos and purely Amiga-oriented program.
<br />
<span class=\"notice\">
YOU <b>MUST</b> HAVE AT LEAST ONE ENTRY TO COMPETITIONS IF YOU COME TO THE PARTY.
</span>
</p>
</div>",
  "News page header text"),


  "entrySubmitInfo" => array(VT_TEXT, "Rules for entry submission ... ", "Entry submission information blurb"),

  "siteMenuHeader" => array(VT_TEXT, "<div>13.FAP:> <span class=\"mblink\">&#9632;</span></div>", "Site menu header text"),
  "siteMenuFooter" => array(VT_TEXT, "", "Site menu footer text"),
);

$sqlTables = array(
  "settings" => "key VARCHAR(32) PRIMARY KEY, vtype INT, vstr VARCHAR(128), vtext TEXT, vint INT, desc VARCHAR(128)",
  "news" => "id INTEGER PRIMARY KEY AUTOINCREMENT, utime INT, title VARCHAR(128), text VARCHAR(4096), author VARCHAR(64), persist INT DEFAULT 0",
  "compos" => "id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128), description VARCHAR(4096), visible INT DEFAULT 0, voting INT DEFAULT 0, showAuthors INT DEFAULT 0",
  "entries" => "id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(64), author VARCHAR(64), compo_id INT DEFAULT NULL, filename VARCHAR(256) DEFAULT NULL",

  "attendees" => "id INTEGER PRIMARY KEY AUTOINCREMENT, regtime INT, name VARCHAR(64), groups VARCHAR(64), oneliner VARCHAR(64), email VARCHAR(80), votekey VARCHAR(64), active INT DEFAULT 0",

  "votes" => "id INTEGER PRIMARY KEY AUTOINCREMENT, entry_id INT DEFAULT NULL, voter_id INT DEFAULT NULL, value INT DEFAULT 0",
);


//
// Helper functions
//
function stConnectDB($dbspec)
{
  global $db;
  try {
    $db = new PDO($dbspec);
  }
  catch (PDOException $e) {
    error_log("Could not connect to SQL database '".$dbspec."': ".$e->getMessage().".");
    return FALSE;
  }
  return TRUE;
}


function stCreateOneTable($name, $schema)
{
  return (stExecSQL("CREATE TABLE ".$name." (".$schema.")") !== FALSE) ? TRUE : FALSE;
}


function stCreateTables()
{
  global $sqlTables;
  echo "Creating tables...\n";
  foreach ($sqlTables as $name => $schema)
  {
    echo " - '".$name."'\n";
    if (!stCreateOneTable($name, $schema))
      return FALSE;
  }
  return TRUE;
}


function stAddSettings()
{
  global $siteDefaults;
  echo "Adding settings to settings table.\n";
  foreach ($siteDefaults as $key => $value)
  {
    switch ($value[0])
    {
      case VT_TEXT: $type = "%s"; $var = "vtext"; break;
      case VT_STR:  $type = "%s"; $var = "vstr"; break;
      case VT_BOOL: $type = "%b"; $var = "vint"; break;
      case VT_INT:  $type = "%d"; $var = "vint"; break;
    }

    $sql = stPrepareSQL(
      "INSERT INTO settings (key,vtype,".$var.",desc) VALUES (%s,%d,".$type.",%s)",
      $key, $value[0], $value[1], $value[2]);
    
    stExecSQL($sql);
  }
}


//
// Main program starts
//
if ($argc < 2)
{
  echo "Usage: ".$argv[0]." <mode> [args]\n".
  "Where mode is one of following:\n".
  "\n".
  "  new [dbspec]      Create a new database with given PDO spec\n".
  "                    or default to the one in mconfig.inc.php\n".
  "\n".
  "  reset [dbspec]    Reset settings (similar to 'new').\n".
  "\n";
  exit;
}


if (($spec = stCArg(2)) === FALSE)
  $spec = $siteSettings["sqlDB"];


if (!stConnectDB($spec))
  die("Could not connect to SQL database '".$spec."'.\n");

echo "Using database spec '".$spec."'.\n";

switch (stCArgLC(1))
{
  case "new":
    if (stCreateTables())
      stAddSettings();
    
    if (substr($spec, 0, 7) == "sqlite:")
    {
      $filename = substr($spec, 7);
      echo "NOTICE! It seems you have SQLite database in use, changing permission ".
      "of the target file '".$filename."' to 0600, for security. You may have to ".
      "loosen up that for the things to actually work, but be careful. Having your ".
      "database world-readable in the web is NOT good.\n";

      if (chmod($filename, 0600) === FALSE)
      {
        echo "ERROR! Could not set permissions!\n";
      }
    }
    break;

  case "reset":
    echo "Deleting old settings.\n";
    stExecSQL("DROP TABLE settings");

    if (stCreateOneTable("settings", $sqlTables["settings"]))
      stAddSettings();
    break;

  default:
    echo "ERROR! Invalid operation mode '".stCArg(1)."'.\n";
    break;
}


//
// Clean up permissions
//
foreach (array("createdb.php") as $filename)
{
  if (chmod($filename, 0700) === FALSE)
  {
    echo "ERROR! Could not set permissions for '$filename'!\n";
  }
}

?>