| 1 | www | 1 | <?php
 | 
        
           |  |  | 2 | declare(strict_types=1);
 | 
        
           |  |  | 3 |   | 
        
           |  |  | 4 | namespace LeadersLinked\Controller;
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 | use Laminas\Db\Adapter\AdapterInterface;
 | 
        
           |  |  | 7 | use Laminas\Cache\Storage\Adapter\AbstractAdapter;
 | 
        
           |  |  | 8 | use Laminas\Mvc\Controller\AbstractActionController;
 | 
        
           |  |  | 9 | use Laminas\Log\LoggerInterface;
 | 
        
           |  |  | 10 | use Laminas\Mvc\I18n\Translator;
 | 
        
           |  |  | 11 |   | 
        
           |  |  | 12 | use Laminas\Authentication\AuthenticationService;
 | 
        
           |  |  | 13 | use LeadersLinked\Library\Functions;
 | 
        
           |  |  | 14 | use LeadersLinked\Mapper\UserMapper;
 | 
        
           |  |  | 15 | use LeadersLinked\Mapper\UserProfileMapper;
 | 
        
           |  |  | 16 | use LeadersLinked\Mapper\CompanyMapper;
 | 
        
           |  |  | 17 | use LeadersLinked\Mapper\GroupMapper;
 | 
        
           |  |  | 18 | use LeadersLinked\Mapper\JobMapper;
 | 
        
           |  |  | 19 | use LeadersLinked\Mapper\ChatUserMapper;
 | 
        
           |  |  | 20 | use LeadersLinked\Mapper\ChatGroupMapper;
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 | class StorageController extends AbstractActionController
 | 
        
           |  |  | 23 | {
 | 
        
           |  |  | 24 |     /**
 | 
        
           |  |  | 25 |      *
 | 
        
           |  |  | 26 |      * @var AdapterInterface
 | 
        
           |  |  | 27 |      */
 | 
        
           |  |  | 28 |     private $adapter;
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 |     /**
 | 
        
           |  |  | 32 |      *
 | 
        
           |  |  | 33 |      * @var AbstractAdapter
 | 
        
           |  |  | 34 |      */
 | 
        
           |  |  | 35 |     private $cache;
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |     /**
 | 
        
           |  |  | 38 |      *
 | 
        
           |  |  | 39 |      * @var  LoggerInterface
 | 
        
           |  |  | 40 |      */
 | 
        
           |  |  | 41 |     private $logger;
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |     /**
 | 
        
           |  |  | 45 |      *
 | 
        
           |  |  | 46 |      * @var array
 | 
        
           |  |  | 47 |      */
 | 
        
           |  |  | 48 |     private $config;
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |     /**
 | 
        
           |  |  | 51 |      *
 | 
        
           |  |  | 52 |      * @param AdapterInterface $adapter
 | 
        
           |  |  | 53 |      * @param AbstractAdapter $cache
 | 
        
           |  |  | 54 |      * @param LoggerInterface $logger
 | 
        
           |  |  | 55 |      * @param array $config
 | 
        
           |  |  | 56 |      */
 | 
        
           |  |  | 57 |     public function __construct($adapter, $cache , $logger, $config)
 | 
        
           |  |  | 58 |     {
 | 
        
           |  |  | 59 |         $this->adapter      = $adapter;
 | 
        
           |  |  | 60 |         $this->cache        = $cache;
 | 
        
           |  |  | 61 |         $this->logger       = $logger;
 | 
        
           |  |  | 62 |         $this->config       = $config;
 | 
        
           |  |  | 63 |     }
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 |     public function downloadAction()
 | 
        
           |  |  | 66 |     {
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |         // Get the file name from GET variable.
 | 
        
           |  |  | 70 |         $code       = $this->params()->fromRoute('code', '');
 | 
        
           |  |  | 71 |         $fileName   = $this->params()->fromRoute('filename', '');
 | 
        
           |  |  | 72 |         $type       = $this->params()->fromRoute('type', 'user');
 | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 |        // echo 'Code = '. $code . ' Filename = ' . $fileName . ' type = ' . $type; exit;
 | 
        
           |  |  | 75 |   | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         $no_image = $this->config['leaderslinked.images_default.no_image'];
 | 
        
           |  |  | 79 |         $path = '';
 | 
        
           |  |  | 80 |         switch($type)
 | 
        
           |  |  | 81 |         {
 | 
        
           |  |  | 82 |             case 'user' :
 | 
        
           |  |  | 83 |                 $no_image = $this->config['leaderslinked.images_default.user_image'];
 | 
        
           |  |  | 84 |                 $path = $this->config['leaderslinked.fullpath.user'];
 | 
        
           |  |  | 85 |                 break;
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 |             case 'user-profile' :
 | 
        
           |  |  | 89 |                 $no_image = $this->config['leaderslinked.images_default.user_profile'];
 | 
        
           |  |  | 90 |                 $path = $this->config['leaderslinked.fullpath.user'];
 | 
        
           |  |  | 91 |                 break;
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |             case 'user-cover' :
 | 
        
           |  |  | 94 |                 $no_image = $this->config['leaderslinked.images_default.user_cover'];
 | 
        
           |  |  | 95 |                 $path = $this->config['leaderslinked.fullpath.user'];
 | 
        
           |  |  | 96 |                 break;
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |             case 'company' :
 | 
        
           |  |  | 99 |                 $no_image = $this->config['leaderslinked.images_default.company_profile'];
 | 
        
           |  |  | 100 |                 $path = $this->config['leaderslinked.fullpath.company'];
 | 
        
           |  |  | 101 |                 break;
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 |             case 'company-cover' :
 | 
        
           |  |  | 104 |                 $no_image = $this->config['leaderslinked.images_default.company_cover'];
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |                 $path = $this->config['leaderslinked.fullpath.company'];
 | 
        
           |  |  | 108 |                 break;
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |             case 'group' :
 | 
        
           |  |  | 111 |                 $no_image = $this->config['leaderslinked.images_default.group_profile'];
 | 
        
           |  |  | 112 |                 $path = $this->config['leaderslinked.fullpath.group'];
 | 
        
           |  |  | 113 |                 break;
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 |             case 'group-cover' :
 | 
        
           |  |  | 116 |                 $no_image = $this->config['leaderslinked.images_default.group_cover'];
 | 
        
           |  |  | 117 |                 $path = $this->config['leaderslinked.fullpath.group'];
 | 
        
           |  |  | 118 |                 break;
 | 
        
           |  |  | 119 |   | 
        
           |  |  | 120 |             case 'job' :
 | 
        
           |  |  | 121 |                 $path = $this->config['leaderslinked.fullpath.job'];
 | 
        
           |  |  | 122 |                 break;
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |             case 'chat' :
 | 
        
           |  |  | 125 |                 $path = $this->config['leaderslinked.fullpath.chat'];
 | 
        
           |  |  | 126 |                 break;
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 |             case 'feed' :
 | 
        
           |  |  | 129 |                 $path = $this->config['leaderslinked.fullpath.feed'];
 | 
        
           |  |  | 130 |                 break;
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 |             case 'post' :
 | 
        
           |  |  | 133 |                 $path = $this->config['leaderslinked.fullpath.post'];
 | 
        
           |  |  | 134 |                 break;
 | 
        
           |  |  | 135 |   | 
        
           |  |  | 136 |             case 'message' :
 | 
        
           |  |  | 137 |                 $path = $this->config['leaderslinked.fullpath.message'];
 | 
        
           |  |  | 138 |                 break;
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |             case 'microlearning-topic' :
 | 
        
           |  |  | 141 |                 $path = $this->config['leaderslinked.fullpath.microlearning_topic'];
 | 
        
           |  |  | 142 |                 break;
 | 
        
           |  |  | 143 |   | 
        
           |  |  | 144 |             case 'microlearning-capsule' :
 | 
        
           |  |  | 145 |                 $path = $this->config['leaderslinked.fullpath.microlearning_capsule'];
 | 
        
           |  |  | 146 |                 break;
 | 
        
           |  |  | 147 |   | 
        
           |  |  | 148 |             case 'microlearning-slide' :
 | 
        
           |  |  | 149 |                 $path = $this->config['leaderslinked.fullpath.microlearning_slide'];
 | 
        
           |  |  | 150 |                 break;
 | 
        
           | 1590 | eleazar | 151 |   | 
        
           |  |  | 152 |             case 'recruitment_selection' :
 | 
        
           |  |  | 153 |                 $path = $this->config['leaderslinked.fullpath.recruitment_selection'];
 | 
        
           |  |  | 154 |                 break;
 | 
        
           | 1 | www | 155 |   | 
        
           |  |  | 156 |             default :
 | 
        
           |  |  | 157 |                 $path = $this->config['leaderslinked.fullpath.image'];
 | 
        
           |  |  | 158 |                 break;
 | 
        
           |  |  | 159 |   | 
        
           |  |  | 160 |         }
 | 
        
           |  |  | 161 |         if($code && $fileName) {
 | 
        
           |  |  | 162 |             $request_fullpath = $path . $code . DIRECTORY_SEPARATOR . $fileName;
 | 
        
           |  |  | 163 |         } else {
 | 
        
           |  |  | 164 |             $request_fullpath = $no_image;
 | 
        
           |  |  | 165 |         }
 | 
        
           |  |  | 166 |   | 
        
           |  |  | 167 |         if(empty($fileName)) {
 | 
        
           |  |  | 168 |             $extensions     = explode('.',$request_fullpath);
 | 
        
           |  |  | 169 |             $extension      = strtolower(trim($extensions[count($extensions)-1]));
 | 
        
           |  |  | 170 |             if ($extension=='jpg' || $extension=='jpeg' || $extension=='gif' || $extension == 'png') {
 | 
        
           |  |  | 171 |                 $request_fullpath =  $no_image;
 | 
        
           |  |  | 172 |             }
 | 
        
           |  |  | 173 |         }
 | 
        
           |  |  | 174 |   | 
        
           |  |  | 175 |   | 
        
           |  |  | 176 |         //echo $request_fullpath; exit;
 | 
        
           |  |  | 177 |   | 
        
           |  |  | 178 |   | 
        
           |  |  | 179 |   | 
        
           |  |  | 180 |         if(file_exists($request_fullpath)) {
 | 
        
           |  |  | 181 |   | 
        
           |  |  | 182 |             // Try to open file
 | 
        
           |  |  | 183 |             if (!is_readable($request_fullpath)) {
 | 
        
           |  |  | 184 |                 return $this->getResponse()->setStatusCode(500);
 | 
        
           |  |  | 185 |             }
 | 
        
           |  |  | 186 |   | 
        
           |  |  | 187 |             // Get file size in bytes.
 | 
        
           |  |  | 188 |             $fileSize = filesize($request_fullpath);
 | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 |             // Get MIME type of the file.
 | 
        
           |  |  | 191 |             $mimeType = mime_content_type($request_fullpath);
 | 
        
           |  |  | 192 |             if($mimeType===false) {
 | 
        
           |  |  | 193 |                 $mimeType = 'application/octet-stream';
 | 
        
           |  |  | 194 |             }
 | 
        
           |  |  | 195 |   | 
        
           |  |  | 196 |             $fileContent = file_get_contents($request_fullpath);
 | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 |             $response = $this->getResponse();
 | 
        
           |  |  | 199 |             $headers = $response->getHeaders();
 | 
        
           |  |  | 200 |             $headers->addHeaderLine('Content-type: ' . $mimeType);
 | 
        
           |  |  | 201 |             $headers->addHeaderLine('Content-length: ' . $fileSize);
 | 
        
           |  |  | 202 |   | 
        
           |  |  | 203 |             if($fileContent!==false) {
 | 
        
           |  |  | 204 |                 $response->setContent($fileContent);
 | 
        
           |  |  | 205 |             } else {
 | 
        
           |  |  | 206 |                 $this->getResponse()->setStatusCode(500);
 | 
        
           |  |  | 207 |                 return;
 | 
        
           |  |  | 208 |             }
 | 
        
           |  |  | 209 |         } else {
 | 
        
           |  |  | 210 |             return $this->getResponse()->setStatusCode(404);
 | 
        
           |  |  | 211 |         }
 | 
        
           |  |  | 212 |   | 
        
           |  |  | 213 |         return $this->getResponse();
 | 
        
           |  |  | 214 |     }
 | 
        
           |  |  | 215 |   | 
        
           |  |  | 216 | }
 |