<commit_before><?php
/*
 * @author Anakeen
 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
 * @package FDL
*/
/**
 * Function to dialog with transformation server engine
 *
 * @author Anakeen 2007
 * @version $Id: Class.TEClient.php,v 1.12 2007/08/14 09:39:33 eric Exp $
 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
 * @package FDL
 */
/**
 */

include_once ("WHAT/Lib.FileMime.php");

class TransformationEngine
{
    const error_connect = - 2;
    const error_noengine = - 3;
    const error_sendfile = - 4;
    const error_emptyfile = - 5;
    const error_convert = - 1;
    const status_inprogress = 2;
    const status_waiting = 3;
    const status_done = 1;
    /**
     * host name of the transformation engine server
     * @private string
     */
    private $host = 'localhost';
    /**
     * port number of the transformation engine server
     * @private int
     */
    private $port = 51968;
    /**
     * initialize host and port
     * @param string $host host name
     * @param int $port port number
     *
     */
    function __construct($host = "localhost", $port = 51968)
    {
        if ($host != "") $this->host = $host;
        if ($port > 0) $this->port = $port;
    }
    /**
     * send a request to do a transformation
     * @param string $te_name Engine name
     * @param string $fkey foreign key
     * @param string $filename the path where is the original file
     * @param string $callback url to activate after transformation is done
     * @param array &$info transformation task info return "tid"=> ,"status"=> ,"comment=>
     *
     * @return string error message, if no error empty string
     */
    function sendTransformation($te_name, $fkey, $filename, $callback, &$info)
    {
        $err = "";
        
        clearstatcache(); // to reset filesize
        $size = filesize($filename);
        if ($size > 0) {
            /* Lit l'adresse IP du serveur de destination */
            $address = gethostbyname($this->host);
            $service_port = $this->port;
            /* Cree une socket TCP/IP. */
            //  echo "Essai de connexion à '$address' sur le port '$service_port'...\n";
            //    $result = socket_connect($socket, $address, $service_port);
            $timeout = floatval(getParam("TE_TIMEOUT", 3));
            $fp = @stream_socket_client("tcp://$address:$service_port", $errno, $errstr, $timeout);
            
            if (!$fp) {
                $err = _("socket creation error") . " : $errstr ($errno)\n";
                $info = array(
                    "status" => self::error_connect
                );
            }
            
            if ($err == "") {
                $in = "CONVERT\n";
                // echo "Envoi de la commande $in ...";
                fputs($fp, $in);
                
                $out = trim(fgets($fp, 2048));
                //      echo "[$out].\n";
                if ($out == "Continue") {
                    $basename = str_replace('"', '_', basename($filename));
                    $mime = getSysMimeFile($filename, $basename);
                    
                    $in = "<TE name=\"$te_name\" fkey=\"$fkey\" fname=\"$basename\" size=\"$size\" mime=\"$mime\" callback=\"$callback\"/>\n";
                    #echo "Envoi du header $in ...";
                    fputs($fp, $in);
                    $out = trim(fgets($fp));
                    $status = "KO";
                    if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $out, $match)) {
                        $status = $match[1];
                    }
                    if ($status == 'OK') {
                        //echo "Envoi du fichier $filename ...";
                        if (file_exists($filename)) {
                            $handle = @fopen($filename, "r");
                            if ($handle) {
                                while (!feof($handle)) {
                                    $buffer = fread($handle, 2048);
                                    $cout = fwrite($fp, $buffer, strlen($buffer));
                                }
                                fclose($handle);
                            }
                            
                            fflush($fp);
                            //echo "OK.\n";
                            // echo "Lire la réponse : \n\n";
                            $out = trim(fgets($fp));
                            if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $out, $match)) {
                                $status = $match[1];
                            }
                            if (preg_match("/<response[^>]*>(.*)<\/response>/i", $out, $match)) {
                                $outmsg = $match[1];
                            }
                            //echo "Response [$status]\n";
                            //echo "Message [$outmsg]\n";
                            if ($status == "OK") {
                                if (preg_match("/ id=[ ]*\"([^\"]*)\"/i", $outmsg, $match)) {
                                    $tid = $match[1];
                                }
                                if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $outmsg, $match)) {
                                    $status = $match[1];
                                }
                                if (preg_match("/<comment>(.*)<\/comment>/i", $outmsg, $match)) {
                                    $comment = $match[1];
                                }
                                $info = array(
                                    "tid" => $tid,
                                    "status" => $status,
                                    "comment" => $comment
                                );
                            } else {
                                $err = $outcode . " [$outmsg]";
                            }
                        }
                    } else {
                        $taskerr = '-';
                        if (preg_match("/<comment>(.*)<\/comment>/i", $out, $match)) {
                            $info = array(
                                "status" => self::error_noengine
                            );
                            $err = $match[1];
                        } else {
                            $err = _("Error sending file");
                            $info = array(
                                "status" => self::error_sendfile
                            );
                        }
                    }
                }
                //echo "Fermeture de la socket...";
                fclose($fp);
            }
        } else {
            $err = _("empty file");
            $info = array(
                "status" => self::error_emptyfile
            );
        }
        return $err;
    }
    /**
     * send a request to get information about a task
     * @param int $tid_task identificator<commit_msg>Detect connection errors in TEClient (closes #2195)<commit_after><?php
/*
 * @author Anakeen
 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
 * @package FDL
*/
/**
 * Function to dialog with transformation server engine
 *
 * @author Anakeen 2007
 * @version $Id: Class.TEClient.php,v 1.12 2007/08/14 09:39:33 eric Exp $
 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
 * @package FDL
 */
/**
 */

include_once ("WHAT/Lib.FileMime.php");

class TransformationEngine
{
    const error_connect = - 2;
    const error_noengine = - 3;
    const error_sendfile = - 4;
    const error_emptyfile = - 5;
    const error_convert = - 1;
    const status_inprogress = 2;
    const status_waiting = 3;
    const status_done = 1;
    /**
     * host name of the transformation engine server
     * @private string
     */
    private $host = 'localhost';
    /**
     * port number of the transformation engine server
     * @private int
     */
    private $port = 51968;
    /**
     * initialize host and port
     * @param string $host host name
     * @param int $port port number
     *
     */
    function __construct($host = "localhost", $port = 51968)
    {
        if ($host != "") $this->host = $host;
        if ($port > 0) $this->port = $port;
    }
    /**
     * send a request to do a transformation
     * @param string $te_name Engine name
     * @param string $fkey foreign key
     * @param string $filename the path where is the original file
     * @param string $callback url to activate after transformation is done
     * @param array &$info transformation task info return "tid"=> ,"status"=> ,"comment=>
     *
     * @return string error message, if no error empty string
     */
    function sendTransformation($te_name, $fkey, $filename, $callback, &$info)
    {
        $err = "";
        
        clearstatcache(); // to reset filesize
        $size = filesize($filename);
        if ($size <= 0) {
            $err = _("empty file");
            $info = array(
                "status" => self::error_emptyfile
            );
            return $err;
        }

        /* Lit l'adresse IP du serveur de destination */
        $address = gethostbyname($this->host);
        $service_port = $this->port;
        /* Cree une socket TCP/IP. */
        //  echo "Essai de connexion à '$address' sur le port '$service_port'...\n";
        //    $result = socket_connect($socket, $address, $service_port);
        $timeout = floatval(getParam("TE_TIMEOUT", 3));
        $fp = @stream_socket_client("tcp://$address:$service_port", $errno, $errstr, $timeout);

        if (!$fp) {
            $err = _("socket creation error") . " : $errstr ($errno)\n";
            $info = array(
                "status" => self::error_connect
            );
            return $err;
        }

        /*
         * TE server should speak first with a "Continue" response
         */
        $out = fgets($fp, 2048);
        if ($out === false ) {
            $err = sprintf(_("Error sending file")." (error reading expected 'Continue' response)");
            $info = array(
                "status" => self::error_sendfile
            );
            return $err;
        }
        if (trim($out) != 'Continue') {
            $err = sprintf(_("Error sending file")." (unexpected response from server: [%s])(server at %s:%s might not be a TE server?)", $out, $address, $service_port);
            $info = array(
                "status" => self::error_sendfile
            );
            return $err;
        }

        /*
         * Send CONVERT request
         */
        $basename = str_replace('"', '_', basename($filename));
        $mime = getSysMimeFile($filename, $basename);

        $in = "CONVERT\n";
        fputs($fp, $in);
        $in = "<TE name=\"$te_name\" fkey=\"$fkey\" fname=\"$basename\" size=\"$size\" mime=\"$mime\" callback=\"$callback\"/>\n";
        fputs($fp, $in);

        /*
         * Receive CONVERT response
         */
        $out = trim(fgets($fp));
        $status = "KO";
        if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $out, $match)) {
            $status = $match[1];
        }
        if ($status == 'OK') {
            //echo "Envoi du fichier $filename ...";
            if (file_exists($filename)) {
                $handle = @fopen($filename, "r");
                if ($handle) {
                    while (!feof($handle)) {
                        $buffer = fread($handle, 2048);
                        $cout = fwrite($fp, $buffer, strlen($buffer));
                    }
                    fclose($handle);
                }

                fflush($fp);
                //echo "OK.\n";
                // echo "Lire la réponse : \n\n";
                $out = trim(fgets($fp));
                if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $out, $match)) {
                    $status = $match[1];
                }
                if (preg_match("/<response[^>]*>(.*)<\/response>/i", $out, $match)) {
                    $outmsg = $match[1];
                }
                //echo "Response [$status]\n";
                //echo "Message [$outmsg]\n";
                if ($status == "OK") {
                    if (preg_match("/ id=[ ]*\"([^\"]*)\"/i", $outmsg, $match)) {
                        $tid = $match[1];
                    }
                    if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $outmsg, $match)) {
                        $status = $match[1];
                    }
                    if (preg_match("/<comment>(.*)<\/comment>/i", $outmsg, $match)) {
                        $comment = $match[1];
                    }
                    $info = array(
                        "tid" => $tid,
                        "status" => $status,
                        "comment" => $comment
                    );
                } else {
                    $err = $outcode . " [$outmsg]";
                }
            }
        } else {
            $taskerr = '-';
            if (preg_match("/<comment>(.*)<\/comment>/i", $out, $match)) {
                $info = array(
                    "status" => self::error_noengine
                );
                $err = $match[1];
            } else {
                $err = _("Error sending file");
                $info = array(
                    "status" => self::error_sendfile
                );
            }
        }

        //echo "Fermeture de la socket...";
        fclose($fp);

        return $err;
    }
    /**
     * send a request to get information about a task
     * @param int $tid_task identificator
{'fraction_non_alphanumeric': 0.15006231214720328, 'fraction_numerical': 0.009236859467780955, 'mean_word_length': 1.2415379559645088, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6403248', 'n_tokens_mistral': 3905, 'n_tokens_neox': 3676, 'n_words': 916}