AmdaNews.php 2.72 KB
<?php
/**
 * @class AmdaNews
 */ 
 
class AmdaNews {

	public $user, $userdir, $infodir;

	public function __construct($user) {
		if ($user) {
			$this->user = $user;
			$this->userdir = USERPATH."/".$user."/";
			$this->infodir = HELPPATH."INFO/";
		}     
	}

/**
*     Add new info message and mark user dir with newInfo file
*/
	public function addInfo($group){

		// special dir for group news
		if ($group) $this->infodir = BASE_PATH.'help/'.$group.'_INFO/';
		else  $this->infodir = BASE_PATH.'help/INFO/';

		$timeStamp = time();
		$stringDate = date("Y-m-d",$timeStamp);
	
		if (!is_dir($this->infodir)) mkdir($this->infodir);

		$ff = fopen($this->infodir.'info.'.$timeStamp,'w');
		$infoToWrite = $_POST['info'];

		if (strpos($infoToWrite,"<a href=") !== false) {
				$infoToWrite = str_replace("<a href=","<a target='new' href=", $infoToWrite);
		}
		fwrite($ff,'<li><b>'.$stringDate.'</b><br/>'.$infoToWrite.PHP_EOL);           
		fclose($ff);  

		$userDir = BASE_PATH."data/";

		$users = glob($userDir."*");
		foreach ($users as $user) {
		
			$file = $user.'/newInfo';
			touch($file);          
			chmod($file, 0775);
	}

		return array("msg" => "New Info is saved", "success" => true);

	}

/**
*     Concate individual info messages into one to be shown to user
*/
	public function makeInfo($groups){

		if (file_exists($this->userdir.'lastLogin')) {

		$timeStamp = filemtime($this->userdir.'lastLogin');
		
		$ff = fopen($this->userdir.'INFO','w');
		fwrite($ff,'<IMG align="right" SRC="help/images/picto_amda.png"/><br/><h2>AMDA Latest News</h2><br/>');       
		fclose($ff);
	
		$userGrps = explode(',',strtoupper($groups));

		foreach ($userGrps as $grp) {
			if (is_dir(HELPPATH.$grp."_INFO")) {
				$grpDir = HELPPATH.$grp."_INFO/";
				$grpInfoFiles = glob($grpDir."info*");
				$grpFileList = array();

			foreach ($grpInfoFiles  as $file) {
					$grpFileList[filemtime($file)] = $file;
			}

			ksort($grpFileList);
			$grpFileList = array_reverse($grpFileList, TRUE);

				foreach ($grpFileList as $info) {
				if (filemtime($info) > $timeStamp) {
					file_put_contents($this->userdir.'INFO', file_get_contents($info), FILE_APPEND);
					$NN++;
					}  
				}
			}
		}

		$publicInfoFiles = glob($this->infodir."info*");
		$fileList = array();

		foreach ($publicInfoFiles  as $file) {
			$fileList[filemtime($file)] = $file;
		}

		ksort($fileList);
		$fileList = array_reverse($fileList, TRUE);

		$NN = 0;

		foreach ($fileList as $info) {
			if (filemtime($info) > $timeStamp) {
					file_put_contents($this->userdir.'INFO', file_get_contents($info), FILE_APPEND);
					$NN++;
			}            
		}  

		if ($NN == 0) {
						unlink($this->userdir.'INFO'); // No news  
						return 0;
				}  
		else return 1;               
	}
		else return 0;
	}
}
?>