users.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /* Integrate svg-edit libraries with Chamilo default documents
  3. * @author Juan Carlos Raña Trabado
  4. * @since 25/september/2010
  5. */
  6. require_once '../../../../../../inc/global.inc.php';
  7. //Add security from Chamilo
  8. api_protect_course_script();
  9. api_block_anonymous_users();
  10. $userId = api_get_user_id();
  11. $user_disk_path = UserManager::getUserPathById($userId, 'system').'my_files/';
  12. $user_web_path = UserManager::getUserPathById($userId, 'web').'my_files/';
  13. //get all files and folders
  14. $scan_files = [];
  15. if (is_dir($user_disk_path)) {
  16. $scan_files = scandir($user_disk_path);
  17. }
  18. //get all svg and png files
  19. $accepted_extensions = array('.svg', '.png');
  20. if (is_array($scan_files) && count($scan_files) > 0) {
  21. foreach ($scan_files as & $file) {
  22. $slideshow_extension = strrchr($file, '.');
  23. $slideshow_extension = strtolower($slideshow_extension);
  24. if (in_array($slideshow_extension, $accepted_extensions)) {
  25. $png_svg_files[] =$file;
  26. }
  27. }
  28. }
  29. $style = '<style>';
  30. $style .= '@import "'.api_get_path(WEB_CSS_PATH).'base.css";';
  31. $style .= '@import "'.api_get_path(WEB_CSS_PATH).'themes/'.api_get_visual_theme().'/default.css";';
  32. $style .='</style>';
  33. ?>
  34. <!doctype html>
  35. <?php echo api_get_jquery_js(); ?>
  36. <?php echo $style ?>
  37. <body>
  38. <?php
  39. echo '<h2>'.get_lang('SocialNetwork').': '.get_lang('MyFiles').'</h2>';
  40. if (!empty($png_svg_files)) {
  41. echo '<h3>'.get_lang('SelectSVGEditImage').'</h3>';
  42. echo '<ul>';
  43. foreach($png_svg_files as $filename) {
  44. $image = $user_disk_path.$filename;
  45. if (strpos($filename, "svg")){
  46. $new_sizes['width'] = 60;
  47. $new_sizes['height'] = 60;
  48. } else {
  49. $new_sizes = api_resize_image($image, 60, 60);
  50. }
  51. echo '<li style="display:inline; padding:8px;"><a href="'.$user_web_path.$filename.'" alt "'.$filename.'" title="'.$filename.'"><img src="'.$user_web_path.$filename.'" width="'.$new_sizes['width'].'" height="'.$new_sizes['height'].'" border="0"></a></li>';
  52. }
  53. echo '</ul>';
  54. } else {
  55. echo Display::return_message(get_lang('NoSVGImages'), 'warning');
  56. }
  57. ?>
  58. </body>
  59. <script>
  60. $('a').click(function() {
  61. var href = this.href;
  62. // Convert Non-SVG images to data URL first
  63. // (this could also have been done server-side by the library)
  64. if(this.href.indexOf('.svg') === -1) {
  65. var meta_str = JSON.stringify({
  66. name: $(this).text(),
  67. id: href
  68. });
  69. window.top.postMessage(meta_str, "*");
  70. var img = new Image();
  71. img.onload = function() {
  72. var canvas = document.createElement("canvas");
  73. canvas.width = this.width;
  74. canvas.height = this.height;
  75. // load the raster image into the canvas
  76. canvas.getContext("2d").drawImage(this,0,0);
  77. // retrieve the data: URL
  78. try {
  79. var dataurl = canvas.toDataURL();
  80. } catch(err) {
  81. // This fails in Firefox with file:// URLs :(
  82. alert("Data URL conversion failed: " + err);
  83. var dataurl = "";
  84. }
  85. window.top.postMessage('|' + href + '|' + dataurl, "*");
  86. }
  87. img.src = href;
  88. } else {
  89. // Send metadata (also indicates file is about to be sent)
  90. var meta_str = JSON.stringify({
  91. name: $(this).text(),
  92. id: href
  93. });
  94. window.top.postMessage(meta_str, "*");
  95. // Do ajax request for image's href value
  96. $.get(href, function(data) {
  97. data = '|' + href + '|' + data;
  98. // This is where the magic happens!
  99. window.top.postMessage(data, "*");
  100. }, 'html'); // 'html' is necessary to keep returned data as a string
  101. }
  102. return false;
  103. });
  104. </script>