<?php
if (!defined('_GNUBOARD_')) exit;

function generate_filename($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

function get_lotto_info($item, $height, $vspace=30) {
    global $height_array, $top_array;

    if( !$height ) {
        return null;
    }

    $info['count'] = 0;
    $info['height'] = 0;

    $height_count = count($height_array[$item]);

    for( $i=1; $i <= $height_count; $i++ ) {
        if( $height_array[$item][$i] - $vspace <= $height && $height <= $height_array[$item][$i] + $vspace ) {
            $info['count'] = $i;
            $info['height'] = $height_array[$item][$i];
            $info['crop_y'] = $top_array[$item][$i];
        }
    }

    return $info;
}

function get_lotto_info2($item, $height, $vspace=30) {
    global $height_array2, $top_array2;

    if( !$height ) {
        return null;
    }

    $info['count'] = 0;
    $info['height'] = 0;

    $height_count = count($height_array2[$item]);

    for( $i=1; $i <= $height_count; $i++ ) {
        if( $height_array2[$item][$i] - $vspace <= $height && $height <= $height_array2[$item][$i] + $vspace ) {
            $info['count'] = $i;
            $info['height'] = $height_array2[$item][$i];
            $info['crop_y'] = $top_array2[$item][$i];
        }
    }

    return $info;
}

function get_lotto_info3($item, $height, $vspace=30) {
    global $height_array3;

    if( !$height ) {
        return null;
    }

    $info['count'] = 0;
    $info['height'] = 0;

    $height_count = count($height_array3[$item]);

    for( $i=1; $i <= $height_count; $i++ ) {
        if( $height_array3[$item][$i] - $vspace <= $height && $height <= $height_array3[$item][$i] + $vspace ) {
            $info['count'] = $i;
            $info['height'] = $height_array3[$item][$i];
        }
    }

    return $info;
}

function get_lotto_info4($item, $height, $vspace=30) {
    global $height_array4;

    if( !$height ) {
        return null;
    }

    $info['count'] = 0;
    $info['height'] = 0;

    $height_count = count($height_array4[$item]);

    for( $i=1; $i <= $height_count; $i++ ) {
        if( $height_array4[$item][$i] - $vspace <= $height && $height <= $height_array4[$item][$i] + $vspace ) {
            $info['count'] = $i;
            $info['height'] = $height_array4[$item][$i];
        }
    }

    return $info;
}

## 원격 이미지 엑박검사 || 유효성검사
function file_exit_check($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL,$url); // don't download content
    curl_setopt($curl, CURLOPT_NOBODY, 1);
    curl_setopt($curl, CURLOPT_FAILONERROR, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    return (curl_exec($curl)!==FALSE)? true : false;
}

function array_strlen_filter($array) {
    return array_filter($array, function($var) {
               if( strlen($var) == 2 ) {
                   return $var;
               }
    });
}

function array_strlen_filter2($array) {
    $temp = implode('', $array);

    $size = floor(strlen($temp) / 2);
    $numbers = str_split($temp, 2);

    return array_slice($numbers, 0 , $size);
}

function naver_ocr_api($file_name, $file_url, $file_type) {
    if( !$file_name || !$file_url ) {
        return array();
    }

    if( !in_array($file_type, array('jpg','jpeg','png')) ) {
        return array();
    }

    $post_field = array();
    $post_field["version"] = "V2";
    $post_field["requestId"] = $file_name;
    $post_field["timestamp"] = time();
      
    // 첫 번째 코드와 동일
    $images = array("format" => $file_type, "name" => "demo", "url" => $file_url);
    // JSON Array를 연관 배열로 저장 키이름은 images
    $post_field["images"] = array($images);
    $curl = curl_init();
    $api_url = 'https://zgmsmnb1s8.apigw.ntruss.com/custom/v1/16034/455221c6cfa4c571ba167d55cdb681a9e9b0756a0c694689cbdb1a60b846cd95/general';

    $timeout = 10; // 1 second timeout
    $header = array(
        'Content-Type: application/json',
        'X-OCR-SECRET: cVpNYk50c0dKV2t2UlNyenJFVUlXUEJsbUpGSnRJSU0='
    );

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $api_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_HEADER, 0);// 헤더를 포함한다.
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_field));
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

    $response = curl_exec($curl);
    $error = curl_error($curl);

    curl_close($curl);

    $response = json_decode($response);

    $text_array = array();
    $count = count($response->images[0]->fields);
    for( $i=0; $i < $count; $i++ ) {
        $text = preg_replace("/[^0-9]*/s", "", $response->images[0]->fields[$i]->inferText);
        if( $text ) {
            $text_array[] = $text;
        }
    }

    return $text_array;
}