got everything working
Foaly* [Thu, 19 Sep 2013 00:47:37 +0000 (01:47 +0100)]
ImageCompressor.module.php
action.ajax.php
inc/ImageCompressor.js
inc/ajax.js [deleted file]
inc/imagecompressor.css
inc/worker.js [deleted file]
lib/class.imagecompressor_utils.php

index 9db23a2..5de7001 100644 (file)
@@ -101,25 +101,5 @@ class ImageCompressor extends CMSModule {
        {
                return $this->Lang('really_uninstall');
        }
-       protected function smush($url, $size)
-       {
-               $config = cmsms()->GetConfig();
-               $url = urldecode($url);
-               if(strpos($url, 'http://ysmushit.zenfs.com/results') !== 0)
-                       return $this->Lang('invalidurl');
-
-               $f = fopen($url, 'r');
-               $savepath = $config['image_uploads_path'].preg_replace('#^http://ysmushit.+?/images#', '', $url);
-               $f2 = fopen($savepath, 'w');
-               while(!feof($f))
-               {
-                       fputs($f2, fgets($f));
-               }
-
-               fclose($f);
-               fclose($f2);
-
-               return $this->Lang('done');
-       }
 }
 ?>
\ No newline at end of file
index ea0bb6f..1296ed5 100644 (file)
@@ -38,13 +38,13 @@ switch($params['action']) {
                        $return['error'] = $this->Lang('fopen_error');
                        break;
                }
-               $data = imagecompressor_utils::smushit_post($config['image_uploads_url'].'/'.$params['path']);
+               $data = imagecompressor_utils::smushit_post($config['image_uploads_url'].DIRECTORY_SEPARATOR.$params['path']);
                if(!$data)
                {
                        $return['error'] = $this->Lang('N/A');
                        break;
                }
-               if(isset($data->error))
+               if(isset($data->error) && $data->error !== 'No savings')
                {
                        $return['error'] = $this->Lang('smushit_error').' '. $data->error;
                        break;
@@ -55,16 +55,33 @@ switch($params['action']) {
                        break;
                }
 
-               $return['message'] = sprintf($this->Lang('couldsave'), $data->percent);
-               if(is_writable($config['image_uploads_path'].'/'.$path))
+               $return['message'] = sprintf($this->Lang('couldsave'), $data->percent) . '<br />';
+               if(is_writable($config['image_uploads_path'].DIRECTORY_SEPARATOR.$params['path']))
                {
-                       $return['message'] .= '<br /><a href="#" class="smush" data-dest="'.$data->dest.'">'.$this->Lang('compress').'</a>';
+                       $return['message'] .= '<a href="#" class="smush" data-source="'.$data->dest.'">'.$this->Lang('compress').'</a>';
                }
                else
                {
                        $return['error'] = $this->Lang('notwritable');
                }
                break;
+       case 'smush':
+               $source = urldecode($params['source']);
+               if(strpos($source, 'http://ysmushit.zenfs.com/results') !== 0)
+               {
+                       $return['error'] = $this->Lang('invalidurl');
+                       break;
+               }
+               $f = fopen($source, 'r');
+               $f2 = fopen($config['image_uploads_path'].DIRECTORY_SEPARATOR.$params['path'], 'w');
+               while(!feof($f))
+               {
+                       fputs($f2, fgets($f));
+               }
+               fclose($f);
+               fclose($f2);
+               $return['message'] = $this->Lang('done');
+               break;
 }
 header("Cache-Control: no-cache, must-revalidate");
 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
index ea399d6..6fc8eee 100644 (file)
@@ -17,21 +17,14 @@ var ImageCompressor = (function ($) {
                        });
                        return mod_params;
                },
-               checkRow = function (row) {
-                       var $row = $(row),
-                               $td = $row.find('td:last-child'),
-                               identifier = Object.keys(queue).length, // shouldn't mix jQuery and underscore
+               ajax = function (mod_params, $td) {
+                       var old_content = $td.html(),
+                               identifier = Object.keys(queue).length,
                                cms_params = {
                                        'module': 'ImageCompressor',
                                        'action': 'ajax',
                                        'showtemplate': false
-                               },
-                               mod_params = {
-                                       'action': 'check',
-                                       'path': $row.find('td:eq(1)').text()
-                               },
-                               old_content = $td.html();
-
+                               };
                        $td.html('<div class="spinner" />');
                        queue[identifier] = $.ajax({
                                url: 'moduleinterface.php' + cms_user_key,
@@ -57,6 +50,27 @@ var ImageCompressor = (function ($) {
                                }
                        });
                },
+               checkRow = function (row) {
+                       var $row = $(row),
+                               $td = $row.find('td:last-child'),
+                               mod_params = {
+                                       'action': 'check',
+                                       'path': $row.find('td:eq(1)').text()
+                               };
+
+                       ajax(mod_params, $td);
+               },
+               smushRow = function (row) {
+                       var $row = $(row),
+                               $td = $row.find('td:last-child'),
+                               mod_params = {
+                                       'action': 'smush',
+                                       'source': $td.find('.smush').data('source'),
+                                       'path': $row.find('td:eq(1)').text()
+                               };
+
+                       ajax(mod_params, $td);
+               },
                abortQueue = function () {
                        $.each(queue, function () {
                                this.abort();
@@ -79,7 +93,7 @@ var ImageCompressor = (function ($) {
                                                $table.find('tbody .check').click();
                                                break;
                                        case 1:
-                                               console.log('smush all');
+                                               $table.find('tbody .smush').click();
                                                break;
                                        case 2:
                                                abortQueue();
@@ -90,6 +104,10 @@ var ImageCompressor = (function ($) {
                                e.preventDefault();
                                checkRow($(this).parent().parent());
                        });
+                       $table.find('tbody .smush').live('click', function (e) {
+                               e.preventDefault();
+                               smushRow($(this).parent().parent().parent());
+                       });
                }
        };
 }(jQuery));
\ No newline at end of file
diff --git a/inc/ajax.js b/inc/ajax.js
deleted file mode 100644 (file)
index 73f7b3d..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-var workerCount = 0;\r
-var queue = new Array();\r
-var queueLength = 0;\r
-var oldState = new Array();\r
-var supportsWebWorkers = !!window.Worker;\r
-\r
-function ajax_function(usr_function, params, container){\r
-       if(workerCount < 8) {\r
-               set_queue(usr_function, params, container);\r
-               if(supportsWebWorkers)\r
-                       webworker();\r
-               else\r
-                       ajaxworker();\r
-       } else {\r
-               set_queue(usr_function, params, container);\r
-       }\r
-       return true;\r
-}\r
-function get_compression_rate(path, container){\r
-       ajax_function('get_compression_rate', path, container);\r
-}\r
-function smush(params, container){\r
-       ajax_function('smush', params, container);\r
-}\r
-function showLoadInfo(container){\r
-       container.html('<span class="checking">'+LANG_CHECKING+'</span>');\r
-}\r
-\r
-function mass_compress_check() {\r
-       $('.massclick').each(function(index){\r
-               $(this).click();\r
-       });\r
-}\r
-\r
-function mass_smush() {\r
-       $('.massclick_smush').each(function(index){\r
-               $(this).click();\r
-       });\r
-}\r
-\r
-function stop_queue() {\r
-       queue = new Array();\r
-       $('.queue').each(function(index) {\r
-               $(this).parent().html(oldState[$(this).parent().parent().find('.file').html()]);\r
-       });\r
-}\r
-\r
-function set_queue(usr_function, params, container) {\r
-       queueLength = queue.length;\r
-       queue[queueLength] = new Array();\r
-       queue[queueLength]['func'] = usr_function;\r
-       queue[queueLength]['para'] = params;\r
-       queue[queueLength]['elem'] = container;\r
-       oldState[container.parent().find(".file").html()] = container.html();\r
-       if(usr_function == 'get_compression_rate') {\r
-               container.html('<span class="queue">'+LANG_INQUEUE_CHECK+'</span>');\r
-       } else if(usr_function == 'smush') {\r
-               container.html('<span class="queue">'+LANG_INQUEUE_SMUSH+'</span>');\r
-       }\r
-}\r
-\r
-function webworker() {\r
-       queueLength = queue.length;\r
-       if(queueLength != 0) {\r
-               var activequeuemember = queue.shift();\r
-               var worker = new Worker(CMS_INC_DIR+'worker.js');\r
-               showLoadInfo(activequeuemember['elem']);\r
-               // Triggered by postMessage in the Web Worker\r
-               mact: 'ImageCompressor,m1_,ajax,0',\r
-               worker.postMessage(encodeURI(CMS_ADMIN_DIR+"/moduleinterface.php"+CMS_USER_KEY+"&popup=1&m1_usr_function="+activequeuemember['func']+"&amp;m1_params="+activequeuemember['para']+"&showtemplate=false&mact=ImageCompressor,m1_,ajax,0"));\r
-               worker.onmessage = function (evt) {\r
-                       worker.terminate();\r
-                       activequeuemember['elem'].html(evt.data);\r
-                       workerCount = workerCount - 1;\r
-                       webworker();\r
-               }\r
-               worker.onerror = function (evt) {\r
-                       worker.terminate();\r
-                       activequeuemember['elem'].html(evt.data);\r
-                       workerCount = workerCount - 1;\r
-                       webworker();\r
-               }\r
-               workerCount = workerCount + 1;\r
-       }\r
-}\r
-function ajaxworker(){\r
-       queueLength = queue.length;\r
-       if(queueLength != 0) {\r
-               var activequeuemember = queue.shift();\r
-               showLoadInfo(activequeuemember['elem']);\r
-               $.post(CMS_ADMIN_DIR+"/moduleinterface.php"+CMS_USER_KEY, { \r
-                       mact: 'ImageCompressor,m1_,ajax,0',\r
-                       m1_usr_function: activequeuemember['func'],\r
-                       showtemplate: false,\r
-                       m1_params: activequeuemember['para']\r
-               },\r
-               function(data){\r
-                       activequeuemember['elem'].html(data);\r
-                       workerCount = workerCount - 1;\r
-                       ajaxworker();\r
-               });\r
-               workerCount = workerCount + 1;\r
-       }\r
-}
\ No newline at end of file
index 572080d..7a5bb38 100644 (file)
@@ -4,22 +4,6 @@
 #filelist_imagecompressor a:hover{
        text-decoration:underline;
 }
-#filelist_imagecompressor .notwritable{
-       color:#f00;
-}
-#filelist_imagecompressor .massclick_smush{
-       font-weight:bold;
-}
-#filelist_imagecompressor .queue,
-#filelist_imagecompressor .nosavings{
-       color:#999;
-}
-#filelist_imagecompressor .checking{
-       color:#0000ff;
-}
-
-/* NEW SHIT */
-
 #filelist_imagecompressor thead th:last-child {
        width: 220px;
 }
diff --git a/inc/worker.js b/inc/worker.js
deleted file mode 100644 (file)
index 37ae196..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-onmessage = function (evt) {
-       var serviceUrl = decodeURI(evt.data);
-       serviceUrl = serviceUrl.replace("&amp;", "&");
-       var req = new XMLHttpRequest();
-       if(req){
-               req.open('GET', serviceUrl, false);
-               req.setRequestHeader('X-PINGOTHER', 'pingpong');
-               req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
-               req.onreadystatechange = function() {
-                       if (req.readyState==4 && req.status==200) {
-                               postMessage(req.responseText);
-                       } else if(req.readyState==4 && req.status != 200) {
-                               postMessage('Ajax Error');
-                       }
-               };
-               req.send("action=init");
-       }
-       worker.terminate();
-}
\ No newline at end of file
index c422506..6ade1af 100644 (file)
@@ -104,7 +104,7 @@ class imagecompressor_utils {
 
        static final public function url_fopen_enabled()
        {
-               if (!ini_get('allow_url_fopen') || $_SERVER['GATEWAY_INTERFACE'])
+               if (!ini_get('allow_url_fopen'))
                {
                        return false;
                }