XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
images.php
Go to the documentation of this file.
1 <?php
2 // $Id: images.php 999 2012-07-02 03:53:17Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
16 include_once '../../include/cp_header.php';
17 require_once XOOPS_ROOT_PATH.'/modules/rmcommon/admin_loader.php';
18 define('RMCLOCATION','imgmanager');
19 
23 function show_images(){
25 
26  define('RMCSUBLOCATION','showimages');
27 
28  $db = XoopsDatabaseFactory::getDatabaseConnection();
29 
30  // Check if some category exists
31  $catnum = RMFunctions::get_num_records("rmc_img_cats");
32  if ($catnum<=0){
33  redirectMsg('images.php?action=newcat', __('There are not categories yet! Please create one in order to add images.','rmcommon'), 1);
34  die();
35  }
36 
37  $cat = rmc_server_var($_GET, 'category', 0);
38  if ($cat<=0){
39  header('location: images.php?action=showcats');
40  die();
41  }
42  $sql = "SELECT COUNT(*) FROM ".$db->prefix("rmc_images");
43  if ($cat>0) $sql .= " WHERE cat='$cat'";
47  $page = rmc_server_var($_GET, 'page', 1);
48  $limit = $xoopsModuleConfig['imgsnumber'];
49  list($num) = $db->fetchRow($db->query($sql));
50 
51  $tpages = ceil($num / $limit);
53 
54  $start = $num<=0 ? 0 : ($page - 1) * $limit;
55 
56  $nav = new RMPageNav($num, $limit, $page, 5);
57  $nav->target_url('images.php?'.($cat>0 ? 'category='.$cat : '').'&page={PAGE_NUM}');
58 
59  // Get categories
60  $sql = "SELECT * FROM ".$db->prefix("rmc_images")." ".($cat>0 ? "WHERE cat='$cat'" : '')." ORDER BY id_img DESC LIMIT $start,$limit";
61 
62  $result = $db->query($sql);
63  $images = array();
64  $categories = array();
65  $authors = array();
66 
68  $sizes = $category->getVar('sizes');
69  $current_size = array();
70 
71  foreach ($sizes as $size){
72  if (empty($current_size)){
73  $current_size = $size;
74  } else {
75  if ($current_size['width']>=$size['width'] && $size['width']>0){
76  $current_size = $size;
77  }
78  }
79  }
80 
81  while($row = $db->fetchArray($result)){
82  $img = new RMImage();
83  $img->assignVars($row);
84  if (!isset($categories[$img->getVar('cat')])){
85  $categories[$img->getVar('cat')] = new RMImageCategory($img->getVar('cat'));
86  }
87 
88  if (!isset($authors[$img->getVar('uid')])){
89  $authors[$img->getVar('uid')] = new XoopsUser($img->getVar('uid'));
90  }
91 
92  $fd = pathinfo($img->getVar('file'));
93 
94  $images[] = array(
95  'id' => $img->id(),
96  'title' => $img->getVar('title'),
97  'date' => $img->getVar('date'),
98  'desc' => $img->getVar('desc', 'n'),
99  'cat' => $categories[$img->getVar('cat')]->getVar('name'),
100  'author' => $authors[$img->getVar('uid')],
101  'file' => XOOPS_UPLOAD_URL.'/'.date('Y',$img->getVar('date')).'/'.date('m',$img->getVar('date')).'/sizes/'.$fd['filename'].'_'.$current_size['width'].'x'.$current_size['height'].'.'.$fd['extension'],
102  'big' => XOOPS_UPLOAD_URL.'/'.date('Y',$img->getVar('date')).'/'.date('m',$img->getVar('date')).'/'.$fd['filename'].'.'.$fd['extension']
103  );
104  }
105 
106  $categories = RMFunctions::load_images_categories();
107 
108  if (RMFunctions::plugin_installed('lightbox')){
109  RMLightbox::get()->add_element('#list-images a.bigimages');
110  RMLightbox::get()->render();
111  }
112 
113  xoops_cp_header();
115  RMTemplate::get()->add_style('imgmgr.css','rmcommon');
116  RMTemplate::get()->add_style('general.css','rmcommon');
117  RMTemplate::get()->add_script('include/js/jquery.checkboxes.js');
118  include RMTemplate::get()->get_template('images_images.php','module','rmcommon');
119  xoops_cp_footer();
120 
121 }
122 
123 function images_form($edit = 0){
125 
126  define('RMCSUBLOCATION','addimages');
127 
128  $category = rmc_server_var($_GET, 'category', 0);
130 
131  if (!$cat->isNew() && $cat->getVar('status')!='open'){
132  showMessage(sprintf(__('Category %s is closed. Please, select another category.','rmcommon'), '"'.$cat->getVar('name').'"'), 1);
133  $cat = new RMImageCategory();
134  }
135 
136  /*$upload = new RMFlashUploader('images', 'images.php');*/
137  if (!$cat->isNew()){
138  $uploader = new RMFlashUploader('files-container', RMCURL.'/include/upload.php');
139  $uploader->add_setting('formData', array(
140  'action'=>'upload',
141  'category'=>$cat->id(),
142  // Need better code
143  'rmsecurity'=>TextCleaner::getInstance()->encrypt($xoopsUser->uid().'|'.RMCURL.'/images.php'.'|'.$xoopsSecurity->createToken(), true))
144  );
145  $uploader->add_setting('multi', true);
146  $uploader->add_setting('fileExt', '*.jpg;*.png;*.gif');
147  $uploader->add_setting('fileDesc', __('All Images (*.jpg, *.png, *.gif)','rmcommon'));
148  $uploader->add_setting('sizeLimit', $cat->getVar('filesize') * $cat->getVar('sizeunit'));
149  $uploader->add_setting('buttonText', __('Browse Images...','rmcommon'));
150  $uploader->add_setting('queueSizeLimit', 100);
151  $uploader->add_setting('onUploadSuccess',"function(file, resp, data){
152  eval('ret = '+resp);
153  if (ret.error){
154  \$('#upload-errors').append('<span class=\"failed\"><strong>'+file.name+'</strong>: '+ret.message+'</span>');
155  } else {
156  total++;
157  ids[total-1] = ret.id;
158  \$('#upload-errors').append('<span class=\"done\"><strong>'+file.name+'</strong>: ".__('Uploaded successfully!','rmcommon')."</span>');
159  }
160  return true;
161  }");
162  $uploader->add_setting('onQueueComplete', "function(event, data){
163 
164  if(total<=0) return;
165 
166  \$('.select_image_cat').hide('slow');
167  \$('#upload-errors').hide('slow');
168  \$('#upload-errors').html('');
169  \$('#upload-controls').hide('slow');
170  \$('#resizer-bar').show('slow');
171  \$('#resizer-bar').effect('highlight',{},1000);
172  \$('#gen-thumbnails').show();
173 
174  var increments = 1/total*100;
175  url = '".RMCURL."/images.php';
176 
177  params = '".TextCleaner::getInstance()->encrypt($xoopsUser->uid().'|'.RMCURL.'/images.php'.'|'.$xoopsSecurity->createToken(), true)."';
178  resize_image(params);
179 
180  }");
181  RMTemplate::get()->add_head($uploader->render());
182  }
183  RMTemplate::get()->add_style('imgmgr.css', 'rmcommon');
184  RMTemplate::get()->add_script('include/js/images.js');
185 
186  // Load Categories
187  $categories = RMFunctions::load_images_categories("WHERE status='open' ORDER BY id_cat DESC");
188 
189  xoops_cp_header();
191 
192  include RMTemplate::get()->get_template('images_uploadimages.php','module','rmcommon');
193 
194  xoops_cp_footer();
195 
196 }
197 
198 function show_categories(){
200 
201  define('RMCSUBLOCATION','showcategories');
202 
203  $db = XoopsDatabaseFactory::getDatabaseConnection();
204  $sql = "SELECT COUNT(*) FROM ".$db->prefix("rmc_img_cats");
208  $page = rmc_server_var($_GET, 'page', 1);
209  $limit = $xoopsModuleConfig['catsnumber'];
210  list($num) = $db->fetchRow($db->query($sql));
211 
212  $tpages = ceil($num / $limit);
213  $page = $page > $tpages ? $tpages : $page;
214 
215  $start = $num<=0 ? 0 : ($page - 1) * $limit;
216 
217  $nav = new RMPageNav($num, $limit, $page, 5);
218  $nav->target_url('images.php?action=showcats&page={PAGE_NUM}');
219 
220  // Get categories
221  $sql = "SELECT * FROM ".$db->prefix("rmc_img_cats")." ORDER BY id_cat DESC LIMIT $start,$limit";
222 
223  $result = $db->query($sql);
224  $categories = array();
225 
226  while ($row = $db->fetchArray($result)){
227  $cat = new RMImageCategory();
228  $cat->assignVars($row);
229  $groups = $cat->getVar('groups');
230  $categories[] = array(
231  'id' => $cat->id(),
232  'name' => $cat->getVar('name'),
233  'status' => $cat->getVar('status'),
234  'gwrite' => RMFunctions::get_groups_names($groups['write']),
235  'gread' => RMFunctions::get_groups_names($groups['read']),
236  'sizes' => $cat->getVar('sizes'),
237  'images' => RMFunctions::get_num_records('rmc_images', 'cat='.$cat->id())
238  );
239  }
240 
241  RMTemplate::get()->add_style('general.css','rmcommon');
242  RMTemplate::get()->add_style('imgmgr.css','rmcommon');
243  RMTemplate::get()->add_script('include/js/jquery.checkboxes.js');
245  xoops_cp_header();
246 
247 
248  include RMTemplate::get()->get_template('images_categories.php', 'module', 'rmcommon');
249 
250  xoops_cp_footer();
251 
252 }
253 
257 function new_category($edit = 0){
258  define('RMCSUBLOCATION','newcategory');
259 
260  extract($_GET);
261 
262  // Check category to edit
263  if ($edit){
264  if ($id<=0){
265  redirectMsg('images.php?action=showcats', __('You must specify a category id to edit!','rmcommon'), 1);
266  die();
267  }
268 
269  $cat = new RMImageCategory($id);
270 
271  if ($cat->isNew()){
272  redirectMsg('images.php?action=showcats', __('The specified category does not exist!','rmcommon'), 1);
273  die();
274  }
275 
276  // Write and read permissions
277  $perms = $cat->getVar('groups');
278  $write = isset($write) ? $write : $perms['write'];
279  $read = isset($read) ? $read : $perms['read'];
280  // Active
281  $active = $cat->getVar('status');
282 
283  }
284 
286  xoops_cp_header();
287 
288  $form = new RMForm('','','');
289  $fwrite = new RMFormGroups('','write',true,1, 3, isset($write) ? $write : array(XOOPS_GROUP_ADMIN));
290  $fread = new RMFormGroups('','read',true,1, 3, isset($read) ? $read : array(0));
291 
292  RMTemplate::get()->add_script('include/js/imgmanager.js');
293  include RMTemplate::get()->get_template('categories_form.php', 'module', 'rmcommon');
294  RMTemplate::get()->add_style('imgmgr.css','rmcommon');
295  xoops_cp_footer();
296 
297 }
298 
302 function save_category($edit = 0){
303  global $xoopsDB, $xoopsModuleConfig, $xoopsModule;
304 
305  $q = 'images.php?action='.($edit ? 'editcat' : 'newcat');
306  foreach($_POST as $k => $v){
307  if ($k=='action' || $k=='XOOPS_TOKEN_REQUEST') continue;
308  if (is_array($v)){
309  $q .= '&'.RMFunctions::urlencode_array($v, $k);
310  } else {
311  $q .= '&'.$k.'='.urlencode($v);
312  }
313 
314  }
315  extract($_POST);
316 
317  if ($edit){
318 
319  if ($id<=0){
320  redirectMsg('images.php?action=showcats', __('Specify a valid category id','rmcommon'), 1);
321  die();
322  }
323 
324  $cat = new RMImageCategory($id);
325  if ($cat->isNew()){
326  redirectMsg('images.php?action=showcats', __('Specified category does not exists!','rmcommon'), 1);
327  die();
328  }
329 
330  } else {
331  $cat = new RMImageCategory();
332  }
333 
334  if ($name==''){
335  redirectMsg($q, __('Please specify a category name','rmcommon'), 1);
336  die();
337  }
338 
339  if (empty($read)) $read = array(0);
340  if (empty($write)) $write = array(0);
341 
342  // Check if resize data is correct
343  $schecked = array();
344  foreach ($sizes as $size){
345  if (trim($size['name'])=='') continue;
346  if ($size['type']!='none' && $size['width']<=0 && $size['height']<=0) continue;
347  $schecked[] = $size;
348  }
349 
350  if (empty($schecked)){
351  redirectMsg($q, __('You must create one size for this category at least!','rmcommon'), 1);
352  die();
353  }
354 
355  // Check if there are a category with same name
356  $num = RMFunctions::get_num_records('rmc_img_cats', "name='$name'".($edit ? " AND id_cat<>'".$cat->id()."'" : ''));
357  if($num>0){
358  redirectMsg($q, __('There is already a category with the same name!','rmcommon'), 1);
359  die();
360  }
361 
362  $cat->setVar('name', $name);
363  $cat->setVar('status', $status);
364  $cat->setVar('groups', array('write'=>$write,'read'=>$read));
365  $cat->setVar('sizes',$schecked);
366  $cat->setVar('filesize', $filesize<=0 ? '50' : $filesize);
367  $cat->setVar('sizeunit', $sizeunit<=0 ? '1024' : $sizeunit);
368 
369  if ($cat->save()){
370  redirectMsg('images.php?action=showcats', __($edit ? 'Category updated successfully!' : 'Category saved successfully!','rmcommon'), 0);
371  } else {
372  redirectMsg($q, __('There were some erros while trying to save this category.','rmcommon').'<br />'.$cat->errors(), 1);
373  }
374 
375 }
376 
380 function category_status($action='open'){
381 
382  $cats = rmc_server_var($_REQUEST, 'cats', array());
383 
384  if (empty($cats)){
385  $id = rmc_server_var($_GET, 'id', 0);
386 
387  if ($id<=0){
388  redirectMsg('images.php?action=showcats', __('Specify one category at least to change status!','rmcommon'), 1);
389  die();
390  }
391 
392  $cats[] = $id;
393 
394  }
395 
396  $db = XoopsDatabaseFactory::getDatabaseConnection();
397  $sql = "UPDATE ".$db->prefix("rmc_img_cats")." SET status='".$action."' WHERE id_cat IN(".implode(',',$cats).")";
398 
399  if ($db->queryF($sql)){
400  redirectMsg('images.php?action=showcats', __('Database updated successfully!','rmcommon'), 0);
401  die();
402  } else {
403  redirectMsg('images.php?action=showcats', __('There were some erros while updating database:','rmcommon').'<br />'.$db->error(), 1);
404  die();
405  }
406 
407 }
408 
409 function send_error($message){
410  $data['error'] = 1;
411  $data['message'] = $message;
412  echo json_encode($data);
413  die();
414 }
415 
416 function resize_images(){
418 
419  set_time_limit(0);
420 
421  error_reporting(0);
422  $xoopsLogger->activated = false;
423 
424  $params = rmc_server_var($_GET, 'data','');
425  $id = rmc_server_var($_GET, 'img', 0);
426 
427  if ($params==''){
428  send_error(__('Unauthorized!','rmcommon'));
429  }
430 
431  if ($id<=0){
432  send_error(__('Invalid image!','rmcommon'));
433  }
434 
435  $params = TextCleaner::decrypt($params);
436  $data = explode('|', $params);
437 
438  if ($data[0]!=$xoopsUser->uid()){
439  send_error(__('Unauthorized!','rmcommon'));
440  }
441 
442  if ($data[1]!=RMCURL.'/images.php'){
443  send_error(__('Unauthorized!','rmcommon'));
444  }
445 
446  if (!$xoopsSecurity->check(false, $data[2])){
447  send_error(__('Unauthorized!','rmcommon'));
448  }
449 
450  $image = new RMImage($id);
451  if ($image->isNew()){
452  send_error(__('Image not found!','rmcommon'));
453  }
454 
455  // Resize image
456  $cat = new RMImageCategory($image->getVar('cat'));
457  if (!$cat->user_allowed_toupload($xoopsUser)){
458  send_error(__('Unauthorized','rmcommon'));
459  }
460 
461  $sizes = $cat->getVar('sizes');
462  $updir = XOOPS_UPLOAD_PATH.'/'.date('Y', $image->getVar('date')).'/'.date('m',time());
463  $upurl = XOOPS_UPLOAD_URL.'/'.date('Y', $image->getVar('date')).'/'.date('m',time());;
464  $width = 0;
465  $tfile = '';
466 
467  $ret['sizes'] = array();
468  foreach ($sizes as $size){
469 
470  if ($size['width']<=0 && $size['height']<=0) continue;
471 
472  $fd = pathinfo($updir.'/'.$image->getVar('file'));
473 
474  $name = $updir.'/sizes/'.$fd['filename'].'_'.$size['width'].'x'.$size['height'].'.'.$fd['extension'];
475 
476  $ret['sizes'][$size['name']] = str_replace(XOOPS_UPLOAD_PATH, XOOPS_UPLOAD_URL, $name);;
477 
478  list($currentWidth, $currentHeight) = getimagesize($updir.'/'.$image->getVar('file'));
479 
480  $sizer = new RMImageResizer($updir.'/'.$image->getVar('file'), $name);
481 
482  $resize = true;
483 
484  // TODO 5: Verificar que funcione correctamente
485  if($size['type']=='crop'){
486 
487  if($size['width'] > $currentWidth || $size['height'] > $currentHeight)
488  $resize = false;
489 
490  } else {
491 
492  if($size['width']>$currentWidth && $size['height']>$currentHeight)
493  $resize = false;
494 
495  if($size['width']>$currentWidth)
496  $size['width'] = $currentWidth;
497 
498  if($size['height']>$currentHeight)
499  $size['height'] = $currentHeight;
500 
501  }
502 
503  if($resize){
504  switch($size['type']){
505  case 'crop':
506  $sizer->resizeAndCrop($size['width'], $size['height']);
507  break;
508  default:
509  if ($size['width']<=0 || $size['height']<=0){
510  $sizer->resizeWidth($size['width']);
511  } else {
512  $sizer->resizeWidthOrHeight($size['width'], $size['height']);
513  }
514  break;
515  }
516  }
517 
518  if($size['width']<=$width || $width==0){
519  $width = $size['width'];
520  $tfile = str_replace(XOOPS_UPLOAD_PATH, XOOPS_UPLOAD_URL, $name);
521  }
522 
523  }
524 
525  $ret['message'] = sprintf(__('%s done!', 'rmcommon'), $image->getVar('file'));
526  $ret['done'] = 1;
527  $ret['file'] = $tfile;
528  $ret['title'] = $image->getVar('title');
529  echo json_encode($ret);
530 
531  die();
532 }
533 
537 function edit_image(){
538  global $xoopsUser, $xoopsSecurity;
539 
540  $id = rmc_server_var($_GET, 'id', 0);
541  $page = rmc_server_var($_GET, 'page', '');
542  if ($id<=0){
543  redirectMsg('images.php', __('Invalid image ID', 'rmcommon'), 1);
544  die();
545  }
546 
547  $image = new RMImage($id);
548  if ($image->isNew()){
549  redirectMsg('images.php', __('Image not found!', 'rmcommon'), 1);
550  die();
551  }
552 
553  $cat = new RMImageCategory($image->getVar('cat'));
554  $sizes = $cat->getVar('sizes');
555  $current_size = array();
556 
557  $fd = pathinfo($image->getVar('file'));
558  $updir = '/'.date('Y', $image->getVar('date')).'/'.date('m',time());
559 
560  foreach ($sizes as $size){
561  if ($size['width']<=0) continue;
562  if (empty($current_size)){
563  $current_size = $size;
564  } else {
565  if ($current_size['width']>=$size['width'] && $size['width']>0){
566  $current_size = $size;
567  }
568  }
569 
570  $image_data['sizes'][] = array(
571  'file' => XOOPS_UPLOAD_URL.$updir.'/sizes/'.$fd['filename'].'_'.$size['width'].'x'.$size['height'].'.'.$fd['extension'],
572  'name' => $size['name']
573  );
574  }
575 
576 
577  $image_data['thumbnail'] = XOOPS_UPLOAD_URL.$updir.'/sizes/'.$fd['filename'].'_'.$current_size['width'].'x'.$current_size['height'].'.'.$fd['extension'];
578  $mimes = include(XOOPS_ROOT_PATH.'/include/mimetypes.inc.php');
579  $image_data['mime'] = isset($mimes[$fd['extension']]) ? $mimes[$fd['extension']] : 'application/octet-stream';
580  $image_data['file'] = $image->getVar('file');
581  $image_data['date'] = $image->getVar('date');
582  $image_data['title'] = $image->getVar('title');
583  $image_data['desc'] = $image->getVar('desc', 'e');
584  $image_data['url'] = XOOPS_UPLOAD_URL.$updir.'/'.$image->getVar('file');
585 
586  $categories = RMFunctions::load_images_categories("WHERE status='open' ORDER BY id_cat DESC");
587 
588  xoops_cp_header();
590 
591  RMTemplate::get()->add_script('include/js/images.js');
592  RMTemplate::get()->add_script('include/js/jquery.validate.min.js');
593  RMTemplate::get()->add_style('imgmgr.css', 'rmcommon');
594  include RMTemplate::get()->get_template('images_edit.php','module','rmcommon');
595 
596  xoops_cp_footer();
597 }
598 
602 function update_image(){
603  global $xoopsUser, $xoopsSecurity;
604 
605  set_time_limit(0);
606 
607  $title = rmc_server_var($_POST, 'title', '');
608  $category = rmc_server_var($_POST, 'cat', '');
609  $desc = rmc_server_var($_POST, 'desc', '');
610  $page = rmc_server_var($_POST, 'page', 1);
611  $id = rmc_server_var($_POST, 'id', 0);
612 
613  if (!$xoopsSecurity->check()){
614  redirectMsg('images.php', __('Operation not allowed!','rmcommon'), 1);
615  die();
616  }
617 
618  if ($id<=0){
619  redirectMsg("images.php?category=$cat&page=$page", __('Image ID not provided!','rmcommon'), 1);
620  die();
621  }
622 
623  if (trim($title)==''){
624  redirectMsg("images.php?action=edit&id=$id&page=$page", __('You must provide a title for this image','rmcommon'), 1);
625  die();
626  }
627 
628  $image = new RMImage($id);
629  if ($image->isNew()){
630  redirectMsg("images.php?category=$cat&page=$page", __('Image not exists!','rmcommon'), 1);
631  die();
632  }
633 
635  if ($cat->isNew()){
636  redirectMsg("images.php", __('Category not exist!','rmcommon'), 1);
637  die();
638  }
639 
640  if ($cat->id()!=$image->getVar('cat')){
641  $pcat = new RMImageCategory($image->getVar('cat'));
642  }
643 
644  $image->setVar('title', $title);
645  $image->setVar('desc', $desc);
646  if (isset($pcat)) $image->setVar('cat', $cat->id());
647 
648  if (!$image->save()){
649  redirectMsg("images.php?action=edit&id=$id&page=$page", __('the image could not be updated!','rmcommon').'<br />'.$image->errors(), 1);
650  die();
651  }
652 
653  // Modify image dimensions if category has changed
654  if (!isset($pcat)){
655  redirectMsg("images.php?category=".$cat->id()."&page=$page", __('Image updated succesfully!','rmcommon'), 0);
656  die();
657  }
658 
659  $fd = pathinfo($image->getVar('file'));
660  $updir = XOOPS_UPLOAD_PATH.'/'.date('Y', $image->getVar('date')).'/'.date('m',time());
661 
662  // Delete current image files
663  foreach ($pcat->getVar('sizes') as $size){
664  if ($size['width']<=0) continue;
665  $file = $updir.'/sizes/'.$fd['filename'].'_'.$size['width'].'x'.$size['height'].'.'.$fd['extension'];
666  @unlink($file);
667 
668  }
669 
670  // Create new image files
671  foreach ($cat->getVar('sizes') as $size){
672  if ($size['width']<=0 && $size['height']<=0) continue;
673 
674  $name = $updir.'/sizes/'.$fd['filename'].'_'.$size['width'].'x'.$size['height'].'.'.$fd['extension'];
675  $sizer = new RMImageResizer($updir.'/'.$image->getVar('file'), $name);
676 
677  switch($size['type']){
678  case 'crop':
679  $sizer->resizeAndCrop($size['width'], $size['height']);
680  break;
681  default:
682  if ($size['width']<=0 || $size['height']<=0){
683  $sizer->resizeWidth($size['width']);
684  } else {
685  $sizer->resizeWidthOrHeight($size['width'], $size['height']);
686  }
687  break;
688  }
689 
690  $width = $width==0 ? $size['width'] : $width;
691  if($width<$size['width']){
692  $with = $size['width'];
693  $tfile = str_replace(XOOPS_UPLOAD_PATH, XOOPS_UPLOAD_URL, $name);
694  }
695  }
696 
697  redirectMsg('images.php?category='.$cat->id(), __('Image updated successfully!', 'rmcommon'), 0);
698 
699 }
700 
704 function delete_image(){
705 
706  $ids = rmc_server_var($_REQUEST, 'imgs', array());
707  $page = rmc_server_var($_REQUEST, 'page', 0);
708  $category = rmc_server_var($_REQUEST, 'category', 0);
709 
710  if (count($ids)<=0){
711  redirectMsg('images.php?category='.$category.'&page='.$page, __('Please, speciy an image at least!','rmcommon'), 1);
712  die();
713  }
714 
715  $errors = '';
716 
717  foreach ($ids as $id){
718 
719  $image = new RMImage($id);
720  if ($image->isNew()){
721  redirectMsg('images.php', __('Image not exists!','rmcommon'), 1);
722  die();
723  }
724 
725  $cat = new RMImageCategory($image->getVar('cat'));
726 
727  $fd = pathinfo($image->getVar('file'));
728  $updir = XOOPS_UPLOAD_PATH.'/'.date('Y', $image->getVar('date')).'/'.date('m',time());
729 
730  // Delete current image files
731  foreach ($cat->getVar('sizes') as $size){
732  if ($size['width']<=0) continue;
733  $file = $updir.'/sizes/'.$fd['filename'].'_'.$size['width'].'x'.$size['height'].'.'.$fd['extension'];
734  @unlink($file);
735  }
736 
737  $file = $updir.'/'.$image->getVar('file');
738  @unlink($file);
739 
740  if (!$image->delete()){
741  $errors .= $image->errors();
742  }
743 
744  }
745 
746  if ($errors!=''){
747  redirectMsg('images.php?category='.$cat->id().'&page='.$page, __('Errors ocurred during images deletion!', 'rmcommon').'<br />'.$errors, 0);
748  } else {
749  redirectMsg('images.php?category='.$cat->id().'&page='.$page, __('Images deleted successfully!', 'rmcommon'), 0);
750  }
751 
752 }
753 
757 function delete_category(){
758  global $xoopsSecurity;
759 
760  $id = rmc_server_var($_GET, 'id', 0);
761 
762  if (!$xoopsSecurity->check()){
763  redirectMsg('images.php?action=showcats', __('Operation not allowed!', 'rmcommon'), 1);
764  die();
765  }
766 
767  if ($id<=0){
768  redirectMsg('images.php?action=showcats', __('Category ID not provided', 'rmcommon'), 1);
769  die();
770  }
771 
772  $cat = new RMImageCategory($id);
773  if ($cat->isNew()){
774  redirectMsg('images.php?action=showcats', __('Category not found', 'rmcommon'), 1);
775  die();
776  }
777 
778  $sizes = array();
779  foreach ($cat->getVar('sizes') as $size){
780  if ($size['width']<=0) continue;
781 
782  $sizes[] = '_'.$size['width'].'x'.$size['height'];
783 
784  }
785 
786  $db = XoopsDatabaseFactory::getDatabaseConnection();
787  $sql = "SELECT * FROM ".$db->prefix("rmc_images")." WHERE cat='".$cat->id()."'";
788  $result = $db->query($sql);
789 
790  while($row = $db->fetchArray($result)){
791  $image = new RMImage();
792  $image->assignVars($row);
793 
794  $updir = XOOPS_UPLOAD_PATH.'/'.date('Y', $image->getVar('date')).'/'.date('m',time());
795  $fd = pathinfo($image->getVar('file'));
796 
797  foreach ($sizes as $size){
798  $file = $updir.'/sizes/'.$fd['filename'].$size.'.'.$fd['extension'];
799  @unlink($file);
800  }
801  $file = $updir.'/'.$image->getVar('file');
802  @unlink($file);
803  $image->delete();
804  }
805 
806  if ($cat->delete()){
807  redirectMsg('images.php?action=showcats', __('Category deleted successfully!', 'rmcommon'), 0);
808  } else {
809  redirectMsg('images.php?action=showcats', __('Errors ocurred while deleting the category', 'rmcommon').'<br />'.$cat->errors(), 0);
810  }
811 
812 }
813 
817 function update_thumbnails(){
818  global $xoopsUser, $xoopsSecurity;
819  $cat = new RMImageCategory(rmc_server_var($_POST, 'category', 0));
820  $imgs = rmc_server_var($_POST, 'imgs', array());
821 
822  $ids = implode(',', $imgs);
823 
824  RMTemplate::get()->add_style('imgmgr.css', 'rmcommon');
825  RMTemplate::get()->add_script('include/js/images.js');
826 
827  xoops_cp_header();
829 
830  $isupdate = true;
831  RMTemplate::get()->add_head("<script type='text/javascript'>
832  \$(document).ready(function(){
833  ids = [$ids];
834  total = ".count($imgs).";
835  \$('#resizer-bar').show('slow');
836  \$('#resizer-bar').effect('highlight',{},1000);
837  \$('#gen-thumbnails').show();
838 
839  var increments = 1/total*100;
840  url = '".RMCURL."/images.php';
841 
842  params = '".TextCleaner::getInstance()->encrypt($xoopsUser->uid().'|'.RMCURL.'/images.php'.'|'.$xoopsSecurity->createToken(), true)."';
843  resize_image(params);
844  });</script>");
845 
846  include RMTemplate::get()->get_template('images_uploadimages.php','module','rmcommon');
847 
848  xoops_cp_footer();
849 
850 }
851 
852 
853 $action = rmc_server_var($_REQUEST, 'action', '');
854 
855 switch ($action){
856  case 'showcats':
857  show_categories();
858  break;
859  case 'newcat':
860  new_category();
861  break;
862  case 'editcat':
863  new_category(1);
864  break;
865  case 'delcat':
866  delete_category();
867  break;
868  case 'save':
869  save_category();
870  break;
871  case 'saveedit':
872  save_category(1);
873  break;
874  case 'opencat':
875  category_status('open');
876  break;
877  case 'closecat':
878  category_status('close');
879  break;
880  case 'new':
881  images_form(0);
882  break;
883  case 'resize':
884  resize_images();
885  break;
886  case 'edit':
887  edit_image();
888  break;
889  case 'update':
890  update_image();
891  break;
892  case 'delete':
893  delete_image();
894  break;
895  case 'thumbs':
897  break;
898  default:
899  show_images();
900  break;
901 }