/** * Prepare SQL query before execution if some arguments are need to be passed to it. * All parameters marked with question (?) symbol in SQL query are replaced with parameters passed after SQL query parameter. * Parameters are properly excaped and surrounded by qutes if needed. * Example: * @code * $sSql = $oDb->prepare("SELECT `a`, `b` from `t` WHERE `c` = ? and `d` = ?", 12, 'aa'); * echo $sSql;// outputs: SELECT `a`, `b` from `t` WHERE `c` = 12 and `d` = 'aa' * $a = $oDb->getAll($sSql); * @endcode * * @param string $sQuery SQL query, parameters for replacing are marked with ? symbol * @param mixed $mixed any number if parameters to replace, number of parameters whould match number of ? symbols in SQL query * @return string with SQL query ready for execution */ function prepare($sQuery) { $aArgs = func_get_args(); $sQuery = array_shift($aArgs); $iPos = 0; foreach ($aArgs as $mixedArg) { if (is_null($mixedArg)) { $s = 'NULL'; } elseif (is_numeric($mixedArg)) { $s = $mixedArg; } else { $s = "'" . mysql_real_escape_string($mixedArg) . "'"; } $i = bx_mb_strpos($sQuery, '?', $iPos); $sQuery = bx_mb_substr_replace($sQuery, $s, $i, 1); $iPos = $i + get_mb_len($s); } return $sQuery; }
/** * Gets file contents by URL. * * @param string $sFileUrl - file URL to be read. * @param array $aParams - an array of parameters to be pathed with URL. * @return string the file's contents. */ function bx_file_get_contents($sFileUrl, $aParams = array(), $bChangeTimeout = false) { if ($aParams) { $sFileUrl = bx_append_url_params($sFileUrl, $aParams); } $sResult = ''; if (function_exists('curl_init')) { $rConnect = curl_init(); curl_setopt($rConnect, CURLOPT_URL, $sFileUrl); curl_setopt($rConnect, CURLOPT_HEADER, 0); curl_setopt($rConnect, CURLOPT_RETURNTRANSFER, 1); curl_setopt($rConnect, CURLOPT_FOLLOWLOCATION, 1); if (bx_mb_strpos($sFileUrl, 'https') !== false) { curl_setopt($rConnect, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($rConnect, CURLOPT_SSL_VERIFYHOST, 0); } if ($bChangeTimeout) { curl_setopt($rConnect, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($rConnect, CURLOPT_TIMEOUT, 3); } $sAllCookies = ''; foreach ($_COOKIE as $sKey => $sValue) { $sAllCookies .= $sKey . "=" . $sValue . ";"; } curl_setopt($rConnect, CURLOPT_COOKIE, $sAllCookies); $sResult = curl_exec($rConnect); curl_close($rConnect); } else { $iSaveTimeout = false; if ($bChangeTimeout) { $iSaveTimeout = ini_get('default_socket_timeout'); ini_set('default_socket_timeout', 3); } $sResult = @file_get_contents($sFileUrl); if ($bChangeTimeout && false !== $iSaveTimeout) { ini_set('default_socket_timeout', $iSaveTimeout); } } return $sResult; }