PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/common/module/media.php

http://lazycms.googlecode.com/
PHP | 231 lines | 193 code | 1 blank | 37 comment | 4 complexity | 67ad454e458fa1cb3ce362338c126d2a MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * +---------------------------------------------------------------------------+
  4. * | LL LLLL LL L LLLL LLLL |
  5. * | LL LL L LLL LL LL L LL LL |
  6. * | LL LLLL LLLLL LL LL LL LLLL LLL LL LL LL LL |
  7. * | LL LL LL LL LL LL L LLL LL LLLLL LL LL LL |
  8. * | LL LLLLL LL LLLL LL L L LL LLLLL LL LL LL |
  9. * | LL LL LL LL LLLL LL L LL LL LLLL LL |
  10. * | LL LL LL LL LL LL L L LL L LL LLLL LL |
  11. * | LLLLLL LLLLL LLLLL LL LLLL L LL LLLL LL LLLLLL |
  12. * | LL |
  13. * | LL |
  14. * +---------------------------------------------------------------------------+
  15. * | Copyright (C) 2007-2010 LazyCMS.com All rights reserved. |
  16. * +---------------------------------------------------------------------------+
  17. * | LazyCMS is free software. See LICENSE for copyright notices and details. |
  18. * +---------------------------------------------------------------------------+
  19. */
  20. defined('COM_PATH') or die('Restricted access!');
  21. /**
  22. * ?????
  23. *
  24. * @param string $content
  25. * @return string
  26. */
  27. function media_localized_images($content) {
  28. include_file(COM_PATH.'/system/httplib.php');
  29. if (preg_match_all('/<img[^>]+src\s*=([^\s\>]+)[^>]*>/i', $content, $matchs)) {
  30. $suffixs = C('UPIMG-Exts');
  31. $matchs[1] = array_unique($matchs[1]);
  32. foreach ($matchs[1] as $url) {
  33. $str = $url;
  34. $url = trim(trim(trim($url),'"'), "'");
  35. // ???????
  36. if (validate_is($url, VALIDATE_IS_URL)) {
  37. if (strpos($url, '&amp;') !== false) $url = xmldecode($url);
  38. $aurl = httplib_parse_url($url);
  39. $resp = httplib_get($url, array(
  40. 'timeout' => 60,
  41. 'headers' => array(
  42. 'referer' => $aurl['referer'],
  43. ),
  44. ));
  45. if (httplib_retrieve_response_code($resp) == 200) {
  46. // ??????
  47. $suffix = pathinfo($aurl['path'], PATHINFO_EXTENSION);
  48. if (!instr($suffix, $suffixs)) {
  49. $ctype = httplib_retrieve_header($resp, 'content-type');
  50. if (($pos=strrpos($ctype, '/')) !== false) {
  51. $suffix = substr($ctype, $pos + 1);
  52. }
  53. // ????????,????jpg
  54. if (!instr($suffix, $suffixs)) {
  55. $suffix = 'jpg';
  56. }
  57. }
  58. $body = httplib_retrieve_body($resp);
  59. // sha1sum
  60. $sha1sum = sha1($body);
  61. // ????????
  62. if ($file = media_no_add($sha1sum)) {
  63. if ($str != '"'.ROOT.$file.'"') {
  64. $content = str_replace($str, '"'.ROOT.$file.'"', $content);
  65. }
  66. continue;
  67. }
  68. // ????
  69. if (!($media = media_get($sha1sum))) {
  70. $time = time();
  71. $path = ABS_PATH . '/' . MEDIA_PATH . '/images/' . date('Y-m-d', $time) . '/' . $sha1sum . '.' . $suffix;
  72. mkdirs(dirname($path)); file_put_contents($path, $body);
  73. // ??
  74. $media = media_get(media_add('images', $sha1sum, pathinfo($aurl['path'], PATHINFO_BASENAME), strlen($body), $suffix, $time));
  75. }
  76. // ??????
  77. $content = str_replace($str, '"'.$media['url'].'"', $content);
  78. }
  79. }
  80. }
  81. }
  82. return $content;
  83. }
  84. /**
  85. * ????
  86. *
  87. * @param string $sha1sum
  88. * @return bool|null
  89. */
  90. function media_no_add($sha1sum) {
  91. $file_sum = array(
  92. '05556151b4c76b812e0f8f5d4619e39b' => 'common/images/emots/default/despise.gif',
  93. '344433f16e4c7458abbefe625ea5cf79' => 'common/images/emots/default/crazy.gif',
  94. '68e089023da6a669bda0019487c9261a' => 'common/images/emots/default/laugh.gif',
  95. 'ae2bb1ad58cd2a251279992be3129798' => 'common/images/emots/default/angry.gif',
  96. 'd5dff372bc0fb83ca5a370d930620394' => 'common/images/emots/default/fastcry.gif',
  97. '0bd0834f7ea3cc376703677b3759e79b' => 'common/images/emots/default/bye.gif',
  98. '36e7068fd4b6050a9ca5e97b6c5ad105' => 'common/images/emots/default/cute.gif',
  99. '7926792eaf17416c6e59e2ae43c77411' => 'common/images/emots/default/quiet.gif',
  100. 'ae551c1340358d0d2e708cb9cb98a378' => 'common/images/emots/default/cry.gif',
  101. 'db43f69d2445682946de87faeca3f320' => 'common/images/emots/default/smile.gif',
  102. '1b5402269f6667281f25eaab21b81cb2' => 'common/images/emots/default/struggle.gif',
  103. '4fbc8725aa18b52a49ae5f3167c2b2d8' => 'common/images/emots/default/mad.gif',
  104. '89b6eca141f5150a765a7416af7de044' => 'common/images/emots/default/sleep.gif',
  105. 'b2b128d1603e3c81a0030619c9a78f29' => 'common/images/emots/default/sad.gif',
  106. 'e366f331c7e86467887cdb44f83a8127' => 'common/images/emots/default/panic.gif',
  107. '20b47ef5c6c4081c7f5c1251f6c9760d' => 'common/images/emots/default/ohmy.gif',
  108. '5807abd3b5c78ec8d2d23cc4b882b3af' => 'common/images/emots/default/wail.gif',
  109. '90ed96b1a358301ac153a19717cd052c' => 'common/images/emots/default/titter.gif',
  110. 'bbe2b8079f05b831631c1fdbdc671e2d' => 'common/images/emots/default/shy.gif',
  111. 'fae6c3f152523d95fe152cd99b476f90' => 'common/images/emots/default/awkward.gif',
  112. '301cf1b333e201d1adfe8b9ae6ff277a' => 'common/images/emots/default/tongue.gif',
  113. '6658f97feb53ffb3151640f9dae1d70f' => 'common/images/emots/default/knock.gif',
  114. '91f2feec64292b805884d328247f2fa3' => 'common/images/emots/default/envy.gif',
  115. 'bcae571397af456b800fa516d15a7d67' => 'common/images/emots/default/curse.gif',
  116. '30da1edf12e5942046091bad38c90f5b' => 'common/images/emots/default/doubt.gif',
  117. '68344b1f75c2b180640be3a9db31c11c' => 'common/images/emots/default/proud.gif',
  118. 'a09b0c20b15ea8206bd133792d6dc203' => 'common/images/emots/default/shutup.gif',
  119. 'bedebef3e120d4fe5a473a8fe23a293f' => 'common/images/emots/default/wronged.gif',
  120. );
  121. return isset($file_sum[$sha1sum]) ? isset($file_sum[$sha1sum]) : null;
  122. }
  123. /**
  124. * ?? media ??
  125. *
  126. * @param int|string $id
  127. * @return array|null
  128. */
  129. function media_get($id) {
  130. $ckey = 'media.id.'.$id;
  131. $data = fcache_get($ckey);
  132. if (fcache_not_null($data)) return $data;
  133. $db = get_conn();
  134. if (strlen($id) == 40) {
  135. $rs = $db->query("SELECT * FROM `#@_media` WHERE `sha1sum`='%s' LIMIT 0,1;", $id);
  136. } else {
  137. $rs = $db->query("SELECT * FROM `#@_media` WHERE `mediaid`=%d LIMIT 0,1;", $id);
  138. }
  139. if ($data = $db->fetch($rs)) {
  140. $file = media_file($data);
  141. $data['path'] = ABS_PATH . '/' . $file;
  142. $data['url'] = ROOT . $file;
  143. fcache_set($ckey, $data);
  144. }
  145. return $data;
  146. }
  147. /**
  148. * ??media??
  149. *
  150. * @param array $data
  151. * @return string
  152. */
  153. function media_file($data) {
  154. if (!is_array($data)) $data = media_get(intval($data));
  155. return MEDIA_PATH . '/' . $data['folder'] . '/' . date('Y-m-d', $data['addtime']) . '/' . $data['sha1sum'] . '.' . $data['suffix'];
  156. }
  157. /**
  158. * ?ID???????
  159. *
  160. * @param string $content
  161. * @return string
  162. */
  163. function media_decode($content) {
  164. if (preg_match_all('/(<img[^>]+src\s*=")\[(\d+)\]("[^>]*>)/i', $content, $matchs)) {
  165. foreach ($matchs[2] as $i=>$id) {
  166. $media = media_get($id);
  167. $content = str_replace(
  168. $matchs[0][$i],
  169. $matchs[1][$i] . $media['url'] . $matchs[3][$i],
  170. $content
  171. );
  172. }
  173. }
  174. return $content;
  175. }
  176. /**
  177. * ????
  178. *
  179. * @param string $folder
  180. * @param string $sha1sum
  181. * @param string $name
  182. * @param int $size
  183. * @param string $suffix
  184. * @param int $addtime
  185. * @return int|bool
  186. */
  187. function media_add($folder, $sha1sum, $name, $size, $suffix, $addtime=null) {
  188. global $_USER;
  189. // ???????
  190. if (!isset($_USER)) $_USER = user_current(false);
  191. return @get_conn()->insert('#@_media', array(
  192. 'folder' => $folder,
  193. 'sha1sum' => $sha1sum,
  194. 'name' => $name,
  195. 'suffix' => $suffix,
  196. 'size' => $size,
  197. 'userid' => $_USER['userid'],
  198. 'addtime' => $addtime ? $addtime : time(),
  199. ));
  200. }
  201. /**
  202. * ????
  203. *
  204. * @param int $id
  205. * @return bool
  206. */
  207. function media_clean_cache($id) {
  208. return fcache_delete('media.id.'.$id);
  209. }
  210. /**
  211. * ????
  212. *
  213. * @param int $id
  214. * @return bool
  215. */
  216. function media_delete($id) {
  217. $id = intval($id);
  218. if (!$id) return false;
  219. if ($media = media_get($id)) {
  220. media_clean_cache($media['mediaid']);
  221. media_clean_cache($media['sha1sum']);
  222. get_conn()->delete('#@_media',array('mediaid'=>$id));
  223. // ????
  224. $files = glob(dirname($media['path']).'/'.$media['sha1sum'].'*');
  225. foreach($files as $file) if (is_file($file)) unlink($file);
  226. return true;
  227. }
  228. return false;
  229. }