Logo  

CS479/579 - Web Programming II

Displaying ./code/Mysqli/mkpwdtable.php

<?php
 include "config.php";
/*
+----------+---------------+------+-----+------------+-------+
| Field    | Type          | Null | Key | Default    | Extra |
+----------+---------------+------+-----+------------+-------+
| username | varchar(32)   | NO   | MUL |            |       |
| password | varchar(256)  | NO   |     | x          |       |
| uid      | int(11)       | NO   |     | -1         |       |
| gid      | int(11)       | NO   |     | -1         |       |
| gecos    | varchar(256)  | NO   |     |            |       |
| home     | varchar(4096) | NO   |     | /          |       |
| shell    | varchar(4096) | NO   |     | /bin/false |       |
+----------+---------------+------+-----+------------+-------+
*/
  function myerror($why) {
    global $myconn;
    die($why . $myconn->error);
  }

  $myconn->query("drop table if exists `passwd`") or myerror("drop table");
  $myconn->query("
     create table `passwd` (
       `username`    varchar(32)     not null default '',
       `password`    varchar(256)    not null default 'x',
       `uid`         int             not null default -1,
       `gid`         int             not null default -1,
       `gecos`       varchar(256)    not null default '',
       `home`        varchar(4096)   not null default '/',
       `shell`       varchar(4096)   not null default '/bin/false',
       index `username` (`username`)
     )"
  ) or myerror("create table");

  $stmt = $myconn->prepare("insert into passwd (`username`, `password`, `uid`, `gid`, `gecos`, `home`, `shell`)
    values (?, ?, ?, ?, ?, ?, ?)") or myerror("prepare");

  $stmt->bind_param("ssiisss", $username, $password, $uid, $gid, $gecos, $home, $shell);

  $pwd = file_get_contents("/etc/passwd");

  foreach(explode("\n", $pwd) as $pent) {
    if ($pent == "") break;
    list($username, $password, $uid, $gid, $gecos, $home, $shell) = explode(":", $pent);
    $stmt->execute();
  }

?>