| [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 |  | 
|---|
|  | 10 | function getMyCnfInfo($path) { | 
|---|
|  | 11 | if (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); | 
|---|
|  | 40 | if (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] | 47 | switch($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 | } | 
|---|