function MinifyJavasript($FilePath) { $PostFields = array('code_url' => '', 'download' => '', 'js_code' => file_get_contents($FilePath)); $PostFields = http_build_query($PostFields); $Result = ClientRequest(array('Url' => 'http://marijnhaverbeke.nl/uglifyjs', 'Post' => True, 'PostFields' => $PostFields)); return $Result; }
/** * Perform client request to server. * Options: see here http://www.php.net/manual/en/function.curl-setopt.php * Bool options: * ReturnTransfer, Post, FollowLocation, Header * Integer options: * ConnectTimeout, Timeout, Timeout_Ms * Other options: * Url, Cookie, CookieFile, CustomRequest, PostFields, Referer, UserAgent, UserPwd * * @param mixed $Url or array $Options. * @return mixed $Result. */ function ClientRequest($Url, $Options = False) { static $Connections = array(); static $ManualFollowLocation; $NumArgs = func_num_args(); if ($NumArgs == 1) { $Options = $Url; if (is_string($Options)) { $Options = array('Url' => $Options); } } else { $Options['Url'] = $Url; } $NewOptions = $Options; $Cache = GetValue('Cache', $Options, False, True); if ($Cache !== False) { $Crc = sprintf('%u', crc32(serialize($Options))); $CacheDirectory = PATH_CACHE . '/client-request'; $CacheFile = $CacheDirectory . DS . $Crc . '.php'; if (file_exists($CacheFile)) { $IncludeCache = True; if (is_int($Cache)) { $LifeTime = time() - filemtime($CacheFile); $Expired = $LifeTime > $Cache; if ($Expired) { $IncludeCache = False; } } if ($IncludeCache) { $Result = (include $CacheFile); return $Result; } } } $Url = GetValue('Url', $Options, False, True); $ConvertEncoding = GetValue('ConvertEncoding', $Options, False, True); $Header = GetValue('Header', $Options); $FollowLocation = GetValue('FollowLocation', $Options); if ($FollowLocation) { if ($ManualFollowLocation === Null) { $ManualFollowLocation = array_filter(array_map('ini_get', array('safe_mode', 'open_basedir'))) > 0; } if ($ManualFollowLocation) { unset($Options['FollowLocation']); } } $GetInfo = GetValue('GetInfo', $Options, False, True); TouchValue('ReturnTransfer', $Options, True); //TouchValue('ConnectTimeout', $Options, 30); //TouchValue('Timeout', $Options, 5); if (!array_key_exists($Url, $Connections)) { $Connections[$Url] = curl_init($Url); } $Connection =& $Connections[$Url]; foreach ($Options as $Option => $Value) { $Constant = 'CURLOPT_' . strtoupper($Option); if (!defined($Constant)) { $InfoConstant = 'CURLINFO_' . strtoupper($Option); if (!defined($InfoConstant)) { trigger_error("cURL. Unknown option: {$Constant} ({$InfoConstant})"); } else { $Constant = $InfoConstant; } } curl_setopt($Connection, constant($Constant), $Value); } $Result = curl_exec($Connection); if ($Result === False) { $ErrorMessage = curl_error($Connection); //$ErrorNo = curl_errno($Connection); trigger_error($ErrorMessage); return False; } if ($Header != False) { $ResponseLines = explode("\n", trim($Result)); $Status = array_shift($ResponseLines); $Response = array(); $Response['HTTP'] = trim($Status); $Response['StatusCode'] = array_pop(array_slice(explode(' ', trim($Status)), 1, 1)); for ($Count = count($ResponseLines), $i = 0; $i < $Count; $i++) { $Line = trim($ResponseLines[$i]); unset($ResponseLines[$i]); if ($Line === '') { break; } $Line = explode(':', $Line); $Key = trim(array_shift($Line)); $Value = trim(implode(':', $Line)); if (!isset($Response[$Key])) { $Response[$Key] = $Value; } else { if (!is_array($Response[$Key])) { $Response[$Key] = array($Response[$Key]); } $Response[$Key][] = $Value; } } $Result = implode("\n", $ResponseLines); unset($ResponseLines); } if ($GetInfo || $ConvertEncoding || $Header) { $Result = array('Result' => $Result); $Result['Info'] = curl_getinfo($Connection); if ($Header) { $Result['Headers'] = $Response; } } if ($FollowLocation != False && $ManualFollowLocation) { $Code = GetValueR('Info.http_code', $Result); if (in_array($Code, array(301, 302))) { $Location = GetValueR('Info.redirect_url', $Result); if ($Location === False) { $Location = GetValueR('Headers.Location', $Result); } $NewOptions['Url'] = $Location; return ClientRequest($NewOptions); } } if ($ConvertEncoding) { list($MimeType, $DirtyCharsetInfo) = array_pad(explode(';', $Result['Info']['content_type']), 2, Null); $Result['MimeType'] = $MimeType; preg_match('/charset=(.+)/', $DirtyCharsetInfo, $Match); $Charset = strtolower(GetValue(1, $Match)); if ($Charset && $Charset != 'utf-8') { $Result['Result'] = mb_convert_encoding($Result['Result'], 'utf-8', $Charset); } if (!$GetInfo) { $Result = $Result['Result']; } } if (isset($Result['Info']['content_type'])) { $Tmp = explode(';', $Result['Info']['content_type']); $Type = trim(array_shift($Tmp)); if (substr($Type, -4) == 'json') { $Result['Json'] = json_decode($Result['Result']); } } if ($Cache !== False) { if (!is_dir($CacheDirectory)) { mkdir($CacheDirectory, 0777, True); } $Contents = "<?php if (!defined('APPLICATION')) exit(); \nreturn " . var_export($Result, True) . ';'; file_put_contents($CacheFile, $Contents); } return $Result; }