PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/news_manager/inc/site.php

https://github.com/cnb/News_Manager-getsimplecms
PHP | 769 lines | 698 code | 19 blank | 52 comment | 73 complexity | f1c7da0c123151512bc3735f9c4637d1 MD5 | raw file
  1. <?php if (!defined('IN_GS')) {die('you cannot load this page directly.');}
  2. /**
  3. * News Manager front-end functions.
  4. */
  5. /*******************************************************
  6. * @function nm_show_page
  7. * @param $index - page index (pagination)
  8. * @param $filter - if true, apply content filter
  9. * @action show posts on news page
  10. */
  11. function nm_show_page($index=NMFIRSTPAGE, $filter=true) {
  12. global $NMPOSTSPERPAGE, $nmoption;
  13. $p1 = intval(NMFIRSTPAGE);
  14. $index = intval($index);
  15. $posts = nm_get_posts();
  16. $pages = array_chunk($posts, intval($NMPOSTSPERPAGE), true);
  17. if ($index >= $p1 && $index-$p1 < sizeof($pages))
  18. $posts = $pages[$index-$p1];
  19. else
  20. $posts = array();
  21. if (!empty($posts)) {
  22. $showexcerpt = nm_get_option('excerpt');
  23. if ($filter) ob_start();
  24. foreach ($posts as $post)
  25. nm_show_post($post->slug, $showexcerpt, false);
  26. if (sizeof($pages) > 1 && nm_get_option('shownav',true))
  27. nm_show_navigation($index, sizeof($pages));
  28. if ($filter) echo nm_ob_get_content(true);
  29. } else {
  30. echo '<p>' . i18n_r('news_manager/NO_POSTS') . '</p>';
  31. }
  32. }
  33. /*******************************************************
  34. * @function nm_show_archive
  35. * @param $id - unique archive id
  36. * @param $filter - if true, apply content filter
  37. * @action show posts by archive
  38. * @return true if posts shown
  39. */
  40. function nm_show_archive($archive, $filter=true) {
  41. global $NMSETTING;
  42. $archives = nm_get_archives($NMSETTING['archivesby']);
  43. if (array_key_exists($archive, $archives)) {
  44. $showexcerpt = nm_get_option('excerpt');
  45. $posts = $archives[$archive];
  46. if ($filter) ob_start();
  47. foreach ($posts as $slug)
  48. nm_show_post($slug, $showexcerpt, false);
  49. if ($filter) echo nm_ob_get_content(true);
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. }
  55. /*******************************************************
  56. * @function nm_show_tag
  57. * @param $tag - unique tag id
  58. * @param $filter - if true, apply content filter
  59. * @action show posts by tag
  60. * @return true if posts shown
  61. */
  62. function nm_show_tag($tag, $filter=true) {
  63. $tag = nm_lowercase_tags($tag);
  64. $tags = nm_get_tags();
  65. if (array_key_exists($tag, $tags)) {
  66. $showexcerpt = nm_get_option('excerpt');
  67. $posts = $tags[$tag];
  68. if ($filter) ob_start();
  69. foreach ($posts as $slug)
  70. nm_show_post($slug, $showexcerpt, false);
  71. if ($filter) echo nm_ob_get_content(true);
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. /*******************************************************
  78. * @function nm_show_tag_page
  79. * @param $tag - unique tag id
  80. * @param $index - page index (pagination)
  81. * @param $filter - if true, apply content filter
  82. * @action show posts by tag with pagination
  83. * @return true if posts shown
  84. * @since 3.0
  85. */
  86. function nm_show_tag_page($tag, $index=NMFIRSTPAGE, $filter=true) {
  87. global $NMPOSTSPERPAGE;
  88. $tag = nm_lowercase_tags($tag);
  89. $tags = nm_get_tags();
  90. if (array_key_exists($tag, $tags)) {
  91. $showexcerpt = nm_get_option('excerpt');
  92. $posts = $tags[$tag];
  93. $p1 = intval(NMFIRSTPAGE);
  94. $index = intval($index);
  95. $pages = array_chunk($posts, intval($NMPOSTSPERPAGE), true);
  96. if ($index >= $p1 && $index-$p1 < sizeof($pages)) {
  97. $posts = $pages[$index-$p1];
  98. if ($filter) ob_start();
  99. foreach ($posts as $slug)
  100. nm_show_post($slug, $showexcerpt, false);
  101. if (sizeof($pages) > 1 && nm_get_option('shownav',true))
  102. nm_show_navigation($index, sizeof($pages), $tag);
  103. if ($filter) echo nm_ob_get_content(true);
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. /*******************************************************
  110. * @function nm_show_search_results()
  111. * @action search posts by keyword(s)
  112. */
  113. function nm_show_search_results() {
  114. $keywords = @explode(' ', $_POST['keywords']);
  115. $posts = nm_get_posts();
  116. foreach ($keywords as $keyword) {
  117. $match = array();
  118. foreach ($posts as $post) {
  119. $data = getXML(NMPOSTPATH.$post->slug.'.xml');
  120. $content = $data->title . $data->content;
  121. if (stripos($content, $keyword) !== false)
  122. $match[] = $post;
  123. }
  124. $posts = $match;
  125. }
  126. if (!empty($posts)) {
  127. $showexcerpt = nm_get_option('excerpt');
  128. echo '<p>' . i18n_r('news_manager/FOUND') . '</p>',PHP_EOL;
  129. foreach ($posts as $post)
  130. nm_show_post($post->slug, $showexcerpt, false);
  131. } else {
  132. echo '<p>' . i18n_r('news_manager/NOT_FOUND') . '</p>',PHP_EOL;
  133. }
  134. }
  135. /*******************************************************
  136. * @function nm_reset_options
  137. * @param $pagetype news page type, can be 'single', 'main', 'archive', 'tag', 'search' or empty
  138. * @action set default or specific layout values
  139. * @since 3.0
  140. */
  141. function nm_reset_options($pagetype='') {
  142. global $nmoption, $NMSETTING, $NMSHOWEXCERPT;
  143. $nmoption = array();
  144. # pre 3.0 default settings (plus readmore in common.php)
  145. if (defined('NM2COMPAT') && NM2COMPAT) {
  146. $nmoption['breakwords'] = true;
  147. $nmoption['titletag'] = false;
  148. $nmoption['navoldnew'] = true;
  149. if (!defined('NMFIRSTPAGE')) define('NMFIRSTPAGE',0);
  150. }
  151. # full/excerpt, readmore
  152. if ($NMSHOWEXCERPT == 'Y' || in_array($pagetype, array('archive','search','tag'))) {
  153. $nmoption['excerpt'] = true;
  154. if ($NMSETTING['readmore'] == 'R')
  155. $nmoption['readmore'] = true;
  156. elseif ($NMSETTING['readmore'] == 'F')
  157. $nmoption['readmore'] = 'a';
  158. else
  159. $nmoption['readmore'] = false;
  160. } else {
  161. $nmoption['excerpt'] = false; // full post
  162. }
  163. # title link
  164. $nmoption['titlelink'] = ($NMSETTING['titlelink']=='Y' || ($NMSETTING['titlelink']=='P' && $pagetype != 'single'));
  165. # go back link
  166. if ($pagetype == 'single') {
  167. if ($NMSETTING['gobacklink'] == 'N')
  168. $nmoption['gobacklink'] = false;
  169. elseif ($NMSETTING['gobacklink'] == 'M')
  170. $nmoption['gobacklink'] = 'main';
  171. else
  172. $nmoption['gobacklink'] = true;
  173. }
  174. # tag separator
  175. $nmoption['tagseparator'] = ' ';
  176. # author
  177. $nmoption['showauthor'] = false;
  178. $nmoption['defaultauthor'] = '';
  179. # images
  180. if ( $NMSETTING['images'] == 'N'
  181. || ($pagetype == 'single' && $NMSETTING['images'] == 'P')
  182. || ($pagetype != 'main' && $NMSETTING['images'] == 'M') ) {
  183. $nmoption['showimages'] = false;
  184. } else {
  185. $nmoption['showimages'] = true;
  186. }
  187. $nmoption['imagewidth'] = intval($NMSETTING['imagewidth']);
  188. $nmoption['imageheight'] = intval($NMSETTING['imageheight']);
  189. $nmoption['imagecrop'] = ($NMSETTING['imagecrop'] == '1');
  190. $nmoption['imagealt'] = ($NMSETTING['imagealt'] == '1');
  191. $nmoption['imagelink'] = ($pagetype != 'single' && $NMSETTING['imagelink'] == '1');
  192. $nmoption['imagetitle'] = false;
  193. $nmoption['imageexternal'] = false;
  194. $nmoption['imagedefault'] = '';
  195. $nmoption['imagesizeattr'] = false;
  196. # custom settings
  197. if ($NMSETTING['enablecustomsettings'] == '1') {
  198. # extract settings
  199. foreach(preg_split('~\R~', $NMSETTING['customsettings']) as $line) {
  200. $line = trim($line);
  201. if ($line && strpos($line,'#') !== 0 && strpos($line,'//') !== 0) { // exclude empty and commented lines
  202. $arr = explode(' ',preg_replace("/[[:blank:]]+/"," ",$line));
  203. if (count($arr) > 1) {
  204. if (in_array($arr[0], array('main','single','archive','tag','search')))
  205. $customsettings[$arr[0]][$arr[1]] = implode(' ',array_slice($arr,2));
  206. else
  207. $customsettings['default'][$arr[0]] = implode(' ',array_slice($arr,1));
  208. }
  209. }
  210. }
  211. # process settings and strings
  212. foreach(array('default', $pagetype) as $type) {
  213. if (isset($customsettings[$type])) {
  214. foreach($customsettings[$type] as $key=>$value) {
  215. if (substr($value,0,1) == '"' || substr($value,0,1) == "'") $value = substr($value,1,strlen($value)-2);
  216. if (strtoupper($key) == $key) {
  217. # language string
  218. nm_set_text($key, $value);
  219. } else {
  220. # setting
  221. $nmoption[strtolower($key)] = $value;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. # html tags
  228. $nmoption['markuppost'] = isset($nmoption['markuppost']) ? str_replace(array('<','>'),'',$nmoption['markuppost']) : 'div';
  229. $nmoption['markuptitle'] = isset($nmoption['markuptitle']) ? str_replace(array('<','>'),'',$nmoption['markuptitle']) : 'h3';
  230. # fields
  231. if (isset($nmoption['showfields'])) {
  232. $nmoption['fields'] = explode(' ',preg_replace('/ +/', ' ',trim(str_replace(',',' ',$nmoption['showfields']))));
  233. } else {
  234. $nmoption['fields'] = array('title','date','author','image','content','tags');
  235. }
  236. # imagesize shorthand
  237. if (isset($nmoption['imagesize'])) {
  238. if ($nmoption['imagesize'] == 0 || $nmoption['imagesize'] == 'full') {
  239. $nmoption['imagewidth'] = 0;
  240. $nmoption['imageheight'] = 0;
  241. $nmoption['imagecrop'] = 0;
  242. } else {
  243. $imageparams = explode(' ',preg_replace('/ +/', ' ',trim(str_replace(',',' ',$nmoption['imagesize']))));
  244. $nmoption['imagewidth'] = isset($imageparams[0]) ? $imageparams[0] : 0;
  245. $nmoption['imageheight'] = isset($imageparams[1]) ? $imageparams[1] : 0;
  246. $nmoption['imagecrop'] = isset($imageparams[2]) ? $imageparams[2] : 0;
  247. }
  248. }
  249. # custom excerpt length
  250. if (isset($nmoption['excerptlength'])) {
  251. global $NMEXCERPTLENGTH;
  252. $NMEXCERPTLENGTH = $nmoption['excerptlength']; // workaround(*)
  253. }
  254. # more
  255. if (!isset($nmoption['more'])) $nmoption['more'] = false;
  256. # readmore
  257. if (!isset($nmoption['readmore']))
  258. $nmoption['readmore'] = false;
  259. else // custom setting - anything beginning with 'a' (all, Always, ...)
  260. if (strtolower($nmoption['readmore'][0]) == 'a')
  261. $nmoption['readmore'] = 'a';
  262. # tag pagination
  263. if (!isset($nmoption['tagpagination'])) {
  264. $nmoption['tagpagination'] = false;
  265. } else { // anything beginning with 'd' (Default, Dynamic...) or 'f' (Fancy, Folder...)
  266. $nmoption['tagpagination'] = strtolower($nmoption['tagpagination'][0]);
  267. if (!in_array($nmoption['tagpagination'], array('d','f')))
  268. $nmoption['tagpagination'] = false;
  269. }
  270. }
  271. /*******************************************************
  272. * @function nm_show_post
  273. * @param $slug post slug
  274. * @param $showexcerpt - if TRUE, print only a short summary
  275. * @param $filter - if true, apply content filter
  276. * @param $single post page?
  277. * @action show the requested post on front-end news page, as defined by $nmoption values
  278. * @return true if post exists
  279. */
  280. function nm_show_post($slug, $showexcerpt=false, $filter=true, $single=false) {
  281. global $nmoption, $nmdata;
  282. $file = NMPOSTPATH.$slug.'.xml';
  283. if (dirname(realpath($file)) == realpath(NMPOSTPATH)) // no path traversal
  284. $post = @getXML($file);
  285. if (!empty($post) && $post->private != 'Y') {
  286. $url = nm_get_url('post') . $slug;
  287. $title = stripslashes($post->title);
  288. $date = nm_get_date(i18n_r('news_manager/DATE_FORMAT'), strtotime($post->date));
  289. $content = strip_decode($post->content);
  290. $image = stripslashes($post->image);
  291. $tags = !empty($post->tags) ? explode(',', nm_lowercase_tags(strip_decode($post->tags))) : array();
  292. # save post data?
  293. $nmdata = ($single) ? compact('slug', 'url', 'title', 'content', 'image', 'tags') : array();
  294. if ($filter) ob_start();
  295. echo ' <',$nmoption['markuppost'],' class="nm_post';
  296. if ($single) echo ' nm_post_single';
  297. echo '">',PHP_EOL;
  298. foreach ($nmoption['fields'] as $field) {
  299. switch($field) {
  300. case 'title':
  301. echo ' <',$nmoption['markuptitle'],' class="nm_post_title">';
  302. if ($nmoption['titlelink'])
  303. echo '<a href="',$url,'">',$title,'</a>';
  304. else
  305. echo $title;
  306. echo '</',$nmoption['markuptitle'],'>',PHP_EOL;
  307. break;
  308. case 'date':
  309. echo ' <p class="nm_post_date">',i18n_r('news_manager/PUBLISHED'),' ',$date,'</p>',PHP_EOL;
  310. break;
  311. case 'content':
  312. echo ' <div class="nm_post_content">';
  313. if ($single) {
  314. echo $content;
  315. } else {
  316. $slice = '';
  317. $readmore = $nmoption['readmore'];
  318. if ($nmoption['more']) {
  319. $morepos = strpos($content, '<hr');
  320. if ($morepos !== false) {
  321. $slice = substr($content, 0, $morepos);
  322. if ($readmore)
  323. $slice .= ' <p class="nm_readmore"><a href="'.$url.'">'.i18n_r('news_manager/READ_MORE').'</a></p>'.PHP_EOL;
  324. }
  325. }
  326. if ($slice) {
  327. echo $slice;
  328. } else {
  329. if ($showexcerpt) {
  330. if (!$readmore)
  331. echo nm_create_excerpt($content);
  332. elseif ($readmore === 'a')
  333. echo nm_create_excerpt($content, $url, true);
  334. else
  335. echo nm_create_excerpt($content, $url);
  336. } else {
  337. echo $content;
  338. if ($readmore === 'a')
  339. echo ' <p class="nm_readmore"><a href="',$url,'">',i18n_r('news_manager/READ_MORE'),'</a></p>',PHP_EOL;
  340. }
  341. }
  342. }
  343. echo ' </div>',PHP_EOL;
  344. break;
  345. case 'tags':
  346. if ($tags) {
  347. echo ' <p class="nm_post_meta"><b>' . i18n_r('news_manager/TAGS') . ':</b> ';
  348. $sep = '';
  349. foreach ($tags as $tag)
  350. if (substr($tag, 0, 1) != '_') {
  351. echo $sep,'<a href="',nm_get_url('tag').rawurlencode($tag),'">',$tag,'</a>';
  352. if ($sep == '') $sep = $nmoption['tagseparator'];
  353. }
  354. echo '</p>',PHP_EOL;
  355. }
  356. break;
  357. case 'image':
  358. $imageurl = $nmoption['showimages'] ? nm_get_image_url($image) : false;
  359. if ($imageurl) {
  360. $str = '';
  361. if (isset($nmoption['imageclass']))
  362. $str .= ' class="'.$nmoption['imageclass'].'"';
  363. if ($nmoption['imagesizeattr'] && $nmoption['imagewidth'] && $nmoption['imageheight'])
  364. $str .= ' width="'.$nmoption['imagewidth'].'" height="'.$nmoption['imageheight'].'"';
  365. $str .= $nmoption['imagealt'] ? ' alt="'.htmlspecialchars($title, ENT_COMPAT).'"' : ' alt=""';
  366. $str .= $nmoption['imagetitle'] ? ' title="'.htmlspecialchars($title, ENT_COMPAT).'"' : '';
  367. $str = '<img src="'.htmlspecialchars($imageurl).'"'.$str.' />';
  368. if ($nmoption['imagelink'])
  369. $str = '<a href="'.$url.'">'.$str.'</a>';
  370. echo ' <div class="nm_post_image">',$str,'</div>',PHP_EOL;
  371. }
  372. break;
  373. case 'author':
  374. if ($nmoption['showauthor']) {
  375. $author = stripslashes($post->author);
  376. if (empty($author) && $nmoption['defaultauthor'])
  377. $author = $nmoption['defaultauthor'];
  378. if (!empty($author))
  379. echo ' <p class="nm_post_author">'.i18n_r('news_manager/AUTHOR').' <em>'.$author.'</em></p>'.PHP_EOL;
  380. }
  381. break;
  382. }
  383. }
  384. if (isset($nmoption['componentbottompost'])) {
  385. get_component($nmoption['componentbottompost']);
  386. echo PHP_EOL;
  387. }
  388. if ($single) {
  389. # show "go back" link?
  390. if ($nmoption['gobacklink']) {
  391. $goback = ($nmoption['gobacklink'] === 'main') ? nm_get_url() : 'javascript:history.back()';
  392. echo ' <p class="nm_post_back"><a href="'.$goback.'">';
  393. i18n('news_manager/GO_BACK');
  394. echo '</a></p>',PHP_EOL;
  395. }
  396. }
  397. echo ' </',$nmoption['markuppost'],'>',PHP_EOL;
  398. if (isset($nmoption['componentafterpost'])) {
  399. get_component($nmoption['componentafterpost']);
  400. echo PHP_EOL;
  401. }
  402. if ($filter) echo nm_ob_get_content(true);
  403. return true;
  404. } else {
  405. echo '<p>' . i18n_r('news_manager/NOT_EXIST') . '</p>',PHP_EOL;
  406. return false;
  407. }
  408. }
  409. /*******************************************************
  410. * @function nm_show_navigation
  411. * @param $index - current page index
  412. * @param $total - total number of subpages
  413. * @param $tag - tag to filter by (optional)
  414. * @action provides links to navigate between subpages in main news or tag page
  415. */
  416. function nm_show_navigation($index, $total, $tag=null) {
  417. $p1 = intval(NMFIRSTPAGE);
  418. if (!$tag) {
  419. $first = nm_get_url();
  420. $page = nm_get_url('page');
  421. } else {
  422. $first = nm_get_url('tag').rawurlencode($tag);
  423. if (nm_get_option('tagpagination') == 'f')
  424. $page = $first.'/'.NMPARAMPAGE.'/';
  425. else
  426. $page = $first.'&amp;'.NMPARAMPAGE.'=';
  427. }
  428. echo '<div class="nm_page_nav">';
  429. if (!nm_get_option('navoldnew',false)) {
  430. $prevnext = nm_get_option('navprevnext', '1');
  431. if (strtolower($prevnext[0]) == 'a') { // navPrevNext a[lways]
  432. $noPrev = '<span class="previous disabled">'.i18n_r('news_manager/PREV_TEXT').'</span>';
  433. $noNext = ' <span class="next disabled">'.i18n_r('news_manager/NEXT_TEXT').'</span>';
  434. } else {
  435. $noPrev = '';
  436. $noNext = '';
  437. }
  438. if ($prevnext && $index > $p1) {
  439. echo '<span class="previous"><a href="';
  440. echo $index > $p1+1 ? $page.($index-1) : $first;
  441. echo '" title="',i18n_r('news_manager/PREV_TITLE'),'">',i18n_r('news_manager/PREV_TEXT'),'</a></span>';
  442. } else {
  443. echo $noPrev;
  444. }
  445. if (nm_get_option('navnumber',true)) {
  446. for ($i = 0; $i < $total; $i++) {
  447. if ($i+$p1 == $index) {
  448. echo ' <span class="current">',$i+1,'</span>';
  449. } else {
  450. echo ' <span><a href="';
  451. echo $i == 0 ? $first : $page.($i+$p1);
  452. echo '">',$i+1,'</a></span>';
  453. }
  454. }
  455. }
  456. if ($prevnext && $index < $total-1+$p1) {
  457. echo ' <span class="next"><a href="',$page.($index+1);
  458. echo '" title="',i18n_r('news_manager/NEXT_TITLE'),'">',i18n_r('news_manager/NEXT_TEXT'),'</a></span>';
  459. } else {
  460. echo $noNext;
  461. }
  462. } else {
  463. # Older/Newer navigation
  464. if ($index < $total-1+$p1) {
  465. ?>
  466. <div class="left">
  467. <a href="<?php echo $page.($index+1); ?>">
  468. <?php i18n('news_manager/OLDER_POSTS'); ?>
  469. </a>
  470. </div>
  471. <?php
  472. }
  473. if ($index > $p1) {
  474. ?>
  475. <div class="right">
  476. <a href="<?php echo ($index > $p1+1) ? $page.($index-1) : $first ?>">
  477. <?php i18n('news_manager/NEWER_POSTS'); ?>
  478. </a>
  479. </div>
  480. <?php
  481. }
  482. }
  483. echo '</div>';
  484. }
  485. /*******************************************************
  486. * @function nm_post_title
  487. * @param $before Text to place before the title. Defaults to ''
  488. * @param $after Text to place after the title. Defaults to ''
  489. * @param $echo Display (true) or return (false)
  490. * @action Display or return the post title. Returns false if not on single post page
  491. * @since 2.3
  492. */
  493. function nm_post_title($before='', $after='', $echo=true) {
  494. global $nmdata;
  495. if (isset($nmdata['title']) && $nmdata['title']) {
  496. $title = $before.$nmdata['title'].$after;
  497. if ($echo) echo $title;
  498. return $title;
  499. } else {
  500. return false;
  501. }
  502. }
  503. /*******************************************************
  504. * @function nm_post_slug
  505. * @param $echo Display (true) or return (false)
  506. * @action Display or return the post id (slug)
  507. * @return slug or false if not on single post page
  508. * @since 3.0
  509. */
  510. function nm_post_slug($echo=true) {
  511. global $nmdata;
  512. if (isset($nmdata['slug']) && $nmdata['slug']) {
  513. $slug = $nmdata['slug'];
  514. if ($echo) echo $slug;
  515. return $slug;
  516. } else {
  517. return false;
  518. }
  519. }
  520. /*******************************************************
  521. * @function nm_post_url
  522. * @param $echo Display (true) or return (false)
  523. * @action Display or return the post URL
  524. * @return URL or false if not on single post page
  525. * @since 3.0
  526. */
  527. function nm_post_url($echo=true) {
  528. global $nmdata;
  529. if (isset($nmdata['url']) && $nmdata['url']) {
  530. $url = $nmdata['url'];
  531. if ($echo) echo $url;
  532. return $url;
  533. } else {
  534. return false;
  535. }
  536. }
  537. /*******************************************************
  538. * @function nm_post_excerpt
  539. * @param $len Length or null for default length (settings)
  540. * @param $ellipsis Custom string for the ellipsis or null for default
  541. * @param $echo Display (true) or return (false)
  542. * @action Display or return a post excerpt
  543. * @return excerpt or empty string
  544. * @since 3.0
  545. */
  546. function nm_post_excerpt($len=null, $ellipsis=null, $echo=true) {
  547. global $nmdata, $NMEXCERPTLENGTH, $nmoption;
  548. if (isset($nmdata['content']) && $nmdata['content']) {
  549. if (!$len) $len = isset($nmoption['excerptlength']) ? $nmoption['excerptlength'] : $NMEXCERPTLENGTH; // workaround(*)
  550. if (!$ellipsis && $ellipsis !== '') $ellipsis = i18n_r('news_manager/ELLIPSIS');
  551. $excerpt = nm_make_excerpt($nmdata['content'], $len, $ellipsis);
  552. if ($echo) echo $excerpt;
  553. return $excerpt;
  554. } else {
  555. return '';
  556. }
  557. }
  558. /*******************************************************
  559. * @function nm_post_image_url
  560. * @param $width or null for default width (settings)
  561. * @param $height or null for default height (settings)
  562. * @param $crop 0, 1, false or true, or null for default crop option (settings)
  563. * @param $default URL or filename of image if post has no image
  564. * @param $echo Display (true) or return (false)
  565. * @action Display or return post image URL
  566. * @return image URL or empty string
  567. * @since 3.0
  568. */
  569. function nm_post_image_url($width=null, $height=null, $crop=null, $default=null, $echo=true) {
  570. global $nmdata;
  571. if (isset($nmdata['image']) && $nmdata['image']) {
  572. $url = htmlspecialchars(nm_get_image_url($nmdata['image'], $width, $height, $crop, $default));
  573. if ($echo) echo $url;
  574. return $url;
  575. } else {
  576. return '';
  577. }
  578. }
  579. /***
  580. frontend functions, since 3.0
  581. @todo: descriptions
  582. ***/
  583. // conditionals
  584. function nm_is_site() {
  585. global $nmpagetype;
  586. return in_array('site', $nmpagetype);
  587. }
  588. function nm_is_single() {
  589. global $nmpagetype;
  590. return in_array('single', $nmpagetype);
  591. }
  592. function nm_is_main() {
  593. global $nmpagetype;
  594. return in_array('main', $nmpagetype);
  595. }
  596. function nm_is_tag($tag=null) {
  597. global $nmpagetype;
  598. if (in_array('tag', $nmpagetype)) {
  599. if (!$tag)
  600. return true;
  601. else
  602. return (isset($_GET[NMPARAMTAG]) && $tag == rawurldecode($_GET[NMPARAMTAG]));
  603. }
  604. }
  605. function nm_is_archive() {
  606. global $nmpagetype;
  607. return in_array('archive', $nmpagetype);
  608. }
  609. function nm_is_search() {
  610. global $nmpagetype;
  611. return in_array('search', $nmpagetype);
  612. }
  613. function nm_is_home() {
  614. global $nmpagetype;
  615. return in_array('home', $nmpagetype);
  616. }
  617. function nm_post_has_image() {
  618. global $nmdata;
  619. return (isset($nmdata['image']) && $nmdata['image']);
  620. }
  621. // check if single post has any tag or a certain tag
  622. function nm_post_has_tag($tag=null) {
  623. global $nmdata;
  624. if ($nmdata) {
  625. if (!isset($tag) && $nmdata['tags'])
  626. return true;
  627. elseif (in_array($tag, $nmdata['tags']))
  628. return true;
  629. }
  630. return false;
  631. }
  632. // set general option
  633. function nm_set_option($option, $value=true) {
  634. global $nmoption;
  635. if ($option) $nmoption[strtolower($option)] = $value;
  636. }
  637. // get option value, return $default if not defined
  638. function nm_get_option($option, $default=false) {
  639. global $nmoption;
  640. if ($option) {
  641. $option = strtolower($option);
  642. if (isset($nmoption[$option]))
  643. return $nmoption[$option];
  644. else
  645. return $default;
  646. }
  647. }
  648. // images
  649. function nm_set_image_size($width=null, $height=null, $crop=false) {
  650. global $nmoption;
  651. $nmoption['imagewidth'] = $width;
  652. $nmoption['imageheight'] = $height;
  653. $nmoption['imagecrop'] = $crop;
  654. }
  655. // custom text/language strings
  656. function nm_set_text($i18nkey=null, $i18nvalue=null) {
  657. global $i18n;
  658. if ($i18nkey && $i18nvalue)
  659. $i18n['news_manager/'.$i18nkey] = $i18nvalue;
  660. }
  661. // patch for <title> tag
  662. function nm_update_page_title() {
  663. if (!nm_is_single() || !nm_get_option('titletag',true) || function_exists('nmt_set_gstitle')) {
  664. return;
  665. } else {
  666. global $title, $nmpagetitle;
  667. $nmpagetitle = $title;
  668. $title = nm_post_title('',' - '.$title,false);
  669. }
  670. }
  671. // restore original title - <title> tag patch
  672. function nm_restore_page_title() {
  673. if (!nm_is_single() || !nm_get_option('titletag',true) || function_exists('nmt_set_gstitle')) {
  674. return;
  675. } else {
  676. global $title, $nmpagetitle;
  677. $title = $nmpagetitle;
  678. }
  679. }
  680. // get output buffer, optionally apply content filter
  681. function nm_ob_get_content($filter=true) {
  682. $output = ob_get_contents();
  683. ob_end_clean();
  684. if ($filter) {
  685. return exec_filter('content', $output);
  686. } else {
  687. return $output;
  688. }
  689. }
  690. ?>