View source
<?php
define('ACCESS_KEY', variable_get('shrinktheweb_access_key', ''));
define('SECRET_KEY', variable_get('shrinktheweb_secret_key', ''));
define('THUMBNAIL_DIR', file_directory_path() . '/' . variable_get('shrinktheweb_thumbs_folder', '') . '/');
define('THUMBNAIL_URI', file_create_url(THUMBNAIL_DIR) . '/');
define('INSIDE_PAGES', variable_get('shrinktheweb_inside_pages', ''));
define('CUSTOM_MSG_URL', variable_get('shrinktheweb_custom_msg_url', ''));
define('CACHE_DAYS', variable_get('shrinktheweb_cache_days', ''));
define('VER', '2.0.6_drupal6');
define('QUOTA_IMAGE', 'quota.jpg');
define('BANDWIDTH_IMAGE', 'bandwidth.jpg');
define('NO_RESPONSE_IMAGE', 'no_response.jpg');
define('MAINTENANCE', 'ShrinkTheWeb is temporarily offline for maintenance');
define('DEBUG', variable_get('shrinktheweb_debug', ''));
error_reporting(E_ALL ^ E_NOTICE);
function shrinktheweb_getThumbnailURL($sUrl, $aOptions = array()) {
$sImageURL = false;
$aOptions = shrinktheweb_generateOptions($aOptions);
$sImageURL = shrinktheweb_getThumbnailAdvanced($sUrl, $aOptions);
return $sImageURL;
}
function shrinktheweb_deleteThumbnail($sUrl, $aOptions = array()) {
$aOptions = shrinktheweb_generateOptions($aOptions);
$aArgs = shrinktheweb_generateRequestArgs($aOptions);
$aArgs['stwurl'] = $sUrl;
$sFilename = shrinktheweb_generateHash($aArgs) . '.jpg';
$sFile = THUMBNAIL_DIR . $sFilename;
if (file_exists($sFile)) {
@unlink($sFile);
}
}
function shrinktheweb_deleteAllImages() {
$rDir = opendir(THUMBNAIL_DIR);
while (false !== ($sFile = readdir($rDir))) {
if ($sFile != "." && $sFile != "..") {
@unlink(THUMBNAIL_DIR . $sFile);
}
}
closedir($rDir);
}
function shrinktheweb_deleteErrorImages() {
$aFilenames = array(
'quota' => THUMBNAIL_DIR . QUOTA_IMAGE,
'bandwidth' => THUMBNAIL_DIR . BANDWIDTH_IMAGE,
'no_response' => THUMBNAIL_DIR . NO_RESPONSE_IMAGE,
);
foreach ($aFilenames as $sFile) {
if (file_exists($sFile)) {
@unlink($sFile);
}
}
}
function shrinktheweb_refreshThumbnail($sUrl, $aOptions = array()) {
$aOptions = shrinktheweb_generateOptions($aOptions);
$aOptions['RefreshOnDemand'] = true;
shrinktheweb_deleteThumbnail($sUrl, $aOptions);
$sImageURL = shrinktheweb_getThumbnailHTML($sUrl, $aOptions);
return $sImageURL;
}
function shrinktheweb_getThumbnailAdvanced($sUrl, $aOptions) {
$sImageUrl = shrinktheweb_getThumbnail($sUrl, $aOptions);
if ($sImageUrl == 'no_response') {
$sImageUrl = shrinktheweb_getNoResponseImage($sUrl, $aOptions);
}
return $sImageUrl;
}
function shrinktheweb_getThumbnail($sUrl, $aOptions) {
shrinktheweb_createCacheDirectory();
$aArgs = shrinktheweb_generateRequestArgs($aOptions);
$iCacheDays = CACHE_DAYS + 0;
if ($iCacheDays >= 0 && $aOptions['Embedded'] != 1) {
$aArgs['stwurl'] = $sUrl;
$sImageUrl = shrinktheweb_getCachedThumbnail($aArgs);
}
else {
unset($aArgs['stwu']);
$aArgs['stwembed'] = 1;
$aArgs['stwurl'] = $sUrl;
$sImageUrl = urldecode("http://images.shrinktheweb.com/xino.php?" . http_build_query($aArgs, '', '&'));
}
return $sImageUrl;
}
function shrinktheweb_generateOptions($aOptions) {
$aOptions['Size'] = isset($aOptions['Size']) ? $aOptions['Size'] : variable_get('shrinktheweb_thumb_size', '');
$aOptions['SizeCustom'] = isset($aOptions['SizeCustom']) ? $aOptions['SizeCustom'] : variable_get('shrinktheweb_thumb_size_custom', '');
$aOptions['FullSizeCapture'] = isset($aOptions['FullSizeCapture']) ? $aOptions['FullSizeCapture'] : variable_get('shrinktheweb_full_size', '');
$aOptions['MaxHeight'] = isset($aOptions['MaxHeight']) ? $aOptions['MaxHeight'] : variable_get('shrinktheweb_max_height', '');
$aOptions['NativeResolution'] = isset($aOptions['NativeResolution']) ? $aOptions['NativeResolution'] : variable_get('shrinktheweb_native_res', '');
$aOptions['WidescreenY'] = isset($aOptions['WidescreenY']) ? $aOptions['WidescreenY'] : variable_get('shrinktheweb_widescreen_y', '');
$aOptions['RefreshOnDemand'] = isset($aOptions['RefreshOnDemand']) ? $aOptions['RefreshOnDemand'] : false;
$aOptions['Delay'] = isset($aOptions['Delay']) ? $aOptions['Delay'] : variable_get('shrinktheweb_delay', '');
$aOptions['Quality'] = isset($aOptions['Quality']) ? $aOptions['Quality'] : variable_get('shrinktheweb_quality', '');
return $aOptions;
}
function shrinktheweb_generateRequestArgs($aOptions) {
$aArgs['stwaccesskeyid'] = ACCESS_KEY;
$aArgs['stwu'] = SECRET_KEY;
$aArgs['stwver'] = VER;
if (INSIDE_PAGES) {
$aArgs['stwinside'] = 1;
}
if (!$aOptions['FullSizeCapture'] && !$aOptions['WidescreenY']) {
if ($aOptions['SizeCustom']) {
$aArgs['stwxmax'] = $aOptions['SizeCustom'];
}
else {
$aArgs['stwsize'] = $aOptions['Size'];
}
}
if ($aOptions['FullSizeCapture']) {
$aArgs['stwfull'] = 1;
if ($aOptions['SizeCustom']) {
$aArgs['stwxmax'] = $aOptions['SizeCustom'];
}
else {
$aArgs['stwxmax'] = 120;
}
if ($aOptions['MaxHeight']) {
$aArgs['stwymax'] = $aOptions['MaxHeight'];
}
}
if ($aOptions['NativeResolution']) {
$aArgs['stwnrx'] = $aOptions['NativeResolution'];
if ($aOptions['WidescreenY']) {
$aArgs['stwnry'] = $aOptions['WidescreenY'];
if ($aOptions['SizeCustom']) {
$aArgs['stwxmax'] = $aOptions['SizeCustom'];
}
else {
$aArgs['stwxmax'] = 120;
}
}
}
if ($aOptions['Delay']) {
$aArgs['stwdelay'] = intval($aOptions['Delay']) <= 45 ? intval($aOptions['Delay']) : 45;
}
if ($aOptions['RefreshOnDemand']) {
$aArgs['stwredo'] = 1;
}
if ($aOptions['Quality']) {
$aArgs['stwq'] = intval($aOptions['Quality']);
}
if (CUSTOM_MSG_URL) {
$aArgs['stwrpath'] = CUSTOM_MSG_URL;
}
return $aArgs;
}
function shrinktheweb_getCachedThumbnail($aArgs = null) {
$aArgs = is_array($aArgs) ? $aArgs : array();
$sFilename = shrinktheweb_generateHash($aArgs) . '.jpg';
$sFile = THUMBNAIL_DIR . $sFilename;
$sReturnName = false;
$iForceUpdate = $aArgs['stwredo'] ? true : false;
if ($iForceUpdate || shrinktheweb_cacheFileExpired($sFile)) {
if (shrinktheweb_checkLimitReached(THUMBNAIL_DIR . BANDWIDTH_IMAGE)) {
$sFilename = BANDWIDTH_IMAGE;
}
else {
if (shrinktheweb_checkLimitReached(THUMBNAIL_DIR . QUOTA_IMAGE)) {
$sFilename = QUOTA_IMAGE;
}
else {
if (shrinktheweb_checkLimitReached(THUMBNAIL_DIR . NO_RESPONSE_IMAGE)) {
$sFilename = NO_RESPONSE_IMAGE;
}
else {
$aImage = shrinktheweb_checkWebsiteThumbnailCaptured($aArgs);
switch ($aImage['status']) {
case 'save':
shrinktheweb_downloadRemoteImageToLocalPath($aImage['url'], $sFile);
break;
case 'nosave':
return $aImage['url'];
break;
case 'quota_exceed':
$sFilename = QUOTA_IMAGE;
$sFile = THUMBNAIL_DIR . $sFilename;
shrinktheweb_downloadRemoteImageToLocalPath($aImage['url'], $sFile);
break;
case 'bandwidth_exceed':
$sFilename = BANDWIDTH_IMAGE;
$sFile = THUMBNAIL_DIR . $sFilename;
shrinktheweb_downloadRemoteImageToLocalPath($aImage['url'], $sFile);
break;
default:
return $aImage['status'];
}
}
}
}
}
$sFile = THUMBNAIL_DIR . $sFilename;
if (file_exists($sFile)) {
$sReturnName = THUMBNAIL_URI . $sFilename;
}
return $sReturnName;
}
function shrinktheweb_addRequestToDB($aSTWArgs, $aResponse, $sHash) {
$iTimestamp = time();
$sQueryUpdate = "UPDATE `shrinktheweb_log` SET `timestamp` = '" . $iTimestamp . "', `capturedon` = '" . $aResponse['stw_last_captured'] . "', `invalid` = '" . $aResponse['invalid'] . "',\n \t\t`stwerrcode` = '" . $aResponse['stw_response_code'] . "', `error` = '" . $aResponse['error'] . "', `errcode` = '" . $aResponse['stw_response_status'] . "' WHERE `hash` = '" . $sHash . "'";
$sQueryInsert = "INSERT INTO `shrinktheweb_log` SET `domain` = '" . $aSTWArgs['stwurl'] . "', `timestamp` = '" . $iTimestamp . "', `capturedon` = '" . $aResponse['stw_last_captured'] . "',\n \t\t`quality` = '" . $aSTWArgs['stwqual'] . "', `full` = '" . $aSTWArgs['stwfull'] . "', `xmax` = '" . $aSTWArgs['stwxmax'] . "', `ymax` = '" . $aSTWArgs['stwymax'] . "',\n \t\t`nrx` = '" . $aSTWArgs['stwnrx'] . "', `nry` = '" . $aSTWArgs['stwnry'] . "', `invalid` = '" . $aResponse['invalid'] . "', `stwerrcode` = '" . $aResponse['stw_response_code'] . "',\n \t\t`error` = '" . $aResponse['error'] . "', `errcode` = '" . $aResponse['stw_response_status'] . "', `hash` = '" . $sHash . "'";
db_query($sQueryUpdate);
if (db_affected_rows() == 0) {
db_query($sQueryInsert);
}
}
function shrinktheweb_checkWebsiteThumbnailCaptured($aArgs) {
$sRequestUrl = 'http://images.shrinktheweb.com/xino.php';
$sRemoteData = shrinktheweb_fileGetContent($sRequestUrl, $aArgs);
if ($sRemoteData == '' || $sRemoteData == 'offline') {
$aImage = array(
'status' => 'no_response',
);
if ($sRemoteData != '') {
$aImage['message'] = MAINTENANCE;
}
}
else {
$aResponse = shrinktheweb_getXMLResponse($sRemoteData);
if ($aResponse['exists'] && $aResponse['thumbnail'] != '') {
$aImage = array(
'status' => 'save',
'url' => $aResponse['thumbnail'],
);
}
else {
if ($aResponse['stw_bandwidth_remaining'] == 0 && !$aResponse['locked'] && !$aResponse['invalid'] && !$aResponse['exists'] && !$aResponse['problem']) {
$aImage = array(
'status' => 'bandwidth_exceed',
'url' => $aResponse['thumbnail'],
);
}
else {
if ($aResponse['stw_quota_remaining'] == 0 && !$aResponse['locked'] && !$aResponse['invalid'] && !$aResponse['exists'] && !$aResponse['problem']) {
$aImage = array(
'status' => 'quota_exceed',
'url' => $aResponse['thumbnail'],
);
}
else {
if (!$aResponse['exists'] && $aResponse['thumbnail'] != '') {
$aImage = array(
'status' => 'nosave',
'url' => $aResponse['thumbnail'],
);
}
else {
$aImage = array(
'status' => 'error',
);
}
}
}
}
if (DEBUG) {
shrinktheweb_addRequestToDB($aArgs, $aResponse, shrinktheweb_generateHash($aArgs));
}
}
return $aImage;
}
function shrinktheweb_downloadRemoteImageToLocalPath($sRemoteUrl, $sFile) {
$sRemoteData = shrinktheweb_fileGetContent($sRemoteUrl, array());
if ($sRemoteData) {
$rFile = fopen($sFile, "w+");
fputs($rFile, $sRemoteData);
fclose($rFile);
}
else {
if (file_exists($sFile)) {
@unlink($sFile);
}
return false;
}
return true;
}
function shrinktheweb_getNoResponseImage($sUrl, $aOptions) {
shrinktheweb_createCacheDirectory();
$aOptions = shrinktheweb_generateOptions($aOptions);
$aArgs['stwaccesskeyid'] = 'accountproblem';
if ($aOptions['SizeCustom']) {
$aArgs['stwxmax'] = $aOptions['SizeCustom'];
}
else {
$aArgs['stwsize'] = $aOptions['Size'];
}
$sRequestUrl = 'http://images.shrinktheweb.com/xino.php';
$sRemoteData = shrinktheweb_fileGetContent($sRequestUrl, $aArgs);
if ($sRemoteData != '') {
$aResponse = shrinktheweb_getXMLResponse($sRemoteData);
if (!$aResponse['exists'] && $aResponse['thumbnail'] != '') {
$sImageUrl = $aResponse['thumbnail'];
$sFilename = NO_RESPONSE_IMAGE;
$sFile = THUMBNAIL_DIR . $sFilename;
$isDownloaded = shrinktheweb_downloadRemoteImageToLocalPath($sImageUrl, $sFile);
if ($isDownloaded == true) {
return THUMBNAIL_URI . $sFilename;
}
}
}
return false;
}
function shrinktheweb_checkLimitReached($sFile) {
if (!file_exists($sFile)) {
return false;
}
$iCutoff = time() - 3600 * 6;
if (filemtime($sFile) <= $iCutoff) {
@unlink($sFile);
return false;
}
return true;
}
function shrinktheweb_createCacheDirectory() {
if (!file_exists(THUMBNAIL_DIR)) {
@mkdir(THUMBNAIL_DIR, 0777, true);
}
else {
@chmod(THUMBNAIL_DIR, 0777);
}
}
function shrinktheweb_generateHash($aArgs) {
$sPrehash = $aArgs['stwfull'] ? 'a' : 'c';
$sPrehash .= $aArgs['stwxmax'] . 'x' . $aArgs['stwymax'];
if ($aArgs['stwnrx']) {
$sPrehash .= 'b' . $aArgs['stwnrx'] . 'x' . $aArgs['stwnry'];
}
$sPrehash .= $aArgs['stwinside'];
$aReplace = array(
'http',
'https',
'ftp',
'://',
);
$sUrl = str_replace($aReplace, '', $aArgs['stwurl']);
return md5($sPrehash . $aArgs['stwsize'] . $aArgs['stwq'] . $sUrl);
}
function shrinktheweb_getXMLResponse($sResponse) {
if (extension_loaded('simplexml')) {
$oDOM = new DOMDocument();
$oDOM
->loadXML($sResponse);
$sXML = simplexml_import_dom($oDOM);
$sXMLLayout = 'http://www.shrinktheweb.com/doc/stwresponse.xsd';
$aResponse['thumbnail'] = $sXML
->children($sXMLLayout)->Response->ThumbnailResult->Thumbnail[0];
$aResponse['stw_action'] = $sXML
->children($sXMLLayout)->Response->ThumbnailResult->Thumbnail[1];
$aResponse['stw_response_status'] = $sXML
->children($sXMLLayout)->Response->ResponseStatus->StatusCode;
$aResponse['stw_response_code'] = $sXML
->children($sXMLLayout)->Response->ResponseCode->StatusCode;
$aResponse['stw_last_captured'] = $sXML
->children($sXMLLayout)->Response->ResponseTimestamp->StatusCode;
$aResponse['stw_quota_remaining'] = $sXML
->children($sXMLLayout)->Response->Quota_Remaining->StatusCode;
$aResponse['stw_bandwidth_remaining'] = $sXML
->children($sXMLLayout)->Response->Bandwidth_Remaining->StatusCode;
$aResponse['stw_category_code'] = $sXML
->children($sXMLLayout)->Response->CategoryCode->StatusCode;
}
else {
$aResponse['stw_response_status'] = shrinktheweb_getLegacyResponse('ResponseStatus', $sRemoteData);
$aResponse['stw_response_code'] = shrinktheweb_getLegacyResponse('ResponseCode', $sRemoteData);
$aResponse['stw_quota_remaining'] = shrinktheweb_getLegacyResponse('Quota_Remaining', $sRemoteData);
$aResponse['stw_bandwidth_remaining'] = shrinktheweb_getLegacyResponse('Bandwidth_Remaining', $sRemoteData);
$aThumbnail = shrinktheweb_getThumbnailStatus($sRemoteData);
$aResponse = array_merge($aResponse, $aThumbnail);
}
if ($aResponse['stw_action'] == 'delivered') {
$aResponse['exists'] = true;
}
else {
$aResponse['exists'] = false;
}
if ($aResponse['stw_action'] == 'fix_and_retry') {
$aResponse['problem'] = true;
}
else {
$aResponse['problem'] = false;
}
if ($aResponse['stw_action'] == 'noretry') {
$aResponse['error'] = true;
}
else {
$aResponse['error'] = false;
}
if ($aResponse['stw_response_code'] == 'INVALID_REQUEST') {
$aResponse['invalid'] = true;
}
else {
$aResponse['invalid'] = false;
}
if ($aResponse['stw_response_code'] == 'LOCK_TO_ACCOUNT') {
$aResponse['locked'] = true;
}
else {
$aResponse['locked'] = false;
}
return $aResponse;
}
function shrinktheweb_getLegacyResponse($sSearch, $s) {
$sRegex = '/<[^:]*:' . $sSearch . '[^>]*>[^<]*<[^:]*:StatusCode[^>]*>([^<]*)<\\//';
if (preg_match($sRegex, $s, $sMatches)) {
return $sMatches[1];
}
return false;
}
function shrinktheweb_getThumbnailStatus($s) {
$sRegex = '/<[^:]*:ThumbnailResult?[^>]*>[^<]*<[^:]*:Thumbnail\\s*(?:Exists=\\"((?:true)|(?:false))\\")+[^>]*>([^<]*)<\\//';
if (preg_match($sRegex, $s, $sMatches)) {
return array(
'stw_action' => $sMatches[1],
'thumbnail' => $sMatches[2],
);
}
return false;
}
function shrinktheweb_cacheFileExpired($sFile) {
$iCacheDays = CACHE_DAYS + 0;
if ($iCacheDays == 0 && file_exists($sFile)) {
return false;
}
else {
$iCutoff = time() - 3600 * 24 * $iCacheDays;
return !file_exists($sFile) || filemtime($sFile) <= $iCutoff;
}
}
function shrinktheweb_getArrayValue($aArray, $sKey, $isReturnSpace = false) {
if ($aArray && isset($aArray[$sKey])) {
return $aArray[$sKey];
}
if ($isReturnSpace) {
return ' ';
}
else {
return false;
}
}
function shrinktheweb_fileGetContent($sFileUrl, $aParams = array()) {
$sParams = '?';
foreach ($aParams as $sKey => $sValue) {
$sParams .= $sKey . '=' . $sValue . '&';
}
$sParams = substr($sParams, 0, -1);
$sResult = '';
if (function_exists('curl_init')) {
$rConnect = curl_init();
curl_setopt($rConnect, CURLOPT_URL, $sFileUrl . $sParams);
curl_setopt($rConnect, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($rConnect, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($rConnect, CURLOPT_HEADER, 0);
$sResult = curl_exec($rConnect);
curl_close($rConnect);
}
else {
$sResult = @file_get_contents($sFileUrl . $sParams);
}
return $sResult;
}
function shrinktheweb_getAccountInfo() {
$aArgs['stwaccesskeyid'] = ACCESS_KEY;
$aArgs['stwu'] = SECRET_KEY;
$sRequestUrl = 'http://images.shrinktheweb.com/account.php';
$sRemoteData = shrinktheweb_fileGetContent($sRequestUrl, $aArgs);
if ($sRemoteData == '' || $sRemoteData == 'offline') {
$aImage = array(
'stw_status' => 'no_response',
);
if ($sRemoteData != '') {
$aImage['message'] = MAINTENANCE;
}
}
else {
$aResponse = shrinktheweb_getAccXMLResponse($sRemoteData);
}
return $aResponse;
}
function shrinktheweb_getAccXMLResponse($sResponse) {
if (extension_loaded('simplexml')) {
$oDOM = new DOMDocument();
$oDOM
->loadXML($sResponse);
$sXML = simplexml_import_dom($oDOM);
$sXMLLayout = 'http://www.shrinktheweb.com/doc/stwacctresponse.xsd';
$aResponse['stw_response_status'] = $sXML
->children($sXMLLayout)->Response->Status->StatusCode;
$aResponse['stw_account_level'] = $sXML
->children($sXMLLayout)->Response->Account_Level->StatusCode;
$aResponse['stw_inside_pages'] = $sXML
->children($sXMLLayout)->Response->Inside_Pages->StatusCode;
$aResponse['stw_custom_size'] = $sXML
->children($sXMLLayout)->Response->Custom_Size->StatusCode;
$aResponse['stw_full_length'] = $sXML
->children($sXMLLayout)->Response->Full_Length->StatusCode;
$aResponse['stw_refresh_ondemand'] = $sXML
->children($sXMLLayout)->Response->Refresh_OnDemand->StatusCode;
$aResponse['stw_custom_delay'] = $sXML
->children($sXMLLayout)->Response->Custom_Delay->StatusCode;
$aResponse['stw_custom_quality'] = $sXML
->children($sXMLLayout)->Response->Custom_Quality->StatusCode;
$aResponse['stw_custom_resolution'] = $sXML
->children($sXMLLayout)->Response->Custom_Resolution->StatusCode;
$aResponse['stw_custom_messages'] = $sXML
->children($sXMLLayout)->Response->Custom_Messages->StatusCode;
}
else {
$aResponse['stw_response_status'] = shrinktheweb_getLegacyResponse('Status', $sRemoteData);
$aResponse['stw_account_level'] = shrinktheweb_getLegacyResponse('Account_Level', $sRemoteData);
$aResponse['stw_inside_pages'] = shrinktheweb_getLegacyResponse('Inside_Pages', $sRemoteData);
$aResponse['stw_custom_size'] = shrinktheweb_getLegacyResponse('Custom_Size', $sRemoteData);
$aResponse['stw_full_length'] = shrinktheweb_getLegacyResponse('Full_Length', $sRemoteData);
$aResponse['stw_refresh_ondemand'] = shrinktheweb_getLegacyResponse('Refresh_OnDemand', $sRemoteData);
$aResponse['stw_custom_delay'] = shrinktheweb_getLegacyResponse('Custom_Delay', $sRemoteData);
$aResponse['stw_custom_quality'] = shrinktheweb_getLegacyResponse('Custom_Quality', $sRemoteData);
$aResponse['stw_custom_resolution'] = shrinktheweb_getLegacyResponse('Custom_Resolution', $sRemoteData);
$aResponse['stw_custom_messages'] = shrinktheweb_getLegacyResponse('Custom_Messages', $sRemoteData);
}
return $aResponse;
}