2017-11-08 17:39:50 +01:00
< ? php if ( count ( get_included_files ()) == 1 ) exit ( " Direct access not permitted. " );
2018-01-27 14:21:34 +01:00
require_once __DIR__ . '/base.php' ;
2017-11-08 17:39:50 +01:00
class Programs
{
2018-01-27 14:21:34 +01:00
const PROG_LANGS = [ 'Java' , 'C#' , 'Delphi' , 'PHP' , 'C++' ];
2018-01-26 21:49:09 +01:00
const URL_ORDER =
[
'github' ,
'sourceforge' ,
'download' ,
'playstore' ,
'amazonappstore' ,
'windowsstore' ,
'itunesstore' ,
'homepage' ,
'wiki' ,
'alternativeto' ,
];
const LICENSES =
[
'MIT' => 'https://choosealicense.com/licenses/mit/' ,
'Unlicense' => 'https://choosealicense.com/licenses/unlicense/' ,
'GPL-3.0' => 'https://choosealicense.com/licenses/gpl-3.0/' ,
'Apache-2.0' => 'https://choosealicense.com/licenses/apache-2.0/' ,
'Mozilla-2.0' => 'https://choosealicense.com/licenses/mpl-2.0/' ,
];
2018-01-03 22:08:56 +01:00
public static function readSingle ( $a )
2017-11-08 17:39:50 +01:00
{
2018-01-27 14:21:34 +01:00
$a [ 'mainimage_url' ] = '/data/images/program_img/' . $a [ 'internal_name' ] . '.png' ;
$a [ 'mainimage_path' ] = __DIR__ . '/../data/images/program_img/' . $a [ 'internal_name' ] . '.png' ;
$a [ 'preview_url' ] = '/data/dynamic/progprev_' . $a [ 'internal_name' ] . '.png' ;
$a [ 'preview_path' ] = __DIR__ . '/../data/dynamic/progprev_' . $a [ 'internal_name' ] . '.png' ;
2018-01-20 03:05:15 +01:00
$a [ 'file_longdescription' ] = ( __DIR__ . '/../statics/programs/' . $a [ 'internal_name' ] . '_description.md' );
2018-01-27 14:21:34 +01:00
2018-01-04 01:11:41 +01:00
$a [ 'url' ] = '/programs/view/' . $a [ 'internal_name' ];
2017-11-08 17:39:50 +01:00
return $a ;
}
public static function listAll ()
{
2018-01-03 22:08:56 +01:00
$all = require ( __DIR__ . '/../statics/programs/__all.php' );
return array_map ( 'self::readSingle' , $all );
2018-01-03 21:02:40 +01:00
}
2018-01-04 16:55:26 +01:00
public static function listAllNewestFirst ( $filter = '' )
2018-01-03 21:02:40 +01:00
{
$data = self :: listAll ();
usort ( $data , function ( $a , $b ) { return strcasecmp ( $b [ 'add_date' ], $a [ 'add_date' ]); });
2018-01-04 16:55:26 +01:00
if ( $filter !== '' ) $data = array_filter ( $data , function ( $a ) use ( $filter ) { return strtolower ( $a [ 'category' ]) === strtolower ( $filter ); });
2018-01-03 21:02:40 +01:00
return $data ;
2017-11-08 17:39:50 +01:00
}
2017-11-20 17:15:12 +01:00
public static function listUpdateData ()
{
$a = require ( __DIR__ . '/../statics/updates/programupdates.php' );
return $a ;
}
2018-01-20 03:05:15 +01:00
public static function getProgramByInternalName ( $id )
{
foreach ( self :: listAll () as $prog ) {
if ( strcasecmp ( $prog [ 'internal_name' ], $id ) === 0 ) return $prog ;
if ( $prog [ 'internal_name_alt' ] !== null && strcasecmp ( $prog [ 'internal_name_alt' ], $id ) === 0 ) return $prog ;
}
return null ;
}
public static function getProgramDescription ( $prog )
{
return file_get_contents ( $prog [ 'file_longdescription' ]);
}
2018-01-26 21:49:09 +01:00
public static function urlComparator ( $a , $b )
{
2018-01-27 14:21:34 +01:00
$na = 0 ;
$nb = 0 ;
if ( strpos ( $a , '#' ) !== FALSE ) { $na = intval ( explode ( '#' , $a )[ 1 ]); $a = explode ( '#' , $a )[ 0 ]; }
if ( strpos ( $b , '#' ) !== FALSE ) { $nb = intval ( explode ( '#' , $b )[ 1 ]); $b = explode ( '#' , $b )[ 0 ]; }
2018-01-26 21:49:09 +01:00
$ia = array_search ( $a , Programs :: URL_ORDER );
$ib = array_search ( $b , Programs :: URL_ORDER );
if ( $ia === false && $ib === false ) return strcasecmp ( $a , $b );
if ( $ia === false && $ib !== false ) return + 1 ; // sort [IA | IB]
if ( $ia !== false && $ib === false ) return - 1 ; // sort [IB | IA]
2018-01-27 14:21:34 +01:00
if ( $ia === $ib ) return ( $na < $nb ) ? - 1 : + 1 ;
2018-01-26 21:49:09 +01:00
return ( $ia < $ib ) ? - 1 : + 1 ;
}
2018-01-27 14:21:34 +01:00
public static function getURLs ( $prog )
2018-01-26 21:49:09 +01:00
{
2018-01-27 14:21:34 +01:00
$urls = $prog [ 'urls' ];
2018-01-26 21:49:09 +01:00
uksort ( $urls , 'self::urlComparator' );
2018-01-27 14:21:34 +01:00
$result = [];
foreach ( $urls as $fulltype => $urldata )
{
$type = $fulltype ;
if ( strpos ( $fulltype , '#' ) !== FALSE ) $type = explode ( '#' , $fulltype )[ 0 ];
$caption = '?' ;
$css = '?' ;
$svg = '?' ;
$direct = false ;
if ( $type === 'download' ) { $caption = 'Download' ; $css = 'prgv_dl_download' ; $svg = 'download' ; }
if ( $type === 'github' ) { $caption = 'Github' ; $css = 'prgv_dl_github' ; $svg = 'github' ; }
if ( $type === 'homepage' ) { $caption = 'Homepage' ; $css = 'prgv_dl_homepage' ; $svg = 'home' ; }
if ( $type === 'wiki' ) { $caption = 'Wiki' ; $css = 'prgv_dl_wiki' ; $svg = 'wiki' ; }
if ( $type === 'playstore' ) { $caption = 'Google Playstore' ; $css = 'prgv_dl_playstore' ; $svg = 'playstore' ; }
if ( $type === 'amazonappstore' ) { $caption = 'Amazon Appstore' ; $css = 'prgv_dl_amznstore' ; $svg = 'amazon' ; }
if ( $type === 'windowsstore' ) { $caption = 'Microsoft Store' ; $css = 'prgv_dl_winstore' ; $svg = 'windows' ; }
if ( $type === 'itunesstore' ) { $caption = 'App Store' ; $css = 'prgv_dl_appstore' ; $svg = 'apple' ; }
if ( $type === 'sourceforge' ) { $caption = 'Sourceforge' ; $css = 'prgv_dl_sourceforge' ; $svg = 'sourceforge' ; }
if ( $type === 'alternativeto' ) { $caption = 'AlternativeTo' ; $css = 'prgv_dl_alternativeto' ; $svg = 'alternativeto' ; }
if ( is_array ( $urldata ))
{
$url = $urldata [ 'url' ];
if ( isset ( $urldata [ 'caption' ])) $caption = $urldata [ 'caption' ];
if ( isset ( $urldata [ 'css' ])) $css = $urldata [ 'css' ];
if ( isset ( $urldata [ 'svg' ])) $svg = $urldata [ 'svg' ];
}
else
{
$url = $urldata ;
}
if ( $url === 'direct' )
{
$direct = true ;
$url = Programs :: getDirectDownloadURL ( $prog );
}
$result [] =
[
'type' => $type ,
'caption' => $caption ,
'svg' => $svg ,
'href' => $url ,
'css' => $css ,
'isdirect' => $direct ,
];
}
return $result ;
2018-01-26 21:49:09 +01:00
}
public static function getLicenseUrl ( $license )
{
return self :: LICENSES [ $license ];
}
2018-01-27 14:21:34 +01:00
public static function getDirectDownloadURL ( $prog )
{
return '/data/binaries/' . $prog [ 'internal_name' ] . '.zip' ;
}
public static function getDirectDownloadPath ( $prog )
{
return ( __DIR__ . '/../data/binaries/' . $prog [ 'internal_name' ] . '.zip' );
}
public static function checkConsistency ()
{
$warn = null ;
$intname = [];
$realname = [];
foreach ( self :: listAll () as $prog )
{
if ( in_array ( $prog [ 'internal_name' ], $intname )) return [ 'result' => 'err' , 'message' => 'Duplicate internal_name ' . $prog [ 'name' ]];
$intname [] = $prog [ 'internal_name' ];
if ( $prog [ 'internal_name_alt' ] !== null )
{
if ( in_array ( $prog [ 'internal_name_alt' ], $intname )) return [ 'result' => 'err' , 'message' => 'Duplicate internal_name ' . $prog [ 'name' ]];
$intname [] = $prog [ 'internal_name_alt' ];
}
if ( in_array ( $prog [ 'name' ], $realname )) return [ 'result' => 'err' , 'message' => 'Duplicate name ' . $prog [ 'name' ]];
$realname [] = $prog [ 'name' ];
if ( strpos ( $prog [ 'internal_name' ], ' ' ) !== FALSE ) return [ 'result' => 'err' , 'message' => 'Internal name contains spaces ' . $prog [ 'name' ]];
foreach ( explode ( '|' , $prog [ 'ui_language' ]) as $lang ) if ( convertLanguageToFlag ( $lang ) === null ) return [ 'result' => 'err' , 'message' => 'Unknown ui-lang ' . $prog [ 'name' ]];;
if ( ! in_array ( $prog [ 'prog_language' ], self :: PROG_LANGS )) return [ 'result' => 'err' , 'message' => 'Unknown prog-lang ' . $prog [ 'name' ]];
if ( strlen ( $prog [ 'short_description' ]) > 128 ) $warn = [ 'result' => 'warn' , 'message' => 'short_description too long ' . $prog [ 'name' ]];
if ( $prog [ 'license' ] !== null && ! array_key_exists ( $prog [ 'license' ], self :: LICENSES )) return [ 'result' => 'err' , 'message' => 'Unknown license ' . $prog [ 'name' ]];
$isdl = false ;
foreach ( self :: getURLs ( $prog ) as $xurl )
{
if ( ! in_array ( $xurl [ 'type' ], self :: URL_ORDER )) return [ 'result' => 'err' , 'message' => 'Unknown url ' . $xurl [ 'type' ]];
if ( $xurl [ 'type' ] === 'download' && $xurl [ 'isdirect' ] && ! file_exists ( self :: getDirectDownloadPath ( $prog ))) return [ 'result' => 'err' , 'message' => 'Direct download not found ' . $prog [ 'name' ]];
if ( $xurl [ 'type' ] === 'download' || $xurl [ 'type' ] === 'playstore' || $xurl [ 'type' ] === 'itunesstore' ) $isdl = true ;
}
if ( ! $isdl ) return [ 'result' => 'err' , 'message' => 'No download link ' . $prog [ 'name' ]];
if ( ! file_exists ( $prog [ 'mainimage_path' ])) return [ 'result' => 'err' , 'message' => 'Image not found ' . $prog [ 'name' ]];
if ( ! file_exists ( $prog [ 'file_longdescription' ])) return [ 'result' => 'err' , 'message' => 'Description not found ' . $prog [ 'name' ]];
}
if ( $warn != null ) return $warn ;
return [ 'result' => 'ok' , 'message' => '' ];
}
public static function checkThumbnails ()
{
foreach ( self :: listAll () as $prog )
{
if ( ! file_exists ( $prog [ 'preview_path' ])) return [ 'result' => 'err' , 'message' => 'Preview not found ' . $prog [ 'name' ]];
}
return [ 'result' => 'ok' , 'message' => '' ];
}
public static function createPreview ( $prog )
{
$src = $prog [ 'mainimage_path' ];
$dst = $prog [ 'preview_path' ];
smart_resize_image ( $src , null , 250 , 0 , true , $dst , 100 );
}
2017-11-08 17:39:50 +01:00
}