summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEudyptula <eitan@mosenkis.net>2009-06-25 15:22:42 -0400
committerEudyptula <eitan@mosenkis.net>2009-06-25 15:22:42 -0400
commita171039a434de5bb44af48c776bc7625a09c7752 (patch)
treebf9b39c864ee5a01a9f5d5b12173899aeab05897 /shared/classes
parentCleaned up various unused bits of code; moved finished images to their own di... (diff)
downloadingenue-a171039a434de5bb44af48c776bc7625a09c7752.tar.gz
ingenue-a171039a434de5bb44af48c776bc7625a09c7752.tar.bz2
ingenue-a171039a434de5bb44af48c776bc7625a09c7752.zip
Added logout and user self-registration with email confirmation; Updates to sql_row_obj; PDO subclass for debugging
Diffstat (limited to 'shared/classes')
-rw-r--r--shared/classes/0sql_row_obj.php89
-rw-r--r--shared/classes/build.php2
-rw-r--r--shared/classes/buildopt.php2
-rw-r--r--shared/classes/pdo.php8
-rw-r--r--shared/classes/registrationtoken.php41
-rw-r--r--shared/classes/session.php22
-rw-r--r--shared/classes/task.php2
7 files changed, 104 insertions, 62 deletions
diff --git a/shared/classes/0sql_row_obj.php b/shared/classes/0sql_row_obj.php
index f2f815f..5c55a62 100644
--- a/shared/classes/0sql_row_obj.php
+++ b/shared/classes/0sql_row_obj.php
@@ -47,7 +47,7 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up
// Makes an SQL query using $sql and returns the resulting object
private static function sql_query($q) {
self::check_pdo_obj();
- // echo $q."\n";
+ debug('sql_row_obj::query', $q);
return self::$pdo->query($q);
}
public static function sql_quote_string($s) {
@@ -535,60 +535,34 @@ class sql_col {
public function __construct($array=null) {
if (is_array($array)) {
// TODO this should probably be a switch inside a foreach
- if (isset($array['type'])) {
+ if (isset($array['type']))
$this->type=strtoupper($array['type']); // To allow for lower-case types
- }
- if (isset($array['length'])) {
+ if (isset($array['length']))
$this->length=$array['length'];
- } elseif (isset($array['len'])) {
- $this->length=$array['len'];
- }
- if (isset($array['unsigned'])) {
+ if (isset($array['unsigned']))
$this->unsigned=$array['unsigned']?true:false; // To allow for non-booleans that can evaluate to boolean
- } elseif (isset($array['signed'])) {
- $this->unsigned=$array['signed']?false:true;
- }
- if (isset($array['charset'])) {
+ if (isset($array['charset']))
$this->charset=$array['charset'];
- }
- if (isset($array['collate'])) {
+ if (isset($array['collate']))
$this->collate=$array['collate'];
- }
- if (isset($array['not null'])) {
- $this->not_null=$array['not null']?true:false;
- } elseif (isset($array['not_null'])) {
+ if (isset($array['not_null']))
$this->not_null=$array['not_null']?true:false;
- } elseif (isset($array['null'])) {
- $this->not_null=$array['null']?false:true;
- }
- if ($this->is_numeric()) {
- if (isset($array['auto_increment'])) {
- $this->auto_increment=$array['auto_increment']?true:false;
- } elseif (isset($array['auto increment'])) {
- $this->auto_increment=$array['auto increment']?true:false;
- } elseif (isset($array['ai'])) {
- $this->auto_increment=$array['ai']?true:false;
- }
- }
- if (isset($array['default'])) {
+ if ($this->is_numeric() && isset($array['auto_increment']))
+ $this->auto_increment=$array['auto_increment']?true:false;
+ if (isset($array['default']))
$this->default=$array['default'];
- } elseif ($this->not_null) { // TODO add the default non-null val for the rest of types (http://dev.mysql.com/doc/refman/5.1/en/data-type-defaults.html)
- if ($this->is_numeric() && !$this->auto_increment) {
+ elseif ($this->not_null) { // TODO add the default non-null val for the rest of types (http://dev.mysql.com/doc/refman/5.1/en/data-type-defaults.html)
+ if ($this->is_numeric() && !$this->auto_increment)
$this->default=0;
- } elseif ($this->type == 'ENUM') {
+ elseif ($this->type == 'ENUM') {
// $this->default=$this->length; // TODO finish this
- } elseif ($this->is_string()) {
+ } elseif ($this->is_string())
$this->default='';
- }
}
- if (isset($array['unique'])) {
+ if (isset($array['unique']))
$this->unique=$array['unique']?true:false;
- }
- if (isset($array['refers to'])) {
- $this->refers_to=$array['refers to'];
- } elseif (isset($array['refersto'])) {
- $this->refers_to=$array['refersto'];
- }
+ if (isset($array['refers_to']))
+ $this->refers_to=$array['refers_to'];
} elseif (is_string($array)) {
$line=$array;
list($type, $line)=explode(' ', $line, 2);
@@ -654,7 +628,11 @@ class sql_col {
$this->default=$string;
break;
case 'COMMENT':
- $this->comment=$string;
+ if (preg_match('/^refers to: ([a-zA-Z0-9_$]+\.[a-zA-Z0-9_$]+)$/', $string, $match)) {
+ $this->refers_to=$match[1];
+ } else {
+ $this->comment=$string;
+ }
break;
}
break;
@@ -752,30 +730,25 @@ class sql_col {
// Returns the row used to create this column in the CREATE statement
public function describe() {
$d=$this->type.($this->has_length()?'('.$this->get_length().')':'');
- if ($this->is_numeric() && $this->unsigned) {
+ if ($this->is_numeric() && $this->unsigned)
$d.=' UNSIGNED';
- }
if ($this->is_string()) {
$d.=' CHARSET '.$this->charset;
- if (isset($this->collate)) {
+ if (isset($this->collate))
$d.=' COLLATE '.$this->collate;
- }
}
- if ($this->not_null) {
+ if ($this->not_null)
$d.=' NOT NULL';
- }
- if (isset($this->default)) {
+ if (isset($this->default))
$d.=' DEFAULT '.$this->sql_value($this->default);
- }
- if ($this->is_numeric() && $this->auto_increment) {
+ if ($this->is_numeric() && $this->auto_increment)
$d.=' AUTO_INCREMENT';
- }
- if ($this->unique === true) {
+ if ($this->unique === true)
$d.=' UNIQUE';
- }
- if (isset($this->comment)) {
+ if (isset($this->comment))
$d.=' COMMENT '.sql_row_obj::sql_quote_string($this->comment);
- }
+ elseif (isset($this->refers_to))
+ $d.=' COMMENT '.sql_row_obj::sql_quote_string('refers to: '.$this->refers_to);
return $d;
}
// Returns the array necessary to generate this column using the constructor
diff --git a/shared/classes/build.php b/shared/classes/build.php
index 54631ee..369425e 100644
--- a/shared/classes/build.php
+++ b/shared/classes/build.php
@@ -13,7 +13,7 @@ class sql_build extends sql_row_obj {
'unsigned' => true,
'not_null' => true,
'default' => 0,
- 'refers to' => 'users.id'
+ 'refers_to' => 'users.id'
),
'name' => array (
'type' => 'VARCHAR',
diff --git a/shared/classes/buildopt.php b/shared/classes/buildopt.php
index 92d24fd..b4b5226 100644
--- a/shared/classes/buildopt.php
+++ b/shared/classes/buildopt.php
@@ -6,7 +6,7 @@ class sql_buildopt extends sql_row_obj {
'length' => 6,
'not_null' => true,
'default' => '',
- 'refers to' => 'builds.id'
+ 'refers_to' => 'builds.id'
),
'name' => array (
'type' => 'VARCHAR',
diff --git a/shared/classes/pdo.php b/shared/classes/pdo.php
new file mode 100644
index 0000000..daf71ba
--- /dev/null
+++ b/shared/classes/pdo.php
@@ -0,0 +1,8 @@
+<?php
+class pdo_debug extends PDO {
+ function query($q, $a1=null, $a2=null, $a3=null) {
+ debug('pdo::query', $q);
+ return parent::query($q, $a1, $a2, $a3);
+ }
+}
+?>
diff --git a/shared/classes/registrationtoken.php b/shared/classes/registrationtoken.php
new file mode 100644
index 0000000..86e8d44
--- /dev/null
+++ b/shared/classes/registrationtoken.php
@@ -0,0 +1,41 @@
+<?php
+class sql_registrationtoken extends sql_row_obj {
+ protected $table='registrationtokens', $primary_key=array('id'), $columns=array(
+ 'id' => array (
+ 'type' => 'CHAR',
+ 'length' => 30,
+ 'not_null' => true,
+ 'default' => ''
+ ),
+ 'owner' => array (
+ 'type' => 'INT',
+ 'length' => 10,
+ 'unsigned' => true
+ ),
+ 'email' => array (
+ 'type' => 'VARCHAR',
+ 'length' => 255,
+ 'not_null' => true,
+ 'unique' => true
+ ),
+ 'expire' => array (
+ 'type' => 'INT',
+ 'length' => 10,
+ 'unsigned' => true,
+ 'not_null' => true,
+ 'default' => 0
+ )
+
+ );
+ static function create() {
+ global $S;
+ $id=null;
+ do {
+ $id=randstring(30);
+ if ($S['pdo']->query('SELECT COUNT(*) FROM `registrationtokens` WHERE `id`=\''.$id.'\'')->fetch(PDO::FETCH_COLUMN))
+ $id=null;
+ } while ($id==null);
+ return new sql_registrationtoken($id, null, null, null);
+ }
+}
+?>
diff --git a/shared/classes/session.php b/shared/classes/session.php
index 19575fe..e422b21 100644
--- a/shared/classes/session.php
+++ b/shared/classes/session.php
@@ -13,7 +13,7 @@ class sql_session extends sql_row_obj {
'unsigned' => true,
'not_null' => true,
'default' => 0,
- 'refers to' => 'users.id'
+ 'refers_to' => 'users.id'
),
'atime' => array (
'type' => 'INT',
@@ -31,5 +31,25 @@ class sql_session extends sql_row_obj {
)
);
+ // Creates a new session for the user at $S['user'] with a unique id, sends a cookie to the user and returns true for success, false for failure
+ static function create() {
+ global $S, $conf;
+ $id=null;
+ while (!$id) {
+ $id=randstring(30);
+ $r=$S['pdo']->query('SELECT * FROM `sessions` WHERE `id`="'.$id.'"');
+ if ($r->rowCount()) {
+ $id=null;
+ }
+ }
+ $S['session']=new sql_session($id, $S['user']->id, time(), $conf['sessionlength']);
+ debug('setcookie', $conf['cookiename'].'='.$id);
+ if (setcookie($conf['cookiename'], $S['session']->id, time()+$conf['sessionlength'], $S['cookie_dir'], '', false, true)) {
+ $S['session']->write();
+ return true;
+ } else {
+ return false;
+ }
+ }
}
?>
diff --git a/shared/classes/task.php b/shared/classes/task.php
index 30adb73..73bde43 100644
--- a/shared/classes/task.php
+++ b/shared/classes/task.php
@@ -13,7 +13,7 @@ class sql_task extends sql_row_obj {
'length' => 6,
'not_null' => true,
'default' => '',
- 'refers to' => 'builds.id'
+ 'refers_to' => 'builds.id'
),
'command' => array (
'type' => 'TEXT',