web/common.php
changeset 1006 bb98a0de3bfd
parent 1003 366c873e2e29
child 1012 5064d9d287a0
equal deleted inserted replaced
1005:8aca18c0da85 1006:bb98a0de3bfd
   405 }
   405 }
   406 
   406 
   407 
   407 
   408 $realm = 'Polemictweet restricted area';
   408 $realm = 'Polemictweet restricted area';
   409 
   409 
   410 /*function authenticate($users, $translate) {
       
   411 	if (!isset($_SESSION['user_id']))
       
   412 	{
       
   413 		// Fetch current URL
       
   414 		$this_url = $_SERVER['REQUEST_URI'];
       
   415 	
       
   416 		// Redirect to login page passing current URL
       
   417 		header('Location: login.php?return_url=' . urlencode($this_url));
       
   418 		exit;
       
   419 	}
       
   420 }*/
       
   421 
   410 
   422 function authenticate($users, $translate) {
   411 function authenticate($users, $translate) {
   423 
   412 
   424 	global $realm;
   413 	global $realm;
   425 	
   414 	
   483 		unset($needed_parts[$m[1]]);
   472 		unset($needed_parts[$m[1]]);
   484 	}
   473 	}
   485 
   474 
   486 	return $needed_parts ? false : $data;
   475 	return $needed_parts ? false : $data;
   487 }
   476 }
       
   477 
       
   478 /**
       
   479  * Modifies a string to remove all non ASCII characters and spaces.
       
   480  */
       
   481 function slugify($text)
       
   482 {
       
   483 	// replace non letter or digits by -
       
   484 	$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
       
   485 
       
   486 	// trim
       
   487 	$text = trim($text, '-');
       
   488 
       
   489 	// transliterate
       
   490 	if (function_exists('iconv'))
       
   491 	{
       
   492 		$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
       
   493 	}
       
   494 
       
   495 	// lowercase
       
   496 	$text = strtolower($text);
       
   497 
       
   498 	// remove unwanted characters
       
   499 	$text = preg_replace('~[^-\w]+~', '', $text);
       
   500 
       
   501 	if (empty($text))
       
   502 	{
       
   503 		return 'n-a';
       
   504 	}
       
   505 
       
   506 	return $text;
       
   507 }
       
   508 
       
   509 
       
   510 // from http://www.house6.com/blog/?p=83
       
   511 function sanitize_filename($f) {
       
   512 	// a combination of various methods
       
   513 	// we don't want to convert html entities, or do any url encoding
       
   514 	// we want to retain the "essence" of the original file name, if possible
       
   515 	// char replace table found at:
       
   516 	// http://www.php.net/manual/en/function.strtr.php#98669
       
   517 	$replace_chars = array(
       
   518 			'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
       
   519 			'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
       
   520 			'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
       
   521 			'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
       
   522 			'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
       
   523 			'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
       
   524 			'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f'
       
   525 	);
       
   526 	$f = strtr($f, $replace_chars);
       
   527 	// convert & to "and", @ to "at", and # to "number"
       
   528 	$f = preg_replace(array('/[\&]/', '/[\@]/', '/[\#]/'), array('-and-', '-at-', '-number-'), $f);
       
   529 	$f = preg_replace('/[^(\x20-\x7F)]*/','', $f); // removes any special chars we missed
       
   530 	$f = str_replace(' ', '-', $f); // convert space to hyphen
       
   531 	$f = str_replace('\'', '', $f); // removes apostrophes
       
   532 	$f = preg_replace('/[^\w\-\.]+/', '', $f); // remove non-word chars (leaving hyphens and periods)
       
   533 	$f = preg_replace('/[\-]+/', '-', $f); // converts groups of hyphens into one
       
   534 	if (function_exists('iconv'))
       
   535 	{
       
   536 		$f = iconv('utf-8', 'us-ascii//TRANSLIT', $f);
       
   537 	}
       
   538 	
       
   539 	return strtolower($f);
       
   540 }