Exemple #1
1
function HTTP_Send($Target, $Settings, $Get = array(), $Post = array(), $Addins = array())
{
    /******************************************************************************/
    $__args_types = array('string', 'array', 'array', 'string,array', 'array');
    #-------------------------------------------------------------------------------
    $__args__ = Func_Get_Args();
    eval(FUNCTION_INIT);
    /******************************************************************************/
    $Default = array('Protocol' => 'tcp', 'Address' => 'localhost', 'Port' => 8080, 'Host' => 'localhost', 'Basic' => '', 'Charset' => 'UTF-8', 'Hidden' => '', 'IsLogging' => TRUE);
    #-------------------------------------------------------------------------------
    Array_Union($Default, $Settings);
    #-------------------------------------------------------------------------------
    $IsLogging = (bool) $Default['IsLogging'];
    #-------------------------------------------------------------------------------
    $Tmp = System_Element('tmp');
    if (Is_Error($Tmp)) {
        return ERROR | @Trigger_Error('[HTTP_Send]: не удалось определить путь временной директории');
    }
    #-------------------------------------------------------------------------------
    $Config = Config();
    #-------------------------------------------------------------------------------
    $Address = $Default['Address'];
    #-------------------------------------------------------------------------------
    Debug(SPrintF('[HTTP_Send]: соединяемся с (%s:%u)', $Address, $Default['Port']));
    #-------------------------------------------------------------------------------
    # https://bugs.php.net/bug.php?id=52913
    # пришлось заменить: $Address -> $Default['Host']
    $Socket = @FsockOpen(SPrintF('%s://%s', $Protocol = $Default['Protocol'], $Default['Host']), $Port = $Default['Port'], $nError, $sError, $Config['Other']['Libs']['HTTP']['SocketTimeout']);
    if (!Is_Resource($Socket)) {
        #-------------------------------------------------------------------------------
        $IsWrite = IO_Write(SPrintF('%s/logs/http-send.log', $Tmp), SPrintF("%s://%s:%u ошибка соединения\n\n", $Protocol, $Address, $Port));
        if (Is_Error($IsWrite)) {
            return ERROR | @Trigger_Error('[HTTP_Send]: не удалось записать данные в лог файл');
        }
        #-------------------------------------------------------------------------------
        return ERROR | @Trigger_Error('[HTTP_Send]: не удалось соединиться с удаленным хостом');
        #-------------------------------------------------------------------------------
    }
    #-------------------------------------------------------------------------------
    # added by lissyara, 2012-01-04 in 08:42:54 MSK, for JBS-130
    Stream_Set_TimeOut($Socket, $Config['Other']['Libs']['HTTP']['StreamTimeout']);
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
    $Charset = $Default['Charset'];
    #-------------------------------------------------------------------------------
    $Method = Count($Post) > 0 ? 'POST' : 'GET';
    #-------------------------------------------------------------------------------
    $Hidden = $Default['Hidden'];
    #-------------------------------------------------------------------------------
    if (Count($Get)) {
        $Target .= SPrintF('?%s', HTTP_Query($Get, $Charset, $Hidden, $IsLogging));
    }
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
    $Headers[] = SPrintF('%s %s HTTP/1.0', $Method, $Target);
    #-------------------------------------------------------------------------------
    $Headers[] = SPrintF('Host: %s', $Default['Host']);
    #-------------------------------------------------------------------------------
    $Headers[] = 'Connection: close';
    #-------------------------------------------------------------------------------
    $Headers = Array_Merge($Headers, $Addins);
    #-------------------------------------------------------------------------------
    if ($Basic = $Default['Basic']) {
        #-------------------------------------------------------------------------------
        $Basic = Base64_Encode($Basic);
        #-------------------------------------------------------------------------------
        $Headers[] = SPrintF('Authorization: Basic %s', $Basic);
        #-------------------------------------------------------------------------------
    }
    #-------------------------------------------------------------------------------
    $Body = '';
    #-------------------------------------------------------------------------------
    if ($Post) {
        #-------------------------------------------------------------------------------
        if (Is_Array($Post)) {
            #-------------------------------------------------------------------------------
            if (Count($Post) > 0) {
                #-------------------------------------------------------------------------------
                $Headers[] = 'Content-Type: application/x-www-form-urlencoded';
                #-------------------------------------------------------------------------------
                $Body = HTTP_Query($Post, $Charset, $Hidden, $IsLogging);
                #-------------------------------------------------------------------------------
            }
            #-------------------------------------------------------------------------------
        } else {
            #-------------------------------------------------------------------------------
            $Body = Mb_Convert_Encoding($Post, $Charset);
            #-------------------------------------------------------------------------------
        }
        #-------------------------------------------------------------------------------
    }
    #-------------------------------------------------------------------------------
    if ($Length = MB_StrLen($Body, 'ASCII')) {
        $Headers[] = SPrintF('Content-Length: %u', $Length);
    }
    #-------------------------------------------------------------------------------
    $Query = SPrintF("%s\r\n\r\n%s", Implode("\r\n", $Headers), $Body);
    #-------------------------------------------------------------------------------
    Debug(SPrintF("[HTTP_Send]: делаем запрос:\n%s", $Query));
    #-------------------------------------------------------------------------------
    if (!@Fwrite($Socket, $Query)) {
        return ERROR | @Trigger_Error('[HTTP_Send]: не удалось записать в сокет');
    }
    #-------------------------------------------------------------------------------
    # added by lissyara, 2014-01-28 in 14:19:08 MSK, for JBS-130
    Stream_Set_TimeOut($Socket, $Config['Other']['Libs']['HTTP']['StreamTimeout']);
    #-------------------------------------------------------------------------------
    $Receive = '';
    #-------------------------------------------------------------------------------
    do {
        #-------------------------------------------------------------------------------
        $Bytes = @FGets($Socket);
        #-------------------------------------------------------------------------------
        $Receive .= $Bytes;
        #-------------------------------------------------------------------------------
    } while ($Bytes);
    #-------------------------------------------------------------------------------
    @Fclose($Socket);
    #-------------------------------------------------------------------------------
    if (Preg_Match('/Content-Type:[\\sa-zA-Z0-9\\/\\-\\;]+charset\\=([a-zA-Z0-9\\-]+)/i', $Receive, $Matches)) {
        #-------------------------------------------------------------------------------
        $Receive = Mb_Convert_Encoding($Receive, 'UTF-8', Next($Matches));
        #-------------------------------------------------------------------------------
    } else {
        #-------------------------------------------------------------------------------
        $Receive = Mb_Convert_Encoding($Receive, 'UTF-8', $Default['Charset']);
        #-------------------------------------------------------------------------------
    }
    #-------------------------------------------------------------------------------
    Debug(SPrintF("[HTTP_Send]: получили ответ:\n%s", $Receive));
    #-------------------------------------------------------------------------------
    $Log = SPrintF("%s://%s:%u [%s]\n%s\n%s\n\n", $Protocol, $Address, $Port, Date('r'), $Query, $Receive);
    #-------------------------------------------------------------------------------
    if ($Hidden) {
        #-------------------------------------------------------------------------------
        if (!Is_Array($Hidden)) {
            $Hidden = array($Hidden);
        }
        #-------------------------------------------------------------------------------
        foreach ($Hidden as $Pattern) {
            #-------------------------------------------------------------------------------
            $Pattern = UrlEncode(Mb_Convert_Encoding($Pattern, $Charset));
            #-------------------------------------------------------------------------------
            $Log = Str_Replace($Pattern, SPrintF('[HIDDEN=(%u)]', StrLen($Pattern)), $Log);
            #-------------------------------------------------------------------------------
        }
        #-------------------------------------------------------------------------------
    }
    #-------------------------------------------------------------------------------
    if ($Default['IsLogging']) {
        #-------------------------------------------------------------------------------
        $IsWrite = IO_Write(SPrintF('%s/logs/http-send.log', $Tmp), $Log);
        if (Is_Error($IsWrite)) {
            return ERROR | @Trigger_Error('[HTTP_Send]: не удалось записать данные в лог файл');
        }
        #-------------------------------------------------------------------------------
    }
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
    $Heads = $Body = array();
    #-------------------------------------------------------------------------------
    foreach (Explode("\r\n\r\n", $Receive) as $Chunk) {
        #-------------------------------------------------------------------------------
        if (Preg_Match('#^HTTP/1\\.*#', $Chunk)) {
            #-------------------------------------------------------------------------------
            $Heads[] = $Chunk;
            #-------------------------------------------------------------------------------
        } else {
            #-------------------------------------------------------------------------------
            $Body[] = $Chunk;
            #-------------------------------------------------------------------------------
        }
        #-------------------------------------------------------------------------------
    }
    #-------------------------------------------------------------------------------
    if (SizeOf($Body) < 1) {
        return ERROR | @Trigger_Error('[HTTP_Send]: ответ от сервера не верен');
    }
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
    return array('Heads' => Implode("\r\n\r\n", $Heads), 'Body' => Implode("\r\n\r\n", $Body));
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
    #$Receive = Preg_Split('/\r\n\r\n/',$Receive,PREG_SPLIT_DELIM_CAPTURE);
    #-------------------------------------------------------------------------------
    #if(Count($Receive) < 2)
    #	return ERROR | @Trigger_Error('[HTTP_Send]: ответ от сервера не верен');
    #-------------------------------------------------------------------------------
    #$Receive = Array_Combine(Array('Heads','Body'),$Receive);
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
    #return $Receive;
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
}
Exemple #2
0
function IO_Write($Path, $Data, $IsRewrite = FALSE, $Wait = 3)
{
    /******************************************************************************/
    $__args_types = array('string', 'string', 'boolean', 'integer');
    #-------------------------------------------------------------------------------
    $__args__ = Func_Get_Args();
    eval(FUNCTION_INIT);
    /******************************************************************************/
    Debug(SPrintF('[IO_Write]: запись в файл (%s)', $Path));
    #-------------------------------------------------------------------------------
    if (File_Exists($Path)) {
        #-------------------------------------------------------------------------------
        $Start = Time() + $Wait;
        #-------------------------------------------------------------------------------
        do {
        } while (!Is_Writable($Path) && Time() < $Start);
        #-------------------------------------------------------------------------------
    } else {
        #-------------------------------------------------------------------------------
        $Folder = DirName($Path);
        #-------------------------------------------------------------------------------
        if (!File_Exists($Folder)) {
            #-------------------------------------------------------------------------------
            Debug(SPrintF('[IO_Write]: создание директории (%s)', $Folder));
            #-------------------------------------------------------------------------------
            if (!@MkDir($Folder, 0777, TRUE)) {
                return ERROR | @Trigger_Error(SPrintF('[IO_Write]: не возможно создать директорию (%s)', $Folder));
            }
            #-------------------------------------------------------------------------------
        }
        #-------------------------------------------------------------------------------
    }
    #-------------------------------------------------------------------------------
    $File = @Fopen($Path, $IsRewrite ? 'w' : 'a');
    if (!$File) {
        return ERROR | @Trigger_Error('[IO_Write]: ошибка открытия файла');
    }
    #-------------------------------------------------------------------------------
    if (!@Fwrite($File, $Data)) {
        return ERROR | @Trigger_Error('[IO_Write]: ошибка записи в файл');
    }
    #-------------------------------------------------------------------------------
    if (!@Fclose($File)) {
        return ERROR | @Trigger_Error('[IO_Write]: ошибка закрытия файла');
    }
    #-------------------------------------------------------------------------------
    # почему закомменчено - не знаю. так было.
    #if(Preg_Match('/\/tmp\//',$Path))
    #	if(!@ChMod($Path,0666))
    #		@Trigger_Error('[IO_Write]: ошибка установки прав на файл');
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
    return TRUE;
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
}