public function __invoke(Request $req, Response $res, callable $next) { $res = $next($req, $res); $identity = $this->authService->getIdentity(); if (!$identity) { return $res; } try { $user = R::findOne('user', 'mail = ?', [$identity->mail]); if (!$user) { $user = R::dispense('user'); $user->uid = $identity->uid; $user->mail = $identity->mail; $user->display_name = $identity->displayName; $user->office_name = $identity->officeName; $user->authentication_source = $identity->authenticationSource; $user->password = ''; $user->created = time(); $user->role = 'school'; $this->logger->info(sprintf('User %s imported from sso.sch.gr to database', $identity->mail)); } $user->last_login = time(); $user_id = R::store($user); $identityClass = get_class($identity); $newIdentity = new $identityClass($user_id, $user->uid, $user->mail, $user->display_name, $user->office_name, $user->authentication_source); $this->authService->getStorage()->write($newIdentity); } catch (\Exception $e) { $this->authService->clearIdentity(); $this->flash->addMessage('danger', 'A problem occured storing user in database. <a href="%s" title="SSO logout">SSO Logout</a>'); $this->logger->error('Problem inserting user form CAS in database', $identity->toArray()); $this->logger->debug('Exception', [$e->getMessage(), $e->getTraceAsString()]); return $res->withRedirect($this->userErrorRedirectUrl); } return $res; }
public function crawlUrl() { $currentUrl = ['url' => $this->currentUrl, 'depth' => $this->currentDepth]; do { $this->setCurrentUrl($currentUrl); if (!($doc = parent::crawlUrl())) { continue; } $username = $doc['.vcard-username']->text(); $user = R::findOne('github', ' username = ? ', [$username]); //$user = R::find('github', " username=$username "); if (empty($user)) { $user = R::dispense('github'); $now = time(); $user->avatar = $doc['.vcard-avatar .avatar']->attr('src'); $user->fullname = $doc['.vcard-fullname']->text(); $user->username = $username; $user->email = $doc['.email']->text(); $user->worksFor = $doc['.vcard-detail[itemprop=worksFor]']->text(); $user->homeLocation = $doc['.vcard-detail[itemprop=homeLocation]']->text(); $user->blogUrl = $doc['.vcard-detail[itemprop=url]']->text(); $user->joinDate = $doc['.join-date']->attr('datetime'); $user->url = $this->currentUrl; $user->createdAt = $now; $user->updatedAt = $now; if (R::store($user)) { echo '存储用户', $username, '成功', PHP_EOL; } else { echo '存储用户', $username, '失败', PHP_EOL; } } else { echo '用户', $username, '已经被存储过了', PHP_EOL; } } while ($currentUrl = $this->nextUrl()); }
public function save() { if (!$this->emptyAttr('id')) { $menu = R::findOne('menu', 'id=?', [$this->getAttr('id')]); } else { $menu = R::dispense('menu'); } $menu->name = $this->getAttr('name'); $oldPicture = null; if (!$this->emptyAttr('picture') && $this->attr['picture']->uploaded) { $picture = $this->getAttr('picture'); $picture->file_new_name_body = $this->generateName("menu_picture_"); // $picture->image_resize = true; $picture->image_convert = 'jpeg'; // $picture->image_x = 964; // $picture->image_y = 1024; // $picture->image_ratio_y = true; $picture->process('upload/'); $oldPicture = $menu->picture; $menu->picture = $picture->file_dst_name; } $success = R::store($menu); if ($success) { if (!is_null($oldPicture)) { @unlink('upload/' . $oldPicture); } } return $success; }
public function createSoftware(array $data) { unset($data['id']); $software = R::dispense('software'); $this->persistSoftware($software, $data); return $this->export($software); }
public function replace($content = '') { if (empty($this->name)) { throw new Exception('Blog post needs name before it can be saved'); } $bean = $this->_bean; if ($bean === null) { $this->_bean = $bean = R::dispense('blog'); } $bean->name = $this->name; $this->content = $bean->content = $content; $bean->edited = R::isoDateTime(); $bean->created = $this->created ?: R::isoDateTime(); $otherUserBean = App::user()->bean(); $bean->user = $this->_user !== null ? $this->_user->bean() : $otherUserBean; $this->contributorIds[] = $otherUserBean->getID(); $this->contributorIds = array_unique($this->contributorIds); $bean->contributorIds = implode(',', $this->contributorIds); if (!empty($this->publishedOn)) { $bean->publishedOn = R::isoDateTime($this->publishedOn); } else { $bean->publishedOn = null; } R::store($bean); }
public function createTeacher(array $data) { unset($data['id']); $teacher = R::dispense('teacher'); $this->persist($teacher, $data); return $this->export($teacher); }
public function createSchool(array $data) { $school = $this->importSchool(R::dispense('school'), $data); $school->created = time(); R::store($school); return $this->exportSchool($school); }
public function createLab(array $data) { unset($data['id']); $lab = R::dispense('lab'); $this->persist($lab, $data); return $this->exportLab($lab); }
/** * Add a job to the queue with name, unique, object * @param string $name * @param string $unique * @return int $res */ protected function create($name, $uniqueid) { $bean = R::dispense($this->queue); $bean->name = $name; $bean->uniqueid = $uniqueid; $bean->done = 0; return R::store($bean); }
/** * @param Array $data * ['name','fullname','password'] * @return string */ public function insertUser($data) { $user = R::dispense('user'); $user->name = $data['name']; $user->fullname = $data['fullname']; $user->hash = password_hash($data['password'], PASSWORD_DEFAULT); $id = R::store($user); return $id; }
/** * @throws \RedBeanPHP\RedException */ protected static function getCurrenciesFromJsonrates() { $data = json_decode(file_get_contents('http://jsonrates.com/currencies.json', true)); foreach ($data as $code => $name) { $currency = RedBean::dispense('currency'); $currency->code = $code; $currency->name = $name; RedBean::store($currency); } }
function store() { $email = R::dispense('email'); foreach (\app\run('input', 'keys', 'post') as $key) { $element = str_replace('-', '_', $key); $email->{$element} = \app\run('input', 'post', $key); } R::store($email); return null; }
public function testGetDetailsCustom() { $details = R::dispense('detail'); $details->desc = 'A test blog.'; $details->image = 'some url'; R::store($details); $response = $this->details->getDetails(new RequestMock(), new ResponseMock(), null); $this->assertEquals('success', $response->status); $this->assertEquals('SMPLog', $response->data[0]['name']); $this->assertEquals('A test blog.', $response->data[0]['desc']); }
public function save() { $account = R::dispense('account'); $account->created_at = date('Y-m-d H:i:s'); $account->email = empty($this->attr['email']) ? null : $this->attr['email']; $account->token = bin2hex(openssl_random_pseudo_bytes(16)); if (R::store($account)) { return $account; } else { return false; } }
public function newBean() { if ($this->bean !== null) { return $this->bean; } $bean = R::dispense('skemafield'); $bean->name = $this->name; $bean->cleanName = $this->cleanName; $bean->created = R::isoDateTime(); $bean->type = get_class($this); $bean->prerequisite = $this->prerequisite; return $this->bean = $bean; }
public static function create($module, $component, $group) { if (self::isUnique($module, $component, $group->name)) { $bean = R::dispense('perm'); $groupBean = $group->bean(); $bean->groupName = $group->name; $bean->module = $module; $bean->component = $component; $groupBean->sharedPermList[] = $bean; R::storeAll([$bean, $groupBean]); return new Perm($module, $component, $group); } return null; }
public function save() { $bean = $this->_bean ?: R::dispense('publisher'); $bean->name = $this->name; $bean->email = $this->email; $bean->phone = $this->phone; $bean->address1 = $this->address1; $bean->address2 = $this->address2; $bean->city = $this->city; $bean->state = $this->state; $bean->zip = $this->zip; R::store($bean); $this->_bean = $bean; return $this; }
public function __construct($table = null) { if (!is_null($table)) { $this->_table = $table; } elseif (empty($this->_table)) { $class = get_called_class(); $class = explode('\\', $class); $class = array_pop($class); $this->_table = strtolower(Inflect::pluralize($class)); } $this->_bean = R::dispense($this->_table); if ($this->_timestamps) { $this->_bean->{'created_at'} = Carbon::now(); } }
public function saveAnswers($teacherId, array $data) { if (isset($data['assets_in_use']) && is_array($data['assets_in_use'])) { $data['assets_in_use'] = implode('|', $data['assets_in_use']); } $bean = R::findOne('tpesurvey', 'teacher_id = ?', [$teacherId]); if (null === $bean) { $bean = R::dispense('tpesurvey'); } $data['edu_fields_future_sync_type'] = isset($data['edu_fields_future_sync_type']); $data['edu_fields_future_async_type'] = isset($data['edu_fields_future_async_type']); $bean->teacher_id = (int) $teacherId; $bean->import($data, ['already_using_tpe', 'knowledge_level', 'assets_in_use', 'sw_web2', 'sw_packages', 'sw_digitalschool', 'sw_other', 'uc_eduprograms', 'uc_digitaldesign', 'uc_asyncedu', 'uc_other', 'edu_fields_current', 'edu_fields_future', 'edu_fields_future_sync_type', 'edu_fields_future_async_type', 'extra_needs']); R::store($bean); }
public function edit(Request $request, Response $response, array $args) { $uid = $args['uid']; if (empty($uid)) { $this->flash->addMessage('flash', 'No record specified'); return $response->withRedirect($request->getUri()->getBaseUrl() . $this->router->pathFor('accounts')); } $id = $this->authenticator->getIdentity(); $user = R::load('users', $id['id']); if ($uid != 'new') { $account = R::load('accounts', $uid); if ($account->id == 0) { $this->flash->addMessage('flash', 'No record found'); return $response->withRedirect($request->getUri()->getBaseUrl() . $this->router->pathFor('accounts')); } // restrict access to own profile or Admin role if ($account->users->id != $id['id']) { if (strtolower($id['role']) != 'admin') { $this->flash->addMessage('flash', 'Access Denied'); return $response->withRedirect($request->getUri()->getBaseUrl() . $this->router->pathFor('accounts')); } } } else { $account = R::dispense('accounts'); } if ($request->isPost()) { $data = $request->getParams(); $account->import($data, 'apikey,accountid,servertype'); $account->users = $user; $account->lasttid = 0; $oandaInfo = FALSE; // verify and get account balance try { $oandaInfo = new Broker_Oanda($account['servertype'], $account['apikey'], $account['accountid'], 0); } catch (\Exception $e) { $viewData['flash'] = 'Account Details Invalid'; } if ($oandaInfo != FALSE) { $aid = R::store($account); $oandaInfo->updateAccount(); $this->flash->addMessage('flash', "account updated"); return $response->withRedirect($request->getUri()->getBaseUrl() . $this->router->pathFor('editaccount', ['uid' => $aid])); } } $viewData['account'] = $account; $this->view->render($response, 'account.twig', $viewData); return $response; }
public function testGetPost() { $request = new RequestMock(); $response = $this->posts->getPost($request, new ResponseMock(), null); $this->assertEquals('failure', $response->status); $this->assertEquals('No post found for slug .', $response->alerts[0]['text']); $post = R::dispense('post'); $post->title = 'test'; $post->slug = 'test'; R::store($post); $request = new RequestMock('test'); $this->posts = new Posts(new ContainerMock()); $response = $this->posts->getPost($request, new ResponseMock(), null); $this->assertEquals('success', $response->status); $this->assertEquals('test', $response->data[0]['title']); }
public function save($employerId) { if (!$this->emptyAttr('id')) { $item = R::findOne('employer_history', 'id=?', [$this->getAttr('id')]); } else { $item = R::dispense('employer_history'); $item->employer_id = $employerId; } $item->term_at = $this->getAttr('term_at', ''); $item->contract_no = $this->getAttr('contract_no', ''); $item->contract_description = $this->getAttr('contract_description', ''); $item->repair_1 = $this->getAttr('repair_1', ''); $item->repair_2 = $this->getAttr('repair_2', ''); $item->repair_3 = $this->getAttr('repair_3', ''); return R::store($item); }
public static function CreateInitialAdmin($container) { if (!R::count('user')) { $admin = R::dispense('user'); $admin->username = '******'; $admin->is_admin = true; $admin->is_active = true; $admin->name = 'Anonymous'; $admin->image = ''; $admin->logins = 0; $admin->last_login = null; $admin->password_hash = password_hash('admin', PASSWORD_BCRYPT); $admin->active_token = null; R::store($admin); } }
public function testDB() { $app = new Application(); $app->register(new RedBeanServiceProvider(), array('db.options' => array('dsn' => 'sqlite:' . __DIR__ . '/test.sqlite'))); $app['db']; //db init $post = R::dispense('post'); $post->text = 'Hello World'; $id = R::store($post); //Create or Update $fetchedPost = R::load('post', $id); //Retrieve $this->assertSame($post->text, $fetchedPost->text); $this->assertTrue(file_exists(__DIR__ . '/test.sqlite')); unlink(__DIR__ . '/test.sqlite'); }
public static function processForm($form) { $data = DataBase::extractDataForm($form); $tables = NULL; if (!isset($data)) { return; } //First we create instance to the tables foreach ($data as $table => $value) { $ob_table = parent::dispense($table); foreach ($value as $col => $value) { $ob_table[$col] = $value; } $tables[] = $ob_table; } return $tables; }
public static function create($groupName, $isDefaultRegistered = false, $isDefaultAnonymous = false, $isEveryone = false, $isSuper = false) { $count = R::count('group', ' name = ? ', [$groupName]); if ($count < 1) { $bean = R::dispense('group'); $bean->name = $groupName; $bean->isDefaultRegistered = $isDefaultRegistered; $bean->isDefaultAnonymous = $isDefaultAnonymous; $bean->isEveryone = $isEveryone; $bean->isSuper = $isSuper; $bean->ownUserList; $bean->sharedPermList; $id = R::store($bean); return new Group($groupName, $bean); } return null; }
public function save() { if (!$this->emptyAttr('id')) { $user = R::findOne('user', 'id=?', [$this->getAttr('id')]); } else { $user = R::dispense('user'); $user->level = 2; } $user->username = $this->getAttr('username'); $user->password = $this->getAttr('password'); $user->email = $this->getAttr('email', ''); $user->first_name = $this->getAttr('first_name', ''); $user->last_name = $this->getAttr('last_name', ''); $user->tel_number = $this->getAttr('tel_number', ''); $user->branch = $this->getAttr('branch', ''); return R::store($user); }
public function save() { if (!$this->emptyAttr('id')) { $ecatalog = R::findOne('ecatalog', 'id=?', [$this->getAttr('id')]); $ecatalog->updated_at = date('Y-m-d H:i:s'); } else { $ecatalog = R::dispense('ecatalog'); $ecatalog->created_at = date('Y-m-d H:i:s'); $ecatalog->updated_at = date('Y-m-d H:i:s'); $ecatalog->sort_order = (int) R::getCell("SELECT MAX(sort_order) FROM ecatalog") + 1; } $ecatalog->name = $this->getAttr('name'); $ecatalog->is_new = $this->getAttr('is_new'); if (!$this->emptyAttr('pdf') && is_uploaded_file($this->attr['pdf']['tmp_name'])) { $pdf = $this->getAttr('pdf'); $pdf_path = $this->generateName('ecatalog_pdf_') . '.pdf'; $cover_path = $this->generateName('ecatalog_cover_') . '.jpeg'; $im = new \Imagick($pdf['tmp_name'] . '[0]'); $im->setImageBackgroundColor('white'); // $im = $im->flattenImages(); $im = $im->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN); $im->setImageFormat('jpeg'); if ($im->getImageColorspace() == \Imagick::COLORSPACE_CMYK) { $im->setImageColorspace(\Imagick::COLORSPACE_SRGB); } $im->thumbnailImage(512, 0); $im->writeImage('upload/' . $cover_path); $im->clear(); $im->destroy(); move_uploaded_file($pdf['tmp_name'], 'upload/' . $pdf_path); $this->pushDeleteWhenSuccess('upload/' . $ecatalog->pdf_path); $this->pushDeleteWhenSuccess('upload/' . $ecatalog->cover_path); $this->pushDeleteWhenFailed('upload/' . $pdf_path); $this->pushDeleteWhenFailed('upload/' . $cover_path); $ecatalog->pdf_path = $pdf_path; $ecatalog->cover_path = $cover_path; } $success = R::store($ecatalog); if ($success) { $this->handlerSuccess(); } else { $this->handlerFailed(); } return $success; }
public function editRota(Request $request, Response $response, array $args) { $id = $this->authenticator->getIdentity(); if (strtolower($id['name']) != 'admin') { $this->flash->addMessage('flash', 'Access Denied'); return $response->withRedirect($this->router->pathFor('homepage')); } $name = $args['name']; if (empty($name)) { $this->flash->addMessage('flash', 'No rota specified'); return $response->withRedirect($this->router->pathFor('rotas')); } if ($name != 'new') { $rota = R::findOrCreate('rotas', ['name' => $name]); } else { $rota = R::dispense('rotas'); } if ($request->isPost()) { $data = $request->getParams(); //$username = $request->getParam('username'); $rota->import($data, 'name,fullname,title,comment'); $rota->sharedUsersList = []; foreach ($data['users'] as $checkUserID) { $rotaUser = R::load('users', $checkUserID); $rota->sharedUsersList[] = $rotaUser; } $id = R::store($rota); try { $fieldtest = R::inspect($rota->name); } catch (\Exception $e) { //thaw for creation R::freeze(['users']); $rotaUser = R::load('users', 1); $rotaDay = R::findOrCreate($rota->name, ['day' => 29, 'month' => 2, 'year' => 2015]); $rotaUser = R::load('users', 1); $rotaDay->name = $rotaUser; $rotaDay->who = $rotaUser; $rotaDay->stamp = date("Y-m-d H:i:s"); R::store($rotaDay); R::freeze(true); } $this->flash->addMessage('flash', "{$rota->name} updated"); return $response->withRedirect($this->router->pathFor('rotas')); } $userList = R::findAll('users'); $data = $rota->export(); $data['userList'] = $userList; $users = []; $userRota = $rota->sharedUsersList; foreach ($userRota as $userCheck) { $users[$userCheck->id] = 'checked'; } $data['userCheck'] = $users; $this->view->render($response, 'rota.twig', $data); return $response; }
public function getBean() { if ($this->bean !== null) { return $this->bean; } $bean = R::findOne('skemaset', ' name = ? ', [$this->name]); if (empty($bean)) { $bean = R::dispense('skemaset'); $bean->name = $this->name; $bean->cleanName = Utility::cleanTableName($this->name); $bean->created = R::isoDateTime(); $bean->description = ''; $bean->ownFieldList; $bean->{'ownSkemarecord' . $this->cleanBaseName . 'List'}; } return $this->bean = $bean; }