/ lib / util.php
util.php
  1  <?
  2  
  3  // Linker for boards since we need to know the subdomain
  4  final class L {
  5    static $nws = array('aco'=>true,'b'=>true,'bant'=>true,'d'=>true,'e'=>true,'f'=>true,'gif'=>true,'h'=>true,'hc'=>true,'hm'=>true,'hr'=>true,'i'=>true,'ic'=>true,'pol'=>true,'r'=>true,'r9k'=>true,'s'=>true,'s4s'=>true,'soc'=>true,'t'=>true,'trash'=>true,'u'=>true,'wg'=>true,'y'=>true);
  6    
  7    private static $blue = '4chan.org'; // Domain for worksafe boards
  8    private static $red = '4chan.org'; // Domain for nws boards
  9    
 10    static public function d($board) {
 11      return isset(self::$nws[$board]) ? self::$red : self::$blue;
 12    }
 13  }
 14  
 15  // FIXME ipv6
 16  function cidrtest ($longip, $CIDR) {
 17  	list ($net, $mask) = explode("/", $CIDR);
 18  	$mask = (int)$mask;
 19  	
 20  	if (!$mask) return false;
 21  
 22  	$ip_net = ip2long ($net);
 23  	
 24  	if (!$ip_net) return false;
 25  	
 26  	$ip_mask = ~((1 << (32 - $mask)) - 1);
 27  
 28  	$ip_ip = $longip;
 29  
 30  	$ip_ip_net = $ip_ip & $ip_mask;
 31  
 32  	return ($ip_ip_net == $ip_net);
 33  }
 34  
 35  function internal_error_log($cat, $err, $trace=true) {
 36  	$err = sprintf("[%s error] %s", $cat, $err);
 37  
 38  	error_log($err);
 39  	if ($trace) {
 40  	ob_start();
 41  	debug_print_backtrace();
 42  	error_log(ob_get_contents());
 43  	ob_end_clean();
 44  	}
 45  }
 46  
 47  function quick_log_to( $f, $s, $do_bt=false )
 48  {
 49  	if ($do_bt) {
 50  	ob_start();
 51  	debug_print_backtrace();
 52  	$bt = ob_get_contents();
 53  	ob_end_clean();
 54  	}
 55  	
 56  	$out = ($do_bt ? $bt : date("r")." ".$s)."\n\n";
 57  
 58  	$h = fopen( $f, "a" );
 59  	flock( $h, LOCK_EX );
 60  	fwrite( $h, $out);
 61  	fclose( $h );
 62  }
 63  
 64  function post_filter_get($base)
 65  {
 66  	$path = "/www/global/yotsuba/filters/";
 67  
 68  	$strs = @file("$path$base.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
 69  	$res  = @file("$path$base-re.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
 70  
 71  	if (!is_array($strs)) $strs = array();
 72  	if (!is_array($res)) $res = array();
 73  
 74  	return array($strs, $res);
 75  }
 76  
 77  function find_ipxff_in($longip,$longxff,$ips)
 78  {
 79  	$badip = false;
 80  
 81  	foreach($ips as $cidr)
 82  	{
 83  		if ($cidr[0] == '#' || !$cidr) continue;
 84  
 85  		if(cidrtest($longip, $cidr) || cidrtest($longxff, $cidr))
 86  		{
 87  			$badip = true;
 88  			break;
 89  		}
 90  	}
 91  
 92  	return $badip;
 93  }
 94  
 95  function utf8_wordwrap( $string, $width = 75, $break = "\n", $cut = false )
 96  {
 97  	if( $cut ) {
 98  		// Cut lines that are too long by hand, even if they aren't official break opportunities
 99  		$search  = '/(.{' . $width . '})/uS';
100  		$replace = '$1$2' . $break;
101  	}
102  
103  	return preg_replace( $search, $replace, $string );
104  }
105  
106  function xhprof_save()
107  {
108  	$xhprof_data = xhprof_disable();
109  
110  	include_once XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
111  	include_once XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
112  
113  	$xhprof_runs = new XHProfRuns_Default();
114  
115  	$name = basename($_SERVER["SELF_PATH"]);
116  	$name = preg_replace("/\..*$/","",$name);
117  	
118  	$run_id = $xhprof_runs->save_run($xhprof_data, $name);
119  }
120  
121  function record_post_info($fn, $ex="")
122  {
123  	$rh = fopen($fn, "a");
124  	flock($rh, LOCK_EX);
125  	fwrite($rh, print_r($_SERVER, TRUE));
126  	fwrite($rh, print_r($_REQUEST, TRUE));
127  	fwrite($rh, print_r($HTTP_RAW_POST_DATA, TRUE));
128  	fwrite($rh, print_r($_FILES, TRUE));
129  	if ($ex) fwrite($rh, $ex);
130  	fwrite($rh, "---\n");
131  	fclose($rh);
132  }
133  
134  function sec2hms($sec, $padTime = false, $showSeconds = false)
135  {
136      $hms = "";
137      $hours = intval(intval($sec) / 3600);
138  	$minutes = intval(intval($sec) / 60);
139  
140  	if ($hours) {
141      $hms .= $padTime
142            ? str_pad($hours, 2, "0", STR_PAD_LEFT). " hour"
143            : $hours. " hour";
144      if( $hours != 1 ) $hms .= 's';
145  
146  	$hms .= ' ';
147  	}
148  
149  	if ($minutes) {
150      $minutes = intval(($sec / 60) % 60);
151  
152      $hms .= $padTime
153  			? str_pad($minutes, 2, "0", STR_PAD_LEFT). " minute"
154  			: $minutes. " minute";
155      if( $minutes != 1 ) $hms .= 's';
156  	}
157  
158  	$seconds = intval($sec % 60);
159  
160  	if ($showSeconds && $seconds) {
161  		$hms .= ' ';
162      	$hms .= $padTime
163  				? str_pad($seconds, 2, "0", STR_PAD_LEFT). " second"
164  				: $seconds. " second";
165  		if ($seconds != 1) $hms .= "s";
166  	}
167  
168      return $hms;
169  }
170  
171  function flush_output_buffers()
172  {
173  	ob_flush();
174  	flush();
175  }
176  
177  function country_table()
178  {
179  	/** COUNTRY CODE */
180  	static $countryLookupArray = array(
181  		'AD' => 'Andorra',
182  		'AE' => 'United Arab Emirates',
183  		'AF' => 'Afghanistan',
184  		'AG' => 'Antigua and Barbuda',
185  		'AI' => 'Anguilla',
186  		'AL' => 'Albania',
187  		'AM' => 'Armenia',
188  		'AN' => 'Netherlands Antilles',
189  		'AO' => 'Angola',
190  		'AQ' => 'Antarctica',
191  		'AR' => 'Argentina',
192  		'AS' => 'American Samoa',
193  		'AT' => 'Austria',
194  		'AU' => 'Australia',
195  		'AW' => 'Aruba',
196  		'AX' => 'Aland',
197  		'AZ' => 'Azerbaijan',
198  		'BA' => 'Bosnia and Herzegovina',
199  		'BB' => 'Barbados',
200  		'BD' => 'Bangladesh',
201  		'BE' => 'Belgium',
202  		'BF' => 'Burkina Faso',
203  		'BG' => 'Bulgaria',
204  		'BH' => 'Bahrain',
205  		'BI' => 'Burundi',
206  		'BJ' => 'Benin',
207  		'BL' => 'Saint Barthélemy',
208  		'BM' => 'Bermuda',
209  		'BN' => 'Brunei',
210  		'BO' => 'Bolivia',
211  		'BQ' => 'Bonaire, Sint Eustatius and Saba',
212  		'BR' => 'Brazil',
213  		'BS' => 'Bahamas',
214  		'BT' => 'Bhutan',
215  		'BV' => 'Bouvet Island',
216  		'BW' => 'Botswana',
217  		'BY' => 'Belarus',
218  		'BZ' => 'Belize',
219  		'CA' => 'Canada',
220  		'CC' => 'Cocos (Keeling) Islands',
221  		'CD' => 'The Democratic Republic of the Congo',
222  		'CF' => 'Central African Republic',
223  		'CG' => 'Congo',
224  		'CH' => 'Switzerland',
225  		'CI' => 'Côte d\'Ivoire',
226  		'CK' => 'Cook Islands',
227  		'CL' => 'Chile',
228  		'CM' => 'Cameroon',
229  		'CN' => 'China',
230  		'CO' => 'Colombia',
231  		'CR' => 'Costa Rica',
232  		'CU' => 'Cuba',
233  		'CV' => 'Cape Verde',
234  		'CW' => 'Curaçao',
235  		'CX' => 'Christmas Island',
236  		'CY' => 'Cyprus',
237  		'CZ' => 'Czech Republic',
238  		'DE' => 'Germany',
239  		'DJ' => 'Djibouti',
240  		'DK' => 'Denmark',
241  		'DM' => 'Dominica',
242  		'DO' => 'Dominican Republic',
243  		'DZ' => 'Algeria',
244  		'EC' => 'Ecuador',
245  		'EE' => 'Estonia',
246  		'EG' => 'Egypt',
247  		'EH' => 'Western Sahara',
248  		'ER' => 'Eritrea',
249  		'ES' => 'Spain',
250  		'ET' => 'Ethiopia',
251  		'EU' => 'Europe',
252  		'FI' => 'Finland',
253  		'FJ' => 'Fiji Islands',
254  		'FK' => 'Falkland Islands',
255  		'FM' => 'Federated States of Micronesia',
256  		'FO' => 'Faroe Islands',
257  		'FR' => 'France',
258  		'GA' => 'Gabon',
259  		'GB' => 'United Kingdom',
260  		'GD' => 'Grenada',
261  		'GE' => 'Georgia',
262  		'GF' => 'French Guiana',
263  		'GG' => 'Guernsey',
264  		'GH' => 'Ghana',
265  		'GI' => 'Gibraltar',
266  		'GL' => 'Greenland',
267  		'GM' => 'Gambia',
268  		'GN' => 'Guinea',
269  		'GP' => 'Guadeloupe',
270  		'GQ' => 'Equatorial Guinea',
271  		'GR' => 'Greece',
272  		'GS' => 'South Georgia and the South Sandwich Islands',
273  		'GT' => 'Guatemala',
274  		'GU' => 'Guam',
275  		'GW' => 'Guinea-Bissau',
276  		'GY' => 'Guyana',
277  		'HK' => 'Hong Kong',
278  		'HM' => 'Heard Island and McDonald Islands',
279  		'HN' => 'Honduras',
280  		'HR' => 'Croatia',
281  		'HT' => 'Haiti',
282  		'HU' => 'Hungary',
283  		'ID' => 'Indonesia',
284  		'IE' => 'Ireland',
285  		'IL' => 'Israel',
286  		'IM' => 'Isle of Man',
287  		'IN' => 'India',
288  		'IO' => 'British Indian Ocean Territory',
289  		'IQ' => 'Iraq',
290  		'IR' => 'Iran',
291  		'IS' => 'Iceland',
292  		'IT' => 'Italy',
293  		'JE' => 'Jersey',
294  		'JM' => 'Jamaica',
295  		'JO' => 'Jordan',
296  		'JP' => 'Japan',
297  		'KE' => 'Kenya',
298  		'KG' => 'Kyrgyzstan',
299  		'KH' => 'Cambodia',
300  		'KI' => 'Kiribati',
301  		'KM' => 'Comoros',
302  		'KN' => 'Saint Kitts and Nevis',
303  		'KP' => 'North Korea',
304  		'KR' => 'South Korea',
305  		'KW' => 'Kuwait',
306  		'KY' => 'Cayman Islands',
307  		'KZ' => 'Kazakhstan',
308  		'LA' => 'Laos',
309  		'LB' => 'Lebanon',
310  		'LC' => 'Saint Lucia',
311  		'LI' => 'Liechtenstein',
312  		'LK' => 'Sri Lanka',
313  		'LR' => 'Liberia',
314  		'LS' => 'Lesotho',
315  		'LT' => 'Lithuania',
316  		'LU' => 'Luxembourg',
317  		'LV' => 'Latvia',
318  		'LY' => 'Libya',
319  		'MA' => 'Morocco',
320  		'MC' => 'Monaco',
321  		'MD' => 'Moldova',
322  		'ME' => 'Montenegro',
323  		'MF' => 'Saint Martin',
324  		'MG' => 'Madagascar',
325  		'MH' => 'Marshall Islands',
326  		'MK' => 'Macedonia',
327  		'ML' => 'Mali',
328  		'MM' => 'Myanmar',
329  		'MN' => 'Mongolia',
330  		'MO' => 'Macao',
331  		'MP' => 'Northern Mariana Islands',
332  		'MQ' => 'Martinique',
333  		'MR' => 'Mauritania',
334  		'MS' => 'Montserrat',
335  		'MT' => 'Malta',
336  		'MU' => 'Mauritius',
337  		'MV' => 'Maldives',
338  		'MW' => 'Malawi',
339  		'MX' => 'Mexico',
340  		'MY' => 'Malaysia',
341  		'MZ' => 'Mozambique',
342  		'NA' => 'Namibia',
343  		'NC' => 'New Caledonia',
344  		'NE' => 'Niger',
345  		'NF' => 'Norfolk Island',
346  		'NG' => 'Nigeria',
347  		'NI' => 'Nicaragua',
348  		'NL' => 'Netherlands',
349  		'NO' => 'Norway',
350  		'NP' => 'Nepal',
351  		'NR' => 'Nauru',
352  		'NU' => 'Niue',
353  		'NZ' => 'New Zealand',
354  		'OM' => 'Oman',
355  		'PA' => 'Panama',
356  		'PE' => 'Peru',
357  		'PF' => 'French Polynesia',
358  		'PG' => 'Papua New Guinea',
359  		'PH' => 'Philippines',
360  		'PK' => 'Pakistan',
361  		'PL' => 'Poland',
362  		'PM' => 'Saint Pierre and Miquelon',
363  		'PN' => 'Pitcairn',
364  		'PR' => 'Puerto Rico',
365  		'PS' => 'Palestine',
366  		'PT' => 'Portugal',
367  		'PW' => 'Palau',
368  		'PY' => 'Paraguay',
369  		'QA' => 'Qatar',
370  		'RE' => 'Réunion',
371  		'RO' => 'Romania',
372  		'RS' => 'Serbia',
373  		'RU' => 'Russian Federation',
374  		'RW' => 'Rwanda',
375  		'SA' => 'Saudi Arabia',
376  		'SB' => 'Solomon Islands',
377  		'SC' => 'Seychelles',
378  		'SD' => 'Sudan',
379  		'SE' => 'Sweden',
380  		'SG' => 'Singapore',
381  		'SH' => 'Saint Helena, Ascension, and Tristan da Cunha',
382  		'SI' => 'Slovenia',
383  		'SJ' => 'Svalbard and Jan Mayen',
384  		'SK' => 'Slovakia',
385  		'SL' => 'Sierra Leone',
386  		'SM' => 'San Marino',
387  		'SN' => 'Senegal',
388  		'SO' => 'Somalia',
389  		'SR' => 'Suriname',
390  		'SS' => 'South Sudan',
391  		'ST' => 'Sao Tome and Principe',
392  		'SV' => 'El Salvador',
393  		'SX' => 'Sint Maarten',
394  		'SY' => 'Syria',
395  		'SZ' => 'Swaziland',
396  		'TC' => 'Turks and Caicos Islands',
397  		'TD' => 'Chad',
398  		'TF' => 'French Southern Territories',
399  		'TG' => 'Togo',
400  		'TH' => 'Thailand',
401  		'TJ' => 'Tajikistan',
402  		'TK' => 'Tokelau',
403  		'TM' => 'Turkmenistan',
404  		'TN' => 'Tunisia',
405  		'TO' => 'Tonga',
406  		'TP' => 'East Timor',
407  		'TR' => 'Turkey',
408  		'TT' => 'Trinidad and Tobago',
409  		'TV' => 'Tuvalu',
410  		'TW' => 'Taiwan',
411  		'TZ' => 'Tanzania',
412  		'UA' => 'Ukraine',
413  		'UG' => 'Uganda',
414  		'UM' => 'United States Minor Outlying Islands',
415  		'US' => 'United States',
416  		'UY' => 'Uruguay',
417  		'UZ' => 'Uzbekistan',
418  		'VA' => 'Holy See (Vatican City State)',
419  		'VC' => 'Saint Vincent and the Grenadines',
420  		'VE' => 'Venezuela',
421  		'VG' => 'British Virgin Islands',
422  		'VI' => 'U.S. Virgin Islands',
423  		'VN' => 'Vietnam',
424  		'VU' => 'Vanuatu',
425  		'WF' => 'Wallis and Futuna',
426  		'WS' => 'Samoa',
427  		'XE' => 'England',
428  		'XK' => 'Kosovo',
429  		'XS' => 'Scotland',
430  		'XW' => 'Wales',
431  		'YE' => 'Yemen',
432  		'YT' => 'Mayotte',
433  		'YU' => 'Yugoslavia',
434  		'ZA' => 'South Africa',
435  		'ZM' => 'Zambia',
436  		'ZW' => 'Zimbabwe',
437  		'XX' => 'Unknown');
438  	
439  	return $countryLookupArray;
440  }
441  
442  function country_code_to_name( $code )
443  {
444  	$countryLookupArray = country_table();
445  
446  	return isset( $countryLookupArray[$code] ) ? $countryLookupArray[$code] : $countryLookupArray['XX'];
447  }
448  
449  function troll_countries() {
450    static $trollCountries = array(
451      'AC' => 'Anarcho-Capitalist',
452      'AN' => 'Anarchist',
453      'BL' => 'Black Lives Matter',
454      'CF' => 'Confederate',
455      'CM' => 'Commie',
456      'CT' => 'Catalonia',
457      'DM' => 'Democrat',
458      'EU' => 'European',
459      'FC' => 'Fascist',
460      'GN' => 'Gadsden',
461      'GY' => 'LGBT',
462      'JH' => 'Jihadi',
463      'KN' => 'Kekistani',
464      'MF' => 'Muslim',
465      'NB' => 'National Bolshevik',
466      'NZ' => 'Nazi',
467      'PC' => 'Hippie',
468      'PR' => 'Pirate',
469      'RE' => 'Republican',
470      'TM' => 'DEUS VULT',
471      'TR' => 'Tree Hugger',
472      'UN' => 'United Nations',
473      'WP' => 'White Supremacist'
474    );
475    
476    return $trollCountries;
477  }
478  
479  // For flag selection dropdown menu
480  function troll_countries_selector() {
481    static $trollCountries = array(
482      'AC' => 'Anarcho-Capitalist',
483      'AN' => 'Anarchist',
484      'BL' => 'Black Nationalist',
485      'CF' => 'Confederate',
486      'CM' => 'Communist',
487      'CT' => 'Catalonia',
488      'DM' => 'Democrat',
489      'EU' => 'European',
490      'FC' => 'Fascist',
491      'GN' => 'Gadsden',
492      'GY' => 'Gay',
493      'JH' => 'Jihadi',
494      'KN' => 'Kekistani',
495      'MF' => 'Muslim',
496      'NB' => 'National Bolshevik',
497      'NZ' => 'Nazi',
498      'PC' => 'Hippie',
499      'PR' => 'Pirate',
500      'RE' => 'Republican',
501      'TM' => 'Templar',
502      'TR' => 'Tree Hugger',
503      'UN' => 'United Nations',
504      'WP' => 'White Supremacist'
505    );
506    
507    return $trollCountries;
508  }
509  
510  function country_code_to_name_troll( $code )
511  {
512  	$trollCountries = troll_countries();
513  
514  	return isset( $trollCountries[$code] ) ? $trollCountries[$code] : $trollCountries['IL'];
515  }
516