color = new Colors(); } /** * Control parameters. * Return true if PLUGIN:GET is given and no other option or force option. * * @see FlowStackInterface::control() */ public function control( $argArray ) { if(isset($argArray[1]) && strtoupper($argArray[1]) == "PLUGIN:GET"){ if(isset($argArray[2])){ $this->pluginName = $argArray[2]; }else{ return false; } if(isset($argArray[3]) && (strtoupper($argArray[3]) == "-F" || strtoupper($argArray[3]) == "--FORCE")){ $this->force = true; }else if(!isset($argArray[3])){ $this->force = false; }else{ return false; } return true; }else{ return false; } } /** * @see FlowStackInterface::getControlMethod() */ public function getControlMethod() { return "installPlugin"; } /** * Download a plugin. * Download a text formated plugin. * * @return boolean|string */ public function downloadPlugin() { echo $this->color->getColoredString("Start download\n", "green"); $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, self::DOWNLOAD_DIRECTORY.$this->pluginName); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $data = curl_exec($ch); $status = curl_getinfo($ch)["http_code"]; curl_close($ch); if($status != 200){ echo $this->color->getColoredString("Undefined plugin ".$this->pluginName, "red"); $data = false; } return $data; } /** * Install a plugin. * Download and write plugin. If force option is defined, an existing plugin * with same name will be overwrite. */ public function installPlugin() { if (($data = $this->downloadPlugin()) !== false) { echo $this->color->getColoredString("Download ended\n", "green"); if(is_file(realpath(__DIR__."/../pluginStuff")."/".$this->pluginName.".php") && !$this->force){ echo "\n"; echo $this->color->getColoredString("A plugin with this name already exist", "red"); echo "\n"; }else{ if(file_put_contents(realpath(__DIR__."/../pluginStuff")."/".$this->pluginName.".php", $data) !== false){ echo $this->color->getColoredString("Plugin corectly installed", "green"); echo "\n"; }else{ echo "\n"; echo $this->color->getColoredString("An error occured during file writing", "red"); echo "\n"; } } }else{ echo "\n"; echo $this->color->getColoredString("Download error during transfer from ".self::DOWNLOAD_DIRECTORY.$this->pluginName, "red"); echo "\n\n"; } } }