| 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); | 
|---|
| 14 |         preg_match('/\[mysql\][^\[]*host=([^\n]*)/',$cnfFile,$match); | 
|---|
| 15 |         $host = $match[1]; | 
|---|
| 16 |         preg_match('/\[mysql\][^\[]*user=([^\n]*)/',$cnfFile,$match); | 
|---|
| 17 |         $user = $match[1]; | 
|---|
| 18 |         preg_match('/\[mysql\][^\[]*password=([^\n]*)/',$cnfFile,$match); | 
|---|
| 19 |         $password = $match[1]; | 
|---|
| 20 |         if (empty($host)) $host = 'sql.mit.edu'; | 
|---|
| 21 |         if (empty($user)) $user = $env_user; | 
|---|
| 22 |         if (empty($password)) $password = 'password'; | 
|---|
| 23 |         return array($host,$user,$password); | 
|---|
| 24 | } | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | $cnfinfo = getMyCnfInfo($cnfPath); | 
|---|
| 28 | if (is_array($cnfinfo)) { | 
|---|
| 29 |         list($h,$u,$p) = $cnfinfo; | 
|---|
| 30 |         echo "$h\t$u\t$p"; | 
|---|
| 31 |         exit; | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | $sql_status = file_get_contents('https://sql.mit.edu/main/do/batch/status?u=' . urlencode($env_user)); | 
|---|
| 35 | switch($sql_status) { | 
|---|
| 36 |         case 1: | 
|---|
| 37 |                 $myPassword = `/usr/bin/sql-signup`; | 
|---|
| 38 |         file_put_contents($cnfPath, "[mysql]\nhost=$host\nuser=$env_user\npassword=$myPassword\n"); | 
|---|
| 39 |         $cnfinfo = getMyCnfInfo($cnfPath); | 
|---|
| 40 |         if (is_array($cnfinfo)) { | 
|---|
| 41 |             list($h,$u,$p) = $cnfinfo; | 
|---|
| 42 |             echo "$h\t$u\t$p"; | 
|---|
| 43 |         } | 
|---|
| 44 |                 break; | 
|---|
| 45 |         case 0: | 
|---|
| 46 | } | 
|---|