require '../../vendor/autoload.php'; require '../api/_apiHeaderGeneric.php'; require '../../libs/contentManager.php'; header("HTTP/1.0 200"); if (!empty($_POST['callback'])) { $json->callback = $_POST['callback']; } if (!empty($_POST['session']) && $json->status == 'OK') { $sessionId = $_POST['session']; if ($memcache = new Memcache()) { if ($memcache->connect('localhost', 11211, 3)) { if ($json->status == 'OK') { $session = verify_session($memcache, $sessionId, $_SERVER, __DOMAIN__); if (!empty($session)) { if (!empty($session->admin) || !empty($session->developer) || !empty($session->content_creator)) { $oDetails = getRequestDetails($session->request->request); if (!empty($oDetails->fullPath)) { $templateParser - new templateVariablesParser(); $mongo = new MongoClient(); $db = $mongo->{__MONGODB__}; $html = getPage($templateParser, $memcache, $pdo, $db, $session->language, $session->country, $session->request->request, false, $session, false, $config); libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->validateOnParse = true; $dom->loadHTML($html); $elm = $dom->getElementById($_POST['id']); if (!empty($elm)) { $json->status = 'EXISTS'; } } else { $json->status = 'ERROR';
// when a user clicks an element in the browser, jquery will c**t the number of simmular elements and stores it in the element->count variable // it also gives us the index of the selected element of count elements. this is the element->index // the $element we receive is the original element unedited. // the $_POST['innerHTML'] and $_POST['attributes'] array contain the values we want to change in the element if (!empty($element)) { $innerHTML = false; $attributes = false; $bSetTag = true; if (!empty($_POST['attributes'])) { $attributes = json_decode(base64_decode($_POST['attributes'])); } if (!empty($_POST['innerHTML'])) { $innerHTML = base64_decode($_POST['innerHTML']); } if (empty($element->id)) { $reqDetails = getRequestDetails($session->request->request, false); $dom = new DOMDocument(); //$domTemplate->validateOnParse = true; $dom->loadHTMLFile($reqDetails->fullPath); $elements = $dom->getElementsByTagName(strtolower($element->tagName)); // count how many times this element (with the same content) appears in the source file $count = 0; foreach ($elements as $key => $_element) { if (empty($_element->getAttribute('id'))) { // there can be slight differences how domdocument reads the file, or how it is interpreted by jquery in the browser. // so we are going to strip cr's ,lf's non printable chars and trim before we do any comparisons $string1 = preg_replace('|[^A-Za-z0-9]|', '', $_element->nodeValue); $string2 = preg_replace('|[^A-Za-z0-9]|', '', $element->innerText); // in jquery sometimes stuff gets put in a different case by css if (strtolower($string1) == strtolower($string2)) { $count++;
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; } }