Skip to content

Commit

Permalink
Merge pull request #1 from axelhahn/update-cdnjs-api
Browse files Browse the repository at this point in the history
v1.0.9 : update for changes in cdnjs api
see cdnjs/cdnjs#14140
  • Loading branch information
axelhahn authored Apr 28, 2023
2 parents c0c05a4 + f1a312f commit 530bbb7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 57 deletions.
95 changes: 57 additions & 38 deletions admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@
$aIcons=array(

'nav-search'=>'fa fa-globe',
'nav-browse'=>'fa fa-folder-open-o',
'nav-browse'=>'fa fa-folder-open',

'home'=>'fa fa-home',
'search'=>'fa fa-search',
'download'=>'fa fa-download',
'info'=>'fa fa-info-circle',
'author'=>'fa fa-user',
'license'=>'fa fa-file-o',
'license'=>'fa fa-file',
'keyword'=>'fa fa-tag',
'keywords'=>'fa fa-tags',
'library'=>'fa fa-suitcase',
'marked'=>'fa fa-star',
'version'=>'fa fa-flag',
'files'=>'fa fa-file',
'usage'=>'fa fa-hand-o-right',
'usage'=>'fa-solid fa-hand-point-right',

'go'=>'fa fa-check',
'refresh'=>'fa fa-refresh',
Expand Down Expand Up @@ -144,8 +144,16 @@ function renderLocalLibs($bSidebar=false){
if(!$aLocalLibs || !count($aLocalLibs)){
return $bSidebar
? ''
: '<p>'
. '<span class="info">'.getIcon('info').'INFO: No downloads so far.</span>'
: '<h2>Welcome!</h2>'
.'<p>'
.'Here you will see all downloaded packages and their versions.<br>'
.'You get an information if a library has an update.<br>'
.'You can download newer versions or delete any library.<br>'
.'<br>'
. '<span class="info">'.getIcon('info').'INFO: No downloads so far.</span><br>'
.'<br>'
.'Go to the tab "API search" to find a library and download it.<br>'
.'<br>'
. '<br><br><a href="'.$sUrlRefresh.'" class="button" title="re-read local directories to scan downloaded libs">'.getIcon('refresh').'Refresh</a>'
. '</p>';
}
Expand Down Expand Up @@ -200,7 +208,7 @@ function renderLocalLibs($bSidebar=false){
. ($sLatestVersion && $sLatestVersion===$sLibversion ? '<span class="ok">'.getIcon('ok').'Up to date</span>' : '')
. ($sLatestVersion && version_compare($sLatestVersion, $sLibversion, '>') ? '<span class="warning">'.getIcon('warning').'outdated</span>' : '')
.'</td>'
. '<td>'.($sLatestVersion===$sLibversion ? '' : getIcon('version').$sLatestVersion).'</td>'
. '<td>'.($sLatestVersion===$sLibversion ? '' : '<a href="?module=search&action=detail&library='.$sMyLibrary.'&version='.$sLibversion.'&q='.$sLibrarySearch.'" title="Show details for latest version '.$sLatestVersion.' of &quot;'.$sMyLibrary.'&quot;">'.getIcon('version').$sLatestVersion.'</a>').'</td>'
. '<td>'.$sDateOfDownload.'</td>'
. '<td>'.$sBtnDelete.'</a> </td>'
: ''
Expand Down Expand Up @@ -278,6 +286,14 @@ function showError($sMessage){
. 'If you feel completely helpless ... here is the safe way <a href="?" class="button">'.getIcon('home').'home</a> ...';
}

function showTableRow($sCol1, $sCol2, $sError=false){
return '<tr>
'.($sError
? '<td colspan="2"><span class="warning">'.getIcon('warning').'Warning: '.$sError.'</span></td>'
: '<td>'.$sCol1.'</td><td>'.$sCol2.'</td>'
).'
</tr>';
}

// ----------------------------------------------------------------------
// handle actions
Expand Down Expand Up @@ -375,9 +391,7 @@ function showError($sMessage){
if (!$sVersion) {
$sVersion = $oCdn->getLibraryLatestVersion($sLibrary);
} else {
$sOut .= '(version ' . $sVersion
. ($sVersion!=$oCdn->getLibraryLatestVersion($sLibrary) ? ' not current' : ' latest')
. ')';
$sOut .= '(version ' . $sVersion. ')';
}
$aFiles = $oCdn->getLibraryAssets($sLibrary, $sVersion);
if(!$aFiles || !count($aFiles)){
Expand All @@ -392,16 +406,17 @@ function showError($sMessage){
$sFirstFile=$sLibrary . '/' . $sVersion . '/' . ($oCdn->getLibraryFilename($sLibrary) ? $oCdn->getLibraryFilename($sLibrary) : $aFiles[0]);

$sDownload = ($oCdn->getLocalfile($sFirstFile))
? '<span class="ok">'.getIcon('ok').'Library <strong>'.$sLibrary.' v'.$sVersion.'</strong> was downloaded already.</span><br>see ' . $oCdn->sVendorDir
? '<span class="ok">'.getIcon('ok').'Library <strong>'.$sLibrary.' v'.$sVersion.'</strong> was downloaded already.</span><br>see ' . $oCdn->sVendorDir.'<br>'
. ($sVersion!=$oCdn->getLibraryLatestVersion($sLibrary) ? '<br><span class="warning">'.getIcon('warning').'Update available. Version '.$oCdn->getLibraryLatestVersion($sLibrary).' is the latest.</span>' : '')
: ''
. 'Here you can download all files (they are listed below) from CDNJS to your local vendor directory ('.$oCdn->sVendorDir.').<br><br>'
.'<a href="?module=search&action=download&library=' . $sLibrary . '&version=' . $sVersion . '&q='.$sLibrarySearch.'" class="button download" title="Start download">'.getIcon('download').'Download <strong>'.$sLibrary.'</strong> v'.$sVersion.'</a><br><br>'
. ($oCdn->getLibraryLatestVersion($sLibrary) !== $sVersion ? ' <span class="warning">'.getIcon('warning').'Warning: This is not the latest version of this library!</span><br>' : '')
. ($sVersion!=$oCdn->getLibraryLatestVersion($sLibrary) ? '<br><span class="warning">'.getIcon('warning').'Update available. Version '.$oCdn->getLibraryLatestVersion($sLibrary).' is the latest.</span>' : '')
. (preg_match('/rc/i', $sVersion) ? ' <span class="warning">'.getIcon('warning').'Warning: This version is a RELEASE CANDIDATE - not a final version.</span><br>' : '')
. (preg_match('/beta/i', $sVersion) ? ' <span class="warning">'.getIcon('warning').'Warning: This version is a BETA release - not a final version.</span><br>' : '')
. (preg_match('/alpha/i', $sVersion) ? ' <span class="warning">'.getIcon('warning').'Warning: This version is an ALPHA release - not a final version.</span><br>' : '')
. (preg_match('/^0\./', $sVersion) ? ' <span class="warning">'.getIcon('warning').'Warning: This version is a 0.x release - not a final version.</span><br>' : '')
. (count($aFiles)>100 ? ' <span class="warning">'.getIcon('warning').'Warning: Many ('.count($aFiles).') files detected. Maybe you need to wait a longer time. On Timeout error: just reload the page to continue downloading still missing files.</span><br>' : '')
. (count($aFiles)>200 ? ' <span class="warning">'.getIcon('warning').'Warning: Many ('.count($aFiles).') files detected. Maybe you need to wait a longer time. On Timeout error: just reload the page to continue downloading still missing files.</span><br>' : '')
. ($oCdn->getLocalfile($sLibrary . '/' . $sVersion.'_in_progress' ) ? ' <span class="warning">'.getIcon('warning').'Warning: An incomplete download was detected. Clicking on download fetches still missing files.</span><br>' : '')
;

Expand Down Expand Up @@ -449,30 +464,31 @@ function showError($sMessage){
)
.'<br><br>'

. ($oCdn->getLibraryHomepage($sLibrary)
? getIcon('home').'Homepage: <a href="' . $oCdn->getLibraryHomepage($sLibrary) . '" target="_blank">' . $oCdn->getLibraryHomepage($sLibrary) . '</a>'
: '<span class="warning">'.getIcon('warning').'Warning: This project has no homepage</span><br>'
)
.'<br>'

. ($oCdn->getLibraryAuthor($sLibrary)
? getIcon('author').'Author: ' . $oCdn->getLibraryAuthor($sLibrary)
: '<span class="warning">'.getIcon('warning').'Warning: The author was not detected</span><br>'
)
.'<br>'

. ($oCdn->getLibraryLicenses($sLibrary)
? getIcon('license').'License(s): ' . implode(' | ', $oCdn->getLibraryLicenses($sLibrary))
: '<span class="warning">'.getIcon('warning').'Warning: The license was not detected</span><br>'
)
.'<br>'
.'<table>'
. showTableRow(
getIcon('home').'Homepage:',
'<a href="' . $oCdn->getLibraryHomepage($sLibrary) . '" target="_blank">' . $oCdn->getLibraryHomepage($sLibrary) . '</a>',
!$oCdn->getLibraryHomepage($sLibrary) ? 'This project has no homepage' : ''
)
. showTableRow(
getIcon('author').'Author:',
$oCdn->getLibraryAuthor($sLibrary),
!$oCdn->getLibraryAuthor($sLibrary) ? 'The author was not detected' : ''
)
. showTableRow(
getIcon('license').'License(s):',
implode(' | ', $oCdn->getLibraryLicenses($sLibrary)),
!$oCdn->getLibraryLicenses($sLibrary) ? 'The license was not detected' : ''
)

. showTableRow(
getIcon('version').'Latest version:',
$oCdn->getLibraryLatestVersion($sLibrary),
!$oCdn->getLibraryLatestVersion($sLibrary) ? 'No version was detected' : ''
)
.'</table>'


. ($oCdn->getLibraryLatestVersion($sLibrary)
? getIcon('version').'Latest version: ' . $oCdn->getLibraryLatestVersion($sLibrary) . '<br>'
: '<span class="warning">'.getIcon('warning').'Warning: No version was detected</span><br>'
)

. '<br><a href="https://cdnjs.com/libraries/'.$sLibrary.'" target="_blank">'.getIcon('linkextern'). ' cdnjs.com: '.$sLibrary.'</a><br>'
. '</p>'
. $sShowVersions
Expand Down Expand Up @@ -536,7 +552,8 @@ function showError($sMessage){
color: #9bd; font-family: verdana,"arial"; margin: 0;}
a{color:#5ce;}
a:hover{color:#aff;}
h1{background:rgba(0,0,0,0.2); border-radius: 0.5em 0.5em 0 0 ; color:#a9f; text-shadow: 1px 1px 2px #000, 0 0 0.8em #abc; font-size: 250%; margin: 0; padding: 0.5em;}
h1{background:rgba(0,0,0,0.2); border-radius: 0.3em 0.3em 0 0 ; color:#a9f; text-shadow: 1px 1px 2px #000, 0 0 0.8em #abc; font-size: 250%; margin: 0; padding: 0.5em;}
h1 span{font-size: 50%;}
h2{color:#ae5; font-size: 240%;text-shadow: 1px 1px 0 #000, 0 0 0.7em #000;}
h3{color:#a6b; font-size: 220%; margin: 1.5em 0 0 0;text-shadow: 1px 1px 0 #000;}
li{padding: 0.5em; transition: ease-in-out 0.2s;}
Expand Down Expand Up @@ -575,20 +592,22 @@ function showError($sMessage){
</style>
<?php
// echo $oCdn->getHtmlInclude('jquery/jquery.min.js');
echo $oCdn->getHtmlInclude('font-awesome/4.7.0/css/font-awesome.min.css');
// echo $oCdn->getHtmlInclude('font-awesome/4.7.0/css/font-awesome.min.css');
echo $oCdn->getHtmlInclude('font-awesome/6.4.0/css/all.min.css');

?>
</head>
<body>
<div id="main">
<h1>CDN OR LOCAL :: admin</h1>
<h1>CDN OR LOCAL :: admin <span>v<?php echo $oCdn->getVersion() ?></span></h1>

<?php
echo renderNavi().$sOut;
?>
<div style="clear: both;"></div>
</div>
<div id="footer">
&copy; 2017 - <?php echo date('Y')?> <a href="https://www.axel-hahn.de/" target="_blank" title="Website of the author (German)"><?php echo getIcon('linkextern')?>Axel Hahn</a>
&copy; 2017 - <?php echo date('Y')?> .. <a href="https://www.axel-hahn.de/" target="_blank" title="Website of the author (German)"><?php echo getIcon('linkextern')?>Axel Hahn</a>
.. <a href="https://www.axel-hahn.de/docs/cdnorlocal/index.htm" target="_blank" title="Docs"><?php echo getIcon('linkextern')?>Docs</a>
.. <a href="https://github.com/axelhahn/cdnorlocal" target="_blank" title="Project page on Github"><?php echo getIcon('linkextern')?>Github</a>
| <a href="https://cdnjs.com/" target="_blank" title="CDN hoster cdnjs"><?php echo getIcon('linkextern')?>cdnjs.com</a>
Expand Down
23 changes: 11 additions & 12 deletions classes/cdnorlocal-admin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* admin functions to request API, download, read existing local downloads
* This file is needed by admin/index.php only - NOT in your projects to publish
*
* @version 1.0.8
* @version 1.0.9
* @author Axel Hahn
* @link https://www.axel-hahn.de
* @license GPL
Expand Down Expand Up @@ -119,6 +119,7 @@ public function getLibraryMetadata($sLibrary, $bRefresh=false) {
$this->_wd(__METHOD__ . "($sLibrary) no _cdn");
return false;
}
// echo '<pre>'; print_r($this->aLibs[$sLibrary]); die();
return true;
}

Expand All @@ -136,15 +137,13 @@ public function getLibraryAssets($sLibrary, $sVersion=false) {
$sVersion=$this->getLibraryLatestVersion($sLibrary);
}
$this->_wd(__METHOD__ . ' version: '. $sVersion);
if ($this->aLibs[$sLibrary]['_cdn']){

foreach ($this->aLibs[$sLibrary]['_cdn']->assets as $aAsset){
if($aAsset->version === $sVersion){
return $aAsset->files;
}
}
}
return false;

$sApiUrl=sprintf($this->sUrlApiPackage, "$sLibrary/$sVersion/");
$this->_wd(__METHOD__ . "($sLibrary) fetch $sApiUrl");
$aJson=json_decode(file_get_contents($sApiUrl), 1);
// echo '<pre>'.print_r($aJson, 1).'</pre>';

return $aJson['files'];
}

/**
Expand Down Expand Up @@ -252,10 +251,10 @@ public function getLibraryVersions($sLibrary, $iMax=false) {
$aReturn=array();
if ($this->aLibs[$sLibrary]['_cdn']){
$iCount=0;
foreach ($this->aLibs[$sLibrary]['_cdn']->assets as $aAsset){
foreach ($this->aLibs[$sLibrary]['_cdn']->versions as $sVersion){
$iCount++;
if (!$iMax || $iCount<=$iMax){
$aReturn[]=$aAsset->version;
array_unshift($aReturn , $sVersion);
}
}
}
Expand Down
33 changes: 26 additions & 7 deletions classes/cdnorlocal.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@
* AND/ OR
* https://unpkg.com/
*
* @version 1.0.8
* @version 1.0.9
* @author Axel Hahn
* @link https://www.axel-hahn.de
* @license GPL
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL 3.0
* @package cdnorlocal
*/
class cdnorlocal {


protected $sVersion='1.0.9';

/**
* flag to show debugging infos (used in _wd method [write debug])
* @var boolean
Expand All @@ -54,7 +56,12 @@ class cdnorlocal {
* url prefix of CDNJS
* @var string
*/
var $sCdnUrl='https://cdnjs.cloudflare.com/ajax/libs';
// var $sCdnUrl='https://cdnjs.cloudflare.com/ajax/libs';

/**
* url prefix of CDNs
* @var string
*/
var $aCdnUrls=array(
'cdnjs.cloudflare.com'=>array(
'about'=>'',
Expand Down Expand Up @@ -321,7 +328,7 @@ public function getLibs($bDetectUnused=false){
$this->_wd(__METHOD__ . "()");
$aReturn=$this->_aLibs;
if($bDetectUnused){
foreach(glob($this->sVendorDir.'/*') as $sDir){
foreach(glob($this->sVendorDir.'/*') as $sDir){
$sMyLib=basename($sDir);
foreach(glob($this->sVendorDir.'/'.$sMyLib.'/*') as $sVersiondir){
$sMyVersion=basename($sVersiondir);
Expand Down Expand Up @@ -373,6 +380,10 @@ public function getLibVersion($sLib){
public function getLibRelpath($sLib){
return $this->_getLibItem('lib', $sLib, 'relpath');
}

public function getVersion(){
return $this->sVersion;
}
/**
* set an array of lib items to the lib
* @param array $aLibs array of relpath (i.e. "jquery/3.2.1")
Expand Down Expand Up @@ -459,14 +470,22 @@ public function getFullUrl($sRelUrl){
* @param string $sRelUrl relative url of css/ js file (i.e. "jquery/3.2.1/jquery.min.js")
* @return string
*/
function getHtmlInclude($sRelUrl){
function getHtmlInclude($sRelUrl, $sCecksum=''){
$sUrl=$this->getFullUrl($sRelUrl);
$ext = pathinfo($sRelUrl, PATHINFO_EXTENSION);

$sSecurity=($sCecksum ? 'integrity="'.$sCecksum.'" ' : '')
.'crossorigin="anonymous" referrerpolicy="no-referrer"'
;
switch ($ext){
case 'css':
return '<link rel="stylesheet" type="text/css" href="'.$sUrl.'">';
return '<link rel="stylesheet" type="text/css" href="'.$sUrl.'" '
. $sSecurity
.' />';
case 'js':
return '<script src="'.$sUrl.'"></script>';
return '<script src="'.$sUrl.'" '
. $sSecurity
.'></script>';
default:
return "<!-- ERROR: I don't know (yet) how to handle extension [$ext] ... to include $sRelUrl; You can use getFullUrl('$sRelUrl'); -->";
}
Expand Down

0 comments on commit 530bbb7

Please sign in to comment.