public static function getPhotoUrl($id, $ts, $width = null, $height = null) { if ($width === 'original') { $size = 'original'; } else { if ($width && !$height) { $size = $width . 'x' . $width; } else { if (!$width) { $width = 96; $size = '96x96'; } else { $size = $width . 'x' . $height; } } } if ($ts) { if (waSystemConfig::systemOption('mod_rewrite')) { return wa()->getDataUrl('photo/' . $id . '/' . $ts . '.' . $size . '.jpg', true, 'contacts'); } else { if (file_exists(wa()->getDataPath('photo/' . $id . '/' . $ts . '.' . $size . '.jpg', true, 'contacts'))) { return wa()->getDataUrl('photo/' . $id . '/' . $ts . '.' . $size . '.jpg', true, 'contacts'); } else { return wa()->getDataUrl('photo/thumb.php/' . $id . '/' . $ts . '.' . $size . '.jpg', true, 'contacts'); } } } else { $size = (int) $width; if (!in_array($size, array(20, 32, 50, 96))) { $size = 96; } return wa()->getRootUrl() . 'wa-content/img/userpic' . $size . '.jpg'; } }
public static function getPhotoUrlTemplate($photo, $absolute = false) { $path = self::getPhotoFolder($photo['id']) . '/' . $photo['id']; if ($photo['status'] <= 0 && !empty($photo['hash'])) { $path .= '.' . $photo['hash']; } $path .= '/' . $photo['id'] . '.%size%.' . $photo['ext']; if (waSystemConfig::systemOption('mod_rewrite')) { return wa()->getDataUrl($path, true, 'photos', $absolute); } else { return wa()->getDataUrl('thumb.php/' . $path, true, 'photos', $absolute); } }
/** * Reads file contents and outputs it to browser with appropriate headers. * * @param string $file File to path * @param string|null $attach Name, which will be suggested to user when he requests to download the file. * If not specified, browser's auto-suggestion will be used. * @param bool $exit Flag requiring to send file transfer headers to user's browser. By default (true) headers are sent. * @param bool $md5 Flag requiring to send the Content-MD5 header. By default (false) this header is not sent. * @throws waException If file does not exist */ public static function readFile($file, $attach = null, $exit = true, $md5 = false) { if (file_exists($file)) { $response = wa()->getResponse(); $file_type = self::getMimeType($attach ? $attach : $file); if ($md5) { $md5 = base64_encode(pack('H*', md5_file($file))); } @ini_set('async_send', 1); wa()->getStorage()->close(); if ($attach !== null) { $send_as = str_replace('"', '\\"', is_string($attach) ? $attach : basename($file)); $send_as = preg_replace('~[\\n\\r]+~', ' ', $send_as); $x_accel_redirect = waSystemConfig::systemOption('x_accel_redirect'); $file_size = filesize($file); $response->setStatus(200); if (empty($x_accel_redirect)) { $from = $to = false; if ($http_range = waRequest::server('HTTP_RANGE')) { // multi range support incomplete list($dimension, $range) = explode("=", $http_range, 2); $ranges = explode(',', $range); $intervals = array(); foreach ($ranges as $range) { $range = trim($range); if (preg_match('/^(\\d+)-(\\d+)$/', $range, $matches)) { $intervals[] = array('from' => intval($matches[1]), 'to' => intval($matches[2])); } elseif (preg_match('/^(\\d+)-$/', $range, $matches)) { $intervals[] = array('from' => intval($matches[1]), 'to' => $file_size - 1); } elseif (preg_match('/^-(\\d+)$/', $range, $matches)) { $intervals[] = array('from' => $file_size - intval($matches[1]), 'to' => $file_size - 1); } else { throw new waException('Requested range not satisfiable', 416); } } foreach ($intervals as $interval) { if ($from === false) { $from = $interval['from']; } if ($to === false) { $to = $interval['to']; } else { if ($to + 1 == $interval['from']) { $to = $interval['to']; } else { //hole at interval throw new waException('Requested range not satisfiable', 416); } } } if ($from < 0 || $to + 1 > $file_size) { throw new waException('Requested range not satisfiable', 416); } $range_length = $to - $from + 1; $response->setStatus(206); $response->addHeader("Content-Length", $range_length); $response->addHeader("Content-Range", "bytes {$from}-{$to}/{$file_size}"); } else { $response->addHeader("Content-Length", $file_size); if ($md5) { $response->addHeader("Content-MD5", $md5); } } $response->addHeader("Cache-Control", "no-cache, must-revalidate"); $response->addHeader("Content-type", "{$file_type}"); $response->addHeader("Content-Disposition", "attachment; filename=\"{$send_as}\""); $response->addHeader("Last-Modified", filemtime($file)); $response->addHeader("Accept-Ranges", "bytes"); $response->addHeader("Connection", "close"); $fp = fopen($file, 'rb'); if ($from) { fseek($fp, $from); } $response->sendHeaders(); $response = null; //TODO: adjust chunk size $chunk = 1048576; //1M while (!feof($fp) && $chunk && connection_status() == 0) { if ($to) { $chunk = min(1 + $to - @ftell($fp), $chunk); } if ($chunk) { print @fread($fp, $chunk); @flush(); } } @fclose($fp); } else { $response->addHeader("Content-type", $file_type); //RFC 6266 $response->addHeader("Content-Disposition", "attachment; filename=\"{$send_as}\""); $response->addHeader("Accept-Ranges", "bytes"); $response->addHeader("Content-Length", $file_size); $response->addHeader("Expires", "0"); $response->addHeader("Cache-Control", "no-cache, must-revalidate", false); $response->addHeader("Pragma", "public"); $response->addHeader("Connection", "close"); if ($md5) { $response->addHeader("Content-MD5", $md5); } $response->addHeader("X-Accel-Redirect", $file); $response->sendHeaders(); $response = null; //@future //$response->addHeader("X-Accel-Limit-Rate", $rate_limit); } } else { $response->addHeader("Content-type", $file_type); $response->addHeader("Last-Modified", filemtime($file)); if ($md5) { $response->addHeader("Content-MD5", $md5); } $response->sendHeaders(); $response = null; @readfile($file); } if ($exit) { if (!empty($response)) { /** * @var waResponse $response */ $response->sendHeaders(); } exit; } } else { throw new waException("File not found", 404); } }
/** * Returns URL of a product image. * * @param array $image Key-value image data object * @param string $size Size value string (e.g., '200x0', '96x96', etc.) * @param bool $absolute Whether absolute URL must be returned * @return string */ public static function getUrl($image, $size = null, $absolute = false) { $path = shopProduct::getFolder($image['product_id']) . "/{$image['product_id']}/images/{$image['id']}/{$image['id']}.{$size}.{$image['ext']}"; if (waSystemConfig::systemOption('mod_rewrite')) { return wa()->getDataUrl($path, true, 'shop', $absolute); } else { if (file_exists(wa()->getDataPath($path, true, 'shop'))) { return wa()->getDataUrl($path, true, 'shop', $absolute); } else { $path = str_replace('products/', 'products/thumb.php/', $path); return wa()->getDataUrl($path, true, 'shop', $absolute); } } }
function smarty_block_wa_js($params, $content, &$smarty) { if (!$content) { return ''; } // jquery ui custom bundle $ui_custom = array('core' => 0, 'widget' => 0, 'mouse' => 0, 'draggable' => 0, 'droppable' => 0, 'sortable' => 0, 'datepicker' => 1); $files = explode("\n", $content); $wa = waSystem::getInstance(); $jquery_ui_path = "wa-content/js/jquery-ui/jquery.ui."; $jquery_ui_path_n = strlen($jquery_ui_path); $n = strlen($wa->getRootUrl()); $locale = $wa->getLocale(); // // Non-debug mode: merge all files into one cache // if (!SystemConfig::isDebug() && isset($params['file'])) { $root_path = $wa->getConfig()->getRootPath(); $app_path = $wa->getConfig()->getAppPath(); $result = ''; $files_combine = array(); $mtime = file_exists($app_path . '/' . $params['file']) ? filemtime($app_path . '/' . $params['file']) : 0; $r = true; foreach ($files as $f) { $f = trim($f); $f = substr($f, $n); if ($f) { if (substr($f, 0, $jquery_ui_path_n) == $jquery_ui_path) { $jquery_f = substr($f, $jquery_ui_path_n); if (substr($jquery_f, -7) == '.min.js') { $jquery_f = substr($jquery_f, 0, -7); } if (isset($ui_custom[$jquery_f])) { if (!$result) { $result = '<script type="text/javascript" src="' . $wa->getRootUrl() . 'wa-content/js/jquery-ui/jquery-ui.custom.min.js"></script>' . "\n"; } // include locale if ($ui_custom[$jquery_f] && $locale != 'en_US') { $result .= '<script type="text/javascript" src="' . $wa->getRootUrl() . 'wa-content/js/jquery-ui/i18n/jquery.ui.' . $jquery_f . '-' . $locale . '.js"></script>' . "\n"; } continue; } } if (!file_exists($f)) { $r = false; break; } $files_combine[] = $f; if ($mtime && filemtime($root_path . '/' . $f) > $mtime) { $mtime = 0; } } } if ($files_combine) { if ($r && !$mtime && waFiles::create($app_path . '/' . $params['file'])) { // check Google Closure Compiler // https://developers.google.com/closure/compiler/docs/gettingstarted_app if ($compiler = waSystemConfig::systemOption('js_compiler')) { $cmd = 'java -jar "' . $compiler . '"'; foreach ($files_combine as $file) { $cmd .= ' --js "' . $root_path . '/' . $file . '"'; } $cmd .= ' --js_output_file "' . $app_path . '/' . $params['file'] . '"'; system($cmd, $res); $r = !$res; if (!$r) { waLog::log("Error occured while compress files:\n\t" . implode("\n\t", $files_combine) . "\n\t{$params['file']}\n\ncommand:\n{$cmd}", __FUNCTION__ . '.log'); } } else { $r = false; } if (!$r) { $data = ""; foreach ($files_combine as $file) { $data .= file_get_contents($root_path . '/' . $file) . ";\n"; } $r = @file_put_contents($app_path . '/' . $params['file'], $data); if (!$r) { waLog::log("Error occured while compress files:\n\t" . implode("\n\t", $files_combine) . "\n\t{$params['file']}", __FUNCTION__ . '.log'); } } } } if ($r) { if ($files_combine) { $result .= '<script type="text/javascript" src="' . $wa->getAppStaticUrl() . $params['file'] . '?v' . $wa->getVersion() . '"></script>' . "\n"; } return $result; } } // // Debug mode (or no file specified): include all files separately // $result = ""; foreach ($files as $f) { $f = trim($f); if ($f) { // Add ?version to circumvent browser caching if (substr($f, $n, 10) !== 'wa-content') { $f .= '?v' . $wa->getVersion(); } $result .= '<script type="text/javascript" src="' . $f . '"></script>' . "\n"; // Add datepicker localization automatically // !!! This is not really a good idea since it will break in non-debug mode anyways if (substr($f, $n) == $jquery_ui_path . 'datepicker.min.js' && $locale != 'en_US') { $result .= '<script type="text/javascript" src="' . $wa->getRootUrl() . 'wa-content/js/jquery-ui/i18n/jquery.ui.datepicker-' . $locale . '.js"></script>' . "\n"; } } } return $result; }
/** * Returns the photo URL of the specified contact. * * @param int $id Contact id * @param int $ts Contact photo id stored in contact's 'photo' property. If not specified, the URL of the default * userpic is returned. * @param int|string|null $width Image width. Arbitrary integer value, or string value 'original', which requires * that method must return the URL of the original image originally uploaded from a user's computer. Defaults to 96. * @param int|string|null $height Image height (integer). If not specified, the integer value specified for the * $width parameter is used. * @param string $type * @param bool $retina * @return string */ public static function getPhotoUrl($id, $ts, $width = null, $height = null, $type = 'person', $retina = null) { if ($width === 'original') { $size = 'original'; } else { if ($width && !$height) { $size = $width . 'x' . $width; } else { if (!$width) { $width = 96; $size = '96x96'; } else { $size = $width . 'x' . $height; } } } if ($retina === null) { $retina = wa()->getEnv() == 'backend'; } $dir = self::getPhotoDir($id, false); if ($ts) { if ($size != 'original' && $retina) { $size .= '@2x'; } if (waSystemConfig::systemOption('mod_rewrite')) { return wa()->getDataUrl("photos/{$dir}{$ts}.{$size}.jpg", true, 'contacts'); } else { if (file_exists(wa()->getDataPath("photos/{$dir}{$ts}.{$size}.jpg", true, 'contacts'))) { return wa()->getDataUrl("photos/{$dir}{$ts}.{$size}.jpg", true, 'contacts'); } else { return wa()->getDataUrl("photos/thumb.php/{$dir}{$ts}.{$size}.jpg", true, 'contacts'); } } } else { $size = (int) $width; if (!in_array($size, array(20, 32, 50, 96))) { $size = 96; } if ($retina) { $size .= '@2x'; } if ($type == 'company') { return wa()->getRootUrl() . 'wa-content/img/company' . $size . '.jpg'; } else { return wa()->getRootUrl() . 'wa-content/img/userpic' . $size . '.jpg'; } } }