PageRenderTime 124ms CodeModel.GetById 80ms app.highlight 16ms RepoModel.GetById 24ms app.codeStats 1ms

/php/classes/page.class.php

http://github.com/newscloud/open-social-media-toolkit
PHP | 342 lines | 281 code | 46 blank | 15 comment | 14 complexity | 318d34f1093a7cd084f8a216bfa543eb MD5 | raw file
  1<?php
  2
  3class newsroomPage extends XHTMLpage {
  4	var $header = '';
  5	var $sidebar = '';
  6	var $content = '';
  7	var $footer = '';
  8	var $agent;
  9	var $googleAnalytics;
 10	var $keywordDescription;
 11	var $keywordList;
 12	var $isIE=false;
 13	
 14	function newsroomPage($title='NewsCloud.com')
 15	{
 16		global $init;	
 17		// Construct the parent
 18		parent::XHTMLPage($title);	
 19		// main layout file
 20		$this->agent=$_SERVER['HTTP_USER_AGENT'];
 21		$this->isIE=(eregi("msie",$this->agent) && !eregi("opera",$this->agent));
 22		$this->googleAnalytics='<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));</script><script type="text/javascript"> var pageTracker = _gat._getTracker("'.$init[analytics].'"); pageTracker._initData(); pageTracker._trackPageview(); </script>';
 23		$this->keywordDescription='Must read stories from around the Web';
 24		$this->keywordList='newscloud, breaking news,international news,united states news,technology,environment,politics,business,opinion,blogging,science,health,sports,arts,oddities,religion,social networks,open source';		
 25	}
 26	
 27	function display()
 28	{
 29		$this->add( $this->_genMain() );
 30		echo $this->getPage();
 31	}
 32	
 33	function buildFrameset($topPage='',$bottomPage='') {
 34		
 35	}
 36	
 37	function addToHeader($string)
 38	{
 39		$this->header .= $string;
 40	}
 41	function addToContent($string)
 42	{
 43		$this->content .= $string;
 44	}
 45	function addToFooter($string)
 46	{
 47		$this->footer .= $string;
 48	}
 49		
 50	function _genMain()
 51	{
 52		$ret = '<div id="main"><!-- main -->';
 53		$ret .= $this->_genHeader();
 54		$ret .= $this->_genContent();
 55		$ret .= $this->_genFooter();		
 56		$ret .= '<!-- /main --></div>';
 57		return $ret;
 58	}
 59
 60	function _genHeader()
 61	{
 62		return '<div id="header"><!-- header -->' . $this->header . '</div><!-- /header -->';
 63	}
 64
 65	function _genContent()
 66	{
 67		return '<div id="content"><!-- content -->' . $this->content . '</div><!-- /content -->';
 68	}
 69
 70	function _genFooter()
 71	{
 72		return '<div id="footer"><!-- footer -->'.$this->footer.'</div><!-- /footer -->';
 73	}
 74}
 75
 76
 77class XHTMLpage {
 78	
 79	var $title = '';
 80	var $description = '';
 81	var $siteTagline='Must read stories from around the Web';
 82	var $html = '';
 83	
 84	var $doctype_tag = '';
 85	var $onload = '';
 86	
 87	var $keywords    = array();
 88	var $stylesheets = array();
 89	var $scripts     = array();
 90	var $miscHead	 = array();
 91	var $rssfeeds	 = array();
 92	var $atomfeeds	 = array();
 93		
 94	var $page_time_start;
 95	
 96	function XHTMLPage($title = '')
 97	{
 98		$this->page_time_start = microtime();		
 99		$this->setTitle($title);
100		$this->doctype_tag = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
101	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
102	}
103	
104	function getPage()
105	{
106		$page = $this->doctype_tag;
107		$page .= '<html>';
108		$page .= $this->_genHead();
109		$page .= $this->_genBody();		
110		return $page . '</html><!-- Page built in ' . $this->getCurrentExecTime() . ' seconds. --> ';
111	}
112	
113	function display()
114	{
115		echo $this->getPage();
116	}
117	
118	function add($HTML)
119	{
120		$this->html .= $HTML;
121	}
122	
123	function addStyle($stylesheet_path)
124	{
125		$this->stylesheets[] = trim($stylesheet_path);
126	}
127	
128	function addHead($miscStr)
129	{
130		$this->miscHead[] = trim($miscStr);
131	}
132	
133	function addScript($script_path)
134	{
135		$this->scripts[] = trim($script_path);		
136	}
137	
138	function pkgScripts($page='default',$scripts) {
139		$scripts=array_merge(array(PATH_PHP_SCRIPTS.'/common.js'),$scripts);
140		$this->scripts[]=URL_CACHE."&cf=".$page."_".$this->fetchPkgVersion($page,$scripts,'js',true).".js";
141	}
142
143	function pkgStyles($page='default',$sheets) {
144		// packages get common, header and layout
145		$sheets=array_merge(array(PATH_PHP_STYLES.'/layout.css',PATH_PHP_STYLES.'/ads.css'),$sheets);
146		$this->stylesheets[]=URL_CACHE."&cf=".$page."_".$this->fetchPkgVersion($page,$sheets,'css',false).".css";
147	}
148
149	function fetchPkgVersion($page,$files,$mode='js',$jsCompress=false) {
150	   $sDocRoot = $_SERVER['DOCUMENT_ROOT'];
151	   define('JSMIN_COMMENTS', ''); // any comments to append to the top of the compressed output
152	   define('JSMIN_AS_LIB', true); 
153	  // get file last modified dates
154	  $aLastModifieds = array();
155	  foreach ($files as $sFile) {
156	     $aLastModifieds[] = filemtime("$sFile");
157	  }
158	  // sort dates, newest first
159	  rsort($aLastModifieds);
160 	 $iETag=$aLastModifieds[0];	
161       // create a directory for storing current and archive versions
162      if (!is_dir(ARCHIVE_FOLDER)) {
163         mkdir(ARCHIVE_FOLDER);
164      }
165      
166      $sMergedFilename = ARCHIVE_FOLDER."/".$page."_".$iETag.".".$mode;
167      // if it does not exist, we need to create a new merged package
168      if (!file_exists($sMergedFilename)) {
169         // get and merge code
170         $sCode = '';
171         $aLastModifieds = array();
172         foreach ($files as $sFile) {
173            $aLastModifieds[] = filemtime("$sFile");
174            $sCode .= file_get_contents("$sFile");
175         }
176         // sort dates, newest first
177         rsort($aLastModifieds);
178         // reset iETag incase of late breaking file update
179	 	 $iETag=$aLastModifieds[0];         
180	      $sMergedFilename = ARCHIVE_FOLDER."/".$page."_".$iETag.".".$mode;
181           $this->pkgWrite($sMergedFilename, $sCode);
182           if ($jsCompress) {
183			  require_once(JSMIN_PATH."/jsmin.php");
184              if (JSMIN_COMMENTS != '') {
185                 $jsMin = new JSMin(file_get_contents($sMergedFilename), false, JSMIN_COMMENTS);
186              } else {
187                 $jsMin = new JSMin(file_get_contents($sMergedFilename), false);
188              }
189              $sCode = $jsMin->minify();              
190              $this->pkgWrite($sMergedFilename, $sCode);
191           }
192      }
193	  // return latest timestamp
194	 return $iETag;
195	}	
196	
197   function pkgWrite($sFilename, $sCode) {
198      $oFile = fopen($sFilename, 'w');
199      if (flock($oFile, LOCK_EX)) {
200         fwrite($oFile, $sCode);
201         flock($oFile, LOCK_UN);
202      }
203      fclose($oFile);
204   }
205
206	function addRSSFeed($rssfeed_path)
207	{
208		$this->rssfeeds[] = trim($rssfeed_path);
209	}
210
211	function addATOMFeed($atomfeed_path)
212	{
213		$this->atomfeeds[] = trim($atomfeed_path);
214	}
215	
216	function setTitle($title='NewsCloud.com')
217	{
218		if ($title=='NewsCloud.com') {
219			$title.=' - '.$this->siteTagline;
220		}
221		$this->title = $title;
222		$this->title.=' - '.SITE_TITLE;
223	}
224	
225	function setDescription($description)
226	{
227		$this->description = strip_tags($description);
228	}
229	
230	function setOnload($string)
231	{
232		$this->onload = $string;
233	}
234
235	function addKeywords($keywords)
236	/**
237	 * Takes either an array or a comma/whitespace seperated list of keywords
238	 * and appends them to the local keywords array property
239	 */
240	{
241		if ( is_array($keywords) ) {
242			$new_array = $keywords;
243		} else {
244			$new_array = preg_split("([[:space:]]|,|;)", $keywords, -1, PREG_SPLIT_NO_EMPTY);
245		}
246		$this->keywords = array_unique(array_merge( $new_array,$this->keywords));
247	}
248	
249	function _genHead()
250	{
251		$head = '<head><meta http-equiv="Content-type" content="text/html;charset=UTF-8" />';
252		$head .= $this->_genTitle();
253		$head .= $this->_genDescription();
254		$head .= $this->_genKeywords();
255		$head .= $this->_genStylesheets();
256		$head .= $this->_genFeeds();
257		$head .= $this->_genMiscHead();
258		$head .= $this->_genScripts();		
259		return $head . '</head>';
260	}
261	
262	function _genBody()
263	{
264		$body = '<body onload="' . $this->onload . '">';
265		if (!strpos($_SERVER['HTTP_HOST'],'local'))
266			$body.=$this->googleAnalytics;
267		$body .= $this->html;
268		return $body.'</body>';
269	}
270	
271	function _genTitle()
272	{
273		$title = '<title>' . $this->title . '</title>';
274		return $title;
275	}
276	
277	function _genDescription()
278	{
279		return '<meta name="description" content="' . trim($this->description) . '" />';
280	}
281	
282	function _genKeywords()
283	{
284		$words = '';
285		foreach ($this->keywords as $key => $val) {
286			$words .= $val . ", ";
287		}
288		$tag = '<meta name="keywords" content="' . substr($words, 0, -2) . '" />';
289		return $tag;
290	}
291
292	function _genStylesheets()
293	{
294		$ret = '';
295		foreach (array_unique($this->stylesheets) as $key => $val) {
296			$ret .= '<link rel="stylesheet" href="' . $val . '" type="text/css" charset="utf-8" />';
297		}
298		$ret.='<link rel="icon" href="'.URL_HOME.'?p=img&img=favicon.gif" type="image/gif" />';
299		return $ret;
300	}
301
302	function _genMiscHead()
303	{
304		$ret = '';
305		foreach (array_unique($this->miscHead) as $key => $val) {
306			$ret .= $val;
307		}
308		return $ret;
309	}
310	
311	function _genFeeds() {
312		$ret = '';
313		foreach (array_unique($this->rssfeeds) as $key => $val) {
314			$ret .= '<link rel="alternate" title="RSS" href="' . $val . '" type="application/rss+xml">';
315		}
316		foreach (array_unique($this->atomfeeds) as $key => $val) {
317			$ret .= '<link rel="alternate" title="ATOM" href="' . $val . '" type="application/atom+xml">';
318		}
319		return $ret;		
320	}
321	
322	function _genScripts()
323	{
324		$ret = '';
325		foreach (array_unique($this->scripts) as $key => $val) {
326			$ret .= '<script src="' . $val . '" type="text/javascript" language="javascript" charset="utf-8"></script>';			
327		}
328
329		return $ret;
330	}
331	
332	function getCurrentExecTime()
333	{
334	    $page_time_end = microtime();
335		return $page_time_end - $this->page_time_start;
336	}
337	
338	function isRobot() {
339		return (eregi("googlebot",$this->agent) || eregi("yahooseeker",$this->agent) || eregi("msnbot",$this->agent));		
340	}
341}
342?>