PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/concreteOLD/blocks/library_file/controller.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 342 lines | 186 code | 46 blank | 110 comment | 30 complexity | 49d5365a896ea56f68e7c87a1190a37d MD5 | raw file
  1. <?php
  2. /**
  3. * @package Blocks
  4. * @category Concrete
  5. * @author Andrew Embler <andrew@concrete5.org>
  6. * @copyright Copyright (c) 2003-2008 Concrete5. <http://www.concrete5.org>
  7. * @license http://www.concrete5.org/license/ MIT License
  8. *
  9. */
  10. /**
  11. * The library file block is an internal block type used by external block types to reference files. Basically, any file in Concrete maps to an instance of the library file block.
  12. *
  13. * @package Blocks
  14. * @access private
  15. * @author Andrew Embler <andrew@concrete5.org>
  16. * @category Concrete
  17. * @copyright Copyright (c) 2003-2008 Concrete5. <http://www.concrete5.org>
  18. * @license http://www.concrete5.org/license/ MIT License
  19. *
  20. */
  21. defined('C5_EXECUTE') or die("Access Denied.");
  22. class LibraryFileBlockController extends BlockController {
  23. protected $btIsInternal = 1;
  24. protected $btTable = 'btFile';
  25. /**
  26. * Used for localization. If we want to localize the name/description we have to include this
  27. */
  28. public function getBlockTypeDescription() {
  29. return t("Files added to the asset library");
  30. }
  31. public function getBlockTypeName() {
  32. return t("Library File");
  33. }
  34. function getFile($fID) {
  35. Loader::model('file');
  36. $mf = new File();
  37. $file_obj = $mf->getByID($fID);
  38. $fileversion_obj = $file_obj->getVersion();
  39. $bf = new LibraryFileBlockController;
  40. $ftype = FileTypeList::getType($fileversion_obj->getExtension());
  41. $this->generictype = strtolower($ftype->getGenericTypeText($ftype->getGenericType()));
  42. $this->filename = $fileversion_obj->getFileName();
  43. $this->type = $fileversion_obj->getType();
  44. $this->url = $fileversion_obj->getURL();
  45. $this->filepath = $fileversion_obj->getPath();
  46. $this->relpath = $fileversion_obj->getRelativePath();
  47. $this->origfilename = $fileversion_obj->getRelativePath();
  48. $this->filesize = $fileversion_obj->getFullSize();
  49. $len = strlen(REL_DIR_FILES_UPLOADED);
  50. $fh = Loader::helper('concrete/file');
  51. $bf->bID = $fileversion_obj->getFileID();
  52. $bf->generictype = $this->generictype;
  53. $bf->type = $this->type;
  54. $bf->url = $this->url;
  55. $bf->filepath = $this->filepath;
  56. $bf->relpath = $this->relpath;
  57. $bf->filename = substr($this->relpath, $len); // for backwards compatibility this must include the prefixes
  58. $bf->filesize = $this->filesize;
  59. return $bf;
  60. }
  61. function getIcon($type, $generictype, $getSRC = true) {
  62. $file = '';
  63. if (file_exists(DIR_AL_ICONS . '/type_' . $type . '.png')) {
  64. $file = REL_DIR_AL_ICONS . '/type_' . $type . '.png';
  65. } else if (file_exists(DIR_AL_ICONS . '/generic_' . $generictype . '.png')) {
  66. $file = REL_DIR_AL_ICONS . '/generic_' . $generictype . '.png';
  67. } else {
  68. $file = REL_DIR_AL_ICONS . '/generic_file.png';
  69. }
  70. if (!$getSRC) {
  71. return $file;
  72. } else {
  73. return '<img class="ccm-al-icon" src="' . $file . '" width="' . AL_ICON_WIDTH . '" height="' . AL_ICON_HEIGHT . '" />';
  74. }
  75. }
  76. function sanitizeTitle($filename, $maxLength = 60) {
  77. // remove all numbers from the front of a file
  78. $st = preg_replace("/^[0-9]+/", "", $filename);
  79. if (strlen($st) > $maxLength) {
  80. $st = substr($st, 0, $maxLength) . '...';
  81. }
  82. return $st;
  83. }
  84. /**
  85. * Gets the original filename of an uploaded file.
  86. * @author Tony Trupp <tony@concrete5.org>
  87. * return string $filename
  88. */
  89. function getOrigfilename() {return $this->origfilename;}
  90. function getOriginalFilename() {return $this->origfilename;}
  91. function getFilename() {return $this->filename;}
  92. function getFileID() {return $this->bID;}
  93. function getURL() {return $this->url;}
  94. function getType() {return $this->type;}
  95. function getGenericType() {return $this->generictype;}
  96. public function getFilePath() {
  97. return $this->filepath;
  98. }
  99. public function getFileRelativePath() {
  100. return $this->relpath;
  101. }
  102. public function getFileFullURL() {
  103. return BASE_URL . $this->url;
  104. }
  105. /*
  106. * Returns the dimensions of the file object (assumed to be an image, movie or flash file) as an array
  107. * Array keys: 0 = width, 1 = height
  108. * @return array $dimensions
  109. */
  110. function getDimensions() {
  111. $r = @getimagesize($this->filepath);
  112. if ($r) {
  113. return $r;
  114. }
  115. }
  116. /**
  117. * Goes through all files without an original filename and makes one by using the sanitizeTitle() function
  118. * @return void
  119. */
  120. function populateOriginalFilenames() {
  121. $db = Loader::db();
  122. $r = $db->Execute("select bID, filename from btFile where origfilename = '' or origfilename is null");
  123. while ($row = $r->FetchRow()) {
  124. $origfilename = $this->sanitizeTitle($row['filename']);
  125. $db->Execute("update btFile set origfilename = ? where bID = ?", array($origfilename, $row['bID']));
  126. }
  127. }
  128. /**
  129. * Creates a new image given an original path, a new path, a target width and height.
  130. * @params string $originalPath, string $newpath, int $width, int $height
  131. * @return void
  132. */
  133. public function createImage($originalPath, $newPath, $width, $height) {
  134. $fi = Loader::helper('image');
  135. $fi->create($originalPath, $newPath, $width, $height);
  136. }
  137. /**
  138. * Gets the system thumbnail for a given file object. This function returns a relative path
  139. * @return string $path
  140. */
  141. function getThumbnailRelativePath($filename = null) {
  142. if (!$filename) {
  143. $db = Loader::db();
  144. $q = "select filename from btFile where bID = '{$this->bID}'";
  145. $filename = $db->getOne($q);
  146. if ($filename) {
  147. $newFileName = substr($filename, 0, strrpos($filename, '.')) . '.jpg';
  148. return REL_DIR_FILES_UPLOADED_THUMBNAILS . '/' . $newFileName;
  149. }
  150. } else {
  151. $newFileName = substr($filename, 0, strrpos($filename, '.')) . '.jpg';
  152. return REL_DIR_FILES_UPLOADED_THUMBNAILS . '/' . $newFileName;
  153. }
  154. }
  155. /**
  156. * Gets the system thumbnail for a given file object. This function returns the absolute path
  157. * @return string $path
  158. */
  159. function getThumbnailAbsolutePath($filename = null) {
  160. }
  161. /**
  162. * Returns a thumbnail for the current file object. If a width and height are specified, attempts to create or retrieve a thumbnail of that size specifically
  163. * Returns an object with the following properties: src, width, height, alt
  164. * @param int $maxWidth
  165. * @param int $maxHeight
  166. * @return object $thumbnail
  167. */
  168. public function getThumbnail($maxWidth = null, $maxHeight = null) {
  169. $thumb = new stdClass;
  170. if ($maxWidth == null && $maxHeight == null) {
  171. $src = $this->getThumbnailRelativePath();
  172. $abspath = $this->getThumbnailAbsolutePath();
  173. } else if ($this->filename != '' && is_int($maxWidth) && is_int($maxHeight)) {
  174. // then we check to see if a file with these dimensions exists
  175. $pi = pathinfo($this->filename);
  176. $filename = $pi['filename'] . '_' . $maxWidth . 'x' . $maxHeight . '.jpg';
  177. if (!file_exists(DIR_FILES_CACHE . '/' . $filename)) {
  178. LibraryFileBlockController::createImage(DIR_FILES_UPLOADED . '/' . $this->filename, DIR_FILES_CACHE . '/' . $filename, $maxWidth, $maxHeight);
  179. }
  180. $src = REL_DIR_FILES_CACHE . '/' . $filename;
  181. $abspath = DIR_FILES_CACHE . '/' . $filename;
  182. }
  183. if (isset($abspath) && file_exists($abspath)) {
  184. $thumb->src = $src;
  185. $dimensions = getimagesize($abspath);
  186. $thumb->width = $dimensions[0];
  187. $thumb->height = $dimensions[1];
  188. $thumb->alt = $this->getOriginalFilename();
  189. return $thumb;
  190. }
  191. }
  192. /**
  193. * Prints out an image string for the current thumbnail, using the getThumbnail method
  194. */
  195. public function outputThumbnail($maxWidth = null, $maxHeight = null) {
  196. $thumb = $this->getThumbnail($maxWidth, $maxHeight);
  197. if (is_object($thumb)) {
  198. print '<img class="ccm-output-thumbnail" alt="' . htmlentities($thumb->alt, ENT_QUOTES, APP_CHARSET) . '" src="' . $thumb->src . '" width="' . $thumb->width . '" height="' . $thumb->height . '" />';
  199. }
  200. }
  201. function getFileSize() {
  202. return $this->filesize;
  203. }
  204. function delete() {
  205. $db = Loader::db();
  206. $bID = $this->bID;
  207. // first, we remove the image
  208. $q = "select filename from btFile where bID = '$bID'";
  209. $filename = $db->getOne($q);
  210. if ($filename) {
  211. if (file_exists(DIR_FILES_UPLOADED . '/' . $filename)) {
  212. unlink(DIR_FILES_UPLOADED . '/' . $filename);
  213. }
  214. if (file_exists(DIR_FILES_UPLOADED_THUMBNAILS . '/' . $filename)) {
  215. unlink(DIR_FILES_UPLOADED_THUMBNAILS . '/' . $filename);
  216. }
  217. }
  218. // now, finally, we remove the instance from the btContentFile table
  219. $q = "delete from btFile where bID = '$bID'";
  220. $r = $db->query($q);
  221. }
  222. function sanitizeAndCopy($pointer, $filename) {
  223. $prefix = rand(1000, 10000) . time();
  224. $filename = preg_replace(array("/[^0-9A-Za-z-.]/","/[\s]/"),"", $filename);
  225. $filename = $prefix . $filename;
  226. $path = DIR_FILES_UPLOADED . '/' . $filename;
  227. copy($pointer, $path);
  228. return $filename;
  229. }
  230. function createThumbnail($existingFile) {
  231. if (file_exists(DIR_FILES_UPLOADED . '/' . $existingFile)) {
  232. $thumbnailFileName = substr($existingFile, 0, strrpos($existingFile, '.')) . '.jpg';
  233. $thumbnailFilePath = DIR_FILES_UPLOADED_THUMBNAILS . '/' . $thumbnailFileName;
  234. LibraryFileBlockController::createImage(DIR_FILES_UPLOADED . '/' . $existingFile, $thumbnailFilePath, AL_THUMBNAIL_WIDTH, AL_THUMBNAIL_HEIGHT);
  235. }
  236. }
  237. function duplicate($newBID) {
  238. // nothing
  239. }
  240. function save($data) {
  241. Loader::library("file/importer");
  242. if (file_exists($data['file'])) {
  243. $fi = new FileImporter();
  244. $resp = $fi->import($data['file'], $data['name']);
  245. $lbc = new LibraryFileBlockController();
  246. return $lbc->getFile($resp->getFileID());
  247. }
  248. }
  249. /*
  250. function save_OLD($data) {
  251. if (file_exists($data['file'])) {
  252. // copy the file into the files directory
  253. $filename = LibraryFileBlockController::sanitizeAndCopy($data['file'], $data['name']);
  254. $bo = $this->getBlockObject();
  255. $bo->updateBlockName($filename);
  256. $size = @getimagesize(DIR_FILES_UPLOADED . '/' . $filename);
  257. // TODO: extend these out to more useful file types
  258. if ($size) {
  259. $generictype = "image";
  260. $type = '';
  261. LibraryFileBlockController::createThumbnail($filename);
  262. switch($size[2]) {
  263. case IMAGETYPE_PNG:
  264. $type = 'png';
  265. break;
  266. case IMAGETYPE_JPEG:
  267. $type = 'jpg';
  268. break;
  269. case IMAGETYPE_GIF:
  270. $type = 'gif';
  271. break;
  272. }
  273. } else {
  274. $generictype = "file";
  275. $type = '';
  276. $ext = substr(strrchr($filename, '.'), 1);
  277. $type = $ext;
  278. }
  279. $db = Loader::db();
  280. $origfilename= LibraryFileBlockController::sanitizeTitle($filename, 12);
  281. $v = array($filename, $origfilename, $type, $generictype, $bo->getBlockID());
  282. $r = $db->query("insert into btFile (filename,origfilename, type, generictype, bID) values (?, ?, ?, ?, ?)", $v);
  283. $bf = LibraryFileBlockController::getFile($bo->getBlockID());
  284. $ret = Events::fire('on_file_upload', $bf);
  285. }
  286. }
  287. */
  288. }
  289. ?>