users.php 3.3 KB

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