<?php

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Weave Minimal Server
#
# The Initial Developer of the Original Code is
# Mozilla Labs.
# Portions created by the Initial Developer are Copyright (C) 2008
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#	Toby Elliott (telliott@mozilla.com)
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

	require_once 'weave_storage2.php';
	$db = new WeaveStorage(null);

	fwrite(STDOUT, "(c)reate, (d)elete or change (p)assword: ");
	switch (substr(fgets(STDIN), 0, 1))
	{
		case 'c':
			fwrite(STDOUT, "Please enter email: ");
			$name = trim(fgets(STDIN));
			if (strpos($name, '@'))
			    $name = convert_username($name);

			fwrite(STDOUT, "Please enter password: ");
			$pwd = trim(fgets(STDIN));
			if ($db->create_user($name, $pwd))
				echo "$name created\n";
			else
				echo "An error occured\n";
			break;
		case 'd':
			fwrite(STDOUT, "Please enter email: ");
			$name = trim(fgets(STDIN));
			if (strpos($name, '@'))
			    $name = convert_username($name);

			if ($db->delete_user($name))
				echo "$name deleted\n";
			else
				echo "An error occured\n";
			break;
		case 'p':
			fwrite(STDOUT, "Please enter email: ");
			$name = trim(fgets(STDIN));
			if (strpos($name, '@'))
			    $name = convert_username($name);

			fwrite(STDOUT, "Please enter new password: ");
			$password = trim(fgets(STDIN));
			if ($db->change_password($name,$password))
				echo "password changed for $name\n";
			else
				echo "An error occured\n";
			break;
		default:
			echo "unknown command";
	}

    function convert_username($name)
    {
        if (strpos($name, '@'))
        {
            $name = strtolower($name);
            $hashed = sha1($name, true);
            $name = strtolower(SHA1::base32encode($hashed));
        }
        return $name;
    }

/**
Utility base32 SHA1 class for PHP5
Copyright (C) 2006  Karl Magdsick (original author for Python)
                    Angel Leon (ported to PHP5)
                    Lime Wire LLC

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
class SHA1 {
  static $BASE32_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';

  /** Given a file it creates a magnetmix */
  static function fileSHA1($file) {
    $raw = sha1_file($file,true);
    return SHA1::base32encode($raw);
  } //fileSHA1

  /** Takes raw input and converts it to base32 */
  static function base32encode($input) {
    $output = '';
    $position = 0;
    $storedData = 0;
    $storedBitCount = 0;
    $index = 0;

    while ($index < strlen($input)) {
      $storedData <<= 8;
      $storedData += ord($input[$index]);
      $storedBitCount += 8;
      $index += 1;

      //take as much data as possible out of storedData
      while ($storedBitCount >= 5) {
        $storedBitCount -= 5;
        $output .= SHA1::$BASE32_ALPHABET[$storedData >> $storedBitCount];
        $storedData &= ((1 << $storedBitCount) - 1);
      }
    } //while

    //deal with leftover data
    if ($storedBitCount > 0) {
      $storedData <<= (5-$storedBitCount);
      $output .= SHA1::$BASE32_ALPHABET[$storedData];
    }

    return $output;
  } //base32encode

}
?>
