1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 
<?php
if (!defined('SMF'))
    die('No direct access...');
function ManageScheduledTasks()
{
    global $context, $txt;
    isAllowedTo('admin_forum');
    loadLanguage('ManageScheduledTasks');
    loadTemplate('ManageScheduledTasks');
    $subActions = array(
        'taskedit' => 'EditTask',
        'tasklog' => 'TaskLog',
        'tasks' => 'ScheduledTasks',
        'settings' => 'TaskSettings',
    );
    
    if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
        $context['sub_action'] = $_REQUEST['sa'];
    else
        $context['sub_action'] = 'tasks';
    
    $context[$context['admin_menu_name']]['tab_data'] = array(
        'title' => $txt['scheduled_tasks_title'],
        'help' => '',
        'description' => $txt['maintain_info'],
        'tabs' => array(
            'tasks' => array(
                'description' => $txt['maintain_tasks_desc'],
            ),
            'tasklog' => array(
                'description' => $txt['scheduled_log_desc'],
            ),
            'settings' => array(
                'description' => $txt['scheduled_tasks_settings_desc'],
            ),
        ),
    );
    call_integration_hook('integrate_manage_scheduled_tasks', array(&$subActions));
    
    call_helper($subActions[$context['sub_action']]);
}
function ScheduledTasks()
{
    global $context, $txt, $sourcedir, $smcFunc, $scripturl;
    
    
    $context['sub_template'] = 'view_scheduled_tasks';
    $context['page_title'] = $txt['maintain_tasks'];
    
    if (isset($_REQUEST['save']) && isset($_POST['enable_task']))
    {
        checkSession();
        
        require_once($sourcedir . '/ScheduledTasks.php');
        
        $enablers = array(0);
        foreach ($_POST['enable_task'] as $id => $enabled)
            if ($enabled)
                $enablers[] = (int) $id;
        
        $smcFunc['db_query']('', '
            UPDATE {db_prefix}scheduled_tasks
            SET disabled = CASE WHEN id_task IN ({array_int:id_task_enable}) THEN 0 ELSE 1 END',
            array(
                'id_task_enable' => $enablers,
            )
        );
        
        $get_info = $smcFunc['db_query']('', '
            SELECT disabled
            FROM {db_prefix}scheduled_tasks
            WHERE task = {string:remove_redirect}',
            array(
                'remove_redirect' => 'remove_topic_redirect'
            )
        );
        $temp = $smcFunc['db_fetch_assoc']($get_info);
        $task_disabled = !empty($temp['disabled']) ? 0 : 1;
        $smcFunc['db_free_result']($get_info);
        updateSettings(array('allow_expire_redirect' => $task_disabled));
        
        CalculateNextTrigger();
    }
    
    if (isset($_REQUEST['run']) && isset($_POST['run_task']))
    {
        $task_string = '';
        
        $tasks = array();
        foreach ($_POST['run_task'] as $task => $dummy)
            $tasks[] = (int) $task;
        
        $request = $smcFunc['db_query']('', '
            SELECT id_task, task, callable
            FROM {db_prefix}scheduled_tasks
            WHERE id_task IN ({array_int:tasks})
            LIMIT {int:limit}',
            array(
                'tasks' => $tasks,
                'limit' => count($tasks),
            )
        );
        
        require_once($sourcedir . '/ScheduledTasks.php');
        ignore_user_abort(true);
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            
            if (!empty($row['callable']))
                $task_string = $row['callable'];
            
            elseif (function_exists('scheduled_' . $row['task']))
                $task_string = 'scheduled_' . $row['task'];
            
            elseif (!empty($row['task']))
                $task_string = $row['task'];
            $start_time = microtime(true);
            
            if (empty($task_string))
                continue;
            
            @set_time_limit(300);
            if (function_exists('apache_reset_timeout'))
                @apache_reset_timeout();
            
            $callable_task = call_helper($task_string, true);
            
            if (!empty($callable_task))
                $completed = call_user_func($callable_task);
            else
                $completed = false;
            
            if ($completed)
            {
                $total_time = round(microtime(true) - $start_time, 3);
                $smcFunc['db_insert']('',
                    '{db_prefix}log_scheduled_tasks',
                    array('id_task' => 'int', 'time_run' => 'int', 'time_taken' => 'float'),
                    array($row['id_task'], time(), $total_time),
                    array('id_task')
                );
            }
        }
        $smcFunc['db_free_result']($request);
        
        if (!empty($context['scheduled_errors']))
            $_SESSION['st_error'] = $context['scheduled_errors'];
        redirectexit('action=admin;area=scheduledtasks;done');
    }
    if (isset($_SESSION['st_error']))
    {
        $context['scheduled_errors'] = $_SESSION['st_error'];
        unset ($_SESSION['st_error']);
    }
    $listOptions = array(
        'id' => 'scheduled_tasks',
        'title' => $txt['maintain_tasks'],
        'base_href' => $scripturl . '?action=admin;area=scheduledtasks',
        'get_items' => array(
            'function' => 'list_getScheduledTasks',
        ),
        'columns' => array(
            'name' => array(
                'header' => array(
                    'value' => $txt['scheduled_tasks_name'],
                    'style' => 'width: 40%;',
                ),
                'data' => array(
                    'sprintf' => array(
                        'format' => '
                            <a href="' . $scripturl . '?action=admin;area=scheduledtasks;sa=taskedit;tid=%1$d">%2$s</a><br><span class="smalltext">%3$s</span>',
                        'params' => array(
                            'id' => false,
                            'name' => false,
                            'desc' => false,
                        ),
                    ),
                ),
            ),
            'next_due' => array(
                'header' => array(
                    'value' => $txt['scheduled_tasks_next_time'],
                ),
                'data' => array(
                    'db' => 'next_time',
                    'class' => 'smalltext',
                ),
            ),
            'regularity' => array(
                'header' => array(
                    'value' => $txt['scheduled_tasks_regularity'],
                ),
                'data' => array(
                    'db' => 'regularity',
                    'class' => 'smalltext',
                ),
            ),
            'run_now' => array(
                'header' => array(
                    'value' => $txt['scheduled_tasks_run_now'],
                    'style' => 'width: 12%;',
                    'class' => 'centercol',
                ),
                'data' => array(
                    'sprintf' => array(
                        'format' =>
                            '<input type="checkbox" name="run_task[%1$d]" id="run_task_%1$d">',
                        'params' => array(
                            'id' => false,
                        ),
                    ),
                    'class' => 'centercol',
                ),
            ),
            'enabled' => array(
                'header' => array(
                    'value' => $txt['scheduled_tasks_enabled'],
                    'style' => 'width: 6%;',
                    'class' => 'centercol',
                ),
                'data' => array(
                    'sprintf' => array(
                        'format' =>
                            '<input type="hidden" name="enable_task[%1$d]" id="task_%1$d" value="0"><input type="checkbox" name="enable_task[%1$d]" id="task_check_%1$d" %2$s>',
                        'params' => array(
                            'id' => false,
                            'checked_state' => false,
                        ),
                    ),
                    'class' => 'centercol',
                ),
            ),
        ),
        'form' => array(
            'href' => $scripturl . '?action=admin;area=scheduledtasks',
        ),
        'additional_rows' => array(
            array(
                'position' => 'below_table_data',
                'value' => '
                    <input type="submit" name="save" value="' . $txt['scheduled_tasks_save_changes'] . '" class="button">
                    <input type="submit" name="run" value="' . $txt['scheduled_tasks_run_now'] . '" class="button">',
            ),
            array(
                'position' => 'after_title',
                'value' => $txt['scheduled_tasks_time_offset'],
            ),
        ),
    );
    require_once($sourcedir . '/Subs-List.php');
    createList($listOptions);
    $context['sub_template'] = 'view_scheduled_tasks';
    $context['tasks_were_run'] = isset($_GET['done']);
}
function list_getScheduledTasks($start, $items_per_page, $sort)
{
    global $smcFunc, $txt;
    $request = $smcFunc['db_query']('', '
        SELECT id_task, next_time, time_offset, time_regularity, time_unit, disabled, task
        FROM {db_prefix}scheduled_tasks',
        array(
        )
    );
    $known_tasks = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        
        $offset = sprintf($txt['scheduled_task_reg_starting'], date('H:i', $row['time_offset']));
        $repeating = sprintf($txt['scheduled_task_reg_repeating'], $row['time_regularity'], $txt['scheduled_task_reg_unit_' . $row['time_unit']]);
        $known_tasks[] = array(
            'id' => $row['id_task'],
            'function' => $row['task'],
            'name' => isset($txt['scheduled_task_' . $row['task']]) ? $txt['scheduled_task_' . $row['task']] : $row['task'],
            'desc' => isset($txt['scheduled_task_desc_' . $row['task']]) ? $txt['scheduled_task_desc_' . $row['task']] : '',
            'next_time' => $row['disabled'] ? $txt['scheduled_tasks_na'] : timeformat(($row['next_time'] == 0 ? time() : $row['next_time']), true, 'server'),
            'disabled' => $row['disabled'],
            'checked_state' => $row['disabled'] ? '' : 'checked',
            'regularity' => $offset . ', ' . $repeating,
        );
    }
    $smcFunc['db_free_result']($request);
    return $known_tasks;
}
function EditTask()
{
    global $context, $txt, $sourcedir, $smcFunc;
    
    $context[$context['admin_menu_name']]['current_subsection'] = 'tasks';
    $context['sub_template'] = 'edit_scheduled_tasks';
    $context['page_title'] = $txt['scheduled_task_edit'];
    $context['server_time'] = timeformat(time(), false, 'server');
    
    if (!isset($_GET['tid']))
        fatal_lang_error('no_access', false);
    $_GET['tid'] = (int) $_GET['tid'];
    
    if (isset($_GET['save']))
    {
        checkSession();
        validateToken('admin-st');
        
        require_once($sourcedir . '/ScheduledTasks.php');
        
        preg_match('~(\d{1,2}):(\d{1,2})~', $_POST['offset'], $matches);
        
        if (!isset($matches[2]) || $matches[2] > 59)
            $matches[2] = 0;
        if (!isset($matches[1]) || $matches[1] > 23)
            $matches[1] = 0;
        
        $offset = $matches[1] * 3600 + $matches[2] * 60 - date('Z');
        
        $interval = max((int) $_POST['regularity'], 1);
        $unit = in_array(substr($_POST['unit'], 0, 1), array('m', 'h', 'd', 'w')) ? substr($_POST['unit'], 0, 1) : 'd';
        
        if ($interval == 1 && $unit == 'm')
            $interval = 2;
        
        $disabled = !isset($_POST['enabled']) ? 1 : 0;
        
        $smcFunc['db_query']('', '
            UPDATE {db_prefix}scheduled_tasks
            SET disabled = {int:disabled}, time_offset = {int:time_offset}, time_unit = {string:time_unit},
                time_regularity = {int:time_regularity}
            WHERE id_task = {int:id_task}',
            array(
                'disabled' => $disabled,
                'time_offset' => $offset,
                'time_regularity' => $interval,
                'id_task' => $_GET['tid'],
                'time_unit' => $unit,
            )
        );
        
        CalculateNextTrigger($_GET['tid'], true);
        
        redirectexit('action=admin;area=scheduledtasks');
    }
    
    $request = $smcFunc['db_query']('', '
        SELECT id_task, next_time, time_offset, time_regularity, time_unit, disabled, task
        FROM {db_prefix}scheduled_tasks
        WHERE id_task = {int:id_task}',
        array(
            'id_task' => $_GET['tid'],
        )
    );
    
    if ($smcFunc['db_num_rows']($request) == 0)
        fatal_lang_error('no_access', false);
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        $context['task'] = array(
            'id' => $row['id_task'],
            'function' => $row['task'],
            'name' => isset($txt['scheduled_task_' . $row['task']]) ? $txt['scheduled_task_' . $row['task']] : $row['task'],
            'desc' => isset($txt['scheduled_task_desc_' . $row['task']]) ? $txt['scheduled_task_desc_' . $row['task']] : '',
            'next_time' => $row['disabled'] ? $txt['scheduled_tasks_na'] : timeformat($row['next_time'] == 0 ? time() : $row['next_time'], true, 'server'),
            'disabled' => $row['disabled'],
            'offset' => $row['time_offset'],
            'regularity' => $row['time_regularity'],
            'offset_formatted' => date('H:i', $row['time_offset']),
            'unit' => $row['time_unit'],
        );
    }
    $smcFunc['db_free_result']($request);
    createToken('admin-st');
}
function TaskLog()
{
    global $scripturl, $context, $txt, $smcFunc, $sourcedir;
    
    loadLanguage('ManageScheduledTasks');
    
    if (!empty($_POST['removeAll']))
    {
        checkSession();
        validateToken('admin-tl');
        $smcFunc['db_query']('truncate_table', '
            TRUNCATE {db_prefix}log_scheduled_tasks',
            array(
            )
        );
    }
    
    $listOptions = array(
        'id' => 'task_log',
        'items_per_page' => 30,
        'title' => $txt['scheduled_log'],
        'no_items_label' => $txt['scheduled_log_empty'],
        'base_href' => $context['admin_area'] == 'scheduledtasks' ? $scripturl . '?action=admin;area=scheduledtasks;sa=tasklog' : $scripturl . '?action=admin;area=logs;sa=tasklog',
        'default_sort_col' => 'date',
        'get_items' => array(
            'function' => 'list_getTaskLogEntries',
        ),
        'get_count' => array(
            'function' => 'list_getNumTaskLogEntries',
        ),
        'columns' => array(
            'name' => array(
                'header' => array(
                    'value' => $txt['scheduled_tasks_name'],
                ),
                'data' => array(
                    'db' => 'name'
                ),
            ),
            'date' => array(
                'header' => array(
                    'value' => $txt['scheduled_log_time_run'],
                ),
                'data' => array(
                    'function' => function($rowData)
                    {
                        return timeformat($rowData['time_run'], true);
                    },
                ),
                'sort' => array(
                    'default' => 'lst.id_log DESC',
                    'reverse' => 'lst.id_log',
                ),
            ),
            'time_taken' => array(
                'header' => array(
                    'value' => $txt['scheduled_log_time_taken'],
                ),
                'data' => array(
                    'sprintf' => array(
                        'format' => $txt['scheduled_log_time_taken_seconds'],
                        'params' => array(
                            'time_taken' => false,
                        ),
                    ),
                ),
                'sort' => array(
                    'default' => 'lst.time_taken',
                    'reverse' => 'lst.time_taken DESC',
                ),
            ),
        ),
        'form' => array(
            'href' => $context['admin_area'] == 'scheduledtasks' ? $scripturl . '?action=admin;area=scheduledtasks;sa=tasklog' : $scripturl . '?action=admin;area=logs;sa=tasklog',
            'token' => 'admin-tl',
        ),
        'additional_rows' => array(
            array(
                'position' => 'below_table_data',
                'value' => '
                    <input type="submit" name="removeAll" value="' . $txt['scheduled_log_empty_log'] . '" data-confirm="' . $txt['scheduled_log_empty_log_confirm'] . '" class="button you_sure">',
            ),
            array(
                'position' => 'after_title',
                'value' => $txt['scheduled_tasks_time_offset'],
            ),
        ),
    );
    createToken('admin-tl');
    require_once($sourcedir . '/Subs-List.php');
    createList($listOptions);
    $context['sub_template'] = 'show_list';
    $context['default_list'] = 'task_log';
    
    $context[$context['admin_menu_name']]['current_subsection'] = 'tasklog';
    $context['page_title'] = $txt['scheduled_log'];
}
function list_getTaskLogEntries($start, $items_per_page, $sort)
{
    global $smcFunc, $txt;
    $request = $smcFunc['db_query']('', '
        SELECT lst.id_log, lst.id_task, lst.time_run, lst.time_taken, st.task
        FROM {db_prefix}log_scheduled_tasks AS lst
            INNER JOIN {db_prefix}scheduled_tasks AS st ON (st.id_task = lst.id_task)
        ORDER BY {raw:sort}
        LIMIT {int:start}, {int:items}',
        array(
            'sort' => $sort,
            'start' => $start,
            'items' => $items_per_page,
        )
    );
    $log_entries = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
        $log_entries[] = array(
            'id' => $row['id_log'],
            'name' => isset($txt['scheduled_task_' . $row['task']]) ? $txt['scheduled_task_' . $row['task']] : $row['task'],
            'time_run' => $row['time_run'],
            'time_taken' => $row['time_taken'],
        );
    $smcFunc['db_free_result']($request);
    return $log_entries;
}
function list_getNumTaskLogEntries()
{
    global $smcFunc;
    $request = $smcFunc['db_query']('', '
        SELECT COUNT(*)
        FROM {db_prefix}log_scheduled_tasks',
        array(
        )
    );
    list ($num_entries) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);
    return $num_entries;
}
function TaskSettings($return_config = false)
{
    global $sourcedir, $txt, $context, $scripturl;
    
    require_once($sourcedir . '/ManageServer.php');
    loadLanguage('Help');
    $config_vars = array(
        array('check', 'cron_is_real_cron', 'subtext' => $txt['cron_is_real_cron_desc'], 'help' => 'cron_is_real_cron'),
    );
    call_integration_hook('integrate_scheduled_tasks_settings', array(&$config_vars));
    if ($return_config)
        return $config_vars;
    
    $context['page_title'] = $txt['scheduled_tasks_settings'];
    $context['sub_template'] = 'show_settings';
    $context['post_url'] = $scripturl . '?action=admin;area=scheduledtasks;save;sa=settings';
    $context['settings_title'] = $txt['scheduled_tasks_settings'];
    
    if (isset($_GET['save']))
    {
        checkSession();
        $save_vars = $config_vars;
        call_integration_hook('integrate_save_scheduled_tasks_settings', array(&$save_vars));
        saveDBSettings($save_vars);
        $_SESSION['adm-save'] = true;
        redirectexit('action=admin;area=scheduledtasks;sa=settings');
    }
    prepareDBSettingContext($config_vars);
}
?>