| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace XoopsModules\Tdmdownloads\Common; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  You may not change or alter any portion of this comment or credits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  of supporting developers from this source code or any supporting source code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  which is considered copyrighted (c) material of the original comment or credit authors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @copyright   XOOPS Project (https://xoops.org) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @license     http://www.fsf.org/copyleft/gpl.html GNU public license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author      mamba <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | trait VersionChecks | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * Verifies XOOPS version meets minimum requirements for this module | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @static | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @param \XoopsModule|null $module | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param null|string       $requiredVer | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @return bool true if meets requirements, false if not | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public static function checkVerXoops(\XoopsModule $module = null, $requiredVer = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $moduleDirName      = basename(dirname(dirname(__DIR__))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $moduleDirNameUpper = mb_strtoupper($moduleDirName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (null === $module) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $module = \XoopsModule::getByDirname($moduleDirName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         xoops_loadLanguage('admin', $moduleDirName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         //check for minimum XOOPS version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $currentVer = mb_substr(XOOPS_VERSION, 6); // get the numeric part of string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if (null === $requiredVer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $success = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (version_compare($currentVer, $requiredVer, '<')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $success = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_XOOPS'), $requiredVer, $currentVer)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |         return $success; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * Verifies PHP version meets minimum requirements for this module | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @static | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @param \XoopsModule|null $module | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @return bool true if meets requirements, false if not | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public static function checkVerPhp(\XoopsModule $module = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $moduleDirName      = basename(dirname(dirname(__DIR__))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $moduleDirNameUpper = mb_strtoupper($moduleDirName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         xoops_loadLanguage('admin', $moduleDirName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         // check for minimum PHP version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $success = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $verNum = PHP_VERSION; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $reqVer =& $module->getInfo('min_php'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if (false !== $reqVer && '' !== $reqVer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if (version_compare($verNum, $reqVer, '<')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $module->setErrors(sprintf(constant('CO_' . $moduleDirNameUpper . '_ERROR_BAD_PHP'), $reqVer, $verNum)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $success = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |         return $success; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * compares current module version with latest GitHub release | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @static | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @param \Xmf\Module\Helper $helper | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @param string             $location | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * @param string             $default | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @return string link to the latest module version, if newer | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public static function checkVerModule($helper, $location = 'github', $default = 'master') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $moduleDirName = basename(dirname(dirname(__DIR__))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $update        = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $repository    = 'XoopsModules25x/' . $moduleDirName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         //        $repository    = 'XoopsModules25x/publisher'; //for testing only | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $ret = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         if ('github' === $location) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $file              = @json_decode(@file_get_contents("https://api.github.com/repos/$repository/releases", false, stream_context_create(['http' => ['header' => "User-Agent:Publisher\r\n"]]))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $latestVersionLink = sprintf("https://github.com/$repository/archive/%s.zip", $file ? reset($file)->tag_name : $default); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             //            $latestVersion     = substr(strrchr($latestVersionLink, '/'), 1, -4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $latestVersion = $file[0]->tag_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $prerelease    = $file[0]->prerelease; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             if ('master' !== $latestVersionLink) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $update = '<span><strong> Latest Release: </strong>' . '   <a href="' . $latestVersionLink . '">' . $latestVersion . '</a> </span><br><br>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             //"PHP-standardized" version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $latestVersion = mb_strtolower($latestVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             if (false !== mb_strpos($latestVersion, 'final')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 $latestVersion = str_replace('_', '', mb_strtolower($latestVersion)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 $latestVersion = str_replace('final', '', mb_strtolower($latestVersion)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $moduleVersion = ($helper->getModule()->getInfo('version') . '_' . $helper->getModule()->getInfo('module_status')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             //"PHP-standardized" version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $moduleVersion = str_replace(' ', '', mb_strtolower($moduleVersion)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             //          $moduleVersion = '3.0'; //for testing only | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             //          $moduleDirName = 'publisher'; //for testing only | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $ret .= "<div align='center'>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             //            $ret .= "<a href='https://xoops.org/'><img src='../assets/images/icons/32/xoopsmicrobutton.gif'></a><br>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             //            if (version_compare($moduleVersion, $latestVersion, '<')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             if (version_compare($moduleVersion, $latestVersion, '<') && !$prerelease) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 //                $ret .= "| "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 $ret .= " <span style='color: #FF0000; font-size:11px'  "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 $ret .= $update; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 //                $ret .= "<a href='https://github.com/XoopsModules25x/$moduleDirName/releases/'>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 $ret .= '</span>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 $ret .= "<img src='https://img.shields.io/github/release/XoopsModules25x/$moduleDirName.svg?style=flat'></a>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $GLOBALS['xoopsTpl']->assign('latestModRelease', $ret); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  |         return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  |