/** * Quick solution to include proprietary or sensitive content for the production * build, such as PayPal, advertising, and so on. Some of these are viewable in * the public site html source, but are better left out of the Open Source * repository to avoid misuse of tracking ids, emails and so on. * * If the asset to include is not found, a box displays a message instead. * * Those assets use a file naming pattern excluded from commits (.gitignore) * * TODO: find way to handle a "production site" branch, instead of run time mode. * * @author Fabrice Denis */ function get_local_content($fromTemplate, $assetName) { ob_start(); if (coreConfig::get('koohii_build')) { $assetFile = '__' . $assetName . 'View.php'; $assetPath = dirname(realpath($fromTemplate)); $file = $assetPath . DIRECTORY_SEPARATOR . $assetFile; if (file_exists($file)) { // render as a partial so we have access to the default template variables $view = new coreView(coreContext::getInstance()); $view->setTemplate($file); echo $view->render(); } else { echo <<<EOD <div style="background:red;color:yellow;padding:10px;"> koohii_build content NOT FOUND: <strong>{$assetName}</strong> </div> EOD; } } else { // show a little box with the name of the local asset echo <<<EOD <div style="border-radius:5px;-moz-border-radius:5px;border:none;background:#b2d2e3;color:#42697e;padding:10px;margin:0 0 1em;"> Public site content: <strong>{$assetName}</strong> </div> EOD; } return ob_get_clean(); }
public function execute($request) { $user_id = coreContext::getInstance()->getUser()->getUserId(); $this->filter = $this->getUser()->getLocalPrefs()->get('review.graph.filter', ''); $carddata = ReviewsPeer::getLeitnerBoxCounts($this->filter); $this->restudy_cards = $carddata[0]['expired_cards']; // count untested cards and add to graph $this->untested_cards = ReviewsPeer::getCountUntested($user_id, $this->filter); $carddata[0]['fresh_cards'] = $this->untested_cards; $carddata[0]['total_cards'] += $this->untested_cards; // count totals (save a database query) //$this->total_cards = 0; $this->expired_total = 0; for ($i = 0; $i < count($carddata); $i++) { $box =& $carddata[$i]; //$this->total_cards += $box['total_cards']; // count expired cards, EXCEPT the red stack if ($i > 0) { $this->expired_total += $box['expired_cards']; } } $this->chart_data = $this->makeChartData($carddata); //DBG::printr($this->chart_data);exit; $this->me = $this; return coreView::SUCCESS; }
/** * Adds a unique version identifier to the css and javascript file names, * (using the local file modified times from build script), to prevent client * browsers from using the cache when a css/js file is updated. * * The .htaccess files redirects those "versioned" files to a php script that * will strip the version number to get the actual file, and return the file * gzipped if possible to minimized download size. * * @param string $url Css or Javascript url * @return string Resource url with version number in it */ protected function getVersionUrl($url) { // leave absolute URLs (usually from CDNs like Google and Yahoo) unchanged if (stripos($url, 'http:') === 0) { return $url; } if (coreContext::getInstance()->getConfiguration()->getEnvironment() === 'dev') { // do not use minified javascript/css in development environment $url = str_replace('.min.', '.', $url); // show the url that would be run by mod_rewrite $url = '/version/cache.php?env=dev&app=' . CORE_APP . '&path=' . urlencode($url); } else { // in production, "juicy" files should be precompiled and minified with a script if (($pos = strpos($url, '.juicy.')) !== false) { // replace the '.juicy' part with '.min' (eg: /css/main.juicy.css => /css/main.min.css) //$len = strlen($url); //$url = substr($url, 0, $pos) . substr($url, $pos + 6); $url = str_replace('.juicy.', '.min.', $url); } // add version string $versions = $this->getResourceVersion(); $path = pathinfo($url); $ver = isset($versions[$url]) ? '_v' . $versions[$url] : ''; preg_match('/(.+)(\\.[a-z]+)/', $path['basename'], $matches); $url = $path['dirname'] . '/' . $matches[1] . $ver . $matches[2]; } return $url; }
public function printStackTrace() { $exception = is_null($this->wrappedException) ? $this : $this->wrappedException; $message = $exception->getMessage(); $response = coreContext::getInstance()->getResponse(); $response->setStatusCode(500); // clean current output buffer while (@ob_end_clean()) { } ob_start(coreConfig::get('sf_compressed') ? 'ob_gzhandler' : ''); header('HTTP/1.1 500 Internal Server Error'); header('Content-Type: text/plain'); if ($message !== '') { header('RTK-Error: ' . $message); } // during development, send back ajax request for debugging if (coreConfig::get('sf_debug')) { try { $request = coreContext::getInstance()->getRequest(); $sJson = $request->getParameter('json'); $oJson = null; if ($sJson !== null) { $oJson = coreJson::decode($sJson); } echo 'Json data = ' . "\n" . ($oJson !== null ? print_r($oJson, true) : $sJson); } catch (Exception $e) { echo 'rtkAjaxException - no Json found, $_POST = ' . "\n" . print_r($_POST, true); } } exit(1); }
/** * Returns a routed URL based on the module/action passed as argument * and the routing configuration. * * Examples: * echo url_for('my_module/my_action'); * => /path/to/my/action * * @param string 'module/action' * @param bool Return absolute path? * @return string Routed URL */ function url_for($internal_uri, $absolute = false) { static $controller; if (!isset($controller)) { $controller = coreContext::getInstance()->getController(); } return $controller->genUrl($internal_uri, $absolute); }
private function createUser($username, $raw_password) { $userinfo = array('username' => $username, 'password' => coreContext::getInstance()->getUser()->getSaltyHashedPassword($raw_password), 'userlevel' => $this->getOption('level', UsersPeer::USERLEVEL_USER), 'email' => trim($this->getOption('email', 'created@localhost')), 'location' => trim($this->getOption('location', 'localhost'))); //die(print_r($userinfo, true)); if (false === UsersPeer::createUser($userinfo)) { $this->throwError('Could not create user.'); } }
/** * Updates any column data for given user id. * * 'raw_password' will be hashed as required and stored into 'password' column. * * @param int $userid * @param array $data * @return boolean */ private function updateUser($userid, $data) { if (isset($data['raw_password'])) { $data['password'] = coreContext::getInstance()->getUser()->getSaltyHashedPassword($data['raw_password']); unset($data['raw_password']); } if (false === UsersPeer::updateUser($userid, $data)) { $this->throwError('Could not update user id %s', $userid); } return true; }
/** * Add 1000 user records in dummy database WITHOUT prepared statement: avg. 700ms * With table locking, avg. 660ms (minimal difference!) * * @return */ function withoutStatementTest() { $db = coreContext::getInstance()->getDatabase(); $db->query("LOCK TABLES users WRITE"); for ($i = 0; $i < 1000; $i++) { $s = 'framenum' . $i; $level = $i & 0xf; $db->query("INSERT users (username, userlevel, joindate) VALUES (?, ?, NOW())", array($s, $level)); } $db->query("UNLOCK TABLES"); }
/** * Return a confirmation message box html code, if any confirmation messages * were set in the Request object. * * @return string Html code to echo */ function form_confirmations() { $request = coreContext::getInstance()->getRequest(); $s = ''; if ($request->hasConfirmations()) { $s = implode("<br/>\n", array_values($request->getConfirmations())); $s = content_tag('p', $s); $s = content_tag('div', $s, array('class' => 'messagebox msgbox-success')); $s .= '<div class="clear"></div>'; } return $s; }
/** * Output all validation errors. * * @return */ function form_errors() { $request = coreContext::getInstance()->getRequest(); $s = ''; if ($request->hasErrors()) { foreach ($request->getErrors() as $key => $message) { $s .= "<strong>{$message}</strong><br />\n"; } $s = content_tag('div', $s, array('class' => 'formerrormessage')); } return $s; }
public function printStackTrace() { $exception = is_null($this->wrappedException) ? $this : $this->wrappedException; if (coreConfig::get('sf_debug')) { $response = coreContext::getInstance()->getResponse(); $response->setStatusCode(404); return parent::printStackTrace(); } else { // debug message //echo $exception->getMessage(); coreContext::getInstance()->getController()->forward(coreConfig::get('error_404_module'), coreConfig::get('error_404_action')); } }
/** * uiChartVs * * Options: * labelLeft, labelRight Labels on each side * valueLeft, valueRight Values, will be summed up to calculate percentage * labelLeftMax Label to use when value of the other side is 0 (OPTIONAL) * labelRightMax * * @see /doc/slicing/RevTK/charts/uiChartVs.html */ function ui_chart_vs(array $options) { $valueTotal = $options['valueLeft'] + $options['valueRight']; $pctLeft = ceil($options['valueLeft'] * 100 / $valueTotal); $pctRight = 100 - $pctLeft; $captionLeft = isset($options['labelLeftMax']) && $options['valueRight'] == 0 ? $options['labelLeftMax'] : $options['labelLeft']; $captionRight = isset($options['labelRightMax']) && $options['valueLeft'] == 0 ? $options['labelRightMax'] : $options['labelRight']; $options = array_merge($options, array('pctLeft' => $pctLeft, 'pctRight' => $pctRight, 'bZeroLeft' => $pctLeft == 0, 'bZeroRight' => $pctRight == 0, 'captionLeft' => $captionLeft, 'captionRight' => $captionRight)); $view = new coreView(coreContext::getInstance()); $view->getParameterHolder()->add($options); $view->setTemplate(dirname(__FILE__) . '/templates/ui_chart_vs.php'); return $view->render(); }
/** * * Options * partial_name The full partial name (module/partial) to use for rendering * * @return */ public function __construct($options = array()) { $this->options = (object) $options; $this->partialName = $this->options->partial_name; $this->user_id = coreContext::getInstance()->getUser()->getUserId(); //testing /* if (isset($this->options->items)) { $x = array(); for ($i = 1; $i<=3007; $i++) { $x[] = 3008 - $i; } $this->options->items = $x; //array_slice($this->options->items, 0, 4); } */ }
public function __construct(array $options) { $this->db = coreContext::getInstance()->getDatabase(); if (!isset($options['internal_uri'])) { throw new coreException("Must set 'internal_uri' in uiSelectPager options."); } $this->setUrl($options['internal_uri']); if (isset($options['select'])) { $this->setSelect($options['select']); } if (isset($options['query_params'])) { $this->setQueryParams($options['query_params']); } if (isset($options['max_per_page'])) { $this->setMaxPerPage((int) $options['max_per_page']); } if (isset($options['page'])) { $this->setPage($options['page']); } }
public function execute($request) { $queryParams = $this->getUser()->getLocalPrefs()->syncRequestParams('mystorieslist', array(uiSelectPager::QUERY_ROWSPERPAGE => 10)); // pager $this->pager = new uiSelectPager(array('select' => StoriesPeer::getMyStoriesSelect($this->getUser()->getUserId()), 'internal_uri' => 'study/mystories', 'query_params' => array(uiSelectTable::QUERY_SORTCOLUMN => $request->getParameter(uiSelectTable::QUERY_SORTCOLUMN, 'framenum'), uiSelectTable::QUERY_SORTORDER => $request->getParameter(uiSelectTable::QUERY_SORTORDER, 1), uiSelectPager::QUERY_ROWSPERPAGE => $queryParams[uiSelectPager::QUERY_ROWSPERPAGE]), 'max_per_page' => $queryParams[uiSelectPager::QUERY_ROWSPERPAGE], 'page' => $request->getParameter(uiSelectPager::QUERY_PAGENUM, 1))); $this->pager->init(); // order by $order_by = array('framenum' => 'framenum ASC', 'keyword' => 'keyword ASC', 'lastedit' => 'updated_on DESC', 'votes' => 'stars DESC'); $sortkey = $request->getParameter('sort', 'lastedit'); $this->getController()->getActionInstance()->forward404Unless(!$sortkey || preg_match('/^[a-z]+$/', $sortkey)); $orderClause = isset($order_by[$sortkey]) ? $order_by[$sortkey] : $order_by['framenum']; // get row data $get_select = clone $this->pager->getSelect(); $get_select->order($orderClause); $rows = coreContext::getInstance()->getDatabase()->fetchAll($get_select); foreach ($rows as &$R) { $R['stars'] = $R['stars'] ? '<span class="star">' . $R['stars'] . '</span>' : '<span> </span>'; $R['kicks'] = $R['kicks'] ? '<span class="report">' . $R['kicks'] . '</span>' : '<span> </span>'; $R['story'] = StoriesPeer::getFormattedStory($R['story'], $R['keyword'], false); } $this->rows = $rows; return coreView::SUCCESS; }
/** * Bridge between the Core framework configuration and Symfony 1.1 Url Routing. * * Load the url routing config from settings.php, and add the routes to * Symfony's sfRouting object with the connect() method. * * @todo Use a cache with sfPatternRouting and evaluate speed gains. * * @return sfPatternRouting */ protected function initializeSymfonyRouting() { $dispatcher = new sfEventDispatcher(); $sf_routing = new sfPatternRouting($dispatcher); coreContext::getInstance()->set('sfPatternRouting', $sf_routing); $app_routes = coreConfig::get('routing_config'); // Add routes to the sfPatternRouting object foreach ($app_routes['routes'] as $name => $route) { $defaults = isset($route['param']) ? $route['param'] : array(); $requirements = isset($route['requirements']) ? $route['requirements'] : array(); $sf_routing->connect($name, $route['url'], $defaults, $requirements); } return $sf_routing; }
<?php define('CORE_ROOT_DIR', realpath(dirname(__FILE__) . '/..')); define('CORE_APP', 'revtk'); define('CORE_ENVIRONMENT', 'dev'); define('CORE_DEBUG', true); require_once CORE_ROOT_DIR . '/apps/' . CORE_APP . '/config/config.php'; $configuration = new revtkConfiguration(CORE_ENVIRONMENT, CORE_DEBUG, CORE_ROOT_DIR); coreContext::createInstance($configuration); coreContext::getInstance()->getController()->dispatch();
/** * Class constructor. * */ public function __construct() { coreContext::getInstance()->set('request', $this); // initialize parameter holder $this->parameterHolder = new sfParameterHolder(); }
function nav_active($nav_id) { $_request = coreContext::getInstance()->getRequest(); $_page_id = $_request->getParameter('module') . '-' . $_request->getParameter('action'); return $nav_id == $_page_id ? true : false; }
/** * Sign Out. * * @return */ public function executeLogout($request) { $this->getUser()->signOut(); // clear the rememberme cookie $this->getUser()->clearRememberMeCookie(); // clear the PunBB cookie (not on the test website) if (coreContext::getInstance()->getConfiguration()->getEnvironment() !== 'staging' && coreConfig::get('app_path_to_punbb') !== null) { PunBBUsersPeer::signOut(); } return $this->redirect('@homepage'); }
/** * Returns a SQL statement which returns a date+time adjusted to the * timezone of the user ($session->timezone). * * The date returned by this statement will switch at midnight time * of the user's timezone (assuming the user set the timezone properly). * (the user's timezone range is -12...+14) * * @param string If set, * * @todo Move to RevTK extension of core class */ public function localTime($column = 'NOW()') { $timezone = coreContext::getInstance()->get('auth')->getTimezone(); $timediff = $timezone - coreConfig::get('server_timezone', 0); $hours = floor($timediff); $minutes = $hours != $timediff ? '30' : '0'; // some timezones have half-hour precision, convert to minutes $s = sprintf('ADDDATE(%s, INTERVAL \'%d:%d\' HOUR_MINUTE)', $column, $hours, $minutes); return $s; }
/** * Returns active user session. * * @return coreUser */ public static function getUser() { if (self::$user === null) { self::$user = coreContext::getInstance()->getUser(); } return self::$user; }
/** * Process editable data requests, and prepare data to render table. * */ public function process() { // some configuration checks if ($this->settings['primaryKey'] === null) { throw new coreException(__METHOD__ . "() primaryKey not set"); } // validate data $this->errorMessages = array(); if ($this->request->get('saveChanges')) { // delete rows $this->deleteRows(); // modify and add rows $this->getPostRowData(); $this->validatePostRowData(); } else { $this->badRows = array(); $this->newRows = array(); } if (count($this->badRows) > 0) { $this->errors = '1'; $this->errorMessages[] = "Some validation errors occured. Please correct the data and then save again."; } else { $this->errors = '0'; } // apply sorting to data source and fetch data $this->rowdata = coreContext::getInstance()->getDatabase()->fetchAll($this->applySorting($this->select)); // ready for view template $this->processed = true; }
/** * Returns a timestamp localized to the user. * * Currently, this timestamp corresponds to the adjusted flashcard's "lastreview" timestamp. * * @return int Server's MySQL timestamp adjusted to user's local time. */ public static function getLocalizedTimestamp() { $user = coreContext::getInstance()->getUser(); $ts = self::$db->fetchOne('SELECT UNIX_TIMESTAMP(?)', new coreDbExpr($user->sqlLocalTime())); if (!$ts) { throw new coreException('getLocalizedTimestamp() failed'); } return $ts; }
/** * Render template file using PHP as the templating engine. * * @param string Filename * @return string A string representing the rendered presentation */ protected function renderFile($templateFile) { // load core and standard helpers $helpers = array_unique(array_merge(array('Core', 'Url', 'Asset', 'Tag'), coreConfig::get('standard_helpers'))); coreToolkit::loadHelpers($helpers); extract($this->parameterHolder->toArray(), EXTR_REFS); // template shortcuts $_context = coreContext::getInstance(); $_request = $_context->getRequest(); $_params = $_request->getParameterHolder(); $_user = $_context->getUser(); $_response = $_context->getResponse(); // render ob_start(); ob_implicit_flush(0); require $templateFile; return ob_get_clean(); }
/** * Change Password. * * Update the user's password on the RevTK site AND the corresponding PunBB forum account. * */ public function executePassword($request) { if ($request->getMethod() != coreRequest::POST) { return coreView::SUCCESS; } // handle the form submission $validator = new coreValidator($this->getActionName()); if ($validator->validate($request->getParameterHolder()->getAll())) { // verify old password $oldpassword = trim($request->getParameter('oldpassword')); $user = $this->getUser()->getUserDetails(); if ($user && $this->getUser()->getSaltyHashedPassword($oldpassword) == $user['password']) { // proceed with password update $new_raw_password = trim($request->getParameter('newpassword')); $user = $this->getUser()->getUserDetails(); // update the password on main site and forum $this->getUser()->changePassword($user['username'], $new_raw_password); // save username before signing out $this->username = $this->getUser()->getUserName(); // log out user (sign out, clear cookie, clear punbb cookie(not on staging website)) $this->getUser()->signOut(); $this->getUser()->clearRememberMeCookie(); if (coreContext::getInstance()->getConfiguration()->getEnvironment() !== 'staging' && coreConfig::get('app_path_to_punbb') !== null) { PunBBUsersPeer::signOut(); } try { // send email confirmation $mailer = new rtkMail(); $mailer->sendUpdatePasswordConfirmation($user['email'], $user['username'], $new_raw_password); } catch (coreException $e) { $request->setError('mail_error', 'Oops, we tried sending you a confirmation email but the mail server didn\'t respond. Your password has been updated though!'); } return 'Done'; } else { $request->setError('login_invalid', "Old password doesn't match."); } } // clear the password fields (avoid input mistakes) $request->setParameter('oldpassword', ''); $request->setParameter('newpassword', ''); $request->setParameter('newpassword2', ''); }
/** * Initialize this table peer instance. * * The only reason we instantiate the class is that static variables * can not be overriden in the extending class as of Php 5.2. * * @param coreDatabase $db coreDatabase instance. * @param string $tableName Table name obtained from the class name without 'Peer' suffix * @return */ public function __construct($peerclass) { // static reference for convenience self::$db = coreContext::getInstance()->getDatabase(); // check naming of model and determine the default table name if (!preg_match('/^(\\w+)Peer$/', $peerclass, $matches)) { throw new coreException('Invalid coreDatabaseTable class name: ' . $peerclass); } // if not explicitly set, the table name is derived from the class name, in lowercase if ($this->tableName === null) { $this->tableName = strtolower($matches[1]); } // columns check if (empty($this->columns)) { throw new coreException('coreDatabaseTable table ' . $this->getName() . ' columns not set.'); } $this->initialize(); }
/** * Static function callback for uiFlashcardReview 'fn_get_flashcard'. * * Returns flashcard data that was already loaded into the session, * given a unique flashcard id, which was used as the key for the * session array 'uifr'. * * @return object Object to be returned as JSON data */ public static function getFlashcardData($id) { $sess = coreContext::getInstance()->getUser()->getAttribute(self::FLASHCARDREVIEW_SESS); if (!$sess) { throw new rtkAjaxException('getFlashcardData(): No session?'); } if (isset($sess[$id - 1])) { // grab the flashcard data from the session $card = (object) $sess[$id - 1]; //new stdClass(); // uiFlashcardReview frontend requires this to match JSON response with its selection $card->id = $id; $card->dispword = self::getKeywordizedCompound($card->compound); return $card; } return null; }
/** * Update local preferences from request parameters and returns current values. * * This method is handy to remember user interaction with data tables and other * components that send parameters on the query string. Specify prefix to uniquely * identify the page to which the request params apply. * * Example: * Remember the state of a data table on page "client_report": * syncRequestParams('client_report', array( * 'sortcol' => 'clientname', * 'rows' => 20 * ) * * This will store the local prefs (user attributes): * * client_report.sortcol * client_report.rows * * It will return a hash with the updated values: * * array( 'sortcol' => ..., 'rows' => ... ) * * * @param string $prefix Prefix for the local preference name of each param * @param array $params Default values for parameters (hash) * * @return array A hash of parameters (without prefix) and their updated values */ public function syncRequestParams($prefix, array $params) { $request = coreContext::getInstance()->getRequest(); $values = array(); foreach ($params as $name => $default) { $values[$name] = $this->sync($prefix . '.' . $name, $request->getParameter($name), $default); } return $values; }
/** * Redirect unauthenticated user to login action. * * Options: * * username => Fill in the user name of the login form * referer => Page to return the user to after signing in * * @param array $params Options to pass to the login page */ public function redirectToLogin($options = array()) { if (isset($options['referer'])) { $this->setAttribute('login_referer', $options['referer']); } if (isset($options['username'])) { $this->setAttribute('login_username', $options['username']); } $login_url = coreConfig::get('login_module') . '/' . coreConfig::get('login_action'); coreContext::getInstance()->getActionInstance()->redirect($login_url); }