123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- <?php
- /*
- @nom: Plugin
- @auteur: Valentin CARRUESCO (http://blog.idleman.fr)
- @description: Classe de gestion des plugins au travers de l'application
- */
- class Plugin{
- const FOLDER = '/plugins';
- protected $name,$author,$address,$link,$licence,$path,$description,$version,$state,$type;
- function __construct(){
- }
- public static function includeAll(){
- global $i18n, $i18n_js, $theme;
- $pluginFiles = Plugin::getFiles(true);
- if(is_array($pluginFiles)) {
- foreach($pluginFiles as $pluginFile) {
- // Chargement du fichier de Langue du plugin
- $i18n->append(new Translation(dirname($pluginFile),$i18n->languages));
- // Inclusion du coeur de plugin
- include $pluginFile;
- // Gestion des css du plugin en fonction du thème actif
- $cssTheme = glob(dirname($pluginFile).'/*/'.$theme.'.css');
- $cssDefault = glob(dirname($pluginFile).'/*/default.css');
- if(isset($cssTheme[0])){
- $GLOBALS['hooks']['css_files'][] = Functions::relativePath(str_replace('\\','/',dirname(__FILE__)),str_replace('\\','/',$cssTheme[0]));
- }else if(isset($cssDefault[0])){
- $GLOBALS['hooks']['css_files'][] = Functions::relativePath(str_replace('\\','/',dirname(__FILE__)),str_replace('\\','/',$cssDefault[0]));
- }
- }
- }
- $i18n_js = $i18n->getJson();
- }
- private static function getStates(){
- $stateFile = dirname(__FILE__).Plugin::FOLDER.'/plugins.states.json';
- if(!file_exists($stateFile)) touch($stateFile);
- return json_decode(file_get_contents($stateFile),true);
- }
- private static function setStates($states){
- $stateFile = dirname(__FILE__).Plugin::FOLDER.'/plugins.states.json';
- file_put_contents($stateFile,json_encode($states));
- }
- public static function pruneStates() {
- $statesBefore = self::getStates();
- if(empty($statesBefore))
- $statesBefore = array();
- $statesAfter = array();
- $error = false;
- if (is_array($statesBefore))
- {
- foreach($statesBefore as $file=>$state) {
- if (file_exists($file))
- $statesAfter[$file] = $state;
- else
- $error = true;
- }
- }
- if ($error) self::setStates($statesAfter);
- }
- private static function getObject($pluginFile){
- $plugin = new Plugin();
- $fileLines = file_get_contents($pluginFile);
- if(preg_match_all("#@author\s(.+)\s\<(.*)\>#", $fileLines, $matches)) {
- foreach($matches[1] as $match) {
- $authors[] = trim($match);
- }
- $plugin->setAuthor($authors);
- foreach($matches[2] as $match) {
- $address[] = strtolower($match);
- }
- $plugin->setAddress($address);
- }
- if(preg_match("#@name\s(.+)[\r\n]#", $fileLines, $match))
- $plugin->setName($match[1]);
- if(preg_match("#@licence\s(.+)[\r\n]#", $fileLines, $match))
- $plugin->setLicence($match[1]);
- if(preg_match("#@version\s(.+)[\r\n]#", $fileLines, $match))
- $plugin->setVersion($match[1]);
- if(preg_match("#@link\s(.+)[\r\n]#", $fileLines, $match))
- $plugin->setLink(trim($match[1]));
- if(preg_match("#@type\s(.+)[\r\n]#", $fileLines, $match))
- $plugin->setType(trim($match[1]));
- if(preg_match("#@description\s(.+)[\r\n]#", $fileLines, $match))
- $plugin->setDescription(trim($match[1]));
- if(Plugin::loadState($pluginFile) || $plugin->getType()=='component'){
- $plugin->setState(1);
- }else{
- $plugin->setState(0);
- }
- $plugin->setPath($pluginFile);
- return $plugin;
- }
- public static function getAll(){
- $pluginFiles = Plugin::getFiles();
- $plugins = array();
- if(is_array($pluginFiles)) {
- foreach($pluginFiles as $pluginFile) {
- $plugin = Plugin::getObject($pluginFile);
- $plugins[]=$plugin;
- }
- }
- usort($plugins, "Plugin::sortPlugin");
- return $plugins;
- }
- public static function install($url) {
- $logger = new Logger('settings');
- if(empty($url)){
- $logger->appendLogs("Pas d'url renseignée.");
- }elseif(filter_var($url, FILTER_VALIDATE_URL) === false){
- $logger->appendLogs("L'url renseignée n'est pas valide.");
- }else{
- $logger->appendLogs('Téléchargement du plugin...');
- $pluginBaseFolder = str_replace('/', '', self::FOLDER).'/';
- $tempZipName = $pluginBaseFolder.md5(microtime());
- file_put_contents($tempZipName,self::getUrlContent(urldecode($url)));
- if(file_exists($tempZipName)){
- $logger->appendLogs('Plugin téléchargé <span class="button notice">OK</span>');
- $logger->appendLogs('Extraction du plugin...');
- $zip = new ZipArchive;
- $res = $zip->open($tempZipName);
- if ($res === TRUE) {
- $tempZipFolder = $tempZipName.'_';
- $pluginFolder = $tempZipFolder;
- $zip->extractTo($tempZipFolder);
- $zip->close();
- $logger->appendLogs('Plugin extrait <span class="button notice">OK</span>');
- $fi = new FilesystemIterator($tempZipFolder, FilesystemIterator::SKIP_DOTS);
- if(iterator_count($fi) === 1) {
- foreach($fi as $file){
- $filename = $file->getFilename();
- $pluginFolder = $pluginBaseFolder.$filename;
- rename($tempZipFolder.'/'.$filename, $pluginFolder);
- rmdir($tempZipFolder);
- }
- }
- $pluginName = glob($pluginFolder.'/*.plugin*.php');
- if(count($pluginName)>0){
- $pluginName = str_replace(array($pluginFolder.'/','.enabled','.disabled','.plugin','.php'),'',$pluginName[0]);
- if(!file_exists($pluginBaseFolder.$pluginName)){
- $logger->appendLogs('Renommage...');
- if(rename($pluginFolder,$pluginBaseFolder.$pluginName)){
- $logger->appendLogs('Plugin installé, rechargez la page pour voir le plugin <span class="button notice">pensez à l\'activer</span>');
- }else{
- Functions::rmFullDir($pluginFolder);
- $logger->appendLogs('Impossible de renommer le plugin <span class="button error">Erreur</span>');
- }
- }else{
- $logger->appendLogs('Plugin déjà installé <span class="button warning">OK</span>');
- }
- }else{
- $logger->appendLogs('Plugin invalide, fichier principal manquant <span class="button error">Erreur</span>');
- }
- } else {
- $logger->appendLogs('Echec de l\'extraction <span class="button error">Erreur</span>');
- }
- unlink($tempZipName);
- }else{
- $logger->appendLogs('Echec du téléchargement <span class="button error">Erreur</span>');
- }
- }
- if(Functions::isAjaxCall()){
- echo json_encode($logger->getLogs(), JSON_HEX_QUOT | JSON_HEX_TAG);
- } else {
- $logger->save();
- header('location: ./settings.php#pluginBloc');
- }
- }
- public function getGithubMarketRepos() {
- header('Content-Type: application/json');
- echo json_encode($this->getGithubMarketReposInfos($this->getGithubMarketReposList()));
- }
- protected function getGithubMarketReposList() {
- return json_decode(self::getUrlContent("https://api.github.com/orgs/Leed-market/repos?per_page=1000"));
- }
- protected function getGithubMarketReposInfos($repos) {
- $infos = array();
- $installedPluginsNames = $this->getInstalledPluginsNames();
- foreach($repos as $repo) {
- $repoName = $repo->name;
- if(!in_array(strtolower($repoName), $installedPluginsNames)) {
- $infos[] = array(
- 'name' => $repoName,
- 'description' => isset($repo->description) ? $repo->description : false,
- 'zipUrl' => 'https://github.com/'.$repo->full_name.'/archive/'.$repo->default_branch.'.zip'
- );
- }
- }
- return $infos;
- }
- protected function getInstalledPluginsNames() {
- $names = array();
- $installedPlugins = self::getAll();
- if(!$installedPlugins || empty($installedPlugins)) {
- return $names;
- }
- foreach($installedPlugins as $installedPlugin) {
- $names[] = strtolower($installedPlugin->getName());
- }
- return $names;
- }
- protected static function getUrlContent($url) {
- $timeout = 20;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_REFERER, $url);
- curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
- $datas = curl_exec($ch);
- curl_close($ch);
- return $datas;
- }
- public static function addHook($hookName, $functionName) {
- $GLOBALS['hooks'][$hookName][] = $functionName;
- }
- public static function addCss($css) {
- $bt = debug_backtrace();
- $pathInfo = explode('/',dirname($bt[0]['file']));
- $count = count($pathInfo);
- $name = $pathInfo[$count-1];
- $path = '.'.Plugin::FOLDER.'/'.$name.$css;
- $GLOBALS['hooks']['css_files'][] = $path;
- }
- public static function callCss(){
- $return='';
- if(isset($GLOBALS['hooks']['css_files'])) {
- foreach($GLOBALS['hooks']['css_files'] as $css_file) {
- $return .='<link rel="stylesheet" href="'.$css_file.'">'."\n";
- }
- }
- return $return;
- }
- public static function addLink($rel, $link, $type='', $title='') {
- $GLOBALS['hooks']['head_link'][] = array("rel"=>$rel, "link"=>$link, "type"=>$type, "title"=>$title);
- }
- public static function callLink(){
- $return='';
- if(isset($GLOBALS['hooks']['head_link'])) {
- foreach($GLOBALS['hooks']['head_link'] as $head_link) {
- $return .='<link rel="'.$head_link['rel'].'" href="'.$head_link['link'].'" type="'.$head_link['type'].'" title="'.$head_link['title'].'" />'."\n";
- }
- }
- return $return;
- }
- public static function path(){
- $bt = debug_backtrace();
- $pathInfo = explode('/',dirname($bt[0]['file']));
- $count = count($pathInfo);
- $name = $pathInfo[$count-1];
- return '.'.Plugin::FOLDER.'/'.$name.'/';
- }
- public static function addJs($js) {
- $bt = debug_backtrace();
- $pathInfo = explode('/',dirname($bt[0]['file']));
- $count = count($pathInfo);
- $name = $pathInfo[$count-1];
- $path = '.'.Plugin::FOLDER.'/'.$name.$js;
- $GLOBALS['hooks']['js_files'][] = $path;
- }
- public static function callJs(){
- $return='';
- if(isset($GLOBALS['hooks']['js_files'])) {
- foreach($GLOBALS['hooks']['js_files'] as $js_file) {
- $return .='<script type="text/javascript" src="'.$js_file.'"></script>'."\n";
- }
- }
- return $return;
- }
- public static function callHook($hookName, $hookArguments) {
- //echo '<div style="display:inline;background-color:#CC47CB;padding:3px;border:5px solid #9F1A9E;border-radius:5px;color:#ffffff;font-size:15px;">'.$hookName.'</div>';
- if(isset($GLOBALS['hooks'][$hookName])) {
- foreach($GLOBALS['hooks'][$hookName] as $functionName) {
- call_user_func_array($functionName, $hookArguments);
- }
- }
- }
- public static function getFiles($onlyActivated=false){
- $enabled = $disabled = array();
- $files = glob(dirname(__FILE__). Plugin::FOLDER .'/*/*.plugin*.php');
- if(empty($files))
- $files = array();
- foreach($files as $file){
- $plugin = Plugin::getObject($file);
- if($plugin->getState()){
- $enabled [] = $file;
- }else{
- $disabled [] = $file;
- }
- }
- if(!$onlyActivated)$enabled = array_merge($enabled,$disabled);
- return $enabled;
- }
- public static function loadState($plugin){
- $states = Plugin::getStates();
- return (isset($states[$plugin])?$states[$plugin]:false);
- }
- public static function changeState($plugin,$state){
- $states = Plugin::getStates();
- $states[$plugin] = $state;
- Plugin::setStates($states);
- }
- public static function enabled($pluginUid){
- $plugins = Plugin::getAll();
- foreach($plugins as $plugin){
- if($plugin->getUid()==$pluginUid){
- Plugin::changeState($plugin->getPath(),true);
- $install = dirname($plugin->getPath()).'/install.php';
- if(file_exists($install))require_once($install);
- }
- }
- }
- public static function disabled($pluginUid){
- $plugins = Plugin::getAll();
- foreach($plugins as $plugin){
- if($plugin->getUid()==$pluginUid){
- Plugin::changeState($plugin->getPath(),false);
- $uninstall = dirname($plugin->getPath()).'/uninstall.php';
- if(file_exists($uninstall))require_once($uninstall);
- }
- }
- }
- function getUid(){
- $pathInfo = explode('/',$this->getPath());
- $count = count($pathInfo);
- $name = $pathInfo[$count-1];
- return $pathInfo[$count -2].'-'.substr($name,0,strpos($name,'.'));
- }
- static function sortPlugin($a, $b){
- if ($a->getState() == $b->getState())
- if ($a->getName() == $b->getName())
- return 0;
- else
- return $a->getName() < $b->getName() ? -1 : 1;
- else
- return $a->getState() < $b->getState() ? -1 : 1;
- }
- function getName(){
- return $this->name;
- }
- function setName($name){
- $this->name = $name;
- }
- function setAuthor($author){
- $this->author = $author;
- }
- function getAuthor(){
- return $this->author;
- }
- function getAddress(){
- return $this->address;
- }
- function setAddress($address){
- $this->address = $address;
- }
- function getLicence(){
- return $this->licence;
- }
- function setLicence($licence){
- $this->licence = $licence;
- }
- function getPath(){
- return $this->path;
- }
- function setPath($path){
- $this->path = $path;
- }
- function getDescription(){
- return $this->description;
- }
- function setDescription($description){
- $this->description = $description;
- }
- function getLink(){
- return $this->link;
- }
- function setLink($link){
- $this->link = $link;
- }
- function getVersion(){
- return $this->version;
- }
- function setVersion($version){
- $this->version = $version;
- }
- function getState(){
- return $this->state;
- }
- function setState($state){
- $this->state = $state;
- }
- function getType(){
- return $this->type;
- }
- function setType($type){
- $this->type = $type;
- }
- }
- ?>
|