1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8namespace Tiki\Files; 9 10use TikiLib; 11 12class CheckAttachmentGallery extends AbstractCheckGallery 13{ 14 /* 15 * Holds current attachment type 16 * t - Tracker Attachments 17 * w - Wiki Attachments 18 * f - Forum Attachments 19 */ 20 private $type; 21 22 /** 23 * @param string $type The attachment type to check 24 */ 25 public function __construct($type) 26 { 27 $this->type = $type; 28 } 29 30 public function analyse() 31 { 32 $usesDatabase = $this->areFilesStoredInDatabase(); 33 $attachmentsPath = $this->getPathOnDisk(); 34 35 $filesInDbCount = 0; 36 $filesInDiskCount = 0; 37 38 $filesToCheckOnDisk = []; 39 40 $attachments = $this->getAttachments(); 41 $filesCountTotal = count($attachments['data']); 42 43 foreach ($attachments['data'] as $attachment) { 44 if ($attachment['path']) { 45 $filesInDiskCount++; 46 $filesToCheckOnDisk[] = [ 47 'id' => $attachment['attId'], 48 'name' => $attachment['path'], 49 'path' => $attachmentsPath, 50 'size' => $attachment['filesize'], 51 ]; 52 } else { 53 $filesInDbCount++; 54 } 55 } 56 57 $filesOnDisk = $this->listFilesInDirectory($attachmentsPath); 58 59 list($missing, $mismatch, $unknown) = $this->matchFileList($filesToCheckOnDisk, $filesOnDisk, []); 60 61 return [ 62 'usesDatabase' => $usesDatabase, 63 'path' => [$attachmentsPath], 64 'mixedLocation' => ($filesInDbCount !== 0 && $filesInDiskCount !== 0) ? true : false, 65 'count' => $filesCountTotal, 66 'countFilesDb' => $filesInDbCount, 67 'countFilesDisk' => $filesInDiskCount, 68 'issueCount' => count($missing) + count($mismatch) + count($unknown), 69 'missing' => $missing, 70 'mismatch' => $mismatch, 71 'unknown' => $unknown, 72 ]; 73 } 74 75 /** 76 * Gets attachments based on their type. "list_all_attachements" is common to all of these libs 77 * @return array 78 * @throws \Exception 79 */ 80 private function getAttachments() 81 { 82 switch ($this->type) { 83 case 't': 84 $lib = TikiLib::lib('trk'); 85 break; 86 case 'w': 87 $lib = TikiLib::lib('wiki'); 88 break; 89 case 'f': 90 $lib = TikiLib::lib('comments'); 91 break; 92 } 93 94 return $lib->list_all_attachements(); 95 } 96 97 /** 98 * Checks if the configuration is to store files in DB or disk 99 * 100 * @return bool 101 */ 102 protected function areFilesStoredInDatabase() 103 { 104 global $prefs; 105 return isset($prefs[$this->type . '_use_db']) ? $prefs[$this->type . '_use_db'] === 'y' : false; 106 } 107 108 /** 109 * Returns where to store files on disk 110 * 111 * @return string 112 */ 113 protected function getPathOnDisk() 114 { 115 global $prefs; 116 return isset($prefs[$this->type . '_use_dir']) ? $prefs[$this->type . '_use_dir'] : false; 117 } 118} 119