url.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. class url{
  4. private $urlrules,$categorys,$html_root;
  5. public function __construct() {
  6. $this->urlrules = getcache('urlrules','commons');
  7. self::set_siteid();
  8. $this->categorys = getcache('category_content_'.$this->siteid,'commons');
  9. $this->html_root = pc_base::load_config('system','html_root');
  10. }
  11. /**
  12. * 内容页链接
  13. * @param $id 内容id
  14. * @param $page 当前页
  15. * @param $catid 栏目id
  16. * @param $time 添加时间
  17. * @param $prefix 前缀
  18. * @param $data 数据
  19. * @param $action 操作方法
  20. * @param $upgrade 是否是升级数据
  21. * @return array 0=>url , 1=>生成路径
  22. */
  23. public function show($id, $page = 0, $catid = 0, $time = 0, $prefix = '',$data = '',$action = 'edit',$upgrade = 0) {
  24. $page = max($page,1);
  25. $urls = $catdir = '';
  26. $category = $this->categorys[$catid];
  27. $setting = string2array($category['setting']);
  28. $content_ishtml = $setting['content_ishtml'];
  29. //当内容为转换或升级时
  30. if($upgrade || (isset($_POST['upgrade']) && defined('IN_ADMIN') && $_POST['upgrade'])) {
  31. if($_POST['upgrade']) $upgrade = $_POST['upgrade'];
  32. $upgrade = '/'.ltrim($upgrade,WEB_PATH);
  33. if($page==1) {
  34. $url_arr[0] = $url_arr[1] = $upgrade;
  35. } else {
  36. $lasttext = strrchr($upgrade,'.');
  37. $len = -strlen($lasttext);
  38. $path = substr($upgrade,0,$len);
  39. $url_arr[0] = $url_arr[1] = $path.'_'.$page.$lasttext;
  40. }
  41. } else {
  42. $show_ruleid = $setting['show_ruleid'];
  43. $urlrules = $this->urlrules[$show_ruleid];
  44. if(!$time) $time = SYS_TIME;
  45. $urlrules_arr = explode('|',$urlrules);
  46. if($page==1) {
  47. $urlrule = $urlrules_arr[0];
  48. } else {
  49. $urlrule = isset($urlrules_arr[1]) ? $urlrules_arr[1] : $urlrules_arr[0];
  50. }
  51. $domain_dir = '';
  52. if (strpos($category['url'], '://')!==false && strpos($category['url'], '?')===false) {
  53. if (preg_match('/^((http|https):\/\/)?([^\/]+)/i', $category['url'], $matches)) {
  54. $match_url = $matches[0];
  55. $url = $match_url.'/';
  56. }
  57. $db = pc_base::load_model('category_model');
  58. $r = $db->get_one(array('url'=>$url), '`catid`');
  59. if($r) $domain_dir = $this->get_categorydir($r['catid']).$this->categorys[$r['catid']]['catdir'].'/';
  60. }
  61. $categorydir = $this->get_categorydir($catid);
  62. $catdir = $category['catdir'];
  63. $year = date('Y',$time);
  64. $month = date('m',$time);
  65. $day = date('d',$time);
  66. $urls = str_replace(array('{$categorydir}','{$catdir}','{$year}','{$month}','{$day}','{$catid}','{$id}','{$page}'),array($categorydir,$catdir,$year,$month,$day,$catid,$id,$page),$urlrule);
  67. $create_to_html_root = $category['create_to_html_root'];
  68. if($create_to_html_root || $category['sethtml']) {
  69. $html_root = '';
  70. } else {
  71. $html_root = $this->html_root;
  72. }
  73. if($content_ishtml && $url) {
  74. if ($domain_dir && $category['isdomain']) {
  75. $url_arr[1] = $html_root.'/'.$domain_dir.$urls;
  76. $url_arr[0] = $url.$urls;
  77. } else {
  78. $url_arr[1] = $html_root.'/'.$urls;
  79. $url_arr[0] = WEB_PATH == '/' ? $match_url.$html_root.'/'.$urls : $match_url.rtrim(WEB_PATH,'/').$html_root.'/'.$urls;
  80. }
  81. } elseif($content_ishtml) {
  82. $url_arr[0] = WEB_PATH == '/' ? $html_root.'/'.$urls : rtrim(WEB_PATH,'/').$html_root.'/'.$urls;
  83. $url_arr[1] = $html_root.'/'.$urls;
  84. } else {
  85. $url_arr[0] = $url_arr[1] = APP_PATH.$urls;
  86. }
  87. }
  88. //生成静态 ,在添加文章的时候,同时生成静态,不在批量更新URL处调用
  89. if($content_ishtml && $data) {
  90. $data['id'] = $id;
  91. $url_arr['content_ishtml'] = 1;
  92. $url_arr['data'] = $data;
  93. }
  94. return $url_arr;
  95. }
  96. /**
  97. * 获取栏目的访问路径
  98. * 在修复栏目路径处重建目录结构用
  99. * @param intval $catid 栏目ID
  100. * @param intval $page 页数
  101. */
  102. public function category_url($catid, $page = 1) {
  103. $category = $this->categorys[$catid];
  104. if($category['type']==2) return $category['url'];
  105. $page = max(intval($page), 1);
  106. $setting = string2array($category['setting']);
  107. $category_ruleid = $setting['category_ruleid'];
  108. $urlrules = $this->urlrules[$category_ruleid];
  109. $urlrules_arr = explode('|',$urlrules);
  110. if ($page==1) {
  111. $urlrule = $urlrules_arr[0];
  112. } else {
  113. $urlrule = $urlrules_arr[1];
  114. }
  115. if (!$setting['ishtml']) { //如果不生成静态
  116. $url = str_replace(array('{$catid}', '{$page}'), array($catid, $page), $urlrule);
  117. if (strpos($url, '\\')!==false) {
  118. $url = APP_PATH.str_replace('\\', '/', $url);
  119. }
  120. } else { //生成静态
  121. if ($category['arrparentid']) {
  122. $parentids = explode(',', $category['arrparentid']);
  123. }
  124. $parentids[] = $catid;
  125. $domain_dir = '';
  126. foreach ($parentids as $pid) { //循环查询父栏目是否设置了二级域名
  127. $r = $this->categorys[$pid];
  128. if (strpos(strtolower($r['url']), '://')!==false && strpos($r['url'], '?')===false) {
  129. $r['url'] = preg_replace('/([(http|https):\/\/]{0,})([^\/]*)([\/]{1,})/i', '$1$2/', $r['url'], -1); //取消掉双'/'情况
  130. if (substr_count($r['url'], '/')==3 && substr($r['url'],-1,1)=='/') { //如果url中包含‘http://’并且‘/’在3个则为二级域名设置栏目
  131. $url = $r['url'];
  132. $domain_dir = $this->get_categorydir($pid).$this->categorys[$pid]['catdir'].'/'; //得到二级域名的目录
  133. }
  134. }
  135. }
  136. $category_dir = $this->get_categorydir($catid);
  137. $urls = str_replace(array('{$categorydir}','{$catdir}','{$catid}','{$page}'),array($category_dir,$category['catdir'],$catid,$page),$urlrule);
  138. if ($url && $domain_dir) { //如果存在设置二级域名的情况
  139. if (strpos($urls, $domain_dir)===0) {
  140. $url = str_replace(array($domain_dir, '\\'), array($url, '/'), $urls);
  141. } else {
  142. $urls = $domain_dir.$urls;
  143. $url = str_replace(array($domain_dir, '\\'), array($url, '/'), $urls);
  144. }
  145. } else { //不存在二级域名的情况
  146. $url = $urls;
  147. }
  148. }
  149. if (in_array(basename($url), array('index.html', 'index.htm', 'index.shtml'))) {
  150. $url = dirname($url).'/';
  151. }
  152. if (strpos($url, '://')===false) $url = str_replace('//', '/', $url);
  153. if(strpos($url, '/')===0) $url = substr($url,1);
  154. return $url;
  155. }
  156. /**
  157. * 生成列表页分页地址
  158. * @param $ruleid 角色id
  159. * @param $categorydir 父栏目路径
  160. * @param $catdir 栏目路径
  161. * @param $catid 栏目id
  162. * @param $page 当前页
  163. */
  164. public function get_list_url($ruleid,$categorydir, $catdir, $catid, $page = 1) {
  165. $urlrules = $this->urlrules[$ruleid];
  166. $urlrules_arr = explode('|',$urlrules);
  167. if ($page==1) {
  168. $urlrule = $urlrules_arr[0];
  169. } else {
  170. $urlrule = $urlrules_arr[1];
  171. }
  172. $urls = str_replace(array('{$categorydir}','{$catdir}','{$year}','{$month}','{$day}','{$catid}','{$page}'),array($categorydir,$catdir,$year,$month,$day,$catid,$page),$urlrule);
  173. return $urls;
  174. }
  175. /**
  176. * 获取父栏目路径
  177. * @param $catid
  178. * @param $dir
  179. */
  180. private function get_categorydir($catid, $dir = '') {
  181. $setting = array();
  182. $setting = string2array($this->categorys[$catid]['setting']);
  183. if ($setting['create_to_html_root']) return $dir;
  184. if ($this->categorys[$catid]['parentid']) {
  185. $dir = $this->categorys[$this->categorys[$catid]['parentid']]['catdir'].'/'.$dir;
  186. return $this->get_categorydir($this->categorys[$catid]['parentid'], $dir);
  187. } else {
  188. return $dir;
  189. }
  190. }
  191. /**
  192. * 设置当前站点
  193. */
  194. private function set_siteid() {
  195. if(defined('IN_ADMIN')) {
  196. $this->siteid = get_siteid();
  197. } else {
  198. if (param::get_cookie('siteid')) {
  199. $this->siteid = param::get_cookie('siteid');
  200. } else {
  201. $this->siteid = 1;
  202. }
  203. }
  204. }
  205. }