PDA

View Full Version : Can't add ips and groups when creating new user with php


St0rm
05-08-2004, 01:02 PM
The topic basically says it all..
When creating a new user using the php extension, the 'ips' line of the userfile doesn't get written, and the 'groups' line always just contains 'groups' (instead of 'groups 0 1 101' or whatever).

ADDiCT
05-08-2004, 04:42 PM
Add the lines yourself? This is what i used to copy an existing 'template' user:

(It sets the IP-list for the new user to the IP of the browser client.)

...

$template_uid = 101;

...

// create new user
$new_uid = io_user_create($_POST["username"]);

// ... check for errors ($new_uid < 0) ...

// read data
$userfile = io_user_open($template_uid);
$contents = io_user_print($userfile);
io_user_close($userfile);

// initialise new lines; windows uses \r\n for newline
$passline = "password ".io_sha1($_POST["password"])."\r";
$ipline = "ips *@".$_SERVER["REMOTE_ADDR"]."\r";
$tagline = "tagline ".$_POST["tagline"]."\r";

// replace/add lines
$lines = explode("\n", $contents);
$size = count($lines);
for($i=0; $i < $size; $i++)
{
if (strtolower(substr($lines[$i],0,9)) == "password ") { $lines[$i] = $passline; $passline = ""; }
elseif (strtolower(substr($lines[$i],0,4)) == "ips ") { $lines[$i] = $ipline; $ipline = ""; }
elseif (strtolower(substr($lines[$i],0,8)) == "tagline ") { $lines[$i] = $tagline; $tagline = ""; }
}
$contents = implode("\n", $lines);
if (strlen($passline)>0) $contents .= $passline."\n"; // incase there was no such line yet.
if (strlen($ipline) >0) $contents .= $ipline ."\n"; // ..
if (strlen($tagline) >0) $contents .= $tagline ."\n"; // ..

// write data
$userfile = io_user_open($new_uid);
io_user_lock($userfile);
io_user_save($userfile, $contents);
io_user_unlock($userfile);
io_user_close($userfile);

St0rm
05-09-2004, 06:55 AM
Forget about the ips thingy, it was a mistake by me, but i still can't add groups to the userfile.. I'm using this code:

<?php

$uid = io_user_create($_POST["username"]);

if ($uid >= 0)
{
$uhandle = io_user_open($uid);
io_user_lock($uhandle);
$userfile = explode("\n", io_user_print($uhandle));
$userfile[2] = "tagline " . $_POST["tagline"];
$userfile[4] = "password " . io_sha1($_POST["password"]);
$userfile[8] = "groups " . $_POST["group"]; // this one isn't working
$userfile[9] = "flags " . $_POST["flags"];
$userfile = implode("\n", $userfile);
io_user_save($uhandle, $userfile);
io_user_unlock($uhandle);
}

?>

Everything works, except for the $userfile[8] line..

ADDiCT
05-09-2004, 07:09 AM
i don't think u can assume the "groups ..." line will always be at index 8

and i don't open the same userfile twice:
- open the template userfile, read all data, close template userfile (uid = 101)
- modify that data, write it to the new userfile (uid = 102 or 103 or ...)
(but i do re-use the $userfile variable for both files)

St0rm
05-09-2004, 07:14 AM
I think you can assume it's at line 8, cause I'm creating a new user, not editing one.

And I noticed about the userfile opening when I was re-reading your code.. I think I was editing my message when you were writing yours ;)