<?php

/*
   +----------------------------------------------------------------------+
   | Text Class                                                           |
   | Author: Adam Goldberg                                                |
   +----------------------------------------------------------------------+
   | Copyright (c) 2008-2009 Adam J Goldberg                              |
   | This file is part of Lifebox.                                        |
   |                                                                      |
   | Lifebox is free software: you can redistribute it and/or modify      |
   | it under the terms of the GNU General Public License as published by |
   | the Free Software Foundation, either version 3 of the License, or    |
   | (at your option) any later version.                                  |
   |                                                                      |
   | Lifebox is distributed in the hope that it will be useful,           |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of the GNU General Public License    |
   | along with Lifebox.  If not, see <http://www.gnu.org/licenses/>.     |
   +----------------------------------------------------------------------+
   | Index of Albums							  |
   +----------------------------------------------------------------------+
        function generateCleanTitle($title,$spaces=false) {
        function getNameArray($string){
        function isValidEmail($email){
        function highlightLinks($string) {
	function divideBySlash($num) {
	function XMLEntities($string)
	function privateXMLEntities($num)
   +----------------------------------------------------------------------+
*/

class Text {

	/*****************************************************************
         * Returns a Topic's ID when the title is specified
         *****************************************************************/
        function generateCleanTitle($title,$spaces=false) {

		$clean_title = '';
                $valid_chars = 'abcdefghijklmnopqrstuvwxyz1234567890_-ABCDEFGHIJKLMNOPQRSTUVWXYZ';

		#Remove spaces
		if (!$spaces) {
                	$title = str_replace(' ','_',$title);
		} else {
			$valid_chars .= ' ,';
		}

		#Replace foreign characters with non-accented ones
		$title = strtr($title,"ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ",
				      "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy");
 
                for ($i=0; $i<strlen($title); $i++){
                        $letter = substr($title,$i,1);
                        if (strpos($valid_chars,$letter) !== false){
                                $clean_title .= $letter;
                        }
                }

		#Replace double underscores
                $clean_title = str_replace('__','_',$clean_title);

                #Don't allow the last character to be a dash
                if (substr($clean_title,0,-1) === '_') {
                        $clean_title = substr($clean_title,0,-1);
                }

                return $clean_title;
        }

	/*****************************************************************
         * Breaks down a name string into its components (First name,
         * last name, title, suffix, middle name, etc ...
         *****************************************************************/
        function getNameArray($string){

                $words = explode(' ',$string);
                $num_words = count($words);

                #---------------DETECT TITLE ----------------------
                #Dr.
                if (preg_match('/^dr[\.]?$/i',$words[0])){
                        $return['title'] = 'Dr.';
                        array_shift($words);
                }
                
                #Miss
                elseif (preg_match('/^miss$/i',$words[0])){
                        $return['title'] = 'Miss';
                        array_shift($words);
                }
                
                #Mr.
                elseif (preg_match('/^mr[\.]?$/i',$words[0])){
                        $return['title'] = 'Mr.';
                        array_shift($words);
                }
                
                #Mrs.
                elseif (preg_match('/^mrs\.]?$/i',$words[0])){
                        $return['title'] = 'Mrs.';
                        array_shift($words);
                }
                
                #Ms.
                elseif (preg_match('/^ms[\.]?$/i',$words[0])){
                        $return['title'] = 'Ms.';
                        array_shift($words);
                }
                
                #Prof
                elseif (preg_match('/^prof[\.]?$/i',$words[0])){
                        $return['title'] = 'Prof.';
                        array_shift($words);
                }
                #-------------------------------------------------
                
                #Recount the words
                $num_words = count($words);
                
                #---------------DETECT SUFFIX ----------------------
                #I 
                if (preg_match('/^i[\.]?$/i',$words[$num_words-1])){
                        $return['suffix'] = 'I';
                        array_pop($words);
                }
                
                #II
                elseif (preg_match('/^ii[\.]?$/i',$words[$num_words-1])){
                        $return['suffix'] = 'II';
                        array_pop($words);
                }
                
                #III
                elseif (preg_match('/^iii[\.]?$/i',$words[$num_words-1])){
                        $return['suffix'] = 'III';
                        array_pop($words);
                }
                
                #Jr.
                elseif (preg_match('/^jr[\.]?$/i',$words[$num_words-1])){
                        $return['suffix'] = 'Jr.';
                        array_pop($words);
                }
                
                #Sr.
                elseif (preg_match('/^Sr[\.]?$/i',$words[$num_words-1])){
                        $return['suffix'] = 'Sr.';
                        array_pop($words);
                }
                #---------------------------------------------------

		#recount the words
                $num_words = count($words);

                if ($num_words == 1){
                        $return['first_name'] = ucwords($words[0]);
                        return $return;
                }

                elseif ($num_words == 2){
                        $return['first_name'] = ucwords($words[0]);
                        $return['last_name'] = ucwords($words[1]);
                        return $return;
                }

                elseif ($num_words > 2){
                        $return['first_name'] = ucwords($words[0]);

                        $return['last_name'] = ucwords($words[$num_words-1]);

                        array_shift($words);
                        array_pop($words);

                        $return['middle_name'] = implode(' ',$words);

                        return $return;
                }
        }

	/*****************************************************************
	 * Checks if an e-mail is valid
         *****************************************************************/
        function isValidEmail($email){
                if( !eregi('^[a-z0-9]+([_\\.-][a-z0-9]+)*'. '@([a-z0-9]+([\.-][a-z0-9]+))*$',$email) ){
                        return false;
                } else {
                        return true;
                }
        }               

	/*****************************************************************
	 * Given a string of text, find all links and return them.
         *****************************************************************/
	function highlightLinks($string) {
		$text = $string;
		$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)','<a class="text_link" target="_blank" title="Click to open in a new window" href="\\1">\\1</a>', $text);
		$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)','\\1<a class="text_link" target="_blank" title="Click to open in a new window" href="http://\\2">\\2</a>', $text);
		$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','<a class="text_link" target="_blank" title="Click to open in a new window" href="mailto:\\1">\\1</a>', $text);
	
		return trim($text);
	}

	/*****************************************************************
	 * Divide by a slash
         *****************************************************************/
	function divideBySlash($num) {

		if (!preg_match('/\//i',$num)) {	
			return $num;
		}

		$string = explode('/',$num);

		if ($string[0] === '0') {
			return false;
		}
	
		$result = $string[0] / $string[1];

		return $result;
	}

	/*****************************************************************
	 * Generate XML Entities
         *****************************************************************/
	function XMLEntities($string) {
		$string = preg_replace('/[^\x09\x0A\x0D\x20-\x7F]/e', 'Text::privateXMLEntities("$0")', $string);
		return $string;
	}

	/*****************************************************************
	 * Private function for generating XML entities
         *****************************************************************/
	function privateXMLEntities($num) {
	    $chars = array(
		128 => '&#8364;',
		130 => '&#8218;',
		131 => '&#402;',
		132 => '&#8222;',
		133 => '&#8230;',
		134 => '&#8224;',
		135 => '&#8225;',
		136 => '&#710;',
		137 => '&#8240;',
		138 => '&#352;',
		139 => '&#8249;',
		140 => '&#338;',
		142 => '&#381;',
		145 => '&#8216;',
		146 => '&#8217;',
		147 => '&#8220;',
		148 => '&#8221;',
		149 => '&#8226;',
		150 => '&#8211;',
		151 => '&#8212;',
		152 => '&#732;',
		153 => '&#8482;',
		154 => '&#353;',
		155 => '&#8250;',
		156 => '&#339;',
		158 => '&#382;',
		159 => '&#376;');
		$num = ord($num);
		return (($num > 127 && $num < 160) ? $chars[$num] : "&#".$num.";" );
	    } 
}
