PageRenderTime 36ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/2011/end_www/helper/common.php

http://endcms.googlecode.com/
PHP | 273 lines | 190 code | 34 blank | 49 comment | 58 complexity | d33eb62c01ca83b3a1d1a73aa89fefe2 MD5 | raw file
  1. <?php
  2. function get_all_items($s,$cond=array())
  3. {
  4. //?????id
  5. $s = (is_numeric($s))?intval($s):array('url'=>$s);
  6. //??????
  7. $cat = model('category')->get_one($s);
  8. //??????????
  9. $item_type = preg_replace('/\_list$/','',$cat['status']);
  10. $children = model('category')->get_list(array('parent_id'=>$cat['category_id']));
  11. if (!$cond['where']) $cond['where'] = '1=1';
  12. if ($children && count($children) > 0)
  13. {
  14. $ids = array($cat['category_id']);
  15. foreach($children as $_c)
  16. {
  17. $ids[] = $_c['category_id'];
  18. }
  19. $cond['where'] .= ' AND category_id IN ('.join(',',$ids).')';
  20. }
  21. else
  22. {
  23. $cond['category_id'] = $cat['category_id'];
  24. }
  25. return model($item_type)->get_list($cond);
  26. }
  27. function fragment($s)
  28. {
  29. $c = model('category')->get_one(array('url'=>$s));
  30. if ($c && $c['content']) return $c['content'];
  31. }
  32. /**
  33. * ??sql??????
  34. *
  35. * @param string $sql
  36. * @return array(array,....)
  37. * 2010-04-20
  38. */
  39. function sql($sql)
  40. {
  41. global $db;
  42. return $db->get_all($sql);
  43. }
  44. /**
  45. * ??category?id??alias?????????
  46. *
  47. * @param string $s category_id(int)??alias(string)
  48. * @param array $cond ????
  49. * @return array(array,...)
  50. * 2010-04-20
  51. */
  52. function get_items($s,$cond=array())
  53. {
  54. //?????id
  55. $s = (is_numeric($s))?intval($s):array('url'=>$s);
  56. //??????
  57. $cat = model('category')->get_one($s);
  58. //??????????
  59. $item_type = preg_replace('/\_list$/','',$cat['status']);
  60. if (!is_array($cond))
  61. {
  62. $_tmp = $cond;
  63. $cond = array('where'=>$_tmp);
  64. unset($_tmp);
  65. }
  66. $cond['category_id'] = $cat['category_id'];
  67. return model($item_type)->get_list($cond);
  68. }
  69. /**
  70. * ??category?id?? url?????????
  71. *
  72. * @param string $s category_id(int)??url(string)
  73. * @return array(array,...)
  74. * 2010-04-20
  75. */
  76. function get_cats($s)
  77. {
  78. return model('category')->get_cats($s);
  79. }
  80. /**
  81. * ??category??????????
  82. *
  83. * @param array $o ?????????:array('category_id'=>1,'name'=>'???' ... )
  84. * @return string
  85. * 2010-04-20
  86. */
  87. function category_link($o)
  88. {
  89. if ($o['status'] == 'link')
  90. return $o['url'];
  91. else if ( $o['status'] == 'page')
  92. return '?page/'.$o['url'];
  93. else
  94. return '?cat/'.$o['url'];
  95. }
  96. /**
  97. * ??item?????????
  98. *
  99. * @param array $o item???????:array('item_id'=>1,'name'=>'xxx' ... )
  100. * @return string
  101. * 2010-04-20
  102. */
  103. function item_link($o)
  104. {
  105. return '?article/'.$o['article_id'];
  106. }
  107. /*
  108. ??????
  109. ?? 3? 5?? 7?
  110. */
  111. function get_past_time($t,$second='?',$minite='?',$hour='??',$day='?',$month='?',$year='?')
  112. {
  113. $d = time()-$t;
  114. if ($d < 60)
  115. {
  116. return $d.$second;
  117. }
  118. $d = intval($d/60);
  119. if ($d < 60)
  120. {
  121. return $d.$minite;
  122. }
  123. $d = intval($d/60);
  124. if ($d < 24)
  125. {
  126. return $d.$hour;
  127. }
  128. $d = intval($d/24);
  129. if ($d < 30)
  130. {
  131. return $d.$day;
  132. }
  133. $d = intval($d/30);
  134. if ($d < 12)
  135. {
  136. return $d.$month;
  137. }
  138. return intval($d/12).$year;
  139. }
  140. /*
  141. ??????
  142. */
  143. function get_past_day($t)
  144. {
  145. $d = time()-$t;
  146. $d = intval($d/60);
  147. $d = intval($d/60);
  148. $d = intval($d/24);
  149. if ($d <= 0) return '??';
  150. else if ($d == 1) return '??';
  151. else if ($d == 2) return '??';
  152. if ($d < 10)
  153. {
  154. return $d.'??';
  155. }
  156. if (date('Y') == date('Y',$t))
  157. {
  158. $d = date('m') - date('m',$t);
  159. if ($d == 0) return '??'.date('d',$t).'?';
  160. else if ($d == 1) return '??'.date('d',$t).'?';
  161. else return date('m?d?',$t);
  162. }
  163. return date('Y?m?d?',$t);
  164. }
  165. function show_plaint($s)
  166. {
  167. $s = str_replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;",$s);
  168. $s = str_replace(" ","&nbsp;",$s);
  169. $s = str_replace("\n","<br>",$s);
  170. return $s;
  171. }
  172. function thumb($orig_path,$mw=100,$mh=100,$method='cut',$thumb=false,$png=false)
  173. {
  174. if ($method != 'box' && $method != 'cut') $method = 'cut';
  175. if (!$orig_path) return 'about:blank';
  176. if (preg_match('/^http\:\/\//i',$orig_path)) return $orig_path;
  177. $path = END_ROOT.$orig_path;
  178. $ftype = array_pop(explode('.',$path));
  179. $etag = basename($path).$method.$mw.'x'.$mh;
  180. $etag.= $png ? '.png':'.jpg';
  181. if (!file_exists($path)) return '';
  182. $thumb = dirname($path).'/'.$etag;
  183. if (file_exists($thumb)) return dirname($orig_path).'/'.$etag;
  184. if (!$imgarr=@getimagesize($path)) return '';
  185. $width_orig=$imgarr[0];
  186. $height_orig=$imgarr[1];
  187. $mime_orig=$imgarr["mime"];
  188. $mime=str_replace("image/","",$mime_orig);
  189. $mime=($mime=="bmp")?"wbmp":$mime;
  190. if (!function_exists("imagecreatefrom$mime")) return false;
  191. $p = $mw/$width_orig;
  192. $_p = $mh/$height_orig;
  193. if ($method == 'cut' && $_p>$p)
  194. {
  195. $p = $_p;
  196. $width = $mw;
  197. $height = $mh;
  198. $cut_height = 0;
  199. $cut_width = intval(($width_orig - $mw/$p)/2);
  200. }
  201. else if ($method == 'cut')
  202. {
  203. $width = $mw;
  204. $height = $mh;
  205. $cut_height = intval(($height_orig - $mh/$p)/2);
  206. $cut_width = 0;
  207. }
  208. else if ($method == 'box' && $_p<$p)
  209. {
  210. $p = $_p;
  211. if ($p > 1) $p = 1;
  212. $width = $p*$width_orig;
  213. $height = $p*$height_orig;
  214. $cut_height = 0;
  215. $cut_width = 0;
  216. }
  217. else if ($method == 'box')
  218. {
  219. if ($p > 1) $p = 1;
  220. $width = $p*$width_orig;
  221. $height = $p*$height_orig;
  222. $cut_height = 0;
  223. $cut_width = 0;
  224. }
  225. $image_p = @imagecreatetruecolor($width, $height);
  226. $_func = 'imagecreatefrom'.$mime;
  227. $image = @$_func($path);
  228. if ($png)
  229. {
  230. imagealphablending($image_p,true);
  231. $tcolor = imagecolortransparent($image_p, imagecolorallocatealpha($image_p, 0, 0, 0,127));
  232. imagefill($image_p, 0, 0, $tcolor);
  233. imagesavealpha($image_p, true);
  234. }
  235. @imagecopyresampled($image_p, $image, 0, 0, $cut_width, $cut_height, $width, $height, $width/$p, $height/$p);
  236. $_func = $png?'imagepng':'imagejpeg';
  237. $_func($image_p,$thumb,90);
  238. return (file_exists($thumb))?dirname($orig_path).'/'.$etag:false;
  239. }