list_unused_img.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php /* For licensing terms, see /license.txt */
  2. /**
  3. * Cron script to list unused images
  4. * @package chamilo.cron
  5. */
  6. /**
  7. * Includes and declarations
  8. */
  9. if (PHP_SAPI!='cli') {
  10. die('Run this script through the command line or comment this line in the code');
  11. }
  12. require_once '../../inc/global.inc.php';
  13. $path = api_get_path(SYS_CODE_PATH).'img/';
  14. ini_set('memory_limit', '128M');
  15. ini_set('max_execution_time', '240');
  16. $unused = array();
  17. global $_configuration;
  18. /**
  19. * Main code
  20. */
  21. // get all the available images and their directory
  22. $found_img = get_img_files($path);
  23. // Now, for each image, check if there is at least one reference
  24. chdir($_configuration['root_sys']);
  25. foreach ($found_img as $i => $p) {
  26. $j = 0;
  27. $output = @shell_exec('rgrep '.$i.' main/');
  28. $outputs = explode('\n', $output);
  29. foreach ($outputs as $line) {
  30. if (substr($line, 0, 5)=='rgrep') {
  31. //this means a permission error, ignore
  32. } else {
  33. $j++;
  34. }
  35. }
  36. if ($j === 0) {
  37. $unused[$i] = $p;
  38. }
  39. }
  40. echo '<table>';
  41. /*
  42. if (count($unexisting_img)<1) { die("No missing image<br />\n"); } else { echo "The following images were nowhere to be found: <br />\n<table>"; }
  43. foreach ($unexisting_img as $term => $file) {
  44. echo "<tr><td>$term</td><td>in $file</td></tr>\n";
  45. }
  46. */
  47. echo '<tr><td colspan="2">Existing images('.count($found_img).'), unused('.count($unused).')</td></tr>'."\n";
  48. echo '<tr><td>Image file</td><td>Used x times</td></tr>'."\n";
  49. $r = ksort($found_img);
  50. foreach ($unused as $term => $path) {
  51. if (isset($unused[$term])) {
  52. echo '<tr>';
  53. echo '<td bgcolor="#55ff55">'.$term.'</td>';
  54. echo '<td bgcolor="#55ff55">'.($path=='/'?'/':$path.'/').$term.'</td>';
  55. echo '</tr>'."\n";
  56. } else {
  57. echo '<tr>';
  58. echo '<td bgcolor="#ff5555">'.$term.'</td>';
  59. echo '<td bgcolor="#ff5555">'.($path=='/'?'/':$path.'/').$term.'</td>';
  60. echo '</tr>'."\n";
  61. }
  62. }
  63. echo "</table>\n";
  64. /**
  65. * @param $base_path
  66. * @return array
  67. */
  68. function get_all_php_files($base_path)
  69. {
  70. $list = scandir($base_path);
  71. $files = array();
  72. foreach ($list as $item) {
  73. if (substr($item, 0, 1)=='.') {
  74. continue;
  75. }
  76. $special_dirs = array(
  77. api_get_path(SYS_TEST_PATH),
  78. api_get_path(SYS_COURSE_PATH),
  79. api_get_path(SYS_LANG_PATH),
  80. api_get_path(SYS_ARCHIVE_PATH)
  81. );
  82. if (in_array($base_path.$item.'/', $special_dirs)) {
  83. continue;
  84. }
  85. if (is_dir($base_path.$item)) {
  86. $files = array_merge($files, get_all_php_files($base_path.$item.'/'));
  87. } else {
  88. //only analyse php files
  89. $ext = substr($item, -4);
  90. if (in_array($ext, array('.php', 'html', '.htm', '.css'))) {
  91. $files[] = $base_path.$item;
  92. }
  93. }
  94. }
  95. $list = null;
  96. return $files;
  97. }
  98. /**
  99. * Get the list of available images
  100. * @param string $path The path to start the scan from
  101. * @return array The files list
  102. */
  103. function get_img_files($path)
  104. {
  105. $files = array();
  106. //We know there are max 3 levels, so don't bother going recursive
  107. $list = scandir($path);
  108. foreach ($list as $entry) {
  109. if (substr($entry, 0, 1)=='.') {
  110. continue;
  111. }
  112. if (is_dir($path.$entry)) {
  113. $sublist = scandir($path.$entry);
  114. foreach ($sublist as $subentry) {
  115. if (substr($subentry, 0, 1)=='.') {
  116. continue;
  117. }
  118. if (is_dir($path.$entry.'/'.$subentry)) {
  119. $subsublist = scandir($path.$entry.'/'.$subentry);
  120. foreach ($subsublist as $subsubentry) {
  121. if (substr($subsubentry, 0, 1)=='.') {
  122. continue;
  123. }
  124. if (is_file($path.$entry.'/'.$subentry.'/'.$subsubentry)) {
  125. $files[$subsubentry] = '/'.$entry.'/'.$subentry;
  126. }
  127. }
  128. } elseif (is_file($path.$entry.'/'.$subentry)) {
  129. $files[$subentry] = '/'.$entry;
  130. }
  131. }
  132. } elseif (is_file($path.$entry)) {
  133. $files[$entry] = '/';
  134. }
  135. }
  136. return $files;
  137. }