PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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