/**
* List all files of a directory recursively
*/
function scandir_rec($dir, $original_dir_length = 0)
{
    $invisible_file_names = array(".", "..", ".htaccess", ".htpasswd");
    $return_data = array();
    if (empty($original_dir_length)) {
        $original_dir_length = strlen($dir) + 1;
    }
    $dir_content = scandir($dir);
    foreach ($dir_content as $content) {
        $path = $dir . '/' . $content;
        if (!in_array($content, $invisible_file_names)) {
            if (is_file($path) && is_readable($path)) {
                $return_data[] = substr($path, $original_dir_length);
            } elseif (is_dir($path) && is_readable($path)) {
                $return_data = array_merge($return_data, scandir_rec($path, $original_dir_length));
            }
        }
    }
    return $return_data;
}
 /**
  * Merge MOD and the according language pack together
  *
  * @param bool $test Whether to generate the MOD package or just test the language pack
  */
 public function merge_packs($test = false)
 {
     global $phpbb_root_path, $config;
     /**
      * Unzip both packages
      */
     $mod = new compress_zip('r', $phpbb_root_path . $config['mods_tmp_dir_path'] . 'mods/' . $this->filename . '.zip');
     $mod->extract($phpbb_root_path . $config['mods_tmp_dir_path'] . 'mods/');
     /* ZipArchive is not currently supported on the server - leave this here for possible later use
     		$mod = new ZipArchive();
     		$mod->open($config['mods_tmp_dir_path'] . 'mods/' . $this->filename . '.zip');
     		$mod->extractTo($config['mods_tmp_dir_path'] . 'mods/');*/
     // If no localisation pack exists then we just check the original MOD package to make sure all Hungarian translation files are included
     if (file_exists($phpbb_root_path . $config['mods_tmp_dir_path'] . 'localisations/' . $this->filename . '.zip')) {
         $loc = new compress_zip('r', $phpbb_root_path . $config['mods_tmp_dir_path'] . 'localisations/' . $this->filename . '.zip');
         $loc->extract($phpbb_root_path . $config['mods_tmp_dir_path'] . 'localisations/');
     }
     /**
      * Merge packages
      */
     $errors = array();
     // Introduce variables with short names for frequently used file paths
     $mod_dir = $phpbb_root_path . $config['mods_tmp_dir_path'] . 'mods/' . $this->filename;
     $loc_dir = $phpbb_root_path . $config['mods_tmp_dir_path'] . 'localisations/' . $this->filename;
     // First look at the language files in the mods directory
     if (file_exists($mod_dir . '/root/language/en/') && !file_exists($mod_dir . '/root/language/hu/')) {
         $files = scandir_rec($mod_dir . '/root/language/en/');
         foreach ($files as $file) {
             if (!file_exists($loc_dir . '/root/language/hu/' . $file)) {
                 $errors[] = array('MISSING_LANGUAGE_FILE', 'root/language/hu/' . $file);
             } else {
                 // Check PHP syntax (assume we are on a unix-based system)
                 if (substr(shell_exec('php -l ' . $loc_dir . '/root/language/hu/' . $file), 0, 11) == 'Parse error') {
                     $errors[] = array('SYNTAX_ERROR', 'root/language/hu/' . $file);
                 } else {
                     $dir_name = $mod_dir . '/root/language/hu/' . site_dirname($file);
                     if (!file_exists($dir_name)) {
                         mkdir($dir_name, 0755, true);
                     }
                     rename($loc_dir . '/root/language/hu/' . $file, $mod_dir . '/root/language/hu/' . $file);
                 }
             }
         }
     }
     // Next the styles directory
     if (file_exists($mod_dir . '/root/styles/prosilver/imageset/en/') && !file_exists($mod_dir . '/root/styles/prosilver/imageset/hu/')) {
         // Check whether prosilver images are in place
         $files = scandir_rec($mod_dir . '/root/styles/prosilver/imageset/en/');
         foreach ($files as $file) {
             if (!file_exists($loc_dir . '/root/styles/prosilver/imageset/hu/' . $file)) {
                 $errors[] = array('MISSING_STYLE_IMAGE', '/root/styles/prosilver/imageset/hu/' . $file);
             }
         }
         // Copy all image files
         $files = scandir_rec($loc_dir . '/root/styles/');
         foreach ($files as $file) {
             if (substr($file, -4) == '.gif' && !file_exists($loc_dir . '/root/styles/' . $file)) {
                 $dir_name = $mod_dir . '/root/styles/' . site_dirname($file);
                 if (!file_exists($dir_name)) {
                     mkdir($dir_name, 0755, true);
                 }
                 rename($loc_dir . '/root/styles/' . $file, $mod_dir . '/root/styles/' . $file);
             }
         }
     }
     // Copy the entire contrib directory
     if (file_exists($loc_dir . '/contrib/')) {
         $files = scandir_rec($loc_dir . '/contrib/');
         foreach ($files as $file) {
             if (!file_exists($mod_dir . '/contrib/' . $file)) {
                 $dir_name = $mod_dir . '/contrib/' . site_dirname($file) . '/';
                 if (!file_exists($dir_name)) {
                     mkdir($dir_name, 0755, true);
                 }
                 rename($loc_dir . '/contrib/' . $file, $mod_dir . '/contrib/' . $file);
             }
         }
     }
     // Now the Hungarian MODX file
     if (file_exists($loc_dir . '/languages/hu.xml') && !file_exists($mod_dir . '/languages/hu.xml')) {
         if (!file_exists($mod_dir . '/languages/')) {
             mkdir($mod_dir . '/languages/', 0755);
         }
         rename($loc_dir . '/languages/hu.xml', $mod_dir . '/languages/hu.xml');
     }
     // Style localisations
     if (file_exists($loc_dir . '/templates/')) {
         $files = scandir_rec($loc_dir . '/templates/');
         foreach ($files as $file) {
             if (preg_match('#^([^/]+)\\/hu\\.xml$#is', $file, $match) && !file_exists($mod_dir . '/templates/' . $file)) {
                 mkdir($mod_dir . '/templates/' . $match[1], 0755, true);
                 rename($loc_dir . '/templates/' . $file, $mod_dir . '/templates/' . $file);
             }
         }
     }
     // And finally merge the translation and the original version of install.xml (or alternatively MOD_NAME.xml)
     // @todo implement the merge
     /**
      * "Zip everything back"
      */
     if (!empty($errors)) {
         throw new ModException($errors);
     }
     if ($test) {
         return true;
     }
     // Remove old file?
     if (file_exists($phpbb_root_path . $config['downloads_path'] . '/mods/' . $this->filename . '.zip')) {
         unlink($phpbb_root_path . $config['downloads_path'] . '/mods/' . $this->filename . '.zip');
     }
     /* ZipArchive is not supported on the server - but leave this here for possible later use
     		$final = new DirZipArchive();
     		$final->open('$config['downloads_path'] . '/mods/' . $this->filename . '.zip', ZIPARCHIVE::CREATE);
     		$final->addDir($mod_dir . '/', $this->filename);
     		$final->close();*/
     // Generate final MOD pack
     $final = new compress_zip('w', $phpbb_root_path . $config['downloads_path'] . '/mods/' . $this->filename . '.zip');
     $filelist = scandir_rec($mod_dir . '/');
     foreach ($filelist as $file) {
         // Add file to archive
         $final->add_custom_file($mod_dir . '/' . $file, $this->filename . '/' . $file);
     }
     $final->close();
     $this->data['size'] = filesize($phpbb_root_path . $config['downloads_path'] . '/mods/' . $this->filename . '.zip');
     return true;
 }