PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/common/components/CUtils.php

https://bitbucket.org/haichau59/manga
PHP | 193 lines | 147 code | 20 blank | 26 comment | 33 complexity | 6f92aab09bf09b2362d8c583be54921c MD5 | raw file
  1. <?php
  2. /**
  3. * Description of CUtils
  4. *
  5. * @author Nguyen Chi Thuc
  6. */
  7. class CUtils {
  8. //put your code here
  9. public static $STREAMING_HTTP = 0;
  10. public static $STREAMING_RTSP = 1;
  11. public static $STREAMING_HLS = 2;
  12. public static $STREAMING_RTMP = 3;
  13. /**
  14. * Log a msg as custom level "CUtils::Debug"
  15. * You need to add this level ("CUtils::Debug") to log component in config/main.php :
  16. * <code>
  17. * 'log'=>array(
  18. * 'class'=>'CLogRouter',
  19. * 'routes'=>array(
  20. * array(
  21. * 'class'=>'CFileLogRoute',
  22. * 'levels'=>'error, warning, <b>CUtils::Debug</b>',
  23. * ),
  24. * array('class'=>'CWebLogRoute',),
  25. * ),
  26. * </code>
  27. * @param string $msg
  28. */
  29. public static function Debug($msg, $category="-=Thuc=-") {
  30. Yii::log($msg, 'CUtils::Debug', $category);
  31. }
  32. public static function randomString($length=32, $chars="abcdefghijklmnopqrstuvwxyz0123456789") {
  33. $max_ind = strlen($chars)-1;
  34. $res = "";
  35. for ($i =0; $i < $length; $i++) {
  36. $res .= $chars{rand(0, $max_ind)};
  37. }
  38. return $res;
  39. }
  40. public static function encrypt($str) {
  41. return md5($str);
  42. }
  43. public static function loadVodPoster($asset) {
  44. $data = VodAsset::getVODImages($asset['id']);
  45. $posters = array();
  46. foreach ($data as $poster) {
  47. $orientation = $poster->orientation == 1 ? "poster_port" : "poster_land";
  48. $posters[$orientation] = $poster->url;
  49. }
  50. return array_merge($asset, $posters);
  51. }
  52. public static function timeElapsedString($ptime) {
  53. $etime = time() - $ptime;
  54. if ($etime < 1) {
  55. return '0 giây';
  56. }
  57. $a = array( 12 * 30 * 24 * 60 * 60 => 'năm',
  58. 30 * 24 * 60 * 60 => 'tháng',
  59. 24 * 60 * 60 => 'ngày',
  60. 60 * 60 => 'giờ',
  61. 60 => 'phút',
  62. 1 => 'giây'
  63. );
  64. foreach ($a as $secs => $str) {
  65. $d = $etime / $secs;
  66. if ($d >= 1) {
  67. $r = round($d);
  68. return $r . ' ' . $str . ' trước';
  69. }
  70. }
  71. }
  72. public static function convertMysqlToTimestamp($dateString) {
  73. $format = '@^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}) (?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})$@';
  74. preg_match($format, $dateString, $dateInfo);
  75. $unixTimestamp = mktime(
  76. $dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
  77. $dateInfo['month'], $dateInfo['day'], $dateInfo['year']
  78. );
  79. return $unixTimestamp;
  80. }
  81. public static function timeElapsedStringFromMysql($dateString) {
  82. $ptime = CUtils::convertMysqlToTimestamp($dateString);
  83. return CUtils::timeElapsedString($ptime);
  84. }
  85. public static function getDeviceInfo() {
  86. $uaString = $_SERVER['HTTP_USER_AGENT'];
  87. //echo $uaString;
  88. $info = array();
  89. if (preg_match("/android/i", $uaString)) {
  90. if (preg_match("/android (\d+)\.(\d+)/i", $uaString, $matches)) {
  91. $info = array('os' => 'android', 'major' => $matches[1], 'minor' => $matches[2]);
  92. } else {
  93. $info = array('os' => 'android', 'major' => 1, 'minor' => 0);
  94. }
  95. } else if (preg_match("/iPhone|iPod|iPad/i", $uaString)) {
  96. $info = array('os' => 'ios', 'major' => 1, 'minor' => 0);
  97. if (preg_match("/iOS (\d+)\.(\d+)/", $uaString, $matches)) {
  98. $info['major'] = $matches[1];
  99. $info['minor'] = $matches[2];
  100. } else if (preg_match("/iPhone OS (\d+)_(\d+)/", $uaString, $matches)) {
  101. $info['major'] = $matches[1];
  102. $info['minor'] = $matches[2];
  103. }
  104. } else if (preg_match("/Windows Phone (\d+)\.(\d+)/", $uaString, $matches)) {
  105. $info = array('os' => 'wp', 'major' => $matches[1], 'minor' => $matches[0]);
  106. } else if (preg_match("/symbian/i", $uaString)) {
  107. $info = array('os' => 'symbian', 'major' => 1, 'minor' => 0);
  108. } else {
  109. $info = array('os' => 'unknown', 'major' => 1, 'minor' => 0);
  110. }
  111. //var_dump($info);
  112. return $info;
  113. }
  114. public static function getSupportedStreamingProtocol() {
  115. // $devInfo = CUtils::getDeviceInfo();
  116. $detect = Yii::app()->mobileDetect;
  117. if ($detect->is('AndroidOS')) {
  118. if ($detect->version('Android') < 3.0) {
  119. return CUtils::$STREAMING_RTSP;
  120. } else {
  121. return CUtils::$STREAMING_HLS;
  122. }
  123. } else if ($detect->is('SymbianOS')) {
  124. return CUtils::$STREAMING_RTSP; // rtsp
  125. } else if ($detect->is('iOS')) {
  126. return CUtils::$STREAMING_HLS; // hls
  127. } else if ($detect->is('WindowsMobileOS')) {
  128. return CUtils::$STREAMING_HTTP;
  129. } else {
  130. return CUtils::$STREAMING_HTTP; // http progressive download
  131. }
  132. }
  133. public static function cidrMatch($ip, $range) {
  134. list ($subnet, $bits) = explode('/', $range);
  135. $ip = ip2long($ip);
  136. $subnet = ip2long($subnet);
  137. $mask = -1 << (32 - $bits);
  138. $subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned
  139. return ($ip & $mask) == $subnet;
  140. }
  141. public static function truncateWords($text, $length = 10){
  142. if(strlen($text) > $length){
  143. $text_temp = substr($text, 0, $length);
  144. $end_point = strrpos($text_temp, ' ');
  145. $text_fi = substr($text_temp, 0, $end_point).'...';
  146. return $text_fi;
  147. }else{
  148. return $text;
  149. }
  150. }
  151. public static function validatorMobile($mobileNumber){
  152. $valid_number = '';
  153. if(preg_match('/^(84|0)(91|94|123|124|125|127|129)\d{7}$/', $mobileNumber, $matches)){
  154. if ($matches[1] == 0){
  155. $valid_number = preg_replace('/^0/', '84', $mobileNumber);
  156. }else{
  157. $valid_number = $mobileNumber;
  158. }
  159. }
  160. return $valid_number;
  161. }
  162. public static function getVirtualView($fromDate){
  163. //Cheat so luot xem theo ngay up len
  164. $createDate = new DateTime($fromDate);
  165. $currentDate = new DateTime('now');
  166. $interval = $createDate->diff($currentDate);
  167. $extraVirtualView = 50*($interval->format('%a') + 1);
  168. if($extraVirtualView > 500)
  169. $extraVirtualView = 500;
  170. return $extraVirtualView;
  171. }
  172. }
  173. ?>