Browse Source

Download file from ftp server

jmontoyaa 7 years ago
parent
commit
3fd96c6b17
2 changed files with 38 additions and 0 deletions
  1. 31 0
      plugin/studentfollowup/post.php
  2. 7 0
      plugin/studentfollowup/view/post.html.twig

+ 31 - 0
plugin/studentfollowup/post.php

@@ -2,6 +2,8 @@
 /* For licensing terms, see /license.txt */
 
 use Doctrine\Common\Collections\Criteria;
+use Gaufrette\Adapter\Ftp as FtpAdapter;
+use Gaufrette\Filesystem;
 use Chamilo\PluginBundle\Entity\StudentFollowUp\CarePost;
 
 require_once __DIR__.'/../../main/inc/global.inc.php';
@@ -11,6 +13,7 @@ $plugin = StudentFollowUpPlugin::create();
 $currentUserId = api_get_user_id();
 $studentId = isset($_GET['student_id']) ? (int) $_GET['student_id'] : api_get_user_id();
 $postId = isset($_GET['post_id']) ? (int) $_GET['post_id'] : 1;
+$action = isset($_GET['action']) ? $_GET['action'] : '';
 
 if (empty($studentId)) {
     api_not_allowed(true);
@@ -48,6 +51,34 @@ $post = $query->getOneOrNullResult();
 // Get related posts (post with same parent)
 $relatedPosts = [];
 if ($post) {
+    if ($action == 'download') {
+        $attachment = $post->getAttachment();
+        $attachmentUrlData = parse_url($attachment);
+        if (!empty($attachment) && !empty($attachmentUrlData)) {
+            $adapter = new FtpAdapter(
+                '/',
+                $attachmentUrlData['host'],
+                [
+                    'port' => 21,
+                    'username' => isset($attachmentUrlData['user']) ? $attachmentUrlData['user'] : '',
+                    'password' => isset($attachmentUrlData['pass']) ? $attachmentUrlData['pass'] : '',
+                    'passive' => true,
+                    'create' => false, // Whether to create the remote directory if it does not exist
+                    'mode' => FTP_BINARY, // Or FTP_TEXT
+                    'ssl' => false,
+                ]
+            );
+            $filesystem = new Filesystem($adapter);
+            if ($filesystem->has($attachmentUrlData['path'])) {
+                echo $filesystem->get($attachmentUrlData['path']);
+            } else {
+                api_not_allowed(true);
+            }
+        } else {
+            api_not_allowed(true);
+        }
+    }
+
     $qb = $em->createQueryBuilder();
     $criteria = Criteria::create();
 

+ 7 - 0
plugin/studentfollowup/view/post.html.twig

@@ -19,6 +19,13 @@
                             </a>
                         {% endif %}
                     {% endif %}
+
+                    {% if post.attachment %}
+                        <a href="{{ post_url }}&action=download&post_id={{ post.id }}" class="btn btn-default">
+                            {{ 'Download' | get_lang }}
+                        </a>
+                    {% endif %}
+
                     {#{% if type == 'all' %}#}
                         {#{% if related_posts %}#}
                             {#<h3>Related</h3>#}