source: trunk/locker/sql/bin/get-password @ 1954

Last change on this file since 1954 was 1313, checked in by geofft, 15 years ago
Make the [mysql] to [client] change everywhere that writes .my.cnf
  • Property svn:executable set to *
File size: 1.5 KB
RevLine 
[131]1#!/usr/bin/php
2<?php
3
4$host = 'sql.mit.edu';
5$env_user = getenv('USER');
6$home = '/mit/'.$env_user;
7
8$cnfPath = $home.'/.sql/my.cnf';
9
10function getMyCnfInfo($path) {
11if (file_exists($path)) {
12        global $env_user;
13        $cnfFile = file_get_contents($path);
[1049]14        if (preg_match('/\[mysql\][^\[]*host *= *([^\r\n]*)/',$cnfFile,$match)) {
[1037]15                $host = $match[1];
[1049]16        } elseif (preg_match('/\[client\][^\[]*host *= *([^\r\n]*)/',$cnfFile,$match)) {
[1037]17                $host = $match[1];
18        } else {
19                $host = 'sql.mit.edu';
20        }
[1049]21        if (preg_match('/\[mysql\][^\[]*user *= *([^\r\n]*)/',$cnfFile,$match)) {
[1037]22                $user = $match[1];
[1049]23        } elseif (preg_match('/\[client\][^\[]*user *= *([^\r\n]*)/',$cnfFile,$match)) {
[1037]24                $user = $match[1];
25        } else {
26                $user = $env_user;
27        }
[1049]28        if (preg_match('/\[mysql\][^\[]*password *= *([^\r\n]*)/',$cnfFile,$match)) {
[1037]29                $password = $match[1];
[1049]30        } elseif (preg_match('/\[client\][^\[]*password *= *([^\r\n]*)/',$cnfFile,$match)) {
[1037]31                $password = $match[1];
32        } else {
33                $password = 'password';
34        }
[131]35        return array($host,$user,$password);
36}
37}
38
39$cnfinfo = getMyCnfInfo($cnfPath);
40if (is_array($cnfinfo)) {
41        list($h,$u,$p) = $cnfinfo;
42        echo "$h\t$u\t$p";
43        exit;
44}
45
[382]46$sql_status = file_get_contents('https://sql.mit.edu/main/do/batch/status?u=' . urlencode($env_user));
[131]47switch($sql_status) {
48        case 1:
49                $myPassword = `/usr/bin/sql-signup`;
[1313]50        file_put_contents($cnfPath, "[client]\nhost=$host\nuser=$env_user\npassword=$myPassword\n");
[131]51        $cnfinfo = getMyCnfInfo($cnfPath);
52        if (is_array($cnfinfo)) {
53            list($h,$u,$p) = $cnfinfo;
54            echo "$h\t$u\t$p";
55        }
56                break;
57        case 0:
58}
Note: See TracBrowser for help on using the repository browser.