PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/WeFlex/Util.php

https://github.com/luoqi1987425/weflex
PHP | 221 lines | 142 code | 58 blank | 21 comment | 9 complexity | 68e1218a48f6d0ef202788d084a49978 MD5 | raw file
  1. <?php
  2. class WeFlex_Util
  3. {
  4. /**
  5. * get a image by it's size
  6. * if url is entire url we will use it. if not we will use view baseUrl
  7. *
  8. * @return String
  9. */
  10. public static function GetImageUrlBySize($surl,$width = null ,$height = null) {
  11. $isExternal = self::_IsExternalUrl( $surl );
  12. if( $isExternal ){
  13. $s3 = Zend_Registry::get( 'magzine-s3' );
  14. $bucket = WeFlex_Application::GetInstance()->config->api->amazon->s3->bucket->issue;
  15. $lifetime = WeFlex_Application::GetInstance()->config->api->amazon->s3->lifetime;
  16. if( $width ){
  17. $durl = substr($surl,0,strrpos($surl,'.')).'_'.$width . 'x' . $height . substr($surl,strrpos($surl,'.'));
  18. }else{
  19. $durl = $surl;
  20. }
  21. //$durl = $s3->getAuthenticatedURL( $bucket, $durl, $lifetime , false , true );
  22. $durl = "http://" . $bucket . ".s3.amazonaws.com/" . $durl ;
  23. }else{
  24. $view = Zend_Registry::get( 'view' );
  25. if( $width ){
  26. $durl = substr($surl,0,strrpos($surl,'.')).'_'.$width . 'x' . $height . substr($surl,strrpos($surl,'.'));
  27. }else{
  28. $durl = $surl;
  29. }
  30. $durl = $view->baseUrl() . $durl;
  31. }
  32. return $durl;
  33. }
  34. /**
  35. * only gener the size picture no intellgcy detect
  36. *
  37. * @param unknown_type $surl
  38. * @param unknown_type $width
  39. * @param unknown_type $height
  40. */
  41. public static function GenerImageUrlBySize($surl,$width ,$height){
  42. $durl = substr($surl,0,strrpos($surl,'.')).'_'.$width . 'x' . $height . substr($surl,strrpos($surl,'.'));
  43. return $durl;
  44. }
  45. public static function GetFileName( $url ){
  46. $fileName = substr($url,strrpos($url,'/'));
  47. $fileName = substr($fileName,1);
  48. return $fileName;
  49. }
  50. public static function GetFileFormat( $url ){
  51. $fileName = substr($url,strrpos($url,'.'));
  52. $fileName = substr($fileName,1);
  53. return $fileName;
  54. }
  55. public static function GetIp(){
  56. if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
  57. {
  58. $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
  59. }
  60. elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
  61. {
  62. $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
  63. }
  64. elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
  65. {
  66. $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
  67. }
  68. elseif (getenv("HTTP_X_FORWARDED_FOR"))
  69. {
  70. $ip = getenv("HTTP_X_FORWARDED_FOR");
  71. }
  72. elseif (getenv("HTTP_CLIENT_IP"))
  73. {
  74. $ip = getenv("HTTP_CLIENT_IP");
  75. }
  76. elseif (getenv("REMOTE_ADDR"))
  77. {
  78. $ip = getenv("REMOTE_ADDR");
  79. }
  80. else
  81. {
  82. $ip = "Unknown";
  83. }
  84. return $ip;
  85. }
  86. public static function GetEurPriceFormat( $price ){
  87. if(strstr($price , '.' )){
  88. $rtn = str_replace(".", ",", $price);
  89. $length = strlen(substr(strrchr( $rtn, "," ) , 1));
  90. if( $length < 2 ){
  91. $rtn .= '0';
  92. }elseif( $length > 2 ){
  93. $rtn = substr($rtn, 0, -( $length -2 ));
  94. }
  95. }
  96. else{
  97. $rtn = $price . ',00';
  98. }
  99. return $rtn;
  100. }
  101. public static function ConvertPrice( $price ){
  102. $rtn = str_replace(",", ".", $price);
  103. return $rtn;
  104. }
  105. public static function GetDomain(){
  106. return 'http://' . $_SERVER['SERVER_NAME'];
  107. }
  108. public static function GetFullUrl( $param , $router = 'default' ){
  109. $view = Zend_Registry::get( "view" );
  110. $domain = self::GetDomain();
  111. $url = $view->url( $param , $router , true);
  112. return $domain . $url;
  113. }
  114. public static function GetFullBaseUrl(){
  115. $view = Zend_Registry::get( "view" );
  116. return 'http://' . $_SERVER['SERVER_NAME'] . $view->baseUrl();
  117. }
  118. public static function GenerNameForSEO( $name ){
  119. $output = preg_replace('/[^A-Za-z0-9\s]/','', $name);
  120. $output = trim( $output );
  121. $output = preg_replace('/[\s]+/','-', $output);
  122. return $output;
  123. }
  124. public static function GenerNameForCacheKey( $name ){
  125. $output = preg_replace('/[^A-Za-z0-9\s_]/','', $name);
  126. $output = trim( $output );
  127. $output = preg_replace('/[\s]+/','_', $output);
  128. return $output;
  129. }
  130. public static function MkDir( $dirPath ){
  131. if( !is_dir( $dirPath ) ){
  132. mkdir($dirPath,0777,true);
  133. @chmod($dirPath,0777);
  134. }
  135. }
  136. public static function Copy( $orginalFile , $toFile ){
  137. copy($orginalFile,$toFile);
  138. @chmod($toFile,0777);
  139. }
  140. /**
  141. * 将字符串包含 地址信息转换成 A
  142. *
  143. * @param unknown_type $str
  144. * @return unknown
  145. */
  146. public static function ConvertStrUrlLink( $str ){
  147. $str = preg_replace_callback("|http://[^\s]+|" , "url_preg_replace_callback", $str);
  148. $str = preg_replace_callback("|https://[^\s]+|", "url_preg_replace_callback", $str);
  149. return $str;
  150. }
  151. private static function _IsExternalUrl( $url ){
  152. //hack
  153. if(preg_match("/^\/upload\//" ,$url)){
  154. return false;
  155. }else{
  156. return true;
  157. }
  158. }
  159. }
  160. function url_preg_replace_callback($matches){
  161. return '<a target="_blank" href="'.$matches[0].'">'.$matches[0].'</a>';
  162. }
  163. ?>