/**
  * Get Meta Info from language translation file content.
  * @param mixed 		The contents of the file using file() or get_file_contents().
  * @param array  	A blank array, strings will be returned by association
  * @param array  	Optional associative array of reference strings
  * @return array		The Meta Info in an array
  */
 function getINIMeta($content, &$strings, $ref_strings = null)
 {
     // convert a string to an array
     if (is_string($content)) {
         $content = explode("\n", $content, 10);
     } else {
         if (!is_array($content)) {
             $content = array();
         }
     }
     // look for a Byte-Order-Marker at the start of the file
     $file['bom'] = 'UTF-8';
     if ($content) {
         $bom = strtolower(bin2hex(substr($content[0], 0, 4)));
         if ($bom == '0000feff') {
             $file['bom'] = 'UTF-32 BE';
         } else {
             if ($bom == 'feff0000') {
                 $file['bom'] = 'UTF-32 LE';
             } else {
                 if (substr($bom, 0, 4) == 'feff') {
                     $file['bom'] = 'UTF-16 BE';
                 } else {
                     if (substr($bom, 0, 4) == 'fffe') {
                         $file['bom'] = 'UTF-16 LE';
                     }
                 }
             }
         }
     }
     // parse the top line from one of these two formats
     //	# $Id: en-GB.mod_poll.ini 6167 2007-01-04 01:16:01Z eddiea $
     //	# version 1.5.0 2007-01-25 10:40:16 ~0 +0
     if (strpos($content[0], '.ini')) {
         $line = preg_replace('/^.*[.]ini[ ]+/', '', $content[0]);
         list($file['version'], $file['date'], $file['time'], $file['owner'], $file['complete']) = explode(' ', $line . '   ', 6);
         $file['headertype'] = 1;
     } else {
         $line = preg_replace('/^.*version/i', '', $content[0]);
         $line = trim($line);
         list($file['version'], $file['date'], $file['time'], $file['complete']) = explode(' ', $line . '   ', 5);
         $file['owner'] = '';
         $file['headertype'] = 2;
     }
     // tidy up the values
     $file['complete'] = preg_replace('/[^0-9]/', '', $file['complete']);
     $file['author'] = preg_replace('/^.*author[ ]+/i', '', trim($content[1], '# '));
     $file['copyright'] = preg_replace('/^.*copyright[ ]+/i', '', trim($content[2], '# '));
     $file['license'] = preg_replace('/^.*license[ ]+/i', '', trim($content[3], '# '));
     // parse the strings in the file into an associative array
     $strings = array();
     foreach ($content as $line) {
         $line = trim($line);
         // 1: skip comments and blanks
         // 2: get the ucase key and value
         if (empty($line) || $line[0] == '#' || $line[0] == ';') {
             continue;
         } else {
             if (strpos($line, '=')) {
                 list($key, $value) = explode('=', $line, 2);
                 $key = strtoupper($key);
                 $strings[$key] = $value;
             }
         }
     }
     // get the status compared to the ref strings
     $file = array_merge($file, TranslationsHelper::getINIstatus($ref_strings, $strings));
     // set a complete flag
     if ($file['complete'] == $file['unchanged'] && $file['missing'] == 0) {
         $file['status'] = 100;
     }
     // return
     return $file;
 }
示例#2
0
    /**
     * Package INI files into an installation zip file
     */
    function package()
    {
        // variables
        global $mainframe;
        $options =& $this->getOptions();
        $files = array();
        // set the zip path
        // optionally change the tag if there is a [tag=xx-XX] in the path
        $zippath = '/' . $options['config']->get('zippath', 'tmp/[tag].[client].zip');
        $ziptag = $options['lang'];
        if (preg_match('/\\[tag=([^\\]]*)\\]/', $zippath, $match)) {
            $zippath = str_replace($match[0], '[tag]', $zippath);
            if (preg_match('/^[a-z]{2}-[a-z]{2}$/i', $match[1])) {
                $ziptag = strtolower(substr($match[1], 0, 2)) . '-' . strtoupper(substr($match[1], -2));
                $mainframe->enqueueMessage(sprintf(JText::_('ZIP Translate Tag'), $options['lang'], $ziptag));
            }
        }
        // process all the files in the selected language directory into an array ready to be packaged
        // translate the language tag if configured
        jimport('joomla.filesystem.file');
        foreach (JFolder::files($options['langPath']) as $k => $filename) {
            // 1: grab the XML data and info
            // 2a: skip checkout marker files
            // 2b: grab the INI data into the files array
            if ($filename == $options['lang'] . '.xml') {
                $xmlname = $filename;
                $xmldata = file_get_contents($options['langPath'] . DS . $filename);
                $xmltime = filemtime($options['langPath'] . DS . $filename);
                $xml = TranslationsHelper::getXMLMeta($options['langPath'] . DS . $filename);
                $xml['client'] = $options['clientKey'];
                $xml['tag'] = $ziptag;
                $xmlname = str_replace($options['lang'], $ziptag, $xmlname);
                $xmldata = str_replace($options['lang'], $ziptag, $xmldata);
            } else {
                if (substr($filename, -4) == '.ini') {
                    if (substr($filename, 0, 4) == 'chk.') {
                        continue;
                    } else {
                        $k = substr($filename, 0, 3) == 'xx.' ? substr($filename, 3) : $filename;
                        $k = str_replace($options['lang'], $ziptag, $k);
                        $files[$k]['name'] = $k;
                        $files[$k]['data'] = file_get_contents($options['langPath'] . DS . $filename);
                        $files[$k]['time'] = filemtime($options['langPath'] . DS . $filename);
                    }
                }
            }
        }
        // check we have XML data and files
        if (!isset($xml)) {
            JError::raiseNotice(500, sprintf(JText::_('ZIP No XML'), $options['clientKey'] . 'DS' . $options['lang'] . '.xml'));
        } else {
            if (!count($files)) {
                JError::raiseNotice(500, sprintf(JText::_('ZIP No Files'), $options['clientKey']));
            } else {
                // sort the files
                ksort($files);
                // build the XML install file
                $install = '<?xml version="1.0" encoding="utf-8" ?>
<install version="1.5" client="' . $xml['client'] . '" type="language">
    <name>' . $xml['name'] . '</name>
    <tag>' . $xml['tag'] . '</tag>
    <version>' . $xml['version'] . '</version>
    <creationDate>' . $xml['creationDate'] . '</creationDate>
    <author>' . $xml['author'] . '</author>
    <authoremail>' . $xml['authorEmail'] . '</authoremail>
    <authorurl>' . $xml['authorUrl'] . '</authorurl>
    <copyright>' . $xml['copyright'] . '</copyright>
    <license>' . $xml['license'] . '</license>
    <description>' . $xml['description'] . '</description>
    <files>';
                foreach ($files as $k => $v) {
                    $install .= '
		<filename>' . $v['name'] . '</filename>';
                }
                $install .= '
		<filename file="meta">' . $xmlname . '</filename>
	</files>
	<params />
</install>';
                // finish the files array
                $files['xml']['name'] = $xmlname;
                $files['xml']['data'] = $xmldata;
                $files['xml']['date'] = $xmltime;
                $files['install']['name'] = 'install.xml';
                $files['install']['data'] = $install;
                $files['install']['date'] = time();
                // configure the package filename and type
                $type = substr($zippath, strrpos($zippath, '.') + 1);
                $zippath = str_replace('[client]', $xml['client'], $zippath);
                $zippath = str_replace('[tag]', $xml['tag'], $zippath);
                $ziproot = JPATH_ROOT . $zippath;
                $zipfile = substr($zippath, strrpos($zippath, DS) + 1);
                $ziplink = JURI::root() . $zipfile;
                // run the packager
                jimport('joomla.filesystem.archive');
                if (!($packager =& JARchive::getAdapter($type))) {
                    JError::raiseWarning(500, sprintf(JText::_('ZIP Adapter Failure'), $type));
                } else {
                    if ($packager->create($ziproot, $files, array())) {
                        $mainframe->enqueueMessage(sprintf(JText::_('ZIP Create Success'), $ziplink, $zipfile));
                    } else {
                        JError::raiseNotice(500, sprintf(JText::_('ZIP Create Failure'), $ziplink));
                    }
                }
            }
        }
        // Redirect to the default page
        $mainframe->redirect('index.php?option=com_translationsmanager');
    }