From 372db3cc504715263cc3b2162679765389d612d3 Mon Sep 17 00:00:00 2001 From: frytimo Date: Fri, 3 Nov 2023 17:04:07 -0300 Subject: [PATCH] Patch contacts calling multiple schemas (#6826) * improve performance for contacts app_defaults * add app_name and app_uuid as constants in class * performance boost by removing scanning app_defaults twice * Update app_defaults.php * Update schema.php --------- Co-authored-by: FusionPBX --- app/contacts/app_defaults.php | 93 ++++++++------------- app/contacts/resources/classes/contacts.php | 6 +- resources/classes/schema.php | 37 +++----- 3 files changed, 47 insertions(+), 89 deletions(-) diff --git a/app/contacts/app_defaults.php b/app/contacts/app_defaults.php index 14bf5e83b1..2f173d0ff6 100644 --- a/app/contacts/app_defaults.php +++ b/app/contacts/app_defaults.php @@ -1,12 +1,15 @@ db_type = $db_type; $obj->schema(); - $field_exists = $obj->column_exists($db_name, 'v_contact_phones', 'phone_type'); //check if field exists + $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; @@ -20,59 +23,48 @@ if ($domains_processed == 1) { $sql .= "or phone_type = 'voicemail' "; $sql .= "or phone_type = 'cell' "; $sql .= "or phone_type = 'pcs' "; - $database = new database; $database->execute($sql); unset($sql); $sql = "update v_contact_phones set phone_type_fax = '1' where phone_type = 'fax'"; - $database = new database; $database->execute($sql); unset($sql); $sql = "update v_contact_phones set phone_type_video = '1' where phone_type = 'video'"; - $database = new database; $database->execute($sql); unset($sql); $sql = "update v_contact_phones set phone_type_text = '1' where phone_type = 'cell' or phone_type = 'pager'"; - $database = new database; $database->execute($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'); + $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) { $sql = "update v_contact_phones set phone_label = :phone_label where phone_type = :phone_type "; $parameters['phone_label'] = $default_phone_labels[$index]; //new $parameters['phone_type'] = $old; - $database = new database; $database->execute($sql, $parameters); unset($sql, $parameters); } // empty phone_type field to prevent confusion in the future $sql = "update v_contact_phones set phone_type is null"; - $database = new database; $database->execute($sql); unset($sql); } - unset($obj); - //populate primary email from deprecated field in v_contact table - $obj = new schema; - $obj->db_type = $db_type; - $obj->schema(); - $field_exists = $obj->column_exists($db_name, 'v_contacts', 'contact_email'); //check if field exists + //populate primary email from deprecated field in v_contact table + $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 != '' "; - $database = new database; $result = $database->select($sql); unset($sql); - if (is_array($result) && @sizeof($result) != 0) { - foreach($result as $row) { + if ($result !== false) { + foreach ($result as $row) { $array['contact_emails'][0]['contact_email_uuid'] = uuid(); $array['contact_emails'][0]['domain_uuid'] = $row['domain_uuid']; $array['contact_emails'][0]['contact_uuid'] = $row['contact_uuid']; @@ -82,9 +74,8 @@ if ($domains_processed == 1) { $p = new permissions; $p->add('contact_email_add', 'temp'); - $database = new database; - $database->app_name = 'contacts'; - $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c'; + $database->app_name = contacts::APP_NAME; + $database->app_uuid = contacts::APP_UUID; $database->save($array, false); unset($array); @@ -98,17 +89,15 @@ if ($domains_processed == 1) { $parameters['domain_uuid'] = $row['domain_uuid']; $parameters['contact_uuid'] = $row['contact_uuid']; $parameters['email_address'] = $row['contact_email']; - $database = new database; $result_2 = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); - if (is_array($result_2) && @sizeof($result_2) != 0) { + if ($result_2 !== false) { $sql = "update v_contacts set contact_email = null "; $sql .= "where domain_uuid = :domain_uuid "; $sql .= "and contact_uuid = :contact_uuid "; $parameters['domain_uuid'] = $row['domain_uuid']; $parameters['contact_uuid'] = $row['contact_uuid']; - $database = new database; $database->execute($sql, $parameters); unset($sql, $parameters); } @@ -117,22 +106,17 @@ if ($domains_processed == 1) { } unset($result, $row); } - unset($obj); - //populate primary url from deprecated field in v_contact table - $obj = new schema; - $obj->db_type = $db_type; - $obj->schema(); - $field_exists = $obj->column_exists($db_name, 'v_contacts', 'contact_url'); //check if field exists + //populate primary url from deprecated field in v_contact table + $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 != ''"; - $database = new database; $result = $database->select($sql); unset($sql); - if (is_array($result) && @sizeof($result) != 0) { - foreach($result as $row) { + if ($result !== false) { + foreach ($result as $row) { $array['contact_urls'][0]['contact_url_uuid'] = uuid(); $array['contact_urls'][0]['domain_uuid'] = $row['domain_uuid']; $array['contact_urls'][0]['contact_uuid'] = $row['contact_uuid']; @@ -142,9 +126,8 @@ if ($domains_processed == 1) { $p = new permissions; $p->add('contact_url_add', 'temp'); - $database = new database; - $database->app_name = 'contacts'; - $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c'; + $database->app_name = contacts::APP_NAME; + $database->app_uuid = contacts::APP_UUID; $database->save($array, false); unset($array); @@ -158,7 +141,6 @@ if ($domains_processed == 1) { $parameters['domain_uuid'] = $row['domain_uuid']; $parameters['contact_uuid'] = $row['contact_uuid']; $parameters['url_address'] = $row['contact_url']; - $database = new database; $result_2 = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); @@ -168,7 +150,6 @@ if ($domains_processed == 1) { $sql .= "and contact_uuid = :contact_uuid "; $parameters['domain_uuid'] = $row['domain_uuid']; $parameters['contact_uuid'] = $row['contact_uuid']; - $database = new database; $database->execute($sql, $parameters); unset($sql, $parameters); } @@ -177,26 +158,23 @@ if ($domains_processed == 1) { } unset($result, $row); } - unset($obj); - //set [name]_primary fields to 0 where null - $name_tables = array('phones','addresses','emails','urls'); - $name_fields = array('phone','address','email','url'); + //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 "; - $database = new database; + $sql = "update v_contact_" . $name_table . " set " . $name_fields[$name_index] . "_primary = 0 "; + $sql .= "where " . $name_fields[$name_index] . "_primary is null "; $database->execute($sql); unset($sql); } unset($name_tables, $name_fields, $name_index, $name_table); - //move the users from the contact groups table into the contact users table + //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) "; - $database = new database; $result = $database->select($sql, null, 'all'); - if (is_array($result) && @sizeof($result) != 0) { + if ($result !== false) { foreach ($result as &$row) { $p = new permissions; $p->add('contact_user_add', 'temp'); @@ -207,27 +185,22 @@ if ($domains_processed == 1) { $array['contact_users'][0]['contact_uuid'] = $row["contact_uuid"]; $array['contact_users'][0]['user_uuid'] = $row["group_uuid"]; - $database = new database; - $database->app_name = 'contacts'; - $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c'; + $database->app_name = contacts::APP_NAME; + $database->app_uuid = contacts::APP_UUID; $database->save($array, false); unset($array); $array['contact_groups'][0]['contact_group_uuid'] = $row["contact_group_uuid"]; - $database = new database; - $database->app_name = 'contacts'; - $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c'; + $database->app_name = contacts::APP_NAME; + $database->app_uuid = contacts::APP_UUID; $database->delete($array); unset($array); $p->delete('contact_user_add', 'temp'); $p->delete('contact_group_delete', 'temp'); } - } unset($sql, $result, $row); - -} - + } ?> diff --git a/app/contacts/resources/classes/contacts.php b/app/contacts/resources/classes/contacts.php index 100d6c676c..9fa93eec23 100644 --- a/app/contacts/resources/classes/contacts.php +++ b/app/contacts/resources/classes/contacts.php @@ -28,6 +28,8 @@ if (!class_exists('contacts')) { class contacts { + const APP_NAME = "contacts"; + const APP_UUID = "04481e0e-a478-c559-adad-52bd4174574c"; /** * declare private variables */ @@ -49,8 +51,8 @@ if (!class_exists('contacts')) { public function __construct() { //assign private variables - $this->app_name = 'contacts'; - $this->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c'; + $this->app_name = self::APP_NAME; + $this->app_uuid = self::APP_UUID; $this->permission_prefix = 'contact_'; $this->list_page = 'contacts.php'; $this->tables[] = 'contact_addresses'; diff --git a/resources/classes/schema.php b/resources/classes/schema.php index b792d6c4a1..3aaa98259a 100644 --- a/resources/classes/schema.php +++ b/resources/classes/schema.php @@ -523,22 +523,9 @@ if (!class_exists('schema')) { //check if table exists // SELECT TABLE_NAME FROM ALL_TABLES - //get the $apps array from the installed apps from the core and mod directories - $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php"); - $x=0; - foreach ($config_list as &$config_path) { - try { - include($config_path); - } - catch (Exception $e) { - //echo 'Caught exception: ', $e->getMessage(), "\n"; - } - $x++; - } - //update the app db array add exists true or false $sql = ''; - foreach ($apps as $x => &$app) { + foreach ($this->apps as $x => &$app) { if (isset($app['db'])) foreach ($app['db'] as $y => &$row) { if (isset($row['table']['name'])) { if (is_array($row['table']['name'])) { @@ -598,10 +585,9 @@ if (!class_exists('schema')) { //prepare the variables $sql_update = ''; - $var_uuid = $_GET["id"] ?? ''; //add missing tables and fields - foreach ($apps as $x => &$app) { + foreach ($this->apps as $x => &$app) { if (isset($app['db'])) foreach ($app['db'] as $y => &$row) { if (is_array($row['table']['name'])) { $table_name = $row['table']['name']['text']; @@ -623,7 +609,7 @@ if (!class_exists('schema')) { } else { $row['exists'] = "false"; - $sql_update .= $this->db_create_table($apps, $db_type, $row['table']['name']['text']); + $sql_update .= $this->db_create_table($this->apps, $db_type, $row['table']['name']['text']); } } } @@ -649,7 +635,7 @@ if (!class_exists('schema')) { if (is_array($field['name'])) { $field_name = $field['name']['text']; if (!$this->db_column_exists ($db_type, $db_name, $table_name, $field_name)) { - $field['exists'] == "false"; + $field['exists'] = "false"; } } else { @@ -734,19 +720,18 @@ if (!class_exists('schema')) { } } - unset($column_array); } } else { //create table if (!is_array($row['table']['name'])) { - $sql_update .= $this->db_create_table($apps, $db_type, $row['table']['name']); + $sql_update .= $this->db_create_table($this->apps, $db_type, $row['table']['name']); } } } } //rebuild and populate the table - foreach ($apps as $x => &$app) { + foreach ($this->apps as $x => &$app) { if (isset($app['db'])) foreach ($app['db'] as $y => &$row) { if (is_array($row['table']['name'])) { $table_name = $row['table']['name']['text']; @@ -761,9 +746,9 @@ if (!class_exists('schema')) { //rename the table $sql_update .= "ALTER TABLE ".$table_name." RENAME TO tmp_".$table_name.";\n"; //create the table - $sql_update .= $this->db_create_table($apps, $db_type, $table_name); + $sql_update .= $this->db_create_table($this->apps, $db_type, $table_name); //insert the data into the new table - $sql_update .= $this->db_insert_into($apps, $db_type, $table_name); + $sql_update .= $this->db_insert_into($this->apps, $db_type, $table_name); //drop the old table $sql_update .= "DROP TABLE tmp_".$table_name.";\n"; //commit the transaction @@ -803,7 +788,7 @@ if (!class_exists('schema')) { $response .= "\n"; //build the html while looping through the app db array $sql = ''; - foreach ($apps as &$app) { + foreach ($this->apps as &$app) { if (isset($app['db'])) foreach ($app['db'] as $row) { if (is_array($row['table']['name'])) { $table_name = $row['table']['name']['text']; @@ -858,7 +843,6 @@ if (!class_exists('schema')) { $response .= "\n"; } } - unset($column_array); $response .= " \n"; $response .= "\n"; } @@ -871,7 +855,6 @@ if (!class_exists('schema')) { $response .= "\n"; } } - unset ($prep_statement); //end the list of tables $response .= "\n"; $response .= "
\n"; @@ -904,7 +887,7 @@ if (!class_exists('schema')) { } //$this->db->commit(); $response .= "\n"; - unset ($file_contents, $sql_update, $sql); + unset ($sql_update, $sql); } //handle response