Skip to content

Commit

Permalink
Code formatting updates
Browse files Browse the repository at this point in the history
Standard space usage, indents, and other aspects to make the code more readable and consistent.
  • Loading branch information
toolstack authored and julianxhokaxhiu committed Apr 19, 2022
1 parent 6961c8f commit 4ca4efc
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 246 deletions.
49 changes: 27 additions & 22 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,43 @@

use \JX\CmOta\CmOta;

if ( isset($_SERVER['HTTP_FORWARDED']) ) {
$fwd_ar = explode(';', $_SERVER['HTTP_FORWARDED']);
for ( $i = 0; $i < count($fwd_ar); $i++ ) {
$kv = explode('=', $fwd_ar[$i]);
if ( count($kv) > 1 ) {
$forwarded[strtoupper($kv[0])] = $kv[1];
if( isset( $_SERVER['HTTP_FORWARDED'] ) ) {
$fwd_ar = explode( ';', $_SERVER['HTTP_FORWARDED'] );

for( $i = 0; $i < count( $fwd_ar ); $i++ ) {
$kv = explode( '=', $fwd_ar[$i] );

if ( count( $kv ) > 1 ) {
$forwarded[strtoupper( $kv[0] )] = $kv[1];
}
}

if( array_key_exists( 'HOST', $forwarded ) ) {
$_SERVER['HTTP_HOST'] = $forwarded['HOST'];
}

if( array_key_exists( 'PROTO', $forwarded ) && strtoupper( $forwarded['PROTO'] ) === 'HTTPS' ) {
$_SERVER['HTTPS'] = 'on';
}
}
if ( array_key_exists('HOST', $forwarded) ) {
$_SERVER['HTTP_HOST'] = $forwarded['HOST'];
}
if ( array_key_exists('PROTO', $forwarded) && strtoupper($forwarded['PROTO']) === 'HTTPS') {
$_SERVER['HTTPS'] = 'on';
}
} else {
if ( isset($_SERVER['HTTP_X_FORWARDED_HOST']) ) {
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}
if ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtoupper($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'HTTPS' ) {
$_SERVER['HTTPS'] = 'on';
}
if( isset( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}

if( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && strtoupper( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) === 'HTTPS' ) {
$_SERVER['HTTPS'] = 'on';
}
}

if( isset($_SERVER['HTTPS']) )
if( isset( $_SERVER['HTTPS'] ) )
$protocol = 'https://';
else
$protocol = 'http://';

if ( isset($_ENV['LINEAGEOTA_BASE_PATH']) )
if( isset( $_ENV['LINEAGEOTA_BASE_PATH'] ) )
$base_path = $_ENV['LINEAGEOTA_BASE_PATH'];
else
$base_path = $protocol.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']);
$base_path = $protocol.$_SERVER['HTTP_HOST'].dirname( $_SERVER['SCRIPT_NAME'] );

$app = new CmOta();
$app
Expand Down
67 changes: 34 additions & 33 deletions src/CmOta.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function setConfig( $key, $value ) {
* @return class Return always itself, so it can be chained within calls
*/
public function setConfigJSON( $key, $file ) {
Flight::cfg()->set( $key, json_decode( file_get_contents( Flight::cfg()->get('realBasePath') . '/' . $file ) , true ) );
Flight::cfg()->set( $key, json_decode( file_get_contents( Flight::cfg()->get( 'realBasePath' ) . '/' . $file ) , true ) );

return $this;
}
Expand All @@ -83,7 +83,7 @@ public function setConfigJSON( $key, $file ) {
* @return class Return always itself, so it can be chained within calls
*/
public function loadConfigJSON( $file ) {
$settingsFile = Flight::cfg()->get('realBasePath') . '/' . $file;
$settingsFile = Flight::cfg()->get( 'realBasePath' ) . '/' . $file;

if( file_exists( $settingsFile ) ) {
$settings = json_decode( file_get_contents( $settingsFile ), true );
Expand All @@ -102,7 +102,7 @@ public function loadConfigJSON( $file ) {
* @return class Return always itself, so it can be chained within calls
*/
public function run() {
$loader = new \Twig\Loader\FilesystemLoader( Flight::cfg()->get('realBasePath') . '/views' );
$loader = new \Twig\Loader\FilesystemLoader( Flight::cfg()->get( 'realBasePath' ) . '/views' );

$twigConfig = array();

Expand Down Expand Up @@ -135,7 +135,7 @@ private function initRouting() {
// Just list the builds folder for now
Flight::route('/', function() {
// Get the template name we're going to use and tack on .twig
$templateName = Flight::cfg()->get('OTAListTemplate') . '.twig';
$templateName = Flight::cfg()->get( 'OTAListTemplate' ) . '.twig';

// Make sure the template exists, otherwise fall back to our default
if( ! file_exists( 'views/' . $templateName ) ) { $templateName = 'ota-list-tables.twig'; }
Expand All @@ -145,8 +145,8 @@ private function initRouting() {
$buildsToSort = array();
$output = '';
$model = 'Unknown';
$deviceNames = Flight::cfg()->get('DeviceNames');
$vendorNames = Flight::cfg()->get('DeviceVendors');
$deviceNames = Flight::cfg()->get( 'DeviceNames' );
$vendorNames = Flight::cfg()->get( 'DeviceVendors' );
$devicesByVendor = array();
$parsedFilenames = array();
$formatedFileSizes = array();
Expand Down Expand Up @@ -205,9 +205,9 @@ private function initRouting() {
}

// Setup branding information for the template
$branding = array( 'name' => Flight::cfg()->get('BrandName'),
'GithubURL' => Flight::cfg()->get('GithubHomeURL'),
'LocalURL' => Flight::cfg()->get('LocalHomeURL')
$branding = array( 'name' => Flight::cfg()->get( 'BrandName' ),
'GithubURL' => Flight::cfg()->get( 'GithubHomeURL' ),
'LocalURL' => Flight::cfg()->get( 'LocalHomeURL' )
);

// Sanity check the branding, use some reasonable deductions if anything is missing
Expand All @@ -230,53 +230,54 @@ private function initRouting() {
});

// Main call
Flight::route('/api', function(){
Flight::route( '/api', function() {
$ret = array(
'id' => null,
'result' => Flight::builds()->get(),
'error' => null
);

Flight::json($ret);
Flight::json( $ret );
});

// Delta updates call
Flight::route('/api/v1/build/get_delta', function(){
Flight::route( '/api/v1/build/get_delta', function() {
$ret = array();

$delta = Flight::builds()->getDelta();

if ( $delta === false ) {
$ret['errors'] = array(
'message' => 'Unable to find delta'
);
} else {
$ret = array_merge($ret, $delta);
$ret = array_merge( $ret, $delta );
}

Flight::json($ret);
});

// LineageOS new API
Flight::route('/api/v1/@deviceType(/@romType(/@incrementalVersion))', function ( $deviceType, $romType, $incrementalVersion ){
Flight::builds()->setPostData(
array(
'params' => array(
'device' => $deviceType,
'channels' => array(
$romType,
),
'source_incremental' => $incrementalVersion,
),
)
);

$ret = array(
'id' => null,
'response' => Flight::builds()->get(),
'error' => null
);

Flight::json($ret);
Flight::route( '/api/v1/@deviceType(/@romType(/@incrementalVersion))', function ( $deviceType, $romType, $incrementalVersion ) {
Flight::builds()->setPostData(
array(
'params' => array(
'device' => $deviceType,
'channels' => array(
$romType,
),
'source_incremental' => $incrementalVersion,
),
)
);

$ret = array(
'id' => null,
'response' => Flight::builds()->get(),
'error' => null
);

Flight::json( $ret );
});
}

Expand Down
80 changes: 43 additions & 37 deletions src/Helpers/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ class Build {
* @return boolean True if valid, False if not.
*/
public function isValid( $params ){
if ( $params === NULL ) return true; // Assume valid if no parameters
if( $params === NULL ) return true; // Assume valid if no parameters

$ret = false;

if ( $params['device'] == $this->model ) {
if ( count($params['channels']) > 0 ) {
foreach ( $params['channels'] as $channel ) {
if ( strtolower($channel) == $this->channel ) $ret = true;
if( $params['device'] == $this->model ) {
if( count($params['channels']) > 0 ) {
foreach( $params['channels'] as $channel ) {
if( strtolower($channel) == $this->channel ) $ret = true;
}
}
}
Expand All @@ -80,15 +80,15 @@ public function getMD5(){
* @return string filesize in bytes
*/
public function getSize() {
return $this->size;
return $this->size;
}

/**
* Get a unique id of the current build
* @return string A unique id
*/
public function getUid() {
return $this->uid;
return $this->uid;
}

/**
Expand Down Expand Up @@ -186,19 +186,19 @@ public function importData( $data ) {
*/
public function parseFilenameFull( $fileName ) {
/*
tokens Schema:
array(
1 => [TYPE] (ex. cm, lineage, etc.)
2 => [VERSION] (ex. 10.1.x, 10.2, 11, etc.)
3 => [DATE OF BUILD] (ex. 20140130)
4 => [CHANNEL OF THE BUILD] (ex. RC, RC2, NIGHTLY, etc.)
5 =>
CM => [SNAPSHOT CODE] (ex. ZNH0EAO2O0, etc.)
LINEAGE => [MODEL] (ex. i9100, i9300, etc.)
6 =>
CM => [MODEL] (ex. i9100, i9300, etc.)
LINEAGE => [SIGNED] (ex. signed)
)
tokens Schema:
array(
1 => [TYPE] (ex. cm, lineage, etc.)
2 => [VERSION] (ex. 10.1.x, 10.2, 11, etc.)
3 => [DATE OF BUILD] (ex. 20140130)
4 => [CHANNEL OF THE BUILD] (ex. RC, RC2, NIGHTLY, etc.)
5 =>
CM => [SNAPSHOT CODE] (ex. ZNH0EAO2O0, etc.)
LINEAGE => [MODEL] (ex. i9100, i9300, etc.)
6 =>
CM => [MODEL] (ex. i9100, i9300, etc.)
LINEAGE => [SIGNED] (ex. signed)
)
*/
$tokens = array( 'type' => '', 'version' => '', 'date' => '', 'channel' => '', 'code' => '', 'model' => '', 'signed' => '' );

Expand Down Expand Up @@ -233,27 +233,28 @@ public function parseFilenameFull( $fileName ) {
* @param type $fileName The filename to be parsed
* @return array The tokens of the filename
*/
protected function parseFilenameDeltal($fileName) {
protected function parseFilenameDeltal( $fileName ) {
/*
tokens Schema:
array(
1 => [SOURCE VERSION] (eng.matthi.20200202.195647)
2 => [TARGET VERSION] (eng.matthi.20200305.185431)
)
*/
preg_match_all('/([\w+]+)-([\w+]+)/', $fileName,$tokens);
return $this->removeTrailingDashes(tokens);
preg_match_all( '/([\w+]+)-([\w+]+)/', $fileName, $tokens );
return $this->removeTrailingDashes( tokens );
}

/**
* Remove trailing dashes
* @param string $token The string where to do the operation
* @return string The string without trailing dashes
*/
protected function removeTrailingDashes($token){
foreach ( $token as $key => $value ) {
protected function removeTrailingDashes( $token ) {
foreach( $token as $key => $value ) {
$token[$key] = trim( $value[0], '-' );
}

return $token;
}

Expand All @@ -264,14 +265,16 @@ protected function removeTrailingDashes($token){
* @param string $version The ROM version from filename
* @return string The correct channel to be returned
*/
protected function _getChannel($token, $type, $version){
protected function _getChannel( $token, $type, $version ) {
$ret = 'stable';

$token = strtolower( $token );
if ( $token > '' ) {

if( $token > '' ) {
$ret = $token;
if ( $token == 'experimental' && ( $type == 'cm' || version_compare ( $version, '14.1', '<' ) ) ) $ret = 'snapshot';
if ( $token == 'unofficial' && ( $type == 'cm' || version_compare ( $version, '14.1', '<' ) ) ) $ret = 'nightly';

if( $token == 'experimental' && ( $type == 'cm' || version_compare ( $version, '14.1', '<' ) ) ) $ret = 'snapshot';
if( $token == 'unofficial' && ( $type == 'cm' || version_compare ( $version, '14.1', '<' ) ) ) $ret = 'nightly';
}

return $ret;
Expand All @@ -282,12 +285,14 @@ protected function _getChannel($token, $type, $version){
* @param string $fileName The name of the file
* @return string The absolute URL for the file to be downloaded
*/
protected function _getUrl($fileName = '', $basePath){
protected function _getUrl( $fileName = '', $basePath ) {
$prop = $this->getBuildPropValue( 'ro.build.ota.url' );
if ( !empty($prop) )

if( !empty( $prop ) )
return $prop;

if ( empty($fileName) ) $fileName = $this->filename;
if( empty( $fileName ) ) $fileName = $this->filename;

return $basePath . '/' . $fileName;
}

Expand All @@ -298,14 +303,15 @@ protected function _getUrl($fileName = '', $basePath){
* @param string $fallback The fallback value if not found in build.prop
* @return string The value for the specified key
*/
protected function getBuildPropValue($key, $fallback = null){
protected function getBuildPropValue( $key, $fallback = null ) {
$ret = $fallback ?: null;

if ($this->buildProp) {
foreach ($this->buildProp as $line) {
if ( strpos($line, $key) !== false ) {
$tmp = explode('=', $line);
if( $this->buildProp ) {
foreach( $this->buildProp as $line ) {
if( strpos( $line, $key ) !== false ) {
$tmp = explode( '=', $line );
$ret = $tmp[1];

break;
}
}
Expand Down
Loading

0 comments on commit 4ca4efc

Please sign in to comment.