diff --git a/newznab/controllers/Content.php b/newznab/controllers/Content.php new file mode 100644 index 000000000..62ca0cf99 --- /dev/null +++ b/newznab/controllers/Content.php @@ -0,0 +1,18 @@ +pdo = ($options['Settings'] instanceof Settings ? $options['Settings'] : new Settings()); } - /** - * Validate a content row before insert/update. - */ - public function validate($content) - { - if (substr($content["url"],0,1) != '/') - { - $content["url"] = "/".$content["url"]; - } - - if (substr($content["url"], strlen($content["url"]) - 1) != '/') - { - $content["url"] = $content["url"]."/"; - } - - return $content; - } - - /** - * Delete a content row. - */ - public function delete($id) - { - return $this->pdo->queryExec(sprintf("DELETE from content where id=%d", $id)); - } - - /** - * Update a content row. - */ - public function update($content) - { - $content = $this->validate($content); - $this->pdo->queryExec(sprintf("update content set role=%d, title = %s , url = %s , body = %s , metadescription = %s , metakeywords = %s , contenttype = %d , showinmenu = %d , status = %d , ordinal = %d where id = %d ", $content["role"], $this->pdo->escapeString($content["title"]), $this->pdo->escapeString($content["url"]), $this->pdo->escapeString($content["body"]), $this->pdo->escapeString($content["metadescription"]), $this->pdo->escapeString($content["metakeywords"]), $content["contenttype"], $content["showinmenu"], $content["status"], $content["ordinal"], $content["id"] )); - } - - /** - * Add a content row. - */ - public function add($content) - { - - $content = $this->validate($content); - - return $this->pdo->queryInsert(sprintf("insert into content (role, title, url, body, metadescription, metakeywords, contenttype, showinmenu, status, ordinal ) values (%d, %s, %s, %s, %s, %s, %d, %d, %d, %d )", $content["role"], $this->pdo->escapeString($content["title"]), $this->pdo->escapeString($content["url"]), $this->pdo->escapeString($content["body"]), $this->pdo->escapeString($content["metadescription"]), $this->pdo->escapeString($content["metakeywords"]), $content["contenttype"], $content["showinmenu"], $content["status"], $content["ordinal"] )); - } - - /** - * Get all active content rows. - */ public function get() { - return $this->pdo->query(sprintf("select * from content where status = 1 order by contenttype, coalesce(ordinal, 1000000)")); + $arr = []; + $rows = $this->data_get(); + if ($rows === false) { + return false; + } + + foreach ($rows as $row) { + $arr[] = $this->row2Object($row); + } + + return $arr; } - /** - * Get all content rows. - */ public function getAll() { - return $this->pdo->query(sprintf("select * from content order by contenttype, coalesce(ordinal, 1000000)")); + $arr = []; + $rows = $this->data_getAll(); + if ($rows === false) { + return false; + } + + foreach ($rows as $row) { + $arr[] = $this->row2Object($row); + } + + return $arr; } /** - * Get a content row by its id. + * Convert get all but from to object. + * + * @return array|bool */ - public function getByID($id, $role) + public function getAllButFront() { - if ($role == Users::ROLE_ADMIN) - $role = ""; - else - $role = sprintf("and (role=%d or role=0)", $role); + $arr = []; + $rows = $this->data_getAllButFront(); + if ($rows === false) { + return false; + } - return $this->pdo->queryOneRow(sprintf("select * from content where id = %d %s", $id, $role)); + foreach ($rows as $row) { + $arr[] = $this->row2Object($row); + } + + return $arr; } - /** - * Get the index page. - */ - public function getIndex() + public function getFrontPage() { - return $this->pdo->queryOneRow(sprintf("select * from content where status=1 and contenttype = %d ", Contents::TYPEINDEX), true); + $arr = []; + $rows = $this->data_getFrontPage(); + if ($rows === false) { + return false; + } + + foreach ($rows as $row) { + $arr[] = $this->row2Object($row); + } + + return $arr; } public function getForMenuByTypeAndRole($id, $role) @@ -123,14 +107,61 @@ class Contents return $arr; } - public function data_getForMenuByTypeAndRole($id, $role) + public function getIndex() { - if ($role == Users::ROLE_ADMIN) { - $role = ""; - } else { - $role = sprintf("AND (role = %d OR role = 0)", $role); + $row = $this->data_getIndex(); + if ($row === false) { + return false; } - return $this->pdo->query(sprintf("SELECT * FROM content WHERE showinmenu = 1 AND status = 1 AND contenttype = %d %s ", $id, $role)); + + return $this->row2Object($row); + } + + public function getByID($id, $role) + { + $row = $this->data_getByID($id, $role); + if ($row === false) { + return false; + } + + return $this->row2Object($row); + } + + public function validate($content) + { + if (substr($content->url, 0, 1) != '/') { + $content->url = "/" . $content->url; + } + + if (substr($content->url, strlen($content->url) - 1) != '/') { + $content->url = $content->url . "/"; + } + + return $content; + } + + public function add($form) + { + $content = $this->row2Object($form); + $content = $this->validate($content); + if ($content->ordinal == 1) { + $this->pdo->queryDirect("UPDATE content SET ordinal = ordinal + 1 WHERE ordinal > 0"); + } + return $this->data_add($content); + } + + public function delete($id) + { + return $this->pdo->queryExec(sprintf("DELETE FROM content WHERE id = %d", $id)); + } + + public function update($form) + { + $content = $this->row2Object($form); + $content = $this->validate($content); + $this->data_update($content); + + return $content; } public function row2Object($row, $prefix = "") @@ -154,4 +185,65 @@ class Contents $obj->role = $row[$prefix . "role"]; return $obj; } + + public function data_update($content) + { + return $this->pdo->queryExec(sprintf("UPDATE content SET role = %d, title = %s, url = %s, body = %s, metadescription = %s, metakeywords = %s, contenttype = %d, showinmenu = %d, status = %d, ordinal = %d WHERE id = %d", $content->role, $this->pdo->escapeString($content->title), $this->pdo->escapeString($content->url), $this->pdo->escapeString($content->body), $this->pdo->escapeString($content->metadescription), $this->pdo->escapeString($content->metakeywords), $content->contenttype, $content->showinmenu, $content->status, $content->ordinal, $content->id)); + } + + public function data_add($content) + { + return $this->pdo->queryInsert(sprintf("INSERT INTO content (role, title, url, body, metadescription, metakeywords, contenttype, showinmenu, status, ordinal) values (%d, %s, %s, %s, %s, %s, %d, %d, %d, %d )", $content->role, $this->pdo->escapeString($content->title), $this->pdo->escapeString($content->url), $this->pdo->escapeString($content->body), $this->pdo->escapeString($content->metadescription), $this->pdo->escapeString($content->metakeywords), $content->contenttype, $content->showinmenu, $content->status, $content->ordinal)); + } + + public function data_get() + { + return $this->pdo->query(sprintf("SELECT * FROM content WHERE status = 1 ORDER BY contenttype, COALESCE(ordinal, 1000000)")); + } + + public function data_getAll() + { + return $this->pdo->query(sprintf("SELECT * FROM content ORDER BY contenttype, COALESCE(ordinal, 1000000)")); + } + + /** + * Get all but front page. + * + * @return array + */ + public function data_getAllButFront() + { + return $this->pdo->query(sprintf("SELECT * FROM content WHERE id != 1 ORDER BY contenttype, COALESCE(ordinal, 1000000)")); + } + + public function data_getByID($id, $role) + { + if ($role == Users::ROLE_ADMIN) { + $role = ""; + } else { + $role = sprintf("AND (role = %d OR role = 0)", $role); + } + + return $this->pdo->queryOneRow(sprintf("SELECT * FROM content WHERE id = %d %s", $id, $role)); + } + + public function data_getFrontPage() + { + return $this->pdo->query(sprintf("SELECT * FROM content WHERE status = 1 AND contenttype = %d ORDER BY ordinal ASC, COALESCE(ordinal, 1000000), id", Contents::TYPEINDEX)); + } + + public function data_getIndex() + { + return $this->pdo->queryOneRow(sprintf("SELECT * FROM content WHERE status = 1 AND contenttype = %d", Contents::TYPEINDEX)); + } + + public function data_getForMenuByTypeAndRole($id, $role) + { + if ($role == Users::ROLE_ADMIN) { + $role = ""; + } else { + $role = sprintf("AND (role = %d OR role = 0)", $role); + } + return $this->pdo->query(sprintf("SELECT * FROM content WHERE showinmenu = 1 AND status = 1 AND contenttype = %d %s ", $id, $role)); + } } \ No newline at end of file diff --git a/newznab/controllers/Menu.php b/newznab/controllers/Menu.php index db16b9108..6954b56b0 100644 --- a/newznab/controllers/Menu.php +++ b/newznab/controllers/Menu.php @@ -8,82 +8,65 @@ use newznab\db\Settings; class Menu { /** - * Get all menu rows for a role. + * @var \newznab\db\Settings */ + public $pdo; + + /** + * @param \newznab\db\Settings $settings + */ + public function __construct($settings = null) + { + $this->pdo = ($settings instanceof Settings ? $settings : new Settings()); + } + public function get($role, $serverurl) { - $db = new Settings(); - $guest = ""; - if ($role != Users::ROLE_GUEST) - $guest = sprintf(" and role != %d ", Users::ROLE_GUEST); + if ($role != Users::ROLE_GUEST) { + $guest = sprintf(" AND role != %d ", Users::ROLE_GUEST); + } - if ($role != Users::ROLE_ADMIN) - $guest .= sprintf(" and role != %d ", Users::ROLE_ADMIN); + if ($role != Users::ROLE_ADMIN) { + $guest .= sprintf(" AND role != %d ", Users::ROLE_ADMIN); + } - $sql = sprintf("select * from menu where role <= %d %s order by ordinal", $role, $guest); + $data = $this->pdo->query(sprintf("SELECT * FROM menu WHERE role <= %d %s ORDER BY ordinal", $role, $guest)); - $data = $db->query($sql); - - $ret = array(); - foreach ($data as $d) - { - if (!preg_match("/http/i", $d["href"])) - { - $d["href"] = $serverurl.$d["href"]; + $ret = []; + foreach ($data as $d) { + if (!preg_match("/http/i", $d["href"])) { + $d["href"] = $serverurl . $d["href"]; $ret[] = $d; - } - else - { + } else { $ret[] = $d; } } return $ret; } - /** - * Get all menu rows. - */ public function getAll() { - $db = new newznab\db\Settings(); - return $db->query(sprintf("select * from menu order by role, ordinal")); + return $this->pdo->query("SELECT * FROM menu ORDER BY role, ordinal"); } - /** - * Get a menu row by its id. - */ public function getById($id) { - $db = new newznab\db\Settings(); - return $db->queryOneRow(sprintf("select * from menu where id = %d", $id)); + return $this->pdo->queryOneRow(sprintf("SELECT * FROM menu WHERE id = %d", $id)); } - /** - * Delete a menu row. - */ public function delete($id) { - $db = new newznab\db\Settings(); - return $db->queryExec(sprintf("DELETE from menu where id = %d", $id)); + return $this->pdo->queryExec(sprintf("DELETE FROM menu WHERE id = %d", $id)); } - /** - * Add a menu row. - */ public function add($menu) { - $db = new newznab\db\Settings(); - return $db->queryInsert(sprintf("INSERT INTO menu (href, title, tooltip, role, ordinal, menueval, newwindow ) - VALUES (%s, %s, %s, %d, %d, %s, %d) ", $db->escapeString($menu["href"]), $db->escapeString($menu["title"]), $db->escapeString($menu["tooltip"]), $menu["role"] , $menu["ordinal"], $db->escapeString($menu["menueval"]), $menu["newwindow"] )); + return $this->pdo->queryInsert(sprintf("INSERT INTO menu (href, title, tooltip, role, ordinal, menueval, newwindow ) VALUES (%s, %s, %s, %d, %d, %s, %d)", $this->pdo->escapeString($menu["href"]), $this->pdo->escapeString($menu["title"]), $this->pdo->escapeString($menu["tooltip"]), $menu["role"], $menu["ordinal"], $this->pdo->escapeString($menu["menueval"]), $menu["newwindow"])); } - /** - * Update a menu row. - */ public function update($menu) { - $db = new newznab\db\Settings(); - return $db->queryExec(sprintf("update menu set href = %s, title = %s, tooltip = %s, role = %d, ordinal = %d, menueval = %s, newwindow=%d where id = %d ", $db->escapeString($menu["href"]), $db->escapeString($menu["title"]), $db->escapeString($menu["tooltip"]), $menu["role"] , $menu["ordinal"], $db->escapeString($menu["menueval"]), $menu["newwindow"], $menu["id"] )); + return $this->pdo->queryExec(sprintf("UPDATE menu SET href = %s, title = %s, tooltip = %s, role = %d, ordinal = %d, menueval = %s, newwindow = %d WHERE id = %d", $this->pdo->escapeString($menu["href"]), $this->pdo->escapeString($menu["title"]), $this->pdo->escapeString($menu["tooltip"]), $menu["role"], $menu["ordinal"], $this->pdo->escapeString($menu["menueval"]), $menu["newwindow"], $menu["id"])); } } \ No newline at end of file diff --git a/newznab/controllers/Page.php b/newznab/controllers/Page.php index 8509e580a..e517769af 100644 --- a/newznab/controllers/Page.php +++ b/newznab/controllers/Page.php @@ -18,17 +18,16 @@ class Page extends BasePage $content = new Contents(); $f = new Forum(); - $menu = new Menu(); + $menu = new Menu($this->settings); $this->smarty->assign('menulist',$menu->get($role, $this->serverurl)); - $this->smarty->assign('usefulcontentlist',$content->getForMenuByTypeAndRole(Contents::TYPEUSEFUL, $role)); - $this->smarty->assign('articlecontentlist',$content->getForMenuByTypeAndRole(Contents::TYPEARTICLE, $role)); + $this->smarty->assign('usefulcontentlist', $content->getForMenuByTypeAndRole(Contents::TYPEUSEFUL, $role)); + $this->smarty->assign('articlecontentlist', $content->getForMenuByTypeAndRole(Contents::TYPEARTICLE, $role)); if ($this->userdata != null) $this->smarty->assign('recentforumpostslist',$f->getRecentPosts($this->pdo->getSetting('showrecentforumposts'))); $this->smarty->assign('main_menu',$this->smarty->fetch('mainmenu.tpl')); $this->smarty->assign('useful_menu',$this->smarty->fetch('usefullinksmenu.tpl')); $this->smarty->assign('article_menu',$this->smarty->fetch('articlesmenu.tpl')); - $this->smarty->assign('recentposts_menu',$this->smarty->fetch('recentforumposts.tpl')); $category = new Category(); if ($this->userdata != null) diff --git a/www/pages/content.php b/www/pages/content.php index ad2836f5f..642d97272 100644 --- a/www/pages/content.php +++ b/www/pages/content.php @@ -1,28 +1,65 @@ $page->settings]); -$role=0; -if ($page->userdata != null) +$role = 0; +if ($page->userdata != null) { $role = $page->userdata["role"]; +} + +/* The role column in the content table values are : + * 0 = everyone + * 1 = logged in users + * 2 = admins + * + * The user role values are: + * 0 = guest + * 1 = user + * 2 = admin + * 3 = disabled + * 4 = moderator + * + * Admins and mods should be the only ones to see admin content. + */ +$page->smarty->assign('admin', (($role == 2 || $role == 4) ? 'true' : 'false')); $contentid = 0; -if (isset($_GET["id"])) +if (isset($_GET["id"])) { $contentid = $_GET["id"]; +} -if ($contentid == 0) - $content = $contents->getIndex(); -else - $content = $contents->getByID($contentid, $role); +$request = false; +if (isset($_REQUEST['page'])) { + $request = $_REQUEST['page']; +} -if ($content == null) +if ($contentid == 0 && $request == 'content') { + $content = $contents->getAllButFront(); + $page->smarty->assign('front', false); + $page->meta_title = 'Contents page'; + $page->meta_keywords = 'contents'; + $page->meta_description = 'This is the contents page.'; +} else if ($contentid != 0 && $request !== false) { + $content = array($contents->getByID($contentid, $role)); + $page->smarty->assign('front', false); + $page->meta_title = 'Contents page'; + $page->meta_keywords = 'contents'; + $page->meta_description = 'This is the contents page.'; +} else { + $content = $contents->getFrontPage(); + $index = $contents->getIndex(); + $page->smarty->assign('front', true); + $page->meta_title = $index->title; + $page->meta_keywords = $index->metakeywords; + $page->meta_description = $index->metadescription; +} + +if ($content == null) { $page->show404(); +} + +$page->smarty->assign('content', $content); -$page->smarty->assign('content',$content); -$page->meta_title = $content["title"]; -$page->meta_keywords = $content["metakeywords"]; -$page->meta_description = $content["metadescription"]; $page->content = $page->smarty->fetch('content.tpl'); $page->render(); - diff --git a/www/templates/nntmux/views/frontend/articlesmenu.tpl b/www/templates/nntmux/views/frontend/articlesmenu.tpl index bfee2a63e..6cbb528b6 100644 --- a/www/templates/nntmux/views/frontend/articlesmenu.tpl +++ b/www/templates/nntmux/views/frontend/articlesmenu.tpl @@ -1,13 +1,12 @@ {if $articlecontentlist|@count > 0} - -{/if} - - - + +{/if} \ No newline at end of file diff --git a/www/templates/nntmux/views/frontend/content.tpl b/www/templates/nntmux/views/frontend/content.tpl index fc0230593..435e887fd 100644 --- a/www/templates/nntmux/views/frontend/content.tpl +++ b/www/templates/nntmux/views/frontend/content.tpl @@ -1,4 +1,19 @@ - -

{$content.title}

- - {$content.body} +{if $loggedin=="false"} + {foreach from=$content item=c} + {if $c->role == 0}{$c->body}{/if} + {/foreach} +{else} + {foreach from=$content item=c} + {if $c->role == 0 || $c->role == 1 || ($c->role == 2 && $admin == 'true')} + {if $front == false} +

+ {$c->title} +

+ {/if} + {$c->body} + {/if} + {/foreach} + {if $front == true} + See more... + {/if} +{/if} \ No newline at end of file diff --git a/www/templates/nntmux/views/frontend/headermenu.tpl b/www/templates/nntmux/views/frontend/headermenu.tpl index 48b5ccbf8..1b3becb5d 100644 --- a/www/templates/nntmux/views/frontend/headermenu.tpl +++ b/www/templates/nntmux/views/frontend/headermenu.tpl @@ -1,126 +1,4 @@ + + \ No newline at end of file