Pārlūkot izejas kodu

\r\n --> \n

Cause all the .php files containing lines ending with \r\n to instead end with \n.

DYI with:

find fusionpbx -type f -name '*.php' -exec dos2unix '{}' \;
Harry G. Coin 9 gadi atpakaļ
vecāks
revīzija
31f2049ef9

+ 218 - 218
app_defaults.php

@@ -1,219 +1,219 @@
-<?php
-
-if ($domains_processed == 1) {
-
-	//populate new phone_label values, phone_type_* values
-		$obj = new schema;
-		$obj->db = $db;
-		$obj->db_type = $db_type;
-		$obj->schema();
-		$field_exists = $obj->column_exists($db_name, 'v_contact_phones', 'phone_type');	//check if field exists
-		if ($field_exists) {
-			//add multi-lingual support
-			$language = new text;
-			$text = $language->get();
-
-			// populate phone_type_* values
-			$sql = "update v_contact_phones set phone_type_voice = '1' ";
-			$sql .= "where phone_type = 'home' ";
-			$sql .= "or phone_type = 'work' ";
-			$sql .= "or phone_type = 'voice' ";
-			$sql .= "or phone_type = 'voicemail' ";
-			$sql .= "or phone_type = 'cell' ";
-			$sql .= "or phone_type = 'pcs' ";
-			$db->exec(check_sql($sql));
-			unset($sql);
-
-			$sql = "update v_contact_phones set phone_type_fax = '1' where phone_type = 'fax'";
-			$db->exec(check_sql($sql));
-			unset($sql);
-
-			$sql = "update v_contact_phones set phone_type_video = '1' where phone_type = 'video'";
-			$db->exec(check_sql($sql));
-			unset($sql);
-
-			$sql = "update v_contact_phones set phone_type_text = '1' where phone_type = 'cell' or phone_type = 'pager'";
-			$db->exec(check_sql($sql));
-			unset($sql);
-
-			// migrate phone_type values to phone_label, correct case and make multilingual where appropriate
-			$default_phone_types = array('home','work','pref','voice','fax','msg','cell','pager','modem','car','isdn','video','pcs');
-			$default_phone_labels = array($text['option-home'],$text['option-work'],'Pref','Voice',$text['option-fax'],$text['option-voicemail'],$text['option-mobile'],$text['option-pager'],'Modem','Car','ISDN','Video','PCS');
-			foreach ($default_phone_types as $index => $old) {
-				$new = $default_phone_labels[$index];
-				$sql = "update v_contact_phones set phone_label = '".$new."' where phone_type = '".$old."'";
-				$db->exec(check_sql($sql));
-				unset($sql);
-			}
-
-			// empty phone_type field to prevent confusion in the future
-			$sql = "update v_contact_phones set phone_type = null";
-			$db->exec(check_sql($sql));
-			unset($sql);
-		}
-		unset($obj);
-
-	//populate primary email from deprecated field in v_contact table
-		$obj = new schema;
-		$obj->db = $db;
-		$obj->db_type = $db_type;
-		$obj->schema();
-		$field_exists = $obj->column_exists($db_name, 'v_contacts', 'contact_email');	//check if field exists
-		if ($field_exists) {
-			// get email records
-			$sql = "select * from v_contacts where contact_email is not null and contact_email != ''";
-			$prep_statement = $db->prepare(check_sql($sql));
-			$prep_statement->execute();
-			$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
-			$result_count = count($result);
-			unset ($prep_statement, $sql);
-			if ($result_count > 0) {
-				foreach($result as $row) {
-					$sql = "insert into v_contact_emails ";
-					$sql .= "(";
-					$sql .= "domain_uuid, ";
-					$sql .= "contact_uuid, ";
-					$sql .= "contact_email_uuid, ";
-					$sql .= "email_primary, ";
-					$sql .= "email_address";
-					$sql .= ")";
-					$sql .= "values ";
-					$sql .= "(";
-					$sql .= "'".$row['domain_uuid']."', ";
-					$sql .= "'".$row['contact_uuid']."', ";
-					$sql .= "'".uuid()."', ";
-					$sql .= "1, ";
-					$sql .= "'".$row['contact_email']."' ";
-					$sql .= ")";
-					$db->exec(check_sql($sql));
-					unset($sql);
-
-					//verify and remove value from old field
-					$sql2 = "select email_address from v_contact_emails ";
-					$sql2 .= "where domain_uuid = '".$row['domain_uuid']."' ";
-					$sql2 .= "and contact_uuid = '".$row['contact_uuid']."' ";
-					$sql2 .= "and email_address = '".$row['contact_email']."' ";
-					$prep_statement2 = $db->prepare(check_sql($sql2));
-					$prep_statement2->execute();
-					$result2 = $prep_statement2->fetchAll(PDO::FETCH_NAMED);
-					$result_count2 = count($result2);
-					if ($result_count2 > 0) {
-						$sql3 = "update v_contacts set contact_email = '' ";
-						$sql3 .= "where domain_uuid = '".$row['domain_uuid']."' ";
-						$sql3 .= "and contact_uuid = '".$row['contact_uuid']."' ";
-						$prep_statement3 = $db->prepare(check_sql($sql3));
-						$prep_statement3->execute();
-						unset($sql3, $prep_statement3);
-					}
-					unset($sql2, $result2, $prep_statement2);
-				}
-			}
-		}
-		unset($obj);
-
-	//populate primary url from deprecated field in v_contact table
-		$obj = new schema;
-		$obj->db = $db;
-		$obj->db_type = $db_type;
-		$obj->schema();
-		$field_exists = $obj->column_exists($db_name, 'v_contacts', 'contact_url');	//check if field exists
-		if ($field_exists) {
-			// get email records
-			$sql = "select * from v_contacts where contact_url is not null and contact_url != ''";
-			$prep_statement = $db->prepare(check_sql($sql));
-			$prep_statement->execute();
-			$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
-			$result_count = count($result);
-			unset ($prep_statement, $sql);
-			if ($result_count > 0) {
-				foreach($result as $row) {
-					$sql = "insert into v_contact_urls ";
-					$sql .= "(";
-					$sql .= "domain_uuid, ";
-					$sql .= "contact_uuid, ";
-					$sql .= "contact_url_uuid, ";
-					$sql .= "url_primary, ";
-					$sql .= "url_address";
-					$sql .= ")";
-					$sql .= "values ";
-					$sql .= "(";
-					$sql .= "'".$row['domain_uuid']."', ";
-					$sql .= "'".$row['contact_uuid']."', ";
-					$sql .= "'".uuid()."', ";
-					$sql .= "1, ";
-					$sql .= "'".$row['contact_url']."' ";
-					$sql .= ")";
-					$db->exec(check_sql($sql));
-					unset($sql);
-
-					//verify and remove value from old field
-					$sql2 = "select url_address from v_contact_urls ";
-					$sql2 .= "where domain_uuid = '".$row['domain_uuid']."' ";
-					$sql2 .= "and contact_uuid = '".$row['contact_uuid']."' ";
-					$sql2 .= "and url_address = '".$row['contact_url']."' ";
-					$prep_statement2 = $db->prepare(check_sql($sql2));
-					$prep_statement2->execute();
-					$result2 = $prep_statement2->fetchAll(PDO::FETCH_NAMED);
-					$result_count2 = count($result2);
-					if ($result_count2 > 0) {
-						$sql3 = "update v_contacts set contact_url = '' ";
-						$sql3 .= "where domain_uuid = '".$row['domain_uuid']."' ";
-						$sql3 .= "and contact_uuid = '".$row['contact_uuid']."' ";
-						$prep_statement3 = $db->prepare(check_sql($sql3));
-						$prep_statement3->execute();
-						unset($sql3, $prep_statement3);
-					}
-					unset($sql2, $result2, $prep_statement2);
-				}
-			}
-		}
-		unset($obj);
-
-	//set [name]_primary fields to 0 where null
-		$name_tables = array('phones','addresses','emails','urls');
-		$name_fields = array('phone','address','email','url');
-		foreach ($name_tables as $name_index => $name_table) {
-			$sql = "update v_contact_".$name_table." set ".$name_fields[$name_index]."_primary = 0 ";
-			$sql .= "where ".$name_fields[$name_index]."_primary is null ";
-			$prep_statement = $db->prepare(check_sql($sql));
-			$prep_statement->execute();
-			unset($sql, $prep_statement);
-		}
-		unset($name_tables, $name_fields);
-
-	//move the users from the contact groups table into the contact users table
-		$sql = "select * from v_contact_groups ";
-		$sql .= "where group_uuid in (select user_uuid from v_users) ";
-		$prep_statement = $db->prepare(check_sql($sql));
-		$prep_statement->execute();
-		$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
-		foreach ($result as &$row) {
-			$sql = "insert into v_contact_users ";
-			$sql .= "( ";
-			$sql .= "contact_user_uuid, ";
-			$sql .= "domain_uuid, ";
-			$sql .= "contact_uuid, ";
-			$sql .= "user_uuid ";
-			$sql .= ") ";
-			$sql .= "values ";
-			$sql .= "( ";
-			$sql .= "'".uuid()."', ";
-			$sql .= "'".$row["domain_uuid"]."', ";
-			$sql .= "'".$row["contact_uuid"]."', ";
-			$sql .= "'".$row["group_uuid"]."' ";
-			$sql .= ");";
-			//echo $sql."\n";
-			$db->exec($sql);
-			unset($sql);
-
-			$sql = "delete from v_contact_groups ";
-			$sql .= "where contact_group_uuid = '".$row["contact_group_uuid"]."';";
-			//echo $sql."\n";
-			$db->exec($sql);
-			unset($sql);
-		}
-		unset ($prep_statement);
-
-}
-
+<?php
+
+if ($domains_processed == 1) {
+
+	//populate new phone_label values, phone_type_* values
+		$obj = new schema;
+		$obj->db = $db;
+		$obj->db_type = $db_type;
+		$obj->schema();
+		$field_exists = $obj->column_exists($db_name, 'v_contact_phones', 'phone_type');	//check if field exists
+		if ($field_exists) {
+			//add multi-lingual support
+			$language = new text;
+			$text = $language->get();
+
+			// populate phone_type_* values
+			$sql = "update v_contact_phones set phone_type_voice = '1' ";
+			$sql .= "where phone_type = 'home' ";
+			$sql .= "or phone_type = 'work' ";
+			$sql .= "or phone_type = 'voice' ";
+			$sql .= "or phone_type = 'voicemail' ";
+			$sql .= "or phone_type = 'cell' ";
+			$sql .= "or phone_type = 'pcs' ";
+			$db->exec(check_sql($sql));
+			unset($sql);
+
+			$sql = "update v_contact_phones set phone_type_fax = '1' where phone_type = 'fax'";
+			$db->exec(check_sql($sql));
+			unset($sql);
+
+			$sql = "update v_contact_phones set phone_type_video = '1' where phone_type = 'video'";
+			$db->exec(check_sql($sql));
+			unset($sql);
+
+			$sql = "update v_contact_phones set phone_type_text = '1' where phone_type = 'cell' or phone_type = 'pager'";
+			$db->exec(check_sql($sql));
+			unset($sql);
+
+			// migrate phone_type values to phone_label, correct case and make multilingual where appropriate
+			$default_phone_types = array('home','work','pref','voice','fax','msg','cell','pager','modem','car','isdn','video','pcs');
+			$default_phone_labels = array($text['option-home'],$text['option-work'],'Pref','Voice',$text['option-fax'],$text['option-voicemail'],$text['option-mobile'],$text['option-pager'],'Modem','Car','ISDN','Video','PCS');
+			foreach ($default_phone_types as $index => $old) {
+				$new = $default_phone_labels[$index];
+				$sql = "update v_contact_phones set phone_label = '".$new."' where phone_type = '".$old."'";
+				$db->exec(check_sql($sql));
+				unset($sql);
+			}
+
+			// empty phone_type field to prevent confusion in the future
+			$sql = "update v_contact_phones set phone_type = null";
+			$db->exec(check_sql($sql));
+			unset($sql);
+		}
+		unset($obj);
+
+	//populate primary email from deprecated field in v_contact table
+		$obj = new schema;
+		$obj->db = $db;
+		$obj->db_type = $db_type;
+		$obj->schema();
+		$field_exists = $obj->column_exists($db_name, 'v_contacts', 'contact_email');	//check if field exists
+		if ($field_exists) {
+			// get email records
+			$sql = "select * from v_contacts where contact_email is not null and contact_email != ''";
+			$prep_statement = $db->prepare(check_sql($sql));
+			$prep_statement->execute();
+			$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
+			$result_count = count($result);
+			unset ($prep_statement, $sql);
+			if ($result_count > 0) {
+				foreach($result as $row) {
+					$sql = "insert into v_contact_emails ";
+					$sql .= "(";
+					$sql .= "domain_uuid, ";
+					$sql .= "contact_uuid, ";
+					$sql .= "contact_email_uuid, ";
+					$sql .= "email_primary, ";
+					$sql .= "email_address";
+					$sql .= ")";
+					$sql .= "values ";
+					$sql .= "(";
+					$sql .= "'".$row['domain_uuid']."', ";
+					$sql .= "'".$row['contact_uuid']."', ";
+					$sql .= "'".uuid()."', ";
+					$sql .= "1, ";
+					$sql .= "'".$row['contact_email']."' ";
+					$sql .= ")";
+					$db->exec(check_sql($sql));
+					unset($sql);
+
+					//verify and remove value from old field
+					$sql2 = "select email_address from v_contact_emails ";
+					$sql2 .= "where domain_uuid = '".$row['domain_uuid']."' ";
+					$sql2 .= "and contact_uuid = '".$row['contact_uuid']."' ";
+					$sql2 .= "and email_address = '".$row['contact_email']."' ";
+					$prep_statement2 = $db->prepare(check_sql($sql2));
+					$prep_statement2->execute();
+					$result2 = $prep_statement2->fetchAll(PDO::FETCH_NAMED);
+					$result_count2 = count($result2);
+					if ($result_count2 > 0) {
+						$sql3 = "update v_contacts set contact_email = '' ";
+						$sql3 .= "where domain_uuid = '".$row['domain_uuid']."' ";
+						$sql3 .= "and contact_uuid = '".$row['contact_uuid']."' ";
+						$prep_statement3 = $db->prepare(check_sql($sql3));
+						$prep_statement3->execute();
+						unset($sql3, $prep_statement3);
+					}
+					unset($sql2, $result2, $prep_statement2);
+				}
+			}
+		}
+		unset($obj);
+
+	//populate primary url from deprecated field in v_contact table
+		$obj = new schema;
+		$obj->db = $db;
+		$obj->db_type = $db_type;
+		$obj->schema();
+		$field_exists = $obj->column_exists($db_name, 'v_contacts', 'contact_url');	//check if field exists
+		if ($field_exists) {
+			// get email records
+			$sql = "select * from v_contacts where contact_url is not null and contact_url != ''";
+			$prep_statement = $db->prepare(check_sql($sql));
+			$prep_statement->execute();
+			$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
+			$result_count = count($result);
+			unset ($prep_statement, $sql);
+			if ($result_count > 0) {
+				foreach($result as $row) {
+					$sql = "insert into v_contact_urls ";
+					$sql .= "(";
+					$sql .= "domain_uuid, ";
+					$sql .= "contact_uuid, ";
+					$sql .= "contact_url_uuid, ";
+					$sql .= "url_primary, ";
+					$sql .= "url_address";
+					$sql .= ")";
+					$sql .= "values ";
+					$sql .= "(";
+					$sql .= "'".$row['domain_uuid']."', ";
+					$sql .= "'".$row['contact_uuid']."', ";
+					$sql .= "'".uuid()."', ";
+					$sql .= "1, ";
+					$sql .= "'".$row['contact_url']."' ";
+					$sql .= ")";
+					$db->exec(check_sql($sql));
+					unset($sql);
+
+					//verify and remove value from old field
+					$sql2 = "select url_address from v_contact_urls ";
+					$sql2 .= "where domain_uuid = '".$row['domain_uuid']."' ";
+					$sql2 .= "and contact_uuid = '".$row['contact_uuid']."' ";
+					$sql2 .= "and url_address = '".$row['contact_url']."' ";
+					$prep_statement2 = $db->prepare(check_sql($sql2));
+					$prep_statement2->execute();
+					$result2 = $prep_statement2->fetchAll(PDO::FETCH_NAMED);
+					$result_count2 = count($result2);
+					if ($result_count2 > 0) {
+						$sql3 = "update v_contacts set contact_url = '' ";
+						$sql3 .= "where domain_uuid = '".$row['domain_uuid']."' ";
+						$sql3 .= "and contact_uuid = '".$row['contact_uuid']."' ";
+						$prep_statement3 = $db->prepare(check_sql($sql3));
+						$prep_statement3->execute();
+						unset($sql3, $prep_statement3);
+					}
+					unset($sql2, $result2, $prep_statement2);
+				}
+			}
+		}
+		unset($obj);
+
+	//set [name]_primary fields to 0 where null
+		$name_tables = array('phones','addresses','emails','urls');
+		$name_fields = array('phone','address','email','url');
+		foreach ($name_tables as $name_index => $name_table) {
+			$sql = "update v_contact_".$name_table." set ".$name_fields[$name_index]."_primary = 0 ";
+			$sql .= "where ".$name_fields[$name_index]."_primary is null ";
+			$prep_statement = $db->prepare(check_sql($sql));
+			$prep_statement->execute();
+			unset($sql, $prep_statement);
+		}
+		unset($name_tables, $name_fields);
+
+	//move the users from the contact groups table into the contact users table
+		$sql = "select * from v_contact_groups ";
+		$sql .= "where group_uuid in (select user_uuid from v_users) ";
+		$prep_statement = $db->prepare(check_sql($sql));
+		$prep_statement->execute();
+		$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
+		foreach ($result as &$row) {
+			$sql = "insert into v_contact_users ";
+			$sql .= "( ";
+			$sql .= "contact_user_uuid, ";
+			$sql .= "domain_uuid, ";
+			$sql .= "contact_uuid, ";
+			$sql .= "user_uuid ";
+			$sql .= ") ";
+			$sql .= "values ";
+			$sql .= "( ";
+			$sql .= "'".uuid()."', ";
+			$sql .= "'".$row["domain_uuid"]."', ";
+			$sql .= "'".$row["contact_uuid"]."', ";
+			$sql .= "'".$row["group_uuid"]."' ";
+			$sql .= ");";
+			//echo $sql."\n";
+			$db->exec($sql);
+			unset($sql);
+
+			$sql = "delete from v_contact_groups ";
+			$sql .= "where contact_group_uuid = '".$row["contact_group_uuid"]."';";
+			//echo $sql."\n";
+			$db->exec($sql);
+			unset($sql);
+		}
+		unset ($prep_statement);
+
+}
+
 ?>

+ 121 - 121
contact_auth.php

@@ -1,122 +1,122 @@
-<?php
-/*
-	FusionPBX
-	Version: MPL 1.1
-
-	The contents of this file are subject to the Mozilla Public License Version
-	1.1 (the "License"); you may not use this file except in compliance with
-	the License. You may obtain a copy of the License at
-	http://www.mozilla.org/MPL/
-
-	Software distributed under the License is distributed on an "AS IS" basis,
-	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
-	for the specific language governing rights and limitations under the
-	License.
-
-	The Original Code is FusionPBX
-
-	The Initial Developer of the Original Code is
-	Mark J Crane <[email protected]>
-	Portions created by the Initial Developer are Copyright (C) 2008-2013
-	the Initial Developer. All Rights Reserved.
-
-	Contributor(s):
-	Mark J Crane <[email protected]>
-*/
-require_once "root.php";
-require_once "resources/require.php";
-require_once "resources/check_auth.php";
-if (permission_exists('contact_add')) {
-	//access granted
-}
-else {
-	echo "access denied";
-	exit;
-}
-
-/*
-echo "bang!";
-exit;
-*/
-
-//add multi-lingual support
-	$language = new text;
-	$text = $language->get();
-
-
-$_SESSION['contact_auth']['source'] = ($_SESSION['contact_auth']['source'] == '') ? $_REQUEST['source'] : $_SESSION['contact_auth']['source'];
-$_SESSION['contact_auth']['target'] = ($_SESSION['contact_auth']['target'] == '') ? $_REQUEST['target'] : $_SESSION['contact_auth']['target'];
-
-
-//google api authentication
-if ($_SESSION['contact_auth']['source'] == 'google') {
-
-	if ($_REQUEST['error']) {
-		$_SESSION['message'] = ($text['message-'.$_REQUEST['error']] != '') ? $text['message-'.$_REQUEST['error']] : $_REQUEST['error'];
-		$_SESSION['message_mood'] = 'negative';
-		header("Location: ".$_SESSION['contact_auth']['referer']);
-		unset($_SESSION['contact_auth']);
-		exit;
-	}
-
-	if (isset($_REQUEST['signout'])) {
-		unset($_SESSION['contact_auth']['token']);
-		$_SESSION['message'] = $text['message-google_signed_out'];
-		header("Location: https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=".(($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].PROJECT_PATH."/app/contacts/".$_SESSION['contact_auth']['referer']);
-		exit;
-	}
-
-	if ($_GET['code'] == '') {
-		header("Location: https://accounts.google.com/o/oauth2/auth?client_id=".$_SESSION['contact']['google_oauth_client_id']['text']."&redirect_uri=".(($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."&scope=https://www.google.com/m8/feeds/&response_type=code");
-		exit;
-	}
-	else {
-		$auth_code = $_GET["code"];
-	}
-
-	/*******************************************************************************************/
-	// request access token
-
-	$fields = array(
-		'code' => urlencode($auth_code),
-		'client_id' => urlencode($_SESSION['contact']['google_oauth_client_id']['text']),
-		'client_secret' => urlencode($_SESSION['contact']['google_oauth_client_secret']['text']),
-		'redirect_uri' => urlencode((($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']),
-		'grant_type' => urlencode('authorization_code')
-		);
-
-	foreach($fields as $key => $value) {
-		$post_fields[] = $key.'='.$value;
-	}
-	$post_fields = implode("&", $post_fields);
-
-	$curl = curl_init();
-	curl_setopt($curl, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
-	curl_setopt($curl, CURLOPT_POST, 5);
-	curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
-	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
-	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
-	$result = curl_exec($curl);
-	curl_close($curl);
-
-	$response =  json_decode($result);
-	$access_token = $response->access_token;
-
-	if ($access_token != '') {
-		// redirect to target script
-		$_SESSION['contact_auth']['token'] = $access_token;
-		header("Location: ".$_SESSION['contact_auth']['target']);
-		exit;
-	}
-
-}
-else {
-
-	$_SESSION['message'] = $text['message-access_denied'];
-	$_SESSION['message_mood'] = 'negative';
-	header("Location: ".$_SESSION['contact_auth']['referer']);
-	unset($_SESSION['contact_auth']);
-	exit;
-
-}
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2013
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+require_once "root.php";
+require_once "resources/require.php";
+require_once "resources/check_auth.php";
+if (permission_exists('contact_add')) {
+	//access granted
+}
+else {
+	echo "access denied";
+	exit;
+}
+
+/*
+echo "bang!";
+exit;
+*/
+
+//add multi-lingual support
+	$language = new text;
+	$text = $language->get();
+
+
+$_SESSION['contact_auth']['source'] = ($_SESSION['contact_auth']['source'] == '') ? $_REQUEST['source'] : $_SESSION['contact_auth']['source'];
+$_SESSION['contact_auth']['target'] = ($_SESSION['contact_auth']['target'] == '') ? $_REQUEST['target'] : $_SESSION['contact_auth']['target'];
+
+
+//google api authentication
+if ($_SESSION['contact_auth']['source'] == 'google') {
+
+	if ($_REQUEST['error']) {
+		$_SESSION['message'] = ($text['message-'.$_REQUEST['error']] != '') ? $text['message-'.$_REQUEST['error']] : $_REQUEST['error'];
+		$_SESSION['message_mood'] = 'negative';
+		header("Location: ".$_SESSION['contact_auth']['referer']);
+		unset($_SESSION['contact_auth']);
+		exit;
+	}
+
+	if (isset($_REQUEST['signout'])) {
+		unset($_SESSION['contact_auth']['token']);
+		$_SESSION['message'] = $text['message-google_signed_out'];
+		header("Location: https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=".(($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].PROJECT_PATH."/app/contacts/".$_SESSION['contact_auth']['referer']);
+		exit;
+	}
+
+	if ($_GET['code'] == '') {
+		header("Location: https://accounts.google.com/o/oauth2/auth?client_id=".$_SESSION['contact']['google_oauth_client_id']['text']."&redirect_uri=".(($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."&scope=https://www.google.com/m8/feeds/&response_type=code");
+		exit;
+	}
+	else {
+		$auth_code = $_GET["code"];
+	}
+
+	/*******************************************************************************************/
+	// request access token
+
+	$fields = array(
+		'code' => urlencode($auth_code),
+		'client_id' => urlencode($_SESSION['contact']['google_oauth_client_id']['text']),
+		'client_secret' => urlencode($_SESSION['contact']['google_oauth_client_secret']['text']),
+		'redirect_uri' => urlencode((($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']),
+		'grant_type' => urlencode('authorization_code')
+		);
+
+	foreach($fields as $key => $value) {
+		$post_fields[] = $key.'='.$value;
+	}
+	$post_fields = implode("&", $post_fields);
+
+	$curl = curl_init();
+	curl_setopt($curl, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
+	curl_setopt($curl, CURLOPT_POST, 5);
+	curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
+	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
+	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
+	$result = curl_exec($curl);
+	curl_close($curl);
+
+	$response =  json_decode($result);
+	$access_token = $response->access_token;
+
+	if ($access_token != '') {
+		// redirect to target script
+		$_SESSION['contact_auth']['token'] = $access_token;
+		header("Location: ".$_SESSION['contact_auth']['target']);
+		exit;
+	}
+
+}
+else {
+
+	$_SESSION['message'] = $text['message-access_denied'];
+	$_SESSION['message_mood'] = 'negative';
+	header("Location: ".$_SESSION['contact_auth']['referer']);
+	unset($_SESSION['contact_auth']);
+	exit;
+
+}
 ?>

+ 676 - 676
contact_import_google.php

@@ -1,677 +1,677 @@
-<?php
-/*
-	FusionPBX
-	Version: MPL 1.1
-
-	The contents of this file are subject to the Mozilla Public License Version
-	1.1 (the "License"); you may not use this file except in compliance with
-	the License. You may obtain a copy of the License at
-	http://www.mozilla.org/MPL/
-
-	Software distributed under the License is distributed on an "AS IS" basis,
-	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
-	for the specific language governing rights and limitations under the
-	License.
-
-	The Original Code is FusionPBX
-
-	The Initial Developer of the Original Code is
-	Mark J Crane <[email protected]>
-	Portions created by the Initial Developer are Copyright (C) 2008-2013
-	the Initial Developer. All Rights Reserved.
-
-	Contributor(s):
-	Mark J Crane <[email protected]>
-*/
-require_once "root.php";
-require_once "resources/require.php";
-require_once "resources/check_auth.php";
-require_once "resources/functions/google_get_groups.php";
-require_once "resources/functions/google_get_contacts.php";
-
-if (permission_exists('contact_add')) {
-	//access granted
-}
-else {
-	echo "access denied";
-	exit;
-}
-
-//add multi-lingual support
-	$language = new text;
-	$text = $language->get();
-
-//handle import
-if ($_POST['a'] == 'import') {
-	if (sizeof($_POST['group_id']) > 0) {
-		//get contact ids for those in the submitted groups
-		if (sizeof($_SESSION['contact_auth']['google']) > 0) {
-			foreach ($_SESSION['contact_auth']['google'] as $contact['id'] => $contact) {
-				foreach ($contact['groups'] as $contact_group['id'] => $meh) {
-					if (in_array($contact_group['id'], $_POST['group_id'])) {
-						$import_ids[] = $contact['id'];
-					}
-				}
-			}
-		}
-	}
-
-	if (sizeof($_POST['contact_id']) > 0) {
-		foreach ($_POST['contact_id'] as $contact_id) {
-			$import_ids[] = $contact_id;
-		}
-	}
-
-	//iterate selected contact ids, insert contact into database
-	$contacts_imported = 0;
-	$contacts_skipped = 0;
-	$contacts_replaced = 0;
-
-	if (sizeof($import_ids) > 0) {
-
-		$import_ids = array_unique($import_ids);
-		foreach ($import_ids as $contact_id) {
-
-			//check for duplicate contact (already exists, previously imported, etc)
-			$sql = "select contact_uuid from v_contact_settings ";
-			$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
-			$sql .= "and contact_setting_category = 'google' ";
-			$sql .= "and contact_setting_subcategory = 'id' ";
-			$sql .= "and contact_setting_value = '".$contact_id."' ";
-			$sql .= "and contact_setting_enabled = 'true' ";
-			$prep_statement = $db->prepare($sql);
-			$prep_statement->execute();
-			$result = $prep_statement->fetch(PDO::FETCH_ASSOC);
-			if ($result['contact_uuid'] != '') {
-				$duplicate_exists = true;
-				$duplicate_contact_uuid = $result['contact_uuid'];
-			}
-			else {
-				$duplicate_exists = false;
-			}
-			unset($sql, $prep_statement, $result);
-
-			//skip importing contact
-			if ($duplicate_exists && $_POST['import_duplicates'] == 'skip') {
-				$contacts_skipped++;
-				continue;
-			}
-			//replace contact (delete before inserts below)
-			else if ($duplicate_exists && $_POST['import_duplicates'] == 'replace') {
-				$contact_uuid = $duplicate_contact_uuid;
-				$included = true;
-				require_once "contact_delete.php";
-				unset($contact_uuid, $duplicate_contact_uuid);
-				$contacts_replaced++;
-			}
-
-			//extract contact record from array using contact id
-			$contact = $_SESSION['contact_auth']['google'][$contact_id];
-
-			//insert contact
-			$contact_uuid = uuid();
-			$sql = "insert into v_contacts ";
-			$sql .= "( ";
-			$sql .= "domain_uuid, ";
-			$sql .= "contact_uuid, ";
-			$sql .= "contact_type, ";
-			$sql .= "contact_organization, ";
-			$sql .= "contact_name_prefix, ";
-			$sql .= "contact_name_given, ";
-			$sql .= "contact_name_middle, ";
-			$sql .= "contact_name_family, ";
-			$sql .= "contact_name_suffix, ";
-			$sql .= "contact_nickname, ";
-			$sql .= "contact_title, ";
-			$sql .= "contact_category, ";
-			$sql .= "contact_note ";
-			$sql .= ") ";
-			$sql .= "values ";
-			$sql .= "( ";
-			$sql .= "'".$_SESSION['domain_uuid']."', ";
-			$sql .= "'".$contact_uuid."', ";
-			$sql .= "'".check_str($_POST['import_type'])."', ";
-			$sql .= "'".check_str($contact['organization'])."', ";
-			$sql .= "'".check_str($contact['name_prefix'])."', ";
-			$sql .= "'".check_str($contact['name_given'])."', ";
-			$sql .= "'".check_str($contact['name_middle'])."', ";
-			$sql .= "'".check_str($contact['name_family'])."', ";
-			$sql .= "'".check_str($contact['name_suffix'])."', ";
-			$sql .= "'".check_str($contact['nickname'])."', ";
-			$sql .= "'".check_str($contact['title'])."', ";
-			$sql .= "'".check_str($_POST['import_category'])."', ";
-			$sql .= "'".check_str($contact['notes'])."' ";
-			$sql .= ")";
-			$db->exec(check_sql($sql));
-			unset($sql);
-
-			//set sharing
-			if ($_POST['import_shared'] != 'true') {
-				$sql = "insert into v_contact_groups ";
-				$sql .= "( ";
-				$sql .= "contact_group_uuid, ";
-				$sql .= "domain_uuid, ";
-				$sql .= "contact_uuid, ";
-				$sql .= "group_uuid ";
-				$sql .= ") ";
-				$sql .= "values ";
-				$sql .= "( ";
-				$sql .= "'".uuid()."', ";
-				$sql .= "'".$_SESSION['domain_uuid']."', ";
-				$sql .= "'".$contact_uuid."', ";
-				$sql .= "'".$_SESSION["user_uuid"]."' ";
-				$sql .= ")";
-				$db->exec(check_sql($sql));
-				unset($sql);
-			}
-
-			//insert emails
-			if ($_POST['import_fields']['email'] && sizeof($contact['emails']) > 0) {
-				foreach ($contact['emails'] as $contact_email) {
-					$sql = "insert into v_contact_emails ";
-					$sql .= "(";
-					$sql .= "domain_uuid, ";
-					$sql .= "contact_uuid, ";
-					$sql .= "contact_email_uuid, ";
-					$sql .= "email_label, ";
-					$sql .= "email_address, ";
-					$sql .= "email_primary ";
-					$sql .= ") ";
-					$sql .= "values ";
-					$sql .= "(";
-					$sql .= "'".$_SESSION['domain_uuid']."', ";
-					$sql .= "'".$contact_uuid."', ";
-					$sql .= "'".uuid()."', ";
-					$sql .= "'".check_str($contact_email['label'])."', ";
-					$sql .= "'".check_str($contact_email['address'])."', ";
-					$sql .= (($contact_email['primary']) ? 1 : 0)." ";
-					$sql .= ")";
-					$db->exec(check_sql($sql));
-					unset($sql);
-				}
-			}
-
-			//insert numbers
-			if ($_POST['import_fields']['number'] && sizeof($contact['numbers']) > 0) {
-				foreach ($contact['numbers'] as $contact_number) {
-					$sql = "insert into v_contact_phones ";
-					$sql .= "(";
-					$sql .= "domain_uuid, ";
-					$sql .= "contact_uuid, ";
-					$sql .= "contact_phone_uuid, ";
-					$sql .= "phone_type_voice, ";
-					$sql .= "phone_type_fax, ";
-					$sql .= "phone_label, ";
-					$sql .= "phone_number, ";
-					$sql .= "phone_primary ";
-					$sql .= ") ";
-					$sql .= "values ";
-					$sql .= "(";
-					$sql .= "'".$domain_uuid."', ";
-					$sql .= "'".$contact_uuid."', ";
-					$sql .= "'".uuid()."', ";
-					$sql .= ((substr_count(strtoupper($contact_number['label']), strtoupper($text['label-fax'])) == 0) ? 1 : 'null').", ";
-					$sql .= ((substr_count(strtoupper($contact_number['label']), strtoupper($text['label-fax'])) != 0) ? 1 : 'null').", ";
-					$sql .= "'".check_str($contact_number['label'])."', ";
-					$sql .= "'".check_str($contact_number['number'])."', ";
-					$sql .= ((sizeof($contact['numbers']) == 1) ? 1 : 0)." ";
-					$sql .= ")";
-					$db->exec(check_sql($sql));
-					unset($sql);
-				}
-			}
-
-			//insert urls
-			if ($_POST['import_fields']['url'] && sizeof($contact['urls']) > 0) {
-				foreach ($contact['urls'] as $contact_url) {
-					$sql = "insert into v_contact_urls ";
-					$sql .= "(";
-					$sql .= "domain_uuid, ";
-					$sql .= "contact_uuid, ";
-					$sql .= "contact_url_uuid, ";
-					$sql .= "url_label, ";
-					$sql .= "url_address, ";
-					$sql .= "url_primary ";
-					$sql .= ") ";
-					$sql .= "values ";
-					$sql .= "(";
-					$sql .= "'".$_SESSION['domain_uuid']."', ";
-					$sql .= "'".$contact_uuid."', ";
-					$sql .= "'".uuid()."', ";
-					$sql .= "'".check_str($contact_url['label'])."', ";
-					$sql .= "'".check_str($contact_url['url'])."', ";
-					$sql .= ((sizeof($contact['urls']) == 1) ? 1 : 0)." ";
-					$sql .= ")";
-					$db->exec(check_sql($sql));
-					unset($sql);
-				}
-			}
-
-			//insert addresses
-			if ($_POST['import_fields']['address'] && sizeof($contact['addresses']) > 0) {
-				foreach ($contact['addresses'] as $contact_address) {
-					$sql = "insert into v_contact_addresses ";
-					$sql .= "(";
-					$sql .= "domain_uuid, ";
-					$sql .= "contact_uuid, ";
-					$sql .= "contact_address_uuid, ";
-					$sql .= "address_type, ";
-					$sql .= "address_label, ";
-					$sql .= "address_street, ";
-					$sql .= "address_extended, ";
-					$sql .= "address_community, ";
-					$sql .= "address_locality, ";
-					$sql .= "address_region, ";
-					$sql .= "address_postal_code, ";
-					$sql .= "address_country, ";
-					$sql .= "address_primary ";
-					$sql .= ") ";
-					$sql .= "values ";
-					$sql .= "(";
-					$sql .= "'".$_SESSION['domain_uuid']."', ";
-					$sql .= "'".$contact_uuid."', ";
-					$sql .= "'".uuid()."', ";
-					if (substr_count(strtoupper($contact_address['label']), strtoupper($text['option-home'])) != 0) {
-						$sql .= "'home', "; // vcard address type
-					}
-					else if (substr_count(strtoupper($contact_address['label']), strtoupper($text['option-work'])) != 0) {
-						$sql .= "'work', "; // vcard address type
-					}
-					else {
-						$sql .= "'', ";
-					}
-					$sql .= "'".check_str($contact_address['label'])."', ";
-					$sql .= "'".check_str($contact_address['street'])."', ";
-					$sql .= "'".check_str($contact_address['extended'])."', ";
-					$sql .= "'".check_str($contact_address['community'])."', ";
-					$sql .= "'".check_str($contact_address['locality'])."', ";
-					$sql .= "'".check_str($contact_address['region'])."', ";
-					$sql .= "'".check_str($contact_address['postal_code'])."', ";
-					$sql .= "'".check_str($contact_address['country'])."', ";
-					$sql .= ((sizeof($contact['addresses']) == 1) ? 1 : 0)." ";
-					$sql .= ")";
-					$db->exec(check_sql($sql));
-					unset($sql);
-				}
-			}
-
-			//add google contact id, etag and updated date to contact settings
-			$contact['updated'] = str_replace('T', ' ', $contact['updated']);
-			$contact['updated'] = str_replace('Z', '', $contact['updated']);
-			$sql = "insert into v_contact_settings ";
-			$sql .= "(";
-			$sql .= "contact_setting_uuid, ";
-			$sql .= "contact_uuid, ";
-			$sql .= "domain_uuid, ";
-			$sql .= "contact_setting_category, ";
-			$sql .= "contact_setting_subcategory, ";
-			$sql .= "contact_setting_name, ";
-			$sql .= "contact_setting_value, ";
-			$sql .= "contact_setting_order, ";
-			$sql .= "contact_setting_enabled ";
-			$sql .= ") ";
-			$sql .= "values ";
-			$sql .= "('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'sync', 'source', 'array', 'google', 0, 'true' )";
-			$sql .= ",('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'google', 'id', 'text', '".check_str($contact_id)."', 0, 'true' )";
-			$sql .= ",('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'google', 'updated', 'date', '".check_str($contact['updated'])."', 0, 'true' )";
-			$sql .= ",('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'google', 'etag', 'text', '".check_str($contact['etag'])."', 0, 'true' )";
-			$db->exec(check_sql($sql));
-			unset($sql);
-
-			$contacts_imported++;
-
-		}
-
-		$message = $text['message-contacts_imported']." ".$contacts_imported;
-		if ($contacts_replaced > 0) { $message .= " (".$text['message_contacts_imported_replaced']." ".$contacts_replaced.")"; }
-		if ($contacts_skipped > 0) { $message .= ", ".$text['message_contacts_imported_skipped']." ".$contacts_skipped; }
-		$_SESSION["message"] = $message;
-		header("Location: contacts.php");
-		exit;
-
-	}
-	else {
-
-		// no contacts imported
-		$_SESSION['message_mood'] = 'negative';
-		$_SESSION["message"] = $text['message-contacts_imported']." ".$contacts_imported;
-
-	}
-}
-
-//*******************************************************************************************
-
-//check if authenticated
-if ($_SESSION['contact_auth']['token'] == '') {
-	$_SESSION['contact_auth']['referer'] = substr($_SERVER["HTTP_REFERER"], strrpos($_SERVER["HTTP_REFERER"],'/')+1);
-	header("Location: contact_auth.php?source=google&target=".substr($_SERVER["PHP_SELF"], strrpos($_SERVER["PHP_SELF"],'/')+1));
-	exit;
-}
-
-unset($_SESSION['contact_auth']['source'], $_SESSION['contact_auth']['target']);
-
-//get groups & contacts
-$groups = google_get_groups($_SESSION['contact_auth']['token']);
-$contacts = google_get_contacts($_SESSION['contact_auth']['token'], 1000);
-
-//store in session variable for use on import
-$_SESSION['contact_auth']['google'] = $contacts;
-
-//include the header
-$document['title'] = $text['title-contacts_import_google'];
-require_once "resources/header.php";
-
-echo "<table cellpadding='0' cellspacing='0' border='0' align='right'>";
-echo "	<tr>";
-echo "		<td style='text-align: right;'>";
-echo "			<input type='button' class='btn' id='btn_back' onclick=\"document.location.href='contact_import.php';\" value=\"".$text['button-back']."\">";
-echo "			<input type='button' class='btn' id='btn_refresh' onclick='document.location.reload();' value=\"".$text['button-reload']."\">";
-echo "			<input type='button' class='btn' id='btn_signout' onclick=\"document.location.href='contact_auth.php?source=google&signout'\" value=\"".$text['button-sign_out']."\">";
-echo "		</td>";
-echo "	</tr>";
-echo "	<tr>";
-echo "		<td style='text-align: right; white-space: nowrap; padding-top: 8px;'><span style='font-weight: bold; color: #000;'>".$_SESSION['contact_auth']['name']."</a> (<a href='https://www.google.com/contacts/#contacts' target='_blank'>".$_SESSION['contact_auth']['email']."</a>)"."</td>";
-echo "	</tr>";
-echo "</table>";
-echo "<b>".$text['header-contacts_import_google']."</b>";
-echo "<br><br>";
-echo $text['description-contacts_import_google'];
-echo "<br><br><br>";
-
-$row_style["0"] = "row_style0";
-$row_style["1"] = "row_style1";
-
-echo "<form name='frm_import' id='frm_import' method='post'>\n";
-echo "<input type='hidden' name='a' value='import'>\n";
-
-echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
-
-echo "<tr>\n";
-echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
-echo "	".$text['label-import_fields']."\n";
-echo "</td>\n";
-echo "<td width='70%' class='vtable' align='left'>\n";
-echo "	<input type='checkbox' disabled='disabled' checked>&nbsp;".$text['label-contact_name']."&nbsp;\n";
-echo "	<input type='checkbox' disabled='disabled' checked>&nbsp;".$text['label-contact_organization']."&nbsp;\n";
-echo "	<input type='checkbox' name='import_fields[email]' id='field_email' value='1' checked><label for='field_email'>&nbsp;".$text['label-contact_email']."</label>&nbsp;\n";
-echo "	<input type='checkbox' name='import_fields[number]' id='field_number' value='1' checked><label for='field_number'>&nbsp;".$text['label-phone_number']."</label>&nbsp;\n";
-echo "	<input type='checkbox' name='import_fields[url]' id='field_url' value='1' checked><label for='field_url'>&nbsp;".$text['label-contact_url']."</label>&nbsp;\n";
-echo "	<input type='checkbox' name='import_fields[address]' id='field_address' value='1' checked><label for='field_address'>&nbsp;".$text['label-address_address']."</label>\n";
-echo "<br />\n";
-echo $text['description-import_fields']."\n";
-echo "</td>\n";
-echo "</tr>\n";
-
-echo "<tr>\n";
-echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
-echo "	".$text['label-contact_type']."\n";
-echo "</td>\n";
-echo "<td class='vtable' align='left'>\n";
-if (is_array($_SESSION["contact"]["type"])) {
-	sort($_SESSION["contact"]["type"]);
-	echo "	<select class='formfld' name='import_type'>\n";
-	echo "		<option value=''></option>\n";
-	foreach($_SESSION["contact"]["type"] as $row) {
-		echo "	<option value='".$row."'>".$row."</option>\n";
-	}
-	echo "	</select>\n";
-}
-else {
-	echo "	<select class='formfld' name='import_type'>\n";
-	echo "		<option value=''></option>\n";
-	echo "		<option value='customer'>".$text['option-contact_type_customer']."</option>\n";
-	echo "		<option value='contractor'>".$text['option-contact_type_contractor']."</option>\n";
-	echo "		<option value='friend'>".$text['option-contact_type_friend']."</option>\n";
-	echo "		<option value='lead'>".$text['option-contact_type_lead']."</option>\n";
-	echo "		<option value='member'>".$text['option-contact_type_member']."</option>\n";
-	echo "		<option value='family'>".$text['option-contact_type_family']."</option>\n";
-	echo "		<option value='subscriber'>".$text['option-contact_type_subscriber']."</option>\n";
-	echo "		<option value='supplier'>".$text['option-contact_type_supplier']."</option>\n";
-	echo "		<option value='provider'>".$text['option-contact_type_provider']."</option>\n";
-	echo "		<option value='user'>".$text['option-contact_type_user']."</option>\n";
-	echo "		<option value='volunteer'>".$text['option-contact_type_volunteer']."</option>\n";
-	echo "	</select>\n";
-}
-echo "<br />\n";
-echo $text['description-contact_type_import']."\n";
-echo "</td>\n";
-echo "</tr>\n";
-
-echo "<tr>\n";
-echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
-echo "	".$text['label-contact_category']."\n";
-echo "</td>\n";
-echo "<td class='vtable' align='left'>\n";
-if (is_array($_SESSION["contact"]["category"])) {
-	sort($_SESSION["contact"]["category"]);
-	echo "	<select class='formfld' name='import_category'>\n";
-	echo "		<option value=''></option>\n";
-	foreach($_SESSION["contact"]["category"] as $row) {
-		echo "	<option value='".$row."'>".$row."</option>\n";
-	}
-	echo "	</select>\n";
-}
-else {
-	echo "	<input class='formfld' type='text' name='import_category' maxlength='255'>\n";
-}
-echo "<br />\n";
-echo $text['description-contact_category_import']."\n";
-echo "</td>\n";
-echo "</tr>\n";
-
-echo "<tr>\n";
-echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
-echo "	".$text['label-shared']."\n";
-echo "</td>\n";
-echo "<td class='vtable' align='left'>\n";
-echo "	<select class='formfld' name='import_shared' id='import_shared'>\n";
-echo "		<option value='false'>".$text['option-false']."</option>\n";
-echo "		<option value='true'>".$text['option-true']."</option>\n";
-echo "	</select>\n";
-echo "	<br />\n";
-echo $text['description-shared_import']."\n";
-echo "</td>\n";
-echo "</tr>\n";
-
-echo "<tr>\n";
-echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
-echo "    ".$text['label-import_duplicates']."\n";
-echo "</td>\n";
-echo "<td class='vtable' align='left'>\n";
-echo "    <select class='formfld' style='width: 150px;' name='import_duplicates'>\n";
-echo "    <option value='skip'>".$text['option-import_duplicates_skip']."</option>\n";
-echo "    <option value='replace'>".$text['option-import_duplicates_replace']."</option>\n";
-echo "    </select>\n";
-echo "<br />\n";
-echo $text['description-import_duplicates']."\n";
-echo "</td>\n";
-echo "</tr>\n";
-
-echo "</table>";
-echo "<br><br>";
-
-//display groups
-echo "<b>".$text['label-groups']."</b>";
-echo "<br><br>";
-
-echo "<table class='tr_hover' width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
-echo "<tr>\n";
-echo "	<th style='width: 30px; text-align: center; padding: 0px;'>&nbsp;</th>";
-echo "	<th>".$text['label-contact_name']."</th>\n";
-echo "</tr>\n";
-
-//determine contact count in groups
-foreach ($contacts as $contact) {
-	foreach ($contact['groups'] as $group_id => $meh) {
-		$groups[$group_id]['count']++;
-	}
-}
-
-$c = 0;
-foreach ($groups as $group['id'] => $group) {
-	if ($group['count'] > 0) {
-		echo "<tr>\n";
-		echo "	<td valign='top' class='".$row_style[$c]."' style='text-align: center; padding: 3px 0px 0px 0px;'><input type='checkbox' name='group_id[]' id='group_id_".$group['id']."' value='".$group['id']."'></td>\n";
-		echo "	<td valign='top' class='".$row_style[$c]."' onclick=\"document.getElementById('group_id_".$group['id']."').checked = (document.getElementById('group_id_".$group['id']."').checked) ? false : true;\">".$group['name']." (".$group['count'].")</td>\n";
-		echo "</tr>\n";
-		$c=($c)?0:1;
-	}
-}
-echo "</table>\n";
-echo "<br>";
-
-echo "<div style='text-align: right;'><input type='submit' class='btn' id='btn_submit' value=\"".$text['button-import']."\"></div>";
-
-echo "<br>";
-
-//display contacts
-echo "<b>".$text['header-contacts']."</b>";
-echo "<br><br>";
-
-echo "<table class='tr_hover' width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
-echo "<tr>\n";
-echo "	<th style='width: 30px; text-align: center; padding: 0px;'><input type='checkbox' onchange=\"(this.checked) ? check('all') : check('none');\"></th>";
-echo "	<th>".$text['label-contact_name']."</th>\n";
-echo "	<th>".$text['label-contact_organization']."</th>\n";
-echo "	<th>".$text['label-contact_email']."</th>\n";
-echo "	<th>".$text['label-phone_number']."</th>\n";
-echo "	<th>".$text['label-contact_url']."</th>\n";
-echo "	<th>".$text['label-address_address']."</th>\n";
-echo "	<th>".$text['label-group']."</th>\n";
-echo "</tr>\n";
-$c = 0;
-foreach ($contacts as $contact['id'] => $contact) {
-	$contact_ids[] = $contact['id'];
-	echo "<tr>\n";
-	echo "	<td valign='top' class='".$row_style[$c]."' style='text-align: center; padding: 3px 0px 0px 0px;'><input type='checkbox' name='contact_id[]' id='contact_id_".$contact['id']."' value='".$contact['id']."'></td>\n";
-	echo "	<td valign='top' class='".$row_style[$c]."' onclick=\"document.getElementById('contact_id_".$contact['id']."').checked = (document.getElementById('contact_id_".$contact['id']."').checked) ? false : true;\">";
-	$contact_name[] = $contact['name_prefix'];
-	$contact_name[] = $contact['name_given'];
-	$contact_name[] = $contact['name_middle'];
-	$contact_name[] = $contact['name_family'];
-	$contact_name[] = $contact['name_suffix'];
-	echo "		".implode(' ', $contact_name)."&nbsp;";
-	unset($contact_name);
-	echo "	</td>\n";
-	echo "	<td valign='top' class='".$row_style[$c]."' style='max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
-	echo "		".(($contact['title']) ? $contact['title']."<br>" : null).$contact['organization']."&nbsp;";
-	echo "	</td>\n";
-	echo "	<td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
-	if (sizeof($contact['emails']) > 0) {
- 		foreach ($contact['emails'] as $contact_email) {
- 			$contact_emails[] = "<span style='font-size: 80%;'>".$contact_email['label'].":</span> <a href='mailto: ".$contact_email['address']."'>".$contact_email['address']."</a>";
- 		}
-		echo implode('<br>', $contact_emails);
-		unset($contact_emails);
-	} else { echo "&nbsp;"; }
-	echo "	</td>\n";
-	echo "	<td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
-	if (sizeof($contact['numbers']) > 0) {
-		foreach ($contact['numbers'] as $contact_number) {
-			$contact_number_part = "<span style='font-size: 80%;'>".$contact_number['label'].":</span> ";
-			if (substr_count(strtoupper($contact_number['label']), 'FAX') == 0) {
-				$contact_number_part .= "<a href='javascript:void(0);' onclick=\"send_cmd('".PROJECT_PATH."/app/click_to_call/click_to_call.php?src_cid_name=".urlencode($contact_number['number'])."&src_cid_number=".urlencode($contact_number['number'])."&dest_cid_name=".urlencode($_SESSION['user']['extension'][0]['outbound_caller_id_name'])."&dest_cid_number=".urlencode($_SESSION['user']['extension'][0]['outbound_caller_id_number'])."&src=".urlencode($_SESSION['user']['extension'][0]['user'])."&dest=".urlencode($contact_number['number'])."&rec=false&ringback=us-ring&auto_answer=true');\">";
-			}
-			$contact_number_part .= format_phone($contact_number['number']);
-			if (substr_count(strtoupper($contact_number['label']), 'FAX') == 0) {
-				$contact_number_part .= "</a>";
-			}
-			$contact_numbers[] = $contact_number_part;
-			unset($contact_number_part);
-		}
-		echo implode('<br>', $contact_numbers);
-		unset($contact_numbers);
-	} else { echo "&nbsp;"; }
-	echo "	</td>\n";
-	echo "	<td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
-	if (sizeof($contact['urls']) > 0) {
-		foreach ($contact['urls'] as $contact_url) {
-			$contact_urls[] = "<span style='font-size: 80%;'>".$contact_url['label'].":</span> <a href='".$contact_url['url']."' target='_blank'>".str_replace("http://", "", str_replace("https://", "", $contact_url['url']))."</a>";
-		}
-		echo implode('<br>', $contact_urls);
-		unset($contact_urls);
-	} else { echo "&nbsp;"; }
-	echo "	</td>\n";
-	echo "	<td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
-	if (sizeof($contact['addresses']) > 0) {
-		foreach ($contact['addresses'] as $contact_address) {
-			if ($contact_address['street'] != '') { $contact_address_parts[] = $contact_address['street']; }
-			if ($contact_address['extended'] != '') { $contact_address_parts[] = $contact_address['extended']; }
-			if ($contact_address['community'] != '') { $contact_address_parts[] = $contact_address['community']; }
-			if ($contact_address['locality'] != '') { $contact_address_parts[] = $contact_address['locality']; }
-			if ($contact_address['region'] != '') { $contact_address_parts[] = $contact_address['region']; }
-			if ($contact_address['postal_code'] != '') { $contact_address_parts[] = $contact_address['postal_code']; }
-			if ($contact_address['country'] != '') { $contact_address_parts[] = $contact_address['country']; }
-			$contact_addresses[] = "<span style='font-size: 80%;'>".$contact_address['label'].":</span> ".implode(', ', $contact_address_parts);
-			unset($contact_address_parts);
-		}
-		echo implode('<br>', $contact_addresses);
-		unset($contact_addresses);
-	} else { echo "&nbsp;"; }
-	echo "	</td>\n";
-	echo "	<td valign='top' class='".$row_style[$c]."' style='white-space: nowrap;'>";
-	foreach ($contact['groups'] as $contact_group['id'] => $contact_group['name']) {
-		$contact_groups[] = $contact_group['name'];
-	}
-	echo "		".implode('<br>', $contact_groups);
-	unset($contact_groups);
-	echo "	</td>\n";
-	echo "</tr>\n";
-	$c=($c)?0:1;
-}
-echo "</table>\n";
-echo "<br>";
-
-echo "<div style='text-align: right;'><input type='submit' class='btn' id='btn_submit' value=\"".$text['button-import']."\"></div>";
-
-echo "</form>";
-echo "<br><br>";
-
-// check or uncheck all contact checkboxes
-if (sizeof($contact_ids) > 0) {
-	echo "<script>\n";
-	echo "	function check(what) {\n";
-	foreach ($contact_ids as $contact_id) {
-		echo "	document.getElementById('contact_id_".$contact_id."').checked = (what == 'all') ? true : false;\n";
-	}
-	echo "	}\n";
-	echo "</script>\n";
-}
-
-/*
-echo "<pre>";
-print_r($contacts);
-echo "</pre>";
-echo "<br><br>";
-
-echo "<hr>";
-echo "<br><br><b>SOURCE JSON DECODED ARRAY</b>...<br><br><pre>";
-print_r($records);
-echo "</pre>";
-*/
-
-//include the footer
-require_once "resources/footer.php";
-
-
-
-
-// used above
-function curl_file_get_contents($url) {
-	$curl = curl_init();
-	$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
-
-	curl_setopt($curl, CURLOPT_URL, $url);	//The URL to fetch. This can also be set when initializing a session with curl_init().
-	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);	//TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
-	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);	//The number of seconds to wait while trying to connect.
-	curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);	//The contents of the "User-Agent: " header to be used in a HTTP request.
-	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);	//To follow any "Location: " header that the server sends as part of the HTTP header.
-	curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);	//To automatically set the Referer: field in requests where it follows a Location: redirect.
-	curl_setopt($curl, CURLOPT_TIMEOUT, 10);	//The maximum number of seconds to allow cURL functions to execute.
-	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);	//To stop cURL from verifying the peer's certificate.
-
-	$contents = curl_exec($curl);
-	curl_close($curl);
-	return $contents;
-}
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2013
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+require_once "root.php";
+require_once "resources/require.php";
+require_once "resources/check_auth.php";
+require_once "resources/functions/google_get_groups.php";
+require_once "resources/functions/google_get_contacts.php";
+
+if (permission_exists('contact_add')) {
+	//access granted
+}
+else {
+	echo "access denied";
+	exit;
+}
+
+//add multi-lingual support
+	$language = new text;
+	$text = $language->get();
+
+//handle import
+if ($_POST['a'] == 'import') {
+	if (sizeof($_POST['group_id']) > 0) {
+		//get contact ids for those in the submitted groups
+		if (sizeof($_SESSION['contact_auth']['google']) > 0) {
+			foreach ($_SESSION['contact_auth']['google'] as $contact['id'] => $contact) {
+				foreach ($contact['groups'] as $contact_group['id'] => $meh) {
+					if (in_array($contact_group['id'], $_POST['group_id'])) {
+						$import_ids[] = $contact['id'];
+					}
+				}
+			}
+		}
+	}
+
+	if (sizeof($_POST['contact_id']) > 0) {
+		foreach ($_POST['contact_id'] as $contact_id) {
+			$import_ids[] = $contact_id;
+		}
+	}
+
+	//iterate selected contact ids, insert contact into database
+	$contacts_imported = 0;
+	$contacts_skipped = 0;
+	$contacts_replaced = 0;
+
+	if (sizeof($import_ids) > 0) {
+
+		$import_ids = array_unique($import_ids);
+		foreach ($import_ids as $contact_id) {
+
+			//check for duplicate contact (already exists, previously imported, etc)
+			$sql = "select contact_uuid from v_contact_settings ";
+			$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
+			$sql .= "and contact_setting_category = 'google' ";
+			$sql .= "and contact_setting_subcategory = 'id' ";
+			$sql .= "and contact_setting_value = '".$contact_id."' ";
+			$sql .= "and contact_setting_enabled = 'true' ";
+			$prep_statement = $db->prepare($sql);
+			$prep_statement->execute();
+			$result = $prep_statement->fetch(PDO::FETCH_ASSOC);
+			if ($result['contact_uuid'] != '') {
+				$duplicate_exists = true;
+				$duplicate_contact_uuid = $result['contact_uuid'];
+			}
+			else {
+				$duplicate_exists = false;
+			}
+			unset($sql, $prep_statement, $result);
+
+			//skip importing contact
+			if ($duplicate_exists && $_POST['import_duplicates'] == 'skip') {
+				$contacts_skipped++;
+				continue;
+			}
+			//replace contact (delete before inserts below)
+			else if ($duplicate_exists && $_POST['import_duplicates'] == 'replace') {
+				$contact_uuid = $duplicate_contact_uuid;
+				$included = true;
+				require_once "contact_delete.php";
+				unset($contact_uuid, $duplicate_contact_uuid);
+				$contacts_replaced++;
+			}
+
+			//extract contact record from array using contact id
+			$contact = $_SESSION['contact_auth']['google'][$contact_id];
+
+			//insert contact
+			$contact_uuid = uuid();
+			$sql = "insert into v_contacts ";
+			$sql .= "( ";
+			$sql .= "domain_uuid, ";
+			$sql .= "contact_uuid, ";
+			$sql .= "contact_type, ";
+			$sql .= "contact_organization, ";
+			$sql .= "contact_name_prefix, ";
+			$sql .= "contact_name_given, ";
+			$sql .= "contact_name_middle, ";
+			$sql .= "contact_name_family, ";
+			$sql .= "contact_name_suffix, ";
+			$sql .= "contact_nickname, ";
+			$sql .= "contact_title, ";
+			$sql .= "contact_category, ";
+			$sql .= "contact_note ";
+			$sql .= ") ";
+			$sql .= "values ";
+			$sql .= "( ";
+			$sql .= "'".$_SESSION['domain_uuid']."', ";
+			$sql .= "'".$contact_uuid."', ";
+			$sql .= "'".check_str($_POST['import_type'])."', ";
+			$sql .= "'".check_str($contact['organization'])."', ";
+			$sql .= "'".check_str($contact['name_prefix'])."', ";
+			$sql .= "'".check_str($contact['name_given'])."', ";
+			$sql .= "'".check_str($contact['name_middle'])."', ";
+			$sql .= "'".check_str($contact['name_family'])."', ";
+			$sql .= "'".check_str($contact['name_suffix'])."', ";
+			$sql .= "'".check_str($contact['nickname'])."', ";
+			$sql .= "'".check_str($contact['title'])."', ";
+			$sql .= "'".check_str($_POST['import_category'])."', ";
+			$sql .= "'".check_str($contact['notes'])."' ";
+			$sql .= ")";
+			$db->exec(check_sql($sql));
+			unset($sql);
+
+			//set sharing
+			if ($_POST['import_shared'] != 'true') {
+				$sql = "insert into v_contact_groups ";
+				$sql .= "( ";
+				$sql .= "contact_group_uuid, ";
+				$sql .= "domain_uuid, ";
+				$sql .= "contact_uuid, ";
+				$sql .= "group_uuid ";
+				$sql .= ") ";
+				$sql .= "values ";
+				$sql .= "( ";
+				$sql .= "'".uuid()."', ";
+				$sql .= "'".$_SESSION['domain_uuid']."', ";
+				$sql .= "'".$contact_uuid."', ";
+				$sql .= "'".$_SESSION["user_uuid"]."' ";
+				$sql .= ")";
+				$db->exec(check_sql($sql));
+				unset($sql);
+			}
+
+			//insert emails
+			if ($_POST['import_fields']['email'] && sizeof($contact['emails']) > 0) {
+				foreach ($contact['emails'] as $contact_email) {
+					$sql = "insert into v_contact_emails ";
+					$sql .= "(";
+					$sql .= "domain_uuid, ";
+					$sql .= "contact_uuid, ";
+					$sql .= "contact_email_uuid, ";
+					$sql .= "email_label, ";
+					$sql .= "email_address, ";
+					$sql .= "email_primary ";
+					$sql .= ") ";
+					$sql .= "values ";
+					$sql .= "(";
+					$sql .= "'".$_SESSION['domain_uuid']."', ";
+					$sql .= "'".$contact_uuid."', ";
+					$sql .= "'".uuid()."', ";
+					$sql .= "'".check_str($contact_email['label'])."', ";
+					$sql .= "'".check_str($contact_email['address'])."', ";
+					$sql .= (($contact_email['primary']) ? 1 : 0)." ";
+					$sql .= ")";
+					$db->exec(check_sql($sql));
+					unset($sql);
+				}
+			}
+
+			//insert numbers
+			if ($_POST['import_fields']['number'] && sizeof($contact['numbers']) > 0) {
+				foreach ($contact['numbers'] as $contact_number) {
+					$sql = "insert into v_contact_phones ";
+					$sql .= "(";
+					$sql .= "domain_uuid, ";
+					$sql .= "contact_uuid, ";
+					$sql .= "contact_phone_uuid, ";
+					$sql .= "phone_type_voice, ";
+					$sql .= "phone_type_fax, ";
+					$sql .= "phone_label, ";
+					$sql .= "phone_number, ";
+					$sql .= "phone_primary ";
+					$sql .= ") ";
+					$sql .= "values ";
+					$sql .= "(";
+					$sql .= "'".$domain_uuid."', ";
+					$sql .= "'".$contact_uuid."', ";
+					$sql .= "'".uuid()."', ";
+					$sql .= ((substr_count(strtoupper($contact_number['label']), strtoupper($text['label-fax'])) == 0) ? 1 : 'null').", ";
+					$sql .= ((substr_count(strtoupper($contact_number['label']), strtoupper($text['label-fax'])) != 0) ? 1 : 'null').", ";
+					$sql .= "'".check_str($contact_number['label'])."', ";
+					$sql .= "'".check_str($contact_number['number'])."', ";
+					$sql .= ((sizeof($contact['numbers']) == 1) ? 1 : 0)." ";
+					$sql .= ")";
+					$db->exec(check_sql($sql));
+					unset($sql);
+				}
+			}
+
+			//insert urls
+			if ($_POST['import_fields']['url'] && sizeof($contact['urls']) > 0) {
+				foreach ($contact['urls'] as $contact_url) {
+					$sql = "insert into v_contact_urls ";
+					$sql .= "(";
+					$sql .= "domain_uuid, ";
+					$sql .= "contact_uuid, ";
+					$sql .= "contact_url_uuid, ";
+					$sql .= "url_label, ";
+					$sql .= "url_address, ";
+					$sql .= "url_primary ";
+					$sql .= ") ";
+					$sql .= "values ";
+					$sql .= "(";
+					$sql .= "'".$_SESSION['domain_uuid']."', ";
+					$sql .= "'".$contact_uuid."', ";
+					$sql .= "'".uuid()."', ";
+					$sql .= "'".check_str($contact_url['label'])."', ";
+					$sql .= "'".check_str($contact_url['url'])."', ";
+					$sql .= ((sizeof($contact['urls']) == 1) ? 1 : 0)." ";
+					$sql .= ")";
+					$db->exec(check_sql($sql));
+					unset($sql);
+				}
+			}
+
+			//insert addresses
+			if ($_POST['import_fields']['address'] && sizeof($contact['addresses']) > 0) {
+				foreach ($contact['addresses'] as $contact_address) {
+					$sql = "insert into v_contact_addresses ";
+					$sql .= "(";
+					$sql .= "domain_uuid, ";
+					$sql .= "contact_uuid, ";
+					$sql .= "contact_address_uuid, ";
+					$sql .= "address_type, ";
+					$sql .= "address_label, ";
+					$sql .= "address_street, ";
+					$sql .= "address_extended, ";
+					$sql .= "address_community, ";
+					$sql .= "address_locality, ";
+					$sql .= "address_region, ";
+					$sql .= "address_postal_code, ";
+					$sql .= "address_country, ";
+					$sql .= "address_primary ";
+					$sql .= ") ";
+					$sql .= "values ";
+					$sql .= "(";
+					$sql .= "'".$_SESSION['domain_uuid']."', ";
+					$sql .= "'".$contact_uuid."', ";
+					$sql .= "'".uuid()."', ";
+					if (substr_count(strtoupper($contact_address['label']), strtoupper($text['option-home'])) != 0) {
+						$sql .= "'home', "; // vcard address type
+					}
+					else if (substr_count(strtoupper($contact_address['label']), strtoupper($text['option-work'])) != 0) {
+						$sql .= "'work', "; // vcard address type
+					}
+					else {
+						$sql .= "'', ";
+					}
+					$sql .= "'".check_str($contact_address['label'])."', ";
+					$sql .= "'".check_str($contact_address['street'])."', ";
+					$sql .= "'".check_str($contact_address['extended'])."', ";
+					$sql .= "'".check_str($contact_address['community'])."', ";
+					$sql .= "'".check_str($contact_address['locality'])."', ";
+					$sql .= "'".check_str($contact_address['region'])."', ";
+					$sql .= "'".check_str($contact_address['postal_code'])."', ";
+					$sql .= "'".check_str($contact_address['country'])."', ";
+					$sql .= ((sizeof($contact['addresses']) == 1) ? 1 : 0)." ";
+					$sql .= ")";
+					$db->exec(check_sql($sql));
+					unset($sql);
+				}
+			}
+
+			//add google contact id, etag and updated date to contact settings
+			$contact['updated'] = str_replace('T', ' ', $contact['updated']);
+			$contact['updated'] = str_replace('Z', '', $contact['updated']);
+			$sql = "insert into v_contact_settings ";
+			$sql .= "(";
+			$sql .= "contact_setting_uuid, ";
+			$sql .= "contact_uuid, ";
+			$sql .= "domain_uuid, ";
+			$sql .= "contact_setting_category, ";
+			$sql .= "contact_setting_subcategory, ";
+			$sql .= "contact_setting_name, ";
+			$sql .= "contact_setting_value, ";
+			$sql .= "contact_setting_order, ";
+			$sql .= "contact_setting_enabled ";
+			$sql .= ") ";
+			$sql .= "values ";
+			$sql .= "('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'sync', 'source', 'array', 'google', 0, 'true' )";
+			$sql .= ",('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'google', 'id', 'text', '".check_str($contact_id)."', 0, 'true' )";
+			$sql .= ",('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'google', 'updated', 'date', '".check_str($contact['updated'])."', 0, 'true' )";
+			$sql .= ",('".uuid()."', '".$contact_uuid."', '".$_SESSION['domain_uuid']."', 'google', 'etag', 'text', '".check_str($contact['etag'])."', 0, 'true' )";
+			$db->exec(check_sql($sql));
+			unset($sql);
+
+			$contacts_imported++;
+
+		}
+
+		$message = $text['message-contacts_imported']." ".$contacts_imported;
+		if ($contacts_replaced > 0) { $message .= " (".$text['message_contacts_imported_replaced']." ".$contacts_replaced.")"; }
+		if ($contacts_skipped > 0) { $message .= ", ".$text['message_contacts_imported_skipped']." ".$contacts_skipped; }
+		$_SESSION["message"] = $message;
+		header("Location: contacts.php");
+		exit;
+
+	}
+	else {
+
+		// no contacts imported
+		$_SESSION['message_mood'] = 'negative';
+		$_SESSION["message"] = $text['message-contacts_imported']." ".$contacts_imported;
+
+	}
+}
+
+//*******************************************************************************************
+
+//check if authenticated
+if ($_SESSION['contact_auth']['token'] == '') {
+	$_SESSION['contact_auth']['referer'] = substr($_SERVER["HTTP_REFERER"], strrpos($_SERVER["HTTP_REFERER"],'/')+1);
+	header("Location: contact_auth.php?source=google&target=".substr($_SERVER["PHP_SELF"], strrpos($_SERVER["PHP_SELF"],'/')+1));
+	exit;
+}
+
+unset($_SESSION['contact_auth']['source'], $_SESSION['contact_auth']['target']);
+
+//get groups & contacts
+$groups = google_get_groups($_SESSION['contact_auth']['token']);
+$contacts = google_get_contacts($_SESSION['contact_auth']['token'], 1000);
+
+//store in session variable for use on import
+$_SESSION['contact_auth']['google'] = $contacts;
+
+//include the header
+$document['title'] = $text['title-contacts_import_google'];
+require_once "resources/header.php";
+
+echo "<table cellpadding='0' cellspacing='0' border='0' align='right'>";
+echo "	<tr>";
+echo "		<td style='text-align: right;'>";
+echo "			<input type='button' class='btn' id='btn_back' onclick=\"document.location.href='contact_import.php';\" value=\"".$text['button-back']."\">";
+echo "			<input type='button' class='btn' id='btn_refresh' onclick='document.location.reload();' value=\"".$text['button-reload']."\">";
+echo "			<input type='button' class='btn' id='btn_signout' onclick=\"document.location.href='contact_auth.php?source=google&signout'\" value=\"".$text['button-sign_out']."\">";
+echo "		</td>";
+echo "	</tr>";
+echo "	<tr>";
+echo "		<td style='text-align: right; white-space: nowrap; padding-top: 8px;'><span style='font-weight: bold; color: #000;'>".$_SESSION['contact_auth']['name']."</a> (<a href='https://www.google.com/contacts/#contacts' target='_blank'>".$_SESSION['contact_auth']['email']."</a>)"."</td>";
+echo "	</tr>";
+echo "</table>";
+echo "<b>".$text['header-contacts_import_google']."</b>";
+echo "<br><br>";
+echo $text['description-contacts_import_google'];
+echo "<br><br><br>";
+
+$row_style["0"] = "row_style0";
+$row_style["1"] = "row_style1";
+
+echo "<form name='frm_import' id='frm_import' method='post'>\n";
+echo "<input type='hidden' name='a' value='import'>\n";
+
+echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
+
+echo "<tr>\n";
+echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
+echo "	".$text['label-import_fields']."\n";
+echo "</td>\n";
+echo "<td width='70%' class='vtable' align='left'>\n";
+echo "	<input type='checkbox' disabled='disabled' checked>&nbsp;".$text['label-contact_name']."&nbsp;\n";
+echo "	<input type='checkbox' disabled='disabled' checked>&nbsp;".$text['label-contact_organization']."&nbsp;\n";
+echo "	<input type='checkbox' name='import_fields[email]' id='field_email' value='1' checked><label for='field_email'>&nbsp;".$text['label-contact_email']."</label>&nbsp;\n";
+echo "	<input type='checkbox' name='import_fields[number]' id='field_number' value='1' checked><label for='field_number'>&nbsp;".$text['label-phone_number']."</label>&nbsp;\n";
+echo "	<input type='checkbox' name='import_fields[url]' id='field_url' value='1' checked><label for='field_url'>&nbsp;".$text['label-contact_url']."</label>&nbsp;\n";
+echo "	<input type='checkbox' name='import_fields[address]' id='field_address' value='1' checked><label for='field_address'>&nbsp;".$text['label-address_address']."</label>\n";
+echo "<br />\n";
+echo $text['description-import_fields']."\n";
+echo "</td>\n";
+echo "</tr>\n";
+
+echo "<tr>\n";
+echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
+echo "	".$text['label-contact_type']."\n";
+echo "</td>\n";
+echo "<td class='vtable' align='left'>\n";
+if (is_array($_SESSION["contact"]["type"])) {
+	sort($_SESSION["contact"]["type"]);
+	echo "	<select class='formfld' name='import_type'>\n";
+	echo "		<option value=''></option>\n";
+	foreach($_SESSION["contact"]["type"] as $row) {
+		echo "	<option value='".$row."'>".$row."</option>\n";
+	}
+	echo "	</select>\n";
+}
+else {
+	echo "	<select class='formfld' name='import_type'>\n";
+	echo "		<option value=''></option>\n";
+	echo "		<option value='customer'>".$text['option-contact_type_customer']."</option>\n";
+	echo "		<option value='contractor'>".$text['option-contact_type_contractor']."</option>\n";
+	echo "		<option value='friend'>".$text['option-contact_type_friend']."</option>\n";
+	echo "		<option value='lead'>".$text['option-contact_type_lead']."</option>\n";
+	echo "		<option value='member'>".$text['option-contact_type_member']."</option>\n";
+	echo "		<option value='family'>".$text['option-contact_type_family']."</option>\n";
+	echo "		<option value='subscriber'>".$text['option-contact_type_subscriber']."</option>\n";
+	echo "		<option value='supplier'>".$text['option-contact_type_supplier']."</option>\n";
+	echo "		<option value='provider'>".$text['option-contact_type_provider']."</option>\n";
+	echo "		<option value='user'>".$text['option-contact_type_user']."</option>\n";
+	echo "		<option value='volunteer'>".$text['option-contact_type_volunteer']."</option>\n";
+	echo "	</select>\n";
+}
+echo "<br />\n";
+echo $text['description-contact_type_import']."\n";
+echo "</td>\n";
+echo "</tr>\n";
+
+echo "<tr>\n";
+echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
+echo "	".$text['label-contact_category']."\n";
+echo "</td>\n";
+echo "<td class='vtable' align='left'>\n";
+if (is_array($_SESSION["contact"]["category"])) {
+	sort($_SESSION["contact"]["category"]);
+	echo "	<select class='formfld' name='import_category'>\n";
+	echo "		<option value=''></option>\n";
+	foreach($_SESSION["contact"]["category"] as $row) {
+		echo "	<option value='".$row."'>".$row."</option>\n";
+	}
+	echo "	</select>\n";
+}
+else {
+	echo "	<input class='formfld' type='text' name='import_category' maxlength='255'>\n";
+}
+echo "<br />\n";
+echo $text['description-contact_category_import']."\n";
+echo "</td>\n";
+echo "</tr>\n";
+
+echo "<tr>\n";
+echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
+echo "	".$text['label-shared']."\n";
+echo "</td>\n";
+echo "<td class='vtable' align='left'>\n";
+echo "	<select class='formfld' name='import_shared' id='import_shared'>\n";
+echo "		<option value='false'>".$text['option-false']."</option>\n";
+echo "		<option value='true'>".$text['option-true']."</option>\n";
+echo "	</select>\n";
+echo "	<br />\n";
+echo $text['description-shared_import']."\n";
+echo "</td>\n";
+echo "</tr>\n";
+
+echo "<tr>\n";
+echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
+echo "    ".$text['label-import_duplicates']."\n";
+echo "</td>\n";
+echo "<td class='vtable' align='left'>\n";
+echo "    <select class='formfld' style='width: 150px;' name='import_duplicates'>\n";
+echo "    <option value='skip'>".$text['option-import_duplicates_skip']."</option>\n";
+echo "    <option value='replace'>".$text['option-import_duplicates_replace']."</option>\n";
+echo "    </select>\n";
+echo "<br />\n";
+echo $text['description-import_duplicates']."\n";
+echo "</td>\n";
+echo "</tr>\n";
+
+echo "</table>";
+echo "<br><br>";
+
+//display groups
+echo "<b>".$text['label-groups']."</b>";
+echo "<br><br>";
+
+echo "<table class='tr_hover' width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
+echo "<tr>\n";
+echo "	<th style='width: 30px; text-align: center; padding: 0px;'>&nbsp;</th>";
+echo "	<th>".$text['label-contact_name']."</th>\n";
+echo "</tr>\n";
+
+//determine contact count in groups
+foreach ($contacts as $contact) {
+	foreach ($contact['groups'] as $group_id => $meh) {
+		$groups[$group_id]['count']++;
+	}
+}
+
+$c = 0;
+foreach ($groups as $group['id'] => $group) {
+	if ($group['count'] > 0) {
+		echo "<tr>\n";
+		echo "	<td valign='top' class='".$row_style[$c]."' style='text-align: center; padding: 3px 0px 0px 0px;'><input type='checkbox' name='group_id[]' id='group_id_".$group['id']."' value='".$group['id']."'></td>\n";
+		echo "	<td valign='top' class='".$row_style[$c]."' onclick=\"document.getElementById('group_id_".$group['id']."').checked = (document.getElementById('group_id_".$group['id']."').checked) ? false : true;\">".$group['name']." (".$group['count'].")</td>\n";
+		echo "</tr>\n";
+		$c=($c)?0:1;
+	}
+}
+echo "</table>\n";
+echo "<br>";
+
+echo "<div style='text-align: right;'><input type='submit' class='btn' id='btn_submit' value=\"".$text['button-import']."\"></div>";
+
+echo "<br>";
+
+//display contacts
+echo "<b>".$text['header-contacts']."</b>";
+echo "<br><br>";
+
+echo "<table class='tr_hover' width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
+echo "<tr>\n";
+echo "	<th style='width: 30px; text-align: center; padding: 0px;'><input type='checkbox' onchange=\"(this.checked) ? check('all') : check('none');\"></th>";
+echo "	<th>".$text['label-contact_name']."</th>\n";
+echo "	<th>".$text['label-contact_organization']."</th>\n";
+echo "	<th>".$text['label-contact_email']."</th>\n";
+echo "	<th>".$text['label-phone_number']."</th>\n";
+echo "	<th>".$text['label-contact_url']."</th>\n";
+echo "	<th>".$text['label-address_address']."</th>\n";
+echo "	<th>".$text['label-group']."</th>\n";
+echo "</tr>\n";
+$c = 0;
+foreach ($contacts as $contact['id'] => $contact) {
+	$contact_ids[] = $contact['id'];
+	echo "<tr>\n";
+	echo "	<td valign='top' class='".$row_style[$c]."' style='text-align: center; padding: 3px 0px 0px 0px;'><input type='checkbox' name='contact_id[]' id='contact_id_".$contact['id']."' value='".$contact['id']."'></td>\n";
+	echo "	<td valign='top' class='".$row_style[$c]."' onclick=\"document.getElementById('contact_id_".$contact['id']."').checked = (document.getElementById('contact_id_".$contact['id']."').checked) ? false : true;\">";
+	$contact_name[] = $contact['name_prefix'];
+	$contact_name[] = $contact['name_given'];
+	$contact_name[] = $contact['name_middle'];
+	$contact_name[] = $contact['name_family'];
+	$contact_name[] = $contact['name_suffix'];
+	echo "		".implode(' ', $contact_name)."&nbsp;";
+	unset($contact_name);
+	echo "	</td>\n";
+	echo "	<td valign='top' class='".$row_style[$c]."' style='max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
+	echo "		".(($contact['title']) ? $contact['title']."<br>" : null).$contact['organization']."&nbsp;";
+	echo "	</td>\n";
+	echo "	<td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
+	if (sizeof($contact['emails']) > 0) {
+ 		foreach ($contact['emails'] as $contact_email) {
+ 			$contact_emails[] = "<span style='font-size: 80%;'>".$contact_email['label'].":</span> <a href='mailto: ".$contact_email['address']."'>".$contact_email['address']."</a>";
+ 		}
+		echo implode('<br>', $contact_emails);
+		unset($contact_emails);
+	} else { echo "&nbsp;"; }
+	echo "	</td>\n";
+	echo "	<td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
+	if (sizeof($contact['numbers']) > 0) {
+		foreach ($contact['numbers'] as $contact_number) {
+			$contact_number_part = "<span style='font-size: 80%;'>".$contact_number['label'].":</span> ";
+			if (substr_count(strtoupper($contact_number['label']), 'FAX') == 0) {
+				$contact_number_part .= "<a href='javascript:void(0);' onclick=\"send_cmd('".PROJECT_PATH."/app/click_to_call/click_to_call.php?src_cid_name=".urlencode($contact_number['number'])."&src_cid_number=".urlencode($contact_number['number'])."&dest_cid_name=".urlencode($_SESSION['user']['extension'][0]['outbound_caller_id_name'])."&dest_cid_number=".urlencode($_SESSION['user']['extension'][0]['outbound_caller_id_number'])."&src=".urlencode($_SESSION['user']['extension'][0]['user'])."&dest=".urlencode($contact_number['number'])."&rec=false&ringback=us-ring&auto_answer=true');\">";
+			}
+			$contact_number_part .= format_phone($contact_number['number']);
+			if (substr_count(strtoupper($contact_number['label']), 'FAX') == 0) {
+				$contact_number_part .= "</a>";
+			}
+			$contact_numbers[] = $contact_number_part;
+			unset($contact_number_part);
+		}
+		echo implode('<br>', $contact_numbers);
+		unset($contact_numbers);
+	} else { echo "&nbsp;"; }
+	echo "	</td>\n";
+	echo "	<td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
+	if (sizeof($contact['urls']) > 0) {
+		foreach ($contact['urls'] as $contact_url) {
+			$contact_urls[] = "<span style='font-size: 80%;'>".$contact_url['label'].":</span> <a href='".$contact_url['url']."' target='_blank'>".str_replace("http://", "", str_replace("https://", "", $contact_url['url']))."</a>";
+		}
+		echo implode('<br>', $contact_urls);
+		unset($contact_urls);
+	} else { echo "&nbsp;"; }
+	echo "	</td>\n";
+	echo "	<td valign='top' class='".$row_style[$c]."' style='width: 15%; max-width: 50px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>";
+	if (sizeof($contact['addresses']) > 0) {
+		foreach ($contact['addresses'] as $contact_address) {
+			if ($contact_address['street'] != '') { $contact_address_parts[] = $contact_address['street']; }
+			if ($contact_address['extended'] != '') { $contact_address_parts[] = $contact_address['extended']; }
+			if ($contact_address['community'] != '') { $contact_address_parts[] = $contact_address['community']; }
+			if ($contact_address['locality'] != '') { $contact_address_parts[] = $contact_address['locality']; }
+			if ($contact_address['region'] != '') { $contact_address_parts[] = $contact_address['region']; }
+			if ($contact_address['postal_code'] != '') { $contact_address_parts[] = $contact_address['postal_code']; }
+			if ($contact_address['country'] != '') { $contact_address_parts[] = $contact_address['country']; }
+			$contact_addresses[] = "<span style='font-size: 80%;'>".$contact_address['label'].":</span> ".implode(', ', $contact_address_parts);
+			unset($contact_address_parts);
+		}
+		echo implode('<br>', $contact_addresses);
+		unset($contact_addresses);
+	} else { echo "&nbsp;"; }
+	echo "	</td>\n";
+	echo "	<td valign='top' class='".$row_style[$c]."' style='white-space: nowrap;'>";
+	foreach ($contact['groups'] as $contact_group['id'] => $contact_group['name']) {
+		$contact_groups[] = $contact_group['name'];
+	}
+	echo "		".implode('<br>', $contact_groups);
+	unset($contact_groups);
+	echo "	</td>\n";
+	echo "</tr>\n";
+	$c=($c)?0:1;
+}
+echo "</table>\n";
+echo "<br>";
+
+echo "<div style='text-align: right;'><input type='submit' class='btn' id='btn_submit' value=\"".$text['button-import']."\"></div>";
+
+echo "</form>";
+echo "<br><br>";
+
+// check or uncheck all contact checkboxes
+if (sizeof($contact_ids) > 0) {
+	echo "<script>\n";
+	echo "	function check(what) {\n";
+	foreach ($contact_ids as $contact_id) {
+		echo "	document.getElementById('contact_id_".$contact_id."').checked = (what == 'all') ? true : false;\n";
+	}
+	echo "	}\n";
+	echo "</script>\n";
+}
+
+/*
+echo "<pre>";
+print_r($contacts);
+echo "</pre>";
+echo "<br><br>";
+
+echo "<hr>";
+echo "<br><br><b>SOURCE JSON DECODED ARRAY</b>...<br><br><pre>";
+print_r($records);
+echo "</pre>";
+*/
+
+//include the footer
+require_once "resources/footer.php";
+
+
+
+
+// used above
+function curl_file_get_contents($url) {
+	$curl = curl_init();
+	$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
+
+	curl_setopt($curl, CURLOPT_URL, $url);	//The URL to fetch. This can also be set when initializing a session with curl_init().
+	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);	//TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
+	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);	//The number of seconds to wait while trying to connect.
+	curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);	//The contents of the "User-Agent: " header to be used in a HTTP request.
+	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);	//To follow any "Location: " header that the server sends as part of the HTTP header.
+	curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);	//To automatically set the Referer: field in requests where it follows a Location: redirect.
+	curl_setopt($curl, CURLOPT_TIMEOUT, 10);	//The maximum number of seconds to allow cURL functions to execute.
+	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);	//To stop cURL from verifying the peer's certificate.
+
+	$contents = curl_exec($curl);
+	curl_close($curl);
+	return $contents;
+}
 ?>

+ 342 - 342
contact_relation_edit.php

@@ -1,342 +1,342 @@
-<?php
-/*
-	FusionPBX
-	Version: MPL 1.1
-
-	The contents of this file are subject to the Mozilla Public License Version
-	1.1 (the "License"); you may not use this file except in compliance with
-	the License. You may obtain a copy of the License at
-	http://www.mozilla.org/MPL/
-
-	Software distributed under the License is distributed on an "AS IS" basis,
-	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
-	for the specific language governing rights and limitations under the
-	License.
-
-	The Original Code is FusionPBX
-
-	The Initial Developer of the Original Code is
-	Mark J Crane <[email protected]>
-	Portions created by the Initial Developer are Copyright (C) 2008-2012
-	the Initial Developer. All Rights Reserved.
-
-	Contributor(s):
-	Mark J Crane <[email protected]>
-	Luis Daniel Lucio Quiroz <[email protected]>
-*/
-require_once "root.php";
-require_once "resources/require.php";
-require_once "resources/check_auth.php";
-if (permission_exists('contact_relation_edit') || permission_exists('contact_relation_add')) {
-	//access granted
-}
-else {
-	echo "access denied";
-	exit;
-}
-
-//add multi-lingual support
-	$language = new text;
-	$text = $language->get();
-
-//action add or update
-	if (isset($_REQUEST["id"])) {
-		$action = "update";
-		$contact_relation_uuid = check_str($_REQUEST["id"]);
-	}
-	else {
-		$action = "add";
-	}
-
-//get the contact uuid
-	if (strlen($_GET["contact_uuid"]) > 0) {
-		$contact_uuid = check_str($_GET["contact_uuid"]);
-	}
-
-//get http post variables and set them to php variables
-	if (count($_POST)>0) {
-		$relation_label = check_str($_POST["relation_label"]);
-		$relation_label_custom = check_str($_POST["relation_label_custom"]);
-		$relation_contact_uuid = check_str($_POST["relation_contact_uuid"]);
-		$relation_reciprocal = check_str($_POST["relation_reciprocal"]);
-		$relation_reciprocal_label = check_str($_POST["relation_reciprocal_label"]);
-		$relation_reciprocal_label_custom = check_str($_POST["relation_reciprocal_label_custom"]);
-
-		//use custom label(s), if set
-		$relation_label = ($relation_label_custom != '') ? $relation_label_custom : $relation_label;
-		$relation_reciprocal_label = ($relation_reciprocal_label_custom != '') ? $relation_reciprocal_label_custom : $relation_reciprocal_label;
-	}
-
-//process the form data
-	if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
-
-		//set the uuid
-			if ($action == "update") {
-				$contact_relation_uuid = check_str($_POST["contact_relation_uuid"]);
-			}
-
-		//check for all required data
-			$msg = '';
-			if (strlen($msg) > 0 && strlen($_POST["persistformvar"]) == 0) {
-				require_once "resources/header.php";
-				require_once "resources/persist_form_var.php";
-				echo "<div align='center'>\n";
-				echo "<table><tr><td>\n";
-				echo $msg."<br />";
-				echo "</td></tr></table>\n";
-				persistformvar($_POST);
-				echo "</div>\n";
-				require_once "resources/footer.php";
-				return;
-			}
-
-		//add or update the database
-			if ($_POST["persistformvar"] != "true") {
-
-				//update last modified
-				$sql = "update v_contacts set ";
-				$sql .= "last_mod_date = now(), ";
-				$sql .= "last_mod_user = '".$_SESSION['username']."' ";
-				$sql .= "where domain_uuid = '".$domain_uuid."' ";
-				$sql .= "and contact_uuid = '".$contact_uuid."' ";
-				$db->exec(check_sql($sql));
-				unset($sql);
-
-				if ($action == "add") {
-					$contact_relation_uuid = uuid();
-					$sql = "insert into v_contact_relations ";
-					$sql .= "(";
-					$sql .= "contact_relation_uuid, ";
-					$sql .= "domain_uuid, ";
-					$sql .= "contact_uuid, ";
-					$sql .= "relation_label, ";
-					$sql .= "relation_contact_uuid ";
-					$sql .= ")";
-					$sql .= "values ";
-					$sql .= "(";
-					$sql .= "'".$contact_relation_uuid."', ";
-					$sql .= "'".$_SESSION['domain_uuid']."', ";
-					$sql .= "'".$contact_uuid."', ";
-					$sql .= "'".$relation_label."', ";
-					$sql .= "'".$relation_contact_uuid."' ";
-					$sql .= ")";
-					$db->exec(check_sql($sql));
-					unset($sql);
-
-					if ($relation_reciprocal) {
-						$contact_relation_uuid = uuid();
-						$sql = "insert into v_contact_relations ";
-						$sql .= "(";
-						$sql .= "contact_relation_uuid, ";
-						$sql .= "domain_uuid, ";
-						$sql .= "contact_uuid, ";
-						$sql .= "relation_label, ";
-						$sql .= "relation_contact_uuid ";
-						$sql .= ")";
-						$sql .= "values ";
-						$sql .= "(";
-						$sql .= "'".$contact_relation_uuid."', ";
-						$sql .= "'".$_SESSION['domain_uuid']."', ";
-						$sql .= "'".$relation_contact_uuid."', ";
-						$sql .= "'".$relation_reciprocal_label."', ";
-						$sql .= "'".$contact_uuid."' ";
-						$sql .= ")";
-						$db->exec(check_sql($sql));
-						unset($sql);
-					}
-
-					$_SESSION["message"] = $text['message-add'];
-					header("Location: contact_edit.php?id=".$contact_uuid);
-					return;
-				} //if ($action == "add")
-
-				if ($action == "update") {
-					$sql = "update v_contact_relations set ";
-					$sql .= "relation_label = '".$relation_label."', ";
-					$sql .= "relation_contact_uuid = '".$relation_contact_uuid."' ";
-					$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
-					$sql .= "and contact_relation_uuid = '".$contact_relation_uuid."'";
-					$db->exec(check_sql($sql));
-					unset($sql);
-
-					$_SESSION["message"] = $text['message-update'];
-					header("Location: contact_edit.php?id=".$contact_uuid);
-					return;
-				} //if ($action == "update")
-			} //if ($_POST["persistformvar"] != "true")
-	} //(count($_POST)>0 && strlen($_POST["persistformvar"]) == 0)
-
-//pre-populate the form
-	if (count($_GET) > 0 && $_POST["persistformvar"] != "true") {
-		$contact_relation_uuid = $_GET["id"];
-		$sql = "select * from v_contact_relations ";
-		$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
-		$sql .= "and contact_relation_uuid = '".$contact_relation_uuid."' ";
-		$prep_statement = $db->prepare(check_sql($sql));
-		$prep_statement->execute();
-		$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
-		foreach ($result as &$row) {
-			$relation_label = $row["relation_label"];
-			$relation_contact_uuid = $row["relation_contact_uuid"];
-			break; //limit to 1 row
-		}
-		unset ($prep_statement);
-	}
-
-//show the header
-	$document['title'] = $text['title-contact_relation'];
-	require_once "resources/header.php";
-
-//javascript to toggle input/select boxes
-	echo "<script type='text/javascript'>";
-	echo "	function toggle_custom(field) {";
-	echo "		$('#'+field).toggle();";
-	echo "		document.getElementById(field).selectedIndex = 0;";
-	echo "		document.getElementById(field+'_custom').value = '';";
-	echo "		$('#'+field+'_custom').toggle();";
-	echo "		if ($('#'+field+'_custom').is(':visible')) { $('#'+field+'_custom').focus(); } else { $('#'+field).focus(); }";
-	echo "	}";
-	echo "</script>";
-
-//show the content
-	echo "<form method='post' name='frm' action=''>\n";
-	echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
-	echo "<tr>\n";
-	echo "<td align='left' valign='top' nowrap='nowrap'>";
-	echo "	<b>".$text['header-contact_relation']."</b>";
-	echo "</td>\n";
-	echo "<td align='right' valign='top'>";
-	echo "	<input type='button' class='btn' name='' alt='".$text['button-back']."' onclick=\"window.location='contact_edit.php?id=".$contact_uuid."'\" value='".$text['button-back']."'>";
-	echo "	<input type='submit' name='submit' class='btn' value='".$text['button-save']."'>\n";
-	echo "</td>\n";
-	echo "</tr>\n";
-	echo "</table>\n";
-	echo "<br />\n";
-
-	echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
-	echo "<tr>\n";
-	echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
-	echo "	".$text['label-contact_relation_label']."\n";
-	echo "</td>\n";
-	echo "<td width='70%' class='vtable' align='left'>\n";
-	if (is_array($_SESSION["contact"]["relation_label"])) {
-		sort($_SESSION["contact"]["relation_label"]);
-		foreach($_SESSION["contact"]["relation_label"] as $row) {
-			$relation_label_options[] = "<option value='".$row."' ".(($row == $relation_label) ? "selected='selected'" : null).">".$row."</option>";
-		}
-		$relation_label_found = (in_array($relation_label, $_SESSION["contact"]["relation_label"])) ? true : false;
-	}
-	else {
-		$selected[$relation_label] = "selected";
-		$default_labels[] = $text['label-contact_relation_option_parent'];
-		$default_labels[] = $text['label-contact_relation_option_child'];
-		$default_labels[] = $text['label-contact_relation_option_employee'];
-		$default_labels[] = $text['label-contact_relation_option_member'];
-		$default_labels[] = $text['label-contact_relation_option_associate'];
-		$default_labels[] = $text['label-contact_relation_option_other'];
-		foreach ($default_labels as $default_label) {
-			$relation_label_options[] = "<option value='".$default_label."' ".$selected[$default_label].">".$default_label."</option>";
-		}
-		$relation_label_found = (in_array($relation_label, $default_labels)) ? true : false;
-	}
-	echo "	<select class='formfld' ".((!$relation_label_found && $relation_label != '') ? "style='display: none;'" : null)." name='relation_label' id='relation_label' onchange=\"getElementById('relation_label_custom').value='';\">\n";
-	echo "		<option value=''></option>\n";
-	echo 		(is_array($relation_label_options)) ? implode("\n", $relation_label_options) : null;
-	echo "	</select>\n";
-	echo "	<input type='text' class='formfld' ".(($relation_label_found || $relation_label == '') ? "style='display: none;'" : null)." name='relation_label_custom' id='relation_label_custom' value=\"".((!$relation_label_found) ? htmlentities($relation_label) : null)."\">\n";
-	echo "	<input type='button' id='btn_toggle_label' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('relation_label');\">\n";
-	echo "<br />\n";
-	echo $text['description-relation_label']."\n";
-	echo "</td>\n";
-	echo "</tr>\n";
-
-	echo "<tr>\n";
-	echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
-	echo "	".$text['label-contact_relation_contact']."\n";
-	echo "</td>\n";
-	echo "<td class='vtable' align='left'>\n";
-	$sql = "select contact_uuid, contact_organization, contact_name_given, contact_name_family from v_contacts ";
-	$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
-	$sql .= "and contact_uuid <> '".$contact_uuid."' ";
-	$sql .= "order by contact_organization desc, contact_name_given asc, contact_name_family asc ";
-	$prep_statement = $db->prepare(check_sql($sql));
-	$prep_statement->execute();
-	$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
-	unset ($prep_statement, $sql);
-	echo "<select class='formfld' name='relation_contact_uuid' id='relation_contact_uuid'>\n";
-	echo "<option value=''></option>\n";
-	foreach($result as $row) {
-		$contact_name = $row['contact_name_given'].(($row['contact_name_given'] != '' && $row['contact_name_family'] != '') ? ' ' : null).$row['contact_name_family'];
-		if ($row['contact_organization'] != '') {
-			if ($contact_name != '') {
-				$contact_name = $row['contact_organization'].', '.$contact_name;
-			}
-			else {
-				$contact_name = $row['contact_organization'];
-			}
-		}
-		echo "<option value='".$row['contact_uuid']."' ".(($row['contact_uuid'] == $relation_contact_uuid) ? "selected='selected'" : null).">".$contact_name."</option>\n";
-	}
-	unset($sql, $result, $row_count);
-	echo "</select>\n";
-// 	echo "<br />\n";
-// 	echo $text['description-related_contact']."\n";
-	echo "</td>\n";
-	echo "</tr>\n";
-
-	if ($action == 'add') {
-		echo "<tr>\n";
-		echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
-		echo "	".$text['label-contact_relation_reciprocal']."\n";
-		echo "</td>\n";
-		echo "<td class='vtable' align='left'>\n";
-		echo "	<select class='formfld' name='relation_reciprocal' id='relation_reciprocal' onchange=\"$('#reciprocal_label').slideToggle(400);\">\n";
-		echo "		<option value='0'>".$text['option-false']."</option>\n";
-		echo "		<option value='1'>".$text['option-true']."</option>\n";
-		echo "	</select>\n";
-		echo "<br />\n";
-		echo $text['description-contact_relation_reciprocal']."\n";
-		echo "</td>\n";
-		echo "</tr>\n";
-		echo "</table>\n";
-
-		echo "<div id='reciprocal_label' style='display: none;'>\n";
-		echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
-		echo "<tr>\n";
-		echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
-		echo "	".$text['label-contact_relation_reciprocal_label']."\n";
-		echo "</td>\n";
-		echo "<td width='70%' class='vtable' align='left'>\n";
-		echo "	<select class='formfld' name='relation_reciprocal_label' id='relation_reciprocal_label' onchange=\"getElementById('relation_reciprocal_label_custom').value='';\">\n";
-		echo "		<option value=''></option>\n";
-		echo 		(is_array($relation_label_options)) ? implode("\n", $relation_label_options) : null;
-		echo "	</select>\n";
-		echo "	<input type='text' class='formfld' style='display: none;' name='relation_reciprocal_label_custom' id='relation_reciprocal_label_custom' value=''>\n";
-		echo "	<input type='button' id='btn_toggle_reciprocal_label' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('relation_reciprocal_label');\">\n";
-		echo "<br />\n";
-		echo $text['description-contact_relation_reciprocal_label']."\n";
-		echo "</td>\n";
-		echo "</tr>\n";
-		echo "</table>\n";
-		echo "</div>\n";
-
-		echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
-	}
-
-	echo "	<tr>\n";
-	echo "		<td colspan='2' align='right'>\n";
-	echo "			<br>\n";
-	echo "			<input type='hidden' name='contact_uuid' value='".$contact_uuid."'>\n";
-	if ($action == "update") {
-		echo "		<input type='hidden' name='contact_relation_uuid' value='".$contact_relation_uuid."'>\n";
-	}
-	echo "			<input type='submit' name='submit' class='btn' value='".$text['button-save']."'>\n";
-	echo "		</td>\n";
-	echo "	</tr>";
-	echo "</table>";
-	echo "<br><br>";
-	echo "</form>";
-
-//include the footer
-	require_once "resources/footer.php";
-?>
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2012
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+	Luis Daniel Lucio Quiroz <[email protected]>
+*/
+require_once "root.php";
+require_once "resources/require.php";
+require_once "resources/check_auth.php";
+if (permission_exists('contact_relation_edit') || permission_exists('contact_relation_add')) {
+	//access granted
+}
+else {
+	echo "access denied";
+	exit;
+}
+
+//add multi-lingual support
+	$language = new text;
+	$text = $language->get();
+
+//action add or update
+	if (isset($_REQUEST["id"])) {
+		$action = "update";
+		$contact_relation_uuid = check_str($_REQUEST["id"]);
+	}
+	else {
+		$action = "add";
+	}
+
+//get the contact uuid
+	if (strlen($_GET["contact_uuid"]) > 0) {
+		$contact_uuid = check_str($_GET["contact_uuid"]);
+	}
+
+//get http post variables and set them to php variables
+	if (count($_POST)>0) {
+		$relation_label = check_str($_POST["relation_label"]);
+		$relation_label_custom = check_str($_POST["relation_label_custom"]);
+		$relation_contact_uuid = check_str($_POST["relation_contact_uuid"]);
+		$relation_reciprocal = check_str($_POST["relation_reciprocal"]);
+		$relation_reciprocal_label = check_str($_POST["relation_reciprocal_label"]);
+		$relation_reciprocal_label_custom = check_str($_POST["relation_reciprocal_label_custom"]);
+
+		//use custom label(s), if set
+		$relation_label = ($relation_label_custom != '') ? $relation_label_custom : $relation_label;
+		$relation_reciprocal_label = ($relation_reciprocal_label_custom != '') ? $relation_reciprocal_label_custom : $relation_reciprocal_label;
+	}
+
+//process the form data
+	if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) {
+
+		//set the uuid
+			if ($action == "update") {
+				$contact_relation_uuid = check_str($_POST["contact_relation_uuid"]);
+			}
+
+		//check for all required data
+			$msg = '';
+			if (strlen($msg) > 0 && strlen($_POST["persistformvar"]) == 0) {
+				require_once "resources/header.php";
+				require_once "resources/persist_form_var.php";
+				echo "<div align='center'>\n";
+				echo "<table><tr><td>\n";
+				echo $msg."<br />";
+				echo "</td></tr></table>\n";
+				persistformvar($_POST);
+				echo "</div>\n";
+				require_once "resources/footer.php";
+				return;
+			}
+
+		//add or update the database
+			if ($_POST["persistformvar"] != "true") {
+
+				//update last modified
+				$sql = "update v_contacts set ";
+				$sql .= "last_mod_date = now(), ";
+				$sql .= "last_mod_user = '".$_SESSION['username']."' ";
+				$sql .= "where domain_uuid = '".$domain_uuid."' ";
+				$sql .= "and contact_uuid = '".$contact_uuid."' ";
+				$db->exec(check_sql($sql));
+				unset($sql);
+
+				if ($action == "add") {
+					$contact_relation_uuid = uuid();
+					$sql = "insert into v_contact_relations ";
+					$sql .= "(";
+					$sql .= "contact_relation_uuid, ";
+					$sql .= "domain_uuid, ";
+					$sql .= "contact_uuid, ";
+					$sql .= "relation_label, ";
+					$sql .= "relation_contact_uuid ";
+					$sql .= ")";
+					$sql .= "values ";
+					$sql .= "(";
+					$sql .= "'".$contact_relation_uuid."', ";
+					$sql .= "'".$_SESSION['domain_uuid']."', ";
+					$sql .= "'".$contact_uuid."', ";
+					$sql .= "'".$relation_label."', ";
+					$sql .= "'".$relation_contact_uuid."' ";
+					$sql .= ")";
+					$db->exec(check_sql($sql));
+					unset($sql);
+
+					if ($relation_reciprocal) {
+						$contact_relation_uuid = uuid();
+						$sql = "insert into v_contact_relations ";
+						$sql .= "(";
+						$sql .= "contact_relation_uuid, ";
+						$sql .= "domain_uuid, ";
+						$sql .= "contact_uuid, ";
+						$sql .= "relation_label, ";
+						$sql .= "relation_contact_uuid ";
+						$sql .= ")";
+						$sql .= "values ";
+						$sql .= "(";
+						$sql .= "'".$contact_relation_uuid."', ";
+						$sql .= "'".$_SESSION['domain_uuid']."', ";
+						$sql .= "'".$relation_contact_uuid."', ";
+						$sql .= "'".$relation_reciprocal_label."', ";
+						$sql .= "'".$contact_uuid."' ";
+						$sql .= ")";
+						$db->exec(check_sql($sql));
+						unset($sql);
+					}
+
+					$_SESSION["message"] = $text['message-add'];
+					header("Location: contact_edit.php?id=".$contact_uuid);
+					return;
+				} //if ($action == "add")
+
+				if ($action == "update") {
+					$sql = "update v_contact_relations set ";
+					$sql .= "relation_label = '".$relation_label."', ";
+					$sql .= "relation_contact_uuid = '".$relation_contact_uuid."' ";
+					$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
+					$sql .= "and contact_relation_uuid = '".$contact_relation_uuid."'";
+					$db->exec(check_sql($sql));
+					unset($sql);
+
+					$_SESSION["message"] = $text['message-update'];
+					header("Location: contact_edit.php?id=".$contact_uuid);
+					return;
+				} //if ($action == "update")
+			} //if ($_POST["persistformvar"] != "true")
+	} //(count($_POST)>0 && strlen($_POST["persistformvar"]) == 0)
+
+//pre-populate the form
+	if (count($_GET) > 0 && $_POST["persistformvar"] != "true") {
+		$contact_relation_uuid = $_GET["id"];
+		$sql = "select * from v_contact_relations ";
+		$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
+		$sql .= "and contact_relation_uuid = '".$contact_relation_uuid."' ";
+		$prep_statement = $db->prepare(check_sql($sql));
+		$prep_statement->execute();
+		$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
+		foreach ($result as &$row) {
+			$relation_label = $row["relation_label"];
+			$relation_contact_uuid = $row["relation_contact_uuid"];
+			break; //limit to 1 row
+		}
+		unset ($prep_statement);
+	}
+
+//show the header
+	$document['title'] = $text['title-contact_relation'];
+	require_once "resources/header.php";
+
+//javascript to toggle input/select boxes
+	echo "<script type='text/javascript'>";
+	echo "	function toggle_custom(field) {";
+	echo "		$('#'+field).toggle();";
+	echo "		document.getElementById(field).selectedIndex = 0;";
+	echo "		document.getElementById(field+'_custom').value = '';";
+	echo "		$('#'+field+'_custom').toggle();";
+	echo "		if ($('#'+field+'_custom').is(':visible')) { $('#'+field+'_custom').focus(); } else { $('#'+field).focus(); }";
+	echo "	}";
+	echo "</script>";
+
+//show the content
+	echo "<form method='post' name='frm' action=''>\n";
+	echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
+	echo "<tr>\n";
+	echo "<td align='left' valign='top' nowrap='nowrap'>";
+	echo "	<b>".$text['header-contact_relation']."</b>";
+	echo "</td>\n";
+	echo "<td align='right' valign='top'>";
+	echo "	<input type='button' class='btn' name='' alt='".$text['button-back']."' onclick=\"window.location='contact_edit.php?id=".$contact_uuid."'\" value='".$text['button-back']."'>";
+	echo "	<input type='submit' name='submit' class='btn' value='".$text['button-save']."'>\n";
+	echo "</td>\n";
+	echo "</tr>\n";
+	echo "</table>\n";
+	echo "<br />\n";
+
+	echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
+	echo "<tr>\n";
+	echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
+	echo "	".$text['label-contact_relation_label']."\n";
+	echo "</td>\n";
+	echo "<td width='70%' class='vtable' align='left'>\n";
+	if (is_array($_SESSION["contact"]["relation_label"])) {
+		sort($_SESSION["contact"]["relation_label"]);
+		foreach($_SESSION["contact"]["relation_label"] as $row) {
+			$relation_label_options[] = "<option value='".$row."' ".(($row == $relation_label) ? "selected='selected'" : null).">".$row."</option>";
+		}
+		$relation_label_found = (in_array($relation_label, $_SESSION["contact"]["relation_label"])) ? true : false;
+	}
+	else {
+		$selected[$relation_label] = "selected";
+		$default_labels[] = $text['label-contact_relation_option_parent'];
+		$default_labels[] = $text['label-contact_relation_option_child'];
+		$default_labels[] = $text['label-contact_relation_option_employee'];
+		$default_labels[] = $text['label-contact_relation_option_member'];
+		$default_labels[] = $text['label-contact_relation_option_associate'];
+		$default_labels[] = $text['label-contact_relation_option_other'];
+		foreach ($default_labels as $default_label) {
+			$relation_label_options[] = "<option value='".$default_label."' ".$selected[$default_label].">".$default_label."</option>";
+		}
+		$relation_label_found = (in_array($relation_label, $default_labels)) ? true : false;
+	}
+	echo "	<select class='formfld' ".((!$relation_label_found && $relation_label != '') ? "style='display: none;'" : null)." name='relation_label' id='relation_label' onchange=\"getElementById('relation_label_custom').value='';\">\n";
+	echo "		<option value=''></option>\n";
+	echo 		(is_array($relation_label_options)) ? implode("\n", $relation_label_options) : null;
+	echo "	</select>\n";
+	echo "	<input type='text' class='formfld' ".(($relation_label_found || $relation_label == '') ? "style='display: none;'" : null)." name='relation_label_custom' id='relation_label_custom' value=\"".((!$relation_label_found) ? htmlentities($relation_label) : null)."\">\n";
+	echo "	<input type='button' id='btn_toggle_label' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('relation_label');\">\n";
+	echo "<br />\n";
+	echo $text['description-relation_label']."\n";
+	echo "</td>\n";
+	echo "</tr>\n";
+
+	echo "<tr>\n";
+	echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
+	echo "	".$text['label-contact_relation_contact']."\n";
+	echo "</td>\n";
+	echo "<td class='vtable' align='left'>\n";
+	$sql = "select contact_uuid, contact_organization, contact_name_given, contact_name_family from v_contacts ";
+	$sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' ";
+	$sql .= "and contact_uuid <> '".$contact_uuid."' ";
+	$sql .= "order by contact_organization desc, contact_name_given asc, contact_name_family asc ";
+	$prep_statement = $db->prepare(check_sql($sql));
+	$prep_statement->execute();
+	$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
+	unset ($prep_statement, $sql);
+	echo "<select class='formfld' name='relation_contact_uuid' id='relation_contact_uuid'>\n";
+	echo "<option value=''></option>\n";
+	foreach($result as $row) {
+		$contact_name = $row['contact_name_given'].(($row['contact_name_given'] != '' && $row['contact_name_family'] != '') ? ' ' : null).$row['contact_name_family'];
+		if ($row['contact_organization'] != '') {
+			if ($contact_name != '') {
+				$contact_name = $row['contact_organization'].', '.$contact_name;
+			}
+			else {
+				$contact_name = $row['contact_organization'];
+			}
+		}
+		echo "<option value='".$row['contact_uuid']."' ".(($row['contact_uuid'] == $relation_contact_uuid) ? "selected='selected'" : null).">".$contact_name."</option>\n";
+	}
+	unset($sql, $result, $row_count);
+	echo "</select>\n";
+// 	echo "<br />\n";
+// 	echo $text['description-related_contact']."\n";
+	echo "</td>\n";
+	echo "</tr>\n";
+
+	if ($action == 'add') {
+		echo "<tr>\n";
+		echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
+		echo "	".$text['label-contact_relation_reciprocal']."\n";
+		echo "</td>\n";
+		echo "<td class='vtable' align='left'>\n";
+		echo "	<select class='formfld' name='relation_reciprocal' id='relation_reciprocal' onchange=\"$('#reciprocal_label').slideToggle(400);\">\n";
+		echo "		<option value='0'>".$text['option-false']."</option>\n";
+		echo "		<option value='1'>".$text['option-true']."</option>\n";
+		echo "	</select>\n";
+		echo "<br />\n";
+		echo $text['description-contact_relation_reciprocal']."\n";
+		echo "</td>\n";
+		echo "</tr>\n";
+		echo "</table>\n";
+
+		echo "<div id='reciprocal_label' style='display: none;'>\n";
+		echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
+		echo "<tr>\n";
+		echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
+		echo "	".$text['label-contact_relation_reciprocal_label']."\n";
+		echo "</td>\n";
+		echo "<td width='70%' class='vtable' align='left'>\n";
+		echo "	<select class='formfld' name='relation_reciprocal_label' id='relation_reciprocal_label' onchange=\"getElementById('relation_reciprocal_label_custom').value='';\">\n";
+		echo "		<option value=''></option>\n";
+		echo 		(is_array($relation_label_options)) ? implode("\n", $relation_label_options) : null;
+		echo "	</select>\n";
+		echo "	<input type='text' class='formfld' style='display: none;' name='relation_reciprocal_label_custom' id='relation_reciprocal_label_custom' value=''>\n";
+		echo "	<input type='button' id='btn_toggle_reciprocal_label' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('relation_reciprocal_label');\">\n";
+		echo "<br />\n";
+		echo $text['description-contact_relation_reciprocal_label']."\n";
+		echo "</td>\n";
+		echo "</tr>\n";
+		echo "</table>\n";
+		echo "</div>\n";
+
+		echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
+	}
+
+	echo "	<tr>\n";
+	echo "		<td colspan='2' align='right'>\n";
+	echo "			<br>\n";
+	echo "			<input type='hidden' name='contact_uuid' value='".$contact_uuid."'>\n";
+	if ($action == "update") {
+		echo "		<input type='hidden' name='contact_relation_uuid' value='".$contact_relation_uuid."'>\n";
+	}
+	echo "			<input type='submit' name='submit' class='btn' value='".$text['button-save']."'>\n";
+	echo "		</td>\n";
+	echo "	</tr>";
+	echo "</table>";
+	echo "<br><br>";
+	echo "</form>";
+
+//include the footer
+	require_once "resources/footer.php";
+?>

+ 112 - 112
contact_relations.php

@@ -1,113 +1,113 @@
-<?php
-/*
-	FusionPBX
-	Version: MPL 1.1
-
-	The contents of this file are subject to the Mozilla Public License Version
-	1.1 (the "License"); you may not use this file except in compliance with
-	the License. You may obtain a copy of the License at
-	http://www.mozilla.org/MPL/
-
-	Software distributed under the License is distributed on an "AS IS" basis,
-	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
-	for the specific language governing rights and limitations under the
-	License.
-
-	The Original Code is FusionPBX
-
-	The Initial Developer of the Original Code is
-	Mark J Crane <[email protected]>
-	Portions created by the Initial Developer are Copyright (C) 2008-2012
-	the Initial Developer. All Rights Reserved.
-
-	Contributor(s):
-	Mark J Crane <[email protected]>
-*/
-require_once "root.php";
-require_once "resources/require.php";
-require_once "resources/check_auth.php";
-if (permission_exists('contact_relation_view')) {
-	//access granted
-}
-else {
-	echo "access denied";
-	exit;
-}
-
-//show the content
-	echo "<table width='100%' border='0'>\n";
-	echo "<tr>\n";
-	echo "<td width='50%' align='left' nowrap='nowrap'><b>".$text['header-contact_relations']."</b></td>\n";
-	echo "<td width='50%' align='right'>&nbsp;</td>\n";
-	echo "</tr>\n";
-	echo "</table>\n";
-
-	//get the related contacts
-		$sql = "select ";
-		$sql .= "cr.contact_relation_uuid, ";
-		$sql .= "cr.relation_label, ";
-		$sql .= "c.contact_uuid, ";
-		$sql .= "c.contact_organization, ";
-		$sql .= "c.contact_name_given, ";
-		$sql .= "c.contact_name_family ";
-		$sql .= "from ";
-		$sql .= "v_contact_relations as cr, ";
-		$sql .= "v_contacts as c ";
-		$sql .= "where ";
-		$sql .= "cr.relation_contact_uuid = c.contact_uuid ";
-		$sql .= "and cr.domain_uuid = '".$_SESSION['domain_uuid']."' ";
-		$sql .= "and cr.contact_uuid = '".$contact_uuid."' ";
-		$sql .= "order by ";
-		$sql .= "c.contact_organization desc, ";
-		$sql .= "c.contact_name_given asc, ";
-		$sql .= "c.contact_name_family asc ";
-		//echo $sql."<br><br>";
-		$prep_statement = $db->prepare(check_sql($sql));
-		$prep_statement->execute();
-		$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
-		$result_count = count($result);
-		unset ($prep_statement, $sql);
-
-	$c = 0;
-	$row_style["0"] = "row_style0";
-	$row_style["1"] = "row_style1";
-
-	echo "<table class='tr_hover' style='margin-bottom: 20px;' width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
-
-	echo "<tr>\n";
-	echo "<th>".$text['label-contact_relation_label']."</th>\n";
-	echo "<th>".$text['label-contact_relation_organization']."</th>\n";
-	echo "<th>".$text['label-contact_relation_name']."</th>\n";
-	echo "<td class='list_control_icons'>";
-	if (permission_exists('contact_relation_add')) {
-		echo "<a href='contact_relation_edit.php?contact_uuid=".$contact_uuid."' alt='".$text['button-add']."'>$v_link_label_add</a>";
-	}
-	echo "</td>\n";
-	echo "</tr>\n";
-
-	if ($result_count > 0) {
-		foreach($result as $row) {
-			if (permission_exists('contact_relation_edit')) {
-				$tr_link = "href='contact_relation_edit.php?contact_uuid=".$row['contact_uuid']."&id=".$row['contact_relation_uuid']."' ";
-			}
-			echo "<tr ".$tr_link.">\n";
-			echo "	<td valign='top' class='".$row_style[$c]."'>".$row['relation_label']."&nbsp;</td>\n";
-			echo "	<td valign='top' class='".$row_style[$c]." tr_link_void'><a href='contact_edit.php?id=".$row['contact_uuid']."'>".$row['contact_organization']."</a>&nbsp;</td>\n";
-			echo "	<td valign='top' class='".$row_style[$c]." tr_link_void'><a href='contact_edit.php?id=".$row['contact_uuid']."'>".$row['contact_name_given'].(($row['contact_name_given'] != '' && $row['contact_name_family'] != '') ? ' ' : null).$row['contact_name_family']."</a>&nbsp;</td>\n";
-			echo "	<td class='list_control_icons'>";
-			if (permission_exists('contact_relation_edit')) {
-				echo "<a href='contact_relation_edit.php?contact_uuid=".$contact_uuid."&id=".$row['contact_relation_uuid']."' alt='".$text['button-edit']."'>$v_link_label_edit</a>";
-			}
-			if (permission_exists('contact_relation_delete')) {
-				echo "<a href='contact_relation_delete.php?contact_uuid=".$contact_uuid."&id=".$row['contact_relation_uuid']."' alt='".$text['button-delete']."' onclick=\"return confirm('".$text['confirm-delete']."')\">$v_link_label_delete</a>";
-			}
-			echo "	</td>\n";
-			echo "</tr>\n";
-			$c = ($c) ? 0 : 1;
-		} //end foreach
-		unset($sql, $result, $row_count);
-	} //end if results
-
-	echo "</table>";
-
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2012
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+require_once "root.php";
+require_once "resources/require.php";
+require_once "resources/check_auth.php";
+if (permission_exists('contact_relation_view')) {
+	//access granted
+}
+else {
+	echo "access denied";
+	exit;
+}
+
+//show the content
+	echo "<table width='100%' border='0'>\n";
+	echo "<tr>\n";
+	echo "<td width='50%' align='left' nowrap='nowrap'><b>".$text['header-contact_relations']."</b></td>\n";
+	echo "<td width='50%' align='right'>&nbsp;</td>\n";
+	echo "</tr>\n";
+	echo "</table>\n";
+
+	//get the related contacts
+		$sql = "select ";
+		$sql .= "cr.contact_relation_uuid, ";
+		$sql .= "cr.relation_label, ";
+		$sql .= "c.contact_uuid, ";
+		$sql .= "c.contact_organization, ";
+		$sql .= "c.contact_name_given, ";
+		$sql .= "c.contact_name_family ";
+		$sql .= "from ";
+		$sql .= "v_contact_relations as cr, ";
+		$sql .= "v_contacts as c ";
+		$sql .= "where ";
+		$sql .= "cr.relation_contact_uuid = c.contact_uuid ";
+		$sql .= "and cr.domain_uuid = '".$_SESSION['domain_uuid']."' ";
+		$sql .= "and cr.contact_uuid = '".$contact_uuid."' ";
+		$sql .= "order by ";
+		$sql .= "c.contact_organization desc, ";
+		$sql .= "c.contact_name_given asc, ";
+		$sql .= "c.contact_name_family asc ";
+		//echo $sql."<br><br>";
+		$prep_statement = $db->prepare(check_sql($sql));
+		$prep_statement->execute();
+		$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
+		$result_count = count($result);
+		unset ($prep_statement, $sql);
+
+	$c = 0;
+	$row_style["0"] = "row_style0";
+	$row_style["1"] = "row_style1";
+
+	echo "<table class='tr_hover' style='margin-bottom: 20px;' width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
+
+	echo "<tr>\n";
+	echo "<th>".$text['label-contact_relation_label']."</th>\n";
+	echo "<th>".$text['label-contact_relation_organization']."</th>\n";
+	echo "<th>".$text['label-contact_relation_name']."</th>\n";
+	echo "<td class='list_control_icons'>";
+	if (permission_exists('contact_relation_add')) {
+		echo "<a href='contact_relation_edit.php?contact_uuid=".$contact_uuid."' alt='".$text['button-add']."'>$v_link_label_add</a>";
+	}
+	echo "</td>\n";
+	echo "</tr>\n";
+
+	if ($result_count > 0) {
+		foreach($result as $row) {
+			if (permission_exists('contact_relation_edit')) {
+				$tr_link = "href='contact_relation_edit.php?contact_uuid=".$row['contact_uuid']."&id=".$row['contact_relation_uuid']."' ";
+			}
+			echo "<tr ".$tr_link.">\n";
+			echo "	<td valign='top' class='".$row_style[$c]."'>".$row['relation_label']."&nbsp;</td>\n";
+			echo "	<td valign='top' class='".$row_style[$c]." tr_link_void'><a href='contact_edit.php?id=".$row['contact_uuid']."'>".$row['contact_organization']."</a>&nbsp;</td>\n";
+			echo "	<td valign='top' class='".$row_style[$c]." tr_link_void'><a href='contact_edit.php?id=".$row['contact_uuid']."'>".$row['contact_name_given'].(($row['contact_name_given'] != '' && $row['contact_name_family'] != '') ? ' ' : null).$row['contact_name_family']."</a>&nbsp;</td>\n";
+			echo "	<td class='list_control_icons'>";
+			if (permission_exists('contact_relation_edit')) {
+				echo "<a href='contact_relation_edit.php?contact_uuid=".$contact_uuid."&id=".$row['contact_relation_uuid']."' alt='".$text['button-edit']."'>$v_link_label_edit</a>";
+			}
+			if (permission_exists('contact_relation_delete')) {
+				echo "<a href='contact_relation_delete.php?contact_uuid=".$contact_uuid."&id=".$row['contact_relation_uuid']."' alt='".$text['button-delete']."' onclick=\"return confirm('".$text['confirm-delete']."')\">$v_link_label_delete</a>";
+			}
+			echo "	</td>\n";
+			echo "</tr>\n";
+			$c = ($c) ? 0 : 1;
+		} //end foreach
+		unset($sql, $result, $row_count);
+	} //end if results
+
+	echo "</table>";
+
 ?>

+ 360 - 360
contact_timer.php

@@ -1,361 +1,361 @@
-<?php
-/*
-	FusionPBX
-	Version: MPL 1.1
-
-	The contents of this file are subject to the Mozilla Public License Version
-	1.1 (the "License"); you may not use this file except in compliance with
-	the License. You may obtain a copy of the License at
-	http://www.mozilla.org/MPL/
-
-	Software distributed under the License is distributed on an "AS IS" basis,
-	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
-	for the specific language governing rights and limitations under the
-	License.
-
-	The Original Code is FusionPBX
-
-	The Initial Developer of the Original Code is
-	Mark J Crane <[email protected]>
-	Portions created by the Initial Developer are Copyright (C) 2008-2015
-	the Initial Developer. All Rights Reserved.
-
-	Contributor(s):
-	Mark J Crane <[email protected]>
-*/
-require_once "root.php";
-require_once "resources/require.php";
-require_once "resources/check_auth.php";
-if (!permission_exists('contact_time_add')) { echo "access denied"; exit; }
-
-//add multi-lingual support
-	$language = new text;
-	$text = $language->get();
-
-//get contact uuid
-	$domain_uuid = check_str($_REQUEST['domain_uuid']);
-	$contact_uuid = check_str($_REQUEST['contact_uuid']);
-
-//get posted variables & set time status
-	if (sizeof($_POST) > 0) {
-		$contact_time_uuid = check_str($_POST['contact_time_uuid']);
-		$contact_uuid = check_str($_POST['contact_uuid']);
-		$time_action = check_str($_POST['time_action']);
-		$time_description = check_str($_POST['time_description']);
-
-		if ($time_description == 'Description...') { unset($time_description); }
-
-		if ($time_action == 'start') {
-			$contact_time_uuid = uuid();
-			$sql = "insert into v_contact_times ";
-			$sql .= "( ";
-			$sql .= "domain_uuid, ";
-			$sql .= "contact_time_uuid, ";
-			$sql .= "contact_uuid, ";
-			$sql .= "user_uuid, ";
-			$sql .= "time_start, ";
-			$sql .= "time_description ";
-			$sql .= ") ";
-			$sql .= "values ";
-			$sql .= "( ";
-			$sql .= "'".$domain_uuid."', ";
-			$sql .= "'".$contact_time_uuid."', ";
-			$sql .= "'".$contact_uuid."', ";
-			$sql .= "'".$_SESSION["user"]["user_uuid"]."', ";
-			$sql .= "'".date("Y-m-d H:i:s")."', ";
-			$sql .= "'".$time_description."' ";
-			$sql .= ")";
-			$db->exec(check_sql($sql));
-			unset($sql);
-		}
-		if ($time_action == 'stop') {
-			$sql = "update v_contact_times ";
-			$sql .= "set ";
-			$sql .= "time_stop = '".date("Y-m-d H:i:s")."', ";
-			$sql .= "time_description = '".$time_description."' ";
-			$sql .= "where ";
-			$sql .= "contact_time_uuid = '".$contact_time_uuid."' ";
-			$sql .= "and domain_uuid = '".$domain_uuid."' ";
-			$sql .= "and contact_uuid = '".$contact_uuid."' ";
-			$sql .= "and user_uuid = '".$_SESSION["user"]["user_uuid"]."' ";
-			$db->exec(check_sql($sql));
-			unset($sql);
-		}
-		header("Location: contact_timer.php?domain_uuid=".$domain_uuid."&contact_uuid=".$contact_uuid);
-	}
-
-//get contact details
-	$sql = "select ";
-	$sql .= "contact_organization, ";
-	$sql .= "contact_name_given, ";
-	$sql .= "contact_name_family, ";
-	$sql .= "contact_nickname ";
-	$sql .= "from v_contacts ";
-	$sql .= "where domain_uuid = '".$domain_uuid."' ";
-	$sql .= "and contact_uuid = '".$contact_uuid."' ";
-	$prep_statement = $db->prepare(check_sql($sql));
-	$prep_statement->execute();
-	$result = $prep_statement->fetch(PDO::FETCH_NAMED);
-	if (sizeof($result) > 0) {
-		$contact_organization = $result["contact_organization"];
-		$contact_name_given = $result["contact_name_given"];
-		$contact_name_family = $result["contact_name_family"];
-		$contact_nickname = $result["contact_nickname"];
-	}
-	else {
-		exit;
-	}
-	unset ($sql, $prep_statement, $result);
-
-//determine timer state and action
-	$sql = "select ";
-	$sql .= "contact_time_uuid, ";
-	$sql .= "time_description ";
-	$sql .= "from v_contact_times ";
-	$sql .= "where domain_uuid = '".$domain_uuid."' ";
-	$sql .= "and user_uuid = '".$_SESSION['user']['user_uuid']."' ";
-	$sql .= "and contact_uuid = '".$contact_uuid."' ";
-	$sql .= "and time_start is not null ";
-	$sql .= "and time_stop is null ";
-	$prep_statement = $db->prepare(check_sql($sql));
-	$prep_statement->execute();
-	$result = $prep_statement->fetch(PDO::FETCH_NAMED);
-	if (sizeof($result) > 0) {
-		$contact_time_uuid = $result["contact_time_uuid"];
-		$time_description = $result["time_description"];
-	}
-	unset ($sql, $prep_statement, $result);
-
-	$timer_state = ($contact_time_uuid != '') ? 'running' : 'stopped';
-	$timer_action = ($timer_state == 'running') ? 'stop' : 'start';
-
-//determine contact name to display
-	if ($contact_nickname != '') {
-		$contact = $contact_nickname;
-	}
-	else if ($contact_name_given != '') {
-		$contact = $contact_name_given;
-	}
-	if ($contact_name_family != '') {
-		$contact .= ($contact != '') ? ' '.$contact_name_family : $contact_name_family;
-	}
-	if ($contact_organization != '') {
-		$contact .= ($contact != '') ? ', '.$contact_organization : $contact_organization;
-	}
-?>
-
-<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
-<head>
-	<title><?php echo $text['label-time_timer']; ?>: <?php echo $contact; ?></title>
-	<style>
-		body {
-			color: #5f5f5f;
-			font-size: 12px;
-			font-family: arial;
-			margin: 0;
-			padding: 15px;
-			}
-
-		b {
-			color: #952424;
-			font-size: 15px;
-			font-family: arial;
-			}
-
-		a {
-			color: #004083;
-			width: 100%;
-			}
-
-		a:hover {
-			color: #5082ca;
-			}
-
-		form {
-			margin: 0;
-			}
-
-		input.btn, input.button {
-			font-family: Candara, Calibri, Segoe, "Segoe UI", Optima, Arial, sans-serif;
-			padding: 2px 6px 3px 6px;
-			color: #fff;
-			font-weight: bold;
-			cursor: pointer;
-			font-size: 11px;
-			-moz-border-radius: 3px;
-			-webkit-border-radius: 3px;
-			-khtml-border-radius: 3px;
-			border-radius: 3px;
-			background-image: -moz-linear-gradient(top, #524f59 25%, #000 64%);
-			background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0.25, #524f59), color-stop(0.64, #000));
-			border: 1px solid #26242a;
-			background-color: #000;
-			text-align: center;
-			text-transform: uppercase;
-			text-shadow: 0px 0px 1px rgba(0, 0, 0, 0.85);
-			opacity: 0.9;
-			-moz-opacity: 0.9;
-			}
-
-		input.btn:hover, input.button:hover, img.list_control_icon:hover {
-			box-shadow: 0 0 5px #cddaf0;
-			-webkit-box-shadow: 0 0 5px #cddaf0;
-			-moz-box-shadow: 0 0 5px #cddaf0;
-			opacity: 1.0;
-			-moz-opacity: 1.0;
-			cursor: pointer;
-			}
-
-		input.txt, textarea.txt, select.txt, .formfld {
-			font-family: arial;
-			font-size: 12px;
-			color: #000;
-			text-align: left;
-			padding: 5px;
-			border: 1px solid #c0c0c0;
-			background-color: #fff;
-			box-shadow: 0 0 3px #cddaf0 inset;
-			-moz-box-shadow: 0 0 3px #cddaf0 inset;
-			-webkit-box-shadow: 0 0 3px #cddaf0 inset;
-			border-radius: 3px;
-			-moz-border-radius: 3px;
-			-webkit-border-radius: 3px;
-			}
-
-		input.txt, .formfld {
-			transition: width 0.25s;
-			-moz-transition: width 0.25s;
-			-webkit-transition: width 0.25s;
-			max-width: 500px;
-			}
-
-		input.txt:focus, .formfld:focus {
-			-webkit-box-shadow: 0 0 5px #cddaf0;
-			-moz-box-shadow: 0 0 5px #cddaf0;
-			box-shadow: 0 0 5px #cddaf0;
-			}
-
-		td {
-			color: #5f5f5f;
-			font-size: 12px;
-			font-family: arial;
-			}
-
-		.vncell {
-			border-bottom: 1px solid #fff;
-			background-color: #e5e9f0;
-			padding: 8px;
-			text-align: right;
-			color: #000;
-			-moz-border-radius: 4px;
-			-webkit-border-radius: 4px;
-			border-radius: 4px;
-			border-right: 3px solid #e5e9f0;
-			}
-
-		DIV.timer_running {
-			vertical-align: middle;
-			padding-top: 7px;
-			line-height: 50px;
-			width: 100%;
-			height: 53px;
-			text-align: center;
-			background-color: #2C9DE8;
-			font-size: 50px;
-			color: #FFFFFF;
-			/*-webkit-text-shadow: 0px 0px 5px #000;*/
-			/*-moz-text-shadow: 0px 0px 5px #000;*/
-			/*text-shadow: 0px 0px 5px #000;*/
-			font-weight: bold;
-			letter-spacing: -0.05em;
-			font-family: "Courier New",Courier,"Lucida Sans Typewriter","Lucida Typewriter",monospace;
-			-moz-border-radius: 4px;
-			-webkit-border-radius: 4px;
-			border-radius: 4px;
-			}
-
-		DIV.timer_stopped {
-			vertical-align: middle;
-			padding-top: 7px;
-			line-height: 50px;
-			width: 100%;
-			height: 53px;
-			text-align: center;
-			background-color: #2C9DE8;
-			font-size: 50px;
-			color: #FFFFFF;
-			/*-webkit-text-shadow: 0px 0px 5px #000;*/
-			/*-moz-text-shadow: 0px 0px 5px #000;*/
-			/*text-shadow: 0px 0px 5px #000;*/
-			font-weight: bold;
-			letter-spacing: -0.05em;
-			font-family: "Courier New",Courier,"Lucida Sans Typewriter","Lucida Typewriter",monospace;
-			-moz-border-radius: 4px;
-			-webkit-border-radius: 4px;
-			border-radius: 4px;
-			}
-
-	</style>
-
-	<script language="JavaScript" type="text/javascript" src="<?php echo PROJECT_PATH; ?>/resources/jquery/jquery-1.11.1.js"></script>
-	<script type="text/javascript">
-		$(document).ready(function(){
-			//ajax for refresh
-			var refresh = 1500;
-			var source_url = 'contact_timer_inc.php?domain_uuid=<?php echo $domain_uuid; ?>&contact_uuid=<?php echo $contact_uuid; ?>&contact_time_uuid=<?php echo $contact_time_uuid; ?>';
-
-			var ajax_get = function () {
-				$.ajax({
-					url: source_url, success: function(response){
-						$("#ajax_reponse").html(response);
-					}
-				});
-				setTimeout(ajax_get, refresh);
-			};
-			<?php if ($timer_state == 'running') { ?>
-				ajax_get();
-			<?php } ?>
-		});
-
-	//set window title to time when timer is running
-		function set_title(title_text) {
-			window.document.title = title_text;
-		}
-
-	</script>
-</head>
-<body>
-	<img src='resources/images/icon_timer.png' style='width: 24px; height: 24px; border: none; margin-left: 15px;' alt="<?php echo $text['label-time_timer']; ?>" align='right'>
-	<b><?php echo $text['label-time_timer']; ?></b>
-	<br><br>
-	<?php echo $text['description_timer']; ?>
-	<br><br>
-	<strong><a href="javascript:void(0);" onclick="window.opener.location.href='contact_edit.php?id=<?php echo $contact_uuid; ?>';"><?php echo $contact; ?></a></strong>
-	<br><br>
-	<div id='ajax_reponse' class='timer_<?php echo $timer_state;?>'>00:00:00</div>
-	<br>
-	<form name='frm' id='frm' method='post' action=''>
-	<input type='hidden' name='domain_uuid' value="<?php echo $domain_uuid; ?>">
-	<input type='hidden' name='contact_time_uuid' value="<?php echo $contact_time_uuid; ?>">
-	<input type='hidden' name='contact_uuid' value="<?php echo $contact_uuid; ?>">
-	<input type='hidden' name='time_action' value="<?php echo $timer_action; ?>">
-	<table cellpadding='0' cellspacing='0' border='0' style='width: 100%;'>
-		<tr>
-			<td class='vncell' style='text-align: center; padding: 10px;'>
-				<?php echo $text['label-description']; ?>
-				<textarea name='time_description' id='timer_description' class='formfld' style='width: 100%; height: 50px; margin-top: 5px;'><?php echo $time_description; ?></textarea>
-				<? if ($timer_state == 'stopped') { ?><script>document.getElementById('timer_description').focus();</script><? } ?>
-			</td>
-		</tr>
-	</table>
-	<br>
-	<center>
-	<?php if ($timer_state == 'running') { ?>
-		<input type='submit' class='btn' value="<?php echo $text['button-stop']; ?>">
-	<?php } else if ($timer_state == 'stopped') { ?>
-		<input type='submit' class='btn' value="<?php echo $text['button-start']; ?>">
-	<?php } ?>
-	</center>
-	</form>
-</body>
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2015
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+require_once "root.php";
+require_once "resources/require.php";
+require_once "resources/check_auth.php";
+if (!permission_exists('contact_time_add')) { echo "access denied"; exit; }
+
+//add multi-lingual support
+	$language = new text;
+	$text = $language->get();
+
+//get contact uuid
+	$domain_uuid = check_str($_REQUEST['domain_uuid']);
+	$contact_uuid = check_str($_REQUEST['contact_uuid']);
+
+//get posted variables & set time status
+	if (sizeof($_POST) > 0) {
+		$contact_time_uuid = check_str($_POST['contact_time_uuid']);
+		$contact_uuid = check_str($_POST['contact_uuid']);
+		$time_action = check_str($_POST['time_action']);
+		$time_description = check_str($_POST['time_description']);
+
+		if ($time_description == 'Description...') { unset($time_description); }
+
+		if ($time_action == 'start') {
+			$contact_time_uuid = uuid();
+			$sql = "insert into v_contact_times ";
+			$sql .= "( ";
+			$sql .= "domain_uuid, ";
+			$sql .= "contact_time_uuid, ";
+			$sql .= "contact_uuid, ";
+			$sql .= "user_uuid, ";
+			$sql .= "time_start, ";
+			$sql .= "time_description ";
+			$sql .= ") ";
+			$sql .= "values ";
+			$sql .= "( ";
+			$sql .= "'".$domain_uuid."', ";
+			$sql .= "'".$contact_time_uuid."', ";
+			$sql .= "'".$contact_uuid."', ";
+			$sql .= "'".$_SESSION["user"]["user_uuid"]."', ";
+			$sql .= "'".date("Y-m-d H:i:s")."', ";
+			$sql .= "'".$time_description."' ";
+			$sql .= ")";
+			$db->exec(check_sql($sql));
+			unset($sql);
+		}
+		if ($time_action == 'stop') {
+			$sql = "update v_contact_times ";
+			$sql .= "set ";
+			$sql .= "time_stop = '".date("Y-m-d H:i:s")."', ";
+			$sql .= "time_description = '".$time_description."' ";
+			$sql .= "where ";
+			$sql .= "contact_time_uuid = '".$contact_time_uuid."' ";
+			$sql .= "and domain_uuid = '".$domain_uuid."' ";
+			$sql .= "and contact_uuid = '".$contact_uuid."' ";
+			$sql .= "and user_uuid = '".$_SESSION["user"]["user_uuid"]."' ";
+			$db->exec(check_sql($sql));
+			unset($sql);
+		}
+		header("Location: contact_timer.php?domain_uuid=".$domain_uuid."&contact_uuid=".$contact_uuid);
+	}
+
+//get contact details
+	$sql = "select ";
+	$sql .= "contact_organization, ";
+	$sql .= "contact_name_given, ";
+	$sql .= "contact_name_family, ";
+	$sql .= "contact_nickname ";
+	$sql .= "from v_contacts ";
+	$sql .= "where domain_uuid = '".$domain_uuid."' ";
+	$sql .= "and contact_uuid = '".$contact_uuid."' ";
+	$prep_statement = $db->prepare(check_sql($sql));
+	$prep_statement->execute();
+	$result = $prep_statement->fetch(PDO::FETCH_NAMED);
+	if (sizeof($result) > 0) {
+		$contact_organization = $result["contact_organization"];
+		$contact_name_given = $result["contact_name_given"];
+		$contact_name_family = $result["contact_name_family"];
+		$contact_nickname = $result["contact_nickname"];
+	}
+	else {
+		exit;
+	}
+	unset ($sql, $prep_statement, $result);
+
+//determine timer state and action
+	$sql = "select ";
+	$sql .= "contact_time_uuid, ";
+	$sql .= "time_description ";
+	$sql .= "from v_contact_times ";
+	$sql .= "where domain_uuid = '".$domain_uuid."' ";
+	$sql .= "and user_uuid = '".$_SESSION['user']['user_uuid']."' ";
+	$sql .= "and contact_uuid = '".$contact_uuid."' ";
+	$sql .= "and time_start is not null ";
+	$sql .= "and time_stop is null ";
+	$prep_statement = $db->prepare(check_sql($sql));
+	$prep_statement->execute();
+	$result = $prep_statement->fetch(PDO::FETCH_NAMED);
+	if (sizeof($result) > 0) {
+		$contact_time_uuid = $result["contact_time_uuid"];
+		$time_description = $result["time_description"];
+	}
+	unset ($sql, $prep_statement, $result);
+
+	$timer_state = ($contact_time_uuid != '') ? 'running' : 'stopped';
+	$timer_action = ($timer_state == 'running') ? 'stop' : 'start';
+
+//determine contact name to display
+	if ($contact_nickname != '') {
+		$contact = $contact_nickname;
+	}
+	else if ($contact_name_given != '') {
+		$contact = $contact_name_given;
+	}
+	if ($contact_name_family != '') {
+		$contact .= ($contact != '') ? ' '.$contact_name_family : $contact_name_family;
+	}
+	if ($contact_organization != '') {
+		$contact .= ($contact != '') ? ', '.$contact_organization : $contact_organization;
+	}
+?>
+
+<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
+<head>
+	<title><?php echo $text['label-time_timer']; ?>: <?php echo $contact; ?></title>
+	<style>
+		body {
+			color: #5f5f5f;
+			font-size: 12px;
+			font-family: arial;
+			margin: 0;
+			padding: 15px;
+			}
+
+		b {
+			color: #952424;
+			font-size: 15px;
+			font-family: arial;
+			}
+
+		a {
+			color: #004083;
+			width: 100%;
+			}
+
+		a:hover {
+			color: #5082ca;
+			}
+
+		form {
+			margin: 0;
+			}
+
+		input.btn, input.button {
+			font-family: Candara, Calibri, Segoe, "Segoe UI", Optima, Arial, sans-serif;
+			padding: 2px 6px 3px 6px;
+			color: #fff;
+			font-weight: bold;
+			cursor: pointer;
+			font-size: 11px;
+			-moz-border-radius: 3px;
+			-webkit-border-radius: 3px;
+			-khtml-border-radius: 3px;
+			border-radius: 3px;
+			background-image: -moz-linear-gradient(top, #524f59 25%, #000 64%);
+			background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0.25, #524f59), color-stop(0.64, #000));
+			border: 1px solid #26242a;
+			background-color: #000;
+			text-align: center;
+			text-transform: uppercase;
+			text-shadow: 0px 0px 1px rgba(0, 0, 0, 0.85);
+			opacity: 0.9;
+			-moz-opacity: 0.9;
+			}
+
+		input.btn:hover, input.button:hover, img.list_control_icon:hover {
+			box-shadow: 0 0 5px #cddaf0;
+			-webkit-box-shadow: 0 0 5px #cddaf0;
+			-moz-box-shadow: 0 0 5px #cddaf0;
+			opacity: 1.0;
+			-moz-opacity: 1.0;
+			cursor: pointer;
+			}
+
+		input.txt, textarea.txt, select.txt, .formfld {
+			font-family: arial;
+			font-size: 12px;
+			color: #000;
+			text-align: left;
+			padding: 5px;
+			border: 1px solid #c0c0c0;
+			background-color: #fff;
+			box-shadow: 0 0 3px #cddaf0 inset;
+			-moz-box-shadow: 0 0 3px #cddaf0 inset;
+			-webkit-box-shadow: 0 0 3px #cddaf0 inset;
+			border-radius: 3px;
+			-moz-border-radius: 3px;
+			-webkit-border-radius: 3px;
+			}
+
+		input.txt, .formfld {
+			transition: width 0.25s;
+			-moz-transition: width 0.25s;
+			-webkit-transition: width 0.25s;
+			max-width: 500px;
+			}
+
+		input.txt:focus, .formfld:focus {
+			-webkit-box-shadow: 0 0 5px #cddaf0;
+			-moz-box-shadow: 0 0 5px #cddaf0;
+			box-shadow: 0 0 5px #cddaf0;
+			}
+
+		td {
+			color: #5f5f5f;
+			font-size: 12px;
+			font-family: arial;
+			}
+
+		.vncell {
+			border-bottom: 1px solid #fff;
+			background-color: #e5e9f0;
+			padding: 8px;
+			text-align: right;
+			color: #000;
+			-moz-border-radius: 4px;
+			-webkit-border-radius: 4px;
+			border-radius: 4px;
+			border-right: 3px solid #e5e9f0;
+			}
+
+		DIV.timer_running {
+			vertical-align: middle;
+			padding-top: 7px;
+			line-height: 50px;
+			width: 100%;
+			height: 53px;
+			text-align: center;
+			background-color: #2C9DE8;
+			font-size: 50px;
+			color: #FFFFFF;
+			/*-webkit-text-shadow: 0px 0px 5px #000;*/
+			/*-moz-text-shadow: 0px 0px 5px #000;*/
+			/*text-shadow: 0px 0px 5px #000;*/
+			font-weight: bold;
+			letter-spacing: -0.05em;
+			font-family: "Courier New",Courier,"Lucida Sans Typewriter","Lucida Typewriter",monospace;
+			-moz-border-radius: 4px;
+			-webkit-border-radius: 4px;
+			border-radius: 4px;
+			}
+
+		DIV.timer_stopped {
+			vertical-align: middle;
+			padding-top: 7px;
+			line-height: 50px;
+			width: 100%;
+			height: 53px;
+			text-align: center;
+			background-color: #2C9DE8;
+			font-size: 50px;
+			color: #FFFFFF;
+			/*-webkit-text-shadow: 0px 0px 5px #000;*/
+			/*-moz-text-shadow: 0px 0px 5px #000;*/
+			/*text-shadow: 0px 0px 5px #000;*/
+			font-weight: bold;
+			letter-spacing: -0.05em;
+			font-family: "Courier New",Courier,"Lucida Sans Typewriter","Lucida Typewriter",monospace;
+			-moz-border-radius: 4px;
+			-webkit-border-radius: 4px;
+			border-radius: 4px;
+			}
+
+	</style>
+
+	<script language="JavaScript" type="text/javascript" src="<?php echo PROJECT_PATH; ?>/resources/jquery/jquery-1.11.1.js"></script>
+	<script type="text/javascript">
+		$(document).ready(function(){
+			//ajax for refresh
+			var refresh = 1500;
+			var source_url = 'contact_timer_inc.php?domain_uuid=<?php echo $domain_uuid; ?>&contact_uuid=<?php echo $contact_uuid; ?>&contact_time_uuid=<?php echo $contact_time_uuid; ?>';
+
+			var ajax_get = function () {
+				$.ajax({
+					url: source_url, success: function(response){
+						$("#ajax_reponse").html(response);
+					}
+				});
+				setTimeout(ajax_get, refresh);
+			};
+			<?php if ($timer_state == 'running') { ?>
+				ajax_get();
+			<?php } ?>
+		});
+
+	//set window title to time when timer is running
+		function set_title(title_text) {
+			window.document.title = title_text;
+		}
+
+	</script>
+</head>
+<body>
+	<img src='resources/images/icon_timer.png' style='width: 24px; height: 24px; border: none; margin-left: 15px;' alt="<?php echo $text['label-time_timer']; ?>" align='right'>
+	<b><?php echo $text['label-time_timer']; ?></b>
+	<br><br>
+	<?php echo $text['description_timer']; ?>
+	<br><br>
+	<strong><a href="javascript:void(0);" onclick="window.opener.location.href='contact_edit.php?id=<?php echo $contact_uuid; ?>';"><?php echo $contact; ?></a></strong>
+	<br><br>
+	<div id='ajax_reponse' class='timer_<?php echo $timer_state;?>'>00:00:00</div>
+	<br>
+	<form name='frm' id='frm' method='post' action=''>
+	<input type='hidden' name='domain_uuid' value="<?php echo $domain_uuid; ?>">
+	<input type='hidden' name='contact_time_uuid' value="<?php echo $contact_time_uuid; ?>">
+	<input type='hidden' name='contact_uuid' value="<?php echo $contact_uuid; ?>">
+	<input type='hidden' name='time_action' value="<?php echo $timer_action; ?>">
+	<table cellpadding='0' cellspacing='0' border='0' style='width: 100%;'>
+		<tr>
+			<td class='vncell' style='text-align: center; padding: 10px;'>
+				<?php echo $text['label-description']; ?>
+				<textarea name='time_description' id='timer_description' class='formfld' style='width: 100%; height: 50px; margin-top: 5px;'><?php echo $time_description; ?></textarea>
+				<? if ($timer_state == 'stopped') { ?><script>document.getElementById('timer_description').focus();</script><? } ?>
+			</td>
+		</tr>
+	</table>
+	<br>
+	<center>
+	<?php if ($timer_state == 'running') { ?>
+		<input type='submit' class='btn' value="<?php echo $text['button-stop']; ?>">
+	<?php } else if ($timer_state == 'stopped') { ?>
+		<input type='submit' class='btn' value="<?php echo $text['button-start']; ?>">
+	<?php } ?>
+	</center>
+	</form>
+</body>
 </html>

+ 56 - 56
contact_timer_inc.php

@@ -1,57 +1,57 @@
-<?php
-/*
-	FusionPBX
-	Version: MPL 1.1
-
-	The contents of this file are subject to the Mozilla Public License Version
-	1.1 (the "License"); you may not use this file except in compliance with
-	the License. You may obtain a copy of the License at
-	http://www.mozilla.org/MPL/
-
-	Software distributed under the License is distributed on an "AS IS" basis,
-	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
-	for the specific language governing rights and limitations under the
-	License.
-
-	The Original Code is FusionPBX
-
-	The Initial Developer of the Original Code is
-	Mark J Crane <[email protected]>
-	Portions created by the Initial Developer are Copyright (C) 2008-2015
-	the Initial Developer. All Rights Reserved.
-
-	Contributor(s):
-	Mark J Crane <[email protected]>
-*/
-require_once "root.php";
-require_once "resources/require.php";
-require_once "resources/check_auth.php";
-if (!permission_exists('contact_time_add')) { echo "access denied"; exit; }
-
-//get contact and time uuids
-	$domain_uuid = check_str($_REQUEST['domain_uuid']);
-	$contact_uuid = check_str($_REQUEST['contact_uuid']);
-	$contact_time_uuid = check_str($_REQUEST['contact_time_uuid']);
-
-//get time quantity
-	$sql = "select ";
-	$sql .= "time_start ";
-	$sql .= "from v_contact_times ";
-	$sql .= "where domain_uuid = '".$domain_uuid."' ";
-	$sql .= "and contact_time_uuid = '".$contact_time_uuid."' ";
-	$sql .= "and user_uuid = '".$_SESSION['user']['user_uuid']."' ";
-	$sql .= "and contact_uuid = '".$contact_uuid."' ";
-	$sql .= "and time_start is not null ";
-	$sql .= "and time_stop is null ";
-	$prep_statement = $db->prepare(check_sql($sql));
-	$prep_statement->execute();
-	$result = $prep_statement->fetch(PDO::FETCH_NAMED);
-	if (sizeof($result) > 0) {
-		$time_start = strtotime($result["time_start"]);
-		$time_now = strtotime(date("Y-m-d H:i:s"));
-		$time_diff = gmdate("H:i:s", ($time_now - $time_start));
-		echo $time_diff;
-		echo "<script id='title_script'>set_title('".$time_diff."');</script>";
-	}
-	unset ($sql, $prep_statement, $result);
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2015
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+require_once "root.php";
+require_once "resources/require.php";
+require_once "resources/check_auth.php";
+if (!permission_exists('contact_time_add')) { echo "access denied"; exit; }
+
+//get contact and time uuids
+	$domain_uuid = check_str($_REQUEST['domain_uuid']);
+	$contact_uuid = check_str($_REQUEST['contact_uuid']);
+	$contact_time_uuid = check_str($_REQUEST['contact_time_uuid']);
+
+//get time quantity
+	$sql = "select ";
+	$sql .= "time_start ";
+	$sql .= "from v_contact_times ";
+	$sql .= "where domain_uuid = '".$domain_uuid."' ";
+	$sql .= "and contact_time_uuid = '".$contact_time_uuid."' ";
+	$sql .= "and user_uuid = '".$_SESSION['user']['user_uuid']."' ";
+	$sql .= "and contact_uuid = '".$contact_uuid."' ";
+	$sql .= "and time_start is not null ";
+	$sql .= "and time_stop is null ";
+	$prep_statement = $db->prepare(check_sql($sql));
+	$prep_statement->execute();
+	$result = $prep_statement->fetch(PDO::FETCH_NAMED);
+	if (sizeof($result) > 0) {
+		$time_start = strtotime($result["time_start"]);
+		$time_now = strtotime(date("Y-m-d H:i:s"));
+		$time_diff = gmdate("H:i:s", ($time_now - $time_start));
+		echo $time_diff;
+		echo "<script id='title_script'>set_title('".$time_diff."');</script>";
+	}
+	unset ($sql, $prep_statement, $result);
 ?>

+ 113 - 113
resources/functions/google_get_contacts.php

@@ -1,114 +1,114 @@
-<?php
-/*
-	FusionPBX
-	Version: MPL 1.1
-
-	The contents of this file are subject to the Mozilla Public License Version
-	1.1 (the "License"); you may not use this file except in compliance with
-	the License. You may obtain a copy of the License at
-	http://www.mozilla.org/MPL/
-
-	Software distributed under the License is distributed on an "AS IS" basis,
-	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
-	for the specific language governing rights and limitations under the
-	License.
-
-	The Original Code is FusionPBX
-
-	The Initial Developer of the Original Code is
-	Mark J Crane <[email protected]>
-	Portions created by the Initial Developer are Copyright (C) 2008-2013
-	the Initial Developer. All Rights Reserved.
-
-	Contributor(s):
-	Mark J Crane <[email protected]>
-*/
-
-function google_get_contacts($token, $max_results = 50) {
-	//global $records;
-	global $groups;
-
-	//$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$_SESSION['contact_auth']['token']; // all contacts as xml
-	//$url = 'https://www.google.com/m8/feeds/contacts/default/full/78967d550d3fdd99?alt=json&v=3.0&oauth_token='.$_SESSION['contact_auth']['token']; // single contact
-	$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$token; // all contacts as json
-	$xml_response = curl_file_get_contents($url);
-	$records = json_decode($xml_response, true);
-
-	//check for authentication errors (logged out of google account, or app access permission revoked, etc)
-	if ($records['error']['code']) {
-		header("Location: contact_auth.php?source=google&target=".substr($_SERVER["PHP_SELF"], strrpos($_SERVER["PHP_SELF"],'/')+1));
-		exit;
-	}
-
-	//create new array of contacts
-	foreach($records['feed']['entry'] as $contact['number'] => $contact) {
-		$contact_id = substr($contact['id']['$t'], strrpos($contact['id']['$t'], "/")+1);
-		$contacts[$contact_id]['etag'] = $contact['gd$etag'];
-		$contacts[$contact_id]['updated'] = $contact['updated']['$t'];
-		$contacts[$contact_id]['name_prefix'] = $contact['gd$name']['gd$namePrefix']['$t'];
-		$contacts[$contact_id]['name_given'] = $contact['gd$name']['gd$givenName']['$t'];
-		$contacts[$contact_id]['name_middle'] = $contact['gd$name']['gd$additionalName']['$t'];
-		$contacts[$contact_id]['name_family'] = $contact['gd$name']['gd$familyName']['$t'];
-		$contacts[$contact_id]['name_suffix'] = $contact['gd$name']['gd$nameSuffix']['$t'];
-		$contacts[$contact_id]['nickname'] = $contact['gContact$nickname']['$t'];
-		$contacts[$contact_id]['title'] = $contact['gd$organization'][0]['gd$orgTitle']['$t'];
-		$contacts[$contact_id]['organization'] = $contact['gd$organization'][0]['gd$orgName']['$t'];
-		foreach ($contact['gd$email'] as $contact_email['number'] => $contact_email) {
-			if ($contact_email['label']) {
-				$contact_email_label = $contact_email['label'];
-			}
-			else {
-				$contact_email_label = substr($contact_email['rel'], strpos($contact_email['rel'], "#")+1);
-				$contact_email_label = ucwords(str_replace("_", " ", $contact_email_label));
-			}
-			$contacts[$contact_id]['emails'][$contact_email['number']]['label'] = $contact_email_label;
-			$contacts[$contact_id]['emails'][$contact_email['number']]['address'] = $contact_email['address'];
-			$contacts[$contact_id]['emails'][$contact_email['number']]['primary'] = ($contact_email['primary']) ? 1 : 0;
-		}
-		foreach ($contact['gd$phoneNumber'] as $contact_phone['number'] => $contact_phone) {
-			if ($contact_phone['label']) {
-				$contact_phone_label = $contact_phone['label'];
-			}
-			else {
-				$contact_phone_label = substr($contact_phone['rel'], strpos($contact_phone['rel'], "#")+1);
-				$contact_phone_label = ucwords(str_replace("_", " ", $contact_phone_label));
-			}
-			$contacts[$contact_id]['numbers'][$contact_phone['number']]['label'] = $contact_phone_label;
-			$contacts[$contact_id]['numbers'][$contact_phone['number']]['number'] = preg_replace('{\D}', '', $contact_phone['$t']);
-		}
-		foreach ($contact['gContact$website'] as $contact_website['number'] => $contact_website) {
-			$contact_website_label = ($contact_website['label']) ? $contact_website['label'] : ucwords(str_replace("_", " ", $contact_website['rel']));
-			$contacts[$contact_id]['urls'][$contact_website['number']]['label'] = $contact_website_label;
-			$contacts[$contact_id]['urls'][$contact_website['number']]['url'] = $contact_website['href'];
-		}
-		foreach ($contact['gd$structuredPostalAddress'] as $contact_address['number'] => $contact_address) {
-			if ($contact_address['label']) {
-				$contact_address_label = $contact_address['label'];
-			}
-			else {
-				$contact_address_label = substr($contact_address['rel'], strpos($contact_address['rel'], "#")+1);
-				$contact_address_label = ucwords(str_replace("_", " ", $contact_address_label));
-			}
-			$contacts[$contact_id]['addresses'][$contact_address['number']]['label'] = $contact_address_label;
-			$contacts[$contact_id]['addresses'][$contact_address['number']]['street'] = $contact_address['gd$street']['$t'];
-			$contacts[$contact_id]['addresses'][$contact_address['number']]['extended'] = $contact_address['gd$pobox']['$t'];
-			$contacts[$contact_id]['addresses'][$contact_address['number']]['community'] = $contact_address['gd$neighborhood']['$t'];
-			$contacts[$contact_id]['addresses'][$contact_address['number']]['locality'] = $contact_address['gd$city']['$t'];
-			$contacts[$contact_id]['addresses'][$contact_address['number']]['region'] = $contact_address['gd$region']['$t'];
-			$contacts[$contact_id]['addresses'][$contact_address['number']]['postal_code'] = $contact_address['gd$postcode']['$t'];
-			$contacts[$contact_id]['addresses'][$contact_address['number']]['country'] = $contact_address['gd$country']['$t'];
-		}
-		foreach ($contact['gContact$groupMembershipInfo'] as $contact_group['number'] => $contact_group) {
-			$contact_group_id = substr($contact_group['href'], strrpos($contact_group['href'], "/")+1);
-			$contacts[$contact_id]['groups'][$contact_group_id] = $groups[$contact_group_id]['name'];
-		}
-		$contacts[$contact_id]['notes'] = $contact['content']['$t'];
-	}
-
-	//set account holder info
-	$_SESSION['contact_auth']['name'] = $records['feed']['author'][0]['name']['$t'];
-	$_SESSION['contact_auth']['email'] = $records['feed']['author'][0]['email']['$t'];
-
-	return $contacts;
-}
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2013
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+
+function google_get_contacts($token, $max_results = 50) {
+	//global $records;
+	global $groups;
+
+	//$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$_SESSION['contact_auth']['token']; // all contacts as xml
+	//$url = 'https://www.google.com/m8/feeds/contacts/default/full/78967d550d3fdd99?alt=json&v=3.0&oauth_token='.$_SESSION['contact_auth']['token']; // single contact
+	$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$token; // all contacts as json
+	$xml_response = curl_file_get_contents($url);
+	$records = json_decode($xml_response, true);
+
+	//check for authentication errors (logged out of google account, or app access permission revoked, etc)
+	if ($records['error']['code']) {
+		header("Location: contact_auth.php?source=google&target=".substr($_SERVER["PHP_SELF"], strrpos($_SERVER["PHP_SELF"],'/')+1));
+		exit;
+	}
+
+	//create new array of contacts
+	foreach($records['feed']['entry'] as $contact['number'] => $contact) {
+		$contact_id = substr($contact['id']['$t'], strrpos($contact['id']['$t'], "/")+1);
+		$contacts[$contact_id]['etag'] = $contact['gd$etag'];
+		$contacts[$contact_id]['updated'] = $contact['updated']['$t'];
+		$contacts[$contact_id]['name_prefix'] = $contact['gd$name']['gd$namePrefix']['$t'];
+		$contacts[$contact_id]['name_given'] = $contact['gd$name']['gd$givenName']['$t'];
+		$contacts[$contact_id]['name_middle'] = $contact['gd$name']['gd$additionalName']['$t'];
+		$contacts[$contact_id]['name_family'] = $contact['gd$name']['gd$familyName']['$t'];
+		$contacts[$contact_id]['name_suffix'] = $contact['gd$name']['gd$nameSuffix']['$t'];
+		$contacts[$contact_id]['nickname'] = $contact['gContact$nickname']['$t'];
+		$contacts[$contact_id]['title'] = $contact['gd$organization'][0]['gd$orgTitle']['$t'];
+		$contacts[$contact_id]['organization'] = $contact['gd$organization'][0]['gd$orgName']['$t'];
+		foreach ($contact['gd$email'] as $contact_email['number'] => $contact_email) {
+			if ($contact_email['label']) {
+				$contact_email_label = $contact_email['label'];
+			}
+			else {
+				$contact_email_label = substr($contact_email['rel'], strpos($contact_email['rel'], "#")+1);
+				$contact_email_label = ucwords(str_replace("_", " ", $contact_email_label));
+			}
+			$contacts[$contact_id]['emails'][$contact_email['number']]['label'] = $contact_email_label;
+			$contacts[$contact_id]['emails'][$contact_email['number']]['address'] = $contact_email['address'];
+			$contacts[$contact_id]['emails'][$contact_email['number']]['primary'] = ($contact_email['primary']) ? 1 : 0;
+		}
+		foreach ($contact['gd$phoneNumber'] as $contact_phone['number'] => $contact_phone) {
+			if ($contact_phone['label']) {
+				$contact_phone_label = $contact_phone['label'];
+			}
+			else {
+				$contact_phone_label = substr($contact_phone['rel'], strpos($contact_phone['rel'], "#")+1);
+				$contact_phone_label = ucwords(str_replace("_", " ", $contact_phone_label));
+			}
+			$contacts[$contact_id]['numbers'][$contact_phone['number']]['label'] = $contact_phone_label;
+			$contacts[$contact_id]['numbers'][$contact_phone['number']]['number'] = preg_replace('{\D}', '', $contact_phone['$t']);
+		}
+		foreach ($contact['gContact$website'] as $contact_website['number'] => $contact_website) {
+			$contact_website_label = ($contact_website['label']) ? $contact_website['label'] : ucwords(str_replace("_", " ", $contact_website['rel']));
+			$contacts[$contact_id]['urls'][$contact_website['number']]['label'] = $contact_website_label;
+			$contacts[$contact_id]['urls'][$contact_website['number']]['url'] = $contact_website['href'];
+		}
+		foreach ($contact['gd$structuredPostalAddress'] as $contact_address['number'] => $contact_address) {
+			if ($contact_address['label']) {
+				$contact_address_label = $contact_address['label'];
+			}
+			else {
+				$contact_address_label = substr($contact_address['rel'], strpos($contact_address['rel'], "#")+1);
+				$contact_address_label = ucwords(str_replace("_", " ", $contact_address_label));
+			}
+			$contacts[$contact_id]['addresses'][$contact_address['number']]['label'] = $contact_address_label;
+			$contacts[$contact_id]['addresses'][$contact_address['number']]['street'] = $contact_address['gd$street']['$t'];
+			$contacts[$contact_id]['addresses'][$contact_address['number']]['extended'] = $contact_address['gd$pobox']['$t'];
+			$contacts[$contact_id]['addresses'][$contact_address['number']]['community'] = $contact_address['gd$neighborhood']['$t'];
+			$contacts[$contact_id]['addresses'][$contact_address['number']]['locality'] = $contact_address['gd$city']['$t'];
+			$contacts[$contact_id]['addresses'][$contact_address['number']]['region'] = $contact_address['gd$region']['$t'];
+			$contacts[$contact_id]['addresses'][$contact_address['number']]['postal_code'] = $contact_address['gd$postcode']['$t'];
+			$contacts[$contact_id]['addresses'][$contact_address['number']]['country'] = $contact_address['gd$country']['$t'];
+		}
+		foreach ($contact['gContact$groupMembershipInfo'] as $contact_group['number'] => $contact_group) {
+			$contact_group_id = substr($contact_group['href'], strrpos($contact_group['href'], "/")+1);
+			$contacts[$contact_id]['groups'][$contact_group_id] = $groups[$contact_group_id]['name'];
+		}
+		$contacts[$contact_id]['notes'] = $contact['content']['$t'];
+	}
+
+	//set account holder info
+	$_SESSION['contact_auth']['name'] = $records['feed']['author'][0]['name']['$t'];
+	$_SESSION['contact_auth']['email'] = $records['feed']['author'][0]['email']['$t'];
+
+	return $contacts;
+}
 ?>

+ 53 - 53
resources/functions/google_get_groups.php

@@ -1,54 +1,54 @@
-<?php
-/*
-	FusionPBX
-	Version: MPL 1.1
-
-	The contents of this file are subject to the Mozilla Public License Version
-	1.1 (the "License"); you may not use this file except in compliance with
-	the License. You may obtain a copy of the License at
-	http://www.mozilla.org/MPL/
-
-	Software distributed under the License is distributed on an "AS IS" basis,
-	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
-	for the specific language governing rights and limitations under the
-	License.
-
-	The Original Code is FusionPBX
-
-	The Initial Developer of the Original Code is
-	Mark J Crane <[email protected]>
-	Portions created by the Initial Developer are Copyright (C) 2008-2013
-	the Initial Developer. All Rights Reserved.
-
-	Contributor(s):
-	Mark J Crane <[email protected]>
-*/
-
-function google_get_groups($token) {
-	// retrieve groups
-	$url = 'https://www.google.com/m8/feeds/groups/default/full?alt=json&v=3.0&oauth_token='.$token;
-	$xml_response = curl_file_get_contents($url);
-	$records = json_decode($xml_response, true);
-
-	//check for authentication errors
-	if ($records['error']['code']) {
-		header("Location: contact_auth.php?source=google&target=".substr($_SERVER["PHP_SELF"], strrpos($_SERVER["PHP_SELF"],'/')+1));
-		exit;
-	}
-
-	//create new array of groups
-	foreach($records['feed']['entry'] as $group['number'] => $group) {
-		$group_id = substr($group['id']['$t'], strrpos($group['id']['$t'], "/")+1);
-		$groups[$group_id]['name'] = ($group['gContact$systemGroup']['id']) ? $group['gContact$systemGroup']['id'] : $group['title']['$t'];
-		$groups[$group_id]['count'] = 0;
-		unset($group_id);
-	}
-	unset($group);
-
-	//set account holder info
-	$_SESSION['contact_auth']['name'] = $records['feed']['author'][0]['name']['$t'];
-	$_SESSION['contact_auth']['email'] = $records['feed']['author'][0]['email']['$t'];
-
-	return $groups;
-}
+<?php
+/*
+	FusionPBX
+	Version: MPL 1.1
+
+	The contents of this file are subject to the Mozilla Public License Version
+	1.1 (the "License"); you may not use this file except in compliance with
+	the License. You may obtain a copy of the License at
+	http://www.mozilla.org/MPL/
+
+	Software distributed under the License is distributed on an "AS IS" basis,
+	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+	for the specific language governing rights and limitations under the
+	License.
+
+	The Original Code is FusionPBX
+
+	The Initial Developer of the Original Code is
+	Mark J Crane <[email protected]>
+	Portions created by the Initial Developer are Copyright (C) 2008-2013
+	the Initial Developer. All Rights Reserved.
+
+	Contributor(s):
+	Mark J Crane <[email protected]>
+*/
+
+function google_get_groups($token) {
+	// retrieve groups
+	$url = 'https://www.google.com/m8/feeds/groups/default/full?alt=json&v=3.0&oauth_token='.$token;
+	$xml_response = curl_file_get_contents($url);
+	$records = json_decode($xml_response, true);
+
+	//check for authentication errors
+	if ($records['error']['code']) {
+		header("Location: contact_auth.php?source=google&target=".substr($_SERVER["PHP_SELF"], strrpos($_SERVER["PHP_SELF"],'/')+1));
+		exit;
+	}
+
+	//create new array of groups
+	foreach($records['feed']['entry'] as $group['number'] => $group) {
+		$group_id = substr($group['id']['$t'], strrpos($group['id']['$t'], "/")+1);
+		$groups[$group_id]['name'] = ($group['gContact$systemGroup']['id']) ? $group['gContact$systemGroup']['id'] : $group['title']['$t'];
+		$groups[$group_id]['count'] = 0;
+		unset($group_id);
+	}
+	unset($group);
+
+	//set account holder info
+	$_SESSION['contact_auth']['name'] = $records['feed']['author'][0]['name']['$t'];
+	$_SESSION['contact_auth']['email'] = $records['feed']['author'][0]['email']['$t'];
+
+	return $groups;
+}
 ?>