PageRenderTime 225ms CodeModel.GetById 121ms app.highlight 14ms RepoModel.GetById 87ms app.codeStats 0ms

/core/classes/pageMobile.class.php

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