PageRenderTime 54ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/ww.php_classes/Page.php

http://kv-webme.googlecode.com/
PHP | 426 lines | 268 code | 27 blank | 131 comment | 60 complexity | a09586423d2998375cddb86d1ca68e81 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, BSD-3-Clause, BSD-2-Clause, Apache-2.0, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * the Page object
  4. *
  5. * PHP version 5.2
  6. *
  7. * @category None
  8. * @package None
  9. * @author Kae Verens <kae@kvsites.ie>
  10. * @license GPL 2.0
  11. * @link http://kvsites.ie/
  12. */
  13. // { class Page
  14. /**
  15. * Page object
  16. *
  17. * @category WebME
  18. * @package WebME
  19. * @author Kae Verens <kae@kvsites.ie>
  20. * @license GPL 2.0
  21. * @link http://kvweb.me/
  22. */
  23. class Page{
  24. static $instances = array();
  25. static $instancesByName = array();
  26. static $instancesByNAndP = array();
  27. static $instancesByProductType = array();
  28. static $instancesBySpecial = array();
  29. static $instancesByType = array();
  30. public $vals;
  31. // { __construct
  32. /**
  33. * instantiate a Page object
  34. *
  35. * @param mixed $v ID of the page, or other method of identification
  36. * @param int $byField which method of identification to use
  37. * @param array $fromRow pre-filled array of page data to load
  38. * @param array $pvq pre-filled array of meta-data to load
  39. *
  40. * @return object the page instance
  41. */
  42. function __construct($v, $byField=0, $fromRow=0, $pvq=0) {
  43. // byField: 0=ID; 1=Name
  44. if (!$byField && is_numeric($v)) {
  45. if ($fromRow) {
  46. $r=$fromRow;
  47. }
  48. else {
  49. if ($v) {
  50. $r=Core_cacheLoad('pages', 'id'.$v, -1);
  51. if ($r===-1) {
  52. $r=dbRow("select * from pages where id=$v limit 1");
  53. if (count($r)) {
  54. Core_cacheSave('pages', 'id'.$v, $r);
  55. }
  56. }
  57. }
  58. else {
  59. $r=array();
  60. }
  61. }
  62. }
  63. elseif ($byField == 1) { // by alias (name)
  64. if (preg_match('/[^a-zA-Z0-9 \-_]/', $v)) {
  65. return false;
  66. }
  67. $name=strtolower(str_replace('-', '_', $v));
  68. $fname='page_by_name_'.md5($name);
  69. $r=Core_cacheLoad('pages', $fname, -1);
  70. if ($r===-1) {
  71. $r=dbRow(
  72. "select * from pages where alias like '".addslashes($name)
  73. ."' limit 1"
  74. );
  75. if (count($r)) {
  76. Core_cacheSave('pages', $fname, $r);
  77. }
  78. }
  79. }
  80. elseif ($byField == 2) { // by type
  81. $fname='page_by_type_'.$v;
  82. $r=Core_cacheLoad('pages', $fname);
  83. if ($r===false) {
  84. $r=dbRow("select * from pages where type like '$v%' limit 1");
  85. if ($r===false) {
  86. $r=array();
  87. }
  88. Core_cacheSave('pages', $fname, $r);
  89. }
  90. }
  91. elseif ($byField == 3 && is_numeric($v)) { // by special
  92. $fname='page_by_special_'.$v;
  93. $r=Core_cacheLoad('pages', $fname);
  94. if ($r===false) {
  95. $r=dbRow("select * from pages where special&$v limit 1");
  96. if ($r===false) {
  97. $r=array();
  98. }
  99. Core_cacheSave('pages', $fname, $r);
  100. }
  101. }
  102. elseif ($byField == 4) {
  103. $r=$v;
  104. }
  105. else {
  106. return false;
  107. }
  108. if (!count($r || !is_array($r))) {
  109. return false;
  110. }
  111. if (!isset($r['id'])) {
  112. $r['id']=0;
  113. }
  114. if (!isset($r['type'])) {
  115. $r['type']=0;
  116. }
  117. if (!isset($r['special'])) {
  118. $r['special']=0;
  119. }
  120. if (!isset($r['name'])) {
  121. $r['name']='NO NAME SUPPLIED';
  122. }
  123. foreach ($r as $k=>$v) {
  124. $this->{$k}=$v;
  125. }
  126. if (!isset($r['alias'])) {
  127. $r['alias']=$r['name'];
  128. }
  129. $this->urlname=$r['alias'];
  130. $this->dbVals=$r;
  131. self::$instances[$this->id] =& $this;
  132. self::$instancesByName[preg_replace(
  133. '/[^,a-z0-9]/',
  134. '-',
  135. strtolower($this->urlname)
  136. )] =& $this;
  137. self::$instancesBySpecial[$this->special] =& $this;
  138. // {page type
  139. if (strpos($this->type, '|')) {
  140. $this->plugin=preg_replace('/\|.*/', '', $this->type);
  141. $this->type=preg_replace('/.*\|/', '', $this->type);
  142. }
  143. // }
  144. self::$instancesByType[$this->type] =& $this;
  145. // { set up values if supplied. otherwise, delay it 'til required
  146. $this->__valuesLoaded=false;
  147. if ($pvq) {
  148. $this->initValues($pvq);
  149. }
  150. // }
  151. }
  152. // }
  153. // { getInstance
  154. /**
  155. * get an instance of a page by its ID
  156. *
  157. * @param int $id ID of the page
  158. * @param array $fromRow pre-filled array of page data to load
  159. * @param array $pvq pre-filled array of meta-data to load
  160. *
  161. * @return object the page instance
  162. */
  163. static function getInstance($id=0, $fromRow=false, $pvq=false) {
  164. if (!is_numeric($id)) {
  165. return false;
  166. }
  167. if (!array_key_exists($id, self::$instances)) {
  168. self::$instances[$id]=new Page($id, 0, $fromRow, $pvq);
  169. }
  170. return self::$instances[$id];
  171. }
  172. // }
  173. // { getInstanceByName
  174. /**
  175. * get an instance of a page by name
  176. *
  177. * @param string $name the name of the page to find
  178. *
  179. * @return object the page instance
  180. */
  181. static function getInstanceByName($name='') {
  182. if (preg_match('/[^!,a-zA-Z0-9 \-_\/]/', $name)) {
  183. return false;
  184. }
  185. $name=strtolower($name);
  186. $nameIndex=preg_replace('#[^,a-z0-9/]#', '-', $name);
  187. if (array_key_exists($nameIndex, self::$instancesByName)) {
  188. return self::$instancesByName[$nameIndex];
  189. }
  190. if (strpos($name, '/')) {
  191. $names=explode('/', $nameIndex);
  192. $pid=0;
  193. foreach ($names as $n) {
  194. $p=self::getInstanceByNameAndParent($n, $pid);
  195. if (!$p || !isset($p->id)) {
  196. return false;
  197. }
  198. $pid=$p->id;
  199. }
  200. self::$instancesByName[$nameIndex]=$p;
  201. }
  202. else {
  203. self::$instancesByName[$nameIndex]=new Page($name, 1);
  204. }
  205. return self::$instancesByName[$nameIndex];
  206. }
  207. // }
  208. // { getInstanceBySpecial
  209. /**
  210. * get an instance of a page by its special attribute
  211. *
  212. * @param int $sp special attribute value to search by
  213. *
  214. * @return object the page instance
  215. */
  216. static function getInstanceBySpecial($sp=0) {
  217. if (!is_numeric($sp)) {
  218. return false;
  219. }
  220. if (!array_key_exists($sp, self::$instancesBySpecial)) {
  221. self::$instancesBySpecial[$sp]=new Page($sp, 3);
  222. }
  223. return self::$instancesBySpecial[$sp];
  224. }
  225. // }
  226. // { getInstanceByType
  227. /**
  228. * get an instance of a page by its type
  229. *
  230. * @param mixed $type integer code or string name of page type
  231. *
  232. * @return object the page instance
  233. */
  234. static function getInstanceByType($type=0) {
  235. if (!array_key_exists($type, self::$instancesByType)) {
  236. new Page($type, 2);
  237. }
  238. if (!isset(self::$instancesByType[$type])) {
  239. return false;
  240. }
  241. return self::$instancesByType[$type];
  242. }
  243. // }
  244. // { getInstanceByNameAndParent
  245. /**
  246. * get an instance of a page by name and parent
  247. *
  248. * @param string $name the name of the page
  249. * @param int $parent the ID of the parent page
  250. *
  251. * @return object the page instance
  252. */
  253. static function getInstanceByNameAndParent($name, $parent) {
  254. if (preg_match('/[^,a-zA-Z0-9 \-_]/', $name)) {
  255. return false;
  256. }
  257. $name=str_replace('-', '_', $name);
  258. if (!array_key_exists($name.'/'.$parent, self::$instancesByNAndP)) {
  259. $r=Core_cacheLoad('pages', md5($parent.'|'.$name));
  260. if ($r===false) {
  261. $r=dbRow(
  262. "SELECT * FROM pages WHERE parent=$parent AND alias LIKE '"
  263. .addslashes($name)."'"
  264. );
  265. if ($r===false) {
  266. $r=array();
  267. }
  268. Core_cacheSave('pages', md5($parent.'|'.$name), $r);
  269. }
  270. if (!count($r)) {
  271. return false;
  272. }
  273. self::$instancesByNAndP[$name.'/'.$parent] = new Page($r, 4);
  274. }
  275. return self::$instancesByNAndP[$name.'/'.$parent];
  276. }
  277. // }
  278. // { getAbsoluteURL
  279. /**
  280. * get an absolute URL for the page, starting from http/https
  281. *
  282. * @return string the URL
  283. */
  284. function getAbsoluteURL() {
  285. $url=@$_SERVER['HTTPS']?'https':'http';
  286. $url.='://'.$_SERVER['HTTP_HOST'];
  287. return $url.$this->getRelativeURL();
  288. }
  289. // }
  290. // { getRelativeURL
  291. /**
  292. * get a relative URL for this page, starting from /
  293. *
  294. * @return string the URL
  295. */
  296. function getRelativeURL() {
  297. if (isset($this->relativeURL)) {
  298. return $this->relativeURL;
  299. }
  300. if (isset($this->vars['_short_url'])) {
  301. $this->relativeURL='/'.dbOne(
  302. 'select short_url from short_urls where page_id='.$this->id,
  303. 'short_url'
  304. );
  305. }
  306. else {
  307. $this->relativeURL='';
  308. if (@$this->parent) {
  309. $p=Page::getInstance($this->parent);
  310. if ($p) {
  311. $this->relativeURL.=$p->getRelativeURL();
  312. }
  313. }
  314. $this->relativeURL.='/'.$this->getURLSafeName();
  315. }
  316. return $this->relativeURL;
  317. }
  318. // }
  319. // { getTopParentId
  320. /**
  321. * get the ID of the top-level parent of this page
  322. *
  323. * @return int ID of the top page
  324. */
  325. function getTopParentId() {
  326. if (!isset($this->parent) || !$this->parent) {
  327. return $this->id;
  328. }
  329. $p=Page::getInstance($this->parent);
  330. return $p->getTopParentId();
  331. }
  332. // }
  333. // { getURLSafeName
  334. /**
  335. * get a version of the page's name which is safe for use in URLs
  336. *
  337. * @return string the name
  338. */
  339. function getURLSafeName() {
  340. if (isset($this->getURLSafeName)) {
  341. return $this->getURLSafeName;
  342. }
  343. $r=$this->urlname;
  344. $r=preg_replace('/[^,a-zA-Z0-9,-]/', '-', __FromJson($r, true));
  345. $this->getURLSafeName=$r;
  346. return $r;
  347. }
  348. // }
  349. // { initValues
  350. /**
  351. * load up a page's meta values
  352. *
  353. * @param array $pvq pre-filled values array (optional)
  354. *
  355. * @return object the Page instance
  356. */
  357. function initValues($pvq=false) {
  358. $this->vars=array();
  359. if (!$pvq) {
  360. $fname='page_vars_'.$this->id;
  361. $pvq=Core_cacheLoad('pages', $fname);
  362. if ($pvq===false) {
  363. $pvq=dbAll("select * from page_vars where page_id=".$this->id);
  364. Core_cacheSave('pages', $fname, $pvq);
  365. }
  366. }
  367. foreach ($pvq as $pvr) {
  368. $this->vars[$pvr['name']]=$pvr['value'];
  369. }
  370. return $this;
  371. }
  372. // }
  373. // { render
  374. /**
  375. * render a page template
  376. *
  377. * @return string rendered page
  378. */
  379. function render() {
  380. foreach ($GLOBALS['PLUGINS'] as $plugin) {
  381. if (isset($plugin['frontend']['body_override'])) {
  382. return $plugin['frontend']['body_override']($this);
  383. }
  384. }
  385. $smarty=Core_smartySetup(USERBASE.'/ww.cache/pages');
  386. global $_languages;
  387. $fname=USERBASE.'/ww.cache/pages/template_'
  388. .md5($this->id.'|'.join(',', $_languages));
  389. if (!file_exists($fname) || !filesize($fname)) {
  390. file_put_contents(
  391. $fname,
  392. __FromJson(str_replace(array("\n", "\r"), ' ', $this->body))
  393. );
  394. }
  395. return $smarty->fetch($fname);
  396. }
  397. // }
  398. }
  399. // }