Автор: Mark Croxton (mcroxton@hallmark-design.co.uk)
Пример #1
0
 public static function define($name, $value)
 {
     switch ($name) {
         case 'stash_dir':
             Stash::$stash_dir = $value;
             break;
         case 'host':
             self::$DATABASE_HOST = $value;
             break;
         case 'dbname':
             self::$DATABASE_NAME = $value;
             break;
         case 'dbuser':
             self::$DATABASE_USER = $value;
             break;
         case 'dbpassword':
             self::$DATABASE_PASSWORD = $value;
             break;
         case 'charset':
             self::$charset = $value;
             break;
     }
 }
 /**
  * Set the current context
  *
  * @access public
  * @return void
  */
 public function context()
 {
     if (!!($name = $this->EE->TMPL->fetch_param('name', FALSE))) {
         self::$context = $name;
     }
 }
Пример #3
0
 }
 if ($mode == 'http' && version_compare(Git::version(), '1.7.9', '<')) {
     freepbx::out("HTTP Mode is only supported with GIT 1.7.9 or Higher");
     die;
 } elseif ($mode == 'http') {
     Git::enable_credential_cache();
 }
 if (isset($options['r'])) {
     $stash->project_key = $project;
     $repo = $stash->getRepo($options['r']);
     if ($repo === false) {
         freepbx::out("[ERROR] Unable to find " . $options['m']);
         exit(0);
     }
 } else {
     $stash = new Stash($username, $password);
     foreach ($projects as $project => $description) {
         $repo = $stash->getRepo($options['m'], $project);
         if ($repo === false) {
             freepbx::out("[WARN] " . $options['m'] . " is NOT in the " . $description);
         } else {
             break;
         }
     }
     if ($repo === false) {
         freepbx::out("[ERROR] Unable to find " . $options['m']);
         exit(0);
     }
     $uri = $mode == 'http' ? $repo['cloneUrl'] : $repo['cloneSSH'];
     $dir = $directory . '/' . $options['m'];
     freepbx::out("Cloning " . $repo['name'] . " into " . $dir);
Пример #4
0
 public function execute()
 {
     try {
         // checking and establish a live db connector
         if (empty($this->dbh)) {
             self::$db = $this->connect();
         }
         $stmt = self::$db->prepare(Stash::getQuery($this->sql_file, $this->sql_type), array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
         if ($stmt->execute()) {
             //return $stmt->fetchAll(PDO::FETCH_CLASS);
             $data = array();
             foreach (new LastIterator(new DbRowIterator($stmt)) as $row) {
                 $data[] = $row;
             }
             return $data;
         }
     } catch (PDOException $exception) {
         echo self::PDOException($exception, self::DISPLAY_TEXT);
     }
 }
Пример #5
0
 /** 
  * Constructor
  *
  * Evaluates case values and extracts the content of the 
  * first case that matches the variable parameter
  *
  * @access public
  * @return void
  */
 public function Switchee()
 {
     $this->EE =& get_instance();
     // reduce the PCRE default recursion limit to a safe level to prevent a server crash
     // (segmentation fault) when the available stack is exhausted before recursion limit reached
     // Apache *nix executable stack size is 8Mb, so safe size is 16777
     // Apache Win32 executable stack size is 256Kb, so safe size is 524
     ini_set('pcre.recursion_limit', '16777');
     // PCRE default backtrack limit is low on PHP <5.3.6
     // Increase it to the default value in newer versions of PHP
     ini_set('pcre.backtrack_limit', '1000000');
     // fetch the tagdata
     $tagdata = $this->EE->TMPL->tagdata;
     // the variable we want to find
     $var = $this->EE->TMPL->fetch_param('variable') ? $this->EE->TMPL->fetch_param('variable') : '';
     $match_all = $this->EE->TMPL->fetch_param('match') == 'all';
     // debug?
     $debug = (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('debug'));
     // register POST and GET values
     if (strncmp($var, 'get:', 4) == 0) {
         $var = filter_var($this->EE->input->get(substr($var, 4)), FILTER_SANITIZE_STRING);
     }
     if (strncmp($var, 'post:', 5) == 0) {
         $var = filter_var($this->EE->input->post(substr($var, 5)), FILTER_SANITIZE_STRING);
     }
     // register variables created by Stash
     if (strncmp($var, 'stash:', 6) == 0) {
         $var = substr($var, 6);
         if (isset($this->EE->session->cache['stash']) && isset($this->EE->session->cache['stash'][$var])) {
             // first look in the native stash variables array, for speed's sake
             $var = $this->EE->session->cache['stash'][$var];
         } else {
             // we'll need to invoke Stash itself
             if (!class_exists('Stash')) {
                 include_once PATH_THIRD . 'stash/mod.stash.php';
             }
             $var = Stash::get($var);
         }
     }
     // register global vars
     if (strncmp($var, 'global:', 7) == 0) {
         $var = substr($var, 7);
         if (array_key_exists($var, $this->EE->config->_global_vars)) {
             $var = $this->EE->config->_global_vars[$var];
         } else {
             // global has not been parsed yet, so we'll do it the hard way (this adds some overhead)
             $var = $this->EE->TMPL->parse_globals(LD . $var . RD);
         }
     }
     // log
     if ($debug) {
         $this->EE->TMPL->log_item("Switchee: evaluating variable {$var}");
     }
     // replace content inside nested tags with indexed placeholders, storing it in an array for later
     // here's the tricky bit - we only match outer tags
     /*
     $pattern = '/{switchee(?>(?!{\/?switchee).|(?R))*{\/switchee/si';
     */
     // more memory efficient version of the above...
     $pattern = '#{switchee(?>(?:[^{]++|{(?!\\/?switchee[^}]*}))+|(?R))*{\\/switchee#si';
     $tagdata = preg_replace_callback($pattern, array(get_class($this), '_placeholders'), $tagdata);
     // returns NULL on PCRE error
     if ($tagdata === NULL && $debug) {
         $this->_pcre_error();
     }
     // loop through case parameters and find a case pair value that matches our variable
     $index = 0;
     // now we need to generate a new array of tag pairs for our tagdata
     $tag_vars = $this->EE->functions->assign_variables($tagdata);
     $has_match = false;
     $temp_return_data = '';
     $default = '';
     foreach ($tag_vars['var_pair'] as $key => $val) {
         // is this tag pair a case?
         if (preg_match('/^case/', $key)) {
             // index of the case tag pair we're looking at
             $index++;
             // replace any regex in the case values with a marker
             $tagdata = str_replace($key, 'case_' . $index, $tagdata);
             // get the position of the content inside the case being evaluated
             $starts_at = strpos($tagdata, "{case_" . $index . "}") + strlen("{case_" . $index . "}");
             $ends_at = strpos($tagdata, "{/case}", $starts_at);
             if (isset($val['value'])) {
                 $val_array = array();
                 if (stristr($val['value'], '|')) {
                     $val_array = explode('|', $val['value']);
                 } else {
                     $val_array[] = $val['value'];
                 }
                 // loop through each value and look for a match
                 foreach ($val_array as $case_index => $case_value) {
                     // convert '' and "" to an actual empty string
                     if ($case_value == "''" || $case_value == '""') {
                         $case_value = '';
                     }
                     // decode any encoded characters
                     $case_value = $this->EE->security->entity_decode($case_value);
                     $var = $this->EE->security->entity_decode($var);
                     // is the case value a regular expression?
                     // check for a string contained within hashes #regex#
                     if (preg_match('/^#(.*)#$/', $case_value)) {
                         if (preg_match($case_value, $var)) {
                             // we've found a match, grab case content and exit loop
                             $temp_return_data .= substr($tagdata, $starts_at, $ends_at - $starts_at);
                             // log
                             if ($debug) {
                                 $this->EE->TMPL->log_item("Switchee: regex match: case '{$case_value}' matched variable '{$var}'");
                             }
                             $has_match = true;
                             if ($match_all) {
                                 break;
                             } else {
                                 break 2;
                             }
                         }
                     }
                     if ($case_value == $var) {
                         // we've found a match, grab case content and exit loop
                         $temp_return_data .= substr($tagdata, $starts_at, $ends_at - $starts_at);
                         // log
                         if ($debug) {
                             $this->EE->TMPL->log_item("Switchee: string match: case '{$case_value}' matched variable '{$var}'");
                         }
                         $has_match = true;
                         if ($match_all) {
                             break;
                         } else {
                             break 2;
                         }
                     }
                 }
             }
             // default value
             if (!$has_match && isset($val['default'])) {
                 $default_param = strtolower($val['default']);
                 if ($default_param == 'yes' || $default_param == 'y' || $default_param == 'true' || $default_param == '1') {
                     // found a default, save matched content but keep search for a match (continue loop)
                     $default = substr($tagdata, $starts_at, $ends_at - $starts_at);
                     // log
                     if ($debug) {
                         $this->EE->TMPL->log_item("Switchee: default case found for variable '{$var}'. This will be returned if no match is found.");
                     }
                 }
             }
         }
     }
     // fallback to default value if no matches
     if (!$has_match) {
         $temp_return_data = $default;
     }
     // replace namespaced no_results with the real deal
     $temp_return_data = str_replace(strtolower(__CLASS__) . '_no_results', 'no_results', $temp_return_data);
     // restore original content inside nested tags
     foreach ($this->_ph as $index => $val) {
         // convert the outer shell of {switchee} tag pairs to plugin tags {exp:switchee}
         // now we can do this all over again...
         $val = preg_replace(array('/^{switchee/i', '/{\\/switchee$/i'), array('{exp:switchee', '{/exp:switchee'), $val);
         $temp_return_data = str_replace('{[_' . __CLASS__ . '_' . ($index + 1) . ']', $val, $temp_return_data);
     }
     $this->return_data = $temp_return_data;
 }
 /**
  * Method for template_post_parse hook
  *
  * @param 	string	Parsed template string
  * @param 	bool	Whether an embed or not
  * @param 	integer	Site ID
  * @return 	string	Template string
  */
 public function template_post_parse($template, $sub, $site_id)
 {
     // play nice with other extensions on this hook
     if (isset($this->EE->extensions->last_call) && $this->EE->extensions->last_call) {
         $template = $this->EE->extensions->last_call;
     }
     // is this the final template?
     if ($sub == FALSE) {
         // check the cache for postponed tags
         if (!isset($this->EE->session->cache['stash']['__template_post_parse__'])) {
             $this->EE->session->cache['stash']['__template_post_parse__'] = array();
         }
         // an array of tags needing to be post-parsed
         $cache = $this->EE->session->cache['stash']['__template_post_parse__'];
         // are we capturing the final output of the rendered EE host template?
         $save_output = FALSE;
         // run any postponed stash tags
         if (!empty($cache)) {
             $context = '';
             if (!class_exists('Stash')) {
                 include_once PATH_THIRD . 'stash/mod.stash.php';
             } else {
                 // get static context if it has been set
                 $context = Stash::$context;
             }
             // save TMPL values for later
             $tagparams = isset($this->EE->TMPL->tagparams) ? $this->EE->TMPL->tagparams : array();
             $tagdata = isset($this->EE->TMPL->tagdata) ? $this->EE->TMPL->tagdata : '';
             // reset tagparams so Stash is instantiated with default values
             $this->EE->TMPL->tagparams = array();
             // instantiate but don't initialise
             $s = new Stash(TRUE);
             // sort by priority
             $cache = $s->sort_by_key($cache, 'priority', 'sort_by_integer');
             // loop through, prep the Stash instance, call the postponed tag and replace output into the placeholder
             foreach ($cache as $placeholder => $tag) {
                 // make sure there is a placeholder in the template
                 // it may have been removed by advanced conditional processing
                 if (strpos($template, $placeholder) !== FALSE) {
                     $this->EE->TMPL->log_item("Stash: post-processing tag: " . $tag['tagproper'] . " will be replaced into " . LD . $placeholder . RD);
                     $this->EE->TMPL->tagparams = $tag['tagparams'];
                     $this->EE->TMPL->tagdata = $tag['tagdata'];
                     // restore context @ pointer in context parameter
                     if (isset($this->EE->TMPL->tagparams['context']) && $this->EE->TMPL->tagparams['context'] == '@') {
                         $this->EE->TMPL->tagparams['context'] = $context;
                     }
                     // restore context @ pointer if hardcoded in name parameter
                     if (isset($this->EE->TMPL->tagparams['name']) && strncmp($this->EE->TMPL->tagparams['name'], '@:', 2) == 0) {
                         $this->EE->TMPL->tagparams['name'] = str_replace('@', $context, $this->EE->TMPL->tagparams['name']);
                     }
                     // restore context @ pointer if hardcoded in file_name parameter
                     if (isset($this->EE->TMPL->tagparams['file_name']) && strncmp($this->EE->TMPL->tagparams['file_name'], '@:', 2) == 0) {
                         $this->EE->TMPL->tagparams['file_name'] = str_replace('@', $context, $this->EE->TMPL->tagparams['file_name']);
                     }
                     // has the save_output tag been called?
                     if ($tag['method'] === 'save_output') {
                         $save_output = $tag;
                         $save_output['placeholder'] = $placeholder;
                     } else {
                         // initialise Stash with our custom tagparams
                         $s->init(TRUE);
                         $out = $s->{$tag['method']}();
                         $template = str_replace(LD . $placeholder . RD, $out, $template);
                         // remove the placeholder from the cache so we don't iterate over it in future calls of this hook
                         unset($this->EE->session->cache['stash']['__template_post_parse__'][$placeholder]);
                     }
                 }
             }
             // restore original TMPL values
             $this->EE->TMPL->tagparams = $tagparams;
             $this->EE->TMPL->tagdata = $tagdata;
         }
         // cache output to a static file
         if ($save_output) {
             $this->EE->TMPL->tagparams = $save_output['tagparams'];
             $s->init(TRUE);
             $template = str_replace(LD . $save_output['placeholder'] . RD, '', $template);
             $s->{$save_output['method']}($template);
             // restore original TMPL values
             $this->EE->TMPL->tagparams = $tagparams;
         }
         // cleanup
         unset($cache);
     }
     return $template;
 }
Пример #7
0
 /**
  * Method for template_post_parse hook
  *
  * @param   string  Parsed template string
  * @param   bool    Whether an embed or not
  * @param   integer Site ID
  * @param   bool    Has the extension been called by Stash rather than EE?
  * @param   bool    Final call of this extension
  * @return  string  Template string
  */
 public function template_post_parse($template, $sub, $site_id, $from_stash = FALSE, $final = FALSE)
 {
     // play nice with other extensions on this hook
     if (isset($this->EE->extensions->last_call) && $this->EE->extensions->last_call) {
         $template = $this->EE->extensions->last_call;
     }
     // is this the final template?
     if ($sub == FALSE && $final == FALSE) {
         // check the cache for postponed tags
         if (!isset($this->EE->session->cache['stash']['__template_post_parse__'])) {
             $this->EE->session->cache['stash']['__template_post_parse__'] = array();
         }
         // an array of tags needing to be post-parsed
         $cache = $this->EE->session->cache['stash']['__template_post_parse__'];
         // are we capturing the final output of the rendered EE host template?
         $save_output = FALSE;
         // run any postponed stash tags
         if (!empty($cache)) {
             $context = '';
             if (!class_exists('Stash')) {
                 include_once PATH_THIRD . 'stash/mod.stash.php';
             } else {
                 // get static context if it has been set
                 $context = Stash::$context;
             }
             // save TMPL values for later
             $tagparams = isset($this->EE->TMPL->tagparams) ? $this->EE->TMPL->tagparams : array();
             $tagdata = isset($this->EE->TMPL->tagdata) ? $this->EE->TMPL->tagdata : '';
             // reset tagparams so Stash is instantiated with default values
             $this->EE->TMPL->tagparams = array();
             // instantiate but don't initialise
             $s = new Stash(TRUE);
             // sort by priority
             $cache = $s->sort_by_key($cache, 'priority', 'sort_by_integer');
             // loop through, prep the Stash instance, call the postponed tag and replace output into the placeholder
             foreach ($cache as $placeholder => $tag) {
                 if (strpos($template, $placeholder) !== FALSE) {
                     // make sure there is a placeholder in the template
                     // it may have been removed by advanced conditional processing
                     $this->EE->TMPL->log_item("Stash: post-processing tag: " . $tag['tagproper'] . " will be replaced into " . LD . $placeholder . RD);
                     $this->EE->TMPL->tagparams = $tag['tagparams'];
                     $this->EE->TMPL->tagdata = $tag['tagdata'];
                     // restore context @ pointer in context parameter
                     if (isset($this->EE->TMPL->tagparams['context']) && $this->EE->TMPL->tagparams['context'] == '@') {
                         $this->EE->TMPL->tagparams['context'] = $context;
                     }
                     // restore context @ pointer if hardcoded in name parameter
                     if (isset($this->EE->TMPL->tagparams['name']) && strncmp($this->EE->TMPL->tagparams['name'], '@:', 2) == 0) {
                         $this->EE->TMPL->tagparams['name'] = str_replace('@', $context, $this->EE->TMPL->tagparams['name']);
                     }
                     // restore context @ pointer if hardcoded in file_name parameter
                     if (isset($this->EE->TMPL->tagparams['file_name']) && strncmp($this->EE->TMPL->tagparams['file_name'], '@:', 2) == 0) {
                         $this->EE->TMPL->tagparams['file_name'] = str_replace('@', $context, $this->EE->TMPL->tagparams['file_name']);
                     }
                     // initialise Stash with our custom tagparams
                     $s->init(TRUE);
                     // has the save_output or final_output tags been called?
                     if ($tag['method'] === 'save_output' || $tag['method'] === 'final_output') {
                         // remove placeholder from the template
                         $template = str_replace(LD . $placeholder . RD, '', $template);
                         // allow the called method to alter/cache the entire template
                         $template = $s->{$tag['method']}($template);
                     } else {
                         // call the tag
                         $out = $s->{$tag['method']}();
                         // replace the output of our tag into the template placeholder
                         $template = str_replace(LD . $placeholder . RD, $out, $template);
                     }
                     // remove the placeholder from the cache so we don't iterate over it in future calls of this hook
                     unset($this->EE->session->cache['stash']['__template_post_parse__'][$placeholder]);
                 }
             }
             // restore original TMPL values
             $this->EE->TMPL->tagparams = $tagparams;
             $this->EE->TMPL->tagdata = $tagdata;
         }
         // cleanup
         unset($cache);
         // just before the template is sent to output
         if (FALSE == $from_stash) {
             // batch processing of cached variables
             $this->EE->load->model('stash_model');
             // get the query queue by reference
             $queue =& $this->EE->stash_model->get_queue();
             // we need to flatten the data in the queue to a string, so we can parse it
             // first, let's extract the data into a simple indexed array...
             $data = array();
             foreach ($queue->inserts as $table => $inserts) {
                 foreach ($inserts as $query) {
                     $data[] = $query['parameters'];
                     // will always exist
                 }
             }
             foreach ($queue->updates as $table => $updates) {
                 foreach ($updates as $query) {
                     if (isset($query['parameters'])) {
                         $data[] = $query['parameters'];
                     }
                 }
             }
             if (count($data) > 0) {
                 // flatten data so we can parse it
                 $delim = '|' . $this->EE->functions->random() . '|';
                 $data = (string) implode($delim, $data);
                 // Run template_post_parse on the flattened data.
                 // We need to disable the in_progress recursion check in EE_Extensions::universal_call
                 // don't even think about making this private, @pkriete !
                 $this->EE->extensions->in_progress = '';
                 $data = $this->EE->extensions->call('template_post_parse', $data, FALSE, $this->EE->config->item('site_id'), TRUE, TRUE);
                 $this->EE->extensions->in_progress = 'template_post_parse';
                 // restore recursion check
                 // explode the data back into an array
                 $data = (array) explode($delim, $data);
                 // update the queues with the parsed parameter values
                 foreach ($queue->inserts as $table => $inserts) {
                     foreach ($inserts as $cache_key => $query) {
                         $queue->inserts[$table][$cache_key]['parameters'] = array_shift($data);
                     }
                 }
                 foreach ($queue->updates as $table => $updates) {
                     foreach ($updates as $cache_key => $query) {
                         if (isset($query['parameters'])) {
                             $queue->updates[$table][$cache_key]['parameters'] = array_shift($data);
                         }
                     }
                 }
                 unset($data);
             }
             // process inserts/updates queue
             $this->EE->TMPL->log_item("Stash: batch processing queued queries");
             $this->EE->stash_model->process_queue();
         }
     }
     return $template;
 }
Пример #8
0
 /**
  * Tagb for cleaning up specific placeholders before final output
  *
  * @access public
  * @return string 
  */
 public function finish()
 {
     /* Sample use
        ---------------------------------------------------------
        {exp:stash:finish nocache="no" compress="yes"}
        */
     // disable nocache for all template data parsed after this point?
     self::$_nocache = (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('nocache', 'y'));
     $this->process = 'end';
     $this->priority = '999998';
     //  should be the *second to last* thing post-processed (by Stash)
     if ($out = $this->_post_parse('final_output')) {
         return $out;
     }
 }
Пример #9
0
 }
 if ($mode == 'http' && version_compare(Git::version(), '1.7.9', '<')) {
     freepbx::out("HTTP Mode is only supported with GIT 1.7.9 or Higher");
     die;
 } elseif ($mode == 'http') {
     Git::enable_credential_cache();
 }
 if (isset($options['r'])) {
     $stash->project_key = $project;
     $repo = $stash->getRepo($options['r']);
     if ($repo === false) {
         freepbx::out("[ERROR] Unable to find " . $options['m']);
         exit(0);
     }
 } else {
     $stash = new Stash($username, $password);
     foreach ($projects as $project => $description) {
         $stash->project_key = $project;
         $repo = $stash->getRepo($options['m']);
         if ($repo === false) {
             freepbx::out("[WARN] " . $options['m'] . " is NOT in the " . $description);
         } else {
             break;
         }
     }
     if ($repo === false) {
         freepbx::out("[ERROR] Unable to find " . $options['m']);
         exit(0);
     }
     $uri = $mode == 'http' ? $repo['cloneUrl'] : $repo['cloneSSH'];
     $dir = $directory . '/' . $options['m'];