心理咨询网
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CmsController.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2018年4月20日
  7. * CMS通用接口控制器
  8. */
  9. namespace app\api\controller;
  10. use core\basic\Controller;
  11. use app\api\model\CmsModel;
  12. use core\basic\Url;
  13. use app\home\controller\ParserController;
  14. class CmsController extends Controller
  15. {
  16. protected $model;
  17. protected $lg;
  18. public function __construct()
  19. {
  20. $this->model = new CmsModel();
  21. $this->lg = get_default_lg();
  22. }
  23. // 站点基础信息
  24. public function site()
  25. {
  26. // 获取参数
  27. $acode = request('acode', 'var') ?: $this->lg;
  28. // 读取数据
  29. if (! $name = request('name', 'var')) {
  30. $data = $this->model->getSiteAll($acode);
  31. } else {
  32. $data = $this->model->getSite($acode, $name);
  33. }
  34. // 输出数据
  35. json(1, $data);
  36. }
  37. // 公司信息
  38. public function company()
  39. {
  40. // 获取参数
  41. $acode = request('acode', 'var') ?: $this->lg;
  42. // 读取数据
  43. if (! $name = request('name', 'var')) {
  44. $data = $this->model->getCompanyAll($acode);
  45. } else {
  46. $data = $this->model->getCompany($acode, $name);
  47. }
  48. // 输出数据
  49. json(1, $data);
  50. }
  51. // 自定义标签信息
  52. public function label()
  53. {
  54. // 获取全部或指定自定义标签
  55. if (! $name = request('name', 'var')) {
  56. $data = $this->model->getLabelAll();
  57. } else {
  58. $data = $this->model->getLabel($name);
  59. }
  60. // 输出数据
  61. json(1, $data);
  62. }
  63. // 获取菜单栏目树
  64. public function nav()
  65. {
  66. // 获取参数
  67. $acode = request('acode', 'var') ?: $this->lg;
  68. // 获取栏目树
  69. if (! $scode = request('scode', 'var')) {
  70. $data = $this->model->getSorts($acode);
  71. } else { // 获取子类
  72. $data = $this->model->getSortsSon($acode, $scode);
  73. }
  74. // 输出数据
  75. json(1, $data);
  76. }
  77. // 当前栏目位置
  78. public function position()
  79. {
  80. // 获取参数
  81. $acode = request('acode', 'var') ?: $this->lg;
  82. if (! ! $scode = request('scode', 'var')) {
  83. $data = $this->model->getPosition($acode, $scode);
  84. json(1, $data);
  85. } else {
  86. json(0, '必须传递当前分类scode参数');
  87. }
  88. }
  89. // 分类信息
  90. public function sort()
  91. {
  92. // 获取参数
  93. $acode = request('acode', 'var') ?: $this->lg;
  94. if (! ! $scode = request('scode', 'var')) {
  95. $data = $this->model->getSort($acode, $scode);
  96. json(1, $data);
  97. } else {
  98. json(0, '必须传递分类scode参数');
  99. }
  100. }
  101. // 内容多图
  102. public function pics()
  103. {
  104. if (! ! $id = request('id', 'int')) {
  105. $acode = request('acode', 'var') ?: $this->lg;
  106. if (! ! $pics = $this->model->getContentPics($acode, $id)) {
  107. $pics = explode(',', $pics);
  108. } else {
  109. $pics = array();
  110. }
  111. json(1, $pics);
  112. } else {
  113. json(0, '必须传递内容id参数');
  114. }
  115. }
  116. // 幻灯片
  117. public function slide()
  118. {
  119. if (! ! $gid = request('gid', 'var')) {
  120. $acode = request('acode', 'var') ?: $this->lg;
  121. $num = request('num', 'int') ?: 10;
  122. $data = $this->model->getSlides($acode, $gid, $num);
  123. json(1, $data);
  124. } else {
  125. json(0, '必须传递幻灯片分组gid参数');
  126. }
  127. }
  128. // 友情链接
  129. public function link()
  130. {
  131. if (! ! $gid = request('gid', 'var')) {
  132. $acode = request('acode', 'var') ?: $this->lg;
  133. $num = request('num', 'int') ?: 20;
  134. $data = $this->model->getLinks($acode, $gid, $num);
  135. json(1, $data);
  136. } else {
  137. json(0, '必须传递友情链接分组gid参数');
  138. }
  139. }
  140. // 搜索
  141. public function search()
  142. {
  143. if (! $_POST) {
  144. json(0, '请使用POST提交!');
  145. }
  146. $acode = request('acode', 'var') ?: $this->lg;
  147. // 获取主要参数
  148. $field = request('field');
  149. if (! preg_match('/^[\w\|]+$/', $field)) {
  150. $field = '';
  151. }
  152. $keyword = request('keyword', 'vars');
  153. $scode = request('scode'); // 支持多个分类逗号隔开
  154. if (! preg_match('/^[\w,\s]+$/', $scode)) {
  155. $scode = '';
  156. }
  157. if ($scode == '*') { // 星号意味任意栏目
  158. $scode = '';
  159. }
  160. $num = request('num', 'int') ?: $this->config('pagesize');
  161. $rorder = request('order');
  162. $tags = request('tags', 'vars');
  163. $fuzzy = request('fuzzy', 'int') ?: true;
  164. if (! preg_match('/^[\w\-,\s]+$/', $rorder)) {
  165. $order = 'a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  166. } else {
  167. switch ($rorder) {
  168. case 'id':
  169. $order = 'a.id DESC,a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.date DESC';
  170. break;
  171. case 'date':
  172. $order = 'a.date DESC,a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.id DESC';
  173. break;
  174. case 'sorting':
  175. $order = 'a.sorting ASC,a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.date DESC,a.id DESC';
  176. break;
  177. case 'istop':
  178. $order = 'a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  179. break;
  180. case 'isrecommend':
  181. $order = 'a.isrecommend DESC,a.istop DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  182. break;
  183. case 'isheadline':
  184. $order = 'a.isrecommend DESC,a.istop DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  185. break;
  186. case 'visits':
  187. case 'likes':
  188. case 'oppose':
  189. $order = $rorder . ' DESC,a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  190. break;
  191. case 'random': // 随机取数
  192. $db_type = get_db_type();
  193. if ($db_type == 'mysql') {
  194. $order = "RAND()";
  195. } elseif ($db_type == 'sqlite') {
  196. $order = "RANDOM()";
  197. }
  198. break;
  199. default:
  200. if ($rorder) {
  201. $orders = explode(',', $rorder);
  202. foreach ($orders as $k => $v) {
  203. if (strpos($v, 'ext_') === 0) {
  204. $orders[$k] = 'e.' . $v;
  205. } else {
  206. $orders[$k] = 'a.' . $v;
  207. }
  208. }
  209. $order = implode(',', $orders);
  210. $order .= ',a.istop DESC,a.isrecommend DESC,a.isheadline DESC,a.sorting ASC,a.date DESC,a.id DESC';
  211. }
  212. }
  213. }
  214. $where1 = array();
  215. // tags数据筛选
  216. $where2 = array();
  217. if ($tags) {
  218. $tags_arr = explode(',', $tags);
  219. foreach ($tags_arr as $value) {
  220. if ($value) {
  221. if ($fuzzy) {
  222. $where2[] = "a.tags like '%" . escape_string($value) . "%'";
  223. } else {
  224. $where2[] = "a.tags='" . escape_string($value) . "'";
  225. }
  226. }
  227. }
  228. }
  229. // 存储搜索条件,条件为“并列”关系,由于为模糊匹配,条件为空时意味着“任意”
  230. $where3 = array();
  231. // 采取keyword方式
  232. if ($keyword) {
  233. if (strpos($field, '|')) { // 匹配多字段的关键字搜索
  234. $field = explode('|', $field);
  235. foreach ($field as $value) {
  236. if ($value == 'title') {
  237. $value = 'a.title';
  238. }
  239. if ($fuzzy) {
  240. $like = " like '%" . $keyword . "%'"; // 前面已经转义过
  241. } else {
  242. $like = " like '" . $keyword . "'"; // 前面已经转义过
  243. }
  244. if (isset($where3[0])) {
  245. $where3[0] .= ' OR ' . $value . $like;
  246. } else {
  247. $where3[0] = $value . $like;
  248. }
  249. }
  250. if (count($field) > 1) {
  251. $where3[0] = '(' . $where3[0] . ')';
  252. }
  253. } else { // 匹配单一字段的关键字搜索
  254. if ($field) {
  255. if ($field == 'title') {
  256. $field = 'a.title';
  257. }
  258. $where3[$field] = $keyword;
  259. } else {
  260. $where3['a.title'] = $keyword;
  261. }
  262. }
  263. }
  264. // 数据接收
  265. if ($_POST) {
  266. $receive = $_POST;
  267. } else {
  268. $receive = $_GET;
  269. }
  270. foreach ($receive as $key => $value) {
  271. if (! ! $value = request($key, 'vars')) {
  272. if ($key == 'title') {
  273. $key = 'a.title';
  274. }
  275. if (preg_match('/^[\w\-\.]+$/', $key)) { // 带有违规字符时不带入查询
  276. $where3[$key] = $value;
  277. }
  278. }
  279. }
  280. // 去除特殊键值
  281. unset($where3['appid']);
  282. unset($where3['timestamp']);
  283. unset($where3['signature']);
  284. unset($where3['keyword']);
  285. unset($where3['field']);
  286. unset($where3['scode']);
  287. unset($where3['page']);
  288. unset($where3['from']);
  289. unset($where3['isappinstalled']);
  290. unset($where3['tdsourcetag']);
  291. unset($where3['x']);
  292. unset($where3['y']);
  293. unset($where3['searchtpl']);
  294. unset($where3['p']);
  295. unset($where3['s']);
  296. // 读取数据
  297. $data = $this->model->getLists($acode, $scode, $num, $order, $where1, $where2, $where3, $fuzzy);
  298. $Parser = new ParserController();
  299. foreach ($data as $key => $value) {
  300. if ($value->outlink) {
  301. $data[$key]->apilink = $value->outlink;
  302. } else {
  303. $data[$key]->apilink = url('/api/content/index/scode/' . $value->id, false);
  304. }
  305. $data[$key]->likeslink = url('/home/Do/likes/id/' . $value->id, false);
  306. $data[$key]->opposelink = url('/home/Do/oppose/id/' . $value->id, false);
  307. $data[$key]->content = str_replace(STATIC_DIR . '/upload/', get_http_url() . STATIC_DIR . '/upload/', $value->content);
  308. // 返回网页链接地址,便于AJAX调用内容
  309. $data[$key]->contentlink = $Parser->parserLink(2, $value->urlname, 'content', $value->scode, $value->sortfilename, $value->id, $value->filename);
  310. }
  311. // 输出数据
  312. if (request('page') <= PAGECOUNT) {
  313. json(1, $data);
  314. } else {
  315. return json(0, '已经到底了!');
  316. }
  317. }
  318. // 读取留言记录
  319. public function msg()
  320. {
  321. // 获取参数
  322. $acode = request('acode', 'var') ?: $this->lg;
  323. $num = request('num', 'int') ?: $this->config('pagesize');
  324. // 获取栏目数
  325. $data = $this->model->getMessage($acode, $num);
  326. if (request('page') <= PAGECOUNT) {
  327. json(1, $data);
  328. } else {
  329. return json(0, '已经到底了!');
  330. }
  331. }
  332. // 新增留言
  333. public function addmsg()
  334. {
  335. if ($_POST) {
  336. if ($this->config('message_status') === '0') {
  337. json(0, '系统已经关闭留言功能,请到后台开启再试!');
  338. }
  339. // 读取字段
  340. if (! $form = $this->model->getFormField(1)) {
  341. json(0, '接收表单不存在任何字段,请核对后重试!');
  342. }
  343. // 接收数据
  344. $mail_body = '';
  345. foreach ($form as $value) {
  346. $field_data = post($value->name);
  347. if (is_array($field_data)) { // 如果是多选等情况时转换
  348. $field_data = implode(',', $field_data);
  349. }
  350. $field_data = str_replace('pboot:if', '', $field_data);
  351. if ($value->required && ! $field_data) {
  352. json(0, $value->description . '不能为空!');
  353. } else {
  354. $data[$value->name] = $field_data;
  355. $mail_body .= $value->description . ':' . $field_data . '<br>';
  356. }
  357. }
  358. $status = $this->config('message_verify') == '0' ? 1 : 0;
  359. // 设置其他字段
  360. if ($data) {
  361. $data['acode'] = request('acode', 'var') ?: $this->lg;
  362. $data['user_ip'] = ip2long(get_user_ip());
  363. $data['user_os'] = get_user_os();
  364. $data['user_bs'] = get_user_bs();
  365. $data['recontent'] = '';
  366. $data['status'] = $status;
  367. $data['create_user'] = 'api';
  368. $data['update_user'] = 'api';
  369. }
  370. // 写入数据
  371. if ($this->model->addMessage($value->table_name, $data)) {
  372. $this->log('API提交留言数据成功!');
  373. if ($this->config('message_send_mail') && $this->config('message_send_to')) {
  374. $mail_subject = "【" . CMSNAME . "】您有新的" . $value->form_name . "信息,请注意查收!";
  375. $mail_body .= '<br>来自网站' . get_http_url() . '(' . date('Y-m-d H:i:s') . ')';
  376. sendmail($this->config(), $this->config('message_send_to'), $mail_subject, $mail_body);
  377. }
  378. json(1, '留言提交成功!');
  379. } else {
  380. $this->log('API提交留言数据失败!');
  381. json(0, '留言提交失败!');
  382. }
  383. } else {
  384. json(0, '留言提交失败,请使用POST方式提交!');
  385. }
  386. }
  387. // 表单记录
  388. public function form()
  389. {
  390. // 获取参数
  391. $num = request('num', 'int') ?: $this->config('pagesize');
  392. // 获取表单编码
  393. if (! $fcode = request('fcode', 'var'))
  394. json(0, '必须传递表单编码fcode');
  395. // 获取表名称
  396. if (! $table = $this->model->getFormTable($fcode)) {
  397. json(0, '传递的fcode有误');
  398. }
  399. // 获取表数据
  400. $data = $this->model->getForm($table, $num);
  401. if (request('page') <= PAGECOUNT) {
  402. json(1, $data);
  403. } else {
  404. return json(0, '已经到底了!');
  405. }
  406. }
  407. // 表单提交
  408. public function addform()
  409. {
  410. if ($_POST) {
  411. if (! $fcode = request('fcode', 'var')) {
  412. json(0, '传递的表单编码fcode有误!');
  413. }
  414. if ($this->config('form_status') === '0') {
  415. json(0, '系统已经关闭表单功能,请到后台开启再试!');
  416. }
  417. // 读取字段
  418. if (! $form = $this->model->getFormField($fcode)) {
  419. json(0, '接收表单不存在任何字段,请核对后重试!');
  420. }
  421. // 接收数据
  422. $mail_body = '';
  423. foreach ($form as $value) {
  424. $field_data = post($value->name);
  425. if (is_array($field_data)) { // 如果是多选等情况时转换
  426. $field_data = implode(',', $field_data);
  427. }
  428. $field_data = str_replace('pboot:if', '', $field_data);
  429. if ($value->required && ! $field_data) {
  430. json(0, $value->description . '不能为空!');
  431. } else {
  432. $data[$value->name] = $field_data;
  433. $mail_body .= $value->description . ':' . $field_data . '<br>';
  434. }
  435. }
  436. // 设置创建时间
  437. if ($data) {
  438. $data['create_time'] = get_datetime();
  439. }
  440. // 写入数据
  441. if ($this->model->addForm($value->table_name, $data)) {
  442. $this->log('API提交表单数据成功!');
  443. if ($this->config('form_send_mail') && $this->config('message_send_to')) {
  444. $mail_subject = "【" . CMSNAME . "】您有新的" . $value->form_name . "信息,请注意查收!";
  445. $mail_body .= '<br>来自网站' . get_http_url() . '(' . date('Y-m-d H:i:s') . ')';
  446. sendmail($this->config(), $this->config('message_send_to'), $mail_subject, $mail_body);
  447. }
  448. json(1, '表单提交成功!');
  449. } else {
  450. $this->log('API提交表单数据失败!');
  451. json(0, '表单提交失败!');
  452. }
  453. } else {
  454. json(0, '表单提交失败,请使用POST方式提交!');
  455. }
  456. }
  457. // 空拦截
  458. public function _empty()
  459. {
  460. json(0, '您调用的接口不存在,请核对后重试!');
  461. }
  462. }