All pastes #70030 Raw Edit

Astronouth7303

public php v1 · immutable
#70030 ·published 2006-06-23 18:25 UTC
rendered paste body
<?phpclass ImagesCache {	var $db, $uid, $ident, $config, $StatusTmp, $qryAdd, $pf;	var $aUid, $aSep1, $aSep2;		function ImagesCache($db, $uid, $configop='', $identity=null) {		$this->op = $configop;		$this->db = $db;		$this->uid = $uid;		$this->ident = $identity;		$this->config = Config::getInst($uid);		$this->pf = $configop;	}	function getSetting($name) {		return $this->config->get($name.$this->pf);	}		function getImage($medium, $status) {		$dir = $this->getSetting('imgdir');		$type = $this->getSetting('imgtyp');		$SPic = $dir.$medium.'_'.$status.'.'.$type;				if (!is_file($SPic)) {			$SPic = $dir.$medium.'_unknown.'.$type;			if (!is_file($SPic)) {				if ($status == "offline") {					$SPic = $dir.'UNKNOWN_offline.'.$type;				} else {					$SPic = $dir.'UNKNOWN_online.'.$type;				}			}		}		return $SPic;	}	function getImageFile($name, $num=false) {		$imgdir = $this->getSetting('imgdir');		$imgtyp = $this->getSetting("imgtyp");		$rvn = "{$imgdir}{$name}_{$num}.{$imgtyp}";		$rvp = "{$imgdir}{$name}.{$imgtyp}";		if ($num === false && is_file($rvp)) { # no number is specified and foo.ext exists...			$rv = $rvp;		} else if ($num !== false && is_file($rvn)) { # a number is specified and foo_#.ext exists ...			$rv = $rvn;		} else if ($num === false && !is_file($rvp)) { # no number is specified but foo.ext does not exist...			$rv = $this->getImageFile($name, 0);			if ($rv === false || !is_file($rv)) {				$rv = $this->getImageFile($name, 1);			}		} else if ($num !== false && !is_file($rvn)) { # a number is specified but foo_#.ext does not exist ...			$rv = $rvp;		} else {			$rv = false;		}		return $rv;	}	function getLeftImage($num=false) {		return $this->getImageFile('left', $num);	}		function getRighttImage($num=false) {		return $this->getImageFile('right', $num);	}	function getSeperatorImage($n) {		if ($n == 99) {			return false;		} else {			return $this->getSetting('imgdir')."SEP_".$n.".".$this->getSetting('imgtyp');		}	}	function getHeaderImages() {		return array(			$this->getLeftImage(),			$this->getImageFile('trillian'),			$this->getImageFile('DownloadTrillian'),		);	}		function getFooterImages() {		return array(			$this->getRighttImage(2),		);	}		#FIXME: Do this using some kind of iterator thing	function getStatusDBResult() {		// **** get all status for user (+ added userid if enabled)		if (isset($this->ident)) {			$sIdent = " AND `ident` = '".mysql_real_escape_string($this->ident)."'";		} else {			$sIdent = "";		}				$DBData = mysql_query("SELECT * FROM `".TABLE_STATUS."` WHERE (			`uid` = '".mysql_real_escape_string($this->uid)."' AND 			`visible` = '0'".$sIdent." AND 			(	`visoff` = '0' OR 				(`visoff` = '1' AND `status` <> 'offline')			)		) ".$this->qryAdd." ORDER BY `medium`, `name`", $this->db);		return $DBData;	}		/**	 * Returns the status and the image	 */	function getImageStatusList() {		// **** set all pics for status and get new temp status		$this->StatusTmp = "";		$TSData = array();				if (isset($this->aSEP1)) {			$TSData[] = $this->aSEP1;		}		$DBData = $this->getStatusDBResult();		if (mysql_num_rows($DBData) > 0) {			$IDTmp = 0;			$td = time() - $this->config->get('timeout');			while ($TData = mysql_fetch_assoc($DBData)) {				if ($IDTmp != $TData["uid"]) {					if ($IDTmp != 0) {						$TSData[] = $this->aSEP2;					}					$IDTmp = $TData["uid"];				}								if ($TData["lastupdate"] < $td) {					$TData["status"] = "offline";				}								if ($TData["medium"] != "ALL") {					$TData['img'] = $this->getImage($TData["medium"], $TData["status"]);					$TSData[] = $TData;					$this->StatusTmp .= $TData["status"];				}			}		}				return $TSData;	}		function getImageList() {		$list = $this->getImageStatusList();		$rv = $this->getHeaderImages();		foreach($list as $i) {			if (is_array($i)) {				$rv[] = $i['img'];			} else {				$rv[] = $i;			}		}		$rv = array_merge($rv, $this->getFooterImages());				chdir(ASTRO73_WWW_DOCROOT.'/status');		foreach($rv as $k => $v) {			if (!is_readable($v)) {				unset($rv[$k]);			}		}		$rv = array_values($rv);		return $rv;	}		function CacheGetPic($CacheOnly) {		// **** set vars if adding an userid for caching is enabled		$this->qryAdd = "";		$cPrefix = "";		if ($this->config->get('cadduid') != "") {			$tmp = explode(",", $this->config->get('cadduid'));			$this->aUID = $tmp[2];			$this->aSEP1 = $this->getSeperatorImage($tmp[0]);			$this->aSEP2 = $this->getSeperatorImage($tmp[1]);						$aIdent = Config::getInst($aUID)->get('SHOWIDENT');						if ($aIdent != "ALL") {				$aIdent = " AND `ident` = '".mysql_real_escape_string($aIdent)."'";			} else {				$aIdent = "";			}						$this->qryAdd = " OR (				`uid` = '".mysql_real_escape_string($aUID)."' AND 				`visible` = '0'".$aIdent." AND  				(`visoff` = '0' OR  					(`visoff` = '1' AND `status` <> 'offline')				)			) ";  						$cPrefix = "a";		}				if (isset($this->ident)) {			$sIdent = "_".strtolower($this->ident);		} else {			$sIdent = "";		}				$TSData = $this->getImageList();		$imgtyp = $this->getSetting("imgtyp");				// **** filename for cache		if ($this->config->get('tsmode') == "S") {			$TempFile = "temp/status".$sIdent.".".$imgtyp;		} else {			if ($cPrefix == "a") {				$TempFile = "temp/status_auid".($this->uid + $aUID).".".$imgtyp;			} else {				$TempFile = "temp/status_uid".$this->uid.$sIdent.".".$imgtyp;			}		}				// **** get temp status for user		if (isset($identity)) {			$sIdent = mysql_real_escape_string($identity);		} else {			$sIdent = "ALL";		}				$DBStat = mysql_query("SELECT `status` FROM `".TABLE_STATTMP."` WHERE `uid` = '".mysql_real_escape_string($this->uid)."' AND `ident` = '".$sIdent."' LIMIT 1", $this->db);				if (mysql_num_rows($DBStat) > 0) {			$StatusDB = mysql_result($DBStat, 0);		} else {			// **** if not status temp -> insert new			#mysql_query("INSERT INTO `".TABLE_STATTMP."` (uid, ident, status) VALUES ('".mysql_real_escape_string($this->uid)."', '".$sIdent."', '')", $this->db);			$StatusDB = "";		}				// **** check -> need to create new cache pic		if (!$this->config->getBool("cacheor")) {			$StatusDB = "ignore";		}				if (($this->StatusTmp != $StatusDB) || (!is_file($TempFile))) {			// save new temp-status			$sql = "REPLACE INTO `".TABLE_STATTMP."` SET `status` = '".mysql_real_escape_string($this->StatusTmp)."', `uid` = '".mysql_real_escape_string($this->uid)."', `ident` = '";			if (isset($this->ident)) {				$sql .= mysql_real_escape_string($this->ident);			} else {				$sql .= 'ALL';			}			$sql .= "'";			mysql_query($sql, $this->db);						// **** get pic-size			$pics = array();			$widths = array();			$heights = array();			foreach($TSData as $p) {				$a = GetImageSize($p);				$widths[] = $a[0];				$heights[] = $a[1];				$pics[] = $a;			}						// **** create new pic			$picw = array_sum($widths);			$pich = max($heights);			$pic = ImageCreateTrueColor($picw, $pich);						// **** fill $pic with background-color			$RGB = explode(",", $this->config->get("backrgb"));			if ($imgtyp == "png") {				ImageAlphaBlending($pic, false);				ImageSaveAlpha($pic, true);				$bgcolor = ImageColorAllocateAlpha($pic, $RGB[0], $RGB[1], $RGB[2], 127);			} else {				$bgcolor = ImageColorAllocate($pic, $RGB[0], $RGB[1], $RGB[2]);			}			ImageFill($pic, 0, 0, $bgcolor);						// **** copy all images to new image			// **** dst, src, dst_x, dst_y, src_x, src_y, src_w, src_h						if (count($TSData) > 0) {				$pic_x = 0;				foreach ($TSData as $picdata) {					if ($imgtyp == "png") {						$pic_tmp = ImageCreateFromPNG($picdata);						ImageAlphaBlending($pic_tmp, false);						ImageSaveAlpha($pic_tmp, true);					} else if ($imgtyp == "jpg") {						$pic_tmp = ImageCreateFromJPEG($picdata);					}					$x = ImageSX($pic_tmp);					ImageCopyMerge($pic, $pic_tmp, $pic_x, 0, 0, 0, $x, ImageSY($pic_tmp), 100);					$pic_x += $x;					ImageDestroy($pic_tmp);				}			}						// **** save new pic in temp-dir			if ($imgtyp == "png") {				imagecolortransparent($pic, $bgcolor);    									if (count($TSData) > 0) {					if ($this->config->getBool("cacheor")) {						ImagePNG($pic, $TempFile);					}				}								if (!$CacheOnly) {					header("Content-type: image/png");					ImagePNG($pic);				}			} else if ($imgtyp == "jpg") {				if (count($TSData) > 0) {					if ($this->config->getBool("cacheor")) {						ImageJPEG($pic, $TempFile);					}				}				if (!$CacheOnly) {					header("Content-type: image/jpeg");					ImageJPEG($pic);				}			}		} else {			if (!$CacheOnly) {				// **** get pic from cache				if ($imgtyp == "png") {					header("Content-type: image/png");				} else if ($imgtyp == "jpg") {					header("Content-type: image/jpeg");				}				readfile($TempFile);			}		}	}}?>