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