<?php

// Pashua Stuff

/**
 * Static class which wraps the two simple methods used for communicating with Pashua
 */
class Pashua
{
    /**
     * Invokes a Pashua dialog window with the given window configuration
     *
     * @param string $conf           Configuration string to pass to Pashua
     * @param string $customLocation Filesystem path to directory containing Pashua
     *
     * @throws \RuntimeException
     * @return array Associative array of values returned by Pashua
     */
    public static function showDialog($conf, $customLocation = null)
    {
        if (ini_get('safe_mode')) {
            $msg = "To use Pashua you will have to disable safe mode or " .
                "change " . __FUNCTION__ . "() to fit your environment.\n";
            fwrite(STDERR, $msg);
            exit(1);
        }

        // Write configuration string to temporary config file
        $configfile = tempnam('/tmp', 'Pashua_');
        if (false === $fp = @fopen($configfile, 'w')) {
            throw new \RuntimeException("Error trying to open $configfile");
        }
        fwrite($fp, $conf);
        fclose($fp);

        $path = __DIR__."/Pashua.app/Contents/MacOS/Pashua";

        // Call pashua binary with config file as argument and read result
        $result = shell_exec(escapeshellarg($path) . ' ' . escapeshellarg($configfile));

        @unlink($configfile);

        // Parse result
        $parsed = array();
        foreach (explode("\n", $result) as $line) {
            preg_match('/^(\w+)=(.*)$/', $line, $matches);
            if (empty($matches) or empty($matches[1])) {
                continue;
            }
            $parsed[$matches[1]] = $matches[2];
        }

        return $parsed;
    }
}

function makeWindowString($p, $strings) {

	$conf = "
	# Set window title
	*.title = Preferences
	*.floating = 1

	bdest.type = openbrowser
	bdest.filetype = directory
	bdest.label = Destination
	bdest.default = ".$p['bdest']."
	bdest.width = 400

	postflight.type = popup
	postflight.label = When finished
	postflight.option = ".$strings[0][0]."
	postflight.option = ".$strings[0][1]."
	postflight.option = ".$strings[0][2]."
	postflight.default = ".$strings[0][$p['postflight']]."
	postflight.width = 400

	rsync_dest.type = textfield
	rsync_dest.label = rsync destination
	rsync_dest.default = ".$p['rsync_dest']."
	rsync_dest.placeholder = user@server.com:files/
	rsync_dest.width = 400

	hr1.type = image
	hr1.path = ".__DIR__."/hr.png"."
	hr1.width = 400
	hr1.height = 2

	readability.type = checkbox
	readability.label = Abort if files can't be read
	readability.default = ".$p['readability']."

	fixatimes.type = checkbox
	fixatimes.label = Fix atimes after access
	fixatimes.default = ".$p['fixatimes']."

	verify_stat.type = checkbox
	verify_stat.label = Verify preflight and postflight stat values match
	verify_stat.default = ".$p['verify_stat']."

	hr2.type = image
	hr2.path = ".__DIR__."/hr.png"."
	hr2.width = 400
	hr2.height = 2

	spotlight.type = checkbox
	spotlight.label = Spotlight
	spotlight.default = ".$p['spotlight']."

	meta.type = checkbox
	meta.label = Collect external metadata
	meta.default = ".$p['meta']."
	meta.x = 200
	meta.y = 227

	thumbs.type = popup
	thumbs.label = Thumbnails
	thumbs.option = ".$strings[1][0]."
	thumbs.option = ".$strings[1][1]."
	thumbs.option = ".$strings[1][2]."
	thumbs.default = ".$strings[1][$p['thumbs']]."
	thumbs.width = 140

	thumbs.type = popup
	thumbs.label = Thumbnails
	thumbs.option = ".$strings[1][0]."
	thumbs.option = ".$strings[1][1]."
	thumbs.option = ".$strings[1][2]."
	thumbs.default = ".$strings[1][$p['thumbs']]."
	thumbs.width = 120

	thumb_priority.type = popup
	thumb_priority.label = Priority
	thumb_priority.option = ".$strings[2][0]."
	thumb_priority.option = ".$strings[2][1]."
	thumb_priority.option = ".$strings[2][2]."
	thumb_priority.option = ".$strings[2][3]."
	thumb_priority.default = ".$strings[2][$p['thumb_priority']]."
	thumb_priority.width = 120
	thumb_priority.x = 150
	thumb_priority.y = 162

	thumb_size.type = textfield
	thumb_size.default = ".$p['thumb_size']."
	thumb_size.label = Size
	thumb_size.placeholder = pixels
	thumb_size.width = 60
	thumb_size.x = 300
	thumb_size.y = 165

	hash.type = checkbox
	hash.label = Generate hashes
	hash.default = ".$p['hash']."

	hash_label.type = text
	hash_label.default = Limit
	hash_label.x = 200
	hash_label.y = 128
	hash_label.width = 40

	hash_limit.type = textfield
	hash_limit.default = ".$p['hash_limit']."
	hash_limit.tooltip = Don't hash files larger than X GB
	hash_limit.width = 60
	hash_limit.x = 240
	hash_limit.y = 125

	hash_label_after.type = text
	hash_label_after.default = GB
	hash_label_after.x = 304
	hash_label_after.y = 128
	hash_label_after.width = 40

	contents.type = checkbox
	contents.label = Collect file contents
	contents.default = ".$p['contents']."

	contents_label.type = text
	contents_label.default = Limit
	contents_label.x = 200
	contents_label.y = 88
	contents_label.width = 40

	contents_limit.type = textfield
	contents_limit.tooltip = Don't save files larger than X kB
	contents_limit.default = ".$p['contents_limit']."
	contents_limit.width = 60
	contents_limit.x = 240
	contents_limit.y = 85

	contents_label_after.type = text
	contents_label_after.default = kB
	contents_label_after.x = 304
	contents_label_after.y = 88
	contents_label_after.width = 40

	profile.type = checkbox
	profile.label = Generate system profile
	profile.default = ".$p['profile']."

	cb.type = cancelbutton
	db.type = defaultbutton
	db.label = Save
	";
	
	return $conf;
	
	}

?>