block_admin.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin', 'admin', 0);
  4. class block_admin extends admin {
  5. private $db, $siteid, $priv_db, $history_db, $roleid;
  6. public function __construct() {
  7. $this->db = pc_base::load_model('block_model');
  8. $this->priv_db = pc_base::load_model('block_priv_model');
  9. $this->history_db = pc_base::load_model('block_history_model');
  10. $this->roleid = $_SESSION['roleid'];
  11. $this->siteid = $this->get_siteid();
  12. parent::__construct();
  13. }
  14. public function init() {
  15. $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
  16. if ($_SESSION['roleid'] != 1) {
  17. $offset = ($page-1) * 20;
  18. $r = $this->priv_db->select(array('roleid'=>$this->roleid, 'siteid'=>$this->siteid),'blockid', $offset.',20');
  19. $blockid_list = array();
  20. foreach ($r as $key=>$v) {
  21. $blockid_list[$key] = $v['blockid'];
  22. }
  23. $sql = implode('\',\'', $blockid_list);
  24. $list = $this->db->listinfo("id in ('$sql')", '', $page, 20);
  25. } else {
  26. $list = $this->db->listinfo(array('siteid'=>$this->siteid), '', $page, 20);
  27. }
  28. $pages = $this->db->pages;
  29. include $this->admin_tpl('block_list');
  30. }
  31. public function add() {
  32. $pos = isset($_GET['pos']) && trim($_GET['pos']) ? trim($_GET['pos']) : showmessage(L('illegal_operation'));
  33. if (isset($_POST['dosubmit'])) {
  34. $name = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : showmessage(L('illegal_operation'), HTTP_REFERER);
  35. $type = isset($_POST['type']) && intval($_POST['type']) ? intval($_POST['type']) : 1;
  36. //判断名称是否已经存在
  37. if ($this->db->get_one(array('name'=>$name))) {
  38. showmessage(L('name').L('exists'), HTTP_REFERER);
  39. }
  40. if ($id = $this->db->insert(array('name'=>$name, 'pos'=>$pos, 'type'=>$type, 'siteid'=>$this->siteid), true)) {
  41. //设置权限
  42. $priv = isset($_POST['priv']) ? $_POST['priv'] : '';
  43. if (!empty($priv)) {
  44. if (is_array($priv)) foreach ($priv as $v) {
  45. if (empty($v)) continue;
  46. $this->priv_db->insert(array('roleid'=>$v, 'blockid'=>$id, 'siteid'=>$this->siteid));
  47. }
  48. }
  49. showmessage(L('operation_success'), '?m=block&c=block_admin&a=block_update&id='.$id);
  50. } else {
  51. showmessage(L('operation_failure'), HTTP_REFERER);
  52. }
  53. } else {
  54. $show_header = $show_validator = true;
  55. pc_base::load_sys_class('form');
  56. $administrator = getcache('role', 'commons');
  57. unset($administrator[1]);
  58. include $this->admin_tpl('block_add_edit');
  59. }
  60. }
  61. public function edit() {
  62. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : showmessage(L('illegal_operation'));
  63. if (!$data = $this->db->get_one(array('id'=>$id))) {
  64. showmessage(L('nofound'));
  65. }
  66. if (isset($_POST['dosubmit'])) {
  67. $name = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : showmessage(L('illegal_operation'), HTTP_REFERER);
  68. if ($data['name'] != $name) {
  69. if ($this->db->get_one(array('name'=>$name))) {
  70. showmessage(L('name').L('exists'), HTTP_REFERER);
  71. }
  72. }
  73. if ($this->db->update(array('name'=>$name, 'siteid'=>$this->siteid), array('id'=>$id))) {
  74. //设置权限
  75. $priv = isset($_POST['priv']) ? $_POST['priv'] : '';
  76. $this->priv_db->delete(array('blockid'=>$id, 'siteid'=>$this->siteid));
  77. if (!empty($priv)) {
  78. if (is_array($priv)) foreach ($priv as $v) {
  79. if (empty($v)) continue;
  80. $this->priv_db->insert(array('roleid'=>$v, 'blockid'=>$id, 'siteid'=>$this->siteid));
  81. }
  82. }
  83. showmessage(L('operation_success'), '', '' ,'edit');
  84. } else {
  85. showmessage(L('operation_failure'), HTTP_REFERER);
  86. }
  87. }
  88. $show_header = $show_validator = true;
  89. pc_base::load_sys_class('form');
  90. $administrator = getcache('role', 'commons');
  91. unset($administrator[1]);
  92. $r = $this->priv_db->select(array('blockid'=>$id, 'siteid'=>$this->siteid),'roleid');
  93. $priv_list = array();
  94. foreach ($r as $v) {
  95. if($v['roleid']) $priv_list[] = $v['roleid'];
  96. }
  97. include $this->admin_tpl('block_add_edit');
  98. }
  99. public function del() {
  100. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : showmessage(L('illegal_operation'));
  101. if (!$data = $this->db->get_one(array('id'=>$id))) {
  102. showmessage(L('nofound'));
  103. }
  104. if ($this->db->delete(array('id'=>$id)) && $this->history_db->delete(array('blockid'=>$id)) && $this->priv_db->delete(array('blockid'=>$id))) {
  105. if (pc_base::load_config('system','attachment_stat')) {
  106. $this->attachment_db = pc_base::load_model('attachment_model');
  107. $keyid = 'block-'.$id;
  108. $this->attachment_db->api_delete($keyid);
  109. }
  110. showmessage(L('operation_success'), HTTP_REFERER);
  111. } else {
  112. showmessage(L('operation_failure'), HTTP_REFERER);
  113. }
  114. }
  115. public function block_update() {
  116. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : showmessage(L('illegal_operation'), HTTP_REFERER);
  117. //进行权限判断
  118. if ($this->roleid != 1) {
  119. if (!$this->priv_db->get_one(array('blockid'=>$id, 'roleid'=>$this->roleid, 'siteid'=>$this->siteid))) {
  120. showmessage(L('not_have_permissions'));
  121. }
  122. }
  123. if (!$data = $this->db->get_one(array('id'=>$id))) {
  124. showmessage(L('nofound'));
  125. }
  126. if (isset($_POST['dosubmit'])) {
  127. $sql = array();
  128. if ($data['type'] == 2) {
  129. $title = isset($_POST['title']) ? $_POST['title'] : '';
  130. $url = isset($_POST['url']) ? $_POST['url'] : '';
  131. $thumb = isset($_POST['thumb']) ? $_POST['thumb'] : '';
  132. $desc = isset($_POST['desc']) ? $_POST['desc'] : '';
  133. $template = isset($_POST['template']) && trim($_POST['template']) ? trim($_POST['template']) : '';
  134. $datas = array();
  135. foreach ($title as $key=>$v) {
  136. if (empty($v) || !isset($url[$key]) ||empty($url[$key])) continue;
  137. $datas[$key] = array('title'=>$v, 'url'=>$url[$key], 'thumb'=>$thumb[$key], 'desc'=>str_replace(array(chr(13), chr(43)), array('<br />', '&nbsp;'), $desc[$key]));
  138. }
  139. if ($template) {
  140. $block = pc_base::load_app_class('block_tag');
  141. $block->template_url($id, $template);
  142. }
  143. if (is_array($thumb) && !empty($thumb)) {
  144. if(pc_base::load_config('system','attachment_stat')) {
  145. $this->attachment_db = pc_base::load_model('attachment_model');
  146. $this->attachment_db->api_update($thumb, 'block-'.$id, 1);
  147. }
  148. }
  149. $sql = array('data'=>array2string($datas), 'template'=>$template);
  150. } elseif ($data['type'] == 1) {
  151. $datas = isset($_POST['data']) && trim($_POST['data']) ? trim($_POST['data']) : '';
  152. $sql = array('data'=>$datas);
  153. }
  154. if ($this->db->update($sql, array('id'=>$id))) {
  155. //添加历史记录
  156. $this->history_db->insert(array('blockid'=>$data['id'], 'data'=>array2string($data), 'creat_at'=>SYS_TIME, 'userid'=>param::get_cookie('userid'), 'username'=>param::get_cookie('admin_username')));
  157. showmessage(L('operation_success').'<script style="text/javascript">if(!parent.right){parent.location.reload();}art.dialog({id:"edit"}).close();</script>', '','','edit');
  158. } else {
  159. showmessage(L('operation_failure'), HTTP_REFERER);
  160. }
  161. } else {
  162. if (!empty($data['data'])) {
  163. if ($data['type'] == 2) $data['data'] = string2array($data['data']);
  164. $total = count($data['data']);
  165. }
  166. pc_base::load_sys_class('form');
  167. pc_base::load_sys_class('format', '', 0);
  168. $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
  169. $history_list = $this->history_db->listinfo(array('blockid'=>$id), '', $page, 10);
  170. $pages = $this->history_db->pages;
  171. $show_header = $show_validator = $show_dialog = true;
  172. include $this->admin_tpl('block_update');
  173. }
  174. }
  175. public function public_visualization() {
  176. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  177. $catid = isset($_GET['catid']) && intval($_GET['catid']) ? intval($_GET['catid']) : 0;
  178. $type = isset($_GET['type']) && trim($_GET['type']) ? trim($_GET['type']) : 'list';
  179. $siteid = $GLOBALS['siteid'] = $this->get_siteid();
  180. if (!empty($catid)) {
  181. $CATEGORY = getcache('category_content_'.$siteid, 'commons');
  182. if (!isset($CATEGORY[$catid])) {
  183. showmessage(L('notfound'));
  184. }
  185. $cat = $CATEGORY[$catid];
  186. $cat['setting'] = string2array($cat['setting']);
  187. }
  188. if($cat['type']==2) showmessage(L('link_visualization_not_exists'));
  189. $file = '';
  190. $style = $cat['setting']['template_list'];
  191. switch ($type) {
  192. case 'category':
  193. if($cat['type']==1) {
  194. $file = $cat['setting']['page_template'];
  195. } else {
  196. $file = $cat['setting']['category_template'];
  197. }
  198. break;
  199. case 'list':
  200. if($cat['type']==1) {
  201. $file = $cat['setting']['page_template'];
  202. } else {
  203. $file = $cat['setting']['list_template'];
  204. }
  205. break;
  206. case 'show':
  207. $file = $cat['setting']['show_template'];
  208. break;
  209. case 'index':
  210. $sites = pc_base::load_app_class('sites', 'admin');
  211. $sites_info = $sites->get_by_id($this->siteid);
  212. $file = 'index';
  213. $style = $sites_info['default_style'];
  214. break;
  215. case 'page':
  216. $file = $cat['setting']['page_template'];
  217. break;
  218. }
  219. pc_base::load_app_func('global','template');
  220. ob_start();
  221. include template('content', $file, $style);
  222. $html = ob_get_contents();
  223. ob_clean();
  224. echo visualization($html, $style, 'content', $file.'.html');
  225. }
  226. public function public_view() {
  227. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : exit('0');
  228. if (!$data = $this->db->get_one(array('id'=>$id))) {
  229. showmessage(L('nofound'));
  230. }
  231. if ($data['type'] == 1) {
  232. exit('<script type="text/javascript">parent.showblock('.$id.', \''.str_replace("\r\n", '', $_POST['data']).'\')</script>');
  233. } elseif ($data['type'] == 2) {
  234. extract($data);
  235. unset($data);
  236. $title = isset($_POST['title']) ? $_POST['title'] : '';
  237. $url = isset($_POST['url']) ? $_POST['url'] : '';
  238. $thumb = isset($_POST['thumb']) ? $_POST['thumb'] : '';
  239. $desc = isset($_POST['desc']) ? $_POST['desc'] : '';
  240. $template = isset($_POST['template']) && trim($_POST['template']) ? trim($_POST['template']) : '';
  241. $data = array();
  242. foreach ($title as $key=>$v) {
  243. if (empty($v) || !isset($url[$key]) ||empty($url[$key])) continue;
  244. $data[$key] = array('title'=>$v, 'url'=>$url[$key], 'thumb'=>$thumb[$key], 'desc'=>str_replace(array(chr(13), chr(43)), array('<br />', '&nbsp;'), $desc[$key]));
  245. }
  246. $tpl = pc_base::load_sys_class('template_cache');
  247. $str = $tpl->template_parse(new_stripslashes($template));
  248. $filepath = CACHE_PATH.'caches_template'.DIRECTORY_SEPARATOR.'block'.DIRECTORY_SEPARATOR.'tmp_'.$id.'.php';
  249. $dir = dirname($filepath);
  250. if(!is_dir($dir)) {
  251. @mkdir($dir, 0777, true);
  252. }
  253. if (@file_put_contents($filepath,$str)) {
  254. ob_start();
  255. include $filepath;
  256. $html = ob_get_contents();
  257. ob_clean();
  258. @unlink($filepath);
  259. }
  260. exit('<script type="text/javascript">parent.showblock('.$id.', \''.str_replace("\r\n", '', $html).'\')</script>');
  261. }
  262. }
  263. public function public_name() {
  264. $name = isset($_GET['name']) && trim($_GET['name']) ? (pc_base::load_config('system', 'charset') == 'gbk' ? iconv('utf-8', 'gbk', trim($_GET['name'])) : trim($_GET['name'])) : exit('0');
  265. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : '';
  266. $name = safe_replace($name);
  267. $data = array();
  268. if ($id) {
  269. $data = $this->db->get_one(array('id'=>$id), 'name');
  270. if (!empty($data) && $data['name'] == $name) {
  271. exit('1');
  272. }
  273. }
  274. if ($this->db->get_one(array('name'=>$name), 'id')) {
  275. exit('0');
  276. } else {
  277. exit('1');
  278. }
  279. }
  280. public function history_restore() {
  281. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : showmessage(L('illegal_operation'), HTTP_REFERER);
  282. if (!$data = $this->history_db->get_one(array('id'=>$id))) {
  283. showmessage(L('nofound'), HTTP_REFERER);
  284. }
  285. $data['data'] = string2array($data['data']);
  286. $this->db->update(array('data'=>new_addslashes($data['data']['data']), 'template'=>new_addslashes($data['data']['template'])), array('id'=>$data['blockid']));
  287. if ($data['data']['type'] == 2) {
  288. $block = pc_base::load_app_class('block_tag');
  289. $block->template_url($data['blockid'], $data['data']['template']);
  290. }
  291. showmessage(L('operation_success'), HTTP_REFERER);
  292. }
  293. public function history_del() {
  294. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : showmessage(L('illegal_operation'), HTTP_REFERER);
  295. if (!$data = $this->history_db->get_one(array('id'=>$id))) {
  296. showmessage(L('nofound'), HTTP_REFERER);
  297. }
  298. $this->history_db->delete(array('id'=>$id));
  299. showmessage(L('operation_success'), HTTP_REFERER);
  300. }
  301. public function public_search_content() {
  302. $catid = isset($_GET['catid']) && intval($_GET['catid']) ? intval($_GET['catid']) : '';
  303. $posids = isset($_GET['posids']) && intval($_GET['posids']) ? intval($_GET['posids']) : 0;
  304. $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
  305. $searchtype = isset($_GET['searchtype']) && intval($_GET['searchtype']) ? intval($_GET['searchtype']) : 0;
  306. $end_time = isset($_GET['end_time']) && trim($_GET['end_time']) ? strtotime(trim($_GET['end_time'])) : '';
  307. $start_time = isset($_GET['start_time']) && trim($_GET['start_time']) ? strtotime(trim($_GET['start_time'])) : '';
  308. $keyword = isset($_GET['keyword']) && trim($_GET['keyword']) ? trim($_GET['keyword']) : '';
  309. if (isset($_GET['dosubmit']) && !empty($catid)) {
  310. if (!empty($start_time) && empty($end_time)) {
  311. $end_time = SYS_TIME;
  312. }
  313. if ($end_time < $start_time) {
  314. showmessage(L('end_of_time_to_time_to_less_than'));
  315. }
  316. if (!empty($end_time) && empty($start_time)) {
  317. showmessage(L('please_set_the_starting_time'));
  318. }
  319. $sql = "`catid` = '$catid' AND `posids` = '$posids'";
  320. if (!empty($start_time) && !empty($end_time)) $sql .= " AND `inputtime` BETWEEN '$start_time' AND '$end_time' ";
  321. if (!empty($searchtype) && !empty($keyword)) {
  322. switch ($searchtype) {
  323. case '1'://标题搜索
  324. $sql .= " AND `title` LIKE '%$keyword%' ";
  325. break;
  326. case '2'://简介搜索
  327. $sql .= " AND `description` LIKE '%$keyword%' ";
  328. break;
  329. case '3'://用户名
  330. $sql .= " AND `username` = '$keyword' ";
  331. break;
  332. case '4'://ID搜索
  333. $sql .= " AND `id` = '$keyword' ";
  334. break;
  335. }
  336. }
  337. $content_db = pc_base::load_model('content_model');
  338. $content_db->set_catid($catid);
  339. $data = $content_db->listinfo($sql, 'id desc', $page);
  340. $pages = $content_db->pages;
  341. }
  342. pc_base::load_sys_class('form');
  343. $show_header = $show_validator = $show_dialog = true;
  344. include $this->admin_tpl('search_content');
  345. }
  346. }