From c41ab549b4d1cfc591278fa31392bb548995a6cc Mon Sep 17 00:00:00 2001 From: "a.cianfarani" Date: Tue, 21 May 2013 17:57:24 +0200 Subject: [PATCH 1/4] Add format controls on url and email value during installation --- PHPCI/Command/InstallCommand.php | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/PHPCI/Command/InstallCommand.php b/PHPCI/Command/InstallCommand.php index 2549e5cc..8c38dc21 100644 --- a/PHPCI/Command/InstallCommand.php +++ b/PHPCI/Command/InstallCommand.php @@ -43,6 +43,11 @@ class InstallCommand extends Command $dbUser = $this->ask('Enter your MySQL username: '); $dbPass = $this->ask('Enter your MySQL password: ', true); $ciUrl = $this->ask('Your PHPCI URL (without trailing slash): '); + // verify url format and re-ask if wrong + while(! $this->controlFormat($ciUrl,array(FILTER_VALIDATE_URL,"/[^\/]$/i"),$status)) { + print $status; + $ciUrl = $this->ask('Your PHPCI URL (without trailing slash): '); + } $ghId = $this->ask('(Optional) Github Application ID: ', true); $ghSecret = $this->ask('(Optional) Github Application Secret: ', true); @@ -52,6 +57,7 @@ class InstallCommand extends Command shell_exec($cmd); + $str = "controlFormat($adminEmail,FILTER_VALIDATE_EMAIL,$status)) { + print $status; + $adminEmail = $this->ask('Enter your email address (leave blank if updating): '); + } + } $adminPass = $this->ask('Enter your desired admin password: '); $adminName = $this->ask('Enter your name: '); @@ -125,4 +138,39 @@ b8\Database::setReadServers(array('{$dbHost}')); return $rtn; } + protected function controlFormat($valueToInspect,$filter,&$statusMessage) + { + $filters = !(is_array($filter))? array($filter) : $filter; + $statusMessage = ''; + $status = true; + $options = array(); + + foreach ($filters as $filter) { + if (! is_int($filter)) { + $regexp = $filter; + $filter = FILTER_VALIDATE_REGEXP; + $options = array( + 'options' => array( + 'regexp' => $regexp, + ) + ); + } + if (! filter_var($valueToInspect,$filter,$options)) { + $status = false; + switch ($filter) + { + case FILTER_VALIDATE_URL : + $statusMessage = 'Incorrect url format.' . PHP_EOL; + break; + case FILTER_VALIDATE_EMAIL : + $statusMessage = 'Incorrect e-mail format.' . PHP_EOL; + break; + case FILTER_VALIDATE_REGEXP : + $statusMessage = 'Incorrect format.' . PHP_EOL; + break; + } + } + } + return $status; + } } From b8e932cb76daaf60836274600019f1ad6fa741fa Mon Sep 17 00:00:00 2001 From: "a.cianfarani" Date: Wed, 22 May 2013 09:58:04 +0200 Subject: [PATCH 2/4] Some coding style corrections --- PHPCI/Command/InstallCommand.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/PHPCI/Command/InstallCommand.php b/PHPCI/Command/InstallCommand.php index 8c38dc21..dea8f430 100644 --- a/PHPCI/Command/InstallCommand.php +++ b/PHPCI/Command/InstallCommand.php @@ -44,7 +44,7 @@ class InstallCommand extends Command $dbPass = $this->ask('Enter your MySQL password: ', true); $ciUrl = $this->ask('Your PHPCI URL (without trailing slash): '); // verify url format and re-ask if wrong - while(! $this->controlFormat($ciUrl,array(FILTER_VALIDATE_URL,"/[^\/]$/i"),$status)) { + while (! $this->controlFormat($ciUrl, array(FILTER_VALIDATE_URL,"/[^\/]$/i"), $status) ) { print $status; $ciUrl = $this->ask('Your PHPCI URL (without trailing slash): '); } @@ -92,15 +92,14 @@ b8\Database::setReadServers(array('{$dbHost}')); if (empty($adminEmail)) { return; - } - else { + } else { // verify e-mail format and re-ask if wrong - while (! $this->controlFormat($adminEmail,FILTER_VALIDATE_EMAIL,$status)) { + while (! $this->controlFormat($adminEmail, FILTER_VALIDATE_EMAIL, $status)) { print $status; $adminEmail = $this->ask('Enter your email address (leave blank if updating): '); } } - + $adminPass = $this->ask('Enter your desired admin password: '); $adminName = $this->ask('Enter your name: '); @@ -155,7 +154,7 @@ b8\Database::setReadServers(array('{$dbHost}')); ) ); } - if (! filter_var($valueToInspect,$filter,$options)) { + if (! filter_var($valueToInspect, $filter, $options)) { $status = false; switch ($filter) { @@ -171,6 +170,7 @@ b8\Database::setReadServers(array('{$dbHost}')); } } } + return $status; } } From ee4eab7a6cb9f7b7ec3705f7c2190be23aff1216 Mon Sep 17 00:00:00 2001 From: "a.cianfarani" Date: Thu, 23 May 2013 11:31:58 +0200 Subject: [PATCH 3/4] Move format controls into ask function --- PHPCI/Command/InstallCommand.php | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/PHPCI/Command/InstallCommand.php b/PHPCI/Command/InstallCommand.php index dea8f430..9c5472c6 100644 --- a/PHPCI/Command/InstallCommand.php +++ b/PHPCI/Command/InstallCommand.php @@ -42,12 +42,7 @@ class InstallCommand extends Command $dbName = $this->ask('Enter the database name PHPCI should use: '); $dbUser = $this->ask('Enter your MySQL username: '); $dbPass = $this->ask('Enter your MySQL password: ', true); - $ciUrl = $this->ask('Your PHPCI URL (without trailing slash): '); - // verify url format and re-ask if wrong - while (! $this->controlFormat($ciUrl, array(FILTER_VALIDATE_URL,"/[^\/]$/i"), $status) ) { - print $status; - $ciUrl = $this->ask('Your PHPCI URL (without trailing slash): '); - } + $ciUrl = $this->ask('Your PHPCI URL (without trailing slash): ', false, array(FILTER_VALIDATE_URL,"/[^\/]$/i")); $ghId = $this->ask('(Optional) Github Application ID: ', true); $ghSecret = $this->ask('(Optional) Github Application Secret: ', true); @@ -88,18 +83,11 @@ b8\Database::setReadServers(array('{$dbHost}')); $gen->generate(); // Try to create a user account: - $adminEmail = $this->ask('Enter your email address (leave blank if updating): ', true); + $adminEmail = $this->ask('Enter your email address (leave blank if updating): ', true, FILTER_VALIDATE_EMAIL); if (empty($adminEmail)) { return; - } else { - // verify e-mail format and re-ask if wrong - while (! $this->controlFormat($adminEmail, FILTER_VALIDATE_EMAIL, $status)) { - print $status; - $adminEmail = $this->ask('Enter your email address (leave blank if updating): '); - } } - $adminPass = $this->ask('Enter your desired admin password: '); $adminName = $this->ask('Enter your name: '); @@ -120,7 +108,7 @@ b8\Database::setReadServers(array('{$dbHost}')); } } - protected function ask($question, $emptyOk = false) + protected function ask($question, $emptyOk = false, $validationFilter = null) { print $question . ' '; @@ -132,9 +120,13 @@ b8\Database::setReadServers(array('{$dbHost}')); $rtn = trim($rtn); if (!$emptyOk && empty($rtn)) { - $rtn = $this->ask($question, $emptyOk); + $rtn = $this->ask($question, $emptyOk, $validationFilter); + } elseif ($validationFilter != null && ! empty($rtn)) { + if(! $this -> controlFormat($rtn, $validationFilter, $statusMessage)) { + print $statusMessage; + $rtn = $this->ask($question, $emptyOk, $validationFilter); + } } - return $rtn; } protected function controlFormat($valueToInspect,$filter,&$statusMessage) @@ -156,6 +148,7 @@ b8\Database::setReadServers(array('{$dbHost}')); } if (! filter_var($valueToInspect, $filter, $options)) { $status = false; + switch ($filter) { case FILTER_VALIDATE_URL : @@ -170,7 +163,6 @@ b8\Database::setReadServers(array('{$dbHost}')); } } } - return $status; } } From 857c14f0cd5fc43aeacab3aad567ff3aebb0bf30 Mon Sep 17 00:00:00 2001 From: "a.cianfarani" Date: Thu, 23 May 2013 12:14:42 +0200 Subject: [PATCH 4/4] Some coding style corrections --- PHPCI/Command/InstallCommand.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/PHPCI/Command/InstallCommand.php b/PHPCI/Command/InstallCommand.php index b93249b3..f605833f 100644 --- a/PHPCI/Command/InstallCommand.php +++ b/PHPCI/Command/InstallCommand.php @@ -122,11 +122,12 @@ b8\Database::setReadServers(array('{$dbHost}')); if (!$emptyOk && empty($rtn)) { $rtn = $this->ask($question, $emptyOk, $validationFilter); } elseif ($validationFilter != null && ! empty($rtn)) { - if(! $this -> controlFormat($rtn, $validationFilter, $statusMessage)) { + if (! $this -> controlFormat($rtn, $validationFilter, $statusMessage)) { print $statusMessage; $rtn = $this->ask($question, $emptyOk, $validationFilter); } } + return $rtn; } protected function controlFormat($valueToInspect,$filter,&$statusMessage) @@ -148,7 +149,7 @@ b8\Database::setReadServers(array('{$dbHost}')); } if (! filter_var($valueToInspect, $filter, $options)) { $status = false; - + switch ($filter) { case FILTER_VALIDATE_URL : @@ -163,6 +164,7 @@ b8\Database::setReadServers(array('{$dbHost}')); } } } + return $status; } }