人人商城

cron.ctrl.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. $dos = array('list', 'post', 'del', 'run');
  8. $do = in_array($do, $dos) ? $do : 'list';
  9. if($do == 'post') {
  10. $id = intval($_GPC['id']);
  11. if(!empty($id)) {
  12. $cron = pdo_fetch('SELECT * FROM ' . tablename('cron') . ' WHERE cronid = :id', array(':id' => $id));
  13. if(empty($cron)) {
  14. message('任务不存在或已删除', '', 'error');
  15. }
  16. $cron['minute'] = str_replace("\t", ',', $cron['minute']);
  17. } else {
  18. $cron = array('weekday' => -1, 'day' => -1, 'hour' => -1);
  19. }
  20. if(checksubmit('submit')) {
  21. $data['name'] = trim($_GPC['name']) ? trim($_GPC['name']) : message('请填写任务名称', '', 'error');
  22. $data['filename'] = trim($_GPC['filename']) ? trim($_GPC['filename']) : message('请填写任务脚本文件名称', '', 'error');
  23. $data['available'] = intval($_GPC['available']);
  24. $data['day'] = intval($_GPC['weekday']) == -1 ? intval($_GPC['day']) : -1;
  25. $data['weekday'] = intval($_GPC['weekday']);
  26. $data['hour'] = intval($_GPC['hour']);
  27. $data['module'] = 'system';
  28. if(strpos($_GPC['minute'], ',') !== FALSE) {
  29. $minutenew = explode(',', $_GPC['minute']);
  30. foreach($minutenew as $key => $val) {
  31. $minutenew[$key] = $val = intval($val);
  32. if($val < 0 || $var > 59) {
  33. unset($minutenew[$key]);
  34. }
  35. }
  36. $minutenew = array_slice(array_unique($minutenew), 0, 12);
  37. $minutenew = implode("\t", $minutenew);
  38. } else {
  39. $minutenew = intval($_GPC['minute']);
  40. $minutenew = $minutenew >= 0 && $minutenew < 60 ? $minutenew : '';
  41. }
  42. $data['minute'] = $minutenew;
  43. if($id > 0) {
  44. pdo_update('cron', $data, array('cronid' => $id));
  45. message('编辑计划任务成功', url('system/cron'), 'success');
  46. } else {
  47. pdo_insert('cron', $data);
  48. message('添加计划任务成功', url('system/cron'), 'success');
  49. }
  50. }
  51. }
  52. if($do == 'list') {
  53. $crons = pdo_fetchall('SELECT * FROM ' . tablename('cron') . ' ORDER BY cronid ASC');
  54. $weekday_cn = array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  55. if(!empty($crons)) {
  56. foreach($crons as &$cron) {
  57. $cn = '';
  58. if($cron['day'] > 0 && $cron['day'] < 32) {
  59. $cn = '每月' . $cron['day'] . '日';
  60. } elseif($cron['weekday'] >= 0 && $cron['weekday'] < 7) {
  61. $cn = '每' . $weekday_cn[$cron['weekday']];
  62. } elseif($cron['hour'] >= 0 && $cron['hour'] < 24) {
  63. $cn = '每天';
  64. } else{
  65. $cn = '每小时';
  66. }
  67. $cn .= ($cron['hour'] >= 0 && $cron['hour'] < 24) ? sprintf('%02d', $cron['hour']) . '时' : '';
  68. if(!in_array($cron['minute'], array(-1, ''))) {
  69. foreach($cron['minute'] = explode("\t", $cron['minute']) as $k => $v) {
  70. $cron['minute'][$k] = sprintf('%02d', $v);
  71. }
  72. $cron['minute'] = implode(',', $cron['minute']);
  73. $cn .= $cron['minute'] . '分';
  74. } else {
  75. $cn .= '00分';
  76. }
  77. $cron['lastrun'] = $cron['lastrun'] ? date('Y-m-d H:i:s', $cron['lastrun']) : 'N/A';
  78. $cron['nextrun'] = $cron['nextrun'] ? date('Y-m-d H:i:s', $cron['nextrun']) : 'N/A';
  79. $cron['run'] = $cron['available'];
  80. $cron['cn'] = $cn;
  81. }
  82. }
  83. }
  84. if($do == 'del') {
  85. if(checksubmit('submit')) {
  86. echo 90;
  87. $ids = $_GPC['cronid'];
  88. if(!empty($ids)) {
  89. $idstr = implode(',', $ids);
  90. if(preg_match('/^(\d{1,10},)*(\d{1,10})$/', $idstr)) {
  91. $state = pdo_query('DELETE FROM ' . tablename('cron') . " WHERE cronid IN ({$idstr})");
  92. if($state !== false) {
  93. message('删除计划任务成功', url('system/cron'), 'success');
  94. } else {
  95. message('删除计划任务失败', url('system/cron'), 'error');
  96. }
  97. }
  98. }
  99. }
  100. }
  101. if($do == 'run') {
  102. $id = intval($_GPC['id']);
  103. load()->func('cron');
  104. cron_run($id);
  105. message('执行计划任务成功', url('system/cron/list'), 'success');
  106. }
  107. template('system/cron');