PageRenderTime 25ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/kodaks_controller.php

https://github.com/bartcc/app.todos
PHP | 177 lines | 150 code | 23 blank | 4 comment | 22 complexity | b0a8e772412fcab4c06f9aae797a98ca MD5 | raw file
  1. <?php
  2. class KodaksController extends AppController {
  3. var $name = 'Kodaks';
  4. var $uses = array();
  5. var $disableSessions = true;
  6. function beforeFilter() {
  7. define('USE_X_SEND', false);
  8. $this->disableCache();
  9. $this->Auth->allowedActions = array('develop');
  10. parent::beforeFilter();
  11. }
  12. function beforeRender() {
  13. parent::beforeRender();
  14. }
  15. private function returnExt($file) {
  16. $pos = strrpos($file, '.');
  17. return strtolower(substr($file, $pos + 1, strlen($file)));
  18. }
  19. private function n($var, $default = false) {
  20. $var = trim($var);
  21. if (is_numeric($var)) {
  22. return $var;
  23. } else if ($default) {
  24. return $default;
  25. } else {
  26. exit;
  27. }
  28. }
  29. function index() {
  30. $this->autoRender = false;
  31. $this->layout = false;
  32. }
  33. function develop() {
  34. $this->autoRender = false;
  35. $this->layout = false;
  36. $val = $this->params['named']['a'];
  37. // $this->log("Kodaks::develop", LOG_DEBUG);
  38. if (strpos($val, 'http://') !== false || substr($val, 0, 1) == '/') {
  39. header('Location: ' . $val);
  40. exit;
  41. } else {
  42. $val = str_replace(' ', '.2B', $val);
  43. }
  44. App::import('Component', 'Salt');
  45. $salt = new SaltComponent();
  46. $val = str_replace(' ', '.2B', $val);
  47. $crypt = $salt->convert($val, false);
  48. $a = explode(',', $crypt);
  49. // $this->log($a, LOG_DEBUG);
  50. $file = $fn = basename($a[2]);
  51. // Make sure supplied filename contains only approved chars
  52. if (preg_match("/[^A-Za-z0-9._-]/", $file)) {
  53. header('HTTP/1.1 403 Forbidden');
  54. exit;
  55. }
  56. $uid = $a[0];
  57. $id = $a[1];
  58. $w = $this->n($a[3]);
  59. $h = $this->n($a[4]);
  60. $sq = $this->n($a[5]);
  61. $q = $this->n($a[6], 100);
  62. $sh = $this->n($a[7], 0);
  63. $x = $this->n($a[8], 50);
  64. $y = $this->n($a[9], 50);
  65. $force = $this->n($a[10], 0);
  66. if ($sq != 1) {
  67. list($w, $h) = computeSize(PHOTOS . DS . $uid . DS . $id . DS . 'lg' . DS . $fn, $w, $h, $sq);
  68. $w = $this->n($w);
  69. $h = $this->n($h);
  70. }
  71. $ext = $this->returnExt($file);
  72. define('PATH', PHOTOS . DS . $uid. DS . $id);
  73. $original = PATH . DS . 'lg' . DS . $file;
  74. $base_dir = PATH . DS . 'cache';
  75. if ($sq == 2) {
  76. $base_dir = PATH . DS . 'lg';
  77. $path_to_cache = $original;
  78. } else {
  79. $fn .= "_{$w}_{$h}_{$sq}_{$q}_{$sh}_{$x}_{$y}";
  80. $fn .= ".$ext";
  81. $base_dir = PATH . DS . 'cache';
  82. $path_to_cache = $base_dir . DS . $fn;
  83. }
  84. // Make sure dirname of the cached copy is sane
  85. if (dirname($path_to_cache) !== $base_dir) {
  86. header('HTTP/1.1 403 Forbidden');
  87. exit;
  88. }
  89. $noob = false;
  90. if (!file_exists($path_to_cache)) {
  91. $noob = true;
  92. if ($sq == 2) {
  93. copy($original, $path_to_cache);
  94. } else {
  95. if (!defined('MAGICK_PATH')) {
  96. define('MAGICK_PATH_FINAL', 'convert');
  97. } else if (strpos(strtolower(MAGICK_PATH), 'c:\\') !== false) {
  98. define('MAGICK_PATH_FINAL', '"' . MAGICK_PATH . '"');
  99. } else {
  100. define('MAGICK_PATH_FINAL', MAGICK_PATH);
  101. }
  102. if (!defined('FORCE_GD')) {
  103. define('FORCE_GD', 0);
  104. }
  105. if (!is_dir(dirname($path_to_cache))) {
  106. $parent_perms = substr(sprintf('%o', fileperms(dirname(dirname($path_to_cache)))), -4);
  107. $old = umask(0);
  108. mkdir(dirname($path_to_cache), octdec($parent_perms));
  109. umask($old);
  110. }
  111. App::import('Component', 'Darkroom');
  112. $d = new DarkroomComponent();
  113. $d->develop($original, $path_to_cache, $w, $h, $q, $sh, $sq, $x, $y, $force);
  114. }
  115. }
  116. $specs = getimagesize($path_to_cache);
  117. $mtime = filemtime($path_to_cache);
  118. $etag = md5($path_to_cache . $mtime);
  119. if (!$noob) {
  120. if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag)) {
  121. header("HTTP/1.1 304 Not Modified");
  122. exit;
  123. }
  124. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($path_to_cache))) {
  125. header("HTTP/1.1 304 Not Modified");
  126. exit;
  127. }
  128. }
  129. $disabled_functions = explode(',', ini_get('disable_functions'));
  130. if (USE_X_SEND) {
  131. header("X-Sendfile: $path_to_cache");
  132. } else {
  133. $specs = getimagesize($path_to_cache);
  134. header('Content-type: ' . $specs['mime']);
  135. header('Content-length: ' . filesize($path_to_cache));
  136. header('Cache-Control: public');
  137. header('Expires: ' . gmdate('D, d M Y H:i:s', strtotime('+1 year')));
  138. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path_to_cache)));
  139. header('ETag: ' . $etag);
  140. if (is_callable('readfile') && !in_array('readfile', $disabled_functions)) {
  141. readfile($path_to_cache);
  142. } else {
  143. die(file_get_contents($path_to_cache));
  144. }
  145. }
  146. }
  147. }
  148. ?>