2014-02-21 13:01:02 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OCA\Files_Sharing;
|
|
|
|
|
|
|
|
use \OCP\Files\NotPermittedException;
|
2014-06-03 18:50:05 +04:00
|
|
|
use \OC\AppFramework\Middleware\Security\SecurityException;
|
|
|
|
use OCA\Files_Sharing\Http\MailTemplateResponse;
|
2014-02-21 13:01:02 +04:00
|
|
|
|
|
|
|
class MailTemplate extends \OC_Template {
|
2014-06-03 18:50:05 +04:00
|
|
|
|
2014-02-21 13:01:02 +04:00
|
|
|
private $path;
|
|
|
|
private $theme;
|
|
|
|
private $editableThemes;
|
|
|
|
private $editableTemplates;
|
|
|
|
|
|
|
|
public function __construct($theme, $path) {
|
|
|
|
$this->theme = $theme;
|
|
|
|
$this->path = $path;
|
2014-06-03 18:50:05 +04:00
|
|
|
|
2014-02-21 13:01:02 +04:00
|
|
|
//determine valid theme names
|
|
|
|
$this->editableThemes = self::getEditableThemes();
|
2014-06-04 17:01:36 +04:00
|
|
|
//for now hard code the valid mail template paths
|
2014-02-21 13:01:02 +04:00
|
|
|
$this->editableTemplates = self::getEditableTemplates();
|
|
|
|
}
|
|
|
|
|
2014-06-03 18:50:05 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return \OCA\Files_Sharing\Http\MailTemplateResponse
|
|
|
|
*/
|
|
|
|
public function getResponse() {
|
|
|
|
if($this->isEditable()) {
|
|
|
|
list($app, $filename) = explode('/templates/', $this->path, 2);
|
|
|
|
$name = substr($filename, 0, -4);
|
|
|
|
list($path, $template) = $this->findTemplate($this->theme, $app, $name, '');
|
|
|
|
return new MailTemplateResponse($template);
|
|
|
|
}
|
2014-06-04 17:01:36 +04:00
|
|
|
throw new SecurityException('Template not editable.', 403);
|
2014-06-03 18:50:05 +04:00
|
|
|
}
|
|
|
|
|
2014-02-21 13:01:02 +04:00
|
|
|
public function renderContent() {
|
|
|
|
if($this->isEditable()) {
|
2014-06-03 18:50:05 +04:00
|
|
|
list($app, $filename) = explode('/templates/', $this->path, 2);
|
2014-02-21 13:01:02 +04:00
|
|
|
$name = substr($filename, 0, -4);
|
|
|
|
list($path, $template) = $this->findTemplate($this->theme, $app, $name, '');
|
|
|
|
\OC_Response::sendFile($template);
|
|
|
|
} else {
|
2014-06-03 18:50:05 +04:00
|
|
|
throw new SecurityException('Template not editable.', 403);
|
2014-02-21 13:01:02 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-03 18:50:05 +04:00
|
|
|
|
2014-02-21 13:01:02 +04:00
|
|
|
public function isEditable() {
|
|
|
|
if ($this->editableThemes[$this->theme]
|
|
|
|
&& $this->editableTemplates[$this->path]
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-03 18:50:05 +04:00
|
|
|
|
2014-02-21 13:01:02 +04:00
|
|
|
public function setContent($data) {
|
|
|
|
if($this->isEditable()) {
|
|
|
|
//save default templates in default folder to overwrite core template
|
|
|
|
$absolutePath = \OC::$SERVERROOT.'/themes/'.$this->theme.'/'.$this->path;
|
|
|
|
$parent = dirname($absolutePath);
|
|
|
|
if ( ! is_dir($parent) ) {
|
|
|
|
if ( ! mkdir(dirname($absolutePath), 0777, true) ){
|
2014-06-03 18:50:05 +04:00
|
|
|
throw new \Exception('Could not create directory.', 500);
|
2014-02-21 13:01:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $this->theme !== 'default' && is_file($absolutePath) ) {
|
|
|
|
if ( ! copy($absolutePath, $absolutePath.'.bak') ){
|
2014-06-03 18:50:05 +04:00
|
|
|
throw new \Exception('Could not overwrite template.', 500);
|
2014-02-21 13:01:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//overwrite theme templates? versioning?
|
|
|
|
return file_put_contents($absolutePath, $data);
|
|
|
|
}
|
2014-06-03 18:50:05 +04:00
|
|
|
throw new SecurityException('Template not editable.', 403);
|
2014-02-21 13:01:02 +04:00
|
|
|
}
|
2014-06-03 18:50:05 +04:00
|
|
|
|
2014-02-21 13:01:02 +04:00
|
|
|
public function reset(){
|
|
|
|
if($this->isEditable()) {
|
|
|
|
$absolutePath = \OC::$SERVERROOT.'/themes/'.$this->theme.'/'.$this->path;
|
|
|
|
if ($this->theme === 'default') {
|
|
|
|
//templates can simply be deleted in the themes folder
|
|
|
|
if (unlink($absolutePath)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//if a bak file exists overwrite the template with it
|
|
|
|
if (is_file($absolutePath.'.bak')) {
|
|
|
|
if (rename($absolutePath.'.bak', $absolutePath)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-03 18:50:05 +04:00
|
|
|
throw new NotPermittedException('Template not editable.', 403);
|
2014-02-21 13:01:02 +04:00
|
|
|
}
|
2014-06-03 18:50:05 +04:00
|
|
|
|
2014-02-21 13:01:02 +04:00
|
|
|
public static function getEditableThemes() {
|
|
|
|
$themes = array(
|
|
|
|
'default' => true
|
|
|
|
);
|
|
|
|
if ($handle = opendir(\OC::$SERVERROOT.'/themes')) {
|
|
|
|
while (false !== ($entry = readdir($handle))) {
|
2014-06-03 18:50:05 +04:00
|
|
|
if ($entry != '.' && $entry != '..' && $entry != 'default') {
|
2014-02-21 13:01:02 +04:00
|
|
|
if (is_dir(\OC::$SERVERROOT.'/themes/'.$entry)) {
|
|
|
|
$themes[$entry] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($handle);
|
|
|
|
}
|
|
|
|
return $themes;
|
|
|
|
}
|
2014-06-03 18:50:05 +04:00
|
|
|
|
2014-02-21 13:01:02 +04:00
|
|
|
public static function getEditableTemplates() {
|
|
|
|
return array(
|
|
|
|
'core/templates/mail.php' => true,
|
|
|
|
'core/templates/altmail.php' => true,
|
|
|
|
'core/lostpassword/templates/email.php' => true,
|
|
|
|
);
|
|
|
|
}
|
2014-06-03 18:50:05 +04:00
|
|
|
}
|