Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.97 KB | None | 0 0
  1. <?php
  2.  
  3. class Request extends MX_Controller
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->load->model('core_model');
  9. }
  10.  
  11. public function exec()
  12. {
  13. $oinput = $this->input;
  14. $srequest_type = $_SERVER['REQUEST_METHOD'];
  15. $stoken = $oinput->get_post('form-token');
  16. $atoken = $this->core_model->get_token($stoken);
  17.  
  18. if(!$atoken && $this->session->userdata('form-token')){
  19. $this->core_model->insert_token($stoken);
  20. $this->session->unset_userdata(array('submit-count' =>''));
  21. if($srequest_type=="POST"){
  22. if($oinput->post('m') && $oinput->post('c') && $oinput->post('cm')){
  23. if($this->load->module($oinput->post('m') . '/' . $oinput->post('c'))){
  24. $scontroller = $oinput->post('c');
  25. $smethod = $oinput->post('cm');
  26. $this->$scontroller->$smethod();
  27. }
  28. }else{
  29. show_404();
  30. }
  31. }elseif($srequest_type=="GET"){
  32. if($oinput->get('m') && $oinput->get('c') && $oinput->get('cm')){
  33. if($this->load->module($oinput->get('m') . '/' . $oinput->get('c'))){
  34. $scontroller = $oinput->get('c');
  35. $smethod = $oinput->get('cm');
  36. $this->$scontroller->$smethod();
  37. }
  38. }else{
  39. show_404();
  40. }
  41. }
  42. }else{
  43. show_404();
  44. }
  45. }
  46.  
  47. public function ajax()
  48. {
  49. $oinput = $this->input;
  50. $srequest_type = $_SERVER['REQUEST_METHOD'];
  51. if($srequest_type=="POST"){
  52. if($oinput->post('module') && $oinput->post('controller') && $oinput->post('method')){
  53. if($this->load->module($oinput->post('module') . '/' . $oinput->post('controller'))){
  54. $scontroller = $oinput->post('controller');
  55. $smethod = $oinput->post('method');
  56. $this->$scontroller->$smethod();
  57. }
  58. }else{
  59. show_404();
  60. }
  61. }elseif($srequest_type=="GET"){
  62. if($oinput->get('module') && $oinput->get('controller') && $oinput->get('method')){
  63. if($this->load->module($oinput->get('module') . '/' . $oinput->get('controller'))){
  64. $scontroller = $oinput->get('controller');
  65. $smethod = $oinput->get('method');
  66. $this->$scontroller->$smethod();
  67. }
  68. }else{
  69. show_404();
  70. }
  71. }
  72. }
  73.  
  74. public function assets()
  75. {
  76. $smodule_name = $this->uri->rsegment(3);
  77. $smodule_path = APPPATH . 'modules/' . $smodule_name;
  78. $bcache = ($this->input->get('cache') && $this->input->get('cache')==='true') ? true : false;
  79.  
  80. if($smodule_name){
  81. $asegment = $this->uri->rsegment_array();
  82. array_splice($asegment,0,3);
  83. if($asegment){
  84. $snext_path = "";
  85. foreach($asegment as $key=>$val){
  86. $snext_path .= (($key==0) ? "" : "/" ) . $val;
  87. }
  88. $srequest_file = $smodule_path . '/assets/'. $snext_path;
  89. if(file_exists($smodule_path . '/assets/'. $snext_path)){
  90. $apath_info = pathinfo($srequest_file);
  91. if(!isset($apath_info['extension'])){
  92.  
  93. show_404();
  94. }
  95. $sfolder = "";
  96.  
  97. if($apath_info['extension']==="css" || $apath_info['extension']=="js" || $apath_info['extension']=="txt"){
  98. $atexttype_file = array("js"=>"js","css" => "css","txt" =>"txt");
  99.  
  100. $sfolder = $atexttype_file[$apath_info['extension']];
  101. $stextheader_type = ($apath_info['extension']==='js') ? "text/javascript" : "text/css";
  102. header("Content-type: {$stextheader_type}", true);
  103. header("Cache-Control: private, max-age=10800, pre-check=10800");
  104. header("Pragma: private");
  105. header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));
  106. $search = array('/\}[^\S ]+/s','/[^\S ]+\{/s','/(\s)+/s');
  107. $replace = array('}','{','\\1');
  108.  
  109. if($bcache === true){
  110.  
  111. $scache_path = $smodule_path . '/cache/' . $sfolder . '/';
  112. $sfile_path = $smodule_path . '/assets/'. $snext_path;
  113. $sfile_name = $apath_info['filename'] . ".{$apath_info['extension']}";
  114. $sfile_modified = filemtime($sfile_path);
  115. $sencrypt = md5($sfile_modified) . '-' . md5($sfile_name) .".{$apath_info['extension']}";
  116. if(is_dir($scache_path)){
  117. $scached_file = $scache_path . $sencrypt;
  118.  
  119. if(!file_exists($scached_file)){
  120.  
  121. $afile = glob ( $scache_path . '*-' . md5($sfile_name). ".{$apath_info['extension']}" );
  122.  
  123. if($afile){
  124. foreach($afile as $rows){
  125. unlink($rows);
  126. }
  127. }
  128.  
  129. ob_start();
  130. require_once($srequest_file);
  131. $soutput = ob_get_clean();
  132. $soutput = str_replace("ASSETS",$this->environment->asset_path,$soutput);
  133. $soutput = preg_replace($search, $replace, $soutput);
  134.  
  135. $screate_cache = fopen($scached_file ,'w');
  136. fwrite($screate_cache,$soutput);
  137. fclose($screate_cache);
  138. echo $soutput;
  139. }else{
  140. require_once($scache_path . $sencrypt);
  141. }
  142. }else{
  143.  
  144. }
  145. }else{
  146. require_once($srequest_file);
  147. }
  148.  
  149. }else if($apath_info['extension']==="jpg" || $apath_info['extension']=="gif" || $apath_info['extension']=="png"){
  150.  
  151. define('MEMORY_TO_ALLOCATE', '100M');
  152. define('DEFAULT_QUALITY', 80);
  153. define('DOCUMENT_ROOT',$_SERVER['DOCUMENT_ROOT']);
  154. define('IMAGE_DIR', $srequest_file);
  155. define('CACHE_DIR_NAME',$smodule_path . '/cache/images/');
  156. define('CACHE_DIR', CACHE_DIR_NAME);
  157. define('MAX_IMAGE_WIDTH', 2000);
  158. define('MAX_IMAGE_HEIGHT', 1000);
  159. $aSize = GetImageSize(IMAGE_DIR);
  160.  
  161. $sMime = $aSize['mime'];
  162.  
  163. if (substr($sMime, 0, 6) != 'image/'){
  164. header('HTTP/1.1 400 Bad Request');
  165. echo 'Error: requested file is not an accepted type: ' . $sImage;
  166. exit();
  167. }
  168.  
  169. $iWidth = $aSize[0];
  170. $iHeight = $aSize[1];
  171. $iMaxWidth = (isset($_GET['w'])) ? (int) $_GET['w'] : 0;
  172. $iMaxHeight = (isset($_GET['h'])) ? (int) $_GET['h'] : 0;
  173. $sCropRation = (isset($_GET['cr'])) ? (string) $_GET['cr'] : null;
  174. $sColor = (isset($_GET['color'])) ? preg_replace('/[^0-9a-fA-F]/', '', (string) $_GET['color']) : FALSE;
  175. $iQuality = (isset($_GET['quality'])) ? (int) $_GET['quality'] : DEFAULT_QUALITY;
  176.  
  177. if (isset($_GET['type'])){
  178. switch ($_GET['type']){
  179. case "pt" :
  180. $iMaxWidth = 50;
  181. $iMaxHeight = 50;
  182. $sCropRation = "4:4";
  183. break;
  184. case "ct" :
  185. $iMaxWidth = 40;
  186. $iMaxHeight = 40;
  187. $sCropRation = "4:4";
  188. break;
  189. case "sc" :
  190. $iMaxWidth = 160;
  191. $iMaxHeight = 0;
  192. break;
  193. case "pp" :
  194. $iMaxWidth = 190;
  195. $iMaxHeight = 400;
  196. break;
  197. case "ba" :
  198. $iMaxWidth = 125;
  199. $iMaxHeight = 94;
  200. $sCropRation = "4:3";
  201. break;
  202. case "max" :
  203. $iMaxWidth = MAX_IMAGE_WIDTH;
  204. $iMaxHeight = MAX_IMAGE_HEIGHT;
  205. break;
  206. }
  207. }
  208.  
  209. if (!$iMaxWidth && $iMaxHeight) $iMaxWidth = 99999999999999;
  210. else if ($iMaxWidth && !$iMaxHeight) $iMaxHeight = 99999999999999;
  211. else if ($sColor && !$iMaxWidth && !$iMaxHeight){
  212. $iMaxWidth = $iWidth;
  213. $iMaxHeight = $iHeight;
  214. }
  215.  
  216. if ((!$iMaxWidth && !$iMaxHeight) || (!$sColor && $iMaxWidth >= $iWidth && $iMaxHeight >= $iHeight)){
  217. $sData = file_get_contents($srequest_file);
  218. $dLastModifiedString = gmdate('D, d M Y H:i:s', filemtime($srequest_file)) . ' GMT';
  219. $sEncryptedTag = md5($sData);
  220.  
  221. $this->doConditionalGet($sEncryptedTag, $dLastModifiedString);
  222.  
  223. header("Content-type: " . $sMime);
  224. header("Content-Length: " . strlen($sData));
  225.  
  226. echo $sData;
  227. exit();
  228. }
  229.  
  230. $iOffsetX = 0;
  231. $iOffsetY = 0;
  232.  
  233. if ($sCropRation != null){
  234. $aCropRatio = explode(':', (string) $sCropRation);
  235. if (count($aCropRatio) == 2){
  236. $iRatioComputed = $iWidth / $iHeight;
  237. $aCropRatioComputed = (float) $aCropRatio[0] / (float) $aCropRatio[1];
  238.  
  239. if ($iRatioComputed < $aCropRatioComputed){
  240. $iOriginalHeight = $iHeight;
  241. $iHeight = $iWidth / $aCropRatioComputed;
  242. $iOffsetY = ($iOriginalHeight - $iHeight) / 2;
  243. }
  244. else if ($iRatioComputed > $aCropRatioComputed){
  245. $iOriginalWidth = $iWidth;
  246. $iWidth = $iHeight * $aCropRatioComputed;
  247. $iOffsetX = ($iOriginalWidth - $iWidth) / 2;
  248. }
  249. }
  250. }
  251.  
  252. $xRatio = $iMaxWidth / $iWidth;
  253. $yRatio = $iMaxHeight / $iHeight;
  254.  
  255. if ($xRatio * $iHeight < $iMaxHeight){
  256. $sTargetHeight = ceil($xRatio * $iHeight);
  257. $sTargetWidth = $iMaxWidth;
  258. }
  259. else {
  260. $sTargetWidth = ceil($yRatio * $iWidth);
  261. $sTargetHeight = $iMaxHeight;
  262. }
  263.  
  264. if ($sTargetWidth <= 500 && $sTargetHeight <= 500) $iQuality = 100;
  265.  
  266. $sResizedImageSource = $sTargetWidth . 'x' . $sTargetHeight . 'x' . $iQuality;
  267.  
  268. if ($sColor) $sResizedImageSource .= 'x' . $sColor;
  269. if (isset($_GET['cropratio'])) $sResizedImageSource .= 'x' . (string) $_GET['cropratio'];
  270.  
  271. $sResizedImageSource .= '-' . $srequest_file;
  272. $sResizedImage = md5($sResizedImageSource);
  273. $sResized = CACHE_DIR . $sResizedImage;
  274.  
  275. if (!isset($_GET['nocache']) && file_exists($sResized)){
  276. $sImageModified = filemtime($srequest_file);
  277. $sThumbModified = filemtime($sResized);
  278.  
  279. if($sImageModified < $sThumbModified){
  280. $sData = file_get_contents($sResized);
  281.  
  282. $dLastModifiedString = gmdate('D, d M Y H:i:s', $sThumbModified) . ' GMT';
  283. $sEncryptedTag = md5($sData);
  284.  
  285. $this->doConditionalGet($sEncryptedTag, $dLastModifiedString);
  286.  
  287. header("Content-type: " . $sMime);
  288. header("Content-Length: " . strlen($sData));
  289. echo $sData;
  290. exit();
  291. }
  292. }
  293.  
  294. ini_set('memory_limit', MEMORY_TO_ALLOCATE);
  295.  
  296. $oNewImage = imagecreatetruecolor($sTargetWidth, $sTargetHeight);
  297.  
  298. switch ($aSize['mime']){
  299. case 'image/gif':
  300. $sCreationFunction = 'ImageCreateFromGif';
  301. $sOutputFunction = 'ImagePng';
  302. $sMime = 'image/png';
  303. $bDoSharpen = FALSE;
  304. $iQuality = round(10 - ($iQuality / 10));
  305. break;
  306.  
  307. case 'image/x-png':
  308. case 'image/png':
  309. $sCreationFunction = 'ImageCreateFromPng';
  310. $sOutputFunction = 'ImagePng';
  311. $bDoSharpen = FALSE;
  312. $iQuality = round(10 - ($iQuality / 10));
  313. break;
  314.  
  315. default:
  316. $sCreationFunction = 'ImageCreateFromJpeg';
  317. $sOutputFunction = 'ImageJpeg';
  318. $bDoSharpen = TRUE;
  319. break;
  320. }
  321.  
  322. $sLocation = $sCreationFunction($srequest_file);
  323.  
  324. if (in_array($aSize['mime'], array('image/gif', 'image/png'))){
  325. if (!$sColor){
  326. imagealphablending($oNewImage, false);
  327. imagesavealpha($oNewImage, true);
  328. }
  329. else {
  330. if ($sColor[0] == '#')
  331. $sColor = substr($sColor, 1);
  332.  
  333. $bBackground = FALSE;
  334.  
  335. if (strlen($sColor) == 6) $bBackground = imagecolorallocate($oNewImage, hexdec($sColor[0].$sColor[1]), hexdec($sColor[2].$sColor[3]), hexdec($sColor[4].$sColor[5]));
  336. else if (strlen($sColor) == 3) $bBackground = imagecolorallocate($oNewImage, hexdec($sColor[0].$sColor[0]), hexdec($sColor[1].$sColor[1]), hexdec($sColor[2].$sColor[2]));
  337. if ($bBackground) imagefill($oNewImage, 0, 0, $bBackground);
  338. }
  339. }
  340.  
  341. ImageCopyResampled($oNewImage, $sLocation, 0, 0, $iOffsetX, $iOffsetY, $sTargetWidth, $sTargetHeight, $iWidth, $iHeight);
  342.  
  343. if ($bDoSharpen){
  344. $sharpness = $this->findSharp($iWidth, $sTargetWidth);
  345.  
  346. $sharpenMatrix = array(
  347. array(-1, -2, -1),
  348. array(-2, $sharpness + 12, -2),
  349. array(-1, -2, -1)
  350. );
  351.  
  352. $divisor = $sharpness;
  353. $offset= 0;
  354.  
  355. imageconvolution($oNewImage, $sharpenMatrix, $divisor, $offset);
  356. }
  357.  
  358. if (!file_exists(CACHE_DIR)) mkdir(CACHE_DIR, 0755);
  359.  
  360. if (!is_readable(CACHE_DIR)){
  361. header('HTTP/1.1 500 Internal Server Error');
  362. echo 'Error: the cache directory is not readable';
  363. exit();
  364. }
  365. else if (!is_writable(CACHE_DIR)){
  366. header('HTTP/1.1 500 Internal Server Error');
  367. echo 'Error: the cache directory is not writable';
  368. exit();
  369. }
  370.  
  371. $sOutputFunction($oNewImage, $sResized, $iQuality);
  372.  
  373. ob_start();
  374. $sOutputFunction($oNewImage, null, $iQuality);
  375. $sData = ob_get_contents();
  376. ob_end_clean();
  377.  
  378. ImageDestroy($sLocation);
  379. ImageDestroy($oNewImage);
  380.  
  381. $dLastModifiedString = gmdate('D, d M Y H:i:s', filemtime($sResized)) . ' GMT';
  382. $sEncryptedTag = md5($sData);
  383.  
  384. $this->doConditionalGet($sEncryptedTag, $dLastModifiedString);
  385.  
  386. header("Content-type: " . $sMime,true);
  387. header("Content-Length: " . strlen($sData));
  388. header("Cache-Control: private, max-age=10800, pre-check=10800");
  389. header("Pragma: private");
  390. header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));
  391. echo $sData;
  392. }else{
  393. require_once($srequest_file);
  394. }
  395.  
  396. }else{
  397. show_404();
  398. }
  399. }else{
  400. show_404();
  401. }
  402. }else{
  403. show_404();
  404. }
  405. }
  406.  
  407. private function findSharp($iOriginal, $iFinal)
  408. {
  409. $iFinal = $iFinal * (750.0 / $iOriginal);
  410. $a = 52;
  411. $b = -0.27810650887573124;
  412. $c = .00047337278106508946;
  413.  
  414. $iResult = $a + $b * $iFinal + $c * $iFinal * $iFinal;
  415.  
  416. return max(round($iResult), 0);
  417. }
  418.  
  419. private function doConditionalGet($sEncryptedTag, $lastModified)
  420. {
  421. header("Last-Modified: " . $lastModified);
  422. header("ETag: \"" . $sEncryptedTag . "\"");
  423.  
  424. $bMatch = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false;
  425.  
  426. $bModified = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
  427.  
  428. if (!$bModified && !$bMatch) return;
  429. if ($bMatch && $bMatch != $sEncryptedTag && $bMatch != '"' . $sEncryptedTag . '"') return;
  430. if ($bModified && $bModified != $lastModified) return;
  431.  
  432. header("HTTP/1.1 304 Not Modified");
  433. exit();
  434. }
  435.  
  436. public function uploads()
  437. {
  438.  
  439. }
  440.  
  441. public function download()
  442. {
  443.  
  444. }
  445. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement