Browse Source

Slim down image tools

Some minor functionality will need to be reimplemented later
Not a priority though
spaghetti 7 years ago
parent
commit
6f6ca3ffa1

+ 17
- 216
classes/imagetools.class.php View File

5
  * Thumbnail aide, mostly
5
  * Thumbnail aide, mostly
6
  */
6
  */
7
 class ImageTools {
7
 class ImageTools {
8
-  /**
9
-   * Store processed links to avoid repetition
10
-   * @var array 'URL' => 'Parsed URL'
11
-   */
12
-  private static $Storage = [];
13
-
14
-  /**
15
-   * We use true as an extra property to make the domain an array key
16
-   * @var array $Hosts Array of image hosts
17
-   */
18
-  private static $Hosts = array(
19
-//    'imgur.com' => true,
20
-    'whatimg.com' => true
21
-  );
22
-
23
-  /**
24
-   * Blacklisted sites
25
-   * @var array $Blacklist Array of blacklisted hosts
26
-   */
27
-  private static $Blacklist = array(
28
-    'tinypic.com'
29
-  );
30
-
31
-  /**
32
-   * Array of image hosts that provide thumbnailing
33
-   * @var array $Thumbs
34
-   */
35
-  private static $Thumbs = array(
36
-//    'i.imgur.com' => true,
37
-    'whatimg.com' => true
38
-  );
39
-
40
-  /**
41
-   * Array of extensions
42
-   * @var array $Extensions
43
-   */
44
-  private static $Extensions = array(
45
-    'jpg' => true,
46
-    'jpeg' => true,
47
-    'png' => true,
48
-    'gif' => true,
49
-  );
50
-
51
-  /**
52
-   * Array of user IDs whose avatars have been checked for size
53
-   * @var array $CheckedAvatars
54
-   */
55
-  private static $CheckedAvatars = [];
56
-  private static $CheckedAvatars2 = [];
57
-
58
-  /**
59
-   * Array of user IDs whose donor icons have been checked for size
60
-   * @var array $CheckedDonorIcons
61
-   */
62
-  private static $CheckedDonorIcons = [];
63
-
64
-  /**
65
-   * Checks from our list of valid hosts
66
-   * @param string $Host Domain/host to check
67
-   * @return boolean
68
-   */
69
-  public static function valid_host($Host) {
70
-    return !empty(self::$Hosts[$Host]) && self::$Hosts[$Host] === true;
71
-  }
72
-
73
-  /**
74
-   * Checks if a link's host is (not) good, otherwise displays an error.
75
-   * @param string $Url Link to an image
76
-   * @return boolean
77
-   */
78
-  public static function blacklisted($Url, $ShowError = true) {
79
-    foreach (self::$Blacklist as &$Value) {
80
-      $Blacklisted = stripos($Url, $Value);
81
-      if ($Blacklisted !== false) {
82
-        $ParsedUrl = parse_url($Url);
83
-        if ($ShowError) {
84
-          error($ParsedUrl['host'] . ' is not an allowed image host. Please use a different host.');
85
-        }
86
-        return true;
87
-      }
88
-    }
89
-    return false;
90
-  }
91
-
92
-  /**
93
-   * Checks to see if a link has a thumbnail
94
-   * @param string $Url Link to an image
95
-   * @return string|false Matched host or false
96
-   */
97
-  private static function thumbnailable($Url) {
98
-    $ParsedUrl = parse_url($Url);
99
-    return !empty(self::$Thumbs[$ParsedUrl['host']]);
100
-  }
101
-
102
-  /**
103
-   * Checks an extension
104
-   * @param string $Ext Extension to check
105
-   * @return boolean
106
-   */
107
-  private static function valid_extension($Ext) {
108
-//    return @self::$Extensions[$Ext] === true;
109
-    return !empty(self::$Extensions[$Ext]) && (self::$Extensions[$Ext] === true);
110
-  }
111
-
112
-  /**
113
-   * Stores a link with a (thumbnail) link
114
-   * @param type $Link
115
-   * @param type $Processed
116
-   */
117
-  private static function store($Link, $Processed) {
118
-    self::$Storage[$Link] = $Processed;
119
-  }
120
-
121
-  /**
122
-   * Retrieves an entry from our storage
123
-   * @param type $Link
124
-   * @return boolean|string Returns false if no match
125
-   */
126
-  private static function get_stored($Link) {
127
-    if (isset(self::$Storage[$Link])) {
128
-      return self::$Storage[$Link];
129
-    }
130
-    return false;
131
-  }
132
-
133
-  /**
134
-   * Checks if URL points to a whatimg thumbnail.
135
-   */
136
-  private static function has_whatimg_thumb($Url) {
137
-    return (strpos($Url, '_thumb') !== false);
138
-  }
139
-
140
-  /**
141
-   * Cleans up imgur URL if it already has a modifier attached to the end of it.
142
-   */
143
-  private static function clean_imgur_url($Url) {
144
-    $Extension = pathinfo($Url, PATHINFO_EXTENSION);
145
-    $Full = preg_replace('/\.[^.]*$/', '', $Url);
146
-    $Base = substr($Full, 0, strrpos($Full, '/'));
147
-    $Path = substr($Full, strrpos($Full, '/') + 1);
148
-    if (strlen($Path) == 6) {
149
-      $Last = $Path[strlen($Path) - 1];
150
-      if ($Last == 'm' || $Last == 'l' || $Last == 's' || $Last == 'h' || $Last == 'b') {
151
-        $Path = substr($Path, 0, -1);
152
-      }
153
-    }
154
-    return "$Base/$Path.$Extension";
155
-  }
156
-
157
-  /**
158
-   * Replaces the extension.
159
-   */
160
-  private static function replace_extension($String, $Extension) {
161
-    return preg_replace('/\.[^.]*$/', $Extension, $String);
162
-  }
163
 
8
 
164
   /**
9
   /**
165
    * Create image proxy URL
10
    * Create image proxy URL
166
    * @param string $Url image URL
11
    * @param string $Url image URL
167
-   * @param bool/string $CheckSize - accepts one of false, "avatar", "avatar2", or "donoricon"
168
-   * @param bool/string/number $UserID - user ID for avatars and donor icons
169
    * @return image proxy URL
12
    * @return image proxy URL
170
    */
13
    */
171
-  public static function proxy_url($Url, $CheckSize, $UserID, &$ExtraInfo) {
172
-    if ($UserID) {
173
-      $ExtraInfo = "&userid=$UserID";
174
-      if ($CheckSize === 'avatar' && !isset(self::$CheckedAvatars[$UserID])) {
175
-        $ExtraInfo .= "&type=$CheckSize";
176
-        self::$CheckedAvatars[$UserID] = true;
177
-      } elseif ($CheckSize === 'avatar2' && !isset(self::$CheckedAvatars2[$UserID])) {
178
-        $ExtraInfo .= "&type=$CheckSize";
179
-        self::$CheckedAvatars2[$UserID] = true;
180
-      } elseif ($CheckSize === 'donoricon' && !isset(self::$CheckedDonorIcons[$UserID])) {
181
-        $ExtraInfo .= "&type=$CheckSize";
182
-        self::$CheckedDonorIcons[$UserID] = true;
183
-      }
184
-    }
185
-
14
+  public static function proxy_url($Url) {
186
     if (preg_match('/^https:\/\/('.SITE_DOMAIN.'|'.IMAGE_DOMAIN.')\//', $Url) || $Url[0]=='/') {
15
     if (preg_match('/^https:\/\/('.SITE_DOMAIN.'|'.IMAGE_DOMAIN.')\//', $Url) || $Url[0]=='/') {
187
       if (strpos($Url, '?') === false) $Url .= '?';
16
       if (strpos($Url, '?') === false) $Url .= '?';
188
       return $Url;
17
       return $Url;
195
    * Determine the image URL. This takes care of the image proxy and thumbnailing.
24
    * Determine the image URL. This takes care of the image proxy and thumbnailing.
196
    * @param string $Url
25
    * @param string $Url
197
    * @param bool $Thumb
26
    * @param bool $Thumb
198
-   * @param bool/string $CheckSize - accepts one of false, "avatar", "avatar2", or "donoricon"
199
-   * @param bool/string/number $UserID - user ID for avatars and donor icons
200
    * @return string
27
    * @return string
201
    */
28
    */
202
-  public static function process($Url, $Thumb = false, $CheckSize = false, $UserID = false) {
203
-    if (empty($Url)) {
204
-      return '';
205
-    }
206
-
207
-    if ($Found = self::get_stored($Url . ($Thumb ? '_thumb' : ''))) {
208
-      return $Found;
209
-    }
210
-
211
-    $ProcessedUrl = $Url;
212
-    if ($Thumb) {
213
-      $Extension = pathinfo($Url, PATHINFO_EXTENSION);
214
-      if (self::thumbnailable($Url) && self::valid_extension($Extension)) {
215
-        if (strpos($Url, 'whatimg') !== false && !self::has_whatimg_thumb($Url)) {
216
-          $ProcessedUrl = self::replace_extension($Url, '_thumb.' . $Extension);
217
-        } elseif (strpos($Url, 'imgur') !== false) {
218
-          $ProcessedUrl = self::replace_extension(self::clean_imgur_url($Url), 'm.' . $Extension);
219
-        }
220
-      }
221
-    }
222
-
223
-    $ExtraInfo = '';
224
-    if (check_perms('site_proxy_images')) {
225
-      $ProcessedUrl = self::proxy_url($ProcessedUrl, $CheckSize, $UserID, $ExtraInfo);
226
-    }
227
-    self::store($Url . ($Thumb ? '_thumb' : ''), $ProcessedUrl);
228
-    return $ProcessedUrl . $ExtraInfo;
29
+  public static function process($Url = '', $Thumb = false) {
30
+    // TODO: Thumbnailing
31
+    return $Url ? self::proxy_url($Url) : '';
229
   }
32
   }
230
 
33
 
231
   /**
34
   /**
232
-   * Cover art thumbnail in browse, on artist pages etc.
233
-   * @global array $CategoryIcons
234
-   * @param string $Url
235
-   * @param int $CategoryID
35
+   * Checks if a link's host is (not) good, otherwise displays an error.
36
+   * @param string $Url Link to an image
37
+   * @return boolean
236
    */
38
    */
237
-  public static function cover_thumb($Url, $CategoryID) {
238
-    global $CategoryIcons;
239
-    if ($Url) {
240
-      $Src = self::process($Url, true);
241
-      $Lightbox = self::process($Url);
242
-    } else {
243
-      $Src = STATIC_SERVER . 'common/noartwork/' . $CategoryIcons[$CategoryID - 1];
244
-      $Lightbox = $Src;
39
+  public static function blacklisted($Url, $ShowError = true) {
40
+    $Blacklist = ['tinypic.com'];
41
+    foreach ($Blacklist as $Value) {
42
+      if (stripos($Url, $Value) !== false) {
43
+        if ($ShowError) {
44
+          error($Value . ' is not an allowed image host. Please use a different host.');
45
+        }
46
+        return true;
47
+      }
245
     }
48
     }
246
-?>
247
-    <img src="<?=$Src?>" width="90" height="90" alt="Cover" class="lightbox-init" lightbox-img="<?=$Lightbox?>" />
248
-<?
49
+    return false;
249
   }
50
   }
250
 }
51
 }

+ 3
- 3
classes/users.class.php View File

412
           $IconLink = display_str($DonorRewards['CustomIconLink']);
412
           $IconLink = display_str($DonorRewards['CustomIconLink']);
413
         }
413
         }
414
         if ($EnabledRewards['HasCustomDonorIcon'] && !empty($DonorRewards['CustomIcon'])) {
414
         if ($EnabledRewards['HasCustomDonorIcon'] && !empty($DonorRewards['CustomIcon'])) {
415
-          $IconImage = ImageTools::process($DonorRewards['CustomIcon'], false, 'donoricon', $UserID);
415
+          $IconImage = ImageTools::process($DonorRewards['CustomIcon']);
416
         } else {
416
         } else {
417
           if ($SpecialRank === MAX_SPECIAL_RANK) {
417
           if ($SpecialRank === MAX_SPECIAL_RANK) {
418
             $DonorHeart = 6;
418
             $DonorHeart = 6;
521
    * @return string
521
    * @return string
522
    */
522
    */
523
   public static function show_avatar($Avatar, $UserID, $Username, $Setting, $Size = 150, $ReturnHTML = true) {
523
   public static function show_avatar($Avatar, $UserID, $Username, $Setting, $Size = 150, $ReturnHTML = true) {
524
-    $Avatar = ImageTools::process($Avatar, false, 'avatar', $UserID);
524
+    $Avatar = ImageTools::process($Avatar);
525
     $Style = 'style="max-height: 400px;"';
525
     $Style = 'style="max-height: 400px;"';
526
     $AvatarMouseOverText = '';
526
     $AvatarMouseOverText = '';
527
     $SecondAvatar = '';
527
     $SecondAvatar = '';
538
       $AvatarMouseOverText = "alt=\"$Username's avatar\"";
538
       $AvatarMouseOverText = "alt=\"$Username's avatar\"";
539
     }
539
     }
540
     if ($EnabledRewards['HasSecondAvatar'] && !empty($Rewards['SecondAvatar'])) {
540
     if ($EnabledRewards['HasSecondAvatar'] && !empty($Rewards['SecondAvatar'])) {
541
-      $SecondAvatar = ' data-gazelle-second-avatar="' . ImageTools::process($Rewards['SecondAvatar'], false, 'avatar2', $UserID) . '"';
541
+      $SecondAvatar = ' data-gazelle-second-avatar="' . ImageTools::process($Rewards['SecondAvatar']) . '"';
542
     }
542
     }
543
     // case 1 is avatars disabled
543
     // case 1 is avatars disabled
544
     switch ($Setting) {
544
     switch ($Setting) {

+ 1
- 1
sections/index/feat_album.php View File

24
       </div>
24
       </div>
25
       <div class="center pad">
25
       <div class="center pad">
26
         <a href="torrents.php?id=<?=$FeaturedAlbum['GroupID']?>" class="tooltip" title="<?=Artists::display_artists($Artists, false, false)?> - <?=$FeaturedAlbum['Name']?>">
26
         <a href="torrents.php?id=<?=$FeaturedAlbum['GroupID']?>" class="tooltip" title="<?=Artists::display_artists($Artists, false, false)?> - <?=$FeaturedAlbum['Name']?>">
27
-          <img src="<?=ImageTools::process($FeaturedAlbum['WikiImage'], true)?>" alt="<?=Artists::display_artists($Artists, false, false)?> - <?=$FeaturedAlbum['Name']?>" width="100%" />
27
+          <img src="<?=ImageTools::process($FeaturedAlbum['WikiImage'])?>" alt="<?=Artists::display_artists($Artists, false, false)?> - <?=$FeaturedAlbum['Name']?>" width="100%" />
28
         </a>
28
         </a>
29
       </div>
29
       </div>
30
       <div class="center pad">
30
       <div class="center pad">

+ 1
- 1
sections/tools/finances/donor_rewards.php View File

80
       </td>
80
       </td>
81
       <td style="word-wrap: break-word;">
81
       <td style="word-wrap: break-word;">
82
 <?    if (!empty($User['CustomIcon'])) { ?>
82
 <?    if (!empty($User['CustomIcon'])) { ?>
83
-        <img src="<?=ImageTools::process($User['CustomIcon'], false, 'donoricon', $User['UserID'])?>" width="15" height="13" alt="" />
83
+        <img src="<?=ImageTools::process($User['CustomIcon'])?>" width="15" height="13" alt="" />
84
 <?    } ?>
84
 <?    } ?>
85
       </td>
85
       </td>
86
       <td style="word-wrap: break-word;">
86
       <td style="word-wrap: break-word;">

+ 1
- 1
sections/torrents/add_cover_art.php View File

24
   $Image = $Images[$i];
24
   $Image = $Images[$i];
25
   $Summary = $Summaries[$i];
25
   $Summary = $Summaries[$i];
26
 
26
 
27
-  if (ImageTools::blacklisted($Image, true) || !preg_match("/^".IMAGE_REGEX."$/i", $Image)) {
27
+  if (ImageTools::blacklisted($Image) || !preg_match("/^".IMAGE_REGEX."$/i", $Image)) {
28
     continue;
28
     continue;
29
   }
29
   }
30
 
30
 

+ 1
- 1
sections/torrents/details.php View File

777
       <div class="body torrent_screenshots hidden">
777
       <div class="body torrent_screenshots hidden">
778
 <?
778
 <?
779
     foreach($Screenshots as $Screenshot) {
779
     foreach($Screenshots as $Screenshot) {
780
-      $SSURL = ImageTools::process($Screenshot['Image'], false);
780
+      $SSURL = ImageTools::process($Screenshot['Image']);
781
       if (check_perms('users_mod')) {
781
       if (check_perms('users_mod')) {
782
         ?><img class='tooltip lightbox-init' title='<?=Users::format_username($Screenshot['UserID'], false, false, false)?> - <?=time_diff($Screenshot['Time'])?>' src="<?=$SSURL?>" /><?
782
         ?><img class='tooltip lightbox-init' title='<?=Users::format_username($Screenshot['UserID'], false, false, false)?> - <?=time_diff($Screenshot['Time'])?>' src="<?=$SSURL?>" /><?
783
       } else {
783
       } else {

Loading…
Cancel
Save