Example #1
0
 public function html_element()
 {
     // Import base data
     $data = $this->data;
     // Get the options and default selection
     $time = $this->time_array(arr::remove('value', $data));
     // No labels or values
     unset($data['label']);
     $input = '';
     foreach ($this->parts as $type => $val) {
         if (is_int($type)) {
             // Just add the separators
             $input .= $val;
             continue;
         }
         // Set this input name
         $data['name'] = $this->data['name'] . '[' . $type . ']';
         // Set the selected option
         $selected = $time[$type];
         if ($type == 'am_pm') {
             // Options are static
             $options = array('AM' => 'AM', 'PM' => 'PM');
         } else {
             // minute(s), hour(s), etc
             $type .= 's';
             // Use the date helper to generate the options
             $options = empty($val) ? date::$type() : call_user_func_array(array('date', $type), $val);
         }
         $input .= form::dropdown($data, $options, $selected);
     }
     return $input;
 }
Example #2
0
 protected static function tiers_POST($id, $envelope)
 {
     if (is_null($id)) {
         self::throwErrorAndDie('Invalid request', array($id), 410);
     }
     $data = self::requireData($envelope);
     $tier_agents = array();
     if ($agents = arr::get($data, 'agents')) {
         foreach ($agents as $agent) {
             if ($tier_agent_id = arr::get($agent, 'tier_agent_id')) {
                 $tier_agent = Doctrine::getTable('TierAgent')->findOneBy('tier_agent_id', $tier_agent_id);
             } else {
                 $tier_agent = new TierAgent();
             }
             try {
                 $tier_agent->synchronizeWithArray($agent);
                 $tier_agent->save();
                 $tier_agents[] = $tier_agent->toArray();
             } catch (Exception $e) {
                 self::throwErrorAndDie('Invalid data', Bluebox_Controller::$validation->errors(), 400);
             }
         }
         arr::remove('agents', $data);
         arr::merge($envelope['data'], $data);
     }
     $response = self::generalAPI_POST($id, 'tier_id', 'Tier', $envelope);
     $response['agents'] = $tier_agents;
     return $response;
 }
 public function html_element()
 {
     // Import base data
     $base_data = $this->data;
     unset($base_data['label']);
     // Get the options and default selection
     $options = arr::remove('options', $base_data);
     $selected = arr::remove('selected', $base_data);
     return form::dropdown($base_data, $options, $selected);
 }
Example #4
0
 /**
  * Get Notice Array
  *
  * Either returns the entire notice array and resets it,
  * or returns and removes a specific type of notice from
  * the array.
  *
  * @param string - Type of notice to return
  * @return array - The notice array
  */
 public static function get_array($type = NULL)
 {
     if ($type === NULL) {
         $notices = self::$notices;
         self::$notices = array();
         return $notices;
     } else {
         return arr::remove($type, self::$notices);
     }
 }
Example #5
0
 /**
  * Creates and returns a form view object based on the defined inputs,
  * rules, and callbacks. ORM::validate is used as the validation method.
  *
  * Error i18n files follow the format: form_model_name, eg: Model_User would
  * use form_user for errors.
  *
  * @param   array    values array to validate
  * @param   boolean  YES to save the model, or a URI to redirect, on success
  * @return  View
  */
 public function form(array &$array, $save = NO)
 {
     $array = Validation::factory($array)->pre_filter('trim');
     foreach ($this->rules as $column => $rules) {
         foreach ($rules as $rule) {
             $array->add_rules($column, $rule);
         }
     }
     foreach ($this->callbacks as $column => $rules) {
         foreach ($rules as $rule) {
             $array->add_callbacks($column, $rule);
         }
     }
     // Load the form
     $form = View::factory($this->view)->set('action', Router::$current_uri)->set('cancel', Router::$current_uri)->set('attributes', array())->bind('inputs', $inputs)->bind('errors', $errors);
     if (!$this->validate($array, $save)) {
         // Load errors
         $errors = $array->errors('form_' . $this->object_name);
     }
     $inputs = array();
     foreach ($this->inputs as $name => $data) {
         if (is_int($name)) {
             $name = $data;
             $data = nil;
         } else {
             if (isset($data['type']) and $data['type'] === 'dropdown') {
                 if (isset($data['options']) and !is_array($data['options'])) {
                     list($model, $attr) = arr::callback_string($data['options']);
                     // Generate a list of options
                     $data['options'] = ORM::factory($model)->select_list($attr[0], $attr[1]);
                 }
                 if (!isset($data['selected'])) {
                     $data['selected'] = $array[$name];
                 }
             } elseif (isset($data['type']) and $data['type'] === 'upload') {
                 // Form must be multi-part
                 $attributes['enctype'] = 'multipart/form-data';
             } else {
                 $data['value'] = $array[$name];
             }
         }
         if (!isset($data['name'])) {
             // Set input name
             $data['name'] = $name;
         }
         if (!isset($data['title'])) {
             // Set field title
             $data['title'] = ucfirst($name);
         }
         // Add the column to the inputs
         $inputs[arr::remove('title', $data)] = $data;
     }
     return $form;
 }
Example #6
0
 /**
  * Creates a temporary image and executes the given actions. By creating a
  * temporary copy of the image before manipulating it, this process is atomic.
  */
 public function process($image, $actions, $dir, $file, $render = FALSE, $background = NULL)
 {
     // We only need the filename
     $image = $image['file'];
     // Unique temporary filename
     $this->tmp_image = $dir . 'k2img--' . sha1(time() . $dir . $file) . substr($file, strrpos($file, '.'));
     // Copy the image to the temporary file
     copy($image, $this->tmp_image);
     // Quality change is done last
     $quality = (int) arr::remove('quality', $actions);
     // Use 95 for the default quality
     empty($quality) and $quality = 95;
     if (is_string($background)) {
         // Set the background color
         $this->background = escapeshellarg($background);
     } else {
         // Use a transparent background
         $this->background = 'transparent';
     }
     // All calls to these will need to be escaped, so do it now
     $this->cmd_image = escapeshellarg($this->tmp_image);
     $this->new_image = $render ? $this->cmd_image : escapeshellarg($dir . $file);
     if ($status = $this->execute($actions)) {
         // Use convert to change the image into its final version. This is
         // done to allow the file type to change correctly, and to handle
         // the quality conversion in the most effective way possible.
         if ($error = exec(escapeshellcmd($this->dir . 'convert' . $this->ext) . ' -background ' . $this->background . ' -flatten -quality ' . $quality . '% ' . $this->cmd_image . ' ' . $this->new_image)) {
             $this->errors[] = $error;
         } else {
             // Output the image directly to the browser
             if ($render === TRUE) {
                 $contents = file_get_contents($this->tmp_image);
                 switch (substr($file, strrpos($file, '.') + 1)) {
                     case 'jpg':
                     case 'jpeg':
                         header('Content-Type: image/jpeg');
                         break;
                     case 'gif':
                         header('Content-Type: image/gif');
                         break;
                     case 'png':
                         header('Content-Type: image/png');
                         break;
                 }
                 echo $contents;
             }
         }
     }
     // Remove the temporary image
     unlink($this->tmp_image);
     $this->tmp_image = '';
     return $status;
 }
Example #7
0
 public function html_element()
 {
     // Import base data
     $base_data = $this->data;
     unset($base_data['label']);
     if (isset($base_data['multiple']) && !!$base_data['multiple']) {
         $base_data['name'] = str_replace('[]', '', $base_data['name']) . '[]';
     }
     // Get the options and default selection
     $options = arr::remove('options', $base_data);
     $selected = arr::remove('selected', $base_data);
     return form::dropdown($base_data, $options, $selected);
 }
 public function save()
 {
     if (request::is_ajax() && request::method() === 'post') {
         $this->auto_render = FALSE;
         $post = security::xss_clean($this->input->post());
         $officeBudget = arr::remove('currentBudget', $post);
         if ($post['status'] == 'Approved') {
             $this->budget_model->updateBudget($post['office_id'], $officeBudget);
             echo $this->request_model->insert($post);
         }
         $this->request_model->insert($post);
         log_helper::add("1", $this->user_log, $this->user_id, "Added New Request");
     }
 }
Example #9
0
 protected function html_element()
 {
     // Import the data
     $data = $this->data;
     if (empty($data['checked'])) {
         // Not checked
         unset($data['checked']);
     } else {
         // Is checked
         $data['checked'] = 'checked';
     }
     if ($label = arr::remove('label', $data)) {
         // There must be one space before the text
         $label = ' ' . ltrim($label);
     }
     return '<label>' . form::input($data) . $label . '</label>';
 }
 public function save()
 {
     if (request::is_ajax() and request::method() === 'post') {
         $this->auto_render = FALSE;
         $office_id = Auth::instance()->get_user()->office_id;
         $post = security::xss_clean($this->input->post());
         $post['office_id'] = $office_id;
         $post['status'] = 'Received';
         $officeBudget = arr::remove('currentBudget', $post);
         if ($post['status'] == 'Approved') {
             $this->budget_model->updateBudget($post['office_id'], $officeBudget);
             echo $this->request_model->insert($post);
         }
         log_helper::add("1", $this->user_log, $this->user_id, "Requested a Budget");
         $this->request_model->insert($post);
     }
 }
Example #11
0
 public function save()
 {
     if (request::is_ajax() and request::method() === 'post') {
         $this->auto_render = FALSE;
         $post = security::xss_clean($this->input->post());
         // echo $this->auth->hash("admin").'<br />';
         // echo $this->auth->hash_password("admin");
         // die();
         $role = arr::remove('role', $post);
         foreach ($post as $key => $value) {
             $this->user_model->{$key} = $value;
         }
         $this->user_model->add(ORM::factory('role', 'login'));
         $this->user_model->add(ORM::factory('role', $role));
         log_helper::add("1", $this->user_log, $this->user_id, "Added New " . $role . " User.");
         echo $this->user_model->save();
     }
 }
Example #12
0
 public function save()
 {
     if ($this->form->validate() and $data = $this->form->as_array()) {
         if (empty($data['password'])) {
             // Remove the empty password so it's not reset
             unset($data['password'], $data['confirm']);
         }
         // Need to set this before saving
         $new_user = $this->object->id == 0;
         // Remove the roles from data
         isset($data['roles']) and $roles = arr::remove('roles', $data);
         foreach ($data as $field => $val) {
             // Set object data from the form
             $this->{$field} = $val;
         }
         if ($status = parent::save()) {
             // if ($new_user)
             // {
             // 	foreach ($roles as $role)
             // 	{
             // 		// Add the user roles
             // 		$this->add_role($role);
             // 	}
             // }
             // else
             // {
             // 	foreach (array_diff($this->roles, $roles) as $role)
             // 	{
             // 		// Remove roles that were deactivated
             // 		$this->remove_role($role);
             // 	}
             //
             // 	foreach (array_diff($roles, $this->roles) as $role)
             // 	{
             // 		// Add new roles
             // 		$this->add_role($role);
             // 	}
             // }
         }
         // Return the save status
         return $status;
     }
     return FALSE;
 }
 public function __call($method, $args)
 {
     // Concat all the arguments into a filepath
     array_unshift($args, $method);
     $path = join('/', $args);
     // Loop through the routes and see if anything matches
     foreach ((array) Kohana::config('assets', FALSE, FALSE) as $key => $val) {
         if (preg_match('#^' . $key . '$#u', $path)) {
             // If the supplied value is a config array ...
             if (is_array($val)) {
                 // ... get the mapped route ...
                 $route = arr::remove('route', $val);
                 // ... and apply the rest of the config settings
                 $this->apply_config($val);
             } else {
                 $route = $val;
             }
             if (strpos($route, '$') !== FALSE) {
                 // Use regex routing
                 $routed_path = preg_replace('#^' . $key . '$#u', $route, $path);
             } else {
                 // Standard routing
                 $routed_path = $route;
             }
             // A valid route has been found
             break;
         }
     }
     // If no matching route is found, then 404
     if (!isset($routed_path)) {
         Event::run('system.404');
     }
     $pathinfo = pathinfo($routed_path);
     $directories = explode('/', $pathinfo['dirname']);
     $directory = array_shift($directories);
     $path = join('/', $directories) . '/' . $pathinfo['filename'];
     // Search for file using cascading file system, 404 if not found
     $file = Kohana::find_file($directory, $path, FALSE, $pathinfo['extension']);
     if (!$file) {
         Event::run('system.404');
     }
     readfile($file);
 }
 public function save()
 {
     if (request::is_ajax() && request::method() === 'post') {
         $this->auto_render = FALSE;
         $post = security::xss_clean($this->input->post());
         $request_id = arr::remove('request_id', $post);
         foreach ($post as $key => $value) {
             $this->transaction_model->{$key} = $value;
         }
         $budget = $this->budget_model->where('office_id', $post['office_id'])->find();
         $budget_left = (double) $budget->amount_left - (double) $post['amount_paid'];
         $this->budget_model->updateBudget($post['office_id'], $budget_left);
         $request = $this->request_model->find($request_id);
         $request->grand_total = $post['amount_left'];
         $request->save();
         log_helper::add("1", $this->user_log, $this->user_id, "Added New Transaction.");
         echo $this->transaction_model->save();
     }
 }
 public function save()
 {
     if (request::is_ajax() && request::method() === 'post') {
         $this->auto_render = FALSE;
         $post = security::xss_clean($this->input->post());
         $items = $post['items'];
         arr::remove('items', $post);
         foreach ($post as $key => $value) {
             $this->purchase_model->{$key} = $value;
         }
         $last_purchase = $this->purchase_model->save();
         if ($last_purchase->id) {
             $i = 0;
             foreach ($items as $key => $value) {
                 $items[$key]['purchase_id'] = $last_purchase->id;
                 $this->item_stock_model->insert($items[$i]);
                 $i++;
             }
         }
     }
 }
Example #16
0
 public function render()
 {
     // Import base data
     $base_data = $this->data;
     // Make it an array
     $base_data['name'] .= '[]';
     // Newline
     $nl = "\n";
     $checklist = '<ul class="' . arr::remove('class', $base_data) . '">' . $nl;
     foreach (arr::remove('options', $base_data) as $val => $opt) {
         // New set of input data
         $data = $base_data;
         // Get the title and checked status
         list($title, $checked) = $opt;
         // Set the name, value, and checked status
         $data['value'] = $val;
         $data['checked'] = $checked;
         $checklist .= '<li><label>' . form::checkbox($data) . ' ' . html::purify($title) . '</label></li>' . $nl;
     }
     $checklist .= '</ul>';
     return $checklist;
 }
Example #17
0
?>
<h4><?php 
echo $class;
echo $static ? ' :: ' : ' -> ';
echo $name;
?>
</h4>

<code class="declaration"><?php 
echo $declaration;
?>
</code>

<?php 
if (!empty($comment['about'])) {
    echo arr::remove('about', $comment);
}
if (!empty($parameters)) {
    ?>
<p class="parameters"><strong>Parameters:</strong></p>
<dl>
<?php 
    foreach ($parameters as $i => $param) {
        if (!empty($comment['param'][$i])) {
            // Extract the type and information
            list($type, $info) = explode(' ', $comment['param'][$i], 2);
            $type = Kodoc::humanize_type($type) . ' ';
            $info = trim($info);
        } else {
            $type = '';
            $info = '';
Example #18
0
 /**
  * Creates an HTML form button input tag.
  *
  * @param   string|array  input name or an array of HTML attributes
  * @param   string        input value, when using a name
  * @param   string        a string to be attached to the end of the attributes
  * @return  string
  */
 public static function button($data = '', $value = '', $extra = '')
 {
     if (!is_array($data)) {
         $data = array('name' => $data);
     }
     if (empty($data['name'])) {
         // Remove the name if it is empty
         unset($data['name']);
     }
     if (isset($data['value']) and empty($value)) {
         $value = arr::remove('value', $data);
     }
     return '<button' . form::attributes($data, 'button') . ' ' . $extra . '>' . $value . '</button>';
 }
Example #19
0
 /**
  * Creates the form HTML
  *
  * @param   string   form view template name
  * @param   boolean  use a custom view
  * @return  string
  */
 public function render($template = 'forge_template', $custom = FALSE)
 {
     // Load template
     $form = new View($template);
     if ($custom) {
         // Using a custom view
         $data = array();
         foreach (array_merge($this->hidden, $this->inputs) as $input) {
             $data[$input->name] = $input;
             // Groups will never have errors, so skip them
             if ($input instanceof Form_Group) {
                 continue;
             }
             // Compile the error messages for this input
             $messages = '';
             $errors = $input->error_messages();
             if (is_array($errors) and !empty($errors)) {
                 foreach ($errors as $error) {
                     // Replace the message with the error in the html error string
                     $messages .= str_replace('{message}', $error, $this->error_format) . $this->newline_char;
                 }
             }
             $data[$input->name . '_errors'] = $messages;
         }
         $form->set($data);
     } else {
         // Using a template view
         $form->set($this->template);
         $hidden = array();
         if (!empty($this->hidden)) {
             foreach ($this->hidden as $input) {
                 $hidden['name'] = $input->name;
                 $hidden['value'] = $input->value;
             }
         }
         $form_type = 'open';
         // See if we need a multipart form
         $check_inputs = array($this->inputs);
         while ($check_inputs) {
             foreach (array_shift($check_inputs) as $input) {
                 if ($input instanceof Form_Upload) {
                     $form_type = 'open_multipart';
                 }
                 if ($input instanceof Form_Group) {
                     $check_inputs += array($input->inputs);
                 }
             }
         }
         // Set the form open and close
         $form->open = form::$form_type(arr::remove('action', $this->attr), $this->attr, $hidden);
         $form->close = "</form>";
         // Set the inputs
         $form->inputs = $this->inputs;
     }
     return $form;
 }
Example #20
0
 public function login_by_email()
 {
     $login_config = Kohana::config('login');
     if ($this->auth->logged_in()) {
         $this->template->title = 'Already Logged In';
         $this->template->content = new View('login/login_message');
         $this->template->content->message = 'You are already logged in.';
         $this->template->content->link_to_home = 'YES';
         $this->template->content->link_to_logout = 'YES';
         return;
     }
     $this->build_template('login_by_email');
     if ($login_config['login_by_email'] != 'YES') {
         $this->template->content->link_to_username = '******';
     }
     if (request::method() == 'post') {
         # this is name complete as needs to convert from email address to username
         # or to extend auth model
         $person = ORM::factory('person')->like('email_address', $_POST['Email'], false)->find();
         if ($this->auth->login(array('person_id' => $person->id), $_POST['Password'], isset($_POST['remember_me']))) {
             $user = new User_Model($_SESSION['auth_user']->id);
             $user->__set('forgotten_password_key', NULL);
             $user->save();
             url::redirect(arr::remove('requested_page', $_SESSION));
             return;
         }
         $this->template->content->error_message = 'Invalid Email address/Password Combination, or insufficient privileges';
     }
 }
 /**
  * Tests the arr::remove() function.
  * @dataProvider remove_provider
  * @group core.helpers.arr.remove
  * @test
  */
 public function remove($input_key, $input_array, $expected_result, $expected_result2)
 {
     $result = arr::remove($input_key, $input_array);
     $this->assertEquals($expected_result, $input_array);
     $this->assertEquals($expected_result2, $result);
 }
Example #22
0
 /**
  * Creates the form HTML
  *
  * @param   string   form view template name
  * @param   boolean  use a custom view
  * @return  string
  */
 public function render($template = 'formation/wrapper', $custom = NO)
 {
     // Load template
     $form = new View($template);
     if ($custom) {
         // Using a custom view
         $data = array();
         foreach (array_merge($this->hidden, $this->inputs) as $input) {
             $data[$input->name] = $input;
             // Groups will never have errors, so skip them
             if ($input instanceof Formation_Group) {
                 continue;
             }
             // Compile the error messages for this input
             $messages = '';
             $errors = $input->error_messages();
             if (is_array($errors) and !empty($errors)) {
                 foreach ($errors as $error) {
                     // Replace the message with the error in the html error string
                     $messages .= str_replace('{message}', $error, $this->error_format) . $this->newline_char;
                 }
             }
             $data[$input->name . '_errors'] = $messages;
         }
         $form->set($data);
     } else {
         // Using a template view
         $form->set($this->template);
         $hidden = array();
         if (!empty($this->hidden)) {
             foreach ($this->hidden as $input) {
                 $hidden[$input->name] = $input->value;
             }
         }
         $form_type = 'open';
         // See if we need a multipart form
         foreach ($this->inputs as $input) {
             if ($input instanceof Formation_Upload) {
                 $form_type = 'open_multipart';
             }
         }
         // Tack on the "formation" class
         $space = str::e($this->attr['class']) ? '' : ' ';
         $this->attr['class'] = 'formation' . $space . $this->attr['class'];
         // Set the form open and close
         $form->open = form::$form_type(arr::remove('action', $this->attr), $this->attr, $hidden);
         $form->close = form::close();
         // Set the inputs
         $form->inputs = $this->inputs;
     }
     return $form;
 }
Example #23
0
 public function process($image, $actions, $dir, $file, $render = FALSE)
 {
     // Set the "create" function
     switch ($image['type']) {
         case IMAGETYPE_JPEG:
             $create = 'imagecreatefromjpeg';
             break;
         case IMAGETYPE_GIF:
             $create = 'imagecreatefromgif';
             break;
         case IMAGETYPE_PNG:
             $create = 'imagecreatefrompng';
             break;
     }
     // Set the "save" function
     switch (strtolower(substr(strrchr($file, '.'), 1))) {
         case 'jpg':
         case 'jpeg':
             $save = 'imagejpeg';
             break;
         case 'gif':
             $save = 'imagegif';
             break;
         case 'png':
             $save = 'imagepng';
             break;
     }
     // Make sure the image type is supported for import
     if (empty($create) or !function_exists($create)) {
         throw new Kohana_Exception('image.type_not_allowed', $image['file']);
     }
     // Make sure the image type is supported for saving
     if (empty($save) or !function_exists($save)) {
         throw new Kohana_Exception('image.type_not_allowed', $dir . $file);
     }
     // Load the image
     $this->image = $image;
     // Create the GD image resource
     $this->tmp_image = $create($image['file']);
     // Get the quality setting from the actions
     $quality = arr::remove('quality', $actions);
     if ($status = $this->execute($actions)) {
         // Prevent the alpha from being lost
         imagealphablending($this->tmp_image, TRUE);
         imagesavealpha($this->tmp_image, TRUE);
         switch ($save) {
             case 'imagejpeg':
                 // Default the quality to 95
                 $quality === NULL and $quality = 95;
                 break;
             case 'imagegif':
                 // Remove the quality setting, GIF doesn't use it
                 unset($quality);
                 break;
             case 'imagepng':
                 // Always use a compression level of 9 for PNGs. This does not
                 // affect quality, it only increases the level of compression!
                 $quality = 9;
                 break;
         }
         if ($render === FALSE) {
             // Set the status to the save return value, saving with the quality requested
             $status = isset($quality) ? $save($this->tmp_image, $dir . $file, $quality) : $save($this->tmp_image, $dir . $file);
         } else {
             // Output the image directly to the browser
             switch ($save) {
                 case 'imagejpeg':
                     header('Content-Type: image/jpeg');
                     break;
                 case 'imagegif':
                     header('Content-Type: image/gif');
                     break;
                 case 'imagepng':
                     header('Content-Type: image/png');
                     break;
             }
             $status = isset($quality) ? $save($this->tmp_image, NULL, $quality) : $save($this->tmp_image);
         }
         // Destroy the temporary image
         imagedestroy($this->tmp_image);
     }
     return $status;
 }
Example #24
0
 /**
  * Validate CSRF token
  *
  * @param   string  $token
  * @return  bool
  */
 public static function token($token)
 {
     // require token to be in session and remove after use
     return !empty($token) && !empty($_SESSION['token']) && $token == arr::remove('token', $_SESSION['token']);
 }
Example #25
0
 /**
  * Return formatted <time> tag
  *
  * @param  string        $str
  * @param  array|string  $attributes  handled as time if not an array
  * @param  boolean       $short       use only date
  */
 public static function time($str, $attributes = null, $short = false)
 {
     // Extract datetime
     $datetime = is_array($attributes) ? arr::remove('datetime', $attributes) : $attributes;
     if ($datetime) {
         $time = is_int($datetime) ? $datetime : strtotime($datetime);
         $datetime = date::format($short ? date::DATE_8601 : date::TIME_8601, $time);
         if (is_array($attributes)) {
             $attributes['datetime'] = $datetime;
         } else {
             $attributes = array('datetime' => $datetime);
         }
         // Set title if not the same as content
         if (!isset($attributes['title'])) {
             $title = date::format($short ? 'DMYYYY' : 'DMYYYY_HM', $time);
             if ($title != $str) {
                 $attributes['title'] = date::format($short ? 'DMYYYY' : 'DMYYYY_HM', $time);
             }
         }
     }
     return '<time' . html::attributes($attributes) . '>' . $str . '</time>';
 }
Example #26
0
 public static function valid($token)
 {
     return $token === arr::remove('csrf', $_SESSION);
 }
Example #27
0
 public function save()
 {
     $user = new User_Model($_POST['id']);
     if (!$user->loaded) {
         $this->template->title = 'New Password Invocation Error';
         $this->template->content = new View('login/login_message');
         $this->template->content->message = 'Invalid user id.';
         return;
     }
     $username = $user->username;
     $password = $_POST['password'];
     $password2 = $_POST['password2'];
     $email_key = $_POST['email_key'];
     $person = ORM::factory('person', $user->person_id);
     if ($email_key != '') {
         /* if the email_key field is filled in, then being called from a forgotten password email */
         if ($user->forgotten_password_key != $email_key) {
             $this->template->title = 'New Password Invocation Error';
             $this->template->content = new View('login/login_message');
             $this->template->content->message = 'The forgotten password identification string embedded in this link is invalid for this user. This may be because there has been a valid login for this user between the point where the Set Password page was brought up and when the Submit button was pressed.';
             return;
         }
     } else {
         if (!empty($_SESSION['auth_user']) and is_object($_SESSION['auth_user']) and $_SESSION['auth_user'] instanceof User_Model and $_SESSION['auth_user']->loaded) {
             if ($user->id != $_SESSION['auth_user']->id) {
                 $this->template->title = 'New Password Invocation Error';
                 $this->template->content = new View('login/login_message');
                 $this->template->content->message = 'Inconsistent user id: POST vs logged in user.';
                 return;
             }
         } else {
             $this->template->title = 'New Password Invocation Error';
             $this->template->content = new View('login/login_message');
             $this->template->content->message = 'Attempt to set password when not logged in.';
             return;
         }
     }
     $user_validation = new Validation($_POST);
     $person_validation = new Validation($_POST);
     // override the user_id for person in submission
     $person_validation['id'] = $user->person_id;
     // Can't just and following together as I want both functions to run
     $userstatus = $user->password_validate($user_validation, false);
     $personstatus = $person->email_validate($person_validation, false);
     if ($userstatus and $personstatus) {
         $user->save();
         $person->save();
         // we need different paths for core users and web site users
         if (is_null($user->core_role_id)) {
             // just return a success confirmation, can't log them in as not a core user
             $this->template->title = 'Password reset successfully';
             $this->template->content = new View('login/login_message');
             $this->template->content->message = 'Your indicia password has been reset and you can now use the new password to <a href="' . url::site() . '/login">log in</a>.<br />';
         } else {
             // with the password updated, login and jump to the home page
             $this->auth->login($user->id, $password);
             url::redirect(arr::remove('requested_page', $_SESSION));
         }
     } else {
         // errors are now embedded in the model
         $view = new View('login/new_password');
         $user->load_values(array('username' => $username));
         // repopulate for error condition after validate has removed it (is a disabled field so not present in POST)
         // have to reset passord as it gets encrypted
         $view->password = $password;
         $view->password2 = $password2;
         $view->email_key = $email_key;
         $view->user_model = $user;
         $view->person_model = $person;
         $this->template->title = 'Enter New Password';
         $this->template->content = $view;
     }
 }