Example #1
0
 /**
  * The standard action for an Area
  */
 public final function action()
 {
     $toret = null;
     $error_number = Controller::getVar('err');
     if (!empty($error_number)) {
         Backend::addError(self::getError($error_number));
     }
     if (Controller::$debug) {
         Backend::addNotice('Checking Method ' . Controller::$action . ' for ' . get_class($this));
     }
     $request_method = strtolower(Controller::getMethod()) . '_' . Controller::$action;
     $action_method = 'action_' . Controller::$action;
     $view_method = Controller::$view->mode . '_' . Controller::$action;
     //Determine / check method
     $method = false;
     if (method_exists($this, $request_method)) {
         $method = $request_method;
     } else {
         if (method_exists($this, $action_method)) {
             $method = $action_method;
         } else {
             if (method_exists($this, $view_method)) {
                 $method = true;
             }
         }
     }
     if (!$method) {
         Controller::whoops('Unknown Method', array('message' => 'Method ' . Controller::$area . '::' . Controller::$action . ' does not exist'));
         return null;
     }
     //Check permissions on existing method
     if (Controller::getCheckPermissions() && !$this->checkPermissions()) {
         //TODO Add a permission denied hook to give the controller a chance to handle the permission denied
         Controller::whoops('Permission Denied', array('message' => 'You do not have permission to ' . Controller::$action . ' ' . get_class($this)));
         return null;
     }
     if ($method === true) {
         //View method, return null;
         return null;
     }
     if (Controller::$debug) {
         Backend::addNotice('Running ' . get_class($this) . '::' . $method);
     }
     return call_user_func_array(array($this, $method), Controller::$parameters);
 }
Example #2
0
 public function get_list($start, $count, array $options = array())
 {
     $toret = false;
     Backend::add('Sub Title', 'List');
     $obj_name = class_name(Controller::$area) . 'Obj';
     if (class_exists($obj_name, true)) {
         $object = new $obj_name();
         if ($start === 'all') {
             $object->read(array());
         } else {
             $object->read(array('limit' => "{$start}, {$count}"));
         }
         $toret = $object;
     } else {
         Controller::whoops();
     }
     return $toret;
 }
Example #3
0
 public function post_import($data = false)
 {
     $obj_name = class_name(Controller::$area) . 'Obj';
     if (!class_exists($obj_name, true)) {
         Controller::whoops();
         return false;
     }
     $object = new $obj_name();
     if (empty($_FILES) || !array_key_exists('import_file', $_FILES)) {
         Backend::addError('There is a problem with the HTML Form');
         return false;
     }
     //TODO Move this to a generic utility
     $file = $_FILES['import_file'];
     if ($file['error']) {
         switch ($file['error']) {
             case UPLOAD_ERR_INI_SIZE:
                 $msg = 'File exceeds PHP size limit';
                 break;
             case UPLOAD_ERR_FORM_SIZE:
                 $msg = 'File exceeds form size limit';
                 break;
             case UPLOAD_ERR_PARTIAL:
                 $msg = 'File unload partially uploaded';
                 break;
             case UPLOAD_ERR_NO_FILE:
                 $msg = 'No file uploaded';
                 break;
             case UPLOAD_ERR_NO_TMP_DIR:
                 $msg = 'Temporary folder doesn\'t exist';
                 break;
             case UPLOAD_ERR_CANT_WRITE:
                 $msg = 'Can\'t create temporary file';
                 break;
             case UPLOAD_ERR_EXTENSION:
                 $msg = 'File upload prohibited by PHP extension';
                 break;
         }
         Backend::addError('Could not upload file: ' . $msg);
         return false;
     }
     if (!in_array($file['type'], array('text/csv', 'application/octet-stream'))) {
         Backend::addError('This import can only handle CSV files. The uploaded file is ' . $file['type']);
         return false;
     }
     return $this->import($file['tmp_name'], $data);
 }
Example #4
0
 public static function hook_init()
 {
     //Check for any system locks
     $query = new SelectQuery('BackendLock');
     $query->field('`name`')->filter('`type` = :type')->filter('`locked` = 1')->filter('`expire` > NOW()');
     while ($lock_name = $query->fetchColumn(array(':type' => self::LOCK_SYSTEM))) {
         $lock = BackendLock::retrieve($lock_name, 'dbobject');
         if (!$lock->check()) {
             //A Lock isn't available, so the request must be aborted.
             Controller::whoops('Service Unavailable', array('message' => 'System Offline until ' . $lock->array['expire'] . '. Locked under ' . $lock->array['name'], 'code_hint' => 503));
             header('X-Backend-Lock: ' . $lock->array['name']);
             header('X-Backend-Lock-Expire: ' . $lock->array['expire']);
         }
     }
 }
Example #5
0
 public function get_display($id)
 {
     if (Backend::getDB('default')) {
         $id = Hook::run('table_display', 'pre', array($id), array('toret' => $id));
         $result = Content::retrieve($id, 'dbobject');
         if ($result instanceof DBObject && !empty($result->object)) {
             if (!$this->checkPermissions(array('subject_id' => $result->object->id, 'subject' => 'content'))) {
                 Controller::whoops(array('title' => 'Permission Denied', 'message' => 'You do not have permission to display ' . $result->object->title));
                 $result = false;
             }
         } else {
             if ($result instanceof DBObject && $id == 'last') {
                 $result->read(array('limit' => 1, 'conditions' => array('`active` = 1'), 'order' => '`added` DESC', 'mode' => 'object'));
                 if (!$result->object) {
                     $result = false;
                 }
             } else {
                 if (Permission::check('create', 'content')) {
                     Backend::addNotice('The content does not exist, but you can create it now');
                     Controller::redirect('?q=content/create/' . $id);
                     $result = false;
                 } else {
                     Controller::whoops(array('title' => 'Unknown Content', 'message' => 'The page you requested could not be found.'));
                     $result = false;
                 }
             }
         }
         if ($result && Controller::$debug) {
             Backend::addNotice('Content ID: ' . $result->object->id);
         }
         $object = Hook::run('table_display', 'post', array($result), array('toret' => $result));
         return $result;
     } else {
         //DB less content
         $template_file = array($id . '.tpl.php', str_replace('/', '.', $id) . '.tpl.php');
         if (Render::checkTemplateFile($template_file[0])) {
             Backend::addContent(Render::file($template_file[0]));
         } else {
             if (Render::checkTemplateFile($template_file[1])) {
                 Backend::addContent(Render::file($template_file[1]));
             } else {
                 Backend::addContent('Could not find file');
             }
         }
         return true;
     }
 }
Example #6
0
 public static function display(AreaCtl $controller, $result)
 {
     if (!self::$view instanceof View) {
         Controller::whoops('Invalid View', array('message' => 'The requested mode doesn\'t have a valid associated View.', 'code_hint' => 406));
         return null;
     }
     Hook::run('action_display', 'pre', array($result));
     self::$view->display($result, $controller);
     Hook::run('action_display', 'post', array($result));
 }