Last active
May 22, 2018 22:36
-
-
Save stelonix/4ef4de29ac0148f4171d320c727d175a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Friend and property support | |
// Read-only properties | |
abstract class PropertyVars { | |
protected $__visible = array(); | |
public function &__get($key) { | |
if(in_array($key, $this->__visible)) { | |
return $this->$key; | |
} | |
// normal __get() code here | |
trigger_error('Cannot access private property ' . __CLASS__ .'::$'.$key, E_USER_ERROR); | |
} | |
} | |
// Friend classes | |
abstract class Friend extends PropertyVars { | |
private $__friends = array('OtherFriend'); | |
public function __set($key, $value) { | |
$trace = debug_backtrace(); | |
if(isset($trace[1]['class']) && in_array($trace[1]['class'],$this->__friends)) { | |
return $this->$key = $value; | |
} | |
// normal __set() code here | |
trigger_error('Cannot access private property ' . __CLASS__ .'::$' . $key, E_USER_ERROR); | |
} | |
} | |
// All read-only except | |
abstract class InvPropertyVars { | |
protected $__invisible = array(); | |
public function __get($key) { | |
if(!in_array($key,$this->__invisible)) { | |
return $this->$key; | |
} | |
// normal __get() code here | |
trigger_error('Cannot access private property ' . __CLASS__ .'::$'.$key, E_USER_ERROR); | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DatabaseField extends InvPropertyVars { | |
protected $MemberName; | |
protected $Field; | |
protected $Changed = 0; | |
function __construct($member,$field) { | |
$this->MemberName = $member; | |
$this->Field = $field; | |
} | |
function Change() { | |
$this->Changed = 1; | |
} | |
} | |
class DatabaseTableObject extends InvPropertyVars { | |
protected $__database_fields = array(); | |
protected $__primary; | |
protected $Table; | |
protected function SetFields(array $fields) { | |
foreach ($fields as $k => $z) { | |
$this->__database_fields[$z] = new DatabaseField($k,$z); | |
} | |
} | |
public function SetTable($tbl) { | |
$this->Table = $tbl; | |
} | |
public function SetPrimary($fdl) { | |
$this->__primary = $fdl; | |
} | |
public function Commit() { | |
$p = $this->__primary; | |
$updtquery = mysqlf('UPDATE %t SET ',$this->Table); | |
foreach ($this->__database_fields as $k => $z) { | |
if ($z->Changed) { | |
$m = $z->MemberName; | |
$updtquery .= mysqlf('%t = %X, ',$z->Field,$this->$m); | |
} | |
} | |
$d = $this->__database_fields[$p]->MemberName; | |
$updtquery = substr($updtquery,0,-2).mysqlf(' WHERE %t = %n',$p,$this->$d); | |
print $updtquery; | |
return MySQL::Query($updtquery); | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sqlgroup($array,$del="'") { | |
if (!is_array($array)) return; | |
$tmp = implode($del.','.$del,$array); | |
return $del.$tmp.$del; | |
} | |
function mysqlf($str,$va_list) { | |
$num_args = func_num_args(); | |
/* Format: %n = Number: '10' | |
%s = String: "test" | |
%t = Table or field: `tablename` | |
%f = Table AND field: `table`.`field` | |
%F = Grouped field: `id`,`user`,`time` | |
%g = Grouped values: '42',"Name",'1246549800' | |
%X = Find out if it's either a numeric value or string literal | |
mysqlf('INSERT INTO %t (%F) VALUES (%g) WHERE %t = %n AND %t = %s;','users',$fields,$values,'id',22,'name',$User->Name) returns: | |
INSERT INTO `users` (`ctime`,`mtime`,`bio`) VALUES ('132456132','132231464',"Hi") WHERE `id` = 22 AND `name` = "Evans"; | |
*/ | |
for ($i=1;$i<$num_args;$i++) { | |
$d = func_get_arg($i); | |
$f = strpos($str,'%'); | |
switch ($str[$f+1]) { | |
case 'n': { | |
$str = substr_replace($str,'\''.$d.'\'',$f,2); | |
break; | |
} | |
case 's': { | |
$str = substr_replace($str,'"'.$d.'"',$f,2); | |
break; | |
} | |
case 't': { | |
$str = substr_replace($str,'`'.$d.'`',$f,2); | |
break; | |
} | |
case 'f': { | |
$repstr = ''; | |
foreach ($d as $z) { | |
$repstr .= '`'.$z.'`.'; | |
} | |
$repstr = substr($repstr,0,-1); | |
$str = substr_replace($str,$repstr,$f,2); | |
break; | |
} | |
case 'F': { | |
$repstr = ''; | |
foreach ($d as $z) { | |
$repstr .= '`'.$z.'`,'; | |
} | |
$repstr = substr($repstr,0,-1); | |
$str = substr_replace($str,$repstr,$f,2); | |
break; | |
} | |
case 'g': { | |
$repstr = ''; | |
foreach ($d as $z) { | |
if (is_numeric($z)) { | |
$repstr .= '\''.$z.'\','; | |
} else { | |
$repstr .= '"'.$z.'",'; | |
} | |
} | |
$repstr = substr($repstr,0,-1); | |
$str = substr_replace($str,$repstr,$f,2); | |
break; | |
} | |
case 'X': { | |
$repstr = ''; | |
if (is_numeric($d)) { | |
$repstr .= '\''.$d.'\','; | |
} else { | |
$repstr .= '"'.$d.'",'; | |
} | |
$repstr = substr($repstr,0,-1); | |
$str = substr_replace($str,$repstr,$f,2); | |
break; | |
} | |
} | |
} | |
return $str; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Post extends InvPropertyVars { | |
protected $Id = ''; | |
protected $ImgFile = ''; | |
protected $TnFile = ''; | |
protected $User = ''; | |
protected $posttime = ''; | |
protected $filter = ''; | |
protected $score = ''; | |
protected $imgsize = ''; | |
protected $ctime = ''; | |
protected $mtime = ''; | |
protected $flags = ''; | |
protected $views = ''; | |
protected $type = ''; | |
protected $tag_list = array(); | |
protected $Source = ''; | |
protected $__trashed_tags = array(); | |
protected $__added_tags = array(); | |
public function __construct($id = '', $imgfile = '', $tnfile = '', $user = '', $posttime = '', $filter = '', $score = '', $imgsize = '', $ctime = '', $mtime = '', $flags = '', $views = '', $type = '', $tag_list = array()) { | |
$this->Id = $id; | |
$this->ImgFile = $imgfile; | |
$this->TnFile = $tnfile; | |
$this->User = $user; | |
$this->PostTime = $posttime; | |
$this->filter = $filter; | |
$this->score = $score; | |
$this->imgsize = $imgsize; | |
$this->ctime = $ctime; | |
$this->mtime = $mtime; | |
$this->flags = $flags; | |
$this->views = $views; | |
$this->type = $type; | |
$this->tag_list = $tag_list; | |
$this->__invisible = array('tag_list','__trashed_tags','__added_tags'); | |
} | |
public function Populate($res) { | |
$this->Id = $res['id']; | |
$this->ImgFile = $res['imgfile']; | |
$this->TnFile = $res['tnfile']; | |
$this->User = $res['user']; | |
$this->PostTime = $res['posttime']; | |
$this->filter = $res['filter']; | |
$this->score = $res['score']; | |
$this->imgsize = $res['imgsize']; | |
$this->ctime = $res['ctime']; | |
$this->mtime = $res['mtime']; | |
$this->flags = $res['flags']; | |
$this->views = $res['views']; | |
$this->type = $res['type']; | |
$this->Source = $res['source']; | |
} | |
public function GetId() { | |
return $this->Id; | |
} | |
public function GetThumb() { | |
return $this->TnFile; | |
} | |
public function AddTag($tag) { | |
if (in_array($tag,$this->tag_list) || in_array($tag,$this->__added_tags)) { | |
return 0; | |
} | |
$this->__added_tags[] = $tag; | |
$this->tag_list[] = $tag; | |
return 1; | |
} | |
// benchmark with array_unique later with SORT_NUMERIC | |
public function AddTags(array $tag_array) { | |
$count = 0; | |
foreach ($tag_array as $z) { | |
$count += $this->AddTag($z); | |
} | |
return $count; | |
} | |
public function RemoveTag($tag) { | |
if (in_array($tag,$this->__added_tags)) { | |
$this->__added_tags = array__remove($tag,$this->__added_tags); | |
return; | |
} | |
$this->__trashed_tags[] = $tag; | |
$this->tag_list = array__remove($tag,$this->tag_list); | |
} | |
public function RemoveTags(array $tag_array) { | |
foreach ($tag_array as $z) { | |
$this->RemoveTag($z); | |
} | |
return; | |
} | |
public function CommitTags() { | |
if ($this->__trashed_tags) { | |
$trashSTR = sqlgroup($this->__trashed_tags); | |
MySQL::Query($q='UPDATE post_tags SET dtime=\''.time().'\', del_id = 0 WHERE dtime IS NULL AND post_id =\''.$this->Id.'\' AND tag_id IN ('.$trashSTR.')'); | |
MySQL::Query($q='UPDATE tags SET count=count-1 WHERE id IN ('.$trashSTR.')'); | |
$this->__trashed_tags = array(); | |
} | |
if ($this->__added_tags) { | |
$upcSTR = sqlgroup($this->__added_tags); | |
foreach ($this->__added_tags as $z) { | |
$updtSTR .= '(\''.$this->Id.'\',\''.$z.'\',\''.time().'\',\'0\'), '; | |
} | |
$updtSTR = trim($updtSTR,' ,'); | |
MySQL::Query($q='INSERT INTO post_tags (post_id,tag_id,ctime,user) VALUES '.$updtSTR); | |
MySQL::Query($q='UPDATE tags SET count=count+1 WHERE id IN ('.$upcSTR.')'); | |
$this->__added_tags = array(); | |
} | |
$this->tag_list = array_merge($this->tag_list,$this->__added_tags); | |
} | |
//MySQL::Query(); | |
public function SetTags(array $tag_array) { | |
$this->tag_list = $tag_array; | |
} | |
public function RetrieveTags() { | |
$res = MySQL::Query($q='SELECT `tags`.`id`, `tags`.`text` FROM `post_tags` LEFT JOIN `tags` ON `tags`.`id` = `post_tags`.`tag_id` WHERE `post_id` = '.$this->Id.' AND `post_tags`.`dtime` IS NULL ORDER BY `text` LIMIT 0 , 1000'); | |
if (MySQL::NumRows($res) != 0) { | |
$this->tag_list = array(); | |
while ($asd = MySQL::Fetch($res)) { | |
$this->tag_list[] = $asd['id']; | |
} | |
} | |
} | |
public function GetTag($index) { | |
if ($index < count($tag_list)) { | |
return $tag_list[$index]; | |
} | |
return false; | |
} | |
public function GetTagList() { | |
return $this->tag_list; | |
} | |
public function __toString() { | |
return $this->Id; | |
} | |
public function Insert() { | |
// inserts post into DB | |
} | |
public static function QueryAndPop($id) { | |
$tmp = new Post($id); | |
$tmp->Populate(MySQL::Fetch(MySQL::Query("SELECT * FROM posts WHERE id = '$id'"))); | |
return $tmp; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('usergroups.php'); | |
require_once('types.php'); | |
class UserException extends Exception {} | |
class User extends DatabaseTableObject { | |
protected $Id = -1; /* done */ | |
protected $PostCount = 0; /* done */ | |
protected $LastIP = '127.0.0.1'; /* done */ | |
protected $LastSeen = null; /* done */ | |
public $Settings = array( 'TimeZone' => 0, | |
'PPP' => 0, | |
'CPP' => 0, | |
'Locale' => 'en_us' | |
); | |
protected $Views = 0; | |
protected $ProfileViews = 0; | |
protected $GroupIds = array(1); | |
public function __construct($id=null) { | |
$this->SetFields(array( 'Id' => 'id', | |
'PostCount' => 'numpost', | |
'LastIP' => 'lastip', | |
'LastSeen' => 'lastseen', | |
'Views' => 'views', | |
'ProfileViews' => 'profviews' | |
) | |
); | |
$this->SetTable('users'); | |
$this->SetPrimary('id'); | |
if ($id) $this->SetId($id); | |
} | |
public function SetId($id) { | |
if (is_int($id) && $id) { | |
if (!$this->Id) $first = 1; | |
$this->Id = $id; | |
if ($first == 1) { | |
$this->__database_fields['id']->Change(); | |
} | |
} else { | |
throw new TypeErrorException(1); | |
} | |
} | |
public function SetPostCount($val) { | |
if (is_int($val)) { | |
if ($val >= 0) { | |
$this->PostCount = $val; | |
$this->__database_fields['numpost']->Change(); | |
} else { | |
throw new TypeErrorException(2); | |
} | |
} else { | |
throw new TypeErrorException(2); | |
} | |
} | |
public function SetIP($ip) { | |
if (preg_match('/\A([0-9]{1,3}\.){3}[0-9]{1,3}\Z/',$ip) != 0) { | |
$ips = explode('.',$ip); | |
foreach ($ips as $z) { | |
if ($z > 255) { | |
throw new TypeErrorException(3); | |
} | |
} | |
$this->LastIP = $ip; | |
$this->__database_fields['lastip']->Change(); | |
} else { | |
throw new TypeErrorException(3); | |
} | |
} | |
public function SetSeenDate($utime) { | |
if (is_int($utime)) { | |
if ($utime >= 0) { | |
$this->LastSeen = $utime; | |
$this->__database_fields['lastseen']->Change(); | |
} | |
} elseif (is_null($utime)) { | |
$this->LastSeen = null; | |
$this->__database_fields['lastseen']->Change(); | |
} else { | |
throw new TypeErrorException(4); | |
} | |
} | |
} | |
class Member extends User { | |
protected $Name = ''; | |
protected $Password = ''; | |
protected $Email = ''; | |
protected $JoinDate = ''; | |
protected $CommentCount = 0; | |
public function __construct($id) { | |
parent::__construct($id); | |
} | |
public function Populate($id) { | |
$res = MySQL::Fetch(MySQL::Query("SELECT * FROM users WHERE id = '$id'")); | |
$this->Id = $res['id']; | |
$this->Name = $res['name']; | |
$this->PostCount = $res['numpost']; | |
$this->LastIP = $res['lastip']; | |
$this->LastSeen = $res['lastseen']; | |
$this->PPP = $res['ppp']; | |
$this->CPP = $res['cpp']; | |
$this->Locale = $res['locale']; | |
$this->Views = $res['views']; | |
$this->ProfileViews = $res['profviews']; | |
$this->Password = $res['password']; | |
$this->Email = $res['email']; | |
$this->JoinDate = $res['joindate']; | |
$this->CommentCount = $res['numcom']; | |
} | |
public function LoginPop() { | |
$res = MySQL::Query($q='SELECT id FROM users WHERE name = "'.$this->Name.'" AND password = "'.$this->Password.'"'); | |
if (MySQL::NumRows($res) == 0) { | |
return false; | |
} | |
$result = MySQL::Fetch($res); | |
$this->Id = $result['id']; | |
return true; | |
} | |
public function LoginPopC() { | |
if ($this->Id == -1) throw new TypeErrorException(5); | |
$res = MySQL::Query($q='SELECT id FROM users WHERE id = "'.$this->Id.'" AND password = "'.$this->Password.'"'); | |
if (MySQL::NumRows($res) == 0) { | |
return false; | |
} | |
$result = MySQL::Fetch($res); | |
return true; | |
} | |
public function RetrieveSettings() { | |
$res = MySQL::Fetch(MySQL::Query($q='SELECT name FROM users WHERE id = "'.$this->Id.'"')); | |
$this->Name = $res['name']; | |
$this->Settings['PPP'] = $res['ppp']; | |
$this->Settings['CPP'] = $res['cpp']; | |
$this->Settings['Locale'] = $res['locale']; | |
} | |
public function SetName($name) { | |
$this->Name = $name; | |
} | |
public function SetPass($pwd) { | |
$this->Password = $pwd; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment