rewriting the whole javascript
Foaly* [Wed, 18 Sep 2013 01:01:00 +0000 (02:01 +0100)]
14 files changed:
ImageCompressor.module.php
action.ajax.php
action.defaultadmin.php
function.admin_imagelist_tab.php
images/filetypes/bmp.png [new file with mode: 0755]
images/filetypes/gif.png [new file with mode: 0755]
images/filetypes/jpg.png [new file with mode: 0755]
images/filetypes/png.png [new file with mode: 0755]
inc/ImageCompressor.js [new file with mode: 0644]
inc/imagecompressor.css
inc/worker.js
lang/en_US.php
lib/class.imagecompressor_utils.php [new file with mode: 0644]
templates/imagelist_tab.tpl

index 107890f..f7bd8f6 100644 (file)
 <?php
-/**
- * ImageCompressor class
- *
- * @author Jonathan Schmid
- * @since 0.10
- * @version $Revision: 12 $
- * @modifiedby $LastChangedBy: foaly* $
- * @lastmodified $Date: 2011-02-01 11:56:16 +0100 (Tue, 01 Feb 2011) $
- * @license GPL
- * 
- * TODO: better error handling
- * TODO: break client cache for webworkers
- * TODO: appearance in ie 7-8
- * TODO: option whether gifs shall be converted or not
- *
- */
+#-------------------------------------------------------------------------
+#
+# Jonathan Schmid, <hi@jonathanschmid.de>
+# Web: www.jonathanschmid.de
+#
+#-------------------------------------------------------------------------
+#
+# ImageCompressir is a CMS Made Simple module that can compress user-uploaded images.
+#
+#-------------------------------------------------------------------------
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
+#
+#-------------------------------------------------------------------------
 
-class ImageCompressor extends CMSModule
-{
-       const SMUSHIT_API_URL = 'http://www.smushit.com/ysmush.it/ws.php?img=%s';
-       const SMUSHIT_UA = 'CMSms Image Compressor/0.9 (http://dev.cmsmadesimple.org/projects/imagecompressor)';
+class ImageCompressor extends CMSModule {
 
-       public function __construct(){
+       public function __construct()
+       {
                parent::__construct();
 
                if (!function_exists('json_decode'))
+               {
                        require_once('JSON/JSON.php');
+               }
        }
-       public function GetName(){
+       public function GetName()
+       {
                return 'ImageCompressor';
        }
-       public function GetFriendlyName(){
+       public function GetFriendlyName()
+       {
                return $this->Lang('friendlyname');
        }
-       public function GetVersion(){
-               return '0.10';
+       public function GetVersion()
+       {
+               return '0.10.1';
        }
-       public function GetHelp(){
+       public function GetHelp()
+       {
                return $this->Lang('help');
        }
-       public function GetAuthor(){
+       public function GetAuthor()
+       {
                return 'JS';
        }
-       public function GetAuthorEmail(){
+       public function GetAuthorEmail()
+       {
                return 'hi@jonathanschmid.de';
        }
-       public function GetChangeLog(){
+       public function GetChangeLog()
+       {
                return $this->Lang('changelog');
        }
-       public function IsPluginModule(){
+       public function IsPluginModule()
+       {
                return false;
        }
-       public function HasAdmin(){
+       public function HasAdmin()
+       {
                return true;
        }
-       public function IsAdminOnly(){
+       public function IsAdminOnly()
+       {
                return true;
        }
-       public function LazyLoadAdmin(){
+       public function LazyLoadAdmin()
+       {
                return true;
        }
-       public function GetAdminDescription(){
+       public function GetAdminDescription()
+       {
                return $this->Lang('moddescription');
        }
-       public function VisibleToAdminUser(){
+       public function VisibleToAdminUser()
+       {
                return $this->CheckPermission('Use Image Compressor');
        }
-       public function GetDependencies(){
-               return array();
-       }
-       public function MinimumCMSVersion(){
+       public function MinimumCMSVersion()
+       {
                return "1.10";
        }
-       public function InstallPostMessage(){
+       public function InstallPostMessage()
+       {
                return $this->Lang('postinstall');
        }
-       public function UninstallPostMessage(){
+       public function UninstallPostMessage()
+       {
                return $this->Lang('postuninstall');
        }
-       public function UninstallPreMessage(){
+       public function UninstallPreMessage()
+       {
                return $this->Lang('really_uninstall');
        }
-       protected function format_bytes($size) {
-               $units = array(' B', ' KB', ' MB', ' GB', ' TB');
-               for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
-               return round($size, 1).$units[$i];
-       }
-       protected function smushit_post($file_url){
-               $uri = sprintf(self::SMUSHIT_API_URL, urlencode($file_url));
-               $f = fopen($uri, 'r');
-               $response = fgets($f);
-               fclose($f);
-
-               if($response == 'null')
-                       return false;
-
-               if(function_exists('json_decode'))
-                       $response = json_decode($response);
-               else{
-                       $json = new Services_JSON();
-                       $response = $json->decode($response);
-               }
-
-               return $response;
-       }
-       protected function get_compression_rate($path){
-               $config = cmsms()->GetConfig();
-               $data = $this->smushit_post($config['image_uploads_url'].'/'.$path);
-               if(!$data)
-                       return $this->Lang('N/A');
-
-               if(intval($data->dest_size) == -1 || intval($data->percent) == 0)
-                       return sprintf('<span class="nosavings">%s</span>', $this->Lang('nosavings'));
-
-               $return = sprintf($this->Lang('couldsave'), $data->percent).'<br />';
-               if(is_writable($config['image_uploads_path'].'/'.$path))
-                       $return .= sprintf('<a href="#" class="massclick_smush" onclick="smush(\'%s,%s\', $(this).parent());return false;">%s</a>', urlencode(urldecode($data->dest)), $data->dest_size, $this->Lang('compress'));
-               else
-                       $return .= sprintf('<span class="notwritable">%s</span>', $this->Lang('notwritable'));
-               return $return;
-       }
-       protected function is_image($path){
-               $info = @exif_imagetype($path);
-               if($info & (IMAGETYPE_GIF | IMAGETYPE_JPEG | IMAGETYPE_PNG))
-                       return true;
-               else
-                       return false;
-       }
-       protected function smush($url, $size){
+       protected function smush($url, $size)
+       {
                $config = cmsms()->GetConfig();
                $url = urldecode($url);
                if(strpos($url, 'http://ysmushit.zenfs.com/results') !== 0)
@@ -137,18 +116,14 @@ class ImageCompressor extends CMSModule
                $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');
        }
-       protected function sort_files_by_date($a, $b){
-               if($a->filetime == $b->filetime)
-                       return 0;
-       
-               return $a->filetime > $b->filetime ? -1 : 1;
-       }
 }
-?>
+?>
\ No newline at end of file
index 72d5b3a..19882ad 100644 (file)
@@ -1,9 +1,70 @@
 <?php
-if(isset($params['usr_function'])){
-       $usr_params = isset($params['params']) ? explode(',', $params['params']) : '';
-       header("Cache-Control: no-cache, must-revalidate");
-       header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
-       echo call_user_func_array(array($this, $params['usr_function']), $usr_params);
+#-------------------------------------------------------------------------
+#
+# Jonathan Schmid, <hi@jonathanschmid.de>
+# Web: www.jonathanschmid.de
+#
+#-------------------------------------------------------------------------
+#
+# ImageCompressir is a CMS Made Simple module that can compress user-uploaded images.
+#
+#-------------------------------------------------------------------------
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
+#
+#-------------------------------------------------------------------------
+
+if(!isset($params['action'])) {
        exit;
 }
+$config = cmsms()->GetConfig();
+$mod = cmsms()->GetModuleInstance(get_called_class());
+$mod->LoadLangMethods();
+
+$return = array();
+switch($params['action']) {
+       case 'check':
+               $data = imagecompressor_utils::smushit_post($config['image_uploads_url'].'/'.$params['path']);
+               if(!$data)
+               {
+                       $return['error'] = $mod->Lang('N/A');
+                       break;
+               }
+               if(isset($data->error))
+               {
+                       $return['error'] = $mod->Lang('smushit_error').' '. $data->error;
+                       break;
+               }
+               if(intval($data->dest_size) == -1 || intval($data->percent) == 0)
+               {
+                       $return['message'] = $mod->Lang('nosavings');
+                       break;
+               }
+
+               $return['message'] = sprintf($mod->Lang('couldsave'), $data->percent);
+               if(is_writable($config['image_uploads_path'].'/'.$path))
+               {
+                       $return['message'] .= '<br /><a href="#" class="smush" data-dest="'.$data->dest.'">'.$mod->Lang('compress').'</a>';
+               }
+               else
+               {
+                       $return['error'] = $mod->Lang('notwritable');
+               }
+               break;
+}
+header("Cache-Control: no-cache, must-revalidate");
+header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
+echo json_encode($return);
 ?>
\ No newline at end of file
index 4e329b4..d5c122c 100644 (file)
@@ -1,21 +1,44 @@
 <?php
 #-------------------------------------------------------------------------
-# Module: ImageCompressor
-# Version: 0.9, JS
-# Method: Defaultadmin
+#
+# Jonathan Schmid, <hi@jonathanschmid.de>
+# Web: www.jonathanschmid.de
+#
+#-------------------------------------------------------------------------
+#
+# ImageCompressir is a CMS Made Simple module that can compress user-uploaded images.
+#
 #-------------------------------------------------------------------------
-# CMS - CMS Made Simple is (c) 2008 by Ted Kulp (wishy@cmsmadesimple.org)
-# This project's homepage is: http://www.cmsmadesimple.org
-# The module's homepage is: http://dev.cmsmadesimple.org/projects/imagecompressor/
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
 #
 #-------------------------------------------------------------------------
+
 if (!$this->CheckPermission('Use Image Compressor'))
+{
        return $this->DisplayErrorPage($id, $params, $returnid, $this->Lang('accessdenied'));
+}
 
 if(!empty($params['active_tab']))
+{
        $tab = $params['active_tab'];
+}
 else
+{
        $tab = '';
+}
 
 $tab_header = $this->StartTabHeaders() .
 $this->SetTabHeader('imagelist', $this->Lang('title_imagelist'),('imagelist' == $tab)?true:false);
index 21a90d2..bbd8b1b 100644 (file)
@@ -1,18 +1,49 @@
 <?php
+#-------------------------------------------------------------------------
+#
+# Jonathan Schmid, <hi@jonathanschmid.de>
+# Web: www.jonathanschmid.de
+#
+#-------------------------------------------------------------------------
+#
+# ImageCompressir is a CMS Made Simple module that can compress user-uploaded images.
+#
+#-------------------------------------------------------------------------
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
+#
+#-------------------------------------------------------------------------
+
 $config = cmsms()->GetConfig();
 $smarty = cmsms()->GetSmarty();
 $admintheme = cmsms()->get_variable('admintheme');
 
 $files = get_recursive_file_list($config['image_uploads_path'], array('^thumb_.+$'), -1, 'FILES');
-foreach($files as $value){
-       if(!$this->is_image($value)) continue;
+foreach($files as $value)
+{
+       if(!imagecompressor_utils::is_image($value))
+       {
+               continue;
+       }
        $path = str_replace($config['image_uploads_path'].'/', '', $value);
        $fileperms = substr(decoct(fileperms($value)),3);
        $entry = new StdClass;
-       $entry->icon = $admintheme->DisplayImage('icons/filetypes/fpaint.gif');
+       $entry->icon = imagecompressor_utils::GetURLPath().'/images/filetypes/'.substr($path, -3).'.png';
        $entry->filename = $path;
        $entry->fileperms = is_writable($value) ? $fileperms : sprintf('<span style="color:#f00">%s</span>', $fileperms);
-       $entry->filesize = $this->format_bytes(filesize($value));
+       $entry->filesize = imagecompressor_utils::format_bytes(filesize($value));
        $entry->filetime = filemtime($value);
        $entry->filedate = date($this->Lang('filedateformat'), $entry->filetime);
        $entry->url = $config['image_uploads_url'].'/'.$path;
@@ -20,27 +51,25 @@ foreach($files as $value){
        $filelist[] = $entry;
 }
 if(count($filelist) == 0)
+{
        echo $this->Lang('nofiles');
-else{  
-       usort($filelist, array($this, 'sort_files_by_date'));
-       
+}
+else
+{      
+       usort($filelist, array(imagecompressor_utils, 'sort_files_by_date'));
+
        $smarty->assign('files', array_values($filelist));
-       $smarty->assign('filenametext', $this->Lang('filenametext'));
-       $smarty->assign('filepermstext', $this->Lang('filepermstext'));
-       $smarty->assign('filesizetext', $this->Lang('filesizetext'));
-       $smarty->assign('filedatetext', $this->Lang('filedatetext'));
+       $smarty->assign('mod', $this);
        $smarty->assign('checkalltext', $this->Lang('checkall'));
        $smarty->assign('smushalltext', $this->Lang('smushall'));
        $smarty->assign('stopqueuetext', $this->Lang('stopqueue'));
-       $smarty->assign('smushittext', $this->Lang('smushittext'));
-       $smarty->assign('checksavings', $this->Lang('checksavings'));
        $smarty->assign('checkingtext', $this->Lang('checking'));
        $smarty->assign('inqueue_smushtext', $this->Lang('inqueue_smush'));
        $smarty->assign('inqueue_checktext', $this->Lang('inqueue_check'));
-       
-       $smarty->assign('admindir', $config['root_url'].'/'.$config['admin_dir']);
+
        $smarty->assign('incdir', $config['root_url'].'/modules/ImageCompressor/inc/');
        $smarty->assign('userkey', get_secure_param());
-       
+       $smarty->assign('actionid', $id);
+
        echo $this->ProcessTemplate('imagelist_tab.tpl');
 }
\ No newline at end of file
diff --git a/images/filetypes/bmp.png b/images/filetypes/bmp.png
new file mode 100755 (executable)
index 0000000..22841a5
Binary files /dev/null and b/images/filetypes/bmp.png differ
diff --git a/images/filetypes/gif.png b/images/filetypes/gif.png
new file mode 100755 (executable)
index 0000000..c2f375d
Binary files /dev/null and b/images/filetypes/gif.png differ
diff --git a/images/filetypes/jpg.png b/images/filetypes/jpg.png
new file mode 100755 (executable)
index 0000000..981821e
Binary files /dev/null and b/images/filetypes/jpg.png differ
diff --git a/images/filetypes/png.png b/images/filetypes/png.png
new file mode 100755 (executable)
index 0000000..be65795
Binary files /dev/null and b/images/filetypes/png.png differ
diff --git a/inc/ImageCompressor.js b/inc/ImageCompressor.js
new file mode 100644 (file)
index 0000000..0a994a4
--- /dev/null
@@ -0,0 +1,95 @@
+Object.keys = Object.keys || function(obj) {
+       if (obj !== Object(obj)) throw new TypeError('Object.keys called on non-object');
+       var keys = [];
+       for (var key in obj) if (_.has(obj, key)) keys.push(key);
+       return keys;
+};
+var ImageCompressor = (function ($) {
+       "use strict";
+       var cms_action_id = '',
+               cms_user_key = '',
+               $table = null,
+               queue = {},
+               moduleParams = function (params) {
+                       var mod_params = {};
+                       $.each(params, function (index, val) {
+                               mod_params[cms_action_id + index] = val;
+                       });
+                       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
+                               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,
+                               data: $.extend({}, moduleParams(mod_params), cms_params),
+                               dataType: 'json',
+                               success: function (data) {
+                                       var $html = $();
+                                       if(typeof data.message !== 'undefined')
+                                       {
+                                               $html.add('<span class="message">' + data.message + '</span>');
+                                       }
+                                       if(typeof data.error !== 'undefined')
+                                       {
+                                               $html.add('<span class="error">' + data.error + '</span>');
+                                       }
+                                       $td.html($html);
+                               },
+                               complete: function (response, status) {
+                                       delete queue[identifier];
+                                       if(status != 'success') {
+                                               $td.html(old_content);
+                                       }
+                               }
+                       });
+               },
+               abortQueue = function () {
+                       $.each(queue, function () {
+                               this.abort();
+                       });
+               };
+
+       return {
+               init: function (action_id, user_key) {
+                       var $massprocessing = null;
+
+                       cms_action_id = action_id;
+                       cms_user_key = user_key;
+                       $table = $('#filelist_imagecompressor');
+                       $massprocessing = $table.find('thead th:last-child a');
+
+                       $massprocessing.click(function (e) {
+                               e.preventDefault();
+                               switch($massprocessing.index(this)) {
+                                       case 0:
+                                               $table.find('tbody .check').click();
+                                               break;
+                                       case 1:
+                                               console.log('smush all');
+                                               break;
+                                       case 2:
+                                               abortQueue();
+                                               break;
+                               }
+                       });
+                       $table.find('tbody .check').live('click', function (e) {
+                               e.preventDefault();
+                               checkRow($(this).parent().parent());
+                       });
+               }
+       };
+}(jQuery));
\ No newline at end of file
index 691a20a..e3a0242 100644 (file)
 }\r
 #filelist_imagecompressor .checking{\r
        color:#0000ff;\r
+}\r
+\r
+/* NEW SHIT */\r
+\r
+#filelist_imagecompressor thead th:last-child {\r
+       width: 220px;\r
+}\r
+\r
+#filelist_imagecompressor .spinner {\r
+       height: 22px;\r
+       width: 22px;\r
+       position: relative;\r
+       -webkit-animation: rotation .6s infinite linear;\r
+       -moz-animation: rotation .6s infinite linear;\r
+       -o-animation: rotation .6s infinite linear;\r
+       animation: rotation .6s infinite linear;\r
+       border-left: 6px solid rgba(0, 174, 239, .15);\r
+       border-right: 6px solid rgba(0, 174, 239, .15);\r
+       border-bottom: 6px solid rgba(0, 174, 239, .15);\r
+       border-top: 6px solid rgba(0, 174, 239, .8);\r
+       border-radius: 100%;\r
+}\r
+\r
+@-webkit-keyframes rotation {\r
+       from { -webkit-transform: rotate(0deg); }\r
+       to { -webkit-transform: rotate(359deg); }\r
+}\r
+@-moz-keyframes rotation {\r
+       from { -moz-transform: rotate(0deg); }\r
+       to { -moz-transform: rotate(359deg); }\r
+}\r
+@-o-keyframes rotation {\r
+       from { -o-transform: rotate(0deg); }\r
+       to { -o-transform: rotate(359deg); }\r
+}\r
+@keyframes rotation {\r
+       from { transform: rotate(0deg); }\r
+       to { transform: rotate(359deg); }\r
 }
\ No newline at end of file
index a080a82..37ae196 100644 (file)
@@ -1,4 +1,3 @@
-// Triggered by postMessage in the page
 onmessage = function (evt) {
        var serviceUrl = decodeURI(evt.data);
        serviceUrl = serviceUrl.replace("&amp;", "&");
index 47a6db4..5d77d77 100644 (file)
@@ -10,6 +10,7 @@ $lang['error'] = 'Error!';
 $lang['moddescription'] = 'Reduces file size of all images uploaded to the Image Manager to improve website performance using the smush.it-API.';\r
 $lang['changelog'] = '';\r
 \r
+$lang['smushit_error'] = 'Smushit error:';\r
 $lang['title_imagelist'] = 'File view';\r
 $lang['filenametext'] = 'Filename';\r
 $lang['filepermstext'] = 'Access perms';\r
@@ -18,7 +19,6 @@ $lang['filedatetext'] = 'Date';
 $lang['checkall'] = 'check all';\r
 $lang['smushall'] = 'smush all';\r
 $lang['stopqueue'] = 'stop queue';\r
-$lang['smushittext'] = 'Smush.it';\r
 $lang['filedateformat'] = "m/d/Y H:m:s";\r
 $lang['nosavings'] = 'No savings';\r
 $lang['checksavings'] = 'Check savings';\r
diff --git a/lib/class.imagecompressor_utils.php b/lib/class.imagecompressor_utils.php
new file mode 100644 (file)
index 0000000..63f328e
--- /dev/null
@@ -0,0 +1,104 @@
+<?php
+#-------------------------------------------------------------------------
+#
+# Jonathan Schmid, <hi@jonathanschmid.de>
+# Web: www.jonathanschmid.de
+#
+#-------------------------------------------------------------------------
+#
+# ImageCompressir is a CMS Made Simple module that can compress user-uploaded images.
+#
+#-------------------------------------------------------------------------
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
+#
+#-------------------------------------------------------------------------
+
+class imagecompressor_utils {
+
+       const SMUSHIT_API_URL = 'http://www.smushit.com/ysmush.it/ws.php?img=%s';
+       const SMUSHIT_UA = 'CMSms Image Compressor/0.9 (http://dev.cmsmadesimple.org/projects/imagecompressor)';
+
+       private function __construct() {}
+
+       static final public function format_bytes($size)
+       {
+               $units = array(' B', ' KB', ' MB', ' GB', ' TB');
+               for ($i = 0; $size >= 1024 && $i < 4; $i++)
+               {
+                       $size /= 1024;
+               }
+               return round($size, 1).$units[$i];
+       }
+
+       static final public function smushit_post($file_url)
+       {
+               $uri = sprintf(self::SMUSHIT_API_URL, urlencode($file_url));
+               $f = fopen($uri, 'r');
+               $response = fgets($f);
+               fclose($f);
+
+               if($response == 'null')
+               {
+                       return false;
+               }
+
+               if(function_exists('json_decode'))
+               {
+                       $response = json_decode($response);
+               }
+               else
+               {
+                       $json = new Services_JSON();
+                       $response = $json->decode($response);
+               }
+
+               return $response;
+       }
+
+       static final public function is_image($path)
+       {
+               $info = @exif_imagetype($path);
+               if($info & (IMAGETYPE_GIF | IMAGETYPE_JPEG | IMAGETYPE_PNG))
+               {
+                       return true;
+               }
+               else
+               {
+                       return false;
+               }
+       }
+
+       static final public function sort_files_by_date($a, $b)
+       {
+               if($a->filetime == $b->filetime)
+               {
+                       return 0;
+               }
+
+               return $a->filetime > $b->filetime ? -1 : 1;
+       }
+
+       static final public function GetURLPath()
+       {
+               $config = cmsms()->GetConfig();
+
+               $url = substr(realpath(__DIR__.'/../'), strlen($config['root_path']));
+               $url = str_replace(DIRECTORY_SEPARATOR, '/', $url);
+               $url = $config['root_url'] . $url;
+
+               return $url;
+       }
+}
\ No newline at end of file
index 46b1922..34b3b19 100644 (file)
@@ -1,41 +1,42 @@
 <script type="text/javascript">\r
 <!--\r
-var CMS_ADMIN_DIR = '{$admindir}';\r
-var CMS_USER_KEY = '{$userkey}';\r
-var CMS_INC_DIR = '{$incdir}';\r
-\r
 var LANG_CHECKING = '{$checkingtext}';\r
 var LANG_INQUEUE_SMUSH = '{$inqueue_smushtext}';\r
 var LANG_INQUEUE_CHECK = '{$inqueue_checktext}';\r
 -->\r
 </script>\r
-<script type="text/javascript" src="{$incdir}ajax.js"></script>\r
 <link rel="stylesheet" type="text/css" href="{$incdir}imagecompressor.css" />\r
-<table width="100%" class="pagetable" cellspacing="0" id="filelist_imagecompressor">\r
+<table class="pagetable" id="filelist_imagecompressor">\r
        <thead>\r
-       <tr>\r
-               <th class="pageicon">&nbsp;</th>\r
-               <th>{$filenametext}</th>\r
-               <th class="pageicon" style="padding-right:50px;">{$filepermstext}</th>\r
-               <th class="pageicon" style="padding-right:50px;">{$filesizetext}</th>\r
-               <th class="pageicon" style="padding-right:50px;">{$filedatetext}</th>\r
-               <th class="pageicon">{$smushittext} <br>\r
-                       <a onclick="mass_compress_check();" style="cursor:pointer;" >({$checkalltext})</a> |\r
-                       <a onclick="mass_smush();" style="cursor:pointer;">({$smushalltext})</a> |\r
-                       <a onclick="stop_queue();" style="cursor:pointer;">({$stopqueuetext})</a>\r
-               </th>\r
-       </tr>\r
+               <tr>\r
+                       <th class="pageicon">&nbsp;</th>\r
+                       <th>{$mod->Lang('filenametext')}</th>\r
+                       <th>{$mod->Lang('filepermstext')}</th>\r
+                       <th>{$mod->Lang('filesizetext')}</th>\r
+                       <th>{$mod->Lang('filedatetext')}</th>\r
+                       <th>Smush.it<br />\r
+                               (<a href="#">{$checkalltext}</a>) |\r
+                               (<a href="#">{$smushalltext}</a>) |\r
+                               (<a href="#">{$stopqueuetext}</a>)\r
+                       </th>\r
+               </tr>\r
        </thead>\r
        <tbody>\r
-       {foreach from=$files item=file name="files"}\r
-       <tr class="row{if $smarty.foreach.files.iteration % 2}1{else}2{/if}">\r
-               <td valign="middle">{$file->icon}</td>\r
-               <td valign="middle"><a href="{$file->url}" target="_blank" class="file">{$file->filename}</a></td>\r
-               <td valign="middle">{$file->fileperms}</td>\r
-               <td valign="middle">{$file->filesize}</td>\r
-               <td valign="middle">{$file->filedate}</td>\r
-               <td valign="middle"><a href="#" class="massclick" onclick="get_compression_rate('{$file->filename}', $(this).parent());return false;">{$checksavings}</a></td>\r
-       </tr>\r
-       {/foreach}\r
+               {foreach from=$files item=file name="files"}\r
+               <tr class="row{if $smarty.foreach.files.iteration % 2}1{else}2{/if}">\r
+                       <td><img src="{$file->icon}" /></td>\r
+                       <td><a href="{$file->url}" target="_blank">{$file->filename}</a></td>\r
+                       <td>{$file->fileperms}</td>\r
+                       <td>{$file->filesize}</td>\r
+                       <td>{$file->filedate}</td>\r
+                       <td><a href="#" class="check">{$mod->Lang('checksavings')}</a></td>\r
+               </tr>\r
+               {/foreach}\r
        </tbody>\r
-</table>
\ No newline at end of file
+</table>\r
+<script src="{$incdir}ImageCompressor.js"></script>\r
+<script>\r
+jQuery(function () {ldelim}\r
+       ImageCompressor.init('{$actionid}', '{$userkey}');\r
+{rdelim});\r
+</script>
\ No newline at end of file