/** * Saves a resource * Redirects to main listing * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); // Initiate extended database class $row = new Resource($this->database); if (!$row->bind($_POST)) { throw new Exception($row->getError(), 400); } $isNew = 0; if ($row->id < 1) { $isNew = 1; } if ($isNew) { // New entry $row->created = $row->created ? $row->created : Date::toSql(); $row->created_by = $row->created_by ? $row->created_by : User::get('id'); $row->access = 0; } else { $old = new Resource($this->database); $old->load($row->id); $created_by_id = Request::getInt('created_by_id', 0); // Updating entry $row->modified = Date::toSql(); $row->modified_by = User::get('id'); if ($created_by_id) { $row->created_by = $row->created_by ? $row->created_by : $created_by_id; } else { $row->created_by = $row->created_by ? $row->created_by : User::get('id'); } } // publish up $row->publish_up = Date::of($row->publish_up, Config::get('offset'))->toSql(); // publish down if (!$row->publish_down || trim($row->publish_down) == '0000-00-00 00:00:00' || trim($row->publish_down) == 'Never') { $row->publish_down = '0000-00-00 00:00:00'; } else { $row->publish_down = Date::of($row->publish_down, Config::get('offset'))->toSql(); } // Get parameters $params = Request::getVar('params', array(), 'post'); if (is_array($params)) { $txt = new \Hubzero\Config\Registry(''); foreach ($params as $k => $v) { $txt->set($k, $v); } $row->params = $txt->toString(); } // Get attributes $attribs = Request::getVar('attrib', array(), 'post'); if (is_array($attribs)) { $txta = new \Hubzero\Config\Registry(''); foreach ($attribs as $k => $v) { if ($k == 'timeof') { if (strtotime(trim($v)) === false) { $v = NULL; } $v = trim($v) ? Date::of($v, Config::get('offset'))->toSql() : NULL; } $txta->set($k, $v); } $row->attribs = $txta->toString(); } // Get custom areas, add wrappers, and compile into fulltxt if (isset($_POST['nbtag'])) { $type = new Type($this->database); $type->load($row->type); include_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'models' . DS . 'elements.php'; $elements = new \Components\Resources\Models\Elements(array(), $type->customFields); $schema = $elements->getSchema(); $fields = array(); foreach ($schema->fields as $field) { $fields[$field->name] = $field; } $nbtag = $_POST['nbtag']; $found = array(); foreach ($nbtag as $tagname => $tagcontent) { $f = ''; $row->fulltxt .= "\n" . '<nb:' . $tagname . '>'; if (is_array($tagcontent)) { $c = count($tagcontent); $num = 0; foreach ($tagcontent as $key => $val) { if (trim($val)) { $num++; } $row->fulltxt .= '<' . $key . '>' . trim($val) . '</' . $key . '>'; } if ($c == $num) { $f = 'found'; } } else { $f = trim($tagcontent); if ($f) { $row->fulltxt .= trim($tagcontent); } } $row->fulltxt .= '</nb:' . $tagname . '>' . "\n"; if (!$tagcontent && isset($fields[$tagname]) && $fields[$tagname]->required) { throw new Exception(Lang::txt('RESOURCES_REQUIRED_FIELD_CHECK', $fields[$tagname]->label), 500); } $found[] = $tagname; } foreach ($fields as $field) { if (!in_array($field->name, $found) && $field->required) { $found[] = $field->name; $this->setError(Lang::txt('COM_CONTRIBUTE_REQUIRED_FIELD_CHECK', $field->label)); } } } // Code cleaner for xhtml transitional compliance if ($row->type != 7) { $row->introtext = str_replace('<br>', '<br />', $row->introtext); $row->fulltxt = str_replace('<br>', '<br />', $row->fulltxt); } // Check content if (!$row->check()) { throw new Exception($row->getError(), 500); } // Store content if (!$row->store()) { throw new Exception($row->getError(), 500); } // Checkin resource $row->checkin(); // Rename the temporary upload directory if it exist $tmpid = Request::getInt('tmpid', 0, 'post'); if ($tmpid != Html::niceidformat($row->id)) { // Build the full paths $path = Html::dateToPath($row->created); $dir_id = Html::niceidformat($row->id); $tmppath = Utilities::buildUploadPath($path . DS . $tmpid); $newpath = Utilities::buildUploadPath($path . DS . $dir_id); // Attempt to rename the temp directory if (\Filesystem::exists($tmppath)) { $result = \Filesystem::move($tmppath, $newpath); if ($result !== true) { $this->setError($result); } } $row->path = str_replace($tmpid, Html::niceidformat($row->id), $row->path); $row->store(); } // Incoming tags $tags = Request::getVar('tags', '', 'post'); // Save the tags $rt = new Tags($row->id); $rt->setTags($tags, User::get('id'), 1, 1); // Incoming authors if ($row->type != 7) { $authorsOldstr = Request::getVar('old_authors', '', 'post'); $authorsNewstr = Request::getVar('new_authors', '', 'post'); if (!$authorsNewstr) { $authorsNewstr = $authorsOldstr; } include_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'contributor.php'; $authorsNew = explode(',', $authorsNewstr); $authorsOld = explode(',', $authorsOldstr); // We have either a new ordering or new authors or both if ($authorsNewstr) { for ($i = 0, $n = count($authorsNew); $i < $n; $i++) { $rc = new Contributor($this->database); $rc->subtable = 'resources'; $rc->subid = $row->id; if (is_numeric($authorsNew[$i])) { $rc->authorid = $authorsNew[$i]; } else { $rc->authorid = $rc->getUserId($authorsNew[$i]); } $rc->ordering = $i; $rc->role = trim(Request::getVar($authorsNew[$i] . '_role', '')); $rc->name = trim(Request::getVar($authorsNew[$i] . '_name', '')); $rc->organization = trim(Request::getVar($authorsNew[$i] . '_organization', '')); $authorsNew[$i] = $rc->authorid; if (in_array($authorsNew[$i], $authorsOld)) { //echo 'update: ' . $rc->authorid . ', ' . $rc->role . ', ' . $rc->name . ', ' . $rc->organization . '<br />'; // Updating record $rc->updateAssociation(); } else { //echo 'create: ' . $rc->authorid . ', ' . $rc->role . ', ' . $rc->name . ', ' . $rc->organization . '<br />'; // New record $rc->createAssociation(); } } } // Run through previous author list and check to see if any IDs had been dropped if ($authorsOldstr) { $rc = new Contributor($this->database); for ($i = 0, $n = count($authorsOld); $i < $n; $i++) { if (!in_array($authorsOld[$i], $authorsNew)) { $rc->deleteAssociation($authorsOld[$i], $row->id, 'resources'); } } } } // If this is a child, add parent/child association $pid = Request::getInt('pid', 0, 'post'); if ($isNew && $pid) { $this->_attachChild($row->id, $pid); } // Is this a standalone resource and we need to email approved submissions? if ($row->standalone == 1 && $this->config->get('email_when_approved')) { // If the state went from pending to published if ($row->published == 1 && $old->published == 3) { $this->_emailContributors($row, $this->database); } } // Redirect App::redirect($this->buildRedirectURL($pid), Lang::txt('COM_RESOURCES_ITEM_SAVED')); }
/** * Method to rename the template in the XML files and rename the language files * * @return boolean true if successful, false otherwise * @since 2.5 */ protected function fixTemplateName() { // Rename Language files // Get list of language files $result = true; $files = Filesystem::files($this->getState('to_path'), '.ini', true, true); $newName = strtolower($this->getState('new_name')); $oldName = $this->getTemplate()->element; foreach ($files as $file) { $newFile = str_replace($oldName, $newName, $file); $result = Filesystem::move($file, $newFile) && $result; } // Edit XML file $xmlFile = $this->getState('to_path') . '/templateDetails.xml'; if (Filesystem::exists($xmlFile)) { $contents = Filesystem::read($xmlFile); $pattern[] = '#<name>\\s*' . $oldName . '\\s*</name>#i'; $replace[] = '<name>' . $newName . '</name>'; $pattern[] = '#<language(.*)' . $oldName . '(.*)</language>#'; $replace[] = '<language${1}' . $newName . '${2}</language>'; $contents = preg_replace($pattern, $replace, $contents); $result = Filesystem::write($xmlFile, $contents) && $result; } return $result; }
/** * Uploads a file to a given directory and returns an attachment string * that is appended to report/comment bodies * * @param string $listdir Directory to upload files to * @return string A string that gets appended to messages */ public function uploadTask($listdir, $comment_id = 0) { if (!$listdir) { $this->setError(Lang::txt('COM_SUPPORT_ERROR_MISSING_UPLOAD_DIRECTORY')); return ''; } // Construct our file path $path = PATH_APP . DS . trim($this->config->get('webpath', '/site/tickets'), DS) . DS . $listdir; $row = new Tables\Attachment($this->database); // Rename temp directories if ($tmp = Request::getInt('tmp_dir')) { $tmpPath = PATH_APP . DS . trim($this->config->get('webpath', '/site/tickets'), DS) . DS . $tmp; if (is_dir($tmpPath)) { if (!\Filesystem::move($tmpPath, $path)) { $this->setError(Lang::txt('COM_SUPPORT_ERROR_UNABLE_TO_MOVE_UPLOAD_PATH')); throw new Exception(Lang::txt('COM_SUPPORT_ERROR_UNABLE_TO_MOVE_UPLOAD_PATH'), 500); return ''; } $row->updateTicketId($tmp, $listdir); } } // Incoming file $file = Request::getVar('upload', '', 'files', 'array'); if (!isset($file['name']) || !$file['name']) { //$this->setError(Lang::txt('SUPPORT_NO_FILE')); return ''; } // Incoming $description = Request::getVar('description', ''); // Build the path if it doesn't exist if (!is_dir($path)) { if (!Filesystem::makeDirectory($path)) { $this->setError(Lang::txt('COM_SUPPORT_ERROR_UNABLE_TO_CREATE_UPLOAD_PATH')); return ''; } } // Make the filename safe $file['name'] = Filesystem::clean($file['name']); $file['name'] = str_replace(' ', '_', $file['name']); $ext = strtolower(Filesystem::extension($file['name'])); //make sure that file is acceptable type if (!in_array($ext, explode(',', $this->config->get('file_ext')))) { $this->setError(Lang::txt('COM_SUPPORT_ERROR_INCORRECT_FILE_TYPE')); return Lang::txt('COM_SUPPORT_ERROR_INCORRECT_FILE_TYPE'); } $filename = Filesystem::name($file['name']); while (file_exists($path . DS . $filename . '.' . $ext)) { $filename .= rand(10, 99); } $finalfile = $path . DS . $filename . '.' . $ext; // Perform the upload if (!Filesystem::upload($file['tmp_name'], $finalfile)) { $this->setError(Lang::txt('COM_SUPPORT_ERROR_UPLOADING')); return ''; } else { // Scan for viruses if (!\Filesystem::isSafe($finalfile)) { if (\Filesystem::delete($finalfile)) { $this->setError(Lang::txt('COM_SUPPORT_ERROR_FAILED_VIRUS_SCAN')); return Lang::txt('COM_SUPPORT_ERROR_FAILED_VIRUS_SCAN'); } } // File was uploaded // Create database entry $description = htmlspecialchars($description); $row->bind(array('id' => 0, 'ticket' => $listdir, 'comment_id' => $comment_id, 'filename' => $filename . '.' . $ext, 'description' => $description)); if (!$row->check()) { $this->setError($row->getError()); } if (!$row->store()) { $this->setError($row->getError()); } if (!$row->id) { $row->getID(); } return '{attachment#' . $row->id . '}'; } }
/** * Save a wiki page * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); // Check if they are logged in if (User::isGuest()) { $url = Request::getVar('REQUEST_URI', '', 'server'); App::redirect(Route::url('index.php?option=com_users&view=login&return=' . base64_encode($url))); return; } // Incoming revision $rev = Request::getVar('revision', array(), 'post', 'none', 2); //$rev['pageid'] = (isset($rev['pageid'])) ? intval($rev['pageid']) : 0; $this->revision = $this->page->revision('current'); $this->revision->set('version', $this->revision->get('version') + 1); if (!$this->revision->bind($rev)) { $this->setError($this->revision->getError()); $this->editTask(); return; } $this->revision->set('id', 0); // Incoming page $page = Request::getVar('page', array(), 'post', 'none', 2); $this->page = new Article(intval($rev['pageid'])); if (!$this->page->bind($page)) { $this->setError($this->page->getError()); $this->editTask(); return; } $this->page->set('pagename', trim(Request::getVar('pagename', '', 'post'))); $this->page->set('scope', trim(Request::getVar('scope', '', 'post'))); // Get parameters $params = new \Hubzero\Config\Registry($this->page->get('params', '')); $params->merge(Request::getVar('params', array(), 'post')); $this->page->set('params', $params->toString()); // Get the previous version to compare against if (!$rev['pageid']) { // New page - save it to the database $this->page->set('created_by', User::get('id')); $old = new Revision(0); } else { // Get the revision before changes $old = $this->page->revision('current'); } // Was the preview button pushed? $this->preview = trim(Request::getVar('preview', '')); if ($this->preview) { // Set the component task if (!$rev['pageid']) { Request::setVar('task', 'new'); $this->_task = 'new'; } else { Request::setVar('task', 'edit'); $this->_task = 'edit'; } // Push on through to the edit form $this->editTask(); return; } // Check content // First, make sure the pagetext isn't empty if ($this->revision->get('pagetext') == '') { $this->setError(Lang::txt('COM_WIKI_ERROR_MISSING_PAGETEXT')); $this->editTask(); return; } // Store new content if (!$this->page->store(true)) { $this->setError($this->page->getError()); $this->editTask(); return; } // Get allowed authors if (!$this->page->updateAuthors(Request::getVar('authors', '', 'post'))) { $this->setError($this->page->getError()); $this->editTask(); return; } // Get the upload path $wpa = new Tables\Attachment($this->database); $path = $wpa->filespace(); // Rename the temporary upload directory if it exist $lid = Request::getInt('lid', 0, 'post'); if ($lid != $this->page->get('id')) { if (is_dir($path . DS . $lid)) { if (!\Filesystem::move($path . DS . $lid, $path . DS . $this->page->get('id'))) { $this->setError(\Filesystem::move($path . DS . $lid, $path . DS . $this->page->get('id'))); } $wpa->setPageID($lid, $this->page->get('id')); } } $this->revision->set('pageid', $this->page->get('id')); $this->revision->set('pagename', $this->page->get('pagename')); $this->revision->set('scope', $this->page->get('scope')); $this->revision->set('group_cn', $this->page->get('group_cn')); $this->revision->set('version', $this->revision->get('version') + 1); if ($this->page->param('mode', 'wiki') == 'knol') { // Set revisions to NOT approved $this->revision->set('approved', 0); // If an author or the original page creator, set to approved if ($this->page->get('created_by') == User::get('id') || $this->page->isAuthor(User::get('id'))) { $this->revision->set('approved', 1); } } else { // Wiki mode, approve revision $this->revision->set('approved', 1); } // Compare against previous revision // We don't want to create a whole new revision if just the tags were changed if (rtrim($old->get('pagetext')) != rtrim($this->revision->get('pagetext'))) { // Transform the wikitext to HTML $this->revision->set('pagehtml', ''); $this->revision->set('pagehtml', $this->revision->content('parsed')); // Parse attachments /*$a = new Tables\Attachment($this->database); $a->pageid = $this->page->id; $a->path = $path; $this->revision->pagehtml = $a->parse($this->revision->pagehtml);*/ if ($this->page->access('manage') || $this->page->access('edit')) { $this->revision->set('approved', 1); } // Store content if (!$this->revision->store(true)) { $this->setError(Lang::txt('COM_WIKI_ERROR_SAVING_REVISION')); $this->editTask(); return; } $this->page->set('version_id', $this->revision->get('id')); $this->page->set('modified', $this->revision->get('created')); } else { $this->page->set('modified', Date::toSql()); } if (!$this->page->store(true)) { // This really shouldn't happen. $this->setError(Lang::txt('COM_WIKI_ERROR_SAVING_PAGE')); $this->editTask(); return; } // Process tags $this->page->tag(Request::getVar('tags', '')); // Redirect App::redirect(Route::url($this->page->link())); }
/** * Process an image * * @return boolean True if no errors */ public function process() { $docRoot = $this->path; $image = $this->image; $cropratio = $this->cropratio; $quality = $this->quality; $color = $this->color; // Make sure that the requested file is actually an image if (!$image) { $this->setError(Lang::txt('No image set.')); return false; } // Make sure that the requested file is actually an image if (!$docRoot) { $this->setError(Lang::txt('No image path set.')); return false; } // Strip the possible trailing slash off the document root //$docRoot = preg_replace('/\/$/', '', $docRoot); if (!is_file($docRoot . $image)) { $this->setError(Lang::txt('File/path not found.')); return false; } // Get the size and MIME type of the requested image $size = GetImageSize($docRoot . $image); $mime = $size['mime']; // Make sure that the requested file is actually an image if (substr($mime, 0, 6) != 'image/') { $this->setError(Lang::txt('File is not an image.')); return false; } $width = $size[0]; $height = $size[1]; $maxWidth = $this->maxWidth; $maxHeight = $this->maxHeight; if ($maxWidth >= $width && $maxHeight >= $height) { return true; } if ($color) { $color = preg_replace('/[^0-9a-fA-F]/', '', (string) $color); } else { $color = FALSE; } // Ratio cropping $offsetX = 0; $offsetY = 0; if ($cropratio) { $cropRatio = explode(':', (string) $cropratio); if (count($cropRatio) == 2) { $ratioComputed = $width / $height; $cropRatioComputed = (double) $cropRatio[0] / (double) $cropRatio[1]; if ($ratioComputed < $cropRatioComputed) { // Image is too tall so we will crop the top and bottom $origHeight = $height; $height = $width / $cropRatioComputed; $offsetY = ($origHeight - $height) / 2; } else { if ($ratioComputed > $cropRatioComputed) { // Image is too wide so we will crop off the left and right sides $origWidth = $width; $width = $height * $cropRatioComputed; $offsetX = ($origWidth - $width) / 2; } } } } // Setting up the ratios needed for resizing. We will compare these below to determine how to // resize the image (based on height or based on width) $xRatio = $maxWidth / $width; $yRatio = $maxHeight / $height; if ($xRatio * $height < $maxHeight) { // Resize the image based on width $tnHeight = ceil($xRatio * $height); $tnWidth = $maxWidth; } else { // Resize the image based on height $tnWidth = ceil($yRatio * $width); $tnHeight = $maxHeight; } // Before we actually do any crazy resizing of the image, we want to make sure that we // haven't already done this one at these dimensions. To the cache! // Note, cache must be world-readable // We store our cached image filenames as a hash of the dimensions and the original filename $resizedImageSource = $tnWidth . 'x' . $tnHeight . 'x' . $quality; if ($cropratio) { $resizedImageSource .= 'x' . (string) $cropratio; } $resizedImageSource .= '-' . $image; $resizedImage = $resizedImageSource; //md5($resizedImageSource); $resized = $docRoot . $resizedImage; // We don't want to run out of memory ini_set('memory_limit', $this->_MEMORY_TO_ALLOCATE); // Set up a blank canvas for our resized image (destination) $dst = imagecreatetruecolor($tnWidth, $tnHeight); // Set up the appropriate image handling functions based on the original image's mime type switch ($size['mime']) { case 'image/gif': // We will be converting GIFs to PNGs to avoid transparency issues when resizing GIFs // This is maybe not the ideal solution, but IE6 can suck it $creationFunction = 'ImageCreateFromGif'; $outputFunction = 'ImagePng'; $mime = 'image/png'; // We need to convert GIFs to PNGs $doSharpen = FALSE; $quality = round(10 - $quality / 10); // We are converting the GIF to a PNG and PNG needs a compression level of 0 (no compression) through 9 break; case 'image/x-png': case 'image/png': $creationFunction = 'ImageCreateFromPng'; $outputFunction = 'ImagePng'; $doSharpen = FALSE; $quality = round(10 - $quality / 10); // PNG needs a compression level of 0 (no compression) through 9 break; default: $creationFunction = 'ImageCreateFromJpeg'; $outputFunction = 'ImageJpeg'; $doSharpen = TRUE; break; } // Read in the original image $src = $creationFunction($docRoot . $image); if (in_array($size['mime'], array('image/gif', 'image/png'))) { if (!$color) { // If this is a GIF or a PNG, we need to set up transparency imagealphablending($dst, false); imagesavealpha($dst, true); } else { // Fill the background with the specified color for matting purposes if ($color[0] == '#') { $color = substr($color, 1); } $background = FALSE; if (strlen($color) == 6) { $background = imagecolorallocate($dst, hexdec($color[0] . $color[1]), hexdec($color[2] . $color[3]), hexdec($color[4] . $color[5])); } else { if (strlen($color) == 3) { $background = imagecolorallocate($dst, hexdec($color[0] . $color[0]), hexdec($color[1] . $color[1]), hexdec($color[2] . $color[2])); } } if ($background) { imagefill($dst, 0, 0, $background); } } } // Resample the original image into the resized canvas we set up earlier ImageCopyResampled($dst, $src, 0, 0, $offsetX, $offsetY, $tnWidth, $tnHeight, $width, $height); if ($doSharpen) { // Sharpen the image based on two things: // (1) the difference between the original size and the final size // (2) the final size $sharpness = $this->findSharp($width, $tnWidth); $sharpenMatrix = array(array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1)); $divisor = $sharpness; $offset = 0; if (function_exists('imageconvolution')) { imageconvolution($dst, $sharpenMatrix, $divisor, $offset); } } // Write the resized image to the cache $outputFunction($dst, $resized, $quality); // Yes - remove it $overwrite = $this->overwrite; if ($overwrite) { $outputName = $this->outputName; if ($outputName) { $image = $outputName; } if (file_exists($resized)) { if (file_exists($docRoot . $image)) { if (!\Filesystem::delete($docRoot . $image)) { $this->setError(Lang::txt('UNABLE_TO_DELETE_FILE')); return false; } } if (!\Filesystem::move($resized, $docRoot . $image)) { $this->setError(Lang::txt('UNABLE_TO_DELETE_FILE')); return false; } } } return true; }
/** * Process the compose step * * @return void */ public function step_compose_process() { // Initiate extended database class $fields = Request::getVar('fields', array(), 'post'); $row = Resource::oneOrNew($fields['id'])->set($fields); $isNew = $row->get('id') < 1 || substr($row->get('id'), 0, 4) == '9999'; //$row->created = ($row->created) ? $row->created : Date::toSql(); //$row->created_by = ($row->created_by) ? $row->created_by : User::get('id'); // Set status to "composing" if ($isNew) { $row->set('published', 2); } $row->set('published', (int) $row->get('published', 2)); $row->set('publish_up', $row->get('publish_up') && $row->get('publish_up') != '0000-00-00 00:00:00' ? $row->get('publish_up') : Date::toSql()); $row->set('publish_down', $row->get('publish_down') && $row->get('publish_down') != '0000-00-00 00:00:00' ? $row->get('publish_down') : '0000-00-00 00:00:00'); $row->set('modified', Date::toSql()); $row->set('modified_by', User::get('id')); $row->set('access', (int) $row->get('access', 0)); $row->set('fulltxt', trim(preg_replace('/\\\\/', "%5C", $row->get('fulltxt')))); $row->set('introtext', String::truncate(strip_tags($row->get('fulltxt')), 500)); // Get custom areas, add wrapper tags, and compile into fulltxt $type = Type::oneOrFail($row->get('type')); include_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'models' . DS . 'elements.php'; $elements = new Elements(array(), $type->customFields); $schema = $elements->getSchema(); $fields = array(); if (is_object($schema)) { foreach ($schema->fields as $field) { $fields[$field->name] = $field; } } $fulltxt = $row->get('fulltxt'); $nbtag = Request::getVar('nbtag', array(), 'post'); $found = array(); foreach ($nbtag as $tagname => $tagcontent) { $f = ''; $fulltxt .= "\n" . '<nb:' . $tagname . '>'; if (is_array($tagcontent)) { $c = count($tagcontent); $num = 0; foreach ($tagcontent as $key => $val) { if (trim($val)) { $num++; } $fulltxt .= '<' . $key . '>' . trim($val) . '</' . $key . '>'; } if ($c == $num) { $f = 'found'; } } else { $f = trim($tagcontent); if ($f) { $fulltxt .= trim($tagcontent); } } $fulltxt .= '</nb:' . $tagname . '>' . "\n"; if (!$f && isset($fields[$tagname]) && $fields[$tagname]->required) { $this->setError(Lang::txt('COM_CONTRIBUTE_REQUIRED_FIELD_CHECK', $fields[$tagname]->label)); } $found[] = $tagname; } $row->set('fulltxt', $fulltxt); foreach ($fields as $field) { if (!in_array($field->name, $found) && $field->required) { $found[] = $field->name; $this->setError(Lang::txt('COM_CONTRIBUTE_REQUIRED_FIELD_CHECK', $field->label)); } } $row->set('title', preg_replace('/\\s+/', ' ', $row->get('title'))); $row->set('title', $this->_txtClean($row->get('title'))); // Strip any scripting there may be if (trim($row->get('fulltxt'))) { $row->set('fulltxt', \Components\Resources\Helpers\Html::stripStyles($row->get('fulltxt'))); $row->set('fulltxt', $this->_txtClean($row->get('fulltxt'))); $row->set('footertext', $this->_txtClean($row->get('footertext'))); } // Fall back to step if any errors found if ($this->getError()) { $this->step--; $this->view->step = $this->step; $this->view->setLayout('compose'); return $this->step_compose($row); } // reset id if ($isNew) { $row->set('id', 0); } // Store new content if (!$row->save()) { $this->setError(Lang::txt('Error: Failed to store changes.')); $this->step--; $this->view->step = $this->step; $this->view->setLayout('compose'); return $this->step_compose($row); } // build path to temp upload folder and future permanent folder $session = App::get('session'); $created = Date::format('Y-m-d 00:00:00'); $oldPath = $row->basepath() . Html::build_path($created, $session->get('resources_temp_id'), ''); $newPath = $row->filespace(); // if we have a temp dir, move it to permanent location if (is_dir($oldPath)) { \Filesystem::move($oldPath, $newPath); $old = DS . $session->get('resources_temp_id') . DS; $new = DS . $row->id . DS; // update all images in abstract $row->set('introtext', str_replace($old, $new, $row->get('introtext'))); $row->set('fulltxt', str_replace($old, $new, $row->get('fulltxt'))); $row->save(); // clear temp id $session->clear('resources_temp_id'); } // Is it a new resource? if ($isNew) { // Automatically attach this user as the first author Request::setVar('pid', $row->get('id')); Request::setVar('id', $row->get('id')); Request::setVar('authid', User::get('id')); include_once __DIR__ . DS . 'authors.php'; $authors = new Authors(); $authors->saveTask(0); } // Log activity $recipients = array(['resource', $row->get('id')], ['user', $row->get('created_by')]); foreach ($row->authors()->where('authorid', '>', 0)->rows() as $author) { $recipients[] = ['user', $author->get('authorid')]; } Event::trigger('system.logActivity', ['activity' => ['action' => $isNew ? 'updated' : 'created', 'scope' => 'resource', 'scope_id' => $row->get('id'), 'description' => Lang::txt('COM_RESOURCES_ACTIVITY_ENTRY_' . (!$isNew ? 'UPDATED' : 'CREATED'), '<a href="' . Route::url('index.php?option=com_resources&id=' . $row->get('id')) . '">' . $row->get('title') . '</a>'), 'details' => array('title' => $row->get('title'), 'url' => Route::url('index.php?option=com_resources&id=' . $row->get('id')))], 'recipients' => $recipients]); }
/** * Process the compose step * * @return void */ public function step_compose_process() { // Initiate extended database class $row = new Resource($this->database); $row->load(Request::getInt('id', 0)); if (!$row->bind($_POST)) { throw new Exception($row->getError(), 500); } $isNew = $row->id < 1 || substr($row->id, 0, 4) == '9999'; $row->created = $row->created ? $row->created : Date::toSql(); $row->created_by = $row->created_by ? $row->created_by : User::get('id'); // Set status to "composing" if ($isNew) { $row->published = 2; } else { $row->published = $row->published ?: 2; } $row->publish_up = $row->publish_up && $row->publish_up != '0000-00-00 00:00:00' ? $row->publish_up : Date::toSql(); $row->publish_down = $row->publish_down && $row->publish_down != '0000-00-00 00:00:00' ? $row->publish_down : '0000-00-00 00:00:00'; $row->modified = Date::toSql(); $row->modified_by = User::get('id'); $row->access = $row->access ?: 0; $row->fulltxt = trim(preg_replace('/\\\\/', "%5C", $row->fulltxt)); $row->introtext = String::truncate(strip_tags($row->fulltxt), 500); //$row->fulltxt = $this->_txtAutoP($row->fulltxt, 1); // Get custom areas, add wrapper tags, and compile into fulltxt $type = new Type($this->database); $type->load($row->type); include_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'models' . DS . 'elements.php'; $elements = new Elements(array(), $type->customFields); $schema = $elements->getSchema(); $fields = array(); if (is_object($schema)) { foreach ($schema->fields as $field) { $fields[$field->name] = $field; } } $nbtag = isset($_POST['nbtag']) ? $_POST['nbtag'] : array(); $found = array(); foreach ($nbtag as $tagname => $tagcontent) { $f = ''; $row->fulltxt .= "\n" . '<nb:' . $tagname . '>'; if (is_array($tagcontent)) { $c = count($tagcontent); $num = 0; foreach ($tagcontent as $key => $val) { if (trim($val)) { $num++; } $row->fulltxt .= '<' . $key . '>' . trim($val) . '</' . $key . '>'; } if ($c == $num) { $f = 'found'; } } else { $f = trim($tagcontent); if ($f) { $row->fulltxt .= trim($tagcontent); //(isset($fields[$tagname]) && $fields[$tagname]->type == 'textarea') ? $this->_txtAutoP(trim($tagcontent), 1) : trim($tagcontent); } } $row->fulltxt .= '</nb:' . $tagname . '>' . "\n"; if (!$f && isset($fields[$tagname]) && $fields[$tagname]->required) { $this->setError(Lang::txt('COM_CONTRIBUTE_REQUIRED_FIELD_CHECK', $fields[$tagname]->label)); } $found[] = $tagname; } foreach ($fields as $field) { if (!in_array($field->name, $found) && $field->required) { $found[] = $field->name; $this->setError(Lang::txt('COM_CONTRIBUTE_REQUIRED_FIELD_CHECK', $field->label)); } } $row->title = preg_replace('/\\s+/', ' ', $row->title); $row->title = $this->_txtClean($row->title); // Strip any scripting there may be if (trim($row->fulltxt)) { $row->fulltxt = \Components\Resources\Helpers\Html::stripStyles($row->fulltxt); $row->fulltxt = $this->_txtClean($row->fulltxt); //$row->fulltxt = $this->_txtAutoP($row->fulltxt, 1); $row->footertext = $this->_txtClean($row->footertext); } // Check content if (!$row->check()) { $this->setError($row->getError()); } // Fall back to step if any errors found if ($this->getError()) { $this->step--; $this->view->step = $this->step; $this->view->setLayout('compose'); $this->step_compose($row); return; } // reset id if ($isNew) { $row->id = null; } // Store new content if (!$row->store()) { $this->setError(Lang::txt('Error: Failed to store changes.')); $this->step--; $this->view->step = $this->step; $this->view->setLayout('compose'); $this->step_compose($row); return; } // build path to temp upload folder and future permanent folder $session = App::get('session'); $created = Date::format('Y-m-d 00:00:00'); $oldPath = PATH_APP . DS . trim($this->config->get('uploadpath', '/site/resources'), DS) . Html::build_path($created, $session->get('resources_temp_id'), ''); $newPath = PATH_APP . DS . trim($this->config->get('uploadpath', '/site/resources'), DS) . Html::build_path($row->created, $row->id, ''); // if we have a temp dir, move it to permanent location if (is_dir($oldPath)) { \Filesystem::move($oldPath, $newPath); $old = DS . $session->get('resources_temp_id') . DS; $new = DS . $row->id . DS; // update all images in abstract $row->introtext = str_replace($old, $new, $row->introtext); $row->fulltxt = str_replace($old, $new, $row->fulltxt); $row->store(); // clear temp id $session->clear('resources_temp_id'); } // Checkin the resource $row->checkin(); // Is it a new resource? if ($isNew) { // Get the resource ID if (!$row->id) { $row->id = $row->insertid(); } // Automatically attach this user as the first author Request::setVar('pid', $row->id); Request::setVar('id', $row->id); Request::setVar('authid', User::get('id')); include_once __DIR__ . DS . 'authors.php'; $authors = new Authors(); $authors->saveTask(0); } }
/** * This method allows you to do any additional work beyond unpacking * the files that is required. This could include work such as downloading * and unpacking an archive. * * The following are some of the methods available to you in this file: * $this->curlFile($url, $destFolder) * $this->move($src, $dest) * $this->unzip($file, $destFolder) */ public function doWork() { $fs = new Filesystem(); // Ensure tmp working dir exists $tmp = $this->mRootPath . "\\tmp"; $this->log("Creating temporary build directory: " . $tmp); $fs->mkdir($tmp); if ($this->p->get('source') != '' && $fs->exists($this->p->get('source'))) { // Use WordPress codebase from source parameter $this->log("Copying WordPress from " . $this->p->get('source')); $fs->copy($this->p->get('source'), $this->mAppRoot); } else { // Download and unpack WordPress $this->log('Downloading WordPress'); $file = $this->curlFile(WP_URL, $tmp); $this->log('Extracting WordPress'); $this->unzip($file, $tmp); $this->log('Moving WordPress files to ' . $this->mAppRoot); $fs->move("{$tmp}\\wordpress", $this->mAppRoot); } // Download and unpack DB abstraction layer $this->log('Downloading Database Abstraction Layer'); $file = $this->curlFile(DB_ABSTRACTION_URL, $tmp); $this->log('Extracting Database Abstraction Layer'); $this->unzip($file, $tmp); $this->log('Moving Database Abstraction Layer files to ' . $this->mAppRoot . "\\wp-content\\mu-plugins"); $fs->copy("{$tmp}\\wordpress-database-abstraction\\wp-db-abstraction\\db.php", $this->mAppRoot . "\\wp-content\\db.php"); $fs->move("{$tmp}\\wordpress-database-abstraction", $this->mAppRoot . "\\wp-content\\mu-plugins"); // Download and unpack Azure Storage Plugin $this->log('Downloading Azure Storage Plugin'); $file = $this->curlFile(WAZ_STORAGE_URL, $tmp); $this->log('Extracting Azure Storage Plugin'); $this->unzip($file, $tmp); $this->log('Moving Azure Storage Plugin files to ' . $this->mAppRoot . "\\wp-content\\plugins"); $fs->move("{$tmp}\\windows-azure-storage", $this->mAppRoot . "\\wp-content\\plugins\\windows-azure-storage"); if ($this->p->get('WP_ALLOW_MULTISITE') && $this->p->get('WP_ALLOW_MULTISITE') != 'false') { $fs->mkdir($this->mAppRoot . "\\wp-content\\blogs.dir"); unlink("{$this->mAppRoot}.config"); if ($this->p->get('SUBDOMAIN_INSTALL')) { copy($this->mAppRoot . "\\resources\\Web-network-subdomains.config", $this->mAppRoot . "\\Web.config"); } else { copy($this->mAppRoot . "\\resources\\Web-network-subfolders.config", $this->mAppRoot . "\\Web.config"); } } // Remove tmp build folder $fs->rm($tmp); $fs->rm($this->mRootPath . "/Params.class.php"); $fs->rm($this->mRootPath . "/FileSystem.class.php"); $this->updateWpConfig(); echo "\n\nCongratulations! You now have a brand new Windows Azure WordPress project at " . $this->mRootPath . "\n"; }
/** * Save a wiki page * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); // Check if they are logged in if (User::isGuest()) { $url = Request::getVar('REQUEST_URI', '', 'server'); App::redirect(Route::url('index.php?option=com_users&view=login&return=' . base64_encode($url), false)); } // Incoming revision $revision = $this->page->version; $revision->set('version', $revision->get('version') + 1); $revision->set(Request::getVar('revision', array(), 'post', 'none', 2)); $revision->set('id', 0); // Incoming page $page = Request::getVar('page', array(), 'post', 'none', 2); if (!isset($page['protected']) || !$page['protected']) { $page['protected'] = 0; } $this->page = Page::oneOrNew(intval($revision->get('page_id'))); $this->page->set($page); $this->page->set('pagename', trim(Request::getVar('pagename', '', 'post'))); // Get parameters $params = new \Hubzero\Config\Registry($this->page->get('params', '')); $params->merge(Request::getVar('params', array(), 'post')); $this->page->set('params', $params->toString()); // Get the previous version to compare against if (!$revision->get('page_id')) { // New page - save it to the database $this->page->set('created_by', User::get('id')); $old = Version::blank(); } else { // Get the revision before changes $old = $this->page->version; } // Was the preview button pushed? $this->preview = trim(Request::getVar('preview', '')); if ($this->preview) { // Set the component task if (!$page['id']) { Request::setVar('task', 'new'); $this->_task = 'new'; } else { Request::setVar('task', 'edit'); $this->_task = 'edit'; } // Push on through to the edit form return $this->editTask($revision); } // Check content // First, make sure the pagetext isn't empty if ($revision->get('pagetext') == '') { $this->setError(Lang::txt('COM_WIKI_ERROR_MISSING_PAGETEXT')); return $this->editTask($revision); } // Store new content if (!$this->page->save()) { $this->setError($this->page->getError()); return $this->editTask($revision); } // Get allowed authors if (!Author::setForPage(Request::getVar('authors', '', 'post'), $this->page->get('id'))) { $this->setError(Lang::txt('COM_WIKI_ERROR_SAVING_AUTHORS')); return $this->editTask($revision); } // Get the upload path $path = Attachment::blank()->filespace(); // Rename the temporary upload directory if it exist $lid = Request::getInt('lid', 0, 'post'); if ($lid != $this->page->get('id')) { if (is_dir($path . DS . $lid)) { if (!\Filesystem::move($path . DS . $lid, $path . DS . $this->page->get('id'))) { $this->setError(\Filesystem::move($path . DS . $lid, $path . DS . $this->page->get('id'))); } } foreach (Attachment::all()->whereEquals('page_id', $lid)->rows() as $attachment) { $attachment->set('page_id', $this->page->get('id')); if (!$attachment->save()) { $this->setError($attachment->getError()); } } } $revision->set('page_id', $this->page->get('id')); $revision->set('version', $revision->get('version') + 1); if ($this->page->param('mode', 'wiki') == 'knol') { // Set revisions to NOT approved $revision->set('approved', 0); // If an author or the original page creator, set to approved if ($this->page->get('created_by') == User::get('id') || $this->page->isAuthor(User::get('id'))) { $revision->set('approved', 1); } } else { // Wiki mode, approve revision $revision->set('approved', 1); } // Compare against previous revision // We don't want to create a whole new revision if just the tags were changed if (rtrim($old->get('pagetext')) != rtrim($revision->get('pagetext'))) { // Transform the wikitext to HTML $revision->set('pagehtml', ''); $revision->set('pagehtml', $revision->content($this->page)); if ($this->page->access('manage') || $this->page->access('edit')) { $revision->set('approved', 1); } // Store content if (!$revision->save()) { $this->setError(Lang::txt('COM_WIKI_ERROR_SAVING_REVISION')); return $this->editTask($revision); } $this->page->set('version_id', $revision->get('id')); $this->page->set('modified', $revision->get('created')); } else { $this->page->set('modified', Date::toSql()); } if (!$this->page->save()) { // This really shouldn't happen. $this->setError(Lang::txt('COM_WIKI_ERROR_SAVING_PAGE')); return $this->editTask($revision); } // Process tags $this->page->tag(Request::getVar('tags', '')); // Log activity $recipients = array(['wiki.site', 1], ['user', $this->page->get('created_by')], ['user', $revision->get('created_by')]); if ($this->page->get('scope') != 'site') { $recipients[] = [$this->page->get('scope'), $this->page->get('scope_id')]; $recipients[0] = ['wiki.' . $this->page->get('scope'), $this->page->get('scope_id')]; } Event::trigger('system.logActivity', ['activity' => ['action' => $page['id'] ? 'updated' : 'created', 'scope' => 'wiki.page', 'scope_id' => $this->page->get('id'), 'description' => Lang::txt('COM_WIKI_ACTIVITY_PAGE_' . ($page['id'] ? 'UPDATED' : 'CREATED'), '<a href="' . Route::url($this->page->link()) . '">' . $this->page->title . '</a>'), 'details' => array('title' => $this->page->title, 'url' => Route::url($this->page->link()), 'name' => $this->page->get('pagename'), 'revision' => $revision->get('id'))], 'recipients' => $recipients]); // Redirect App::redirect(Route::url($this->page->link())); }