public static function generateFso($path, $recursive = true)
 {
     if (is_file($path)) {
         $content = FilesHelper::readFile($path);
         $type = 'text';
         $ext = pathinfo($path, PATHINFO_EXTENSION);
         if (in_array($ext, ['jpg', 'jpeg', 'bmp', 'png', 'gif'])) {
             $type = 'data';
             $content = base64_encode($content);
         }
         return array('type' => $type, 'content' => $content);
     }
     if (!is_dir($path)) {
         return array();
     }
     $result = array('type' => 'dir', 'items' => array());
     if ($handle = opendir($path)) {
         while (($name = readdir($handle)) !== false) {
             if (preg_match('#^\\.#', $name)) {
                 continue;
             }
             $result['items'][$name] = FilesHelper::generateFso($path . "/" . $name, $recursive);
         }
         closedir($handle);
     }
     return $result;
 }
 public function replace($matches)
 {
     $path = $this->themeDir . '/' . $matches[3] . $matches[4];
     if (file_exists($path)) {
         $content = FilesHelper::readFile($path);
         $pattern = '~(\\{assign var=hookName value=[\'"])(.*?)([\'"])([^\\}]*)(\\})~';
         $result = preg_replace_callback($pattern, array($this, 'hookReplace'), $content);
         return $result;
     }
     return '';
 }
 public function complete()
 {
     ProviderLog::start('Chunk complete');
     $content = '';
     for ($i = 1, $count = (int) $this->_lastChunk['total']; $i <= $count; $i++) {
         if (!file_exists($this->_chunkFolder . "/{$i}")) {
             trigger_error('Missing chunk #' . $i . ' : ' . implode(' / ', scandir($this->_chunkFolder)), E_USER_NOTICE);
         }
         $data = FilesHelper::readFile($this->_chunkFolder . "/{$i}");
         if (!empty($this->_lastChunk['encode'])) {
             $data = base64_decode($data);
         }
         $content .= $data;
     }
     FilesHelper::removeDir($this->_chunkFolder);
     $content = empty($this->_lastChunk['encode']) ? $content : rawurldecode($content);
     ProviderLog::end('Chunk complete');
     return $content;
 }
 public function restoreDataId($path)
 {
     $content = FilesHelper::readFile($path);
     $key = $this->_getKey($path);
     if (trim($content) && array_key_exists($key, $this->_data)) {
         $diff = $this->_data[$key];
         $lines = preg_split('/(\\R)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
         for ($i = 0; $i < count($lines); $i++) {
             $line = $lines[$i];
             $lineLength = strlen(trim($line));
             if ($lineLength === 0) {
                 continue;
             }
             foreach ($diff as $key => $d) {
                 //if (($lev = levenshtein(substr($line, 0, 255), substr($d['str'], 0, 255))) / $lineLength > .2) continue;
                 if (strcmp($line, $d['str']) !== 0) {
                     continue;
                 }
                 foreach (array_reverse($d['ids']) as $dataId) {
                     if (!array_key_exists('type', $dataId)) {
                         continue;
                     }
                     if ($dataId['type'] === 'attr') {
                         $line = substr_replace($line, sprintf($this->_data_id_string . '="%d"', $dataId['id']), $dataId['offset'], 0);
                     } else {
                         if ($dataId['type'] === 'class') {
                             $line = substr_replace($line, sprintf($this->_data_id_string . '-%d', $dataId['id']), $dataId['offset'], 0);
                         }
                     }
                 }
                 array_splice($diff, $key, 1);
                 break;
             }
             $lines[$i] = $line;
         }
         $content = implode($lines);
     }
     return $content;
 }
function save_config($src_dir, $dest_dir, $user_theme_name)
{
    $config = '/designer/Export/Config.xml';
    $content = preg_replace('/name="([^"]*?)" directory="([^"]*?)"/', 'name="' . $user_theme_name . '" directory="' . $user_theme_name . '"', FilesHelper::readFile($src_dir . $config));
    FilesHelper::writeFile($dest_dir . $config, $content);
}
function theme_load_manifest($version)
{
    $manifests_dir = theme_get_manifests_dir();
    if (file_exists("{$manifests_dir}/{$version}")) {
        $content = FilesHelper::readFile("{$manifests_dir}/{$version}");
        $protocol = Tools::getShopProtocol();
        if ($protocol === 'https://') {
            $content = str_replace('http://', 'https://', $content);
        }
        return $content;
    }
    return false;
}
 public function zipFso($data)
 {
     $base_upload_dir = get_base_upload_dir();
     $tmp_dir = $base_upload_dir . '/zip-data.tmp';
     $zip_file = $base_upload_dir . '/zip-data.zip';
     FilesHelper::emptyDirRecursive($tmp_dir);
     $changed_files = array();
     $replace_data = array();
     $this->_fillThemeStorage($data['fso'], $changed_files, $replace_data, false, $tmp_dir);
     $archive = new PclZip($zip_file);
     if (0 == $archive->create($tmp_dir, PCLZIP_OPT_REMOVE_PATH, $tmp_dir)) {
         throw new Exception("Extract error : " . $archive->errorInfo(true));
     }
     $result = array('result' => 'done', 'data' => base64_encode(FilesHelper::readFile($zip_file)));
     FilesHelper::emptyDirRecursive($tmp_dir);
     FilesHelper::deleteFile($zip_file);
     return $result;
 }