~george-edison55/charms/oneiric/thinkup/update-to-1-0-2

« back to all changes in this revision

Viewing changes to opt/update_admin_info

  • Committer: Nathan Osman
  • Date: 2011-12-01 00:54:48 UTC
  • Revision ID: admin@quickmediasolutions.com-20111201005448-tib82nn6k47843wu
Added website-relation-changed hook.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/php
 
2
<?php
 
3
 
 
4
//=======================================
 
5
//    This 'small' PHP script updates
 
6
// the information for the administrator
 
7
//=======================================
 
8
 
 
9
// Include the file containing DB credentials
 
10
require_once '/var/www/config.inc.php';
 
11
 
 
12
// We should have received 4 command line arguments
 
13
if($argc != 4)
 
14
{
 
15
    echo "Usage: {$argv[0]} name email password\n";
 
16
    exit(1);
 
17
}
 
18
 
 
19
$name  = $argv[1];
 
20
$email = $argv[2];
 
21
$pass  = $argv[3];
 
22
 
 
23
// Connect to the database server
 
24
$sql = new mysqli($THINKUP_CFG['db_host'],
 
25
                  $THINKUP_CFG['db_user'],
 
26
                  $THINKUP_CFG['db_password'],
 
27
                  $THINKUP_CFG['db_name']);
 
28
 
 
29
// Make sure the connection is valid
 
30
if($sql->connect_error)
 
31
{
 
32
    echo "MySQL connection error: {$sql->connect_error}\n";
 
33
    exit(1);
 
34
}
 
35
 
 
36
// Check if the user exists (NOTE: this is not foolproof there are no
 
37
// provisions for changing your email address.)
 
38
if(!($statement = $sql->prepare('SELECT id FROM tu_owners WHERE email = ?')))
 
39
{
 
40
    echo "MySQL error: {$sql->error}\n";
 
41
    exit(1);
 
42
}
 
43
 
 
44
$statement->bind_param('s', $email);
 
45
$statement->bind_result($id);
 
46
 
 
47
if($statement->execute() === FALSE)
 
48
{
 
49
    echo "MySQL error: {$sql->error}\n";
 
50
    exit(1);
 
51
}
 
52
 
 
53
$user_exists = $statement->fetch();
 
54
$statement->close();
 
55
 
 
56
if($user_exists)
 
57
{
 
58
    // Okay, we're updating the user with the specified email address
 
59
    if(!($statement = $sql->prepare('UPDATE tu_owners SET full_name = ?, pwd = ?, pwd_salt = ? WHERE id = ?')))
 
60
    {
 
61
        echo "MySQL error: {$sql->error}\n";
 
62
        exit(1);
 
63
    }
 
64
    
 
65
    // Generate a salt and hash the password
 
66
    $salt = hash('sha256', rand() . $email);
 
67
    $hash = hash('sha256', $pass . $salt);
 
68
    
 
69
    $statement->bind_param('sssi', $name, $hash, $salt, $id);
 
70
    
 
71
    // Perform the update
 
72
    if($statement->execute() === FALSE)
 
73
    {
 
74
        echo "MySQL error: {$sql->error}\n";
 
75
        exit(1);
 
76
    }
 
77
}
 
78
else
 
79
{
 
80
    // Create the user
 
81
    if(!($statement = $sql->prepare('INSERT INTO tu_owners (full_name, pwd, pwd_salt, email, joined, is_activated, is_admin, last_login) VALUES (?, ?, ?, ?, NOW(), 1, 1, NOW())')))
 
82
    {
 
83
        echo "MySQL error: {$sql->error}\n";
 
84
        exit(1);
 
85
    }
 
86
    
 
87
    // Generate a salt and hash the password
 
88
    $salt = hash('sha256', rand() . $email);
 
89
    $hash = hash('sha256', $pass . $salt);
 
90
    
 
91
    // Bind the parameters
 
92
    $statement->bind_param('ssss', $name, $hash, $salt, $email);
 
93
    
 
94
    // Insert the user
 
95
    if($statement->execute() === FALSE)
 
96
    {
 
97
        echo "MySQL error: {$sql->error}\n";
 
98
        exit(1);
 
99
    }
 
100
}
 
101
 
 
102
?>