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

/plugins/Blog/Blog.php

https://github.com/allanfreitas/CandyCMS
PHP | 552 lines | 374 code | 168 blank | 10 comment | 51 complexity | 140162f59b86ea4eec2826b769542d39 MD5 | raw file
  1. <?php
  2. /**
  3. * @plugin Blog
  4. * @description A simple blog for CandyCMS. Use {{blog}}
  5. * @author Cocoon Design
  6. * @authorURI http://www.wearecocoon.co.uk/
  7. * @copyright 2012 (C) Cocoon Design
  8. * @version 1.0
  9. * @since 0.1
  10. */
  11. class Blog {
  12. public static function install() {
  13. CandyDB::q("CREATE TABLE IF NOT EXISTS ". DB_PREFIX ."posts (post_id INT(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY(post_id), post_title VARCHAR(64) NOT NULL, UNIQUE KEY (`post_title`), post_body TEXT NOT NULL, permalink TEXT NOT NULL, cat_id VARCHAR(256) NOT NULL, post_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, status TEXT NOT NULL)");
  14. CandyDB::q("CREATE TABLE IF NOT EXISTS ". DB_PREFIX ."categories (cat_id INT(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (cat_id), cat_name VARCHAR(256), UNIQUE KEY (`cat_name`))");
  15. CandyDB::q("INSERT INTO ".DB_PREFIX."options (option_key, option_value) VALUES (:key, :value)", array('key' => 'disqus', 'value' => ''));
  16. CandyDB::q("INSERT INTO ".DB_PREFIX."options (option_key, option_value) VALUES (:key, :value)", array('key' => 'perpage', 'value' => 5));
  17. }
  18. public static function listCategories($cat_id){
  19. $cats = array();
  20. $cat_id = json_decode(stripslashes($cat_id));
  21. foreach ($cat_id as $value) {
  22. $cats[] = CandyDB::col("SELECT cat_name FROM ".DB_PREFIX."categories WHERE cat_id = :id", array('id' => $value));
  23. }
  24. $html = '';
  25. if (!empty($cats)) {
  26. $html .= '<ul class="category-list">';
  27. foreach ($cats as $value) {
  28. $catlink = str_replace(' ', '-', strtolower($value));
  29. $html .= '<li class="cat-'.strtolower($value).'"><a href="'.URL_PATH.self::getBlogPage().'/'.$catlink.'" title="'.$value.'">'.$value.'</a></li>';
  30. }
  31. $html .= '</ul>';
  32. }
  33. echo $html;
  34. }
  35. public static function adminNav(){
  36. return array('blog' => 'Blog');
  37. }
  38. public static function postsTable(){
  39. $posts = CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts ORDER BY post_id DESC');
  40. $html = '<table>';
  41. $html .= '<thead><tr><th>Post Title</th><th>Posted</th><th>Status</th><th></th><th></th></tr></thead>';
  42. foreach ($posts as $post) {
  43. $html .= '<tr>';
  44. $html .= '<td>'.$post->post_title.'</td>';
  45. $html .= '<td>'.date('d/m/Y H:i:s', strtotime($post->post_date)).'</td>';
  46. $html .= '<td>'.ucwords($post->status).'</td>';
  47. $html .= '<td><a href="dashboard.php?page=blog&edit='.$post->post_id.'" title="Edit Page">Edit</a></td>';
  48. $html .= '<td><a href="dashboard.php?page=blog&delete='.$post->post_id.'" title="'.$post->post_title.'" class="delete">[x]</a></td>';
  49. $html .= '</tr>';
  50. }
  51. $html .= '</table>';
  52. echo $html;
  53. }
  54. public static function catsTable(){
  55. $cats = CandyDB::results('SELECT * FROM '.DB_PREFIX.'categories ORDER BY cat_id DESC');
  56. $html = '<table id="catstable">';
  57. $html .= '<thead><tr><th>ID</th><th>Category Name</th><th></th><th></th></tr></thead>';
  58. foreach ($cats as $cat) {
  59. $html .= '<tr>';
  60. $html .= '<td>'.$cat->cat_id.'</td>';
  61. $html .= '<td>'.$cat->cat_name.'</td>';
  62. $html .= '<td><!--Edit--></td>';
  63. $html .= '<td><a href="#'.$cat->cat_id.'" title="'.$cat->cat_name.'" class="delcat">[x]</a></td>';
  64. $html .= '</tr>';
  65. }
  66. $html .= '</table>';
  67. echo $html;
  68. }
  69. public static function addPost($post_title, $post_body, $categories, $permalink, $status){
  70. $categories = json_encode($categories);
  71. $cats = addslashes($categories);
  72. $title = $post_title;
  73. $body = $post_body;
  74. CandyDB::q("INSERT INTO ". DB_PREFIX ."posts (post_title, post_body, cat_id, permalink, status) VALUES (:title, :body, :cats, :permalink, :status)",
  75. array(
  76. 'title' => $title,
  77. 'body' => $body,
  78. 'cats' => $cats,
  79. 'permalink' => $permalink,
  80. 'status' => $status
  81. )
  82. );
  83. }
  84. public static function editPost($post_title, $post_body, $categories, $permalink, $pid, $status=null){
  85. $cats = addslashes(json_encode($categories));
  86. $title = $post_title;
  87. $body = $post_body;
  88. if (is_null($status)) {
  89. CandyDB::q("UPDATE ".DB_PREFIX."posts SET post_title = :title, post_body = :body, cat_id = :cats, permalink = :permalink WHERE post_id = :id",
  90. array(
  91. 'title' => $title,
  92. 'body' => $body,
  93. 'cats' => $cats,
  94. 'permalink' => $permalink,
  95. 'id' => $pid
  96. )
  97. );
  98. } else {
  99. CandyDB::q("UPDATE ".DB_PREFIX."posts SET post_title = :title, post_body = :body, cat_id = :cats, permalink = :permalink, status = :status WHERE post_id = :id",
  100. array(
  101. 'title' => $title,
  102. 'body' => $body,
  103. 'cats' => $cats,
  104. 'permalink' => $permalink,
  105. 'status' => $status,
  106. 'id' => $pid
  107. )
  108. );
  109. }
  110. }
  111. public static function deletePost($id){
  112. CandyDB::q('DELETE FROM '. DB_PREFIX .'posts WHERE post_id = :id', compact('id'));
  113. }
  114. public static function postDate($id, $format = "d/m/Y H:i:s"){
  115. $dbdate = CandyDB::col("SELECT post_date FROM ". DB_PREFIX ."posts WHERE post_id = :id", compact('id'));
  116. echo date($format, strtotime($dbdate));
  117. }
  118. public static function addShorttag(){
  119. $theme = Candy::Options('theme');
  120. ob_start();
  121. include 'frontend.php';
  122. $include = ob_get_clean();
  123. ob_start();
  124. $sidebar = THEME_PATH.$theme.'/blog/sidebar.php';
  125. if (file_exists($sidebar)) {
  126. include($sidebar);
  127. } else {
  128. include('templates/sidebar.php');
  129. }
  130. $sidebar = ob_get_clean();
  131. return array('{{blog}}' => $include, '{{sidebar}}' => $sidebar);
  132. }
  133. public static function getPostUri($id){
  134. $permalink = CandyDB::col("SELECT permalink FROM ". DB_PREFIX ."posts WHERE post_id = :id", compact('id'));
  135. $cats = CandyDB::col("SELECT cat_id FROM ". DB_PREFIX ."posts WHERE post_id = :id", compact('id'));
  136. $cats = json_decode(stripslashes($cats));
  137. if (empty($cats)) {
  138. $cat = false;
  139. } else {
  140. $cat = CandyDB::col("SELECT cat_name FROM ". DB_PREFIX ."categories WHERE cat_id = :cat", array('cat' => $cats[0]));
  141. }
  142. $catname = ($cat == false) ? 'uncategorised' : str_replace(' ', '-', strtolower($cat));
  143. $uri = self::getBlogPage();
  144. return URL_PATH.$uri.'/'.$catname.'/'.$permalink;
  145. }
  146. public static function postUri($id){
  147. echo self::getPostUri($id);
  148. }
  149. public static function postExcerpt($id, $length = 200){
  150. $body = CandyDB::col("SELECT post_body FROM ". DB_PREFIX ."posts WHERE post_id = :id", compact('id'));
  151. if (strlen($body) >= 200) {
  152. echo substr($body, 0, $length).'&hellip;';
  153. } else {
  154. echo $body;
  155. }
  156. }
  157. public static function getCats(){
  158. return CandyDB::results("SELECT * FROM ". DB_PREFIX ."categories");
  159. }
  160. public static function adminCats($selected = false){
  161. $cats = self::getCats();
  162. $html = '<div id="blog-cats">';
  163. $html .= '<h3>Categories</h3>';
  164. $html .= '<ul>';
  165. if (!empty($cats)) {
  166. foreach ($cats as $cat) {
  167. if ($selected == false) {
  168. $html .= "<li>{$cat->cat_name}<input type='checkbox' value='{$cat->cat_id}' name='categories[]' /></li>";
  169. } else {
  170. $cats = json_decode(stripslashes($selected));
  171. if (is_array($cats) && in_array($cat->cat_id, $cats)) {
  172. $html .= "<li>{$cat->cat_name}<input type='checkbox' value='{$cat->cat_id}' name='categories[]' checked='checked' /></li>";
  173. } else {
  174. $html .= "<li>{$cat->cat_name}<input type='checkbox' value='{$cat->cat_id}' name='categories[]' /></li>";
  175. }
  176. }
  177. }
  178. } else {
  179. $html .= '<li>Add a category to begin</li>';
  180. }
  181. $html .= '</ul>';
  182. $html .= '<div><input type="text" name="addcat" placeholder="Category" id="newcat" /><a href="javascript:void(0);" id="addcat" class="button">Add +</a></div>';
  183. echo $html;
  184. }
  185. public static function adminHead(){
  186. return '<script type="text/javascript" src="'.PLUGIN_URL.'Blog/js/admin.jquery.js"></script>';
  187. }
  188. public static function ajax(){
  189. if (isset($_POST['catname'])) {
  190. CandyDB::q("INSERT INTO ".DB_PREFIX."categories (`cat_name`) VALUES (:name)", array('name' => $_POST['catname']));
  191. echo CandyDB::col("SELECT cat_id FROM ".DB_PREFIX."categories WHERE cat_name = :name", array('name' => $_POST['catname']));
  192. } elseif (isset($_POST['search'])) {
  193. $posts = searchBlog($_POST['q']['term']);
  194. echo json_encode($posts);
  195. } else {
  196. $id = $_POST['id'];
  197. CandyDB::q("DELETE FROM ".DB_PREFIX."categories WHERE cat_id = :id", compact('id'));
  198. }
  199. }
  200. public static function disqusAccount(){
  201. return CandyDB::col("SELECT option_value FROM ". DB_PREFIX ."options WHERE option_key = :key", array('key' => 'disqus'));
  202. }
  203. public static function commentForm(){
  204. $post = getBlogPost($_GET['post']);
  205. $url = Candy::Options('site_url');
  206. $html = '<div id="disqus_thread"></div>'."\n";
  207. $html .= '<script type="text/javascript">'."\n";
  208. $html .= "var disqus_shortname = '".self::disqusAccount()."';\n";
  209. if (!empty($post)) $html .= "var disqus_identifier = '".$post[0]->post_id."';\n";
  210. $html .= "var disqus_url = '$url".$_GET['page']."/".$_GET['category']."/".$_GET['post']."';\n";
  211. $html .= "(function() {\n";
  212. $html .= "var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;\n";
  213. $html .= "dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';\n";
  214. $html .= "(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);\n";
  215. $html .= "})();\n";
  216. $html .= "</script>";
  217. echo $html;
  218. }
  219. public static function adminSettings(){
  220. $limit = CandyDB::col("SELECT `option_value` FROM `".DB_PREFIX."options` WHERE `option_key` = :key", array('key' => 'perpage'));
  221. $disqus = self::disqusAccount();
  222. $html = "<h3>Blog Settings</h3>";
  223. $html .= "<ul>";
  224. $html .= "<li>";
  225. $html .= "<label>Disqus Account</label>";
  226. $html .= "<input type='text' name='disqus' value='$disqus'/>";
  227. $html .= "</li>";
  228. $html .= "<label>Posts Per Page</label>";
  229. $html .= "<input type='text' name='perpage' value='$limit'/>";
  230. $html .= "</li>";
  231. $html .= "</ul>";
  232. return $html;
  233. }
  234. public static function saveSettings(){
  235. $account = $_POST['disqus'];
  236. $limit = $_POST['perpage'];
  237. CandyDB::q('UPDATE '. DB_PREFIX .'options SET option_value = :value WHERE option_key = :key', array('value' => $account, 'key' => 'disqus'));
  238. CandyDB::q('UPDATE '. DB_PREFIX .'options SET option_value = :value WHERE option_key = :key', array('value' => $limit, 'key' => 'perpage'));
  239. }
  240. public static function nextLink($text = 'Next', $class = false){
  241. $site_url = Candy::Options('site_url');
  242. $count = CandyDB::col("SELECT COUNT(*) FROM `".DB_PREFIX."posts`");
  243. $limit = CandyDB::col("SELECT option_value FROM ".DB_PREFIX."options WHERE option_key = :key", array('key' => 'perpage'));
  244. if (isset($_GET['page'])) {
  245. $uri = $_GET['page'];
  246. } else {
  247. $uri = Candy::Options('homepage');
  248. }
  249. if (isset($_GET['category']) && is_numeric($_GET['category'])) {
  250. $offset = $_GET['category']+1;
  251. $offset = $offset*$limit;
  252. $offset = $offset-$limit;
  253. $posts = CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts ORDER BY post_id DESC LIMIT '.$limit.' OFFSET '.$offset);
  254. $page = $_GET['category']+1;
  255. if ($posts != false) {
  256. if ($class !=false) {
  257. echo "<a href='".$site_url."$uri/$page' class='$class'>$text</a>";
  258. } else {
  259. echo "<a href='".$site_url."$uri/$page'>$text</a>";
  260. }
  261. }
  262. } elseif ($count > $limit) {
  263. if ($class !=false) {
  264. echo "<a href='".$site_url."$uri/2' class='$class'>$text</a>";
  265. } else {
  266. echo "<a href='".$site_url."$uri/2'>$text</a>";
  267. }
  268. }
  269. }
  270. public static function prevLink($text = 'Prev', $class = false){
  271. $site_url = Candy::Options('site_url');
  272. if (isset($_GET['category']) && is_numeric($_GET['category'])) {
  273. $limit = CandyDB::col("SELECT option_value FROM ".DB_PREFIX."options WHERE option_key = :key", array('key' => 'perpage'));
  274. if (isset($_GET['page'])) {
  275. $uri = explode('/', $_SERVER['REQUEST_URI']);
  276. $uri = $uri[1];
  277. } else {
  278. $uri = Candy::Options('homepage');
  279. }
  280. if ($_GET['category'] == 2) {
  281. if ($class !=false) {
  282. echo "<a href='".$site_url.Blog::getBlogPage()."' class='$class'>$text</a>";
  283. } else {
  284. echo "<a href='".$site_url."'>$text</a>";
  285. }
  286. } else {
  287. $page = $_GET['category']-1;
  288. if ($class !=false) {
  289. echo "<a href='".$site_url."$uri/$page' class='$class'>$text</a>";
  290. } else {
  291. echo "<a href='".$site_url."$uri/$page'>$text</a>";
  292. }
  293. }
  294. }
  295. }
  296. public static function theCategories(){
  297. $cats = CandyDB::results('SELECT cat_name FROM '.DB_PREFIX.'categories ORDER BY cat_id DESC');
  298. $html = '';
  299. if (!empty($cats)) {
  300. $path = URL_PATH;
  301. $html .= '<ul>';
  302. foreach ($cats as $cat) {
  303. $html .= "<li><a href='".$path.self::getBlogPage()."/".str_replace(' ', '-', strtolower($cat->cat_name))."'>".$cat->cat_name."</a></li>";
  304. }
  305. $html .= '</ul>';
  306. }
  307. echo $html;
  308. }
  309. public static function getBlogPage() {
  310. return CandyDB::col('SELECT rewrite FROM '.DB_PREFIX.'pages WHERE page_body LIKE "%{{blog}}%"');
  311. }
  312. public static function getPostTitle($permalink){
  313. return CandyDB::col('SELECT post_title FROM '.DB_PREFIX.'posts WHERE permalink = :permalink', compact('permalink'));
  314. }
  315. }
  316. function listBlogPosts(){
  317. $limit = CandyDB::col("SELECT `option_value` FROM `".DB_PREFIX."options` WHERE `option_key` = :key", array('key' => 'perpage'));
  318. if (isset($_GET['category']) && is_numeric($_GET['category'])) {
  319. $page = $_GET['category'];
  320. $offset = $page*$limit;
  321. $offset = $offset-$limit;
  322. $results = CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts WHERE `status` = :status ORDER BY post_id DESC LIMIT '.$limit.' OFFSET '.$offset,
  323. array(
  324. 'status' => 'published'
  325. )
  326. );
  327. } else {
  328. $results = CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts WHERE `status` = :status ORDER BY post_id DESC LIMIT '.$limit,
  329. array(
  330. 'status' => 'published'
  331. )
  332. );
  333. }
  334. return $results;
  335. }
  336. function listCategoryPosts(){
  337. $category = str_replace('-', ' ', $_GET['category']);
  338. $catid = CandyDB::col('SELECT cat_id FROM '.DB_PREFIX.'categories WHERE cat_name = :cat', array('cat' => $category));
  339. $posts = CandyDB::results('SELECT post_id, cat_id FROM '.DB_PREFIX.'posts WHERE status = :status', array('status' => 'published'));
  340. $return = array();
  341. foreach ($posts as $post) {
  342. $ids = json_decode(stripslashes($post->cat_id));
  343. if (is_array($ids) && in_array($catid, $ids)) {
  344. $return[] = $post->post_id;
  345. }
  346. }
  347. $ids = join(',', $return);
  348. return CandyDB::results("SELECT * FROM ". DB_PREFIX ."posts WHERE `post_id` IN (".$ids.") ORDER BY post_title ASC");
  349. }
  350. function searchBlog($query){
  351. $search = addslashes($query);
  352. return CandyDB::results("SELECT * FROM ".DB_PREFIX."posts WHERE (`post_title` LIKE :title) OR (`post_body` LIKE :body)", array('title' => "%$search%", 'body' => "%$search%"));
  353. }
  354. function getBlogPost($uri){
  355. return CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts WHERE `permalink` = :permalink', array('permalink' => $uri));
  356. }
  357. function getBlogPostById($id){
  358. return CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts WHERE `post_id` = :id', compact('id'));
  359. }
  360. $uri = $_SERVER['REQUEST_URI'];
  361. if (!stristr($uri, 'cms-admin') && !stristr($uri, 'ajax.php')) {
  362. //The following will generate and rss feed in the root of the CandyCMS install
  363. $xml = '<?xml version="1.0" encoding="UTF-8"?>';
  364. $xml .= '<rss version="2.0">';
  365. $xml .= '<channel>';
  366. $xml .= '<title>'.Candy::Options('site_title').'</title>';
  367. $xml .= '<link>'.Candy::Options('site_url').'</link>';
  368. $xml .= '<description>'.Candy::Options('site_title').' Blog</description>';
  369. $xml .= '<pubDate>'.date('Y-m-d H:i:s').'</pubDate>';
  370. $posts = listBlogPosts();
  371. foreach ($posts as $post) {
  372. $xml .= '<item>';
  373. $xml .= '<title>'.$post->post_title.'</title>';
  374. $xml .= '<link>'.Blog::getPostUri($post->post_id).'</link>';
  375. $xml .= '<pubDate>'.$post->post_date.'</pubDate>';
  376. $xml .= '<description><![CDATA['.$post->post_body.']]></description>';
  377. $xml .= '</item>';
  378. }
  379. $xml .= '</channel>';
  380. $xml .= '</rss>';
  381. $fp = fopen(CMS_PATH.'rss.xml', 'w');
  382. fwrite($fp, $xml);
  383. fclose($fp);
  384. }