PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/ctf-f2015/www/wp-includes/cron.php

https://github.com/mchow01/Security
PHP | 469 lines | 196 code | 54 blank | 219 comment | 49 complexity | 479da49acde4a630436c9e4066ffa37d MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress CRON API
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Schedules a hook to run only once.
  9. *
  10. * Schedules a hook which will be executed once by the WordPress actions core at
  11. * a time which you specify. The action will fire off when someone visits your
  12. * WordPress site, if the schedule time has passed.
  13. *
  14. * @since 2.1.0
  15. * @link http://codex.wordpress.org/Function_Reference/wp_schedule_single_event
  16. *
  17. * @param int $timestamp Timestamp for when to run the event.
  18. * @param string $hook Action hook to execute when cron is run.
  19. * @param array $args Optional. Arguments to pass to the hook's callback function.
  20. */
  21. function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
  22. // don't schedule a duplicate if there's already an identical event due in the next 10 minutes
  23. $next = wp_next_scheduled($hook, $args);
  24. if ( $next && $next <= $timestamp + 10 * MINUTE_IN_SECONDS )
  25. return;
  26. $crons = _get_cron_array();
  27. $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
  28. /**
  29. * Filter a single event before it is scheduled.
  30. *
  31. * @since 3.1.0
  32. *
  33. * @param object $event An object containing an event's data.
  34. */
  35. $event = apply_filters( 'schedule_event', $event );
  36. // A plugin disallowed this event
  37. if ( ! $event )
  38. return false;
  39. $key = md5(serialize($event->args));
  40. $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
  41. uksort( $crons, "strnatcasecmp" );
  42. _set_cron_array( $crons );
  43. }
  44. /**
  45. * Schedule a periodic event.
  46. *
  47. * Schedules a hook which will be executed by the WordPress actions core on a
  48. * specific interval, specified by you. The action will trigger when someone
  49. * visits your WordPress site, if the scheduled time has passed.
  50. *
  51. * Valid values for the recurrence are hourly, daily and twicedaily. These can
  52. * be extended using the cron_schedules filter in wp_get_schedules().
  53. *
  54. * Use wp_next_scheduled() to prevent duplicates
  55. *
  56. * @since 2.1.0
  57. *
  58. * @param int $timestamp Timestamp for when to run the event.
  59. * @param string $recurrence How often the event should recur.
  60. * @param string $hook Action hook to execute when cron is run.
  61. * @param array $args Optional. Arguments to pass to the hook's callback function.
  62. * @return bool|null False on failure, null when complete with scheduling event.
  63. */
  64. function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
  65. $crons = _get_cron_array();
  66. $schedules = wp_get_schedules();
  67. if ( !isset( $schedules[$recurrence] ) )
  68. return false;
  69. $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
  70. /** This filter is documented in wp-includes/cron.php */
  71. $event = apply_filters( 'schedule_event', $event );
  72. // A plugin disallowed this event
  73. if ( ! $event )
  74. return false;
  75. $key = md5(serialize($event->args));
  76. $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
  77. uksort( $crons, "strnatcasecmp" );
  78. _set_cron_array( $crons );
  79. }
  80. /**
  81. * Reschedule a recurring event.
  82. *
  83. * @since 2.1.0
  84. *
  85. * @param int $timestamp Timestamp for when to run the event.
  86. * @param string $recurrence How often the event should recur.
  87. * @param string $hook Action hook to execute when cron is run.
  88. * @param array $args Optional. Arguments to pass to the hook's callback function.
  89. * @return bool|null False on failure. Null when event is rescheduled.
  90. */
  91. function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
  92. $crons = _get_cron_array();
  93. $schedules = wp_get_schedules();
  94. $key = md5( serialize( $args ) );
  95. $interval = 0;
  96. // First we try to get it from the schedule
  97. if ( isset( $schedules[ $recurrence ] ) ) {
  98. $interval = $schedules[ $recurrence ]['interval'];
  99. }
  100. // Now we try to get it from the saved interval in case the schedule disappears
  101. if ( 0 == $interval ) {
  102. $interval = $crons[ $timestamp ][ $hook ][ $key ]['interval'];
  103. }
  104. // Now we assume something is wrong and fail to schedule
  105. if ( 0 == $interval ) {
  106. return false;
  107. }
  108. $now = time();
  109. if ( $timestamp >= $now ) {
  110. $timestamp = $now + $interval;
  111. } else {
  112. $timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
  113. }
  114. wp_schedule_event( $timestamp, $recurrence, $hook, $args );
  115. }
  116. /**
  117. * Unschedule a previously scheduled cron job.
  118. *
  119. * The $timestamp and $hook parameters are required, so that the event can be
  120. * identified.
  121. *
  122. * @since 2.1.0
  123. *
  124. * @param int $timestamp Timestamp for when to run the event.
  125. * @param string $hook Action hook, the execution of which will be unscheduled.
  126. * @param array $args Arguments to pass to the hook's callback function.
  127. * Although not passed to a callback function, these arguments are used
  128. * to uniquely identify the scheduled event, so they should be the same
  129. * as those used when originally scheduling the event.
  130. */
  131. function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
  132. $crons = _get_cron_array();
  133. $key = md5(serialize($args));
  134. unset( $crons[$timestamp][$hook][$key] );
  135. if ( empty($crons[$timestamp][$hook]) )
  136. unset( $crons[$timestamp][$hook] );
  137. if ( empty($crons[$timestamp]) )
  138. unset( $crons[$timestamp] );
  139. _set_cron_array( $crons );
  140. }
  141. /**
  142. * Unschedule all cron jobs attached to a specific hook.
  143. *
  144. * @since 2.1.0
  145. *
  146. * @param string $hook Action hook, the execution of which will be unscheduled.
  147. * @param array $args Optional. Arguments that were to be pass to the hook's callback function.
  148. */
  149. function wp_clear_scheduled_hook( $hook, $args = array() ) {
  150. // Backward compatibility
  151. // Previously this function took the arguments as discrete vars rather than an array like the rest of the API
  152. if ( !is_array($args) ) {
  153. _deprecated_argument( __FUNCTION__, '3.0', __('This argument has changed to an array to match the behavior of the other cron functions.') );
  154. $args = array_slice( func_get_args(), 1 );
  155. }
  156. // This logic duplicates wp_next_scheduled()
  157. // It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
  158. // and, wp_next_scheduled() returns the same schedule in an infinite loop.
  159. $crons = _get_cron_array();
  160. if ( empty( $crons ) )
  161. return;
  162. $key = md5( serialize( $args ) );
  163. foreach ( $crons as $timestamp => $cron ) {
  164. if ( isset( $cron[ $hook ][ $key ] ) ) {
  165. wp_unschedule_event( $timestamp, $hook, $args );
  166. }
  167. }
  168. }
  169. /**
  170. * Retrieve the next timestamp for a cron event.
  171. *
  172. * @since 2.1.0
  173. *
  174. * @param string $hook Action hook to execute when cron is run.
  175. * @param array $args Optional. Arguments to pass to the hook's callback function.
  176. * @return bool|int The UNIX timestamp of the next time the scheduled event will occur.
  177. */
  178. function wp_next_scheduled( $hook, $args = array() ) {
  179. $crons = _get_cron_array();
  180. $key = md5(serialize($args));
  181. if ( empty($crons) )
  182. return false;
  183. foreach ( $crons as $timestamp => $cron ) {
  184. if ( isset( $cron[$hook][$key] ) )
  185. return $timestamp;
  186. }
  187. return false;
  188. }
  189. /**
  190. * Send request to run cron through HTTP request that doesn't halt page loading.
  191. *
  192. * @since 2.1.0
  193. *
  194. * @return null Cron could not be spawned, because it is not needed to run.
  195. */
  196. function spawn_cron( $gmt_time = 0 ) {
  197. if ( ! $gmt_time )
  198. $gmt_time = microtime( true );
  199. if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
  200. return;
  201. /*
  202. * multiple processes on multiple web servers can run this code concurrently
  203. * try to make this as atomic as possible by setting doing_cron switch
  204. */
  205. $lock = get_transient('doing_cron');
  206. if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS )
  207. $lock = 0;
  208. // don't run if another process is currently running it or more than once every 60 sec.
  209. if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time )
  210. return;
  211. //sanity check
  212. $crons = _get_cron_array();
  213. if ( !is_array($crons) )
  214. return;
  215. $keys = array_keys( $crons );
  216. if ( isset($keys[0]) && $keys[0] > $gmt_time )
  217. return;
  218. if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
  219. if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {
  220. return;
  221. }
  222. $doing_wp_cron = sprintf( '%.22F', $gmt_time );
  223. set_transient( 'doing_cron', $doing_wp_cron );
  224. ob_start();
  225. wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
  226. echo ' ';
  227. // flush any buffers and send the headers
  228. while ( @ob_end_flush() );
  229. flush();
  230. WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' );
  231. return;
  232. }
  233. $doing_wp_cron = sprintf( '%.22F', $gmt_time );
  234. set_transient( 'doing_cron', $doing_wp_cron );
  235. /**
  236. * Filter the cron request arguments.
  237. *
  238. * @since 3.5.0
  239. *
  240. * @param array $cron_request_array {
  241. * An array of cron request URL arguments.
  242. *
  243. * @type string $url The cron request URL.
  244. * @type int $key The 22 digit GMT microtime.
  245. * @type array $args {
  246. * An array of cron request arguments.
  247. *
  248. * @type int $timeout The request timeout in seconds. Default .01 seconds.
  249. * @type bool $blocking Whether to set blocking for the request. Default false.
  250. * @type bool $sslverify Whether SSL should be verified for the request. Default false.
  251. * }
  252. * }
  253. */
  254. $cron_request = apply_filters( 'cron_request', array(
  255. 'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),
  256. 'key' => $doing_wp_cron,
  257. 'args' => array(
  258. 'timeout' => 0.01,
  259. 'blocking' => false,
  260. /** This filter is documented in wp-includes/class-http.php */
  261. 'sslverify' => apply_filters( 'https_local_ssl_verify', false )
  262. )
  263. ) );
  264. wp_remote_post( $cron_request['url'], $cron_request['args'] );
  265. }
  266. /**
  267. * Run scheduled callbacks or spawn cron for all scheduled events.
  268. *
  269. * @since 2.1.0
  270. *
  271. * @return null When doesn't need to run Cron.
  272. */
  273. function wp_cron() {
  274. // Prevent infinite loops caused by lack of wp-cron.php
  275. if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
  276. return;
  277. if ( false === $crons = _get_cron_array() )
  278. return;
  279. $gmt_time = microtime( true );
  280. $keys = array_keys( $crons );
  281. if ( isset($keys[0]) && $keys[0] > $gmt_time )
  282. return;
  283. $schedules = wp_get_schedules();
  284. foreach ( $crons as $timestamp => $cronhooks ) {
  285. if ( $timestamp > $gmt_time ) break;
  286. foreach ( (array) $cronhooks as $hook => $args ) {
  287. if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
  288. continue;
  289. spawn_cron( $gmt_time );
  290. break 2;
  291. }
  292. }
  293. }
  294. /**
  295. * Retrieve supported and filtered Cron recurrences.
  296. *
  297. * The supported recurrences are 'hourly' and 'daily'. A plugin may add more by
  298. * hooking into the 'cron_schedules' filter. The filter accepts an array of
  299. * arrays. The outer array has a key that is the name of the schedule or for
  300. * example 'weekly'. The value is an array with two keys, one is 'interval' and
  301. * the other is 'display'.
  302. *
  303. * The 'interval' is a number in seconds of when the cron job should run. So for
  304. * 'hourly', the time is 3600 or 60*60. For weekly, the value would be
  305. * 60*60*24*7 or 604800. The value of 'interval' would then be 604800.
  306. *
  307. * The 'display' is the description. For the 'weekly' key, the 'display' would
  308. * be <code>__('Once Weekly')</code>.
  309. *
  310. * For your plugin, you will be passed an array. you can easily add your
  311. * schedule by doing the following.
  312. * <code>
  313. * // filter parameter variable name is 'array'
  314. * $array['weekly'] = array(
  315. * 'interval' => 604800,
  316. * 'display' => __('Once Weekly')
  317. * );
  318. * </code>
  319. *
  320. * @since 2.1.0
  321. *
  322. * @return array
  323. */
  324. function wp_get_schedules() {
  325. $schedules = array(
  326. 'hourly' => array( 'interval' => HOUR_IN_SECONDS, 'display' => __( 'Once Hourly' ) ),
  327. 'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ),
  328. 'daily' => array( 'interval' => DAY_IN_SECONDS, 'display' => __( 'Once Daily' ) ),
  329. );
  330. /**
  331. * Filter the non-default cron schedules.
  332. *
  333. * @since 2.1.0
  334. *
  335. * @param array $new_schedules An array of non-default cron schedules. Default empty.
  336. */
  337. return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
  338. }
  339. /**
  340. * Retrieve Cron schedule for hook with arguments.
  341. *
  342. * @since 2.1.0
  343. *
  344. * @param string $hook Action hook to execute when cron is run.
  345. * @param array $args Optional. Arguments to pass to the hook's callback function.
  346. * @return string|bool False, if no schedule. Schedule on success.
  347. */
  348. function wp_get_schedule($hook, $args = array()) {
  349. $crons = _get_cron_array();
  350. $key = md5(serialize($args));
  351. if ( empty($crons) )
  352. return false;
  353. foreach ( $crons as $timestamp => $cron ) {
  354. if ( isset( $cron[$hook][$key] ) )
  355. return $cron[$hook][$key]['schedule'];
  356. }
  357. return false;
  358. }
  359. //
  360. // Private functions
  361. //
  362. /**
  363. * Retrieve cron info array option.
  364. *
  365. * @since 2.1.0
  366. * @access private
  367. *
  368. * @return array CRON info array.
  369. */
  370. function _get_cron_array() {
  371. $cron = get_option('cron');
  372. if ( ! is_array($cron) )
  373. return false;
  374. if ( !isset($cron['version']) )
  375. $cron = _upgrade_cron_array($cron);
  376. unset($cron['version']);
  377. return $cron;
  378. }
  379. /**
  380. * Updates the CRON option with the new CRON array.
  381. *
  382. * @since 2.1.0
  383. * @access private
  384. *
  385. * @param array $cron Cron info array from {@link _get_cron_array()}.
  386. */
  387. function _set_cron_array($cron) {
  388. $cron['version'] = 2;
  389. update_option( 'cron', $cron );
  390. }
  391. /**
  392. * Upgrade a Cron info array.
  393. *
  394. * This function upgrades the Cron info array to version 2.
  395. *
  396. * @since 2.1.0
  397. * @access private
  398. *
  399. * @param array $cron Cron info array from {@link _get_cron_array()}.
  400. * @return array An upgraded Cron info array.
  401. */
  402. function _upgrade_cron_array($cron) {
  403. if ( isset($cron['version']) && 2 == $cron['version'])
  404. return $cron;
  405. $new_cron = array();
  406. foreach ( (array) $cron as $timestamp => $hooks) {
  407. foreach ( (array) $hooks as $hook => $args ) {
  408. $key = md5(serialize($args['args']));
  409. $new_cron[$timestamp][$hook][$key] = $args;
  410. }
  411. }
  412. $new_cron['version'] = 2;
  413. update_option( 'cron', $new_cron );
  414. return $new_cron;
  415. }