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

/components/com_easyblog/classes/mediamanager/local.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 463 lines | 266 code | 93 blank | 104 comment | 36 complexity | 987fa09a5f5fec26ef47629f06b1e1fb MD5 | raw file
  1. <?php
  2. /**
  3. * @package EasyBlog
  4. * @copyright Copyright (C) 2010 Stack Ideas Private Limited. All rights reserved.
  5. * @license GNU/GPL, see LICENSE.php
  6. *
  7. * EasyBlog is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. defined('_JEXEC') or die('Restricted access');
  14. require_once( EBLOG_CLASSES . DIRECTORY_SEPARATOR . 'json.php' );
  15. class EasyBlogMediaManagerLocalSource
  16. {
  17. private $types = array(
  18. 'jpg' => 'image',
  19. 'png' => 'image',
  20. 'gif' => 'image',
  21. 'bmp' => 'image',
  22. 'jpeg' => 'image',
  23. 'mp4' => 'video',
  24. 'swf' => 'video',
  25. 'flv' => 'video',
  26. 'mov' => 'video',
  27. 'f4v' => 'video',
  28. '3gp' => 'video',
  29. 'aac' => 'video',
  30. 'm4a' => 'audio',
  31. 'm4v' => 'video',
  32. 'webm' => 'video',
  33. 'ogv' => 'video',
  34. 'mp3' => 'audio',
  35. 'ogg' => 'audio'
  36. );
  37. private $relative = null;
  38. private $path = null;
  39. private $fileName = null;
  40. private $baseURI = null;
  41. private $includeVariation = null;
  42. private $place = null;
  43. /**
  44. * Returns an array of items that are available in a given path.
  45. *
  46. * @access public
  47. * @param string $path The path that contains the items.
  48. * @param int $depth The depth level to search for child items.
  49. */
  50. public function getItems( $absolutePath , $baseURI , $relativePath = '' , $variation = false , $flatList = false , $exclude = array( 'index.html' , '.svn', 'CVS', '.DS_Store', '__MACOSX') , $foldersOnly = false , $place = '' , $paginated = false )
  51. {
  52. // @task: Assign the absolute path
  53. $this->path = $absolutePath;
  54. // @task: Assign the base URI.
  55. $this->baseURI = $baseURI;
  56. // @task: Assign the relative path
  57. $this->relative = $relativePath;
  58. // @task: Include variations?
  59. $this->includeVariation = $variation;
  60. // @task: Assign the file name
  61. $this->fileName = basename( $absolutePath );
  62. $this->place = $place;
  63. $folders = JFolder::folders( rtrim( $absolutePath , DIRECTORY_SEPARATOR ) , '.' , $flatList , true );
  64. if( $foldersOnly )
  65. {
  66. $items = $folders;
  67. }
  68. else
  69. {
  70. $files = JFolder::files( rtrim( $absolutePath , DIRECTORY_SEPARATOR ) , '.' , $flatList , true , $exclude );
  71. $items = $this->mix( $folders , $files );
  72. }
  73. $result = array();
  74. $i = 1;
  75. foreach( $items as $item )
  76. {
  77. // @task: We need to ignore all types of variations.
  78. if( stristr( $item , EBLOG_SYSTEM_VARIATION_PREFIX ) === false && stristr( $item , EBLOG_USER_VARIATION_PREFIX ) === false && stristr( $item , EBLOG_BLOG_IMAGE_PREFIX ) === false )
  79. {
  80. // @task: Legacy support for old "thumbnail_"
  81. if( stristr( $item , EBLOG_MEDIA_THUMBNAIL_PREFIX ) === false && ( !$paginated || $paginated && $i < EBLOG_MEDIA_PAGINATION_TOTAL ) )
  82. {
  83. $result[] = $this->getObject( $item , $flatList , false , $foldersOnly );
  84. $i++;
  85. }
  86. }
  87. }
  88. return $result;
  89. }
  90. /**
  91. * Retrieves a single item object given the absolute path to that particular item.
  92. */
  93. public function getItem( $absolutePath , $baseURI , $relativePath = '' , $variation = false, $place = '' , $isUpload = false , $foldersOnly = false , $paginated = false , $filesize = false )
  94. {
  95. // @task: Assign the absolute path
  96. $this->path = $absolutePath;
  97. // @task: Assign the base URI.
  98. $this->baseURI = $baseURI;
  99. // @task: Assign the relative path
  100. $this->relative = $relativePath;
  101. // @task: Include variations?
  102. $this->includeVariation = $variation;
  103. $this->place = $place;
  104. if( $place == 'user' )
  105. {
  106. $this->place = $place . ':' . JFactory::getUser()->id;
  107. }
  108. return $this->getObject( $absolutePath , false , $isUpload , $foldersOnly , $paginated , $filesize );
  109. }
  110. /**
  111. * Creates a variation item based on the original image.
  112. * This only works with image type currently.
  113. *
  114. * @access public
  115. * @param string $absolutePath
  116. * @param string $variationName
  117. * @param int $width
  118. * @param int $height
  119. */
  120. public function createVariation( $absolutePath , $absoluteURI , $variationName , $width , $height , $variationType = EBLOG_VARIATION_USER_TYPE )
  121. {
  122. $mediaItem = $this->getTypeObject( $absolutePath );
  123. return $mediaItem->createVariation( $absoluteURI , $variationName , $width , $height , $variationType );
  124. }
  125. /**
  126. * Creates a variation item based on the original image.
  127. * This only works with image type currently.
  128. *
  129. * @access public
  130. * @param string $absolutePath
  131. * @param string $variationName
  132. */
  133. public function deleteVariation( $absolutePath , $variationName )
  134. {
  135. $mediaItem = $this->getTypeObject( $absolutePath );
  136. return $mediaItem->deleteVariation( $variationName );
  137. }
  138. /**
  139. * Merge both folder and files and sort them based on the creation time.
  140. */
  141. private function mix()
  142. {
  143. $args = func_get_args();
  144. if( count( $args ) <= 0 )
  145. {
  146. return array();
  147. }
  148. $items = $args[0];
  149. if( count( $args ) > 1 )
  150. {
  151. for( $i = 1; $i < count( $args ); $i++ )
  152. {
  153. $items = array_merge( $items , $args[ $i ] );
  154. }
  155. }
  156. // @task: Sort the result.
  157. array_multisort(
  158. array_map( 'filectime', $items ),
  159. SORT_NUMERIC,
  160. SORT_DESC,
  161. $items
  162. );
  163. return $items;
  164. }
  165. private function getType( $path )
  166. {
  167. // Unknown extensions will use the types/file.php
  168. $classType = 'file';
  169. if( is_dir( $path ) )
  170. {
  171. $classType = 'folder';
  172. }
  173. if( $classType != 'folder' )
  174. {
  175. $extension = JString::strtolower( JFile::getExt( $path ) );
  176. if( isset( $this->types[ $extension ] ) )
  177. {
  178. $classType = $this->types[ $extension ];
  179. }
  180. }
  181. return $classType;
  182. }
  183. private function getTypeObject( $path , $flatList = false , $isUpload = false , $foldersOnly = false , $paginated = '' )
  184. {
  185. $classType = $this->getType( $path );
  186. // @task: Let's try to see if this library exists in our known list.
  187. $lib = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'types' . DIRECTORY_SEPARATOR . strtolower( $classType ) . '.php';
  188. if( !JFile::exists( $lib ) )
  189. {
  190. return false;
  191. }
  192. require_once( $lib );
  193. $className = 'EasyBlogMediaManager' . ucfirst( $classType );
  194. $mediaItem = new $className( $path , $this->baseURI , $this->relative , $this->includeVariation , $flatList , $this->path , $isUpload , $foldersOnly , $this->place , $paginated );
  195. return $mediaItem;
  196. }
  197. private function getObject( $item , $flatList = false , $isUpload = false , $foldersOnly = false , $paginated = false , $filesize = false )
  198. {
  199. $mediaItem = $this->getTypeObject( $item , $flatList , $isUpload , $foldersOnly , $paginated );
  200. $obj = new stdClass();
  201. // @task: The media type.
  202. $obj->type = $mediaItem->getType();
  203. // @task: Get the media item's title.
  204. if( $obj->type == 'folder' && !empty( $this->place ))
  205. {
  206. $place = explode( ':' , $this->place );
  207. $storeType = $place[ 0 ];
  208. if( $mediaItem->getRelativePath() == '/' )
  209. {
  210. if( $storeType == 'user' )
  211. {
  212. $obj->title = JText::_( 'COM_EASYBLOG_MM_MY_MEDIA' );
  213. }
  214. else
  215. {
  216. $obj->title = JText::_( 'COM_EASYBLOG_MM_SHARED_MEDIA' );
  217. }
  218. }
  219. else
  220. {
  221. $obj->title = $mediaItem->getTitle();
  222. }
  223. }
  224. else
  225. {
  226. $obj->title = $mediaItem->getTitle();
  227. }
  228. // @task: Get the mime type
  229. $obj->mime = $mediaItem->getMime();
  230. // @task: Determine the filesize of this item (Bytes).
  231. if( $filesize )
  232. {
  233. $obj->filesize = $mediaItem->getSize();
  234. }
  235. // @task: Get the absolute URI to the item.
  236. $obj->url = $mediaItem->getURI();
  237. // @task: Get the creation date of the item.
  238. $obj->dateCreated = $mediaItem->getDateCreated();
  239. // @task: Get the creation date of the item.
  240. $obj->dateModified = $mediaItem->getDateModified();
  241. // @task: Get the contents
  242. // @todo
  243. $obj->path = $mediaItem->getRelativePath();
  244. // Set the place of the current item.
  245. $obj->place = $this->place;
  246. // @task: Allow the media item to inject it's own properties.
  247. $mediaItem->inject( $obj );
  248. return $obj;
  249. }
  250. /**
  251. * Determins if an item exists.
  252. *
  253. * @access public
  254. * @param string $path The path to the item.
  255. * @return boolean True on success, false otherwise.
  256. */
  257. public static function exists( $path )
  258. {
  259. // @rule: Test with JFolder first.
  260. if( @JFolder::exists( $path ) )
  261. {
  262. return true;
  263. }
  264. if( @JFile::exists( $path ) )
  265. {
  266. return true;
  267. }
  268. return false;
  269. }
  270. /**
  271. * Delete's an item from the local storage.
  272. *
  273. * @access public
  274. * @param string $file The path to the item.
  275. */
  276. public function delete( $absolutePath )
  277. {
  278. $mediaItem = $this->getTypeObject( $absolutePath );
  279. return $mediaItem->delete( $absolutePath );
  280. }
  281. public function getUniqueName( $storagePath , $fileName )
  282. {
  283. jimport( 'joomla.filesystem.file' );
  284. $i = 1;
  285. $itemPath = JPath::clean( $storagePath . DIRECTORY_SEPARATOR . $fileName );
  286. // @task: Now, we need to ensure that this file doesn't exist on the system.
  287. if( JFile::exists( $itemPath ) )
  288. {
  289. while( JFile::exists( $itemPath ) )
  290. {
  291. // get file extension here:
  292. $ext = EasyImageHelper::getFileExtension($fileName);
  293. $ext = '.' . $ext;
  294. $tmpFileName = str_replace( $ext , '', $fileName);
  295. $tmp = $tmpFileName . '_' . EasyBlogHelper::getDate()->toFormat( "%Y%m%d-%H%M%S" ) . '_' . $i . $ext;
  296. // Reset the itempath.
  297. $itemPath = JPath::clean( $storagePath . DIRECTORY_SEPARATOR . $tmp );
  298. $i++;
  299. }
  300. // Get the new file name for this item.
  301. $fileName = $tmp;
  302. }
  303. return $fileName;
  304. }
  305. /**
  306. * Responsible to handle file uploads
  307. *
  308. */
  309. public function upload( $storagePath , $storageURI , $file , $relativePath , $place )
  310. {
  311. // Cleanup any trailing slashes here.
  312. $storageURI = rtrim( $storageURI , '/' );
  313. // Import necessary library.
  314. jimport( 'joomla.filesystem.file' );
  315. // Ensure that the file name is safe.
  316. $file[ 'name' ] = JFile::makeSafe( $file[ 'name' ] );
  317. // Ensure that the file name does not contain UTF-8 data.
  318. $file[ 'name' ] = trim( $file[ 'name' ] );
  319. $fileName = $file[ 'name' ];
  320. //
  321. $date = EasyBlogDateHelper::getDate();
  322. if(strpos( $fileName , '.' ) === false )
  323. {
  324. $fileName = $date->toFormat( "%Y%m%d-%H%M%S" ) . '.' . $fileName;
  325. }
  326. else if( strpos( $fileName , '.' ) == 0 )
  327. {
  328. $fileName = $date->toFormat( "%Y%m%d-%H%M%S" ) . $fileName;
  329. }
  330. // We do not want to allow spaces in the name.
  331. $fileName = str_ireplace(' ', '-', $fileName);
  332. // Try to see if there's a need to generate a unique file name.
  333. $fileName = self::getUniqueName( $storagePath , $fileName );
  334. // Get the full path to this temporary item that would be uploaded.
  335. $itemPath = JPath::clean( $storagePath . DIRECTORY_SEPARATOR . $fileName );
  336. // @TODO: For now this is hardcoded to determine the type of the item. It is either an image or a file. That's it.
  337. $lib = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'types' . DIRECTORY_SEPARATOR . 'item.php';
  338. $classType = 'Item';
  339. if( self::isImage( $file[ 'tmp_name' ] ) )
  340. {
  341. $lib = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'types' . DIRECTORY_SEPARATOR . 'image.php';
  342. $classType = 'Image';
  343. }
  344. require_once( $lib );
  345. $className = 'EasyBlogMediaManager' . ucfirst( $classType );
  346. // Let's pass this to the respective media items to process.
  347. // PHP 5.2 doesn't allow calling dynamic class variables for some reason.
  348. $obj = new $className( '' , '' , $relativePath );
  349. $result = $obj->upload( $storagePath , $fileName , $file );
  350. if( $result !== true )
  351. {
  352. return $result;
  353. }
  354. // Set the relative path.
  355. $this->relative = $relativePath;
  356. $obj = $this->getItem( $itemPath , $storageURI , $relativePath , true , $place , true );
  357. return $obj;
  358. }
  359. /**
  360. * Returns the file type
  361. */
  362. private function isImage( $path )
  363. {
  364. if( @getimagesize( $path ) === false )
  365. {
  366. return false;
  367. }
  368. return true;
  369. }
  370. }