Date: 08/16/01
- Next message: Oliver.Maske <email protected>: "[phplib] cannot retrieve http url via function file"
- Previous message: Ralf Pfeiffer: "[phplib] Wie kann man die Ausgabe von table.inc von phplib gestalten?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi.
This is the kind of things that inspire un unbusy person during the 15
of august
It started as a play, to see if I could have generated class names and
extended classes basing on defined constants, e.g.:
....
define ("_CLASSNAME_","myClass1");
define ("_EXTENDS_CLASSNAME_","myClass2");
class _CLASSNAME_ extends _EXTENDS_CLASSNAME_
....
but this obviously didn't work.
So I understood that the only way to do it was through eval and 'here
documents'.
It ended up in a couple of scripts, based on phplib-stable prepend.php3
and local.inc, that will generate or generate_and_run a valid local.inc,
based on the settings in the attached setup_prepend.php
It is quite easy to use, you just (just!) need to know which pieces of
the delivered 'local.inc' to include, in which case, and eventually code
for the insertion.
To write 'setup_localinc.php' I just went into the distributed
local.inc, backslashed all the $ signs, and added some conditions and
code before and after any piece of code that I wanted to enter in the
auto-generated local.inc.
The script, in setup_prepend.php (which should be included in place of
prepend.php3 for the test, and values set in it properly), has a major
switch, _PHPLIB_SETUP_ACTION, which can be either 'gen', 'debug' or
'run'.
'gen' will just generate the local.inc code, based on the settings of
the constants as defined in setup_prepend.php, and display them on
running, (in fact any phplib page can do, if it is prepended) and exit
'debug' will do much the same, but with numbered 'eval' instruction, so
if you have an error in the eval code, you can get to the line.
'run' will, after having generated the code, eval it and properly
continue running. It can work as a test for the generated code. Eval
should be quite slow, so after run, you should copy the code outputted
by gen and use it as local.inc.
I mean, for the moment is more a joke than a serious intention, but
coding the conditional creation of code is damn easy, once you know what
to do..
Giancarlo
<?php
/*
* Session Management for PHP3
*
* Copyright (c) 1998-2000 NetUSE AG
* Boris Erdmann, Kristian Koehntopp
*
* $Id: prepend.php3,v 1.5 2001/08/11 07:12:14 richardarcher Exp $
*
*/
# Can't control your include path?
# Point this to your PHPLIB base directory. Use a trailing "/"!
$_PHPLIB["libdir"] = "/home/ping/phplib/php/";
# test this setup or only generate code?
define ("_PHPLIB_SETUP_ACTION","gen" );
#define ("_PHPLIB_SETUP_ACTION","debug" );
#define ("_PHPLIB_SETUP_ACTION","run" );
# default setup
define ("_PHPLIB_SETUP_TYPE", "default");
#define ("_PHPLIB_VERSION","4");
#define ("_PHPLIB_DB_TYPE","mysql");
## DEFAULT DB settings
define ("_PHPLIB_DEFAULT_DB_TYPE","mysql");
define ("_PHPLIB_DEFAULT_DB_DATABASE","test");
define ("_PHPLIB_DEFAULT_DB_USER","root");
define ("_PHPLIB_DEFAULT_DB_PASSWORD","oliver");
define ("_PHPLIB_DEFAULT_DB_HOST","localhost");
## SESSION
define ("_PHPLIB_SESS_CLASSNAME","Example_Session");
define ("_PHPLIB_SESS_MAGIC","chinchunchao");
## AUTH
define ("_PHPLIB_AUTH",TRUE);
define ("_PHPLIB_AUTH_CLASSNAME","Example_Auth");
define ("_PHPLIB_AUTH_NOBODY", TRUE);
define ("_PHPLIB_AUTH_NOBODY_CLASSNAME","Example_Deafult_Auth");
define ("_PHPLIB_AUTH_CHALLENGE","cr");
define ("_PHPLIB_AUTH_CHALLENGE_MAGIC","opensesame");
## USER
define ("_PHPLIB_USER",TRUE);
define ("_PHPLIB_USER_CLASSNAME","Example_User");
define ("_PHPLIB_USER_PASSWORDS","md5");
define ("_PHPLIB_USER_MAGIC",_PHPLIB_SESS_MAGIC);
## PERM
define ("_PHPLIB_PERM", TRUE);
define ("_PHPLIB_PERM_CLASSNAME","Example_Perm");
function _PHPLIB_setup()
{
global $_PHPLIB;
if (_PHPLIB_SETUP_ACTION )
{
require($_PHPLIB["libdir"] . "db_mysql.inc"); // Change this to match your database.
require($_PHPLIB["libdir"] . "ct_sql.inc"); // Change this to match your data storage container
require($_PHPLIB["libdir"] . "session.inc"); // Required for everything below.
require($_PHPLIB["libdir"] . "auth.inc"); // Disable this, if you are not using aut hentication.
require($_PHPLIB["libdir"] . "perm.inc"); // Disable this, if you are not using permission checks.
require($_PHPLIB["libdir"] . "user.inc"); // Disable this, if you are not using per-user variables.
require($_PHPLIB["libdir"] . "setup_localinc.php"); // Disable this, if you are not using per-user variables.
// Additional require statements go below this line
# require($_PHPLIB["libdir"] . "menu.inc"); // Enable to use Menu
// Additional require statements go before this line
require($_PHPLIB["libdir"] . "page.inc"); // Required, contains the page management functions.
}
}
_PHPLIB_setup();
?>
<?php
/*
* Session Management for PHP3
*
* Copyright (c) 1998-2000 NetUSE AG
* Boris Erdmann, Kristian Koehntopp
*
* $Id: local.inc,v 1.5 2001/02/02 21:19:14 sbergmann Exp $
*
*/
class codeBuilder {
var $setup_str="";
function addCode($c)
{
$this->setup_str.=$c;
}
}
$c=new codeBuilder;
if (_PHPLIB_SETUP_TYPE == "default") {
$h=_PHPLIB_DEFAULT_DB_HOST;
$d=_PHPLIB_DEFAULT_DB_DATABASE;
$u=_PHPLIB_DEFAULT_DB_USER;
$p=_PHPLIB_DEFAULT_DB_PASSWORD;
$code=<<<EOD
class DB_Example extends DB_Sql {
var \$Host = "$h";
var \$Database = "$d";
var \$User = "$u";
var \$Password = "$p";
}
EOD;
$c->addCode($code);
}
if (_PHPLIB_SETUP_TYPE == "default"){
$code=<<<EOD
class Example_CT_Sql extends CT_Sql {
var \$database_class = "DB_Example"; ## Which database to connect...
var \$database_table = "active_sessions"; ## and find our session data in this table.
}
EOD;
$c->addCode($code);
}
#class Example_CT_Split_Sql extends CT_Split_Sql {
# var $database_class = "DB_Example"; ## Which database to connect...
# var $database_table = "active_sessions_split"; ## and find our session data in this table.
# var $split_length = 4096 ## Split rows every 4096 bytes
#}
#class Example_CT_Shm extends CT_Shm {
# var $max_sessions = 500; ## number of maximum sessions
# var $shm_key = 0x123754; ## unique shm identifier
# var $shm_size = 64000; ## size of segment
#}
#class Example_CT_Ldap extends CT_Ldap {
# var $ldap_host = "localhost";
# var $ldap_port = 389;
# var $basedn = "dc=your-domain, dc=com";
# var $rootdn = "cn=root, dc=your-domain, dc=com";
# var $rootpw = "secret";
# var $objclass = "phplibdata";
#}
#class Example_CT_Dbm extends CT_DBM {
# var $dbm_file = "must_exist.dbm";
#}
if (_PHPLIB_SETUP_TYPE == "default") {
$subst=_PHPLIB_SESS_CLASSNAME;
$code=<<<EOD
class $subst extends Session {
var \$classname = "$subst";
var \$cookiename = ""; ## defaults to classname
var \$magic = "Hocuspocus"; ## ID seed
var \$mode = "cookie"; ## We propagate session IDs with cookies
var \$fallback_mode = "get";
var \$lifetime = 0; ## 0 = do session cookies, else minutes
var \$that_class = "Example_CT_Sql"; ## name of data storage container
var \$gc_probability = 5;
}
EOD;
$c->addCode($code);
}
if (_PHPLIB_SETUP_TYPE == "default") {
$subst=_PHPLIB_USER_CLASSNAME;
$code=<<<EOD
class $subst extends User {
var \$classname = "$subst";
var \$magic = "Abracadabra"; ## ID seed
var \$that_class = "Example_CT_Sql"; ## data storage container
}
EOD;
$c->addCode($code);
}
if (_PHPLIB_SETUP_TYPE == "default"
&& _PHPLIB_AUTH_CHALLENGE == "no") {
$subst=_PHPLIB_AUTH_CLASSNAME;
$code=<<<EOD
class $subst extends Auth {
var \$classname = "$subst";
var \$lifetime = 15;
var \$database_class = "DB_Example";
var \$database_table = "auth_user";
function auth_loginform() {
global \$sess;
global \$_PHPLIB;
include(\$_PHPLIB["libdir"] . "loginform.ihtml");
}
function auth_validatelogin() {
global \$username, \$password;
if(isset(\$username)) {
\$this->auth["uname"]=\$username; ## This provides access for "loginform.ihtml"
}
\$uid = false;
\$this->db->query(sprintf("select user_id, perms ".
" from %s ".
" where username = '%s' ".
" and password = '%s'",
\$this->database_table,
addslashes(\$username),
addslashes(\$password)));
while(\$this->db->next_record()) {
\$uid = \$this->db->f("user_id");
\$this->auth["perm"] = \$this->db->f("perms");
}
return \$uid;
}
}
EOD;
$c->addCode($code);
}
if (_PHPLIB_SETUP_TYPE == "default"
&& _PHPLIB_AUTH_NOBODY == TRUE) {
$subst=_PHPLIB_AUTH_NOBODY_CLASSNAME; $code=<<<EOD
class $subst extends Example_Auth {
var \$classname = "$subst";
var \$nobody = true;
}
EOD;
$c->addCode($code);
}
if (_PHPLIB_SETUP_TYPE == "default"
&& (_PHPLIB_AUTH_CHALLENGE == "cr")) {
$subst=_PHPLIB_AUTH_CLASSNAME;
$magic=_PHPLIB_AUTH_CHALLENGE_MAGIC;
$code=<<<EOD
class $subst extends Auth {
var \$classname = "$subst";
var \$lifetime = 1;
var \$magic = "$magic"; ## Challenge seed
var \$database_class = "DB_Example";
var \$database_table = "auth_user";
function auth_loginform() {
global \$sess;
global \$challenge;
global \$_PHPLIB;
\$challenge = md5(uniqid(\$this->magic));
\$sess->register("challenge");
include(\$_PHPLIB["libdir"] . "crloginform.ihtml");
}
function auth_validatelogin() {
global \$username, \$password, \$challenge, \$response;
if(isset(\$username)) {
\$this->auth["uname"]=\$username; ## This provides access for "loginform.ihtml"
}
\$this->db->query(sprintf("select user_id,perms,password ".
"from %s where username = '%s'",
\$this->database_table,
addslashes(\$username)));
while(\$this->db->next_record()) {
\$uid = \$this->db->f("user_id");
\$perm = \$this->db->f("perms");
\$pass = \$this->db->f("password");
}
\$exspected_response = md5("\$username:\$pass:\$challenge");
## True when JS is disabled
if (\$response == "") {
if (\$password != \$pass) {
return false;
} else {
\$this->auth["perm"] = \$perm;
return \$uid;
}
}
## Response is set, JS is enabled
if (\$exspected_response != \$response) {
return false;
} else {
\$this->auth["perm"] = \$perm;
return \$uid;
}
}
}
EOD;
$c->addCode($code);
}
if (_PHPLIB_SETUP_TYPE == "default"
&& _PHPLIB_AUTH_CHALLENGE == "crc" ) {
$subst=_PHPLIB_AUTH_CLASSNAME;
$magic=_PHPLIB_AUTH_CHALLENGE_MAGIC;
$code=<<<EOD
class $subst extends Auth {
var \$classname = "$subst";
var \$lifetime = 1;
var \$magic = "$magic"; ## Challenge seed
var \$database_class = "DB_Example";
var \$database_table = "auth_user_md5";
function auth_loginform() {
global \$sess;
global \$challenge;
\$challenge = md5(uniqid(\$this->magic));
\$sess->register("challenge");
include("crcloginform.ihtml");
}
function auth_validatelogin() {
global \$username, \$password, \$challenge, \$response;
\$this->auth["uname"]=\$username; ## This provides access for "loginform.ihtml"
\$this->db->query(sprintf("select user_id,perms,password ".
"from %s where username = '%s'",
\$this->database_table,
addslashes(\$username)));
while(\$this->db->next_record()) {
\$uid = \$this->db->f("user_id");
\$perm = \$this->db->f("perms");
\$pass = \$this->db->f("password"); ## Password is stored as a md5 hash
}
\$exspected_response = md5("\$username:\$pass:\$challenge");
## True when JS is disabled
if (\$response == "") {
if (md5(\$password) != \$pass) { ## md5 hash for non-JavaScript browsers
return false;
} else {
\$this->auth["perm"] = \$perm;
return \$uid;
}
}
## Response is set, JS is enabled
if (\$exspected_response != \$response) {
return false;
} else {
\$this->auth["perm"] = \$perm;
return \$uid;
}
}
}
EOD;
$c->addCode($code);
}
if (_PHPLIB_SETUP_TYPE == "default"
&& _PHPLIB_PERM
&& _PHPLIB_PERM_CLASSNAME) {
$subst=_PHPLIB_PERM_CLASSNAME;
$code=<<<EOD
class $subst extends Perm {
var \$classname = "$subst";
var \$permissions = array(
"user" => 1,
"author" => 2,
"editor" => 4,
"supervisor" => 8,
"admin" => 16
);
function perm_invalid(\$does_have, \$must_have) {
global \$perm, \$auth, \$sess;
global \$_PHPLIB;
include(\$_PHPLIB["libdir"] . "perminvalid.ihtml");
}
}
EOD;
$c->addCode($code);
}
if (_PHPLIB_SETUP_ACTION == "gen")
{
echo "<pre>".$c->setup_str."</pre>";
exit();
}
elseif (_PHPLIB_SETUP_ACTION == "debug")
{
echo "<pre>";
$arr=split("\n",$c->setup_str);
for ($i=1;$i<=count($arr);$i++)
echo $i.". ".$arr[$i]."\n";
echo "</pre>";
exit();
}
elseif (_PHPLIB_SETUP_ACTION == "run")
return eval($c->setup_str); ## here
#### ##
#return;
echo "<pre>".$c->setup_str."</pre>";
?>
-- Abbestellen mit Mail an: phplib-unsubscribe <email protected> Kommandoliste mit Mail an: phplib-help <email protected>
- Next message: Oliver.Maske <email protected>: "[phplib] cannot retrieve http url via function file"
- Previous message: Ralf Pfeiffer: "[phplib] Wie kann man die Ausgabe von table.inc von phplib gestalten?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

