PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/Core.php

http://lightblog.googlecode.com/
PHP | 546 lines | 303 code | 54 blank | 189 comment | 49 complexity | 4ae5341be3b80851ffa2cb373c001b8d MD5 | raw file
  1. <?php
  2. /***********************************************
  3. LightBlog 0.9
  4. SQLite blogging platform
  5. Sources/Core.php
  6. Š2008-2012 The LightBlog Team. All
  7. rights reserved. Released under the
  8. GNU General Public License 3. For
  9. all licensing information, please
  10. see the LICENSE.txt document
  11. included in this distribution.
  12. ***********************************************/
  13. // Let's make sure that LightBlog has been installed.
  14. if(!file_exists(dirname(__FILE__). '/../config.php'))
  15. {
  16. // Is there an install.php file?
  17. if(file_exists(dirname(__FILE__). '/../install.php'))
  18. {
  19. // We will need to find the installer's relation to where we're
  20. // currently at.
  21. $filename = $_SERVER['SCRIPT_FILENAME'];
  22. $try_count = 0;
  23. while(strlen($filename) > 0 && !file_exists(($filename = dirname($filename)). '/install.php') && $try_count++ < 3);
  24. // Redirect to the installer, then.
  25. header('HTTP/1.1 307 Temporary Redirect');
  26. header('Location: '. str_repeat('../', $try_count). 'install.php');
  27. exit;
  28. }
  29. else
  30. {
  31. die('LightBlog Error: config.php file missing (no install.php).');
  32. }
  33. }
  34. else
  35. {
  36. // Include the config.php file, we need it!
  37. require(dirname(__FILE__). '/../config.php');
  38. }
  39. // Check to make sure that the database exists.
  40. if(file_exists(DBH))
  41. {
  42. $dbh = new PDO('sqlite2:'.DBH);
  43. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  44. $dbh->setAttribute(PDO::ATTR_TIMEOUT, 5);
  45. }
  46. else
  47. {
  48. $error_message = 'The database file does not exist';
  49. }
  50. if(!empty($error_message))
  51. {
  52. trigger_error($error_message, E_USER_ERROR);
  53. }
  54. // Include the extra user, database, and string functions
  55. require(ABSPATH. '/Sources/Errors.php');
  56. require(ABSPATH. '/Sources/CleanRequest.php');
  57. require(ABSPATH. '/Sources/FunctionReplacements.php');
  58. require(ABSPATH .'/Sources/DatabaseFunctions.php');
  59. require(ABSPATH. '/Sources/User.php');
  60. require(ABSPATH .'/Sources/UserFunctions.php');
  61. require(ABSPATH .'/Sources/StringFunctions.php');
  62. require(ABSPATH. '/Sources/Language.php');
  63. // Start up our session.
  64. session_start();
  65. // Now output buffering, too. With compression, if supported.
  66. /*if(function_exists('ob_gzhandler') && (get_bloginfo('disable_compression') === false || get_bloginfo('disable_compression') == 0))
  67. {
  68. ob_start('ob_gzhandler');
  69. }
  70. else
  71. {
  72. ob_start();
  73. }*/
  74. /*
  75. Function: LightyVersion
  76. Returns the installed version number of LightBlog.
  77. Parameters:
  78. output - Specifies whether the version will be echoed or returned.
  79. Returns:
  80. The installed version number.
  81. */
  82. function LightyVersion($output = 'e') {
  83. # DON'T TOUCH!
  84. $version = '0.9.4';
  85. # Are we echoing or returning?
  86. if($output == 'e') { echo $version; }
  87. # Returning!
  88. else { return $version; }
  89. }
  90. /*
  91. Function: dirlist
  92. Reads a directory and outputs its directories into a sorted array.
  93. Parameters:
  94. input - The path of the directory to inspect.
  95. Returns:
  96. An array sorted in ascending order by values containing the directories in the given path.
  97. */
  98. function dirlist($input) {
  99. # Start foreach loop and set search pattern
  100. foreach(glob($input.'/*', GLOB_ONLYDIR) as $dir) {
  101. # Remove the containing directory
  102. $dir = str_replace($input.'/', '', $dir);
  103. # Place directories in an array
  104. $array[$dir] = ucwords(strtolower($dir));
  105. }
  106. # Sort the array into ascending order by values
  107. asort($array);
  108. # Return it!
  109. return $array;
  110. }
  111. function currentURL() {
  112. $pageURL = 'http';
  113. if($_SERVER["HTTPS"] == "on") {
  114. $pageURL .= "s";
  115. }
  116. $pageURL .= "://";
  117. if($_SERVER["SERVER_PORT"] != "80") {
  118. $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  119. }
  120. else {
  121. $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  122. }
  123. return $pageURL;
  124. }
  125. /*
  126. Function: advancedPagination
  127. Creates a more advanced pagination that is more efficient for handling large amounts of data than <simplePagination>.
  128. Parameters:
  129. type - Type of content being processed.
  130. target - URL of the page that the pagination will be displayed on.
  131. page - The page the user is currently on.
  132. limit - Defines how many items are in a page.
  133. adjacents - Number of items in the pagination on either side of the current page? (not entirely sure)
  134. pagestring - GET argument to be used for the current page.
  135. Returns:
  136. HTML code for a full pagination menu.
  137. */
  138. function advancedPagination($type, $target, $page = 1, $limit = 8, $adjacents = 1, $pagestring = "&page=") {
  139. // Global the database handle so we can use it in this function
  140. global $dbh;
  141. // The page cannot be less than 1.
  142. if($page < 1)
  143. {
  144. $page = 1;
  145. }
  146. // Set defaults
  147. if(!$adjacents) $adjacents = 1;
  148. if(!$limit) $limit = 8;
  149. if(!$page) $page = 1;
  150. // Set teh query to retrieve the number of rows
  151. $query = $dbh->query("
  152. SELECT
  153. COUNT(*)
  154. FROM '".sqlite_escape_string($type)."'");
  155. // Query the database
  156. @list($totalitems) = $query->fetch(PDO::FETCH_NUM);
  157. // Set various required variables
  158. $prev = $page - 1; // Previous page is page - 1
  159. $next = $page + 1; // Next page is page + 1
  160. $lastpage = ceil($totalitems/$limit); // Last page is = total items / items per page, rounded up.
  161. $lpm1 = $lastpage - 1; // Last page minus 1
  162. // The page also cannot exceed the last page.
  163. if($page > $lastpage)
  164. {
  165. $page = $lastpage;
  166. }
  167. // Clear $pagination
  168. $pagination = "";
  169. // Do we have more than one page?
  170. if($totalitems > $limit)
  171. {
  172. // Start the pagination div
  173. $pagination .= "<div class=\"pagination\">";
  174. // Add the previous button
  175. if($page > 1)
  176. {
  177. $pagination .= "<a href=\"" . $target . $pagestring . $prev . "\">&laquo; prev</a>";
  178. }
  179. else
  180. {
  181. // Disable the previous button, since we're on the first page
  182. $pagination .= "<span class=\"disabled\">&laquo; prev</span>";
  183. }
  184. // Add the page buttons
  185. if ($lastpage < 7 + ($adjacents * 2))
  186. {
  187. // There aren't enough pages to bother breaking it up
  188. // Loop through the pages and create links for all
  189. for($counter = 1; $counter <= $lastpage; $counter++)
  190. {
  191. if($counter == $page)
  192. {
  193. $pagination .= "<span class=\"current\">$counter</span>";
  194. }
  195. else
  196. {
  197. $pagination .= "<a href=\"" . $target . $pagestring . $counter . "\">$counter</a>";
  198. }
  199. }
  200. }
  201. elseif($lastpage >= 7 + ($adjacents * 2))
  202. {
  203. // We have enough pages to hide some of them now
  204. if($page < 1 + ($adjacents * 3))
  205. {
  206. // Start a loop and create the first few pages
  207. for($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
  208. {
  209. if($counter == $page)
  210. {
  211. $pagination .= "<span class=\"current\">$counter</span>";
  212. }
  213. else
  214. {
  215. $pagination .= "<a href=\"" . $target . $pagestring. $counter . "\">$counter</a>";
  216. }
  217. }
  218. // Add the ellipses
  219. $pagination .= "<span class=\"elipses\">...</span>";
  220. $pagination .= "<a href=\"" . $target . $pagestring . $lpm1 . "\">$lpm1</a>";
  221. $pagination .= "<a href=\"" . $target . $pagestring . $lastpage . "\">$lastpage</a>";
  222. }
  223. // We're in the middle; hide some in the front and back
  224. elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
  225. {
  226. // Add the first two links
  227. $pagination .= "<a href=\"" . $target . $pagestring . "1\">1</a>";
  228. $pagination .= "<a href=\"" . $target . $pagestring . "2\">2</a>";
  229. // Add the ellipses
  230. $pagination .= "<span class=\"elipses\">...</span>";
  231. // Start the for loop to make the page links
  232. for($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
  233. {
  234. if($counter == $page)
  235. {
  236. $pagination .= "<span class=\"current\">$counter</span>";
  237. }
  238. else
  239. {
  240. $pagination .= "<a href=\"" . $target . $target . $pagestring . $counter . "\">$counter</a>";
  241. }
  242. }
  243. // Add the ellipses and the last few pages
  244. $pagination .= "...";
  245. $pagination .= "<a href=\"" . $target . $pagestring . $lpm1 . "\">$lpm1</a>";
  246. $pagination .= "<a href=\"" . $target . $pagestring . $lastpage . "\">$lastpage</a>";
  247. }
  248. // We're close to the end, so only hide the early pages
  249. else
  250. {
  251. // Add the first few pages
  252. $pagination .= "<a href=\"".$target.$pagestring."1\">1</a>";
  253. $pagination .= "<a href=\"".$target.$pagestring."2\">2</a>";
  254. // Add the ellipses
  255. $pagination .= "<span class=\"elipses\">...</span>";
  256. for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
  257. {
  258. if ($counter == $page)
  259. {
  260. $pagination .= "<span class=\"current\">$counter</span>";
  261. }
  262. else
  263. {
  264. $pagination .= "<a href=\"".$target.$pagestring.$counter."\">$counter</a>";
  265. }
  266. }
  267. }
  268. }
  269. // Add the next button
  270. if ($page < $counter - 1)
  271. {
  272. $pagination .= "<a href=\"".$target.$pagestring.$next."\">next &raquo;</a>";
  273. }
  274. else
  275. {
  276. $pagination .= "<span class=\"disabled\">next &raquo;</span>";
  277. }
  278. // End the pagination div
  279. $pagination .= "</div>\n";
  280. }
  281. // Return the final pagination div
  282. return $pagination;
  283. }
  284. /*
  285. Function: list_themes
  286. Outputs a list of themes in HTML <option> tags.
  287. */
  288. function list_themes()
  289. {
  290. // List directories
  291. $dir = dirlist(ABSPATH .'/themes');
  292. foreach($dir as $k => $v)
  293. {
  294. echo '<option value="'. utf_htmlspecialchars($k). '"'. (get_bloginfo('theme') == $k ? ' selected="selected"' : ''). '>'. utf_htmlspecialchars($v). '</option>';
  295. }
  296. }
  297. /*
  298. Function: list_pages
  299. Lists pages as HTML list items.
  300. Parameters:
  301. tag - The HTML tag that will contain the link (e.g. li)
  302. limit - The maximum number of pages to list.
  303. Returns:
  304. HTML list items for however many pages were requested.
  305. */
  306. function list_pages($tag = 'li', $limit = 5)
  307. {
  308. global $dbh;
  309. $query_c = "
  310. SELECT
  311. COUNT(*)
  312. FROM pages
  313. ORDER BY page_id DESC
  314. LIMIT 0, ". ((int)$limit);
  315. $result = $dbh->query("
  316. SELECT
  317. *
  318. FROM pages
  319. ORDER BY page_id DESC
  320. LIMIT 0, ". ((int)$limit));
  321. if(count_rows($query_c))
  322. {
  323. while($page = $result->fetchObject())
  324. {
  325. echo '<'. $tag. '><a href="'. get_bloginfo('url'). '?page='. $page->page_id. '">'. $page->page_title. '</a></'. $tag. '>';
  326. }
  327. }
  328. else
  329. {
  330. echo '<'. $tag. '>No pages.</'. $tag. '>';
  331. }
  332. }
  333. /*
  334. Function: list_categories
  335. Lists categories as HTML list items.
  336. Parameters:
  337. tag - The HTML tag that will contain the link (e.g. li or option)
  338. limit - The maximum number of pages to list.
  339. Returns:
  340. HTML list items for however many pages were requested.
  341. */
  342. function list_categories($tag = 'li', $limit = 5, $selected = null)
  343. {
  344. // Grab the database handle
  345. global $dbh;
  346. // Is there a limit? If so, typecast it and add it to the query
  347. if($limit != null)
  348. {
  349. $limit = "LIMIT 0, ".(int)$limit;
  350. }
  351. // Get category data from database
  352. $result = $dbh->query("
  353. SELECT
  354. *
  355. FROM categories
  356. ORDER BY category_id DESC
  357. ".$limit);
  358. // What tag are we using?
  359. if($tag == 'option')
  360. {
  361. while($row = $result->fetchObject())
  362. {
  363. echo '<option value="'. $row->category_id. '"'. ($row->category_id == $selected ? ' selected="selected"' : ''). '>'. $row->full_name. '</option>';
  364. }
  365. }
  366. else
  367. {
  368. // Sort through and create list items
  369. while($row = $result->fetchObject())
  370. {
  371. echo '<'. $tag. '><a href="'. get_bloginfo('url'). '?category='. $row->category_id. '">'. $row->full_name .'</a></'. $tag. '>';
  372. }
  373. }
  374. }
  375. /*
  376. Function: list_archives
  377. Outputs a multi-level HTML list containing links for monthly post archives.
  378. */
  379. function list_archives($limit = 10)
  380. {
  381. // Grab the database handle
  382. global $dbh;
  383. // Get archive data
  384. $result = $dbh->query(
  385. "SELECT
  386. strftime('%m', post_date, 'unixepoch') AS 'month',
  387. strftime('%Y', post_date, 'unixepoch') AS 'year',
  388. post_date
  389. FROM posts
  390. WHERE published != 0
  391. GROUP BY month");
  392. // Sort through and create list items
  393. $i = 0;
  394. $return = '';
  395. while($row = $result->fetchObject())
  396. {
  397. $month = $row->month;
  398. $monthname = date('F', $row->post_date);
  399. $year = $row->year;
  400. $return .= '<li><a href="'. get_bloginfo('url'). '?archive='. $year. $month.'">'. $monthname. ' '. $year. '</a></li>';
  401. $i++;
  402. if($i >= $limit)
  403. {
  404. break;
  405. }
  406. }
  407. echo $return;
  408. }
  409. /*
  410. Function: get_commentnum
  411. Outputs the number of comments on a post.
  412. Returns:
  413. The number of comments as an integer.
  414. */
  415. function get_commentnum($id)
  416. {
  417. // Make the database handle available here
  418. global $dbh;
  419. // If it's null, use the global
  420. if($id == null)
  421. {
  422. $id = $GLOBALS['pid'];
  423. }
  424. // Set the query
  425. $query = $dbh->query("
  426. SELECT
  427. COUNT(*)
  428. FROM comments
  429. WHERE published = 1 AND post_id= ". ((int)$id));
  430. // Query the database
  431. @list($commentnum) = $query->fetch(PDO::FETCH_NUM);
  432. // Return data
  433. return $commentnum;
  434. }
  435. function commentnum($id)
  436. {
  437. echo get_commentnum($id);
  438. }
  439. /*
  440. Function: alternateColor
  441. Alternates colors using CSS classes. Technically, it could alternate anything.
  442. Parameters:
  443. class1 - Name of the first class.
  444. class2 - Name of the second class.
  445. Returns:
  446. The appropriate class name.
  447. */
  448. function alternateColor($class1, $class2)
  449. {
  450. static $count = 1;
  451. return (($count++) % 2) == 0 ? $class1 : $class2;
  452. }
  453. ?>