/2011/end_www/helper/common.php
PHP | 273 lines | 190 code | 34 blank | 49 comment | 58 complexity | d33eb62c01ca83b3a1d1a73aa89fefe2 MD5 | raw file
- <?php
- function get_all_items($s,$cond=array())
- {
- //?????id
- $s = (is_numeric($s))?intval($s):array('url'=>$s);
- //??????
- $cat = model('category')->get_one($s);
- //??????????
- $item_type = preg_replace('/\_list$/','',$cat['status']);
-
-
- $children = model('category')->get_list(array('parent_id'=>$cat['category_id']));
- if (!$cond['where']) $cond['where'] = '1=1';
- if ($children && count($children) > 0)
- {
- $ids = array($cat['category_id']);
- foreach($children as $_c)
- {
- $ids[] = $_c['category_id'];
- }
- $cond['where'] .= ' AND category_id IN ('.join(',',$ids).')';
- }
- else
- {
- $cond['category_id'] = $cat['category_id'];
- }
- return model($item_type)->get_list($cond);
- }
- function fragment($s)
- {
- $c = model('category')->get_one(array('url'=>$s));
- if ($c && $c['content']) return $c['content'];
- }
- /**
- * ??sql??????
- *
- * @param string $sql
- * @return array(array,....)
- * 2010-04-20
- */
- function sql($sql)
- {
- global $db;
- return $db->get_all($sql);
- }
- /**
- * ??category?id??alias?????????
- *
- * @param string $s category_id(int)??alias(string)
- * @param array $cond ????
- * @return array(array,...)
- * 2010-04-20
- */
- function get_items($s,$cond=array())
- {
- //?????id
- $s = (is_numeric($s))?intval($s):array('url'=>$s);
- //??????
- $cat = model('category')->get_one($s);
- //??????????
- $item_type = preg_replace('/\_list$/','',$cat['status']);
-
- if (!is_array($cond))
- {
- $_tmp = $cond;
- $cond = array('where'=>$_tmp);
- unset($_tmp);
- }
- $cond['category_id'] = $cat['category_id'];
- return model($item_type)->get_list($cond);
- }
- /**
- * ??category?id?? url?????????
- *
- * @param string $s category_id(int)??url(string)
- * @return array(array,...)
- * 2010-04-20
- */
- function get_cats($s)
- {
- return model('category')->get_cats($s);
- }
- /**
- * ??category??????????
- *
- * @param array $o ?????????:array('category_id'=>1,'name'=>'???' ... )
- * @return string
- * 2010-04-20
- */
- function category_link($o)
- {
- if ($o['status'] == 'link')
- return $o['url'];
- else if ( $o['status'] == 'page')
- return '?page/'.$o['url'];
- else
- return '?cat/'.$o['url'];
- }
- /**
- * ??item?????????
- *
- * @param array $o item???????:array('item_id'=>1,'name'=>'xxx' ... )
- * @return string
- * 2010-04-20
- */
- function item_link($o)
- {
- return '?article/'.$o['article_id'];
- }
- /*
- ??????
- ?? 3? 5?? 7?
- */
- function get_past_time($t,$second='?',$minite='?',$hour='??',$day='?',$month='?',$year='?')
- {
- $d = time()-$t;
- if ($d < 60)
- {
- return $d.$second;
- }
- $d = intval($d/60);
- if ($d < 60)
- {
- return $d.$minite;
- }
- $d = intval($d/60);
- if ($d < 24)
- {
- return $d.$hour;
- }
- $d = intval($d/24);
- if ($d < 30)
- {
- return $d.$day;
- }
- $d = intval($d/30);
- if ($d < 12)
- {
- return $d.$month;
- }
- return intval($d/12).$year;
- }
- /*
- ??????
- */
- function get_past_day($t)
- {
- $d = time()-$t;
- $d = intval($d/60);
- $d = intval($d/60);
- $d = intval($d/24);
- if ($d <= 0) return '??';
- else if ($d == 1) return '??';
- else if ($d == 2) return '??';
-
- if ($d < 10)
- {
- return $d.'??';
- }
-
- if (date('Y') == date('Y',$t))
- {
- $d = date('m') - date('m',$t);
-
- if ($d == 0) return '??'.date('d',$t).'?';
- else if ($d == 1) return '??'.date('d',$t).'?';
- else return date('m?d?',$t);
- }
-
- return date('Y?m?d?',$t);
- }
- function show_plaint($s)
- {
- $s = str_replace("\t"," ",$s);
- $s = str_replace(" "," ",$s);
- $s = str_replace("\n","<br>",$s);
- return $s;
- }
- function thumb($orig_path,$mw=100,$mh=100,$method='cut',$thumb=false,$png=false)
- {
- if ($method != 'box' && $method != 'cut') $method = 'cut';
- if (!$orig_path) return 'about:blank';
-
- if (preg_match('/^http\:\/\//i',$orig_path)) return $orig_path;
-
- $path = END_ROOT.$orig_path;
-
- $ftype = array_pop(explode('.',$path));
- $etag = basename($path).$method.$mw.'x'.$mh;
- $etag.= $png ? '.png':'.jpg';
- if (!file_exists($path)) return '';
-
- $thumb = dirname($path).'/'.$etag;
- if (file_exists($thumb)) return dirname($orig_path).'/'.$etag;
- if (!$imgarr=@getimagesize($path)) return '';
- $width_orig=$imgarr[0];
- $height_orig=$imgarr[1];
- $mime_orig=$imgarr["mime"];
- $mime=str_replace("image/","",$mime_orig);
- $mime=($mime=="bmp")?"wbmp":$mime;
- if (!function_exists("imagecreatefrom$mime")) return false;
- $p = $mw/$width_orig;
- $_p = $mh/$height_orig;
-
- if ($method == 'cut' && $_p>$p)
- {
- $p = $_p;
- $width = $mw;
- $height = $mh;
- $cut_height = 0;
- $cut_width = intval(($width_orig - $mw/$p)/2);
- }
- else if ($method == 'cut')
- {
- $width = $mw;
- $height = $mh;
- $cut_height = intval(($height_orig - $mh/$p)/2);
- $cut_width = 0;
- }
- else if ($method == 'box' && $_p<$p)
- {
- $p = $_p;
- if ($p > 1) $p = 1;
- $width = $p*$width_orig;
- $height = $p*$height_orig;
- $cut_height = 0;
- $cut_width = 0;
- }
- else if ($method == 'box')
- {
- if ($p > 1) $p = 1;
- $width = $p*$width_orig;
- $height = $p*$height_orig;
- $cut_height = 0;
- $cut_width = 0;
- }
-
- $image_p = @imagecreatetruecolor($width, $height);
- $_func = 'imagecreatefrom'.$mime;
- $image = @$_func($path);
- if ($png)
- {
- imagealphablending($image_p,true);
- $tcolor = imagecolortransparent($image_p, imagecolorallocatealpha($image_p, 0, 0, 0,127));
- imagefill($image_p, 0, 0, $tcolor);
- imagesavealpha($image_p, true);
- }
- @imagecopyresampled($image_p, $image, 0, 0, $cut_width, $cut_height, $width, $height, $width/$p, $height/$p);
- $_func = $png?'imagepng':'imagejpeg';
- $_func($image_p,$thumb,90);
- return (file_exists($thumb))?dirname($orig_path).'/'.$etag:false;
- }