This repository was archived by the owner on Mar 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions_convert.php
2479 lines (2093 loc) · 60.7 KB
/
functions_convert.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
*
* @package install
* @version $Id$
* @copyright (c) 2006 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Default avatar width/height
* @ignore
*/
define('DEFAULT_AVATAR_X', 80);
define('DEFAULT_AVATAR_Y', 80);
// Global functions - all functions can be used by convertors
// SIMPLE FUNCTIONS
/**
* Return the preceding value
*/
function dec($var)
{
return --$var;
}
/**
* Return the next value
*/
function inc($var)
{
return ++$var;
}
/**
* Return whether the value is positive
*/
function is_positive($n)
{
return ($n > 0) ? 1 : 0;
}
/**
* Boolean inverse of the value
*/
function not($var)
{
return ($var) ? 0 : 1;
}
/**
* Convert a textual value to it's equivalent boolean value
*
* @param string $str String to convert (converts yes, on, y, 1 and true to boolean true)
* @return boolean The equivalent value
*/
function str_to_bool($str)
{
$str = strtolower($str);
return ($str == 'yes' || $str == 'on' || $str == 'y' || $str == 'true' || $str == '1') ? true : false;
}
/**
* Function to mimic php's empty() function (it is the same)
*/
function is_empty($mixed)
{
return empty($mixed);
}
/**
* Convert the name of a user's primary group to the appropriate equivalent phpBB group id
*
* @param string $status The name of the group
* @return int The group_id corresponding to the equivalent group
*/
function str_to_primary_group($status)
{
switch (ucfirst(strtolower($status)))
{
case 'Administrator':
return get_group_id('administrators');
break;
case 'Super moderator':
case 'Global moderator':
case 'Moderator':
return get_group_id('global_moderators');
break;
case 'Guest':
case 'Anonymous':
return get_group_id('guests');
break;
default:
return get_group_id('registered');
break;
}
}
/**
* Convert a boolean into the appropriate phpBB constant indicating whether the item is locked
*/
function is_item_locked($bool)
{
return ($bool) ? ITEM_LOCKED : ITEM_UNLOCKED;
}
/**
* Convert a value from days to seconds
*/
function days_to_seconds($days)
{
return ($days * 86400);
}
/**
* Determine whether a user is anonymous and return the appropriate new user_id
*/
function is_user_anonymous($user_id)
{
return ($user_id > ANONYMOUS) ? $user_id : ANONYMOUS;
}
/**
* Generate a key value based on existing values
*
* @param int $pad Amount to add to the maximum value
* @return int Key value
*/
function auto_id($pad = 0)
{
global $auto_id, $convert_row;
if (!empty($convert_row['max_id']))
{
return $convert_row['max_id'] + $pad;
}
return $auto_id + $pad;
}
/**
* Convert a boolean into the appropriate phpBB constant indicating whether the user is active
*/
function set_user_type($user_active)
{
return ($user_active) ? USER_NORMAL : USER_INACTIVE;
}
/**
* Convert a value from minutes to hours
*/
function minutes_to_hours($minutes)
{
return ($minutes / 3600);
}
/**
* Return the group_id for a given group name
*/
function get_group_id($group_name)
{
global $db, $group_mapping;
if (empty($group_mapping))
{
$sql = 'SELECT group_name, group_id
FROM ' . GROUPS_TABLE;
$result = $db->sql_query($sql);
$group_mapping = array();
while ($row = $db->sql_fetchrow($result))
{
$group_mapping[strtoupper($row['group_name'])] = (int) $row['group_id'];
}
$db->sql_freeresult($result);
}
if (!sizeof($group_mapping))
{
add_default_groups();
return get_group_id($group_name);
}
if (isset($group_mapping[strtoupper($group_name)]))
{
return $group_mapping[strtoupper($group_name)];
}
return $group_mapping['REGISTERED'];
}
/**
* Generate the email hash stored in the users table
*
* Note: Deprecated, calls should directly go to phpbb_email_hash()
*/
function gen_email_hash($email)
{
return phpbb_email_hash($email);
}
/**
* Convert a boolean into the appropriate phpBB constant indicating whether the topic is locked
*/
function is_topic_locked($bool)
{
return (!empty($bool)) ? ITEM_LOCKED : ITEM_UNLOCKED;
}
/**
* Generate a bbcode_uid value
*/
function make_uid($timestamp)
{
static $last_timestamp, $last_uid;
if (empty($last_timestamp) || $timestamp != $last_timestamp)
{
$last_uid = substr(base_convert(unique_id(), 16, 36), 0, BBCODE_UID_LEN);
}
$last_timestamp = $timestamp;
return $last_uid;
}
/**
* Validate a website address
*/
function validate_website($url)
{
if ($url === 'http://')
{
return '';
}
else if (!preg_match('#^[a-z0-9]+://#i', $url) && strlen($url) > 0)
{
return 'http://' . $url;
}
return $url;
}
/**
* Convert nulls to zeros for fields which allowed a NULL value in the source but not the destination
*/
function null_to_zero($value)
{
return ($value === NULL) ? 0 : $value;
}
/**
* Convert nulls to empty strings for fields which allowed a NULL value in the source but not the destination
*/
function null_to_str($value)
{
return ($value === NULL) ? '' : $value;
}
// EXTENDED FUNCTIONS
/**
* Get old config value
*/
function get_config_value($config_name)
{
static $convert_config;
if (!isset($convert_config))
{
$convert_config = get_config();
}
if (!isset($convert_config[$config_name]))
{
return false;
}
return (empty($convert_config[$config_name])) ? '' : $convert_config[$config_name];
}
/**
* Convert an IP address from the hexadecimal notation to normal dotted-quad notation
*/
function decode_ip($int_ip)
{
if (!$int_ip)
{
return $int_ip;
}
$hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
// Any mod changing the way ips are stored? Then we are not able to convert and enter the ip "as is" to not "destroy" anything...
if (sizeof($hexipbang) < 4)
{
return $int_ip;
}
return hexdec($hexipbang[0]) . '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]);
}
/**
* Reverse the encoding of wild-carded bans
*/
function decode_ban_ip($int_ip)
{
return str_replace('255', '*', decode_ip($int_ip));
}
/**
* Determine the MIME-type of a specified filename
* This does not actually inspect the file, but simply uses the file extension
*/
function mimetype($filename)
{
if (!preg_match('/\.([a-z0-9]+)$/i', $filename, $m))
{
return 'application/octet-stream';
}
switch (strtolower($m[1]))
{
case 'zip': return 'application/zip';
case 'jpeg': return 'image/jpeg';
case 'jpg': return 'image/jpeg';
case 'jpe': return 'image/jpeg';
case 'png': return 'image/png';
case 'gif': return 'image/gif';
case 'htm':
case 'html': return 'text/html';
case 'tif': return 'image/tiff';
case 'tiff': return 'image/tiff';
case 'ras': return 'image/x-cmu-raster';
case 'pnm': return 'image/x-portable-anymap';
case 'pbm': return 'image/x-portable-bitmap';
case 'pgm': return 'image/x-portable-graymap';
case 'ppm': return 'image/x-portable-pixmap';
case 'rgb': return 'image/x-rgb';
case 'xbm': return 'image/x-xbitmap';
case 'xpm': return 'image/x-xpixmap';
case 'xwd': return 'image/x-xwindowdump';
case 'z': return 'application/x-compress';
case 'gtar': return 'application/x-gtar';
case 'tgz': return 'application/x-gtar';
case 'gz': return 'application/x-gzip';
case 'tar': return 'application/x-tar';
case 'xls': return 'application/excel';
case 'pdf': return 'application/pdf';
case 'ppt': return 'application/powerpoint';
case 'rm': return 'application/vnd.rn-realmedia';
case 'wma': return 'audio/x-ms-wma';
case 'swf': return 'application/x-shockwave-flash';
case 'ief': return 'image/ief';
case 'doc':
case 'dot':
case 'wrd': return 'application/msword';
case 'ai':
case 'eps':
case 'ps': return 'application/postscript';
case 'asc':
case 'txt':
case 'c':
case 'cc':
case 'h':
case 'hh':
case 'cpp':
case 'hpp':
case 'php':
case 'php3': return 'text/plain';
default: return 'application/octet-stream';
}
}
/**
* Obtain the dimensions of all remotely hosted avatars
* This should only be called from execute_last
* There can be significant network overhead if there are a large number of remote avatars
* @todo Look at the option of allowing the user to decide whether this is called or to force the dimensions
*/
function remote_avatar_dims()
{
global $db;
$sql = 'SELECT user_id, user_avatar
FROM ' . USERS_TABLE . '
WHERE user_avatar_type = ' . AVATAR_REMOTE;
$result = $db->sql_query($sql);
$remote_avatars = array();
while ($row = $db->sql_fetchrow($result))
{
$remote_avatars[(int) $row['user_id']] = $row['user_avatar'];
}
$db->sql_freeresult($result);
foreach ($remote_avatars as $user_id => $avatar)
{
$width = (int) get_remote_avatar_dim($avatar, 0);
$height = (int) get_remote_avatar_dim($avatar, 1);
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_avatar_width = ' . (int) $width . ', user_avatar_height = ' . (int) $height . '
WHERE user_id = ' . $user_id;
$db->sql_query($sql);
}
}
function import_avatar_gallery($gallery_name = '', $subdirs_as_galleries = false)
{
global $config, $convert, $phpbb_root_path, $user;
$relative_path = empty($convert->convertor['source_path_absolute']);
// check for trailing slash
if (rtrim($convert->convertor['avatar_gallery_path'], '/') === '')
{
$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_GALLERY_PATH'], 'import_avatar_gallery()'), __LINE__, __FILE__);
}
$src_path = relative_base(path($convert->convertor['avatar_gallery_path'], $relative_path), $relative_path);
if (is_dir($src_path))
{
// Do not die on failure... safe mode restrictions may be in effect.
copy_dir($convert->convertor['avatar_gallery_path'], path($config['avatar_gallery_path']) . $gallery_name, !$subdirs_as_galleries, false, false, $relative_path);
// only doing 1 level deep. (ibf 1.x)
// notes: ibf has 2 tiers: directly in the avatar directory for base gallery (handled in the above statement), plus subdirs(handled below).
// recursive subdirs ignored. -- i don't know if other forums support recursive galleries. if they do, this following code could be upgraded to be recursive.
if ($subdirs_as_galleries)
{
$dirlist = array();
if ($handle = @opendir($src_path))
{
while ($entry = readdir($handle))
{
if ($entry[0] == '.' || $entry == 'CVS' || $entry == 'index.htm')
{
continue;
}
if (is_dir($src_path . $entry))
{
$dirlist[] = $entry;
}
}
closedir($handle);
}
else if ($dir = @dir($src_path))
{
while ($entry = $dir->read())
{
if ($entry[0] == '.' || $entry == 'CVS' || $entry == 'index.htm')
{
continue;
}
if (is_dir($src_path . $entry))
{
$dirlist[] = $entry;
}
}
$dir->close();
}
for ($i = 0; $i < sizeof($dirlist); ++$i)
{
$dir = $dirlist[$i];
// Do not die on failure... safe mode restrictions may be in effect.
copy_dir(path($convert->convertor['avatar_gallery_path'], $relative_path) . $dir, path($config['avatar_gallery_path']) . $dir, true, false, false, $relative_path);
}
}
}
}
function import_attachment_files($category_name = '')
{
global $config, $convert, $phpbb_root_path, $db, $user;
$sql = 'SELECT config_value AS upload_path
FROM ' . CONFIG_TABLE . "
WHERE config_name = 'upload_path'";
$result = $db->sql_query($sql);
$config['upload_path'] = $db->sql_fetchfield('upload_path');
$db->sql_freeresult($result);
$relative_path = empty($convert->convertor['source_path_absolute']);
if (empty($convert->convertor['upload_path']))
{
$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_UPLOAD_DIR'], 'import_attachment_files()'), __LINE__, __FILE__);
}
if (is_dir(relative_base(path($convert->convertor['upload_path'], $relative_path), $relative_path)))
{
copy_dir($convert->convertor['upload_path'], path($config['upload_path']) . $category_name, true, false, true, $relative_path);
}
}
function attachment_forum_perms($forum_id)
{
if (!is_array($forum_id))
{
$forum_id = array($forum_id);
}
return serialize($forum_id);
}
// base64todec function
// -> from php manual?
function base64_unpack($string)
{
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-';
$base = strlen($chars);
$length = strlen($string);
$number = 0;
for ($i = 1; $i <= $length; $i++)
{
$pos = $length - $i;
$operand = strpos($chars, substr($string, $pos, 1));
$exponent = pow($base, $i-1);
$dec_value = $operand * $exponent;
$number += $dec_value;
}
return $number;
}
function _import_check($config_var, $source, $use_target)
{
global $convert, $config;
$result = array(
'orig_source' => $source,
'copied' => false,
'relative_path' => (empty($convert->convertor['source_path_absolute'])) ? true : false,
);
// copy file will prepend $phpBB_root_path
$target = $config[$config_var] . '/' . utf8_basename(($use_target === false) ? $source : $use_target);
if (!empty($convert->convertor[$config_var]) && strpos($source, $convert->convertor[$config_var]) !== 0)
{
$source = $convert->convertor[$config_var] . $source;
}
$result['source'] = $source;
if (file_exists(relative_base($source, $result['relative_path'], __LINE__, __FILE__)))
{
$result['copied'] = copy_file($source, $target, false, false, $result['relative_path']);
}
if ($result['copied'])
{
$result['target'] = utf8_basename($target);
}
else
{
$result['target'] = ($use_target !== false) ? $result['orig_source'] : utf8_basename($target);
}
return $result;
}
function import_attachment($source, $use_target = false)
{
if (empty($source))
{
return '';
}
global $convert, $phpbb_root_path, $config, $user;
// check for trailing slash
if (rtrim($convert->convertor['upload_path'], '/') === '')
{
$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_UPLOAD_DIR'], 'import_attachment()'), __LINE__, __FILE__);
}
$result = _import_check('upload_path', $source, $use_target);
if ($result['copied'])
{
// Thumbnails?
if (is_array($convert->convertor['thumbnails']))
{
$thumb_dir = $convert->convertor['thumbnails'][0];
$thumb_prefix = $convert->convertor['thumbnails'][1];
$thumb_source = $thumb_dir . $thumb_prefix . utf8_basename($result['source']);
if (strpos($thumb_source, $convert->convertor['upload_path']) !== 0)
{
$thumb_source = $convert->convertor['upload_path'] . $thumb_source;
}
$thumb_target = $config['upload_path'] . '/thumb_' . $result['target'];
if (file_exists(relative_base($thumb_source, $result['relative_path'], __LINE__, __FILE__)))
{
copy_file($thumb_source, $thumb_target, false, false, $result['relative_path']);
}
}
}
return $result['target'];
}
function import_rank($source, $use_target = false)
{
if (empty($source))
{
return '';
}
global $convert, $phpbb_root_path, $config, $user;
if (!isset($convert->convertor['ranks_path']))
{
$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_RANKS_PATH'], 'import_rank()'), __LINE__, __FILE__);
}
$result = _import_check('ranks_path', $source, $use_target);
return $result['target'];
}
function import_smiley($source, $use_target = false)
{
if (empty($source))
{
return '';
}
global $convert, $phpbb_root_path, $config, $user;
// check for trailing slash
if (rtrim($convert->convertor['smilies_path'], '/') === '')
{
$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_SMILIES_PATH'], 'import_smiley()'), __LINE__, __FILE__);
}
$result = _import_check('smilies_path', $source, $use_target);
return $result['target'];
}
/*
*/
function import_avatar($source, $use_target = false, $user_id = false)
{
if (empty($source) || preg_match('#^https?:#i', $source) || preg_match('#blank\.(gif|png)$#i', $source))
{
return;
}
global $convert, $phpbb_root_path, $config, $user;
// check for trailing slash
if (rtrim($convert->convertor['avatar_path'], '/') === '')
{
$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_AVATAR_PATH'], 'import_avatar()'), __LINE__, __FILE__);
}
if ($use_target === false && $user_id !== false)
{
$use_target = $config['avatar_salt'] . '_' . $user_id . '.' . substr(strrchr($source, '.'), 1);
}
$result = _import_check('avatar_path', $source, $use_target);
return ((!empty($user_id)) ? $user_id : $use_target) . '.' . substr(strrchr($source, '.'), 1);
}
/**
* @todo all image dimension functions below (there are a *lot*) should get revisited and converted to one or two functions (no more needed, really).
*/
/**
* Calculate the size of the specified image
* Called from the following functions for calculating the size of specific image types
*/
function get_image_dim($source)
{
if (empty($source))
{
return array(0, 0);
}
global $convert;
$relative_path = empty($convert->convertor['source_path_absolute']);
if (file_exists(relative_base($source, $relative_path)))
{
$image = relative_base($source, $relative_path);
return @getimagesize($image);
}
return false;
}
/**
* Obtain the width of the specified smilie
*/
function get_smiley_width($src)
{
return get_smiley_dim($src, 0);
}
/**
* Obtain the height of the specified smilie
*/
function get_smiley_height($src)
{
return get_smiley_dim($src, 1);
}
/**
* Obtain the size of the specified smilie (using the cache if possible) and cache the value
*/
function get_smiley_dim($source, $axis)
{
if (empty($source))
{
return 15;
}
static $smiley_cache = array();
if (isset($smiley_cache[$source]))
{
return $smiley_cache[$source][$axis];
}
global $convert, $phpbb_root_path, $config, $user;
$orig_source = $source;
if (!isset($convert->convertor['smilies_path']))
{
$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_SMILIES_PATH'], 'get_smiley_dim()'), __LINE__, __FILE__);
}
if (!empty($convert->convertor['smilies_path']) && strpos($source, $convert->convertor['smilies_path']) !== 0)
{
$source = $convert->convertor['smilies_path'] . $source;
}
$smiley_cache[$orig_source] = get_image_dim($source);
if (empty($smiley_cache[$orig_source]) || empty($smiley_cache[$orig_source][0]) || empty($smiley_cache[$orig_source][1]))
{
$smiley_cache[$orig_source] = array(15, 15);
return 15;
}
return $smiley_cache[$orig_source][$axis];
}
/**
* Obtain the width of the specified avatar
*/
function get_avatar_width($src, $func = false, $arg1 = false, $arg2 = false)
{
return get_avatar_dim($src, 0, $func, $arg1, $arg2);
}
/**
* Obtain the height of the specified avatar
*/
function get_avatar_height($src, $func = false, $arg1 = false, $arg2 = false)
{
return get_avatar_dim($src, 1, $func, $arg1, $arg2);
}
/**
*/
function get_avatar_dim($src, $axis, $func = false, $arg1 = false, $arg2 = false)
{
$avatar_type = AVATAR_UPLOAD;
if ($func)
{
if ($arg1 || $arg2)
{
$ary = array($arg1);
if ($arg2)
{
$ary[] = $arg2;
}
$avatar_type = call_user_func_array($func, $ary);
}
else
{
$avatar_type = call_user_func($func);
}
}
switch ($avatar_type)
{
case AVATAR_UPLOAD:
return get_upload_avatar_dim($src, $axis);
break;
case AVATAR_GALLERY:
return get_gallery_avatar_dim($src, $axis);
break;
case AVATAR_REMOTE:
// see notes on this functions usage and (hopefully) model $func to avoid this accordingly
return get_remote_avatar_dim($src, $axis);
break;
default:
$default_x = (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
$default_y = (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
return $axis ? $default_y : $default_x;
break;
}
}
/**
* Obtain the size of the specified uploaded avatar (using the cache if possible) and cache the value
*/
function get_upload_avatar_dim($source, $axis)
{
static $cachedims = false;
static $cachekey = false;
if (empty($source))
{
return 0;
}
if ($cachekey == $source)
{
return $cachedims[$axis];
}
$orig_source = $source;
if (substr($source, 0, 7) == 'upload:')
{
$source = substr($source, 7);
}
global $convert, $phpbb_root_path, $config, $user;
if (!isset($convert->convertor['avatar_path']))
{
$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_AVATAR_PATH'], 'get_upload_avatar_dim()'), __LINE__, __FILE__);
}
if (!empty($convert->convertor['avatar_path']) && strpos($source, $convert->convertor['avatar_path']) !== 0)
{
$source = path($convert->convertor['avatar_path'], empty($convert->convertor['source_path_absolute'])) . $source;
}
$cachedims = get_image_dim($source);
if (empty($cachedims) || empty($cachedims[0]) || empty($cachedims[1]))
{
$default_x = (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
$default_y = (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
$cachedims = array($default_x, $default_y);
}
return $cachedims[$axis];
}
/**
* Obtain the size of the specified gallery avatar (using the cache if possible) and cache the value
*/
function get_gallery_avatar_dim($source, $axis)
{
if (empty($source))
{
return 0;
}
static $avatar_cache = array();
if (isset($avatar_cache[$source]))
{
return $avatar_cache[$source][$axis];
}
global $convert, $phpbb_root_path, $config, $user;
$orig_source = $source;
if (!isset($convert->convertor['avatar_gallery_path']))
{
$convert->p_master->error(sprintf($user->lang['CONV_ERROR_NO_GALLERY_PATH'], 'get_gallery_avatar_dim()'), __LINE__, __FILE__);
}
if (!empty($convert->convertor['avatar_gallery_path']) && strpos($source, $convert->convertor['avatar_gallery_path']) !== 0)
{
$source = path($convert->convertor['avatar_gallery_path'], empty($convert->convertor['source_path_absolute'])) . $source;
}
$avatar_cache[$orig_source] = get_image_dim($source);
if (empty($avatar_cache[$orig_source]) || empty($avatar_cache[$orig_source][0]) || empty($avatar_cache[$orig_source][1]))
{
$default_x = (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
$default_y = (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
$avatar_cache[$orig_source] = array($default_x, $default_y);
}
return $avatar_cache[$orig_source][$axis];
}
/**
* Obtain the size of the specified remote avatar (using the cache if possible) and cache the value
* Whilst it's unlikely that remote avatars will be duplicated, it is possible so caching seems the best option
* This should only be called from a post processing step due to the possibility of network timeouts
*/
function get_remote_avatar_dim($src, $axis)
{
if (empty($src))
{
return 0;
}
static $remote_avatar_cache = array();
// an ugly hack: we assume that the dimensions of each remote avatar are accessed exactly twice (x and y)
if (isset($remote_avatar_cache[$src]))
{
$retval = $remote_avatar_cache[$src][$axis];
unset($remote_avatar_cache);
return $retval;
}
$url_info = @parse_url($src);
if (empty($url_info['host']))
{
return 0;
}
$host = $url_info['host'];
$port = (isset($url_info['port'])) ? $url_info['port'] : 0;
$protocol = (isset($url_info['scheme'])) ? $url_info['scheme'] : 'http';
if (empty($port))
{
switch(strtolower($protocol))
{
case 'ftp':
$port = 21;
break;
case 'https':
$port = 443;
break;
default:
$port = 80;
}
}
$timeout = @ini_get('default_socket_timeout');
@ini_set('default_socket_timeout', 2);
// We're just trying to reach the server to avoid timeouts
$fp = @fsockopen($host, $port, $errno, $errstr, 1);
if ($fp)
{
$remote_avatar_cache[$src] = @getimagesize($src);
fclose($fp);
}
$default_x = (defined('DEFAULT_AVATAR_X_CUSTOM')) ? DEFAULT_AVATAR_X_CUSTOM : DEFAULT_AVATAR_X;
$default_y = (defined('DEFAULT_AVATAR_Y_CUSTOM')) ? DEFAULT_AVATAR_Y_CUSTOM : DEFAULT_AVATAR_Y;
$default = array($default_x, $default_y);
if (empty($remote_avatar_cache[$src]) || empty($remote_avatar_cache[$src][0]) || empty($remote_avatar_cache[$src][1]))
{
$remote_avatar_cache[$src] = $default;
}
else