public function _actionGet(KCommandContext $context) { $data = array('url' => $this->_request->url, 'content-length' => false); if (!function_exists('curl_init')) { $context->setError(new KControllerException('Curl library does not exist', KHttpResponse::SERVICE_UNAVAILABLE)); return; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $data['url']); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 20); //CURLOPT_NOBODY changes the request from GET to HEAD curl_setopt($ch, CURLOPT_NOBODY, true); $response = curl_exec($ch); if (curl_errno($ch)) { $context->setError(new KControllerException('Curl Error: ' . curl_error($ch), KHttpResponse::SERVICE_UNAVAILABLE)); return; } $info = curl_getinfo($ch); if (isset($info['http_code']) && $info['http_code'] != 200) { $context->setError(new KControllerException($data['url'] . ' Not Found', $info['http_code'])); } if (isset($info['download_content_length'])) { $data['content-length'] = $info['download_content_length']; } curl_close($ch); return json_encode($data); }
public function beforeAdd(KCommandContext $context) { $data = $context->data; $translator = $this->getService('translator')->getTranslator($this->getIdentifier()); $page = JFactory::getApplication()->getMenu()->getItem($this->getRequest()->Itemid); if (!$page) { $context->setError(new KControllerException($translator->translate('Invalid menu item.'))); return false; } foreach ($this->getModel()->getTable()->getColumns() as $key => $column) { if (!in_array($key, array('storage_type', 'title', 'description'))) { unset($data->$key); } } $data->docman_category_id = $page->params->get('category_id'); $data->enabled = $page->params->get('auto_publish') ? 1 : 0; if (empty($data->storage_type)) { $data->storage_type = $data->storage_path_remote ? 'remote' : 'file'; } if ($data->storage_type === 'file') { $file = KRequest::get('files.storage_path_file', 'raw'); if (empty($file) || empty($file['name'])) { $context->setError(new KControllerException($translator->translate('You did not select a file to be uploaded.'))); return false; } try { $controller = $this->getService('com://admin/files.controller.file', array( 'request' => array('container' => 'docman-files', 'Itemid' => $page->id) )); $this->_uploaded = $controller->add(array( 'file' => $file['tmp_name'], 'name' => $file['name'], 'folder' => $page->params->get('folder') )); $data->storage_path = $this->_uploaded->path; } catch (KControllerException $e) { $context->setError($e); return false; } } else { $data->storage_path = $data->{'storage_path_'.$data->storage_type}; } }
/** * Command handler * * Only handles before.action commands to check ACL rules. * * @param string The command name * @param object The command context * @return boolean Can return both true or false. * @throws KControllerException */ public function execute($name, KCommandContext $context) { $parts = explode('.', $name); if ($parts[0] == 'before') { $action = $parts[1]; //Check if the action exists if (!in_array($action, $context->caller->getActions())) { $context->setError(new KControllerException('Action ' . ucfirst($action) . ' Not Implemented', KHttpResponse::NOT_IMPLEMENTED)); $context->header = array('Allow' => $context->caller->execute('options', $context)); return false; } //Check if the action can be executed $method = 'can' . ucfirst($action); if (method_exists($this, $method)) { if ($this->{$method}() === false) { if ($context->action != 'options') { $context->setError(new KControllerException('Action ' . ucfirst($action) . ' Not Allowed', KHttpResponse::METHOD_NOT_ALLOWED)); $context->header = array('Allow' => $context->caller->execute('options', $context)); } return false; } } } return true; }
/** * Halts the delete if the category has documents attached to it. * * Also makes sure subcategories are deleted correctly when both * they and their parents are in the rowset to be deleted. * * @param KCommandContext $context */ public function beforeDelete(KCommandContext $context) { $data = $this->getModel()->getList(); $documents = $data->getDocumentMap(); if ($count = count($documents)) { $translator = $this->getService('translator')->getTranslator($this->getIdentifier()); $message = $translator->choose(array('This category or its children has a document attached. You first need to delete or move it before deleting this category.', 'This category or its children has %count% documents attached. You first need to delete or move them before deleting this category.'), $count, array('%count%' => $count)); $context->setError(new KControllerException($message)); return false; } /* * This removes the child categories from the rowset since they will be deleted by their parent. * If we don't do this, rowset gets confused when it tries to delete a non-existant row. */ if ($data instanceof KDatabaseRowsetInterface) { $to_be_deleted = array(); // PHP gets confused if you extract a row and then continue iterating on the rowset $iterator = clone $data; foreach ($iterator as $row) { if (in_array($row->id, $to_be_deleted)) { $data->extract($row); } $to_be_deleted += $row->getDescendants()->getColumn('id'); } } }
protected function _actionMove(KCommandContext $context) { $data = $this->getModel()->getItem(); if (!$data->isNew()) { $data->setData(KConfig::unbox($context->data)); //Only throw an error if the action explicitly failed. if ($data->move() === false) { $error = $data->getStatusMessage(); $context->setError(new KControllerException($error ? $error : 'Move Action Failed', KHttpResponse::INTERNAL_SERVER_ERROR)); } else { $context->status = $data->getStatus() === KDatabase::STATUS_CREATED ? KHttpResponse::CREATED : KHttpResponse::NO_CONTENT; } } else { $context->setError(new KControllerException('Resource Not Found', KHttpResponse::NOT_FOUND)); } return $data; }
protected function _actionPurge(KCommandContext $context) { if (!$this->getModel()->getTable()->getDatabase()->execute($this->getModel()->getPurgeQuery())) { $context->setError(new KControllerException('Delete Action Failed', KHttpResponse::INTERNAL_SERVER_ERROR)); } else { $context->status = KHttpResponse::NO_CONTENT; } }
protected function _actionRead(KCommandContext $context) { $name = ucfirst($this->getView()->getName()); if (!$this->getModel()->getState()->isUnique()) { $context->setError(new KControllerException($name . ' Not Found', KHttpResponse::NOT_FOUND)); } return parent::_actionRead($context); }
protected function _databaseBeforeCopy(KCommandContext $context) { $row = $context->caller; if (!array_intersect(array('destination_folder', 'destination_name'), $row->getModified())) { $context->setError(JText::_('Please supply a destination.')); return false; } if ($row->fullpath === $row->destination_fullpath) { $context->setError(JText::_('Source and destination are the same.')); return false; } $dest_adapter = $row->container->getAdapter($row->getIdentifier()->name, array('path' => $row->destination_fullpath)); $exists = $dest_adapter->exists(); if ($exists) { if (!$row->overwrite) { $context->setError(JText::_('Destination resource already exists.')); return false; } else { $row->overwritten = true; } } return true; }
/** * Error handler. * * @param KCommandContext $context */ public function handleErrors(KCommandContext $context) { $result = $context->result; if ($result->getStatus() !== KDatabase::STATUS_CREATED) { if (JFactory::getApplication()->getCfg('debug')) { // Notify user about error. $translator = $this->getService('com://admin/logman.translator'); $message = $translator->translate($result->getStatusMessage()); JFactory::getApplication()->enqueueMessage($translator->translate('Error while adding Activity', array('%message%' => $message)), 'notice'); } // Avoid exceptions from being thrown. $context->setError(null); } }
/** * @param KCommandContext $context * @return object */ protected function _actionSubmit(KCommandContext $context) { $data = $this->getModel()->getItem(); $row = $this->getService('com://admin/wufoo.database.row.api_entry'); $row->setData($context->data->toArray()); $row->hash = $data->hash; // Save to send the mail if ($row->save() === false) { $error = $row->getStatusMessage(); $context->setError(new KControllerException($error ? $error : 'Add Action Failed', KHttpResponse::INTERNAL_SERVER_ERROR)); } else { $context->status = KHttpResponse::CREATED; } return $row; }
public function execute($name, KCommandContext $context) { /* * For config and file controllers, we have specific checks for all actions on them */ $result = true; if ($this->_mixer->getIdentifier()->name === 'config') { $result = $this->canAdmin(); } if ($this->_mixer->getIdentifier()->name === 'file' || $this->getRequest()->routed) { if (!in_array($context->action, array('get', 'display', 'read', 'browse'))) { $result = JFactory::getUser()->authorise('com_docman.upload', 'com_docman'); } else { $result = $this->canManage() || $this->canChangeAnything(); } } if ($result === false) { $context->setError(new KControllerException('Action ' . ucfirst($context->action) . ' Not Allowed', KHttpResponse::METHOD_NOT_ALLOWED)); return false; } return parent::execute($name, $context); }
/** * Generic delete function * * @param KCommandContext A command context object * @return KDatabaseRowset A rowset object containing the deleted rows */ protected function _actionDelete(KCommandContext $context) { $data = $this->getModel()->getData(); if (count($data)) { $data->setData(KConfig::unbox($context->data)); //Only throw an error if the action explicitly failed. if ($data->delete() === false) { $error = $data->getStatusMessage(); $context->setError(new KControllerException($error ? $error : 'Delete Action Failed', KHttpResponse::INTERNAL_SERVER_ERROR)); } else { $context->status = KHttpResponse::NO_CONTENT; } } else { $context->setError(new KControllerException('Resource Not Found', KHttpResponse::NOT_FOUND)); } return $data; }
protected function _saveFiles(KCommandContext $context) { if ($context->error) { return; } $row = $context->result; $count = $this->getService('com://admin/attachments.controller.attachment')->row($row->id)->table($row->getTable()->getBase())->browse(); $count = count($count); $limit = $this->_attachment_limit; foreach ($this->_attachments as $attachment) { if ($limit !== false && $count >= $limit) { $context->setError(new KControllerException('You have reached the attachment limit for this item.')); return false; } if ($this->_saveFile($context, $attachment)) { $count++; } } return true; }
/** * Fetches an entity. * * @param object POST data */ public function fetchEntity(KCommandContext $context) { if ($context->action == 'addphoto') { if ($context->data->id) { $this->id = $context->data->id; } //clone the context so it's not touched $set = $this->__call('fetchEntity', array($context)); if (!$set) { $context->setError(null); //if the action is addphoto and there are no sets then create an set $set = $this->add($context); } return $set; } else { return $this->__call('fetchEntity', array($context)); } }