source: extensions/download_by_size/main.inc.php @ 24124

Last change on this file since 24124 was 23893, checked in by plg, 11 years ago

update function dlsize_getFilename with Batch Downloader getFilename (escape, upper...)

File size: 3.8 KB
Line 
1<?php
2/*
3Plugin Name: Download by Size
4Version: auto
5Description: Select a photo size before download
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=
7Author: plg
8Author URI: http://le-gall.net/pierrick
9*/
10
11if (!defined('PHPWG_ROOT_PATH'))
12{
13  die('Hacking attempt!');
14}
15
16define('DLSIZE_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
17
18add_event_handler('loc_end_picture', 'dlsize_picture');
19function dlsize_picture()
20{
21  global $conf, $template, $picture;
22
23  // in case of file with a pwg_representative, we simply fallback to the
24  // standard button (which downloads the original file)
25  if (!$picture['current']['src_image']->is_original())
26  {
27    return;
28  }
29 
30  $template->set_prefilter('picture', 'dlsize_picture_prefilter');
31
32  $params = array(
33    'id' => $picture['current']['id'],
34    'part' => 'e',
35    'download' => null,
36    );
37  $base_dl_url = add_url_params(get_root_url().PHPWG_PLUGINS_PATH.'download_by_size/action.php', $params);
38 
39  $template->assign(
40    array(
41      'DLSIZE_URL' => $base_dl_url.'&amp;size=',
42      )
43    );
44
45  if ($conf['picture_download_icon'])
46  {
47    // even if original can't be downloaded, we set U_DOWNLOAD so that
48    // visitor can download smaller sizes
49    $template->append('current', array('U_DOWNLOAD' => '#'), true);
50   
51    if (!empty($picture['current']['download_url']))
52    {
53      $template->assign('DLSIZE_ORIGINAL', $picture['current']['download_url']);
54    }
55  }
56
57  $template->set_filename('dlsize_picture', realpath(DLSIZE_PATH.'picture.tpl'));
58  $template->parse('dlsize_picture');
59}
60
61function dlsize_picture_prefilter($content, &$smarty)
62{
63  $pattern = '#\{if isset\(\$current\.U_DOWNLOAD\)\}\s*<a #';
64  $replacement = '{if isset($current.U_DOWNLOAD)}<a id="downloadSizeLink" ';
65  $content = preg_replace($pattern, $replacement, $content);
66
67  return $content;
68}
69
70/**
71 * getFilename function, copied from Batch Manager
72 */
73function dlsize_getFilename($row, $filesize=array())
74{
75  global $conf;
76
77  if (!isset($conf['download_by_size_file_pattern']))
78  {
79    $conf['download_by_size_file_pattern'] = '%filename%_%dimensions%';
80  }
81 
82  $row['filename'] = stripslashes(get_filename_wo_extension($row['file']));
83
84  // datas
85  $search = array('%id%', '%filename%', '%author%', '%dimensions%');
86  $replace = array($row['id'], $row['filename']);
87
88  $replace[2] = empty($row['author']) ? null : $row['author'];
89  $replace[3] = empty($filesize) ? null : $filesize['width'].'x'.$filesize['height'];
90
91  $filename = str_replace($search, $replace, $conf['download_by_size_file_pattern']);
92
93  // functions
94  $filename = preg_replace_callback('#\$escape\((.*?)\)#', create_function('$m', 'return str2url($m[1]);'),   $filename);
95  $filename = preg_replace_callback('#\$upper\((.*?)\)#',  create_function('$m', 'return str2upper($m[1]);'), $filename);
96  $filename = preg_replace_callback('#\$lower\((.*?)\)#',  create_function('$m', 'return str2lower($m[1]);'), $filename);
97  $filename = preg_replace_callback('#\$strpad\((.*?),(.*?),(.*?)\)#', create_function('$m', 'return str_pad($m[1],$m[2],$m[3],STR_PAD_LEFT);'), $filename);
98
99  // cleanup
100  $filename = preg_replace(
101    array('#_+#', '#-+#', '# +#', '#^([_\- ]+)#', '#([_\- ]+)$#'),
102    array('_', '-', ' ', null, null),
103    $filename
104    );
105
106  if (empty($filename) || $filename == $conf['download_by_size_file_pattern'])
107  {
108    $filename = $row['filename'];
109  }
110
111  $filename.= '.'.get_extension($row['path']);
112
113  return $filename;
114}
115
116if (!function_exists('str2lower'))
117{ 
118  if (function_exists('mb_strtolower') && defined('PWG_CHARSET'))
119  { 
120    function str2lower($term)
121    {
122      return mb_strtolower($term, PWG_CHARSET);
123    }
124    function str2upper($term)
125    {
126      return mb_strtoupper($term, PWG_CHARSET);
127    }
128  }
129  else
130  { 
131    function str2lower($term)
132    {
133      return strtolower($term);
134    }
135    function str2upper($term)
136    {
137      return strtoupper($term);
138    }
139  }
140}
141?>
Note: See TracBrowser for help on using the repository browser.