PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/system/plugins/habarisilo/habarisilo.plugin.php

https://github.com/HabariMag/habarimag-old
PHP | 650 lines | 400 code | 81 blank | 169 comment | 69 complexity | d3eb876affd5fb7fd7585b2673c89530 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Simple file access silo
  4. *
  5. * @todo Create some helper functions in a superclass to display panel controls more easily, so that you don't need to include 80 lines of code to build a simple upload form every time.
  6. */
  7. class HabariSilo extends Plugin implements MediaSilo
  8. {
  9. protected $root = null;
  10. protected $url = null;
  11. const SILO_NAME = 'Habari';
  12. const DERIV_DIR = '.deriv';
  13. /**
  14. * Initialize some internal values when plugin initializes
  15. */
  16. public function action_init()
  17. {
  18. $user_path = HABARI_PATH . '/' . Site::get_path( 'user', true );
  19. $this->root = $user_path . 'files'; //Options::get('simple_file_root');
  20. $this->url = Site::get_url( 'user', true ) . 'files'; //Options::get('simple_file_url');
  21. }
  22. public function filter_activate_plugin( $ok, $file )
  23. {
  24. if ( Plugins::id_from_file( $file ) == Plugins::id_from_file( __FILE__ ) ) {
  25. if ( !$this->check_files() ) {
  26. EventLog::log( _t( "Habari Silo activation failed. The web server does not have permission to create the 'files' directory for the Habari Media Silo." ), 'warning', 'plugin' );
  27. Session::error( _t( "Habari Silo activation failed. The web server does not have permission to create the 'files' directory for the Habari Media Silo." ) );
  28. $ok = false;
  29. }
  30. // Don't bother loading if the gd library isn't active
  31. if ( !function_exists( 'imagecreatefromjpeg' ) ) {
  32. EventLog::log( _t( "Habari Silo activation failed. PHP has not loaded the gd imaging library." ), 'warning', 'plugin' );
  33. Session::error( _t( "Habari Silo activation failed. PHP has not loaded the gd imaging library." ) );
  34. $ok = false;
  35. }
  36. }
  37. return $ok;
  38. }
  39. public function action_plugin_activation( $file )
  40. {
  41. // Create required tokens
  42. ACL::create_token( 'create_directories', _t( 'Create media silo directories' ), 'Administration' );
  43. ACL::create_token( 'delete_directories', _t( 'Delete media silo directories' ), 'Administration' );
  44. ACL::create_token( 'upload_media', _t( 'Upload files to media silos' ), 'Administration' );
  45. ACL::create_token( 'delete_media', _t( 'Delete files from media silos' ), 'Administration' );
  46. }
  47. /**
  48. *
  49. * @param string $file. The name of the plugin file
  50. *
  51. * Delete the special silo permissions if they're no longer
  52. * being used.
  53. */
  54. public function action_plugin_deactivation( $file ) {
  55. $silos = Plugins::get_by_interface( 'MediaSilo' );
  56. if ( count( $silos ) <= 1 ) {
  57. ACL::destroy_token( 'upload_media' );
  58. ACL::destroy_token( 'delete_media' );
  59. ACL::destroy_token( 'create_directories' );
  60. ACL::destroy_token( 'delete_directories' );
  61. }
  62. }
  63. /**
  64. *
  65. * Checks if files directory is usable
  66. */
  67. private function check_files() {
  68. $user_path = HABARI_PATH . '/' . Site::get_path( 'user', true );
  69. $this->root = $user_path . 'files'; //Options::get('simple_file_root');
  70. $this->url = Site::get_url( 'user', true ) . 'files'; //Options::get('simple_file_url');
  71. if ( !is_dir( $this->root ) ) {
  72. if ( is_writable( $user_path ) ) {
  73. mkdir( $this->root, 0755 );
  74. }
  75. else {
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. /**
  82. * Return basic information about this silo
  83. * name- The name of the silo, used as the root directory for media in this silo
  84. **/
  85. public function silo_info()
  86. {
  87. return array(
  88. 'name' => self::SILO_NAME,
  89. 'icon' => URL::get_from_filesystem( __FILE__ ) . '/icon.png'
  90. );
  91. }
  92. /**
  93. * Return directory contents for the silo path
  94. * @param string $path The path to retrieve the contents of
  95. * @return array An array of MediaAssets describing the contents of the directory
  96. **/
  97. public function silo_dir( $path )
  98. {
  99. if ( !isset( $this->root ) ) {
  100. return array();
  101. }
  102. $path = preg_replace( '%\.{2,}%', '.', $path );
  103. $results = array();
  104. $dir = Utils::glob( $this->root . ( $path == '' ? '' : '/' ) . $path . '/*' );
  105. foreach ( $dir as $item ) {
  106. if ( substr( basename( $item ), 0, 1 ) == '.' ) {
  107. continue;
  108. }
  109. if ( basename( $item ) == 'desktop.ini' ) {
  110. continue;
  111. }
  112. $file = basename( $item );
  113. $props = array(
  114. 'title' => basename( $item ),
  115. );
  116. if(is_dir($item)) {
  117. $results[] = new MediaAsset(
  118. self::SILO_NAME . '/' . $path . ($path == '' ? '' : '/') . basename( $item ),
  119. is_dir( $item ),
  120. $props
  121. );
  122. }
  123. else {
  124. $results[] = $this->silo_get( $path . ($path == '' ? '' : '/') . basename( $item ) );
  125. }
  126. }
  127. //print_r($results);
  128. return $results;
  129. }
  130. /**
  131. * Get the file from the specified path
  132. * @param string $path The path of the file to retrieve
  133. * @param array $qualities Qualities that specify the version of the file to retrieve.
  134. * @return MediaAsset The requested asset
  135. **/
  136. public function silo_get( $path, $qualities = null )
  137. {
  138. if ( ! isset( $this->root ) ) {
  139. return false;
  140. }
  141. $path = preg_replace( '%\.{2,}%', '.', $path );
  142. $file = basename( $path );
  143. $props = array(
  144. 'title' => basename( $path ),
  145. );
  146. $realfile = $this->root . '/' . $path;
  147. $thumbnail_suffix = HabariSilo::DERIV_DIR . '/' . $file . '.thumbnail.jpg';
  148. $thumbnail_url = $this->url . '/' . dirname( $path ) . ( dirname( $path ) == '' ? '' : '/' ) . $thumbnail_suffix;
  149. $mimetype = preg_replace(' %[^a-z_0-9]%', '_', Utils::mimetype( $realfile ) );
  150. $mtime = '';
  151. if ( !file_exists( dirname( $realfile ) . '/' . $thumbnail_suffix ) ) {
  152. if ( !$this->create_thumbnail( $realfile ) ) {
  153. // there is no thumbnail so use icon based on mimetype.
  154. $icon_path = Plugins::filter( 'habarisilo_icon_base_path', dirname( $this->get_file() ) . '/icons' );
  155. $icon_url = Plugins::filter( 'habarisilo_icon_base_url', $this->get_url() . '/icons' );
  156. if ( ( $icons = Utils::glob( $icon_path . '/*.{png,jpg,gif,svg}', GLOB_BRACE ) ) && $mimetype ) {
  157. $icon_keys = array_map( create_function( '$a', 'return pathinfo($a, PATHINFO_FILENAME);' ), $icons );
  158. $icons = array_combine( $icon_keys, $icons );
  159. $icon_filter = create_function( '$a, $b', "\$mime = '$mimetype';".'return (((strpos($mime, $a)===0) ? (strlen($a) / strlen($mime)) : 0) >= (((strpos($mime, $b)===0)) ? (strlen($b) / strlen($mime)) : 0)) ? $a : $b;' );
  160. $icon_key = array_reduce( $icon_keys, $icon_filter );
  161. if ($icon_key) {
  162. $icon = basename( $icons[$icon_key] );
  163. $thumbnail_url = $icon_url .'/'. $icon;
  164. }
  165. else {
  166. // couldn't find an icon so use default
  167. $thumbnail_url = $icon_url .'/default.png';
  168. }
  169. }
  170. }
  171. }
  172. // If the asset is an image, obtain the image dimensions
  173. if ( in_array( $mimetype, array( 'image_jpeg', 'image_png', 'image_gif' ) ) ) {
  174. list( $props['width'], $props['height'] ) = getimagesize( $realfile );
  175. $mtime = '?' . filemtime( $realfile );
  176. }
  177. $props = array_merge(
  178. $props,
  179. array(
  180. 'url' => $this->url . '/' . dirname( $path ) . ( $path == '' ? '' : '/' ) . $file,
  181. 'thumbnail_url' => $thumbnail_url . $mtime,
  182. 'filetype' => $mimetype,
  183. )
  184. );
  185. $asset = new MediaAsset( self::SILO_NAME . '/' . $path, false, $props );
  186. if ( file_exists( $realfile ) && is_file( $realfile ) ) {
  187. $asset->content = file_get_contents( $realfile );
  188. }
  189. return $asset;
  190. }
  191. /**
  192. * Create a thumbnail in the derivative directory
  193. *
  194. * @param string $src_filename The source filename
  195. * @param integer $max_width The maximum width of the output image
  196. * @param integer $max_height The maximum height of the output image
  197. * @return boolean true if the thumbnail creation succeeded
  198. */
  199. private function create_thumbnail( $src_filename, $max_width = Media::THUMBNAIL_WIDTH, $max_height = Media::THUMBNAIL_HEIGHT )
  200. {
  201. // Does derivative directory not exist?
  202. $thumbdir = dirname( $src_filename ) . '/' . HabariSilo::DERIV_DIR . '';
  203. if ( !is_dir( $thumbdir ) ) {
  204. // Create the derivative driectory
  205. if ( !mkdir( $thumbdir, 0755 ) ) {
  206. // Couldn't make derivative directory
  207. return false;
  208. }
  209. }
  210. // Get information about the image
  211. list( $src_width, $src_height, $type, $attr )= getimagesize( $src_filename );
  212. // Load the image based on filetype
  213. switch ( $type ) {
  214. case IMAGETYPE_JPEG:
  215. $src_img = imagecreatefromjpeg( $src_filename );
  216. break;
  217. case IMAGETYPE_PNG:
  218. $src_img = imagecreatefrompng( $src_filename );
  219. break;
  220. case IMAGETYPE_GIF:
  221. $src_img = imagecreatefromgif ( $src_filename );
  222. break;
  223. default:
  224. return false;
  225. }
  226. // Did the image fail to load?
  227. if ( !$src_img ) {
  228. return false;
  229. }
  230. // Calculate the output size based on the original's aspect ratio
  231. $y_displacement = 0;
  232. if ( $src_width / $src_height > $max_width / $max_height ) {
  233. $thumb_w = $max_width;
  234. $thumb_h = $src_height * $max_width / $src_width;
  235. // thumbnail is not full height, position it down so that it will be padded on the
  236. // top and bottom with black
  237. $y_displacement = ( $max_height - $thumb_h ) / 2;
  238. }
  239. else {
  240. $thumb_w = $src_width * $max_height / $src_height;
  241. $thumb_h = $max_height;
  242. }
  243. // Create the output image and copy to source to it
  244. $dst_img = ImageCreateTrueColor( $thumb_w, $max_height );
  245. imagecopyresampled( $dst_img, $src_img, 0, $y_displacement, 0, 0, $thumb_w, $thumb_h, $src_width, $src_height );
  246. /* Sharpen before save?
  247. $sharpenMatrix= array( array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1) );
  248. $divisor= 8;
  249. $offset= 0;
  250. imageconvolution( $dst_img, $sharpenMatrix, $divisor, $offset );
  251. //*/
  252. // Define the thumbnail filename
  253. $dst_filename = $thumbdir . '/' . basename( $src_filename ) . ".thumbnail.jpg";
  254. // Save the thumbnail as a JPEG
  255. imagejpeg( $dst_img, $dst_filename );
  256. // Clean up memory
  257. imagedestroy( $dst_img );
  258. imagedestroy( $src_img );
  259. return true;
  260. }
  261. /**
  262. * Create a new asset instance for the specified path
  263. * @param string $path The path of the new file to create
  264. * @return MediaAsset The requested asset
  265. **/
  266. public function silo_new( $path )
  267. {
  268. }
  269. /**
  270. * Store the specified media at the specified path
  271. * @param string $path The path of the file to retrieve
  272. * @param MediaAsset $filedata The MediaAsset to store
  273. * @return boolean True on success
  274. **/
  275. public function silo_put( $path, $filedata )
  276. {
  277. $path = preg_replace('%\.{2,}%', '.', $path);
  278. $file = $this->root . '/' . $path;
  279. $result = $filedata->save( $file );
  280. if ( $result ) {
  281. $this->create_thumbnail( $file );
  282. }
  283. return $result;
  284. }
  285. /**
  286. * Delete the file at the specified path
  287. * @param string $path The path of the file to retrieve
  288. **/
  289. public function silo_delete( $path )
  290. {
  291. $file = $this->root . '/' . $path;
  292. // Delete the file
  293. $result = unlink( $file );
  294. // If it's an image, remove the file in .deriv too
  295. $thumbdir = dirname( $file ) . '/' . HabariSilo::DERIV_DIR . '';
  296. $thumb = $thumbdir . '/' . basename( $file ) . ".thumbnail.jpg";
  297. if ( file_exists( $thumbdir ) && file_exists( $thumb ) ) {
  298. unlink( $thumb );
  299. // if this is the last thumb, delete the .deriv dir too
  300. if ( self::isEmptyDir( $thumbdir ) ) {
  301. rmdir( $thumbdir );
  302. }
  303. }
  304. return $result;
  305. }
  306. /**
  307. * Retrieve a set of highlights from this silo
  308. * This would include things like recently uploaded assets, or top downloads
  309. * @return array An array of MediaAssets to highlihgt from this silo
  310. **/
  311. public function silo_highlights()
  312. {
  313. }
  314. /**
  315. * Retrieve the permissions for the current user to access the specified path
  316. * @param string $path The path to retrieve permissions for
  317. * @return array An array of permissions constants (MediaSilo::PERM_READ, MediaSilo::PERM_WRITE)
  318. **/
  319. public function silo_permissions( $path )
  320. {
  321. }
  322. /**
  323. * Produce a link for the media control bar that causes a specific path to be displayed
  324. *
  325. * @param string $path The path to display
  326. * @param string $title The text to use for the link in the control bar
  327. * @return string The link to create
  328. */
  329. public function link_path( $path, $title = '' )
  330. {
  331. if ( $title == '' ) {
  332. $title = basename( $path );
  333. }
  334. return '<a href="#" onclick="habari.media.showdir(\''.$path.'\');return false;">' . $title . '</a>';
  335. }
  336. /**
  337. * Produce a link for the media control bar that causes a specific panel to be displayed
  338. *
  339. * @param string $path The path to pass
  340. * @param string $path The panel to display
  341. * @param string $title The text to use for the link in the control bar
  342. * @return string The link to create
  343. */
  344. public function link_panel( $path, $panel, $title )
  345. {
  346. return '<a href="#" onclick="habari.media.showpanel(\''.$path.'\', \''.$panel.'\');return false;">' . $title . '</a>';
  347. }
  348. /**
  349. * Provide controls for the media control bar
  350. *
  351. * @param array $controls Incoming controls from other plugins
  352. * @param MediaSilo $silo An instance of a MediaSilo
  353. * @param string $path The path to get controls for
  354. * @param string $panelname The name of the requested panel, if none then emptystring
  355. * @return array The altered $controls array with new (or removed) controls
  356. *
  357. * @todo This should really use FormUI, but FormUI needs a way to submit forms via ajax
  358. */
  359. public function filter_media_controls( $controls, $silo, $path, $panelname )
  360. {
  361. $class = __CLASS__;
  362. if ( $silo instanceof $class ) {
  363. $controls[] = $this->link_path( self::SILO_NAME . '/' . $path, _t( 'Browse' ) );
  364. if ( User::identify()->can( 'upload_media' ) ) {
  365. $controls[] = $this->link_panel( self::SILO_NAME . '/' . $path, 'upload', _t( 'Upload' ) );
  366. }
  367. if ( User::identify()->can( 'create_directories' ) ) {
  368. $controls[] = $this->link_panel( self::SILO_NAME . '/' . $path, 'mkdir', _t( 'Create Directory' ) );
  369. }
  370. if ( User::identify()->can( 'delete_directories' ) && ( $path && self::isEmptyDir( $this->root . '/' . $path ) ) ) {
  371. $controls[] = $this->link_panel( self::SILO_NAME . '/' . $path, 'rmdir', _t( 'Delete Directory' ) );
  372. }
  373. }
  374. return $controls;
  375. }
  376. /**
  377. * Provide requested media panels for this plugin
  378. *
  379. * Regarding Uploading:
  380. * A panel is returned to the media bar that contains a form, an iframe, and a javascript function.
  381. * The form allows the user to select a file, and is submitted back to the same URL that produced this panel in the first place.
  382. * This has the result of submitting the uploaded file to here when the form is submitted.
  383. * To prevent the panel form from reloading the whole publishing page, the form is submitted into the iframe.
  384. * An onload event attached to the iframe calls the function.
  385. * The function accesses the content of the iframe when it loads, which should contain the results of the request to obtain this panel, which are in JSON format.
  386. * The JSON data is passed to the habari.media.jsonpanel() function in media.js to process the data and display the results, just like when displaying a panel normally.
  387. *
  388. * @param string $panel The HTML content of the panel to be output in the media bar
  389. * @param MediaSilo $silo The silo for which the panel was requested
  390. * @param string $path The path within the silo (silo root omitted) for which the panel was requested
  391. * @param string $panelname The name of the requested panel
  392. * @return string The modified $panel to contain the HTML output for the requested panel
  393. *
  394. * @todo Move the uploaded file from the temporary location to the location indicated by the path field.
  395. */
  396. public function filter_media_panels( $panel, $silo, $path, $panelname)
  397. {
  398. $class = __CLASS__;
  399. if ( $silo instanceof $class ) {
  400. switch ( $panelname ) {
  401. case 'mkdir':
  402. $fullpath = self::SILO_NAME . '/' . $path;
  403. $form = new FormUI( 'habarisilomkdir' );
  404. $form->append( 'static', 'ParentDirectory', '<div style="margin: 10px auto;">' . _t( 'Parent Directory:' ) . " <strong>/{$path}</strong></div>" );
  405. // add the parent directory as a hidden input for later validation
  406. $form->append( 'hidden', 'path', 'null:unused' )->value = $path;
  407. $form->append( 'hidden', 'action', 'null:unused')->value = $panelname;
  408. $dir_text_control = $form->append( 'text', 'directory', 'null:unused', _t('What would you like to call the new directory?') );
  409. $dir_text_control->add_validator( array( $this, 'mkdir_validator' ) );
  410. $form->append( 'submit', 'submit', _t('Submit') );
  411. $form->media_panel($fullpath, $panelname, 'habari.media.forceReload();');
  412. $form->on_success( array( $this, 'dir_success' ) );
  413. $panel = $form->get(); /* form submission magicallly happens here */
  414. return $panel;
  415. break;
  416. case 'rmdir':
  417. $fullpath = self::SILO_NAME . '/' . $path;
  418. $form = new FormUI( 'habarisilormdir' );
  419. $form->append( 'static', 'RmDirectory', '<div style="margin: 10px auto;">' . _t( 'Directory:' ) . " <strong>/{$path}</strong></div>" );
  420. // add the parent directory as a hidden input for later validation
  421. $form->append( 'hidden', 'path', 'null:unused' )->value = $path;
  422. $form->append( 'hidden', 'action', 'null:unused')->value = $panelname;
  423. $dir_text_control = $form->append( 'static', 'directory', _t( 'Are you sure you want to delete this directory?' ) );
  424. $form->append( 'submit', 'submit', _t('Delete') );
  425. $form->media_panel( $fullpath, $panelname, 'habari.media.forceReload();' );
  426. $form->on_success( array( $this, 'dir_success' ) );
  427. $panel = $form->get(); /* form submission magicallly happens here */
  428. return $panel;
  429. break;
  430. case 'delete':
  431. $fullpath = self::SILO_NAME . '/' . $path;
  432. $form = new FormUI( 'habarisilodelete' );
  433. $form->append( 'static', 'RmFile', '<div style="margin: 10px auto;">' . _t( 'File:' ) . " <strong>/{$path}</strong></div>" );
  434. // add the parent directory as a hidden input for later validation
  435. $form->append( 'hidden', 'path', 'null:unused' )->value = $path;
  436. $dir_text_control = $form->append( 'static', 'directory', '<p>' . _t( 'Are you sure you want to delete this file?' ) . '</p>');
  437. $form->append( 'submit', 'submit', _t('Delete') );
  438. $form->media_panel( $fullpath, $panelname, 'habari.media.forceReload();' );
  439. $form->on_success( array( $this, 'do_delete' ) );
  440. $panel = $form->get();
  441. return $panel;
  442. break;
  443. case 'upload':
  444. if ( isset( $_FILES['file'] ) ) {
  445. $size = Utils::human_size( $_FILES['file']['size'] );
  446. $panel .= "<div class=\"span-18\" style=\"padding-top:30px;color: #e0e0e0;margin: 0px auto;\"><p>" . _t( 'File Uploaded: ' ) . "{$_FILES['file']['name']} ($size)</p>";
  447. $path = self::SILO_NAME . '/' . preg_replace( '%\.{2,}%', '.', $path ). '/' . $_FILES['file']['name'];
  448. $asset = new MediaAsset( $path, false );
  449. $asset->upload( $_FILES['file'] );
  450. if ( $asset->put() ) {
  451. $panel .= '<p>' . _t( 'File added successfully.' ) . '</p>';
  452. }
  453. else {
  454. $panel .= '<p>' . _t( 'File could not be added to the silo.' ) . '</p>';
  455. }
  456. $panel .= '<p><a href="#" onclick="habari.media.forceReload();habari.media.showdir(\'' . dirname( $path ) . '\');">' . _t( 'Browse the current silo path.' ) . '</a></p></div>';
  457. }
  458. else {
  459. $fullpath = self::SILO_NAME . '/' . $path;
  460. $form_action = URL::get( 'admin_ajax', array( 'context' => 'media_panel' ) );
  461. $panel .= <<< UPLOAD_FORM
  462. <form enctype="multipart/form-data" method="post" id="simple_upload" target="simple_upload_frame" action="{$form_action}" class="span-10" style="margin:0px auto;text-align: center">
  463. <p style="padding-top:30px;">%s <b style="font-weight:normal;color: #e0e0e0;font-size: 1.2em;">/{$path}</b></p>
  464. <p><input type="file" name="file"><input type="submit" name="upload" value="%s">
  465. <input type="hidden" name="path" value="{$fullpath}">
  466. <input type="hidden" name="panel" value="{$panelname}">
  467. </p>
  468. </form>
  469. <iframe id="simple_upload_frame" name="simple_upload_frame" style="width:1px;height:1px;" onload="simple_uploaded();"></iframe>
  470. <script type="text/javascript">
  471. var responsedata;
  472. function simple_uploaded() {
  473. if (!$('#simple_upload_frame')[0].contentWindow) return;
  474. var response = $($('#simple_upload_frame')[0].contentWindow.document.body).text();
  475. if (response) {
  476. eval('responsedata = ' + response);
  477. window.setTimeout(simple_uploaded_complete, 500);
  478. }
  479. }
  480. function simple_uploaded_complete() {
  481. habari.media.jsonpanel(responsedata);
  482. }
  483. </script>
  484. UPLOAD_FORM;
  485. $panel = sprintf( $panel, _t( "Upload to:" ), _t( "Upload" ) );
  486. }
  487. }
  488. }
  489. return $panel;
  490. }
  491. /**
  492. * A validator for the mkdir form created with FormUI. Checks to see if the
  493. * webserver can write to the parent directory and that the directory does
  494. * not already exist.
  495. * @param $dir The input from the form
  496. * @param $control The FormControl object
  497. * @param $form The FormUI object
  498. */
  499. public function mkdir_validator( $dir, $control, $form )
  500. {
  501. if ( strpos( $dir, '*' ) !== false || preg_match( '%(?:^|/)\.%', $dir ) ) {
  502. return array( _t( "The directory name contains invalid characters: %s.", array( $dir ) ) );
  503. }
  504. $path = preg_replace( '%\.{2,}%', '.', $form->path->value );
  505. $dir = $this->root . ( $path == '' ? '' : '/' ) . $path . '/'. $dir;
  506. if ( !is_writable( $this->root . '/' . $path ) ) {
  507. return array( _t( "Webserver does not have permission to create directory: %s.", array( $dir ) ) );
  508. }
  509. if ( is_dir( $dir ) ) {
  510. return array( _t( "Directory: %s already exists.", array( $dir ) ) );
  511. }
  512. return array();
  513. }
  514. /**
  515. * This function performs the mkdir and rmdir actions on submission of the form.
  516. * It is called by FormUI's success() method.
  517. * @param FormUI $form
  518. */
  519. public function dir_success ( $form )
  520. {
  521. $dir = preg_replace( '%\.{2,}%', '.', $form->directory->value );
  522. $path = preg_replace( '%\.{2,}%', '.', $form->path->value );
  523. switch ( $form->action->value ) {
  524. case 'rmdir':
  525. $dir = $this->root . ( $path == '' ? '' : '/' ) . $path;
  526. rmdir( $dir );
  527. $msg = 'Directory Deleted:';
  528. $what = $path;
  529. break;
  530. case 'mkdir':
  531. $dir = $this->root . ( $path == '' ? '' : '/' ) . $path . '/'. $dir;
  532. mkdir( $dir, 0755 );
  533. $msg = 'Directory Created:';
  534. $what = $path . '/' . $form->directory->value;
  535. break;
  536. }
  537. return '<div class="span-18"style="padding-top:30px;color: #e0e0e0;margin: 0px auto;"><p>' . _t( $msg ) . ' ' . $what . '</p></div>';
  538. }
  539. /**
  540. * This function takes the path passed from the form and passes it to silo_delete
  541. * to delete the file and it's thumbnail if it's an image.
  542. *
  543. * @param FormUI $form
  544. */
  545. public function do_delete ( $form )
  546. {
  547. $path = preg_replace( '%\.{2,}%', '.', $form->path->value );
  548. $result = $this->silo_delete($path);
  549. $panel = '<div class="span-18"style="padding-top:30px;color: #e0e0e0;margin: 0px auto;">';
  550. if ( $result ) {
  551. $panel .= '<p>' . _t( 'File deleted successfully.' ) . '</p>';
  552. } else {
  553. $panel .= '<p>' . _t( 'Failed to delete file.' ) . '</p>';
  554. }
  555. $panel .= '<p><a href="#" onclick="habari.media.forceReload();habari.media.showdir(\'' . self::SILO_NAME . '/' . dirname( $path ) . '\');">' . _t( 'Browse the current silo path.' ) . '</a></p></div>';
  556. return $panel;
  557. }
  558. /**
  559. * This function is used to check if a directory is empty.
  560. *
  561. * @param string $dir
  562. * @return boolean
  563. */
  564. private static function isEmptyDir( $dir )
  565. {
  566. return ( ( $files = @scandir( $dir ) ) && count( $files ) <= 2 );
  567. }
  568. }
  569. ?>