人人商城

cron.func.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. function cron_check($cronid = 0) {
  8. global $_W;
  9. $cron = pdo_get('core_cron', array('cloudid' => $cronid));
  10. $_W['uniacid'] = $cron['uniacid'];
  11. if(empty($cron)) {
  12. return error(-1000, '任务不存在或已删除');
  13. }
  14. if(!$cron['status']) {
  15. return error(-1001, '任务已关闭');
  16. }
  17. if ($cron['filename'] != 'sms') {
  18. if(!$cron['uniacid']) {
  19. return error(-1002, '任务uniacid错误1');
  20. }
  21. }
  22. if(empty($cron['module'])) {
  23. return error(-1003, '任务所属模块为空');
  24. } else {
  25. if($cron['module'] != 'task') {
  26. $modules = array_keys(uni_modules());
  27. if(!in_array($cron['module'], $modules)) {
  28. return error(-1004, "公众号没有操作模块{$cron['module']}的权限");
  29. }
  30. }
  31. }
  32. if(empty($cron['filename'])) {
  33. return error(-1005, '任务脚本名称为空');
  34. }
  35. return $cron;
  36. }
  37. function cron_run($id) {
  38. global $_W;
  39. $cron = pdo_get('core_cron', array('uniacid' => $_W['uniacid'], 'id' => $id));
  40. if(empty($cron)) {
  41. return false;
  42. }
  43. $extra = array();
  44. $extra['Host'] = $_SERVER['HTTP_HOST'];
  45. load()->func('communication');
  46. $urlset = parse_url($_W['siteurl']);
  47. $urlset = pathinfo($urlset['path']);
  48. $response = ihttp_request($_W['sitescheme'] . '127.0.0.1/'. $urlset['dirname'] . '/' . url('cron/entry', array('id' => $cron['cloudid'])), array(), $extra);
  49. $response = json_decode($response['content'], true);
  50. if (is_error($response['message'])) {
  51. return $response['message'];
  52. } else {
  53. cron_setnexttime($cron);
  54. $cron_new = pdo_get('core_cron', array('uniacid' => $_W['uniacid'], 'id' => $id));
  55. if(empty($cron_new)) return true;
  56. if($cron_new['status'] != $cron['status'] || $cron_new['lastruntime'] != $cron['lastruntime'] || $cron_new['nextruntime'] != $cron['nextruntime']) {
  57. load()->model('cloud');
  58. $cron_new['id'] = $cron_new['cloudid'];
  59. $status = cloud_cron_update($cron_new);
  60. if(is_error($status)) {
  61. return $status;
  62. }
  63. }
  64. }
  65. return true;
  66. }
  67. function cron_setnexttime($cron) {
  68. if(empty($cron)) return FALSE;
  69. if($cron['type'] == 1) {
  70. pdo_update('core_cron', array('status' => 0, 'lastruntime' => TIMESTAMP, 'nextruntime' => TIMESTAMP), array('id' => $cron['id']));
  71. return true;
  72. }
  73. if(!empty($cron['minute'])) {
  74. $cron['minute'] = explode("\t", $cron['minute']);
  75. }
  76. list($yearnow, $monthnow, $daynow, $weekdaynow, $hournow, $minutenow) = explode('-', date('Y-m-d-w-H-i', TIMESTAMP));
  77. if($cron['weekday'] == -1) {
  78. if($cron['day'] == -1) {
  79. $firstday = $daynow;
  80. $secondday = $daynow + 1;
  81. } else {
  82. $firstday = $cron['day'];
  83. $secondday = $cron['day'] + date('t', TIMESTAMP);
  84. }
  85. } else {
  86. $firstday = $daynow + ($cron['weekday'] - $weekdaynow);
  87. $secondday = $firstday + 7;
  88. }
  89. if($firstday < $daynow) {
  90. $firstday = $secondday;
  91. }
  92. if($firstday == $daynow) {
  93. $todaytime = cron_todaynextrun($cron);
  94. if($todaytime['hour'] == -1 && $todaytime['minute'] == -1) {
  95. $cron['day'] = $secondday;
  96. $nexttime = cron_todaynextrun($cron, 0, -1);
  97. $cron['hour'] = $nexttime['hour'];
  98. $cron['minute'] = $nexttime['minute'];
  99. } else {
  100. $cron['day'] = $firstday;
  101. $cron['hour'] = $todaytime['hour'];
  102. $cron['minute'] = $todaytime['minute'];
  103. }
  104. } else {
  105. $cron['day'] = $firstday;
  106. $nexttime = cron_todaynextrun($cron, 0, -1);
  107. $cron['hour'] = $nexttime['hour'];
  108. $cron['minute'] = $nexttime['minute'];
  109. }
  110. $nextrun = mktime($cron['hour'], $cron['minute'] > 0 ? $cron['minute'] : 0, 0, $monthnow, $cron['day'], $yearnow);
  111. $data = array('lastruntime' => TIMESTAMP, 'nextruntime' => $nextrun);
  112. if($nextrun <= TIMESTAMP) {
  113. $data['status'] = 0;
  114. }
  115. pdo_update('core_cron', $data, array('id' => $cron['id']));
  116. return true;
  117. }
  118. function cron_todaynextrun($cron, $hour = -2, $minute = -2) {
  119. $hour = $hour == -2 ? date('H', TIMESTAMP) : $hour;
  120. $minute = $minute == -2 ? date('i', TIMESTAMP) : $minute;
  121. $nexttime = array();
  122. if($cron['hour'] == -1 && !$cron['minute']) {
  123. $nexttime['hour'] = $hour + 1;
  124. $nexttime['minute'] = 0;
  125. } elseif($cron['hour'] == -1 && $cron['minute'] != '') {
  126. $nexttime['hour'] = $hour;
  127. if(($nextminute = cron_nextminute($cron['minute'], $minute)) === false) {
  128. ++$nexttime['hour'];
  129. $nextminute = $cron['minute'][0];
  130. }
  131. $nexttime['minute'] = $nextminute;
  132. } elseif($cron['hour'] != -1 && !$cron['minute']) {
  133. if($cron['hour'] <= $hour) {
  134. $nexttime['hour'] = $nexttime['minute'] = -1;
  135. } else {
  136. $nexttime['hour'] = $cron['hour'];
  137. $nexttime['minute'] = 0;
  138. }
  139. } elseif($cron['hour'] != -1 && $cron['minute'] != '') {
  140. $nextminute = cron_nextminute($cron['minute'], $minute);
  141. if($cron['hour'] < $hour || ($cron['hour'] == $hour && $nextminute === false)) {
  142. $nexttime['hour'] = -1;
  143. $nexttime['minute'] = -1;
  144. } else {
  145. $nexttime['hour'] = $cron['hour'];
  146. $nexttime['minute'] = $nextminute;
  147. }
  148. }
  149. return $nexttime;
  150. }
  151. function cron_nextminute($nextminutes, $minutenow) {
  152. foreach($nextminutes as $nextminute) {
  153. if($nextminute > $minutenow) {
  154. return $nextminute;
  155. }
  156. }
  157. return false;
  158. }
  159. function cron_add($data) {
  160. global $_W;
  161. load()->model('cloud');
  162. if(empty($data['uniacid'])) {
  163. $data['uniacid'] = $_W['uniacid'];
  164. }
  165. if(empty($data['name'])) {
  166. return error(-1, '任务名称不能为空');
  167. }
  168. if(empty($data['filename'])) {
  169. return error(-1, '任务脚本不能为空');
  170. }
  171. if(empty($data['module'])) {
  172. return error(-1, '任务所属模块不能为空');
  173. }
  174. if(empty($data['type']) || !in_array($data['type'], array(1, 2))) {
  175. return error(-1, '任务的类型不能为空');
  176. }
  177. if($data['type'] == 1 && $data['lastruntime'] <= TIMESTAMP) {
  178. return error(-1, '定时任务的执行时间不能小于当前时间');
  179. } else {
  180. $data['nextruntime'] = $data['lastruntime'];
  181. }
  182. $data['day'] = intval($data['weekday']) == -1 ? intval($data['day']) : -1;
  183. $data['weekday'] = intval($data['weekday']);
  184. $data['hour'] = intval($data['hour']);
  185. $data['module'] = trim($data['module']);
  186. $data['minute'] = str_replace(',', ',', $data['minute']);
  187. if(strpos($data['minute'], ',') !== FALSE) {
  188. $minutenew = explode(',', $data['minute']);
  189. foreach($minutenew as $key => $val) {
  190. $minutenew[$key] = $val = intval($val);
  191. if($val < 0 || $val > 59) {
  192. unset($minutenew[$key]);
  193. }
  194. }
  195. $minutenew = array_slice(array_unique($minutenew), 0, 2);
  196. $minutenew = implode("\t", $minutenew);
  197. } else {
  198. $minutenew = intval($data['minute']);
  199. $minutenew = $minutenew >= 0 && $minutenew < 60 ? $minutenew : '';
  200. }
  201. $data['minute'] = $minutenew;
  202. $data['createtime'] = TIMESTAMP;
  203. $data = array_elements(array('uniacid', 'name', 'filename', 'module', 'type', 'status', 'day','weekday','hour','minute','status', 'lastruntime', 'nextruntime', 'createtime', 'extra'), $data);
  204. $status = cloud_cron_create($data);
  205. if(is_error($status)) {
  206. return $status;
  207. }
  208. $data['cloudid'] = $status['cron_id'];
  209. pdo_insert('core_cron', $data);
  210. return pdo_insertid();
  211. }
  212. function cron_delete($ids) {
  213. global $_W;
  214. load()->model('cloud');
  215. if (empty($ids) || !is_array($ids)) {
  216. return true;
  217. }
  218. $ids = safe_gpc_array($ids);
  219. $corns = pdo_getall('core_cron', array('uniacid' => $_W['uniacid'], 'id' => $ids), array(), 'cloudid');
  220. $cloudid = array_keys($corns);
  221. if(!empty($cloudid)) {
  222. $status = cloud_cron_remove($cloudid);
  223. if(is_error($status)) {
  224. return $status;
  225. }
  226. pdo_delete('core_cron', array('uniacid' => $_W['uniacid'], 'id' => $ids));
  227. }
  228. return true;
  229. }