/** * Create a token for a form of ours. Will be saved in session. * @param string $name Optional name of session variable * @return string token */ public static function create($name = 'Default') { // create a token if (!isset($_SESSION[self::SESSION_STORAGE . APP_SALT . $name])) { // store it in a session $_SESSION[self::SESSION_STORAGE . APP_SALT . '\\' . $name] = Fari_Tools::randomCode(); } // return it so that we can store it in a hidden form param return $_SESSION[self::SESSION_STORAGE . APP_SALT . '\\' . $name]; }
public function __construct($file) { // set the file $this->set($file); // a new filepath with coded filename for a slightly better security $this->path = BASEPATH . '/tmp/' . Fari_Tools::randomCode() . '.jpg'; // 1. determine if image // 2. upload // 3. create the image from path switch ($this->type) { case 'image/jpeg': case 'image/jpg': $this->upload(); if ($this->needsResize()) { $image = imagecreatefromjpeg($this->path); } break; case 'image/png': $this->upload(); if ($this->needsResize()) { $image = imagecreatefrompng($this->path); } break; case 'image/gif': $this->upload(); if ($this->needsResize()) { $image = imagecreatefromgif($this->path); } break; } // do we have an image? if (isset($image)) { // set the new height $height = self::THUMB_WIDTH / ($this->width / $this->height); // create a new JPEG thumbnail $thumb = imagecreatetruecolor(self::THUMB_WIDTH, $height); imagecopyresampled($thumb, $image, 0, 0, 0, 0, self::THUMB_WIDTH, $height, $this->width, $this->height); // just save as JPEG //$thumb = imagecreatetruecolor($this->width, $this->height); //imagecopyresampled($thumb, $image, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height); // save it to a new path imagejpeg($thumb, $this->path); $this->created = TRUE; imagedestroy($image); imagedestroy($thumb); } }
function newInvitation($firstName, $lastName, $email = NULL) { $shortName = $longName = $firstName; // default if (!empty($lastName)) { $last = explode(' ', $lastName); $shortName = $firstName . ' ' . substr(end($last), 0, 1) . '.'; // short $longName = $firstName . ' ' . implode(' ', $last); // long } $this->db->insert('users', array('email' => $email, 'name' => $firstName, 'surname' => $lastName, 'short' => $shortName, 'long' => $longName, 'invitation' => Fari_Tools::randomCode(6), 'role' => 'invited')); if (!empty($lastName)) { $firstName .= ' ' . $lastName; } return $firstName; }
private function randomCode($db) { $code = Fari_Tools::randomCode(6); $result = $db->selectRow('files', 'id', array('code' => $code)); return !empty($result) ? $this->randomCode() : $code; }
/** * Get code and name from the form and create a new user for us (generate username) */ public function actionCreate() { $name = Fari_Decode::accents($this->request->getPost('name')); $code = $this->request->getPost('code'); if (!empty($name)) { $name = explode(' ', $name); // do we have a 'long' name? if (count($name) > 1) { $short = $name[0] . ' ' . substr(end($name), 0, 1) . '.'; $long = implode(' ', $name); $surname = end($name); $name = $name[0]; } else { $short = $long = $name = $name[0]; $surname = ''; } // generate a username $username = Fari_Escape::slug($long) . Fari_Tools::randomCode(10); $db = Fari_Db::getConnection(); // insert the user in a guest role $userId = $db->insert('users', array('short' => $short, 'long' => $long, 'name' => $name, 'surname' => $surname, 'role' => 'guest', 'username' => $username)); // log them in automatically Fari_AuthenticatorSimple::forceAuthenticate($username); // give them permissions to enter this room $room = $db->selectRow('rooms', 'id', array('guest' => $code)); if (!empty($room)) { $db->insert('user_permissions', array('room' => $room['id'], 'user' => $userId)); } } // redirect to the room, if we've ailed will be asked for guest's name again $this->redirectTo('/g/' . $code); }