public function saveAction() { $app_id = $this->getAppId(); $theme_id = waRequest::get('theme_id'); $file = waRequest::get('file'); $errors = null; $path = wa()->getDataPath('themes', true, $app_id, false); $this->checkAccess($path); // copy original theme $theme = new waTheme($theme_id, $app_id); if ($theme['type'] == waTheme::ORIGINAL) { $theme->copy(); } // create file if (!$file) { // parent if (waRequest::post('type')) { $file = waRequest::post('parent'); $theme->addFile($file, '', array('parent' => 1)); } else { $file = waRequest::post('file'); if ($this->checkFile($file, $errors)) { $theme->addFile($file, waRequest::post('description')); } } if (!$errors) { if (!$theme->save()) { $errors = _ws('Insufficient file access permissions to save theme settings'); } else { $this->logAction('template_add', $file); } } } else { if (waRequest::post('file') && $file != waRequest::post('file')) { if (!$this->checkFile(waRequest::post('file'), $errors)) { $this->displayJson(array(), $errors); return; } $theme->removeFile($file); $file = waRequest::post('file'); if (!$theme->addFile($file, waRequest::post('description'))->save()) { $errors = _ws('Insufficient file access permissions to save theme settings'); } else { $this->logAction('template_edit', $file); } } else { $f = $theme->getFile($file); if (!empty($theme['parent_theme_id']) && $f['parent']) { $theme = new waTheme($theme['parent_theme_id']); if ($theme['type'] == waTheme::ORIGINAL) { $theme->copy(); } } if (!$theme->changeFile($file, waRequest::post('description'))) { $errors = _ws('Insufficient file access permissions to save theme settings'); } else { $this->logAction('template_edit', $file); } } @touch($theme->getPath() . '/' . waTheme::PATH); } $response = array(); if ($file && !$errors) { // update mtime of theme.xml @touch($path); $response['id'] = $file; switch ($ext = waFiles::extension($file)) { case 'css': case 'js': $response['type'] = $ext; break; default: $response['type'] = ''; } $response['theme'] = $theme_id; // if not parent if (!waRequest::post('type')) { $content = waRequest::post('content'); $file_path = $theme->getPath() . '/' . $file; if (!file_exists($file_path) || is_writable($file_path)) { if ($content || file_exists($file_path)) { $r = @file_put_contents($file_path, $content); if ($r !== false) { $r = true; if (in_array($ext, array('css', 'js'))) { $theme['edition'] = true; $theme->save(); } } } else { $r = @touch($file_path); } } else { $r = false; } if (!$r) { $errors = _ws('Insufficient access permissions to save the file') . ' ' . $file_path; } } else { $response['inherit'] = 1; } } $this->displayJson($response, $errors); }