Microweber\Utils\Backup::cronjob PHP Метод

cronjob() публичный Метод

public cronjob ( $params = false )
    public function cronjob($params = false)
    {
        if (!defined('INI_SYSTEM_CHECK_DISABLED')) {
            define('INI_SYSTEM_CHECK_DISABLED', ini_get('disable_functions'));
        }
        if (!defined('MW_NO_SESSION')) {
            define('MW_NO_SESSION', true);
        }
        if (!defined('IS_ADMIN')) {
            define('IS_ADMIN', true);
        }
        if (!strstr(INI_SYSTEM_CHECK_DISABLED, 'ini_set')) {
            ini_set('memory_limit', '512M');
        }
        if (!strstr(INI_SYSTEM_CHECK_DISABLED, 'set_time_limit')) {
            set_time_limit(600);
        }
        if (!strstr(INI_SYSTEM_CHECK_DISABLED, 'ignore_user_abort')) {
            ignore_user_abort();
        }
        $type = 'full';
        if (isset($params['type'])) {
            $type = trim($params['type']);
        }
        $cache_id = 'backup_queue';
        $cache_id_loc = 'backup_progress';
        $cache_state_id = 'backup_zip_state';
        $cache_content = $this->app->cache_manager->get($cache_id, 'backup');
        $cache_lock = $this->app->cache_manager->get($cache_id_loc, 'backup');
        $cache_state = $this->app->cache_manager->get($cache_state_id, 'backup', 30);
        $time = time();
        $here = $this->get_bakup_location();
        if ($cache_state == 'opened') {
            return $cache_content;
        }
        if ($cache_content == false or empty($cache_content)) {
            $this->app->cache_manager->save(false, $cache_id_loc, 'backup');
            $this->app->cache_manager->save(false, $cache_id, 'backup');
            return true;
        } else {
            $bak_fn = 'backup_' . date('Y-M-d-His') . '_' . uniqid() . '';
            $filename = $here . $bak_fn . '.zip';
            if ($cache_lock == false or !is_array($cache_lock)) {
                $cache_lock = array();
                $cache_lock['processed'] = 0;
                $cache_lock['files_count'] = count($cache_content);
                $cache_lock['time'] = $time;
                $cache_lock['filename'] = $filename;
                $this->app->cache_manager->save($cache_lock, $cache_id_loc, 'backup');
            } else {
                if (isset($cache_lock['filename'])) {
                    $filename = $cache_lock['filename'];
                }
            }
            if (isset($cache_lock['time'])) {
                $time_sec = intval($cache_lock['time']);
                if ($time - 3 < $time_sec) {
                    return $cache_content;
                }
            }
            $backup_actions = $cache_content;
            global $mw_backup_zip_obj;
            if (!is_object($mw_backup_zip_obj)) {
                $mw_backup_zip_obj = new ZipArchive();
            }
            $zip_opened = false;
            if (is_array($backup_actions)) {
                $i = 0;
                $this->app->cache_manager->save($filename, $cache_id_loc, 'backup');
                if (!$mw_backup_zip_obj->open($filename, ZIPARCHIVE::CREATE)) {
                    $zip_opened = 1;
                    return false;
                }
                $this->app->cache_manager->save('opened', $cache_state_id, 'backup');
                $limit_per_turn = 20;
                foreach ($backup_actions as $key => $item) {
                    $flie_ext = strtolower(get_file_extension($item));
                    if ($flie_ext == 'php' or $flie_ext == 'css' or $flie_ext == 'js') {
                        $limit_per_turn = 150;
                    }
                    if ($i > $limit_per_turn or $cache_lock == $item) {
                        if (isset($mw_backup_zip_obj) and is_object($mw_backup_zip_obj)) {
                            if ($zip_opened == 1) {
                                $mw_backup_zip_obj->close();
                            }
                        }
                        $this->app->cache_manager->save('closed', $cache_state_id, 'backup');
                    } else {
                        ++$cache_lock['processed'];
                        $cache_lock['time'] = time();
                        $cache_lock['filename'] = $filename;
                        $precent = $cache_lock['processed'] / $cache_lock['files_count'] * 100;
                        $precent = round($precent);
                        $cache_lock['percent'] = $precent;
                        $back_log_action = "Progress  {$precent}% ({$cache_lock['processed']}/{$cache_lock['files_count']}) <br><small>" . basename($item) . '</small>';
                        $this->log_action($back_log_action);
                        $this->app->cache_manager->save($cache_lock, $cache_id_loc, 'backup');
                        if ($item == 'make_db_backup') {
                            $limit_per_turn = 1;
                            $mw_backup_zip_obj->close();
                            $this->app->cache_manager->save('closed', $cache_state_id, 'backup');
                            $db_file = $this->create($bak_fn . '.sql');
                            if (!$mw_backup_zip_obj->open($filename, ZIPARCHIVE::CREATE)) {
                                $zip_opened = 1;
                                return false;
                            }
                            $this->app->cache_manager->save('opened', $cache_state_id, 'backup');
                            if (isset($db_file['filename'])) {
                                $filename2 = $here . $db_file['filename'];
                                if (is_file($filename2)) {
                                    $back_log_action = 'Adding sql restore to zip';
                                    $this->log_action($back_log_action);
                                    $mw_backup_zip_obj->addFile($filename2, 'mw_sql_restore.sql');
                                }
                            }
                        } else {
                            $relative_loc = str_replace(userfiles_path(), '', $item);
                            $new_backup_actions = array();
                            if (is_dir($item)) {
                                $mw_backup_zip_obj->addEmptyDir($relative_loc);
                            } elseif (is_file($item)) {
                                $mw_backup_zip_obj->addFile($item, $relative_loc);
                            }
                        }
                        unset($backup_actions[$key]);
                        if (isset($new_backup_actions) and !empty($new_backup_actions)) {
                            $backup_actions = array_merge($backup_actions, $new_backup_actions);
                            array_unique($backup_actions);
                            $this->app->cache_manager->save($backup_actions, $cache_id, 'backup');
                        } else {
                            $this->app->cache_manager->save($backup_actions, $cache_id, 'backup');
                        }
                        if (empty($backup_actions)) {
                            $this->app->cache_manager->save(false, $cache_id, 'backup');
                        }
                    }
                    ++$i;
                }
                $mw_backup_zip_obj->close();
                $this->app->cache_manager->save('closed', $cache_state_id, 'backup');
            }
        }
        if (empty($backup_actions)) {
            $this->app->cache_manager->save(false, $cache_id, 'backup');
        }
        return $cache_content;
    }