public static function export_db($sql_file)
 {
     global $wpdb;
     $export_str = "-- Wordpress database dump --\n";
     Sns_Log::log_action('Fetching tables list');
     $query = "SHOW TABLES";
     $tables = $wpdb->get_results($query, ARRAY_A);
     if ($tables === false) {
         throw new Sns_Exception_DB_Error($query);
     }
     Sns_Log::log_action('Fetching tables list', SNS_LOG_END);
     foreach ($tables as $db_item) {
         $table = current($db_item);
         Sns_Log::log_action('Fetching table - ' . $table);
         $query = "SHOW CREATE TABLE " . $table;
         $create = $wpdb->get_var($query, 1);
         if ($create === false) {
             throw new Sns_Exception_DB_Error($query);
         }
         $myisam = strpos($create, 'MyISAM');
         if ($table == SNS_DB_PREFIX . 'backups' || $table == SNS_DB_PREFIX . 'settings' || $table == SNS_DB_PREFIX . 'options' || $table == SNS_DB_PREFIX . 'settings_destinations' || $table == SNS_DB_PREFIX . 'settings_ftp' || $table == SNS_DB_PREFIX . 'state') {
             continue;
         }
         $export_str .= "-- ----------------------------------------------------------\n";
         $export_str .= "-- Dump of table `" . $table . "` --\n";
         $export_str .= "-- ----------------------------------------------------------\n\n";
         $export_str .= "DROP TABLE IF EXISTS `" . $table . "`;\n\n" . $create . ";\n\n";
         $query = "SELECT * FROM `" . $table . "` LIMIT 1000";
         $data = $wpdb->get_results($query, ARRAY_A);
         if ($query === false) {
             throw new Sns_Exception_DB_Error($query);
         }
         if (!empty($data)) {
             if (false !== $myisam) {
                 $export_str .= "-- !40000 ALTER TABLE `" . $table . "` DISABLE KEYS ;\n\n";
             }
             $offset = 0;
             while (!empty($data)) {
                 foreach ($data as $entry) {
                     $cols = '';
                     foreach ($entry as $key => $value) {
                         if (NULL === $value) {
                             $entry[$key] = "NULL";
                         } elseif ("" === $value || false === $value) {
                             $entry[$key] = "''";
                         } elseif (!is_numeric($value)) {
                             $entry[$key] = "'" . esc_sql($value) . "'";
                         }
                         $cols .= ", `" . $key . "` ";
                     }
                     $cols = substr($cols, 1);
                     $export_str .= "INSERT INTO `" . $table . "` ( " . $cols . " ) VALUES ( " . implode(", ", $entry) . " );\n";
                     if (file_put_contents($sql_file, $export_str, FILE_APPEND) === false) {
                         throw new Sns_Exception_Unavailable_Operation('Cannot write in ' . $sql_file);
                     }
                     $export_str = '';
                 }
                 $offset += 1000;
                 $query = "SELECT * FROM `" . $table . "` LIMIT " . $offset . ",1000";
                 //force to free memory
                 //                        $data = null;
                 //                        if( gc_enabled() ){
                 //                            gc_collect_cycles();
                 //                        }else{
                 //                            gc_enable();
                 //                            gc_collect_cycles();
                 //                            gc_disable();
                 //                        }
                 $data = $wpdb->get_results($query, ARRAY_A);
                 if ($data === false) {
                     throw new Sns_Exception_DB_Error($query);
                 }
             }
             if (false !== $myisam) {
                 $export_str .= "\n --!40000 ALTER TABLE `" . $table . "` ENABLE KEYS ;";
             }
         }
         if (file_put_contents($sql_file, $export_str, FILE_APPEND) === false) {
             throw new Sns_Exception_Unavailable_Operation('Cannot write in ' . $sql_file);
         }
         $export_str = '';
         Sns_Log::log_action('Fetching table - ' . $table, SNS_LOG_END);
     }
 }
function sns_backup_action()
{
    $proc = Sns_State::get_status();
    $scheduleState = Sns_State::get_status(Sns_Backup::BACKUP_MODE_SCHEDULE);
    if ($proc['status'] != Sns_State::STATUS_ACTIVE && empty($scheduleState)) {
        $backup = new Sns_Backup('schedule');
        $data = array('status' => Sns_State::STATUS_ACTIVE);
        Sns_State::update($data);
        try {
            $backup->backup();
        } catch (Exception $e) {
            Sns_Log::log_exception_obj($e);
        }
        $data = array('status' => Sns_State::STATUS_FINISHED);
        Sns_State::update($data);
    }
}
 public static function log($ex)
 {
     Sns_Log::log_exception(get_class($ex), $ex->getMessage(), $ex->getFile(), $ex->getLine());
 }
}
?>
                                    </div>
                                </div>
                                <div id="settings-tab-log">
                                    <div class="fr">
                                        <button id="log-refresh" type="button" class="btn btn-primary">
                                            <span class="glyphicon glyphicon-refresh"></span>
                                        </button>
                                        <button id="log-empty" type="button" class="btn btn-default">
                                            <span class="glyphicon glyphicon-trash"></span>
                                        </button>
                                    </div>
                                    <div class="cb"></div>
                                    <textarea id="log-content"><?php 
Sns_Log::print_log();
?>
</textarea>
                                    <label class="checkbox-inline sns-report-block">
                                        <input id="sns-reporting" type="checkbox" <?php 
echo get_option('sns_backup_report_log') == "1" ? ' checked="checked" ' : '';
?>
>Automatically report logs
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php 
 public static function delete($backup_id)
 {
     global $wpdb;
     $backup_id = intval($backup_id);
     $table = SNS_DB_PREFIX . 'backups';
     $backup_data = self::get_backup_by_id($backup_id);
     $query = " DELETE FROM `{$table}`\n                       WHERE `id` = {$backup_id}\n                      ";
     $response = $wpdb->query($query);
     if ($response === false) {
         throw new Sns_Exception_DB_Error();
     }
     $backupFile = SNS_BACKUPS_PATH . $backup_data->filename . '.zip';
     if (file_exists($backupFile) && !unlink($backupFile)) {
         Sns_Log::log_msg('Cannot delete the file ' . $backupFile);
     }
 }
function sns_backup_log_empty()
{
    $result = new stdClass();
    $result->status = 'OK';
    Sns_Log::empty_log();
    sns_send_response($result);
}