001/*
002 *  Copyright 2015 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.sms;
017
018import java.util.regex.Matcher;
019import java.util.regex.Pattern;
020
021import org.apache.commons.lang.StringUtils;
022
023import org.ametys.runtime.config.Config;
024
025/**
026 * Static helper gathering mutual operations of the SMS plugin
027 */
028public final class SMSHelper
029{
030    /** The pattern for an international phone number */
031    public static final Pattern PHONE_NUMBER_INTERNATIONAL_VALIDATOR = Pattern.compile("^\\+[0-9]{6,}$");
032
033    private SMSHelper()
034    {
035        // nothing
036    }
037    
038    /**
039     * Transform the phone number to the format defined by the admin
040     * @param phoneNumber the phone number to transform
041     * @return the transformed phone number
042     */
043    public static String transformPhoneNumber(String phoneNumber)
044    {
045        String regexConvert = Config.getInstance().getValue("org.ametys.plugins.sms.broker.convert");
046        String[] tabRegexConvert = StringUtils.split(regexConvert, "\n");
047        String newPhoneNumber = phoneNumber;
048
049        for (int i = 0; i < tabRegexConvert.length; i++)
050        {
051            String[] regexParams = StringUtils.split(tabRegexConvert[i], "/");
052            String regex = regexParams[0];
053            String replaceExpr = regexParams[1];
054            
055            // ensure regexp takes the whole expression
056            if (!regex.startsWith("^"))
057            {
058                regex = "^" + regex;
059            }
060            if (!regex.endsWith("$"))
061            {
062                regex = regex + "$";
063            }
064             
065            // We transform the phone number to an international phone number
066            Matcher m = Pattern.compile(regex).matcher(phoneNumber);
067            if (m.matches())
068            {
069                newPhoneNumber = replaceExpr;
070                
071                for (int c = 1; c <= m.groupCount(); c++)
072                {
073                    newPhoneNumber = newPhoneNumber.replaceAll("\\{" + c + "\\}", m.group(c));
074                }
075                break;
076            }
077        }
078        
079        return newPhoneNumber;
080    }
081    
082    /**
083     * Check if the phoneNumber is in the international format defined by the admin
084     * @param phoneNumber the phone number
085     * @return <code>true</code> if the phone number matches the format,<code>false</code> otherwise
086     */
087    public static boolean checkPhoneNumber(String phoneNumber)
088    {
089        String regexPhone = Config.getInstance().getValue("org.ametys.plugins.sms.broker.autorized");         
090        String[] tabRegexPhone = StringUtils.split(regexPhone, "\n");
091
092        for (String regex : tabRegexPhone)
093        {
094            // ensure regexp takes the whole expression
095            String fullRegex = regex;
096            if (!regex.startsWith("^"))
097            {
098                fullRegex = "^" + fullRegex;
099            }
100            if (!regex.endsWith("$"))
101            {
102                fullRegex = fullRegex + "$";
103            }
104            
105            // We check if the phoneNumber format is correct
106            if (Pattern.compile(fullRegex).matcher(phoneNumber).matches())
107            {
108                return true;
109            }
110        }
111        
112        return false;
113    }
114}