1.9.x compatibility
[imagecompressor.git] / action.ajax.php
1 <?php
2 #-------------------------------------------------------------------------
3 #
4 # Jonathan Schmid, <hi@jonathanschmid.de>
5 # Web: www.jonathanschmid.de
6 #
7 #-------------------------------------------------------------------------
8 #
9 # ImageCompressor is a CMS Made Simple module that can compress user-uploaded images.
10 #
11 #-------------------------------------------------------------------------
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 # Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
26 #
27 #-------------------------------------------------------------------------
28
29 if(!isset($params['action'])) {
30         exit;
31 }
32 $config = cmsms()->GetConfig();
33 $return = array();
34 switch($params['action']) {
35         case 'check':
36                 if(!imagecompressor_utils::url_fopen_enabled())
37                 {
38                         $return['error'] = $this->Lang('fopen_error');
39                         break;
40                 }
41                 $data = imagecompressor_utils::smushit_post($config['image_uploads_url'].DIRECTORY_SEPARATOR.$params['path']);
42                 if(!$data)
43                 {
44                         $return['error'] = $this->Lang('N/A');
45                         break;
46                 }
47                 if(isset($data->error) && $data->error !== 'No savings')
48                 {
49                         $return['error'] = $this->Lang('smushit_error').' '. $data->error;
50                         break;
51                 }
52                 if(intval($data->dest_size) == -1 || intval($data->percent) == 0)
53                 {
54                         $return['message'] = $this->Lang('nosavings');
55                         break;
56                 }
57
58                 $return['message'] = sprintf($this->Lang('couldsave'), $data->percent) . '<br />';
59                 if(is_writable($config['image_uploads_path'].DIRECTORY_SEPARATOR.$params['path']))
60                 {
61                         $return['message'] .= '<a href="#" class="smush" data-source="'.$data->dest.'">'.$this->Lang('compress').'</a>';
62                 }
63                 else
64                 {
65                         $return['error'] = $this->Lang('notwritable');
66                 }
67                 break;
68         case 'smush':
69                 $source = urldecode($params['source']);
70                 if(strpos($source, 'http://ysmushit.zenfs.com/results') !== 0)
71                 {
72                         $return['error'] = $this->Lang('invalidurl');
73                         break;
74                 }
75                 $f = fopen($source, 'r');
76                 $f2 = fopen($config['image_uploads_path'].DIRECTORY_SEPARATOR.$params['path'], 'w');
77                 while(!feof($f))
78                 {
79                         fputs($f2, fgets($f));
80                 }
81                 fclose($f);
82                 fclose($f2);
83                 $return['message'] = $this->Lang('done');
84                 break;
85 }
86 header("Cache-Control: no-cache, must-revalidate");
87 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
88 echo json_encode($return);
89 exit; // need this for 1.9.x
90 ?>