PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/2010/end_admin/helper/common.php

http://endcms.googlecode.com/
PHP | 360 lines | 283 code | 46 blank | 31 comment | 56 complexity | da4bb589db6d9cf17087b1786ff97d92 MD5 | raw file
  1. <?php
  2. function check_show($action)
  3. {
  4. $controller = defined('ITEM_TYPE')?ITEM_TYPE:END_CONTROLLER;
  5. return $_SESSION['login_user']['rights'][$controller.'_'.$action];
  6. }
  7. /*
  8. check if the action is allowed
  9. */
  10. function check_allowed($controller,$action,$text = false)
  11. {
  12. if (END_MODULE != 'admin') return true;
  13. if (!$action)
  14. $action = $controller;
  15. else
  16. $action = $controller.'_'.$action;
  17. if (!$_SESSION['login_user']['rights'][$action])
  18. {
  19. if ($text)
  20. {
  21. echo LANG_NOT_ALLOWED;
  22. die;
  23. }
  24. else
  25. end_exit(LANG_NOT_ALLOWED);
  26. }
  27. else return true;
  28. }
  29. function check_allowed_category($category_id,$text = false)
  30. {
  31. if (END_MODULE != 'admin') return true;
  32. if ($_SESSION['login_user']['limit_category_id'] && !$_SESSION['login_user']['rights']['categroy_'.$category_id])
  33. {
  34. if ($text)
  35. {
  36. echo LANG_NOT_ALLOWED;
  37. die;
  38. }
  39. else
  40. end_exit(LANG_NOT_ALLOWED);
  41. }
  42. else return true;
  43. }
  44. /*
  45. output a msg box for $t seconds then redirect to $url
  46. */
  47. function end_exit($content,$url='javascript:history.go(-1);',$t = 2)
  48. {
  49. $temp = template('exit.html');
  50. $temp->assign( array
  51. (
  52. 'content' => $content,
  53. 'url' => $url,
  54. 'time' => $t,
  55. ));
  56. $temp->display();
  57. die;
  58. }
  59. /*
  60. get a simple formated date string
  61. */
  62. function format_date($t)
  63. {
  64. if (strpos($t,'-') !== false) return $t;
  65. return date('Y-m-d H:i',$t);
  66. }
  67. function show_day($t)
  68. {
  69. if (strpos($t,'-') !== false) return $t;
  70. return date('m-d',$t);
  71. }
  72. /*
  73. get a short type date string
  74. e.g. today 21:23 (when it s the same day)
  75. yesterday 21:23 (when it's yesterday)
  76. */
  77. function format_date_short($t)
  78. {
  79. if (date('Y',$t) != date('Y'))
  80. {
  81. $re = date('Y'.LANG_YEAR.'m'.LANG_MONTH.'d'.LANG_DAY,$t);
  82. }
  83. else if (date('m',$t) != date('m'))
  84. {
  85. $re = date('m'.LANG_MONTH.'d'.LANG_DAY,$t);
  86. }
  87. else if (date('d',$t) != date('d'))
  88. {
  89. switch( date('d',$t) - date('d') )
  90. {
  91. case 2:
  92. $re = LANG_TDAT;
  93. break;
  94. case 1:
  95. $re = LANG_TOMORROW;
  96. break;
  97. case -1:
  98. $re = LANG_YESTERDAY;
  99. break;
  100. default:
  101. $re = date('d'.LANG_DAY,$t);
  102. }
  103. }
  104. else
  105. {
  106. $re = LANG_TODAY;
  107. }
  108. $re .= date('H:i',$t);
  109. return $re;
  110. }
  111. /*
  112. check if certain action is allowed or not
  113. */
  114. function if_allowed($action)
  115. {
  116. if ($_SESSION['login_user']['name'] == 'Administrator' || $_SESSION['login_user']['rights'] == 'all')
  117. {
  118. return true;
  119. }
  120. else
  121. {
  122. return (strpos(','.$_SESSION['login_user']['rights'].',',','.$action.',') !== false);
  123. }
  124. }
  125. /*
  126. get user name with user_id
  127. */
  128. function get_user_name($id)
  129. {
  130. global $end_users;
  131. if (!is_array($end_users))
  132. {
  133. include_once('model/user.php');
  134. $user = new END_User;
  135. $end_users = $user->get_array();
  136. }
  137. return $end_users[$id];
  138. }
  139. /*
  140. separate description and whole content
  141. */
  142. function end_separate($s,$url)
  143. {
  144. global $config;
  145. if (strpos($s,$config['end_separator']) === false) return $s;
  146. $arr = explode($config['end_separator'],$s);
  147. return $arr[0].'...<a href="'.$url.'" class="readmore">'.$config['end_readmore'].'</a>';
  148. }
  149. function getext($filename)
  150. {
  151. $filename = trim(strtolower(basename($filename)));
  152. $arr = explode('.',$filename);
  153. $type = $arr[count($arr)-1];
  154. return $type;
  155. }
  156. function myurlencode($url)
  157. {
  158. return str_replace( array('+','%2F'),array('%20','/'),urlencode($url));
  159. }
  160. function thumb_png($orig_path,$mw=100,$mh=100,$thumb=false,$method='fill')
  161. {
  162. return thumb($orig_path,$mw,$mh,$thumb,$method,true);
  163. }
  164. function thumb($orig_path,$mw=100,$mh=100,$thumb=false,$method='fill',$png=false)
  165. {
  166. if (!$orig_path) return 'about:blank';
  167. $path = END_ROOT.$orig_path;
  168. $ftype = array_pop(explode('.',$path));
  169. $etag = basename($path).$mw.'x'.$mh;
  170. $etag.= $png ? '.png':'.jpg';
  171. if (!file_exists($path)) return '';
  172. if ($thumb === false)
  173. $thumb = dirname($path).'/'.$etag;
  174. if (file_exists($thumb)) return dirname($orig_path).'/'.$etag;
  175. if (!$imgarr=@getimagesize($path)) return '';
  176. $width_orig=$imgarr[0];
  177. $height_orig=$imgarr[1];
  178. $mime_orig=$imgarr["mime"];
  179. $mime=str_replace("image/","",$mime_orig);
  180. $mime=($mime=="bmp")?"wbmp":$mime;
  181. if (!function_exists("imagecreatefrom$mime")) return false;
  182. $p = $mw/$width_orig;
  183. $_p = $mh/$height_orig;
  184. if ($_p == 1 && $p == 1) //如果尺寸相同
  185. {
  186. return $orig_path;
  187. }
  188. if ($_p>$p)
  189. {
  190. $p = $_p;
  191. $width = $p*$width_orig;
  192. $height = $p*$height_orig;
  193. $cut_height = 0;
  194. $cut_width = intval(($width_orig - $mw/$p)/2);
  195. }
  196. else
  197. {
  198. $width = $p*$width_orig;
  199. $height = $p*$height_orig;
  200. $cut_height = intval(($height_orig - $mh/$p)/2);;
  201. $cut_width = 0;
  202. }
  203. $width = $mw;
  204. $height = $mh;
  205. $image_p = @imagecreatetruecolor($width, $height);
  206. $_func = 'imagecreatefrom'.$mime;
  207. $image = @$_func($path);
  208. if ($png) //保存透明
  209. {
  210. imagealphablending($image_p,true);
  211. $tcolor = imagecolortransparent($image_p, imagecolorallocatealpha($image_p, 0, 0, 0,127));
  212. imagefill($image_p, 0, 0, $tcolor);
  213. imagesavealpha($image_p, true);
  214. }
  215. @imagecopyresampled($image_p, $image, 0, 0, $cut_width, $cut_height, $width, $height, $mw/$p, $mh/$p);
  216. $_func = $png?'imagepng':'imagejpeg';
  217. imagepng($image_p,$thumb);
  218. return (file_exists($thumb))?dirname($orig_path).'/'.$etag:false;
  219. }
  220. /*
  221. if the client browser is IE
  222. */
  223. function is_ie()
  224. {
  225. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  226. if((strpos($useragent, 'opera') !== false) || (strpos($useragent, 'konqueror') !== false)) return false;
  227. if(strpos($useragent, 'msie ') !== false) return true;
  228. return false;
  229. }
  230. function load_models()
  231. {
  232. global $end_models,$end_rights;
  233. $_h = opendir(END_ROOT);
  234. while($v = readdir($_h))
  235. {
  236. if (is_dir(END_ROOT.$v)
  237. && preg_match('/^end_/',$v)
  238. && is_dir($mdir = END_ROOT.$v.'/model/')
  239. && $__h = opendir($mdir) )
  240. {
  241. while($v = readdir($__h))
  242. {
  243. if (is_dir($mdir.$v) && file_exists($mfile = $mdir.$v.'/'.$v.'.config.php'))
  244. {
  245. include_once($mfile);
  246. $end_models[$v]['model_path'] = $mdir.$v.'/';
  247. //define('LANG_RIGHTS_'.strtoupper($v),$end_models[$v]['name']);
  248. }
  249. }
  250. closedir($__h);
  251. }
  252. }
  253. closedir($_h);
  254. $_models = array();
  255. foreach($end_models as $key=>$arr)
  256. {
  257. if ($arr['type'] == 'list') $key.= '_list';
  258. $_models[$key] = $arr;
  259. }
  260. $end_models = $_models;
  261. }
  262. function end_show_view_button($id,$s=LANG_VIEW)
  263. {
  264. echo ' <a href="admin.php?p=item&action=view_item&category_id='.END_ADMIN_CATEGORY_ID.'&item_id='.$id.'">'.$s.'</a> ';
  265. }
  266. function end_show_edit_button($id,$s=LANG_EDIT)
  267. {
  268. echo ' <a href="admin.php?p=item&action=edit_item&category_id='.END_ADMIN_CATEGORY_ID.'&item_id='.$id.'">'.$s.'</a> ';
  269. }
  270. function end_show_delete_button($id,$s = LANG_DELETE)
  271. {
  272. echo ' <a href="javascript:;//'.$id.'" onclick="delete_item(\''.$id.'\',this)">'.$s.'</a> ';
  273. }
  274. function end_show_update_status_button($id,$status,$m)
  275. {
  276. echo ' <a href="javascript:;" onclick="change_status(\''.$id.'\',\''.$status.'\',this)">'.$m.'</a> ';
  277. }
  278. function get_admin_id()
  279. {
  280. return $_SESSION['login_user']['admin_id'];
  281. }
  282. /*
  283. 获得过去多久
  284. 比如 3秒 5小时 7天
  285. */
  286. function get_past_time($t,$second='秒钟',$minite='分钟',$hour='个小时',$day='天',$month='个月',$year='年')
  287. {
  288. $d = time()-$t;
  289. if ($d < 60)
  290. {
  291. return $d.$second;
  292. }
  293. $d = intval($d/60);
  294. if ($d < 60)
  295. {
  296. return $d.$minite;
  297. }
  298. $d = intval($d/60);
  299. if ($d < 24)
  300. {
  301. return $d.$hour;
  302. }
  303. $d = intval($d/24);
  304. if ($d < 30)
  305. {
  306. return $d.$day;
  307. }
  308. $d = intval($d/30);
  309. if ($d < 12)
  310. {
  311. return $d.$month;
  312. }
  313. return intval($d/12).$year;
  314. }