Location: PHPKode > scripts > AIRProjectXML > airprojectxml/AIRProjectXML.php
<?php

/**
 * The set of classes to manage Adobe AIR application descriptor files.
 *
 * Classes in package:
 *   AIRProjectXML_initialWindow
 *   AIRProjectXML_icon
 *   AIRProjectXML_fileType
 *   AIRProjectXML_application
 *   AIRProjectXML
 *   AIRProjectXMLParser
 *
 * Version requirements: PHP >= 4.3.0
 * The classes are compatible with Adobe AIR 1.1
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @version  0.1
 * @author   Vagharshak Tozalakyan <hide@address.com>
 * @license  http://www.opensource.org/licenses/mit-license.php
 */

define('APX_SYSTEM_CHROME_STANDARD', 'standard');
define('APX_SYSTEM_CHROME_NONE', 'none');
define('APX_ICON_16', '16x16');
define('APX_ICON_32', '32x32');
define('APX_ICON_48', '48x48');
define('APX_ICON_128', '128x128');
define('APX_FOO', 'undefined');

class AIRProjectXML_initialWindow
{
    var $content = null;
    var $title = null;
    var $systemChrome = null;
    var $transparent = null;
    var $visible = null;
    var $minimizable = null;
    var $maximizable = null;
    var $resizable = null;
    var $width = null;
    var $height = null;
    var $x = null;
    var $y = null;
    var $minSize = null;
    var $maxSize = null;

    function AIRProjectXML_initialWindow($content)
    {
        $content = trim($content);
        if (empty($content)) {
            trigger_error('The required parameter "content" is empty.', E_USER_WARNING);
        }
        $this->content = $content;
    }

}

class AIRProjectXML_icon
{
    var $image16x16 = null;
    var $image32x32 = null;
    var $image48x48 = null;
    var $image128x128 = null;

    function AIRProjectXML_icon($image, $size)
    {
        $image = trim($image);
        if (empty($image)) {
            trigger_error('The required parameter "image" is empty.', E_USER_WARNING);
        }
        if (!in_array($size, array('16x16', '32x32', '48x48', '128x128'))) {
            trigger_error('The specified image size is incorrect.', E_USER_WARNING);
        }
        switch ($size) {
        case '16x16':
            $this->image16x16 = $image;
            break;
        case '32x32':
            $this->image32x32 = $image;
            break;
        case '48x48':
            $this->image48x48 = $image;
            break;
        case '128x128':
            $this->image128x128 = $image;
            break;
        }
    }

}

class AIRProjectXML_fileType
{
    var $name = null;
    var $extension = null;
    var $description = null;
    var $contentType = null;
    var $icon = null;

    function AIRProjectXML_fileType($name, $extension)
    {
        $name = trim($name);
        $extension = trim($extension);
        if (empty($name)) {
            trigger_error('The required parameter "name" is empty.', E_USER_WARNING);
        }
        if (empty($extension)) {
            trigger_error('The required parameter "extension" is empty.', E_USER_WARNING);
        }
        $this->name = $name;
        $this->extension = $extension;
    }

}

class AIRProjectXML_application
{
    var $xmlns = 'http://ns.adobe.com/air/application/1.1';
    var $minimumPatchLevel = null;
    var $id = null;
    var $filename = null;
    var $name = null;
    var $version = null;
    var $description = null;
    var $copyright = null;
    var $initialWindow = null;
    var $installFolder = null;
    var $programMenuFolder = null;
    var $icon = null;
    var $customUpdateUI = null;
    var $allowBrowserInvocation = null;
    var $fileTypes = null;

    function AIRProjectXML_application($id, $filename, $version, $initialWindow)
    {
        $id = trim($id);
        $filename = trim($filename);
        $version = trim($version);
        if (empty($id)) {
            trigger_error('The required parameter "id" is empty.', E_USER_WARNING);
        }
        if (empty($filename)) {
            trigger_error('The required parameter "filename" is empty.', E_USER_WARNING);
        }
        if (empty($version)) {
            trigger_error('The required parameter "version" is empty.', E_USER_WARNING);
        }
        if (strcasecmp(get_class($initialWindow), 'AIRProjectXML_initialWindow')) {
            trigger_error('Incorrect initial window object.', E_USER_WARNING);
        }
        $this->id = $id;
        $this->filename = $filename;
        $this->version = $version;
        $this->initialWindow = $initialWindow;
    }

}

class AIRProjectXML
{
    var $application = null;
    var $indent = "\t";
    var $lineBreak = "\n";

    function AIRProjectXML($application)
    {
        $this->application = $application;
    }

    function _getIndent($depth = 1)
    {
        $indent = '';
        for ($i = 0; $i < $depth; $i++) {
            $indent .= $this->indent;
        }
        return $indent;
    }

    function _getLineBreaks($numBreaks = 1)
    {
        $lineBreaks = '';
        for ($i = 0; $i < $numBreaks; $i++) {
            $lineBreaks .= $this->lineBreak;
        }
        return $lineBreaks;
    }

    function _getNode($name, $value, $depth, $numBreaks, $attributes = '') {
        $node = '';
        if (!empty($value)) {
            $node .= $this->_getIndent($depth);
            $node .= '<' . $name;
            if (!empty($attributes)) {
                $node .= ' ' . $attributes;
            }
            $node .= '>' . $value . '</' . $name . '>';
            $node .= $this->_getLineBreaks(1);
        }
        return $node;
    }

    function _getInitialWindowNode()
    {
        $application = $this->application;
        $initialWindow = $application->initialWindow;
        $node = $this->_getIndent(1) . '<initialWindow>' . $this->_getLineBreaks(1);
        $node .= $this->_getNode('content', $initialWindow->content, 2, 1);
        $node .= $this->_getNode('title', $initialWindow->title, 2, 1);
        $node .= $this->_getNode('systemChrome', $initialWindow->systemChrome, 2, 1);
        $node .= $this->_getNode('transparent', ($initialWindow->transparent ? 'true' : 'false'), 2, 1);
        $node .= $this->_getNode('visible', ($initialWindow->visible ? 'true' : 'false'), 2, 1);
        $node .= $this->_getNode('minimizable', ($initialWindow->minimizable ? 'true' : 'false'), 2, 1);
        $node .= $this->_getNode('maximizable', ($initialWindow->maximizable ? 'true' : 'false'), 2, 1);
        $node .= $this->_getNode('resizable', ($initialWindow->resizable ? 'true' : 'false'), 2, 1);
        $node .= $this->_getNode('width', $initialWindow->width, 2, 1);
        $node .= $this->_getNode('height', $initialWindow->height, 2, 1);
        $node .= $this->_getNode('x', $initialWindow->x, 2, 1);
        $node .= $this->_getNode('y', $initialWindow->y, 2, 1);
        $node .= $this->_getNode('minSize', $initialWindow->minSize, 2, 1);
        $node .= $this->_getNode('maxSize', $initialWindow->maxSize, 2, 1);
        $node .= $this->_getIndent(1) . '</initialWindow>' . $this->_getLineBreaks(1);
        return $node;
    }

    function _getIconNode($object, $depth)
    {
        $node = '';
        if (!is_null($object)) {
            $node .= $this->_getIndent($depth) . '<icon>' . $this->_getLineBreaks(1);
            $node .= $this->_getNode('image16x16', $object->image16x16, $depth + 1, 1);
            $node .= $this->_getNode('image32x32', $object->image32x32, $depth + 1, 1);
            $node .= $this->_getNode('image48x48', $object->image48x48, $depth + 1, 1);
            $node .= $this->_getNode('image128x128', $object->image128x128, $depth + 1, 1);
            $node .= $this->_getIndent($depth) . '</icon>' . $this->_getLineBreaks(1);
        }
        return $node;
    }

    function _getFileTypeNode($object)
    {
        $node = $this->_getIndent(2) . '<fileType>' . $this->_getLineBreaks(1);
        $node .= $this->_getNode('name', $object->name, 3, 1);
        $node .= $this->_getNode('extension', $object->extension, 3, 1);
        $node .= $this->_getNode('description', $object->description, 3, 1);
        $node .= $this->_getNode('contentType', $object->contentType, 3, 1);
        $node .= $this->_getIconNode($object->icon, 3);
        $node .= $this->_getIndent(2) . '</fileType>' . $this->_getLineBreaks(1);
        return $node;
    }

    function _getFileTypesNode()
    {
        $application = $this->application;
        $node  = '';
        if (!is_null($application->fileTypes) && sizeof($application->fileTypes)) {
            $node .= $this->_getIndent(1) . '<fileTypes>' . $this->_getLineBreaks(1);
            foreach ($application->fileTypes as $fileType) {
                $node .= $this->_getFileTypeNode($fileType);
            }
            $node .= $this->_getIndent(1) . '</fileTypes>' . $this->_getLineBreaks(1);
        }
        return $node;
    }

    function _constructXML()
    {
        $application = $this->application;
        $icon = $application->icon;
        $xml = '<?xml version="1.0" encoding="utf-8" ?>' . $this->_getLineBreaks(1);
        $xml .= '<application xmlns="' . $application->xmlns . '"';
        if (!empty($application->minimumPatchLevel)) {
            $xml .= ' minimumPatchLevel="' . $application->minimumPatchLevel . '"';
        }
        $xml .= '>' . $this->_getLineBreaks(1);
        $xml .= $this->_getNode('id', $application->id, 1, 1);
        $xml .= $this->_getNode('filename', $application->filename, 1, 1);
        $xml .= $this->_getNode('name', $application->name, 1, 1);
        $xml .= $this->_getNode('version', $application->version, 1, 1);
        $xml .= $this->_getNode('description', $application->description, 1, 1);
        $xml .= $this->_getNode('copyright', $application->copyright, 1, 1);
        $xml .= $this->_getInitialWindowNode();
        $xml .= $this->_getNode('installFolder', $application->installFolder, 1, 1);
        $xml .= $this->_getNode('programMenuFolder', $application->programMenuFolder, 1, 1);
        $xml .= $this->_getIconNode($icon, 1);
        $xml .= $this->_getNode('customUpdateUI', ($application->customUpdateUI ? 'true' : 'false'), 1, 1);
        $xml .= $this->_getNode('allowBrowserInvocation', ($application->allowBrowserInvocation ? 'true' : 'false'), 1, 1);
        $xml .= $this->_getFileTypesNode();
        $xml .= '</application>';
        return $xml;
    }

    function setSize($width, $height)
    {
        return $width . ' ' . $height;
    }

    function appendMultilanguage($texts)
    {
        $nodes = '';
        foreach ($texts as $lang => $value) {
            $nodes .= '<text xml:lang="' . $lang . '">' . $value . '</text>';
        }
        return $nodes;
    }

    function saveFile($filename)
    {
        $xml = $this->_constructXML();
        if ($fp = fopen($filename, 'w')) {
            fwrite($fp, $xml);
            fclose($fp);
            return true;
        }
        return false;
    }

    function getString()
    {
        return $this->_constructXML();
    }

    function forceDownload($filename)
    {
        header('Content-disposition: attachment; filename=' . $filename);
        header('Content-type: application/octetstream');
        header('Pragma: no-cache');
        header('Expires: 0');
        echo $this->_constructXML();
    }

}

class AIRProjectXMLParser
{
    var $application = null;
    var $parserEncoding = 'UTF-8';
    var $_currentTag = '';
    var $_parentTag = '';

    function AIRProjectXMLParser()
    {
        $initialWindow = new AIRProjectXML_initialWindow(APX_FOO);
        $this->application = new AIRProjectXML_application(APX_FOO, APX_FOO, APX_FOO, $initialWindow);
    }

    function _toBoolean($str)
    {
        if ($str == 'true') {
            return true;
        } elseif ($str == 'false') {
            return false;
        }
        return (bool)$str;
    }

    function _xmlToArray($xml)
    {
        if(empty($xml)) {
            return array();
        }
        if(!function_exists('xml_parser_create')) {
            trigger_error('XML parser module was not found.', E_USER_WARNING);
            return array();
        }
        $parser = xml_parser_create($this->parserEncoding);
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
        xml_parse_into_struct($parser, $xml, $values);
        xml_parser_free($parser);
        $xmlArray = array();
        $parents = array();
        $openedTags = array();
        $arr = array();
        $current = &$xmlArray;
        foreach($values as $data) {
            unset($attributes, $value);
            extract($data);
            $result = array();
            if (isset($value)) {
                $result['__value'] = $value;
            }
            if(isset($attributes)) {
                foreach($attributes as $attr => $val) {
                    $result['__attr'][$attr] = $val;
                }
            }
            if($type == 'open') {
                $parent[$level - 1] = &$current;
                if(!is_array($current) or (!in_array($tag, array_keys($current)))) {
                    $current[$tag] = $result;
                    $current = &$current[$tag];
                } else {
                    if(isset($current[$tag][0])) {
                        array_push($current[$tag], $result);
                    } else {
                        $current[$tag] = array($current[$tag], $result);
                    }
                    $last = count($current[$tag]) - 1;
                    $current = &$current[$tag][$last];
                }
            } elseif($type == 'complete') {
                if(!isset($current[$tag])) {
                    $current[$tag] = $result;
                } else {
                    if(isset($current[$tag][0]) && is_array($current[$tag][0])) {
                        array_push($current[$tag], $result);
                    } else {
                        $current[$tag] = array($current[$tag], $result);
                    }
                }
            } elseif($type == 'close') {
                $current = &$parent[$level - 1];
            }
        }
        return $xmlArray;
    }

    function _getMultilanguage($xmlArray)
    {
        $xml = '';
        if (!isset($xmlArray[0])) {
            $xmlArray = array($xmlArray);
        }
        foreach ($xmlArray as $textArray) {
            if (isset($textArray['__attr']['xml:lang']) && isset($textArray['__value'])) {
                $xml .= '<text xml:lang="' . $textArray['__attr']['xml:lang'] . '">' . $textArray['__value'] . '</text>';
            }
        }
        return $xml;
    }

    function parseMultilanguageString($str)
    {
        $output = array();
        $xml = '<texts>' . $str . '</texts>';
        $xmlArray = $this->_xmlToArray($xml);
        if (!isset($xmlArray['texts']['text'])) {
            return $str;
        }
        if (!isset($xmlArray['texts']['text'][0])) {
            $xmlArray['texts']['text'] = array($xmlArray['texts']['text']);
        }
        foreach ($xmlArray['texts']['text'] as $textArray) {
            if (isset($textArray['__attr']['xml:lang']) && isset($textArray['__value'])) {
                $output[$textArray['__attr']['xml:lang']] = $textArray['__value'];
            }
        }
        return $output;
    }

    function parseXMLString($xml)
    {
        $xmlArray = $this->_xmlToArray($xml);
        if (!isset($xmlArray['application'])) {
            return false;
        }
        if (isset($xmlArray['application']['__attr']['xmlns'])) {
            $this->application->xmlns = $xmlArray['application']['__attr']['xmlns'];
        }
        if (isset($xmlArray['application']['__attr']['minimumPatchLevel'])) {
            $this->application->minimumPatchLevel = $xmlArray['application']['__attr']['minimumPatchLevel'];
        }
        if (isset($xmlArray['application']['id']['__value'])) {
            $this->application->id = $xmlArray['application']['id']['__value'];
        }
        if (isset($xmlArray['application']['filename']['__value'])) {
            $this->application->filename = $xmlArray['application']['filename']['__value'];
        }
        if (isset($xmlArray['application']['name']['__value'])) {
            $this->application->name = $xmlArray['application']['name']['__value'];
        }
        if (isset($xmlArray['application']['version']['__value'])) {
            $this->application->version = $xmlArray['application']['version']['__value'];
        }
        if (isset($xmlArray['application']['description']['__value'])) {
            $this->application->description = $xmlArray['application']['description']['__value'];
        }
        if (isset($xmlArray['application']['copyright']['__value'])) {
            $this->application->copyright = $xmlArray['application']['copyright']['__value'];
        }
        if (isset($xmlArray['application']['initialWindow']['content']['__value'])) {
            $this->application->initialWindow->content = $xmlArray['application']['initialWindow']['content']['__value'];
        }
        if (isset($xmlArray['application']['initialWindow']['title']['__value'])) {
            $this->application->initialWindow->title = $xmlArray['application']['initialWindow']['title']['__value'];
        }
        if (isset($xmlArray['application']['initialWindow']['systemChrome']['__value'])) {
            $this->application->initialWindow->systemChrome = $xmlArray['application']['initialWindow']['systemChrome']['__value'];
        }
        if (isset($xmlArray['application']['initialWindow']['transparent']['__value'])) {
            $this->application->initialWindow->transparent = $this->_toBoolean($xmlArray['application']['initialWindow']['transparent']['__value']);
        }
        if (isset($xmlArray['application']['initialWindow']['visible']['__value'])) {
            $this->application->initialWindow->visible = $this->_toBoolean($xmlArray['application']['initialWindow']['visible']['__value']);
        }
        if (isset($xmlArray['application']['initialWindow']['minimizable']['__value'])) {
            $this->application->initialWindow->minimizable = $this->_toBoolean($xmlArray['application']['initialWindow']['minimizable']['__value']);
        }
        if (isset($xmlArray['application']['initialWindow']['maximizable']['__value'])) {
            $this->application->initialWindow->maximizable = $this->_toBoolean($xmlArray['application']['initialWindow']['maximizable']['__value']);
        }
        if (isset($xmlArray['application']['initialWindow']['resizable']['__value'])) {
            $this->application->initialWindow->resizable = $this->_toBoolean($xmlArray['application']['initialWindow']['resizable']['__value']);
        }
        if (isset($xmlArray['application']['initialWindow']['width']['__value'])) {
            $this->application->initialWindow->width = $xmlArray['application']['initialWindow']['width']['__value'];
        }
        if (isset($xmlArray['application']['initialWindow']['height']['__value'])) {
            $this->application->initialWindow->height = $xmlArray['application']['initialWindow']['height']['__value'];
        }
        if (isset($xmlArray['application']['initialWindow']['x']['__value'])) {
            $this->application->initialWindow->x = $xmlArray['application']['initialWindow']['x']['__value'];
        }
        if (isset($xmlArray['application']['initialWindow']['y']['__value'])) {
            $this->application->initialWindow->y = $xmlArray['application']['initialWindow']['y']['__value'];
        }
        if (isset($xmlArray['application']['initialWindow']['minSize']['__value'])) {
            $this->application->initialWindow->minSize = $xmlArray['application']['initialWindow']['minSize']['__value'];
        }
        if (isset($xmlArray['application']['initialWindow']['maxSize']['__value'])) {
            $this->application->initialWindow->maxSize = $xmlArray['application']['initialWindow']['maxSize']['__value'];
        }
        if (isset($xmlArray['application']['installFolder']['__value'])) {
            $this->application->installFolder = $xmlArray['application']['installFolder']['__value'];
        }
        if (isset($xmlArray['application']['programMenuFolder']['__value'])) {
            $this->application->programMenuFolder = $xmlArray['application']['programMenuFolder']['__value'];
        }
        if (isset($xmlArray['application']['icon']['image16x16']['__value'])) {
            $this->application->icon->image16x16 = $xmlArray['application']['icon']['image16x16']['__value'];
        }
        if (isset($xmlArray['application']['icon']['image32x32']['__value'])) {
            $this->application->icon->image32x32 = $xmlArray['application']['icon']['image32x32']['__value'];
        }
        if (isset($xmlArray['application']['icon']['image48x48']['__value'])) {
            $this->application->icon->image48x48 = $xmlArray['application']['icon']['image48x48']['__value'];
        }
        if (isset($xmlArray['application']['icon']['image128x128']['__value'])) {
            $this->application->icon->image128x128 = $xmlArray['application']['icon']['image128x128']['__value'];
        }
        if (isset($xmlArray['application']['customUpdateUI']['__value'])) {
            $this->application->customUpdateUI = $this->_toBoolean($xmlArray['application']['customUpdateUI']['__value']);
        }
        if (isset($xmlArray['application']['allowBrowserInvocation']['__value'])) {
            $this->application->allowBrowserInvocation = $this->_toBoolean($xmlArray['application']['allowBrowserInvocation']['__value']);
        }
        if (isset($xmlArray['application']['fileTypes']['fileType'])) {
            if (!isset($xmlArray['application']['fileTypes']['fileType'][0])) {
                $xmlArray['application']['fileTypes']['fileType'] = array($xmlArray['application']['fileTypes']['fileType']);
            }
            foreach ($xmlArray['application']['fileTypes']['fileType'] as $fileTypeArray) {
                $fileType = new AIRProjectXML_fileType(APX_FOO, APX_FOO);
                if (isset($fileTypeArray['name']['__value'])) {
                    $fileType->name = $fileTypeArray['name']['__value'];
                }
                if (isset($fileTypeArray['extension']['__value'])) {
                    $fileType->extension = $fileTypeArray['extension']['__value'];
                }
                if (isset($fileTypeArray['description']['__value'])) {
                    $fileType->description = $fileTypeArray['description']['__value'];
                }
                if (isset($fileTypeArray['contentType']['__value'])) {
                    $fileType->contentType = $fileTypeArray['contentType']['__value'];
                }
                if (isset($fileTypeArray['icon']['image16x16']['__value'])) {
                    $fileType->icon->image16x16 = $fileTypeArray['icon']['image16x16']['__value'];
                }
                if (isset($fileTypeArray['icon']['image32x32']['__value'])) {
                    $fileType->icon->image32x32 = $fileTypeArray['icon']['image32x32']['__value'];
                }
                if (isset($fileTypeArray['icon']['image48x48']['__value'])) {
                    $fileType->icon->image48x48 = $fileTypeArray['icon']['image48x48']['__value'];
                }
                if (isset($fileTypeArray['icon']['image128x128']['__value'])) {
                    $fileType->icon->image128x128 = $fileTypeArray['icon']['image128x128']['__value'];
                }
                $this->application->fileTypes[] = $fileType;
            }
        }
        if (isset($xmlArray['application']['name']['text'])) {
            $this->application->name = $this->_getMultilanguage($xmlArray['application']['name']['text']);
        }
        if (isset($xmlArray['application']['description']['text'])) {
            $this->application->description = $this->_getMultilanguage($xmlArray['application']['description']['text']);
        }
        return true;
    }

    function parseXMLFile($filename)
    {
        if (!($xml = file_get_contents($filename))) {
            return false;
        }
        return $this->parseXMLString($xml);
    }

}

?>
Return current item: AIRProjectXML