<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -360,6 +360,18 @@ CREATE TABLE `video_timestamps` (
   UNIQUE KEY `gid_user_id` (`gid`, `user_id`)
 );
 
+CREATE TABLE `api_key` (
+  `id` int(11) NOT NULL auto_increment,
+  `user_id` mediumint(9) NOT NULL,
+  `api_key` char(24) NOT NULL,
+  `commercial` tinyint(1) NOT NULL,
+  `created` datetime NOT NULL,
+  `disabled` datetime default NULL,
+  `reason` text NOT NULL,
+  PRIMARY KEY (`id`),
+  KEY `api_key` (`api_key`)
+);
+
 CREATE TABLE `api_stats` (
   `id` int(11) NOT NULL auto_increment,
   `api_key` char(24) NOT NULL,</diff>
      <filename>db/schema.sql</filename>
    </modified>
    <modified>
      <diff>@@ -14,34 +14,73 @@ api_key_current_message();
 echo '&lt;p&gt;TheyWorkForYou API calls require a key, so that we can monitor usage
 of the service, and provide usage stats to you.';
 if ($THEUSER-&gt;loggedin()) {
+	if (get_http_var('create_key')) {
+		create_key(get_http_var('commercial'), get_http_var('reason'));
+	}
 	$db = new ParlDB;
-	$q = $db-&gt;query('SELECT api_key FROM users WHERE user_id=' . $THEUSER-&gt;user_id());
-	$key = $q-&gt;field(0, 'api_key');
-	if (!$key) {
-		# Haven't yet got a key, generate one
-		$key = auth_ab64_encode(random_bytes(16));
-		$db-&gt;query('UPDATE users SET api_key=&quot;' . $key . '&quot; where user_id=' . $THEUSER-&gt;user_id());
+	$q = $db-&gt;query('SELECT api_key, commercial, created, reason FROM api_key WHERE user_id=' . $THEUSER-&gt;user_id());
+	$keys = array();
+	for ($i=0; $i&lt;$q-&gt;rows(); $i++) {
+		$keys[] = array($q-&gt;field($i, 'api_key'), $q-&gt;field($i, 'commercial'), $q-&gt;field($i, 'created'), $q-&gt;field($i, 'reason'));
+	}
+	if ($keys) {
+		echo '&lt;h3&gt;Your keys&lt;/h3&gt; &lt;ul&gt;';
+	}
+	foreach ($keys as $keyarr) {
+		list($key, $commercial, $created, $reason) = $keyarr;
+		echo '&lt;li&gt;&lt;span style=&quot;font-size:200%&quot;&gt;' . $key . '&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #666666;&quot;&gt;';
+		echo $commercial ? 'Commercial' : 'Non-commercial';
+		echo ' key, created ', $created; # , ' ', $reason;
+		echo '&lt;/span&gt;&lt;br&gt;&lt;em&gt;Usage statistics&lt;/em&gt;: ';
+		$q = $db-&gt;query('SELECT count(*) as count FROM api_stats WHERE api_key=&quot;' . $key . '&quot; AND query_time &gt; NOW() - interval 1 day');
+		$c = $q-&gt;field(0, 'count');
+		echo &quot;last 24 hours: $c, &quot;;
+		$q = $db-&gt;query('SELECT count(*) as count FROM api_stats WHERE api_key=&quot;' . $key . '&quot; AND query_time &gt; NOW() - interval 1 week');
+		$c = $q-&gt;field(0, 'count');
+		echo &quot;last week: $c, &quot;;
+		$q = $db-&gt;query('SELECT count(*) as count FROM api_stats WHERE api_key=&quot;' . $key . '&quot; AND query_time &gt; NOW() - interval 1 month');
+		$c = $q-&gt;field(0, 'count');
+		echo &quot;last month: $c&quot;;
+		echo '&lt;/p&gt;';
 	}
-	echo '&lt;p style=&quot;font-size:200%&quot;&gt;Your key is: ', $key, '&lt;/p&gt;';
-	echo '&lt;h3&gt;Usage stats&lt;/h3&gt; &lt;ul style=&quot;font-size:125%&quot;&gt;';
-	$q = $db-&gt;query('SELECT count(*) as count FROM api_stats WHERE api_key=&quot;' . $key . '&quot; AND query_time &gt; NOW() - interval 1 day');
-	$c = $q-&gt;field(0, 'count');
-	echo &quot;&lt;li&gt;Last 24 hours: $c&quot;;
-	$q = $db-&gt;query('SELECT count(*) as count FROM api_stats WHERE api_key=&quot;' . $key . '&quot; AND query_time &gt; NOW() - interval 1 week');
-	$c = $q-&gt;field(0, 'count');
-	echo &quot;&lt;li&gt;Last week: $c&quot;;
-	$q = $db-&gt;query('SELECT count(*) as count FROM api_stats WHERE api_key=&quot;' . $key . '&quot; AND query_time &gt; NOW() - interval 1 month');
-	$c = $q-&gt;field(0, 'count');
-	echo &quot;&lt;li&gt;Last month: $c&quot;;
-	echo '&lt;/ul&gt;';
+	if ($keys) {
+		echo '&lt;/ul&gt;';
+	}
+	api_key_form();
 } else {
-	echo ' This key is tied to your TheyWorkForYou account,
+	echo ' The key is tied to your TheyWorkForYou account,
 so if you don\'t yet have one, please &lt;a href=&quot;/user/?pg=join&quot;&gt;sign up&lt;/a&gt;, then 
-return here to find your key.&lt;/p&gt;';
-	echo '&lt;p style=&quot;font-size:200%&quot;&gt;&lt;strong&gt;&lt;a href=&quot;/user/login/?ret=/api/key&quot;&gt;Log in&lt;/a&gt;&lt;/strong&gt; (or &lt;a href=&quot;/user/?pg=join&quot;&gt;sign up&lt;/a&gt;) to view your API key.&lt;/p&gt;';
+return here to get a key.&lt;/p&gt;';
+	echo '&lt;p style=&quot;font-size:200%&quot;&gt;&lt;strong&gt;&lt;a href=&quot;/user/login/?ret=/api/key&quot;&gt;Log in&lt;/a&gt;&lt;/strong&gt; (or &lt;a href=&quot;/user/?pg=join&quot;&gt;sign up&lt;/a&gt;) to get an API key.&lt;/p&gt;';
 }
 
 $sidebar = api_sidebar();
 $PAGE-&gt;stripe_end(array($sidebar));
 $PAGE-&gt;page_end();
 
+# ---
+
+function create_key($commercial, $reason) {
+	global $THEUSER;
+	$key = auth_ab64_encode(random_bytes(16));
+	$db = new ParlDB;
+	$db-&gt;query('INSERT INTO api_key (user_id, api_key, commercial, created, reason) VALUES
+		(' . $THEUSER-&gt;user_id() . ', &quot;' . $key . '&quot;, ' . $commercial . ', NOW(), &quot;' . $reason . '&quot;)');
+}
+
+function api_key_form() {
+?&gt;
+&lt;br&gt;
+&lt;h3&gt;Apply for a new key&lt;/h3&gt;
+&lt;form action=&quot;/api/key&quot; method=&quot;post&quot;&gt;
+&lt;p&gt;Is your application for:
+&lt;input id=&quot;non_comm&quot; type=&quot;radio&quot; name=&quot;commercial&quot; value=&quot;0&quot;&gt; &lt;label for=&quot;non_comm&quot;&gt;Non-commercial use&lt;/label&gt;
+&lt;input id=&quot;comm&quot; type=&quot;radio&quot; name=&quot;commercial&quot; value=&quot;1&quot;&gt; &lt;label for=&quot;comm&quot;&gt;Commercial use&lt;/label&gt;
+&lt;p&gt;&lt;label for=&quot;reason&quot;&gt;Please describe what you're going to use the key for:&lt;/label&gt;
+&lt;br&gt;
+&lt;textarea id=&quot;reasons&quot; name=&quot;reason&quot; rows=7 cols=40&gt;&lt;/textarea&gt;
+&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Get key&quot;&gt;
+&lt;input type=&quot;hidden&quot; name=&quot;create_key&quot; value=&quot;1&quot;&gt;
+&lt;/form&gt;
+&lt;?
+}</diff>
      <filename>www/docs/api/key.php</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>264dc96ec4dbc5e4d6c917c38949ef04dc831915</id>
    </parent>
  </parents>
  <author>
    <name>matthew</name>
    <email>matthew</email>
  </author>
  <url>http://github.com/mlandauer/twfy/commit/6c7aae9d225e638f81bd7c888333b9ef7762b028</url>
  <id>6c7aae9d225e638f81bd7c888333b9ef7762b028</id>
  <committed-date>2008-08-07T23:26:08-07:00</committed-date>
  <authored-date>2008-07-21T03:18:51-07:00</authored-date>
  <message>Put keys in separate table so can have multiple keys, ask for reason of key use.</message>
  <tree>023c200d246ad2daaf79daaa70105ed29c5b6c64</tree>
  <committer>
    <name>Matthew Landauer</name>
    <email>matthew@openaustralia.org</email>
  </committer>
</commit>
