PageRenderTime 26ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/4.6/administrator/components/com_media/admin.media.php

http://miacms.googlecode.com/
PHP | 359 lines | 260 code | 64 blank | 35 comment | 66 complexity | c9deb2ac5dde59b0c905fc34ad051bf4 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-2.0
  1. <?php
  2. /**
  3. * @package MiaCMS
  4. * @subpackage Media Manager
  5. * @author MiaCMS see README.php
  6. * @copyright see README.php
  7. * See COPYRIGHT.php for copyright notices and details.
  8. * @license GNU/GPL Version 2, see LICENSE.php
  9. * MiaCMS is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2 of the License.
  12. */
  13. /** ensure this file is being included by a parent file */
  14. defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  15. // ensure user has access to this function
  16. if (!($acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'all' )
  17. | $acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'com_media' ))) {
  18. mosRedirect( 'index2.php', T_('You are not authorized to view this resource.') );
  19. }
  20. require_once( $mainframe->getPath( 'admin_html' ) );
  21. //require_once( $mainframe->getPath( 'class' ) );
  22. $cid = mosGetParam( $_POST, 'cid', array(0) );
  23. $foldername = mosGetParam($_POST, 'foldername', '');
  24. $listdir = mosGetParam($_REQUEST, 'listdir', '');
  25. $dirPath = mosGetParam($_POST, 'dirPath', '');
  26. $task = mosGetParam($_REQUEST, 'task', '');
  27. $delFile = mosGetParam($_REQUEST, 'delFile', '');
  28. $delFolder = mosGetParam($_REQUEST, 'delFolder', '');
  29. if (!is_array( $cid )) {
  30. $cid = array(0);
  31. }
  32. if (is_int(strpos ($listdir, "..")) && $listdir<>'') {
  33. mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], T_("NO HACKING PLEASE") );
  34. }
  35. switch ($task) {
  36. case "upload":
  37. upload();
  38. showMedia($dirPath);
  39. break;
  40. case "newdir":
  41. if (ini_get('safe_mode')=="On") {
  42. mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], T_("Directory creation not allowed while running in SAFE MODE as this can cause problems.") );
  43. }
  44. else {
  45. create_folder($foldername,$dirPath);
  46. }
  47. showMedia($dirPath);
  48. break;
  49. case "delete":
  50. delete_file($delFile,$listdir);
  51. showMedia($listdir);
  52. break;
  53. case "deletefolder":
  54. delete_folder($delFolder,$listdir);
  55. showMedia($listdir);
  56. break;
  57. case "list":
  58. listImages($listdir);
  59. break;
  60. default:
  61. showMedia($listdir);
  62. break;
  63. }
  64. function delete_file($delfile, $listdir)
  65. {
  66. global $mosConfig_absolute_path;
  67. $del_image = $mosConfig_absolute_path."/images/stories".$listdir."/".$delfile;
  68. unlink($del_image);
  69. }
  70. function create_folder($folder_name,$dirPath)
  71. {
  72. global $mosConfig_absolute_path;
  73. if(strlen($folder_name) >0)
  74. {
  75. if (eregi("[^0-9a-zA-Z_]", $folder_name)) {
  76. mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], T_("Directory name must only contain alphanumeric characters and no spaces please.") );
  77. }
  78. $folder = $mosConfig_absolute_path."/images/stories".$dirPath."/".$folder_name;
  79. if(!is_dir($folder) && !is_file($folder))
  80. {
  81. mosMakePath($folder);
  82. $fp = fopen($folder."/index.html", "w" );
  83. fwrite( $fp, "<html>\n<body bgcolor=\"#FFFFFF\">\n</body>\n</html>" );
  84. fclose( $fp );
  85. mosChmod($folder."/index.html");
  86. $refresh_dirs = true;
  87. }
  88. }
  89. }
  90. function delete_folder($delFolder,$listdir)
  91. {
  92. global $mosConfig_absolute_path;
  93. $del_html = $mosConfig_absolute_path.'/images/stories'.$listdir.$delFolder.'/index.html';
  94. $del_folder = $mosConfig_absolute_path.'/images/stories'.$listdir.$delFolder;
  95. $entry_count = 0;
  96. $dir = opendir( $del_folder );
  97. while ( false !== ($entry = readdir( $dir )))
  98. {
  99. if( $entry != "." & $entry != ".." & strtolower($entry) != "index.html" )
  100. $entry_count++;
  101. }
  102. closedir( $dir );
  103. if( $entry_count < 1 )
  104. {
  105. @unlink($del_html);
  106. rmdir($del_folder);
  107. } else {
  108. echo '<font color="red">'.T_('Unable to delete: not empty!').'</font>';
  109. }
  110. }
  111. function upload(){
  112. global $mosConfig_absolute_path;
  113. if(isset($_FILES['upload']) && is_array($_FILES['upload']) && isset($_POST['dirPath']))
  114. {
  115. $dirPathPost = $_POST['dirPath'];
  116. if(strlen($dirPathPost) > 0)
  117. {
  118. if(substr($dirPathPost,0,1)=='/')
  119. $IMG_ROOT .= $dirPathPost;
  120. else
  121. $IMG_ROOT = $dirPathPost;
  122. }
  123. if(strrpos($IMG_ROOT, '/')!= strlen($IMG_ROOT)-1)
  124. $IMG_ROOT .= '/';
  125. do_upload( $_FILES['upload'], $mosConfig_absolute_path.'/images/stories/'.$dirPathPost.'/');
  126. }
  127. }
  128. function do_upload($file, $dest_dir)
  129. {
  130. global $clearUploads;
  131. if (file_exists($dest_dir.$file['name'])) {
  132. mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], T_("Upload FAILED. File already exists") );
  133. }
  134. if (!eregi( ".bmp$|.gif$|.jpg$|.png$|.ppt$|.doc$|.xls$|.swf$|.pdf$", $file['name'] )){
  135. mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], T_("Only files of type gif, png, jpg, bmp, pdf, swf, doc, xls or ppt can be uploaded") );
  136. }
  137. if (!move_uploaded_file($file['tmp_name'], $dest_dir.strtolower($file['name']))){
  138. mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], T_("Upload FAILED") );
  139. }
  140. else {
  141. mosChmod($dest_dir.strtolower($file['name']));
  142. mosRedirect( "index2.php?option=com_media&listdir=".$_POST['dirPath'], T_("Upload complete") );
  143. }
  144. $clearUploads = true;
  145. }
  146. function recursive_listdir($base) {
  147. static $filelist = array();
  148. static $dirlist = array();
  149. if(is_dir($base)) {
  150. $dh = opendir($base);
  151. while (false !== ($dir = readdir($dh))) {
  152. if (is_dir($base ."/". $dir) && $dir !== '.' && $dir !== '..' && strtolower($dir) !== 'cvs') {
  153. $subbase = $base ."/". $dir;
  154. $dirlist[] = $subbase;
  155. $subdirlist = recursive_listdir($subbase);
  156. }
  157. }
  158. closedir($dh);
  159. }
  160. return $dirlist;
  161. }
  162. /**
  163. * Show media manager
  164. * @param string The image directory to display
  165. */
  166. function showMedia($listdir) {
  167. global $mosConfig_absolute_path, $mosConfig_live_site;
  168. // get list of directories
  169. $imgFiles = recursive_listdir( $mosConfig_absolute_path."/images/stories" );
  170. $images = array();
  171. $folders = array();
  172. $folders[] = mosHTML::makeOption( "/" );
  173. foreach ($imgFiles as $file) {
  174. $folders[] = mosHTML::makeOption( substr($file,strlen($mosConfig_absolute_path."/images/stories")) );
  175. }
  176. if (is_array($folders)) {
  177. sort( $folders );
  178. }
  179. // create folder selectlist
  180. $dirPath = mosHTML::selectList( $folders, 'dirPath', "class=\"inputbox\" size=\"1\" "
  181. ."onchange=\"goUpDir()\" ",
  182. 'value', 'text', $listdir );
  183. HTML_Media::showMedia($dirPath,$listdir);
  184. }
  185. /**
  186. * Build imagelist
  187. * @param string The image directory to display
  188. */
  189. function listImages($listdir) {
  190. global $mosConfig_absolute_path, $mosConfig_live_site;
  191. // get list of images
  192. $d = @dir($mosConfig_absolute_path."/images/stories/".$listdir);
  193. if($d)
  194. {
  195. //var_dump($d);
  196. $images = array();
  197. $folders = array();
  198. $docs = array();
  199. while (false !== ($entry = $d->read()))
  200. {
  201. $img_file = $entry;
  202. if(is_file($mosConfig_absolute_path."/images/stories".$listdir.'/'.$img_file) && substr($entry,0,1) != '.' && strtolower($entry) !== 'index.html')
  203. {
  204. if (eregi( ".bmp$|.gif$|.jpg$|.png$", $img_file )) {
  205. $image_info = @getimagesize($mosConfig_absolute_path."/images/stories/".$listdir.'/'.$img_file);
  206. $file_details['file'] = $mosConfig_absolute_path."/images/stories".$listdir."/".$img_file;
  207. $file_details['img_info'] = $image_info;
  208. $file_details['size'] = filesize($mosConfig_absolute_path."/images/stories".$listdir."/".$img_file);
  209. $images[$entry] = $file_details;
  210. }
  211. else {
  212. // file is document
  213. $docs[$entry] = $img_file;
  214. }
  215. }
  216. else if(is_dir($mosConfig_absolute_path."/images/stories/".$listdir.'/'.$img_file) && substr($entry,0,1) != '.' && strtolower($entry) !== 'cvs')
  217. {
  218. $folders[$entry] = $img_file;
  219. }
  220. }
  221. $d->close();
  222. HTML_Media::imageStyle($listdir);
  223. if(count($images) > 0 || count($folders) > 0 || count($docs) > 0)
  224. {
  225. //now sort the folders and images by name.
  226. ksort($images);
  227. ksort($folders);
  228. ksort($docs);
  229. HTML_Media::draw_table_header();
  230. for($i=0; $i<count($folders); $i++)
  231. {
  232. $folder_name = key($folders);
  233. HTML_Media::show_dir('/'.$folders[$folder_name], $folder_name,$listdir);
  234. next($folders);
  235. }
  236. for($i=0; $i<count($docs); $i++)
  237. {
  238. $doc_name = key($docs);
  239. $iconfile= $mosConfig_absolute_path."/administrator/components/com_media/images/".substr($doc_name,-3)."_16.png";
  240. if (file_exists($iconfile)) {
  241. $icon = "components/com_media/images/".(substr($doc_name,-3))."_16.png" ; }
  242. else {
  243. $icon = "components/com_media/images/con_info.png";
  244. }
  245. HTML_Media::show_doc($docs[$doc_name], $listdir, $icon);
  246. next($docs);
  247. }
  248. for($i=0; $i<count($images); $i++)
  249. {
  250. $image_name = key($images);
  251. HTML_Media::show_image($images[$image_name]['file'], $image_name, $images[$image_name]['img_info'], $images[$image_name]['size'],$listdir);
  252. next($images);
  253. }
  254. HTML_Media::draw_table_footer();
  255. }
  256. else
  257. {
  258. HTML_Media::draw_no_results();
  259. }
  260. }
  261. else
  262. {
  263. HTML_Media::draw_no_dir();
  264. }
  265. function rm_all_dir($dir)
  266. {
  267. //$dir = dir_name($dir);
  268. //echo "OPEN:".$dir.'<Br>';
  269. if(is_dir($dir))
  270. {
  271. $d = @dir($dir);
  272. while (false !== ($entry = $d->read()))
  273. {
  274. //echo "#".$entry.'<br>';
  275. if($entry != '.' && $entry != '..')
  276. {
  277. $node = $dir.'/'.$entry;
  278. //echo "NODE:".$node;
  279. if(is_file($node)) {
  280. //echo " - is file<br>";
  281. unlink($node);
  282. }
  283. else if(is_dir($node)) {
  284. //echo " - is Dir<br>";
  285. rm_all_dir($node);
  286. }
  287. }
  288. }
  289. $d->close();
  290. rmdir($dir);
  291. }
  292. //echo "RM: $dir <br>";
  293. }
  294. }
  295. ?>