/** * Construct the constructor with a default condition. */ function __construct($base = '', $args = array()) { $this->db = Pluf::db(); if (strlen($base) > 0) { $this->Q($base, $args); } }
/** * Get an item to process. * * @return mixed False if no item to proceed. */ public static function getItem() { $item = false; $db = Pluf::db(); $db->begin(); // In a transaction to not process the same item at // the same time from to processes. $gqueue = new Pluf_Queue(); $items = $gqueue->getList(array('filter' => $db->qn('lock') . '=0', 'order' => 'creation_dtime ASC')); if ($items->count() > 0) { $item = $items[0]; $item->lock = 1; $item->update(); } $db->commit(); if ($item === false) { return false; } // try to get the corresponding object $obj = Pluf::factory($item->model_class, $item->model_id); if ($obj->id != $item->model_id) { $obj = null; } return array('queue' => $item, 'item' => $obj); }
/** * @param mixed Values, will be encoded using json_encode */ function __construct($data, $mimetype = null) { if (null == $mimetype) { $mimetype = Pluf::f('mimetype_json', 'application/json') . '; charset=utf-8'; } parent::__construct(json_encode($data), $mimetype); }
public function testAddPrefixedViews() { $res = Pluf_Dispatcher::addPrefixToViewFile('/alternate', Pluf::f('app_views')); $this->assertEquals('#^/alternate/$#', $res[0]['regex']); $this->assertEquals('Todo_Views', $res[0]['model']); $this->assertEquals('#^/alternate/install/$#', $res[1]['regex']); }
/** * Process the request. * * @param Pluf_HTTP_Request The request * @return bool false */ function process_request(&$request) { // Find which language to use. By priority: // A session value with 'pluf_language' // A cookie with 'pluf_language' // The browser information. $lang = false; if (!empty($request->session)) { $lang = $request->session->getData('pluf_language', false); if ($lang and !in_array($lang, Pluf::f('languages', array('en')))) { $lang = false; } } if ($lang === false and !empty($request->COOKIE[Pluf::f('lang_cookie', 'pluf_language')])) { $lang = $request->COOKIE[Pluf::f('lang_cookie', 'pluf_language')]; if ($lang and !in_array($lang, Pluf::f('languages', array('en')))) { $lang = false; } } if ($lang === false) { // will default to 'en' $lang = Pluf_Translation::getAcceptedLanguage(Pluf::f('languages', array('en'))); } Pluf_Translation::loadSetLocale($lang); $request->language_code = $lang; return false; }
function __construct($exception, $mimetype = null) { $content = ''; $admins = Pluf::f('admins', array()); if (count($admins) > 0) { // Get a nice stack trace and send it by emails. $stack = Pluf_HTTP_Response_ServerError_Pretty($exception); $subject = $exception->getMessage(); $subject = substr(strip_tags(nl2br($subject)), 0, 50) . '...'; foreach ($admins as $admin) { $email = new Pluf_Mail($admin[1], $admin[1], $subject); $email->addTextMessage($stack); $email->sendMail(); } } try { $context = new Pluf_Template_Context(array('message' => $exception->getMessage())); $tmpl = new Pluf_Template('500.html'); $content = $tmpl->render($context); $mimetype = null; } catch (Exception $e) { $mimetype = 'text/plain'; $content = 'The server encountered an unexpected condition which prevented it from fulfilling your request.' . "\n\n" . 'An email has been sent to the administrators, we will correct this error as soon as possible. Thank you for your comprehension.' . "\n\n" . '500 - Internal Server Error'; } parent::__construct($content, $mimetype); $this->status_code = 500; }
function __construct($request, $vars = array()) { $vars = array_merge(array('request' => $request), $vars); foreach (Pluf::f('template_context_processors', array()) as $proc) { Pluf::loadFunction($proc); $vars = array_merge($proc($request), $vars); } $params = array('request' => $request, 'context' => $vars); /** * [signal] * * Pluf_Template_Context_Request::construct * * [sender] * * Pluf_Template_Context_Request * * [description] * * This signal allows an application to dynamically modify the * context array. * * [parameters] * * array('request' => $request, * 'context' => array()); * */ Pluf_Signal::send('Pluf_Template_Context_Request::construct', 'Pluf_Template_Context_Request', $params); $this->_vars = new Pluf_Template_ContextVars($params['context']); }
public static function url($file = '') { if ($file !== '' && Pluf::f('last_update_file', false) && false !== ($last_update = Pluf::fileExists(Pluf::f('last_update_file')))) { $file = $file . '?' . substr(md5(filemtime($last_update)), 0, 5); } return Pluf::f('url_media', Pluf::f('app_base') . '/media') . $file; }
/** * Get the form field for this field. * * We put this method at the field level as it allows us to easily * create a new DB field and a new Form field and use them without * the need to modify another place where the mapping would be * performed. * * @param array Definition of the field. * @param string Form field class. */ function formField($def, $form_field = 'Pluf_Form_Field_Varchar') { Pluf::loadClass('Pluf_Form_BoundField'); // To get mb_ucfirst $defaults = array('required' => !$def['blank'], 'label' => mb_ucfirst($def['verbose']), 'help_text' => $def['help_text']); unset($def['blank'], $def['verbose'], $def['help_text']); if (isset($def['default'])) { $defaults['initial'] = $def['default']; unset($def['default']); } if (isset($def['choices'])) { $defaults['widget'] = 'Pluf_Form_Widget_SelectInput'; if (isset($def['widget_attrs'])) { $def['widget_attrs']['choices'] = $def['choices']; } else { $def['widget_attrs'] = array('choices' => $def['choices']); } } foreach (array_keys($def) as $key) { if (!in_array($key, array('widget', 'label', 'required', 'multiple', 'initial', 'choices', 'widget_attrs'))) { unset($def[$key]); } } $params = array_merge($defaults, $def); return new $form_field($params); }
/** * Returns the path to access the file. */ function Pluf_DB_Field_File_Path($field, $method, $model, $args = null) { if (strlen($model->{$field}) != 0) { return Pluf::f('upload_path') . '/' . $model->{$field}; } return ''; }
/** * Constructor. * * If the folder name is not provided, it will default to * Pluf::f('template_folders') * If the cache folder name is not provided, it will default to * Pluf::f('tmp_folder') * * @param string Template name. * @param string Template folder paths (null) * @param string Cache folder name (null) */ function __construct($template, $folders = null, $cache = null) { $this->tpl = $template; if (null == $folders) { $this->folders = Pluf::f('template_folders'); } else { $this->folders = $folders; } if (null == $cache) { $this->cache = Pluf::f('tmp_folder'); } else { $this->cache = $cache; } if (defined('IN_UNIT_TESTS')) { if (!isset($GLOBALS['_PX_tests_templates'])) { $GLOBALS['_PX_tests_templates'] = array(); } } $this->compiled_template = $this->getCompiledTemplateName(); $b = $this->compiled_template[1]; $this->class = 'Pluf_Template_' . $b; $this->compiled_template = $this->compiled_template[0]; if (!class_exists($this->class, false)) { if (!file_exists($this->compiled_template) or Pluf::f('debug')) { $compiler = new Pluf_Template_Compiler($this->tpl, $this->folders); $this->template_content = $compiler->getCompiledTemplate(); $this->write($b); } include $this->compiled_template; } }
public function testFullRender() { $renderer = Pluf::factory('Pluf_Text_Wiki_Renderer'); $string = file_get_contents(dirname(__FILE__) . '/wikisample.txt'); $render = file_get_contents(dirname(__FILE__) . '/wikisample.render.txt'); $this->assertEquals($render, $renderer->render($string)); }
/** * Process the response of a view. * * If the status code and content type are allowed, add the * tracking code. * * @param Pluf_HTTP_Request The request * @param Pluf_HTTP_Response The response * @return Pluf_HTTP_Response The response */ function process_response($request, $response) { if (!Pluf::f('google_analytics_id', false)) { return $response; } if (!in_array($response->status_code, array(200, 201, 202, 203, 204, 205, 206, 404, 501))) { return $response; } $ok = false; $cts = array('text/html', 'text/html', 'application/xhtml+xml'); foreach ($cts as $ct) { if (false !== strripos($response->headers['Content-Type'], $ct)) { $ok = true; break; } } if ($ok == false) { return $response; } $js = '<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("' . Pluf::f('google_analytics_id') . '"); pageTracker._trackPageview(); } catch(err) {} </script>'; $response->content = str_replace('</body>', $js . '</body>', $response->content); return $response; }
/** * Save the model in the database. * * @param bool Commit in the database or not. If not, the object * is returned but not saved in the database. * @return Object Model with data set from the form. */ function save($commit = true) { if (!$this->isValid()) { throw new Exception(__('Cannot save the model from an invalid form.')); } return Pluf::f('url_base') . Pluf_HTTP_URL_urlForView('IDF_Views_User::changeEmailDo', array($this->cleaned_data['key'])); }
/** * Get the current item. */ public function current() { $i = current($this->results); $doc = Pluf::factory($i['model_class'], $i['model_id']); $doc->_searchScore = $i['score']; return $doc; }
function setUp() { $GLOBALS['_PX_config']['log_delayed'] = false; Pluf_Log::$level = Pluf_Log::ALL; Pluf_Log::$stack = array(); $this->logfile = Pluf::f('pluf_log_file', Pluf::f('tmp_folder', '/tmp') . '/pluf.log'); @unlink($this->logfile); }
protected function tearDown() { $db = Pluf::db(); $schema = Pluf::factory('Pluf_DB_Schema', $db); $m1 = new TestFormModel(); $schema->model = $m1; $schema->dropTables(); }
/** * Predelete to drop the row level permissions. */ function preDelete() { if (Pluf::f('pluf_use_rowpermission', false)) { $_rpt = Pluf::factory('Pluf_RowPermission')->getSqlTable(); $sql = new Pluf_SQL('owner_class=%s AND owner_id=%s', array($this->_a['model'], $this->_data['id'])); $this->_con->execute('DELETE FROM ' . $_rpt . ' WHERE ' . $sql->gen()); } }
/** * Get the default DB connection. * * The default database connection is defined in the configuration file * through the following configuration variables: * - db_login : Login to connect to the database * - db_password : Password to the database * - db_server : Name of the server * - db_database : Name of the database * - db_table_prefix : Prefix for the table names * - db_version : Version of the database engine * - db_engine : Engine for exampe 'MySQL', 'SQLite' * * Once the first connection is created the following calls to Pluf::db() * are getting the same connection. * */ function Pluf_DB_getConnection($extra = null) { if (isset($GLOBALS['_PX_db']) && (is_resource($GLOBALS['_PX_db']->con_id) or is_object($GLOBALS['_PX_db']->con_id))) { return $GLOBALS['_PX_db']; } $GLOBALS['_PX_db'] = Pluf_DB::get(Pluf::f('db_engine'), Pluf::f('db_server'), Pluf::f('db_database'), Pluf::f('db_login'), Pluf::f('db_password'), Pluf::f('db_table_prefix'), Pluf::f('debug'), Pluf::f('db_version')); return $GLOBALS['_PX_db']; }
function tearDown() { $db = Pluf::db(); $schema = Pluf::factory('Pluf_DB_Schema', $db); $m = new Pluf_Permission(); $schema->model = $m; $schema->dropTables(); }
/** * Return a "URL friendly" version in lowercase. * * Define the words separator with the configuration * option <code>slug-separator</code>. Default to <code>-</code>. * * @param $value string Value to convert * @return string The slugify version. */ public static function slugify($value) { $separator = Pluf::f('slug-separator', '-'); $value = Pluf_Text_UTF8::romanize(Pluf_Text_UTF8::deaccent($value)); $value = preg_replace('#[^' . $separator . '\\w]#u', $separator, mb_strtolower($value, Pluf::f('encoding', 'UTF-8'))); // remove redundant $value = preg_replace('#' . $separator . '{2,}#u', $separator, trim($value, $separator)); return $value; }
/** * Get an object by SQL or raise a 404 error. * * Usage: * <pre> * $obj = Pluf_Shortcuts_GetOneOr404('MyApp_Model', * 'path=%s AND status=%s', * array('welcome', 1)); * </pre> * * @param string Model * @param string Base SQL request * @param string Parameters for the base SQL * @return Object The found object */ function Pluf_Shortcuts_GetOneOr404($object, $bsql, $psql) { $sql = new Pluf_SQL($bsql, $psql); $item = Pluf::factory($object)->getOne(array('filter' => $sql->gen())); if ($item != null) { return $item; } throw new Pluf_HTTP_Error404(); }
/** * Flush the stack to the disk. * * @param $stack Array */ public static function write($stack) { $file = Pluf::f('pluf_log_file', Pluf::f('tmp_folder', '/tmp') . '/pluf.log'); $out = array(); foreach ($stack as $elt) { $out[] = date(DATE_ISO8601, (int) $elt[0]) . ' ' . Pluf_Log::$reverse[$elt[1]] . ': ' . json_encode($elt[2]); } file_put_contents($file, implode(PHP_EOL, $out) . PHP_EOL, FILE_APPEND); }
/** * Set a value in the cache. * * @param string Key to store the information * @param mixed Value to store * @param int Timeout in seconds (null) * @return bool Success */ public function set($key, $value, $timeout = null) { if ($this->memcache) { if ($timeout == null) { $timeout = Pluf::f('cache_timeout', 300); } $this->memcache->set($this->keyprefix . $key, serialize($value), Pluf::f('cache_memcached_compress', 0), $timeout); } }
/** * Process the request. * * @param Pluf_HTTP_Request The request * @return bool false */ function process_request(&$request) { if (0 !== strpos($request->query, Pluf::f('maintenance_root')) && file_exists(Pluf::f('tmp_folder') . '/MAINTENANCE')) { $res = new Pluf_HTTP_Response('Server in maintenance' . "\n\n" . 'We are upgrading the system to make it better for you, please try again later...', 'text/plain'); $res->status_code = 503; return $res; } return false; }
function IDF_Migrations_1Download_down($params = null) { $models = array('IDF_Upload'); $db = Pluf::db(); $schema = new Pluf_DB_Schema($db); foreach ($models as $model) { $schema->model = new $model(); $schema->dropTables(); } }
/** * Factory. * * @return Pluf_Cache_* Cache object */ public static function factory() { if (false === ($engine = Pluf::f('cache_engine', false))) { throw new Pluf_Exception_SettingError('"cache_engine" setting not defined.'); } if (!isset($GLOBALS['_PX_Pluf_Cache-' . $engine])) { $GLOBALS['_PX_Pluf_Cache-' . $engine] = new $engine(); } return $GLOBALS['_PX_Pluf_Cache-' . $engine]; }
function IDF_Migrations_7Wiki_down($params = null) { $models = array('IDF_WikiRevision', 'IDF_WikiPage'); $db = Pluf::db(); $schema = new Pluf_DB_Schema($db); foreach ($models as $model) { $schema->model = new $model(); $schema->dropTables(); } }
/** * Return false or an array with the email and id. * * This is a static function to be reused by other forms. * * @param string Confirmation key * @return mixed Either false or array(email, id) */ public static function checkKeyHash($key) { $hash = substr($key, 0, 2); $encrypted = substr($key, 2); if ($hash != substr(md5(Pluf::f('secret_key') . $encrypted), 0, 2)) { return false; } $cr = new Pluf_Crypt(md5(Pluf::f('secret_key'))); return explode(':', $cr->decrypt($encrypted), 2); }
/** * Display the configuration variable. * * @param string Configuration variable. * @param mixed Default value to return display (''). * @param bool Display the value (true). * @param string Prefix to set to the variable if not displayed ('cfg_'). */ function start($cfg, $default = '', $display = true, $prefix = 'cfg_') { if ($cfg != 'secret_key' or 0 !== strpos($cfg, 'db_')) { if ($display) { echo Pluf::f($cfg, $default); } else { $this->context->set($prefix . $cfg, Pluf::f($cfg, $default)); } } }