<?php
session_start();
include "../../inc/config.php";
session_check_adm();
$string = '<?php
session_start();
include "../../inc/config.php";
';
		$data = $db->fetch_custom("select * from sys_users");
		$string .= '$data = array(
		';
		foreach ($data as $isi) {
			$string.= "array(
				'id' => '$isi->id',
				'first_name' => '$isi->first_name',
				'last_name' => '$isi->last_name',
				'username' => '$isi->username',
				'password' => '$isi->password',
				'email' => '$isi->email',
				'date_created' => '$isi->date_created',
				'foto_user' => '$isi->foto_user',
				'id_group' => '$isi->id_group',
				'kode_jurusan' => '$isi->kode_jurusan',
				'aktif' => '$isi->aktif'
				),
				";

		}

		$string .=');

		//check group exist
				foreach ($data as $dt) {
			$check = $db->check_exist("sys_users",array("id" => $dt["id"]));
			if ($check==false) {
				$data = array(
					"id" => $dt["id"],
					"first_name" => $dt["first_name"],
					"last_name" => $dt["last_name"],
					"username" => $dt["username"],
					"password" => $dt["password"],
					"email" => $dt["email"],
					"date_created" => $dt["date_created"],
					"foto_user" => $dt["foto_user"],
					"id_group" => $dt["id_group"],
					"kode_jurusan" => $dt["kode_jurusan"],
					"aktif" => $dt["aktif"],
					);
				$db->insert("sys_users",$data);
			} else {
				$data = array(
					"first_name" => $dt["first_name"],
					"last_name" => $dt["last_name"],
					"username" => $dt["username"],
					"password" => $dt["password"],
					"email" => $dt["email"],
					"date_created" => $dt["date_created"],
					"foto_user" => $dt["foto_user"],
					"id_group" => $dt["id_group"],
					"kode_jurusan" => $dt["kode_jurusan"],
					"aktif" => $dt["aktif"],
					);
				$db->update("sys_users",$data,"id",$dt["id"]);
			}
		}
		';

		$zip = new ZipArchive;
		$res = $zip->open('user_backup.zip', ZipArchive::CREATE);
		if ($res === TRUE) {
		    $zip->addFromString('write.php', $string);
		    $zip->close();
	        header('Content-disposition: attachment; filename=user_backup.zip');
	        header('Content-type: application/zip');
	        readfile('user_backup.zip');
		    echo 'ok';
		} else {
		    echo 'failed';
		}