<?php

class AxcotoFontBirdConverterHelper {

    public static function ttf2eot($file) {
        $upfile = $file;
        $eotPath = substr($upfile, 0, strlen($upfile) - 3) . 'eot';

        //Convert font
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_URL, 'http://www.kirsle.net/wizards/ttf2eot.cgi');
        curl_setopt($ch, CURLOPT_REFERER, "http://www.kirsle.net/wizards/ttf2eot.cgi");
        $post = array(
            "font" => "@{$upfile}",
            'action' => 'upload'
        );

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $response = curl_exec($ch);
        curl_close($ch);

        if (strpos($response, 'has been successfully converted into an') === FALSE) {
            return false;
            //exit('File cannot convert into EOT! Try other font');
        }
        preg_match('~src: url\("(.*?)"\);\s\/\* EOT for IE \*\/~is', $response, $result);
        $fontUrl = "http://www.kirsle.net/wizards/ttf2eot-temp/" . $result[1];

        $downloader = curl_init($fontUrl);
        $fp = fopen($eotPath, "w");
        curl_setopt($downloader, CURLOPT_FILE, $fp);
        curl_setopt($downloader, CURLOPT_HEADER, 0);

        curl_exec($downloader);
        curl_close($downloader);
        fclose($fp);
        return true;
    }

    public static function otf2eot($file) {
        $upfile = $file;
        $part = explode('/', $file);
        $fontname = $part[count($part) - 1];
        $fontname = explode('.', $fontname);

        $zipfile = dirname(dirname(__FILE__)) . '/assets/fonts/' . $fontname[0] . '.zip';

        if (file_exists($zipfile)) {
            return true;
            die('Converted');
        }
        //Convert font
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_URL, 'http://www.font2web.com/convert.php');
        curl_setopt($ch, CURLOPT_REFERER, "http://www.font2web.com/");
        $fp = fopen($zipfile, "w");
        curl_setopt($ch, CURLOPT_FILE, $fp);

        $post = array(
            "userfile" => "@{$upfile}",
            'MAX_FILE_SIZE' => '524288'
        );

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $response = curl_exec($ch);
        curl_close($ch);
        fclose($fp);

        $zip = new ZipArchive;
        $zopen = $zip->open($zipfile);
        if (true !== $zopen) {
            return false;
            die('Unzipp errro');
        }

        for ($i = 0; $i < $zip->numFiles; $i++) {
            if (!$info = $zip->statIndex($i)) {
                return false;
                //return new WP_Error('stat_failed', __('Could not retrieve file from archive.'));
            }

            if ('/' != substr($info['name'], -1) && preg_match('#\.(eot)$#i', $info['name'])) {// directory
                copy('zip://' . $zipfile . '#' . $info['name'], 'assets/fonts/' . $fontname[0] . '.eot');
                break;
            }
        }
        return true;
    }

}