public function assert(Acl $acl, $role = null, $resource = null, $privilege = null) { foreach ($this->_arguments as $role_key => $resource_key) { // normalize arguments & compare $allow_same = TRUE; if (strpos($role_key, '!') === 0) { $role_key = substr($role_key, 1); $allow_same = !$allow_same; } if (strpos($resource_key, '!') === 0) { $resource_key = substr($resource_key, 1); $allow_same = !$allow_same; } if (Mango::normalize($role->{$role_key}) === Mango::normalize($resource->{$resource_key})) { if (!$allow_same) { // fields are the same, not allowed return FALSE; } } else { if ($allow_same) { // fields are different, not allowed return FALSE; } } } return TRUE; }
public static function build($document, array $post) { if (is_string($document)) { $document = Mango::factory($document); } foreach ($document->fields() as $name => $data) { switch ($data['type']) { case 'boolean': $post[$name] = isset($post[$name]); break; } } foreach ($post as $name => $value) { if ($field = $document->field($name)) { switch ($field['type']) { case 'set': $value = is_string($value) && $value !== '' ? explode(',', $value) : NULL; break; } } else { continue; } $document->__set($name, $value); } return $document; }
public function update($criteria = array(), $safe = TRUE) { if (isset($this->_changed['password'])) { $this->password = $this->hash($this->password); } return parent::update($criteria, $safe); }
public function loop(array $config) { if ($this->_auto_terminate && memory_get_usage() > $this->_auto_terminate) { Kohana::$log->add(Log::WARNING, "Queue autoterminating. Memory usage (:usage bytes) higher than limit (:limit bytes).", array(":usage" => memory_get_usage(), ":limit" => $this->_auto_terminate)); return FALSE; } else { if (count($this->_pids) < $this->_max_tasks) { try { // find next task $task = Mango::factory('task')->get_next(); } catch (MongoException $e) { Kohana::$log->add(Log::ERROR, Kohana_Exception::text($e)); Kohana::$log->add(Log::ERROR, 'Error loading next task. Exiting'); return FALSE; } if ($task->loaded()) { // Write log to prevent memory issues Kohana::$log->write(); // close all database connections before forking foreach (MangoDB::$instances as $instance) { $instance->disconnect(); } $pid = pcntl_fork(); if ($pid == -1) { // error forking Kohana::$log->add(Log::ERROR, 'Queue. Could not spawn child task process.'); Kohana::$log->write(); exit; } else { if ($pid) { // parent process $this->_pids[$pid] = time(); } else { // child process $task->execute(); exit(0); } } } } } return TRUE; }
/** * Find a path in array * * @param string|array delimiter notated keystring or array * @param mixed default value to return if key not found * @param string delimiter (defaults to dot '.') * @return mixed value (if found) or default value */ public function path_get($path, $default = NULL, $delimiter = '.') { if (!is_array($path)) { $path = explode($delimiter, (string) $path); } $next = $this; while (count($path) && (is_array($next) || $next instanceof ArrayObject)) { $key = array_shift($path); $next = isset($next[$key]) ? $next[$key] : $default; } return !count($path) ? Mango::normalize($next) : $default; }
public function action_demo13() { $user = Mango::factory('user', array('role' => 'viewer', 'email' => '*****@*****.**'))->create(); $group = Mango::factory('group', array('name' => 'wouter'))->create(); $user->add($group, 'circle'); echo Debug::vars($user->changed(TRUE), $group->changed(TRUE)); $user->update(); $group->update(); $group->reload(); $user->reload(); foreach ($group->users as $user) { echo Debug::vars($user->as_array(FALSE)); } foreach ($user->circles as $circle) { echo Debug::vars($circle->as_array(FALSE)); } $group->remove($user); echo Debug::vars($user->changed(TRUE), $group->changed(TRUE)); $user->delete(); $group->delete(); }
public function create($safe = TRUE) { $this->car_type = $this->_car_type; return parent::create($safe); }
/** * Constructor * * @param Mango $mango */ public function __construct(Mango $mango) { $this->connection = $mango->getConnection(); $mango::setDocumentManager($this); }
/** * Formats a value into correct a valid value for the specific field * * @param string field name * @param mixed field value * @param boolean is value clean (from Db) * @return mixed formatted value */ protected function load_field($name, $value, $clean = FALSE) { // Load field data $field = $this->_fields[$name]; if (!$clean) { // Apply filters $value = $this->run_filters($name, $value); // Empty string if (is_string($value) && $value === '') { $value = NULL; } } if ($value !== NULL || $clean === TRUE) { switch ($field['type']) { case 'MongoId': if ($value !== NULL and !$value instanceof MongoId) { $value = new MongoId($value); } break; case 'date': if (!$value instanceof MongoDate) { $value = new MongoDate(is_int($value) ? $value : strtotime($value)); } break; case 'enum': if ($clean) { $value = isset($field['values'][$value]) ? $value : NULL; } else { $value = ($key = array_search($value, $field['values'])) !== FALSE ? $key : NULL; } break; case 'int': if ((double) $value > PHP_INT_MAX) { // This number cannot be represented by a PHP integer, so we convert it to a float $value = (double) $value; } else { $value = (int) $value; } break; case 'float': $value = (double) $value; break; case 'timestamp': if (!is_int($value)) { $value = ctype_digit($value) ? (int) $value : strtotime($value); } break; case 'boolean': $value = (bool) $value; break; case 'email': if (!$clean) { $value = strtolower(trim((string) $value)); } break; case 'string': $value = trim((string) $value); if (!$clean && strlen($value)) { $max_size = Arr::get($field, 'max_size', Mango::MAX_SIZE_STRING); if (UTF8::strlen($value) > $max_size) { $value = UTF8::substr($value, 0, $max_size); } if (Arr::get($field, 'xss_clean') && !empty($value)) { $value = Security::xss_clean($value); } } break; case 'has_one': if (is_array($value)) { $value = Mango::factory($field['model'], $value, $clean ? Mango::CLEAN : Mango::CHANGE); } if (!$value instanceof Mango) { $value = NULL; } else { $value->set_parent($this); } break; case 'has_many': $value = new Mango_Set($value, $field['model'], Arr::get($field, 'duplicates', FALSE), $clean); foreach ($value as $model) { $model->set_parent($this); } break; case 'counter': $value = new Mango_Counter($value); break; case 'array': $value = new Mango_Array($value, Arr::get($field, 'type_hint'), $clean); break; case 'set': $value = new Mango_Set($value, Arr::get($field, 'type_hint'), Arr::get($field, 'duplicates', TRUE), $clean); break; case 'mixed': $value = !is_object($value) ? $value : NULL; break; } if (!$clean && is_string($value) && $value === '') { $value = NULL; } } return $value; }
/** * Iterator: current */ public function current() { return Mango::factory($this->_model, $this->_cursor->current(), Mango::CLEAN); }
public function update($criteria = array(), $options = array()) { $this->updated = time(); return parent::update($criteria, $options); }
/** * Loading user by name * * @param string $username * @return object / NULL */ public function load_user($username) { $user = Mango::factory($this->_config['user_model'], array($this->_config['columns']['username'] => $username, 'account_id' => Request::$account->_id))->load(); // @TODO: do the right thing here return NULL; }
protected function daemon() { while (!$this->_sigterm) { if (count($this->_pids) < $this->_config['max']) { try { // find next task $task = Mango::factory('task')->get_next(); } catch (MongoException $e) { Kohana::$log->add($this->_config['log']['error'], Kohana_Exception::text($e)); Kohana::$log->add($this->_config['log']['error'], 'Error loading next task. Exiting'); Kohana::$log->write(); $this->clean(); exit; } if ($task->loaded()) { if (!$task->valid()) { // invalid tasks are discarded immediately Kohana::$log->add($this->_config['log']['error'], strtr('Discarded invalid :type task"', array(':type' => $task->type))); $task->delete(); } else { // Write log to prevent memory issues Kohana::$log->write(); $pid = $this->fork(); if ($pid == -1) { Kohana::$log->add($this->_config['log']['error'], 'Queue. Could not spawn child task process.'); exit; } elseif ($pid) { // Parent - add the child's PID to the running list $this->_pids[$pid] = time(); } else { $task->execute($this->_config); exit; } } } else { // queue is empty usleep($this->_config['sleep']); } } else { // daemon is busy usleep($this->_config['sleep']); } } // clean up $this->clean(); }
protected function daemon() { while (!$this->_sigterm) { // Find first task that is not being executed $task = Mango::factory('task')->load(1, array('_id' => 1), array(), array('e' => array('$exists' => FALSE))); if ($task->loaded() && count($this->_pids) < $this->_config['max']) { // Task found // Update task status $task->e = TRUE; $task->update(); // Write log to prevent memory issues Kohana::$log->write(); // Fork process to execute task $pid = pcntl_fork(); if ($pid == -1) { Kohana::$log->add('error', 'Queue. Could not spawn child task process.'); exit; } elseif ($pid) { // Parent - add the child's PID to the running list $this->_pids[$pid] = time(); } else { try { // Child - Execute task Request::factory(Route::get($task->route)->uri($task->uri->as_array()))->execute(); } catch (Exception $e) { // Task failed - log message Kohana::$log->add('error', strtr('Queue. Task failed - route: :route, uri: :uri, msg: :msg', array(':route' => $task->route, ':uri' => http_build_query($task->uri->as_array()), ':msg' => $e->getMessage()))); } // Remove task from queue $task->delete(); exit; } } else { // No task in queue - sleep usleep($this->_config['sleep']); } } // clean up $this->clean(); }
/** * Garbage Collector to delete old sessions */ protected function _gc() { if ($this->_lifetime) { $expires = $this->_lifetime; } else { $expires = Date::MONTH; } // Delete old sessions Mango::factory('session')->db()->remove('sessions', array('last_active' => array('$lt' => time() - $expires))); }
public function action_demo12() { $this->template->bind('content', $content); $content = ''; // Create blog $blog = Mango::factory('blog', array('title' => 'title1', 'text' => 'text1', 'time_written' => time()))->create(); // Unset a field unset($blog->title); echo Kohana::debug('Unset title', $blog->changed(true)); // Save $blog->update(); // Reload blog $blog->reload(); // Check if title is really missing echo Kohana::debug('Title should be missing', $blog->as_array()); // Add 2 comments $blog->add(Mango::factory('comment', array('name' => 'Name1', 'comment' => 'Text1', 'time' => time()))); $blog->add(Mango::factory('comment', array('name' => 'Name2', 'comment' => 'Text2', 'time' => time()))); // This should use pushAll echo Kohana::debug('Adding comments using $pushAll', $blog->changed(TRUE)); $blog->update(); $blog->reload(); $blog->comments[0]->comment = 'New Text'; echo Kohana::debug('Update using array indices', $blog->changed(TRUE)); $blog->update(); $blog->reload(); echo Kohana::debug('New text in first comment', $blog->as_array()); // Clean up $blog->delete(); }
/** * Loads the user object from database using username * * @param string username * @return object User Object */ protected function _load_user($username) { return Mango::factory($this->_config['user_model'], array($this->_config['columns']['username'] => $username))->load(); }