/application/third_party/10layer/libraries/Tlpicture.php

https://github.com/10layer/10Layer-CMS · PHP · 128 lines · 83 code · 11 blank · 34 comment · 22 complexity · 903ec8cdcd58f6af057b24b8a2986a39 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * 10Layer Picture class
  4. *
  5. * Deals with finding pictures associated with content
  6. *
  7. * @package 10Layer
  8. * @subpackage Libraries
  9. * @category Libraries
  10. * @author Jason Norwood-Young
  11. * @link http://10layer.com
  12. *
  13. */
  14. class TlPicture {
  15. protected $ci;
  16. protected $_filename=false;
  17. protected $_cachedir="./resources/cache/pictures/";
  18. public function __construct() {
  19. $this->ci=&get_instance();
  20. //$this->ci->load->library("memcacher");
  21. }
  22. public function findPicture($urlid, $contenttype_urlid) {
  23. $this->_filename="";
  24. $content_type=$this->ci->model_content->get_content_type($contenttype_urlid);
  25. $this->ci->load->model($content_type->model, "content");
  26. $pic=$this->ci->content->getByIdORM($urlid, $contenttype_urlid);
  27. if (empty($pic->content_id)) {
  28. show_error("Content $urlid not found");
  29. }
  30. $fields=$pic->getFields();
  31. foreach($fields as $field) { //CDN file - let's download it
  32. if (($field->type=="cdn") && (!empty($field->value))) {
  33. $tmpfile="/resources/cache/pictures/cdn/".basename($field->value);
  34. if (!file_exists($tmpfile)) {
  35. file_put_contents(".".$tmpfile,file_get_contents($field->value));
  36. }
  37. $this->_filename=$tmpfile;
  38. }
  39. }
  40. foreach($fields as $field) {
  41. if ($field->type=="file") {
  42. $this->_filename=$field->value;
  43. }
  44. }
  45. if (empty($this->_filename)) {
  46. //We didn't find the pic. Let's look elsewhere.
  47. foreach($fields as $field) {
  48. if ($field->type=="rich") {
  49. if (!empty($field->data->fields["filename"]->value)) {
  50. $this->_filename=$field->data->fields["filename"]->value;
  51. break;
  52. }
  53. }
  54. }
  55. }
  56. if (empty($this->_filename)) {
  57. //Still not found. Try a bit deeper.
  58. $this->_filename=$this->breadthSearch($fields);
  59. }
  60. //Let's just double-check that this is in fact an image
  61. if (!empty($this->_filename)) {
  62. $parts=pathinfo($this->_filename);
  63. $ext=$parts["extension"];
  64. if (($ext=="png") || ($ext=="gif") || ($ext=="jpg")) {
  65. //Continue
  66. } else {
  67. $this->ci->load->helper('file');
  68. $mime=str_replace("/","-",get_mime_by_extension($this->_filename)).".png";
  69. if (file_exists(APPPATH."third_party/10layer/resources/images/mimetypes/{$mime}")) {
  70. $this->_filename=APPPATH."third_party/10layer/resources/file/images/mimetypes/{$mime}";
  71. } else {
  72. $this->_filename=false;
  73. }
  74. }
  75. }
  76. return $this->_filename;
  77. }
  78. public function hasPic($urlid, $contenttype_urlid) {
  79. // $picture=$this->ci->memcacher->picFilename($contenttype_urlid,$urlid);
  80. // if (!empty($picture) && file_exists(".".$picture)) {
  81. // return true;
  82. // }
  83. return false;
  84. }
  85. public function filename($urlid, $contenttype_urlid) {
  86. // $picture=$this->ci->memcacher->picFilename($contenttype_urlid,$urlid);
  87. // if (empty($picture) || !file_exists(".".$picture)) {
  88. // return false;
  89. // }
  90. // return $picture;
  91. return false;
  92. }
  93. public function clearCache($urlid, $contenttype_urlid) {
  94. // exec("rm ".$this->_cachedir.$urlid."*");
  95. // $this->ci->memcacher->clearPic($contenttype_urlid, $urlid);
  96. }
  97. /*protected function _cachefilename() {
  98. $segs=$this->uri->segment_array();
  99. $params=array_slice($segs,3);
  100. $cachefilename=$this->_cachedir.implode("_",$params).".jpg";
  101. return $cachefilename;
  102. }*/
  103. protected function breadthSearch($fields) {
  104. foreach($fields as $field) {
  105. if (is_object($field)) {
  106. if (isset($field->type) && ($field->type=="rich")) {
  107. if (!empty($field->data->fields["filename"]->value)) {
  108. return $field->data->fields["filename"]->value;
  109. }
  110. }
  111. }
  112. if (isset($field->data->fields)) {
  113. return $this->breadthSearch($field->data->fields);
  114. }
  115. }
  116. }
  117. }