PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/helper.inc.php

https://github.com/d13design/d13framework
PHP | 311 lines | 243 code | 32 blank | 36 comment | 44 complexity | a1237f7445e74cffebc3147059735954 MD5 | raw file
  1. <?php
  2. //
  3. // helper.inc.php
  4. // d13design
  5. //
  6. // Created by Dave Waller on 2011-10-11.
  7. // Copyright 2011 Dave Waller. All rights reserved.
  8. //
  9. /* Helper to embed a stylesheet */
  10. function html_css($file,$rel="stylesheet"){
  11. echo '<link rel="'.$rel.'" type="text/css" href="'.SITE_URL.'/template/'.TEMPLATE.'/css/'.$file.'" />';
  12. }
  13. /* Helper to embed javascript */
  14. function html_js($file){
  15. echo '<script type="text/javascript" src="'.ASSETS_URL.'/js/'.$file.'"></script>';
  16. }
  17. /* Helper to generate HTML for an image */
  18. function html_img($file,$class='',$alt='',$link='',$linkclass='',$title='',$target=''){
  19. // Always fallback to links opening in _self
  20. if($target=='' || !isset($target)) $target='_self';
  21. // If link is provided, open the <a> tag
  22. if($link != '') echo '<a href="'.$link.'" title="'.$title.'" class="'.$linkclass.'" target="'.$target.'">';
  23. //Output image HTML
  24. echo '<img src="'.ASSETS_URL.'/'.$file.'" alt="'.$alt.'" class="'.$class.'" />';
  25. // If link is provided, close the <a> tag
  26. if($link != '') echo '</a>';
  27. }
  28. /* Helper to generare HTML for a link */
  29. function html_link($link,$text,$linkClass='',$title='',$target='',$span=false){
  30. // Always fallback to links opening in _self
  31. if($target=='' || !isset($target)) $target='_self';
  32. echo '<a href="'.$link.'" title="'.$title.'" class="'.$linkClass.'" target="'.$target.'">';
  33. if($span) echo '<span>';
  34. echo $text;
  35. if($span) echo '</span>';
  36. echo '</a>';
  37. }
  38. /* Helper to create a compatible path */
  39. function create_path($a='',$b='',$c=''){
  40. $pathString = '';
  41. if($a!=''){
  42. $pathString = $pathString.'/'.$a;
  43. }
  44. if($a!='' && $b!=''){
  45. $pathString = $pathString.'/'.$b;
  46. }
  47. if($a!='' && $b!='' && $c!=''){
  48. $pathString = $pathString.'/'.$c;
  49. }
  50. return SITE_URL.$pathString;
  51. }
  52. /* Helper to get the section name from a provided slug */
  53. function get_section($slug){
  54. $connection = mysql_connect(DB_HOST,DB_USER,DB_PWRD);
  55. if (!$connection){ die('Could not connect: ' . mysql_error()); }
  56. mysql_select_db(DB_NAME, $connection);
  57. $r = mysql_query("SELECT title FROM ".TBL_PRE."sections WHERE slug='".$slug."'");
  58. $rw = mysql_fetch_row($r);
  59. return(rawurldecode($rw[0]));
  60. }
  61. /* Helper to list pages */
  62. function list_pages($a){
  63. $connection = mysql_connect(DB_HOST,DB_USER,DB_PWRD);
  64. if (!$connection){ die('Could not connect: ' . mysql_error()); }
  65. mysql_select_db(DB_NAME, $connection);
  66. $result = mysql_query("SELECT * FROM ".TBL_PRE."pages");
  67. mysql_close($connection);
  68. while($row = mysql_fetch_array($result)){
  69. if($a['section']=='pages' && $a['page']==$row['slug']){ $class="active"; }else{ $class=""; }
  70. echo '<li class="'.$class.'">';
  71. html_link(create_path('pages',$row['slug']),rawurldecode($row['title']));
  72. echo '</li>';
  73. }
  74. }
  75. /* Helper to list content sections */
  76. function list_sections($a){
  77. $connection = mysql_connect(DB_HOST,DB_USER,DB_PWRD);
  78. if (!$connection){ die('Could not connect: ' . mysql_error()); }
  79. mysql_select_db(DB_NAME, $connection);
  80. $result = mysql_query("SELECT * FROM ".TBL_PRE."sections");
  81. mysql_close($connection);
  82. while($row = mysql_fetch_array($result)){
  83. if($a['section']==$row['slug']){ $class="active"; }else{ $class=""; }
  84. echo '<li class="'.$class.'">';
  85. html_link(create_path($row['slug']),rawurldecode($row['title']));
  86. echo '</li>';
  87. }
  88. }
  89. /* Helper to return article data in an array */
  90. function get_articles($section_slug,$start=0,$total=1000000){
  91. $articles = array();
  92. $connection = mysql_connect(DB_HOST,DB_USER,DB_PWRD);
  93. if (!$connection){ die('Could not connect: ' . mysql_error()); }
  94. mysql_select_db(DB_NAME, $connection);
  95. $r = mysql_query("SELECT id FROM ".TBL_PRE."sections WHERE slug='".$section_slug."'");
  96. $rw = mysql_fetch_row($r);
  97. $result = mysql_query("SELECT * FROM ".TBL_PRE."articles WHERE section_id=".$rw[0]." ORDER BY created DESC LIMIT ".$start.", ".$total);
  98. mysql_close($connection);
  99. while($row = mysql_fetch_array($result)){
  100. $articles[] = array(
  101. 'id' => $row['id'],
  102. 'section_id' => $row['section_id'],
  103. 'section_slug' => $section_slug,
  104. 'title' => rawurldecode($row['title']),
  105. 'slug' => $row['slug'],
  106. 'synopsis' => stripslashes(rawurldecode($row['synopsis'])),
  107. 'contents' => stripslashes(rawurldecode($row['contents'])),
  108. 'custom_data' => rawurldecode($row['custom_data']),
  109. 'created' => $row['created'],
  110. );
  111. }
  112. return($articles);
  113. }
  114. /* Helper to return latest articles */
  115. function list_articles($section_slug='',$total=5,$icon='file'){
  116. $connection = mysql_connect(DB_HOST,DB_USER,DB_PWRD);
  117. if (!$connection){ die('Could not connect: ' . mysql_error()); }
  118. mysql_select_db(DB_NAME, $connection);
  119. if($section_slug != ''){
  120. $r = mysql_query("SELECT id FROM ".TBL_PRE."sections WHERE slug='".$section_slug."'");
  121. $rw = mysql_fetch_row($r);
  122. $result = mysql_query("SELECT * FROM ".TBL_PRE."articles WHERE section_id=".$rw[0]." ORDER BY created DESC LIMIT 0, ".$total);
  123. mysql_close($connection);
  124. while($row = mysql_fetch_array($result)){
  125. echo '<li><i class="icon-'.$icon.'"></i>';
  126. html_link(create_path($section_slug,$row['slug']),rawurldecode($row['title']));
  127. echo '</li>';
  128. }
  129. }else{
  130. $r = mysql_query("SELECT * FROM ".TBL_PRE."sections");
  131. $slugs = array();
  132. while($row = mysql_fetch_array($r)){
  133. $slugs[$row['id']] = $row['slug'];
  134. }
  135. $result = mysql_query("SELECT * FROM ".TBL_PRE."articles ORDER BY created DESC LIMIT 0, ".$total);
  136. mysql_close($connection);
  137. while($row = mysql_fetch_array($result)){
  138. echo '<li><i class="icon-'.$icon.'"></i>';
  139. html_link(create_path($slugs[$row['section_id']],$row['slug']),rawurldecode($row['title']));
  140. echo '</li>';
  141. }
  142. }
  143. }
  144. /* Helper to get the contents of a page */
  145. function get_page($page_slug){
  146. $connection = mysql_connect(DB_HOST,DB_USER,DB_PWRD);
  147. if (!$connection){ die('Could not connect: ' . mysql_error()); }
  148. mysql_select_db(DB_NAME, $connection);
  149. $result = mysql_query("SELECT * FROM ".TBL_PRE."pages WHERE slug='".$page_slug."'");
  150. mysql_close($connection);
  151. $page_data = array();
  152. $row = mysql_fetch_row($result);
  153. $page_data['id'] = $row[0];
  154. $page_data['title'] = rawurldecode($row[1]);
  155. $page_data['contents'] = stripslashes(rawurldecode($row[3]));
  156. return($page_data);
  157. }
  158. /* Helper to get the contents of an article */
  159. function get_article($slug){
  160. $connection = mysql_connect(DB_HOST,DB_USER,DB_PWRD);
  161. if (!$connection){ die('Could not connect: ' . mysql_error()); }
  162. mysql_select_db(DB_NAME, $connection);
  163. $result = mysql_query("SELECT * FROM ".TBL_PRE."articles WHERE slug='".$slug."'");
  164. $article = array();
  165. $row = mysql_fetch_row($result);
  166. $article['id'] = $row[0];
  167. $article['section_id'] = $row[1];
  168. $result2 = mysql_query("SELECT * FROM ".TBL_PRE."sections WHERE id=".$row[1]);
  169. $row2 = mysql_fetch_row($result2);
  170. $article['section_title'] = rawurldecode($row2[1]);
  171. $article['section_slug'] = $row2[2];
  172. $article['title'] = rawurldecode($row[2]);
  173. $article['slug'] = $row[3];
  174. $article['synopsis'] = stripslashes(rawurldecode($row[4]));
  175. $article['contents'] = stripslashes(rawurldecode($row[5]));
  176. $article['custom_data'] = rawurldecode($row[6]);
  177. $article['created'] = strtotime($row[7]);
  178. mysql_close($connection);
  179. return($article);
  180. }
  181. /* Helper to convert a 'created' timestamp into "March 2012" */
  182. function pretty_date($archive,$length='short'){
  183. if($length=='short'){ return date("F Y",$archive); }
  184. if($length=='medium'){ return date("d F Y",$archive); }
  185. if($length=='long'){ return date("l, d F Y",$archive); }
  186. if($length=='x-long'){ return date("l, d F Y - H:i a",$archive); }
  187. }
  188. /* A helper to trim a string to a given number of words */
  189. function trimmer($str, $len=12, $extra='&#133;'){
  190. $pieces = explode(" ", $str);
  191. $op = '';
  192. for($a=0;$a<$len;$a++){
  193. $op = $op.$pieces[$a].' ';
  194. }
  195. if(count($pieces)>$len) $op = $op.$extra;
  196. return $op;
  197. }
  198. /* Helper to create pagination based on an array of content files */
  199. function pagination($pagelist,$section,$qs,$first="&larr;",$prev="&lt;",$next="&gt;",$last="&rarr;"){
  200. if(count($pagelist) > PAGE_COUNT){
  201. //PAGINATION
  202. $items = array();
  203. //First page and previous links...
  204. if($qs==0){
  205. $items[] = '<li class="prev disabled"><a href="#">'.$first.'</a></li>';
  206. $items[] = '<li class="disabled"><a href="#">'.$prev.'</a></li>';
  207. }else{
  208. $items[] = '<li class="prev"><a title="First page" href="'.create_path($section).'">'.$first.'</a></li>';
  209. $items[] = '<li><a title="Previous page" href="'.create_path($section.'?'.($qs-PAGE_COUNT)).'">'.$prev.'</a></li>';
  210. }
  211. //Page links...
  212. for($x=0;$x<ceil(count($pagelist)/PAGE_COUNT);$x++){
  213. if($x*PAGE_COUNT==$qs){
  214. $items[] = '<li class="active"><a href="">'.($x+1).'</a></li>';
  215. }else{
  216. $items[] = '<li><a title="Page '.($x+1).'" href="'.create_path($section.'?'.($x*PAGE_COUNT)).'">'.($x+1).'</a></li>';
  217. }
  218. }
  219. //Next and last links...
  220. if($qs+PAGE_COUNT >= count($pagelist)){
  221. $items[] = '<li class="disabled"><a href="#">'.$next.'</a></li>';
  222. $items[] = '<li class="next disabled"><a href="#">'.$last.'</a></li>';
  223. }else{
  224. $items[] = '<li><a title="Next page" href="'.create_path($section.'?'.($qs+PAGE_COUNT)).'">'.$next.'</a></li>';
  225. $items[] = '<li class="next"><a title="Last page" href="'.create_path($section.'?'.(($x-1)*PAGE_COUNT)).'">'.$last.'</a></li>';
  226. }
  227. //Render pagination...
  228. echo '<div class="pagination"><ul>';
  229. foreach($items as $item){
  230. echo $item;
  231. }
  232. echo '</ul></div>';
  233. }
  234. }
  235. /* Helper to decode custom article properties based on two different separators */
  236. function parse_properties($customProps,$pairSep="|",$keyValSep="="){
  237. $returnArray = array();
  238. $custom_data = explode($pairSep,$customProps);
  239. foreach($custom_data as $data_pair){
  240. $pair = explode($keyValSep,$data_pair);
  241. $returnArray[$pair[0]] = $pair[1];
  242. }
  243. return($returnArray);
  244. }
  245. /* Helper to get the latest tweet from a provided Twitter username */
  246. function get_status($twitter_id, $hyperlinks = true) {
  247. $c = curl_init();
  248. curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=1");
  249. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  250. $src = curl_exec($c);
  251. curl_close($c);
  252. preg_match('/<text>(.*)<\/text>/', $src, $m);
  253. $status = htmlentities($m[1]);
  254. if( $hyperlinks ) $status = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", '<a href="%5C%22%5C%5C0%5C%22">\\0</a>', $status);
  255. if($status){
  256. return($status);
  257. }else{
  258. return('No tweets were returned, follow <a href="http://twitter.com/'.$twitter_id.'" target="_blank">@'.$twitter_id.'</a> on Twitter');
  259. }
  260. }
  261. /* Admin helpers */
  262. /* Helper to decide if a user is authenticated */
  263. function authenticated(){
  264. if(isset($_SESSION['username'])){
  265. $connection = mysql_connect(DB_HOST,DB_USER,DB_PWRD);
  266. if (!$connection){ die('Could not connect: ' . mysql_error()); }
  267. mysql_select_db(DB_NAME, $connection);
  268. $result = mysql_query("SELECT id FROM ".TBL_PRE."users WHERE username='".$_SESSION['username']."'");
  269. $row = mysql_fetch_row($result);
  270. if($row[0]){
  271. return true;
  272. }else{
  273. return false;
  274. }
  275. }else{
  276. return false;
  277. }
  278. }
  279. ?>