Example #1
0
 /**
  * Adds a JS file to be loaded upon clientside rendering. Leave the localpath empty for remote files
  * @param \SimpleXMLElement The XML root
  * @param string The (full)path to the local JS file
  * @param string The (full)path to the remote file
  * @param int The \System\Web\BasePage\Page\InclusionLocation for inclusion location
  */
 public function addJSFile(\SimpleXMLElement $xml, $localFile, $remoteFile, $location = \System\Web\BasePage\Page\InclusionLocation::LOCATION_HEAD)
 {
     if (!isset($xml->jsfiles)) {
         $xml->addChild('jsfiles');
     }
     $jsFiles = $xml->jsfiles;
     $jsFile = $jsFiles->addChild('jsfile');
     if ($localFile) {
         $file = new \System\IO\File($localFile);
         if (MINIFY_ENABLE && mb_strpos($file->getFilename(), self::JS_MIN_EXTENSION) === false && mb_strpos($file->getFilename(), self::JS_MIN_PACKED) === false) {
             $localMinFile = $file->getPath() . basename($file->getFilename(), self::JS_EXTENSION) . self::JS_MIN_EXTENSION;
             if (!file_exists($localMinFile)) {
                 $file = \System\IO\File::writeContents($localMinFile, \System\Web\Minify\JS\Minify::minify($file->getContents()));
             } else {
                 $file = new \System\IO\File($localMinFile);
             }
             $remoteFile = str_ireplace(self::JS_EXTENSION, self::JS_MIN_EXTENSION, $remoteFile);
         }
         $jsFile->addChild('filesize', $file->getFileSizeInBytes());
     }
     $jsFile->addChild('name', $remoteFile);
     $jsFile->addChild('location', $location);
 }
 /**
  * Deletes a file from the filesystem. Listens to EVENT_DELETE_FILE and requires a 'host', 'port', 'username', 'password' and 'file' field.
  * @param OnInteractionEvent The event to listen to
  */
 public static final function deleteFile(\System\System\Interaction\Event\OnInteractionEvent $event)
 {
     $msg = $event->getMessage();
     if ($msg->getType() == \System\System\Interaction\MessageType::TYPE_SYSTEM && $msg->getMessage() == SystemInteractionEventEvent::EVENT_DELETE_FILE) {
         $params = $msg->getParams();
         if (isset($params['file']) && isset($params['host']) && isset($params['username']) && isset($params['password']) && isset($params['port'])) {
             //test if the file exists
             $file = new \System\IO\File($params['file']);
             if (!function_exists('ssh2_connect') || !function_exists('ssh2_auth_password') || !function_exists('ssh2_sftp')) {
                 throw new \System\Error\Exception\SystemException('The required module, SSH2, is not loaded in PHP');
             }
             $connection = ssh2_connect($params['host'], $params['port']);
             if ($connection && ssh2_auth_password($connection, $params['username'], $params['password'])) {
                 $sftp = ssh2_sftp($connection);
                 $result = ssh2_sftp_unlink($sftp, getcwd() . '/' . $params['file']);
                 $response = new \System\System\Interaction\Response($msg, 'File delete ' . $file->getFilename() . ': ' . ($result ? 'success' : 'fail'));
                 $event->addResponse($response);
             }
         }
     }
 }