Esempio n. 1
0
 public function parse($pdo, $memcache, $session, $language, $country, $continent, $page, $template)
 {
     $content = false;
     $sqlInsert = false;
     $count = 0;
     $vars = array();
     // find all {$xxxxx} sub strings
     preg_match_all('/{\\$(.*?)}/', $template, $vars[$count++], 2);
     // iterate all vars, replace content with content from db
     if (!empty($vars[0]) && !empty($pdo)) {
         foreach ($vars[0] as $needle) {
             $tag = $needle[1];
             if (substr($tag, 0, 1) != '_') {
                 $content = getTagContent($memcache, $pdo, $session, $tag, $language, $country, $continent, $page);
                 if ($content === false) {
                     // page variable is not found in the db, insert it into the db
                     $content = $tag;
                     if (empty($sqlInsert)) {
                         $sql = "INSERT IGNORE INTO tags SET language=:language, country=:country, element_id=:tag, content=:content, page=:page";
                         $stmt = $pdo->prepare($sql);
                         $stmt->bindParam(':language', $language);
                         $stmt->bindParam(':country', $country);
                         $stmt->bindParam(':tag', $tag);
                         $stmt->bindParam(':content', $content);
                         $stmt->bindValue(':page', $page);
                     }
                     $stmt->execute();
                     $this->assign($tag, $tag);
                 } else {
                     $this->assign($tag, $content);
                 }
             }
         }
     }
     // replace page vars with content from db
     if (!empty($this->templateVars)) {
         foreach ($this->templateVars as $key => $value) {
             $template = str_replace('{$' . $key . '}', $value, $template);
         }
     }
     return $template;
 }
Esempio n. 2
0
function getPage($memcache, $pdo, $db, $__language, $__country, $path, $directives = null, $session = null, $facebook = null, $config = null)
{
    error_log('getPage: ' . $path);
    $editMode = false;
    if (!empty($session->admin) || !empty($session->contentcreator) || !empty($session->developer)) {
        $editMode = true;
    }
    $smarty = new templateVariablesParser();
    if (!empty($directives)) {
        foreach ($directives as $key => $value) {
            $smarty->set($key, $value);
        }
    }
    if (empty($__language) || empty($__country) || empty($path)) {
        error_log('getPage called with illegal parameters');
        return false;
    }
    $oReturn = getRequestDetails($path, true);
    if (!empty($oReturn->template)) {
        $template = $oReturn->template;
        if (!empty($oReturn->fullPath)) {
            // execute the template's  php file
            if (!empty($oReturn->extension)) {
                $phpFile = str_ireplace('.' . $oReturn->extension, '.php', $oReturn->fullPath);
                if (is_file($phpFile)) {
                    include $phpFile;
                }
            }
        }
        $_path = $path;
        if (!empty($template)) {
            // iterate the dom elements that have an id, and replace elements that are found in the database
            $dom = str_get_dom(implode('', $template));
            foreach ($dom("*[id!='']") as $element) {
                if (!empty($element->id)) {
                    // if the directive '_parent' is set, then translate tags marked for the parent page i.e. set the page to whatever the parent page is
                    // if it is not set, then use the include src as page
                    if (!empty($directives)) {
                        if (!empty($directives['target'])) {
                            switch ($directives['target']) {
                                case 'parent':
                                case '_parent':
                                    $_path = $directives['parent'];
                                    break;
                                default:
                                    $_path = $directives['target'];
                                    break;
                            }
                        }
                    }
                    $content = getTagContent($memcache, $pdo, $session, $element->id, $__language, $__country, $session->continent, $_path, 5);
                    if ($content !== false) {
                        $element->setOuterText($content);
                    }
                }
            }
            // evaluate condition
            // to enable conditions we have the following attributes available
            // tag must contain a condition attr like: condition="$session->language == 'en'"
            // we enable or disable the use of conditions in the config file
            if (defined('__CONDITIONS__')) {
                if (__CONDITIONS__) {
                    foreach ($dom('[condition]') as $element) {
                        $condition = html_entity_decode($element->condition, ENT_QUOTES, 'UTF-8');
                        error_log($condition);
                        $evaluated = false;
                        if (eval($condition) === true) {
                            $evaluated = true;
                            error_log('condition = true');
                        } else {
                            error_log('condition = false');
                        }
                        if (!$evaluated) {
                            $element->detach();
                        }
                        $element->deleteAttribute('condition');
                    }
                }
            }
            // get the smarty vars from the new template and replace the vars with content from the underlying php
            //    (index.html executes an index.php in that same directory
            $template = $smarty->parse($pdo, $memcache, $session, $__language, $__country, $session->region, $_path, (string) $dom);
            unset($smarty);
            // include css. (inline them if the inline tag is set), compile the scss into css, cache the css (or get from cache)
            $dom = str_get_dom($template);
            foreach ($dom('link') as $element) {
                if (!empty($element->href) && !empty($element->inline)) {
                    // check if src translates to a local css or scss file tha we can serve
                    if (strpos($element->href, '//') === false) {
                        $parts = explode('.', $element->href);
                        if (strtolower($parts[sizeof($parts) - 1]) == 'css' || strtolower($parts[sizeof($parts) - 1]) == 'scss') {
                            $scss = false;
                            if (strtolower($parts[sizeof($parts) - 1]) == 'scss') {
                                $scss = new scssc();
                            }
                            $fn = implode('.', $parts);
                            if ($fn[0] !== '/') {
                                $fullPathContent = DOCUMENT_ROOT . '/' . $fn;
                            } else {
                                $fullPathContent = DOCUMENT_ROOT . $fn;
                            }
                            $content = false;
                            if (empty($element->nocache)) {
                                $content = $memcache->get($session->id . '_' . $fullPathContent);
                            }
                            if (empty($content)) {
                                $partsPHP = $parts;
                                $partsPHP[sizeof($partsPHP) - 1] = 'php';
                                $fn = implode('.', $partsPHP);
                                if ($fn[0] !== '/') {
                                    $fullPathPHP = DOCUMENT_ROOT . '/' . $fn;
                                } else {
                                    $fullPathPHP = DOCUMENT_ROOT . $fn;
                                }
                                $templateParser = new templateVariablesParser();
                                if (is_file($fullPathPHP)) {
                                    include $fullPathPHP;
                                }
                                $parsed = false;
                                if (is_file($fullPathPHP)) {
                                    include $fullPathPHP;
                                    $parsed = true;
                                }
                                $content = file_get_contents(trim($fullPathContent));
                                if ($parsed) {
                                    $content = $templateParser->parseMinimal($content);
                                }
                                if (!empty($scss)) {
                                    $content = $scss->compile($content);
                                }
                                if (empty($element->nocache)) {
                                    $memcache->set($session->id . '_' . $fullPathContent, $content);
                                }
                                unset($templateParser);
                                $element->setOuterText("<style>" . $content . "</style>");
                            } else {
                                $element->setOuterText("<style>" . $content . "</style>");
                            }
                        }
                    }
                }
            }
            // include script. (inline if attribute is set)
            foreach ($dom('script') as $element) {
                if (!empty($element->src) && !empty($element->inline)) {
                    if (strpos($element->src, "//") === false) {
                        $parts = explode('.', $element->src);
                        if (strtolower($parts[sizeof($parts) - 1]) == 'js') {
                            $fn = implode('.', $parts);
                            if ($fn[0] !== '/') {
                                $fullPathContent = DOCUMENT_ROOT . '/' . $fn;
                            } else {
                                $fullPathContent = DOCUMENT_ROOT . $fn;
                            }
                            $content = false;
                            if (empty($element->nocache)) {
                                $content = $memcache->get($session->id . '_' . $fullPathContent);
                            }
                            $content = $memcache->get($session->id . '_' . $fullPathContent);
                            if (empty($content)) {
                                $partsPHP = $parts;
                                $partsPHP[sizeof($partsPHP) - 1] = 'php';
                                $fn = implode('.', $partsPHP);
                                if ($fn[0] !== '/') {
                                    $fullPathPHP = DOCUMENT_ROOT . '/' . $fn;
                                } else {
                                    $fullPathPHP = DOCUMENT_ROOT . $fn;
                                }
                                $templateParser = new templateVariablesParser();
                                $parsed = false;
                                if (is_file($fullPathPHP)) {
                                    include $fullPathPHP;
                                    $parsed = true;
                                }
                                $content = file_get_contents(trim($fullPathContent));
                                if ($parsed) {
                                    $content = $templateParser->parseMinimal($content);
                                }
                                $element->setOuterText("<script>" . $content . "</script>");
                                if (empty($element->nocache)) {
                                    $memcache->set($session->id . '_' . $fullPathContent, $content);
                                }
                                unset($templateParser);
                            } else {
                                $element->setOuterText("<script>" . $content . "</script>");
                            }
                        }
                    }
                }
            }
            // handle include elements
            //
            // Normally the internal template variables are saved in the db under the snippet src name
            // There are instances that we would like to use the parent as the as the source of translation
            //  In that case use the target="_parent" attribute
            //  We can also specify _parent or _self or XXXX for another page name.
            foreach ($dom('include') as $element) {
                $language = $session->language;
                $country = $session->country;
                if (!empty($element->language)) {
                    $language = $element->language;
                }
                if (!empty($element->country)) {
                    $country = $element->country;
                }
                $directives = array();
                if (!empty($element->target)) {
                    $directives['parent'] = $path;
                    $directives['target'] = $element->target;
                }
                if (!empty($element->src)) {
                    $content = getPage($memcache, $pdo, $db, $language, $country, $element->src, $directives, $session, $facebook, $config);
                    if (!empty($content)) {
                        if ($editMode) {
                            $includeDom = str_get_dom($content);
                            // below foreach is required for editing purposes. (When we edit we need to know which file we need to save)
                            foreach ($includeDom("*[id!='']") as $_element) {
                                $_element->addAttribute('data-src', $element->src);
                            }
                            $element->setOuterText((string) $includeDom);
                        } else {
                            $element->setOuterText($content);
                        }
                    } else {
                        $element->setOuterText('');
                        error_log('Include element not found. path: ' . $element->src);
                    }
                }
            }
            return (string) $dom;
        }
        return '';
    } else {
        error_log('** NOT FOUND ** domain: ' . $_SERVER['SERVER_NAME'] . ', request: ' . $path . ', language: ' . $__language . ', country: ' . $__country);
        return false;
    }
}
Esempio n. 3
0
                 require ENGINE_ROOT . "/libs/facebook/facebook.php";
                 $templateParser = new templateVariablesParser();
                 $mongo = new MongoClient();
                 $db = $mongo->{__MONGODB__};
                 $facebook = new Facebook(array('appId' => __FB_APP_ID__, 'secret' => __FB_SECRET_APP_ID__, 'cookie' => true));
                 $smarty = getSmartyHandle($paths, 'smarty');
                 $content = getPage($templateParser, $memcache, $pdo, $db, $_POST['_language'], $_POST['_country'], $_POST['_request'], false, $session, $facebook, $config);
                 $doc = phpQuery::newDocument($content);
                 foreach ($doc['body a[id]'] as $node) {
                     foreach ($node as $key => $value) {
                         $s = $value;
                     }
                     $attributes = pq($node);
                     $id = pq($node)->attr('id');
                     $html = pq($node)->serializeArray();
                     $_content = getTagContent($memcache, $pdo, $session, $id, $__language, $__country, $__continent, $_POST['_request'], 5);
                     if ($_content !== false) {
                         pq($node)->replaceWith($_content);
                     }
                 }
             }
         } else {
             $json->status = 'ERROR';
             $json->message = 'Invalid session';
         }
     }
 } catch (PDOException $e) {
     error_log(__FILE__ . ' : ' . $e->getMessage());
     $json->status = 'ERROR';
     $json->message = 'PDOException: ' . $e->getMessage();
 } catch (MemcacheException $e) {