Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8f6b21e5 authored by Arnau Vàzquez's avatar Arnau Vàzquez
Browse files

Merge branch 'api-improvements' into 'master'

Api improvements

See merge request e/os/LineageOTA!13
parents 5aaedeb4 f30d8496
Loading
Loading
Loading
Loading
+139 −115
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@

    use \Flight;

    class Build {

    class Build
    {
        private $apiLevel = -1;
        private $channel = '';
        private $model = '';
@@ -49,7 +49,8 @@
         * @param type $fileName The current filename of the build
         * @param type $physicalPath The current path where the build lives
         */
    	public function __construct($fileName, $physicalPath, $logger) {
        public function __construct($fileName, $physicalPath, $logger)
        {
            $this->logger = $logger;

            /*
@@ -81,51 +82,40 @@
            // - builds/CURRENT_ZIP_FILE.zip.prop ( which must exist )
            $this->buildProp = explode("\n", @file_get_contents($this->getPropFilePath()));
            // Try to fetch build.prop values. In some cases, we can provide a fallback, in other a null value will be given
            $prop = "";
            $this->timestamp = intval($this->getBuildPropValue('ro.build.date.utc') ?? filemtime($this->filePath));
            $prop.='ro.build.date.utc='. $this->timestamp . "\n";
            $this->incremental = $this->getBuildPropValue('ro.build.version.incremental') ?? '';
            $prop.='ro.build.version.incremental='. $this->incremental . "\n";
            $this->apiLevel = $this->getBuildPropValue('ro.build.version.sdk') ?? '';
            $this->model = $this->getBuildPropValue('ro.lineage.device') ?? $this->getBuildPropValue('ro.cm.device') ?? ($tokens[1] == 'cm' ? $tokens[6] : $tokens[5]);
            $this->version = $tokens[2];
            $this->displayVersion = $this->getBuildPropValue('ro.cm.display.version') ?? $this->getBuildPropValue('ro.lineage.display.version') ?? '';
            $this->androidVersion = $this->getBuildPropValue('ro.build.version.release') ?? '';
            $this->uid = hash('sha256', $this->timestamp.$this->model.$this->apiLevel, false);
            $this->size = filesize($this->filePath);
            $position = strrpos($physicalPath, '/builds/full');
            if ($position === false) {
                $this->url = $this->_getUrl('', Flight::cfg()->get('buildsPath'));
            } else {
                $this->url = $this->_getUrl('', Flight::cfg()->get('basePath') . substr($physicalPath, $position));
            }
            $this->changelogUrl = $this->_getChangelogUrl();
            if (!file_exists($this->filePath . '.prop')) {
                $prop = "";
                $prop.= 'ro.build.date.utc='.$this->timestamp."\n";
                $prop.='ro.build.version.incremental='.$this->incremental."\n";
                $prop.='ro.build.version.sdk='.$this->apiLevel."\n";
            if ($this->getBuildPropValue( 'ro.lineage.device' )){
                $this->model = $this->getBuildPropValue( 'ro.lineage.device' );
                if (!empty($this->getBuildPropValue('ro.lineage.device'))) {
                    $prop.='ro.lineage.device='.$this->model."\n";
            }
            else if( $this->getBuildPropValue( 'ro.cm.device' )) {
                $this->model = $this->getBuildPropValue( 'ro.cm.device' );
                } elseif (!empty($this->getBuildPropValue('ro.cm.device'))) {
                    $prop.='ro.cm.device='.$this->model."\n";
                }
            else if   ( $tokens[1] == 'cm') {
                $this->model = $tokens[6];
            } else {
                $this->model = $tokens[5];
            }
            $this->version = $tokens[2];
            if( $this->getBuildPropValue( 'ro.cm.display.version' )){
                $this->displayVersion = $this->getBuildPropValue( 'ro.cm.display.version' );
                if (!empty($this->getBuildPropValue('ro.cm.display.version'))) {
                    $prop.='ro.cm.display.version='.$this->displayVersion."\n";
            }
             else if( $this->getBuildPropValue( 'ro.lineage.display.version' )){
                $this->displayVersion = $this->getBuildPropValue( 'ro.lineage.display.version' );
                } elseif (!empty($this->getBuildPropValue('ro.lineage.display.version'))) {
                    $prop.='ro.lineage.display.version='.$this->displayVersion."\n";
             } else {
                $this->displayVersion= '';
                }
            $this->androidVersion = $this->getBuildPropValue( 'ro.build.version.release' ) ?? '';
                $prop.='ro.build.version.release='.$this->androidVersion."\n";
            if(!file_exists($this->filePath.'.prop')) {
                file_put_contents($this->filePath . '.prop', $prop);
            }
            $this->uid = hash( 'sha256', $this->timestamp.$this->model.$this->apiLevel, false );
            $this->size = filesize($this->filePath);
            $position = strrpos( $physicalPath, '/builds/full' );
            if ( $position === FALSE )
                $this->url = $this->_getUrl( '', Flight::cfg()->get('buildsPath') );
            else
                $this->url = $this->_getUrl( '', Flight::cfg()->get('basePath') . substr( $physicalPath, $position ) );

            $this->changelogUrl = $this->_getChangelogUrl();
        }

        /**
@@ -133,15 +123,20 @@
         * @param type $params The params dictionary inside the current POST request
         * @return boolean True if valid, False if not.
         */
    	public function isValid($params){
            if ( $params === NULL ) return true;  // Assume valid if no parameters
        public function isValid($params)
        {
            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 (strtolower($channel) == $this->channel) {
                            $ret = true;
                        }
                    }
                }
            }
@@ -154,13 +149,14 @@
         * @param type $targetToken The target build from where to build the Delta
         * @return array/boolean Return an array performatted with the correct data inside, otherwise false if not possible to be created
         */
        public function getDelta($targetToken){
        public function getDelta($targetToken)
        {
            $ret = false;

            $deltaFile = $this->incremental . '-' . $targetToken->incremental . '.zip';
            $deltaFilePath = Flight::cfg()->get('realBasePath') . '/builds/delta/' . $deltaFile;

            if ( file_exists( $deltaFilePath ) )
            if (file_exists($deltaFilePath)) {
                $ret = array(
                'filename' => $deltaFile,
                'timestamp' => filemtime($deltaFilePath),
@@ -169,6 +165,7 @@
                'api_level' => $this->apiLevel,
                'incremental' => $targetToken->incremental
              );
            }

            return $ret;
        }
@@ -178,16 +175,18 @@
         * @param string $path The path of the file
         * @return string The MD5 hash
         */
        public function getMD5($path = ''){
        public function getMD5($path = '')
        {
            $ret = '';

            if ( empty($path) ) $path = $this->filePath;
            if (empty($path)) {
                $path = $this->filePath;
            }
            // Pretty much faster if it is available
            if (file_exists($path . ".md5sum")) {
                $tmp = explode("  ", file_get_contents($path . '.md5sum'));
                $ret = $tmp[0];
            }
            elseif ( $this->commandExists( 'md5sum' ) ) {
            } elseif ($this->commandExists('md5sum')) {
                $tmp = explode("  ", exec('md5sum ' . $path));
                $ret = $tmp[0];
            } else {
@@ -203,7 +202,8 @@
         * Get filesize of the current build
         * @return string filesize in bytes
         */
        public function getSize() {
        public function getSize()
        {
            return $this->size;
        }

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

@@ -219,7 +220,8 @@
         * Get the Incremental value of the current build
         * @return string The incremental value
         */
        public function getIncremental() {
        public function getIncremental()
        {
            return $this->incremental;
        }

@@ -227,7 +229,8 @@
         * Get the API Level of the current build.
         * @return string The API Level value
         */
        public function getApiLevel() {
        public function getApiLevel()
        {
            return $this->apiLevel;
        }

@@ -235,7 +238,8 @@
         * Get the Url of the current build
         * @return string The Url value
         */
        public function getUrl() {
        public function getUrl()
        {
            return $this->url;
        }

@@ -243,7 +247,8 @@
         * Get the timestamp of the current build
         * @return string The timestamp value
         */
        public function getTimestamp() {
        public function getTimestamp()
        {
            return $this->timestamp;
        }

@@ -251,7 +256,8 @@
         * Get the changelog Url of the current build
         * @return string The changelog Url value
         */
        public function getChangelogUrl() {
        public function getChangelogUrl()
        {
            return $this->changelogUrl;
        }

@@ -259,7 +265,8 @@
         * Get the channel of the current build
         * @return string The channel value
         */
        public function getChannel() {
        public function getChannel()
        {
            return $this->channel;
        }

@@ -267,7 +274,8 @@
         * Get the filename of the current build
         * @return string The filename value
         */
        public function getFilename() {
        public function getFilename()
        {
            return $this->filename;
        }

@@ -275,7 +283,8 @@
         * Get the version of the current build
         * @return string the version value
         */
        public function getVersion() {
        public function getVersion()
        {
            return $this->version;
        }

@@ -283,7 +292,8 @@
         * Get the version of the current build
         * @return string the version value
         */
        public function getDisplayVersion() {
        public function getDisplayVersion()
        {
            return $this->displayVersion;
        }

@@ -291,7 +301,8 @@
         * Get the Android version of the current build
         * @return string the Android version value
         */
        public function getAndroidVersion() {
        public function getAndroidVersion()
        {
            return $this->androidVersion;
        }

@@ -301,7 +312,8 @@
         * Return the correct prop file path (depending of version)
         * @return boolean
         */
        private function getPropFilePath() {
        private function getPropFilePath()
        {
            return file_exists($this->filePath.'.prop') ?
            $this->filePath.'.prop'
            : 'zip://'.$this->filePath.'#system/build.prop';
@@ -313,7 +325,8 @@
         * @param type $token The string where to do the operation
         * @return string The string without trailing dashes
         */
        private function removeTrailingDashes($token){
        private function removeTrailingDashes($token)
        {
            foreach ($token as $key => $value) {
                $token[$key] = trim($value[0], '-');
            }
@@ -327,7 +340,8 @@
         * @param string $version The ROM version from filename
         * @return string The correct channel to be returned
         */
        private function _getChannel($token, $type, $version){
        private function _getChannel($token, $type, $version)
        {
            $ret = 'stable';

            $token = strtolower($token);
@@ -343,12 +357,16 @@
         * @param string $fileName The name of the file
         * @return string The absolute URL for the file to be downloaded
         */
        private function _getUrl($fileName = '', $basePath){
        private 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;
        }

@@ -356,13 +374,15 @@
         * Get the changelog URL for the current build
         * @return string The changelog URL
         */
        private function _getChangelogUrl(){
            if ( file_exists( str_replace('.zip', '.txt', $this->filePath) ) )
        private function _getChangelogUrl()
        {
            if (file_exists(str_replace('.zip', '.txt', $this->filePath))) {
                $ret = str_replace('.zip', '.txt', $this->url);
            elseif ( file_exists( str_replace('.zip', '.html', $this->filePath) ) )
            } elseif (file_exists(str_replace('.zip', '.html', $this->filePath))) {
                $ret = str_replace('.zip', '.html', $this->url);
            else
            } else {
                $ret = '';
            }

            return $ret;
        }
@@ -374,7 +394,8 @@
         * @param string $fallback The fallback value if not found in build.prop
         * @return string The value for the specified key
         */
        private function getBuildPropValue($key, $fallback = null){
        private function getBuildPropValue($key, $fallback = null)
        {
            $ret = $fallback ?: null;

            if ($this->buildProp) {
@@ -395,9 +416,11 @@
         * @param string $cmd The current command to execute
         * @return boolean Return True if available, False if not
         */
        private function commandExists($cmd){
            if (!$this->functionEnabled('shell_exec'))
        private function commandExists($cmd)
        {
            if (!$this->functionEnabled('shell_exec')) {
                return false;
            }

            $returnVal = shell_exec("which $cmd");
            return (empty($returnVal) ? false : true);
@@ -408,7 +431,8 @@
         * @param string $func The function to check for
         * @return boolean true if the function is enabled, false if not
         */
        private function functionEnabled($func) {
        private function functionEnabled($func)
        {
            return is_callable($func) && false === stripos(ini_get('disable_functions'), $func);
        }
    }
+1 −1
Original line number Diff line number Diff line
@@ -189,7 +189,7 @@

                            if ( $build->isValid( $this->postData['params'] ) ) {
                                array_push( $this->builds , $build );
                                if (strcmp($this->postData['params']['source_incremental'], $build->getIncremental()) == 0) {
                                if (!empty($this->postData['params']) && strcmp($this->postData['params']['source_incremental'], $build->getIncremental()) == 0) {
                                    $this->currentBuild = $build;
                                    $this->logger->info($build->getIncremental().' is the current build');
                                  }