function testAbsoluteUrl() { $tests = array(array('base' => 'http://solutions.weblite.ca/', 'in' => 'index.html', 'out' => 'http://solutions.weblite.ca/index.html'), array('base' => 'http://solutions.weblite.ca/', 'in' => '/index.html', 'out' => 'http://solutions.weblite.ca/index.html'), array('base' => 'http://solutions.weblite.ca/foo/', 'in' => '/index.html', 'out' => 'http://solutions.weblite.ca/index.html'), array('base' => 'http://solutions.weblite.ca', 'in' => 'index.html', 'out' => 'http://solutions.weblite.ca/index.html'), array('base' => 'http://solutions.weblite.ca/foo', 'in' => 'index.html', 'out' => 'http://solutions.weblite.ca/foo/index.html'), array('base' => 'http://solutions.weblite.ca/foo/', 'in' => 'index.html', 'out' => 'http://solutions.weblite.ca/foo/index.html'), array('base' => 'https://foo.bar.com/foo/', 'in' => 'http://foo.bar.com/foo/index.html', 'out' => 'http://foo.bar.com/foo/index.html'), array('base' => 'https://foo.bar.com/foo/', 'in' => '//foo.bar.com/foo/index.html', 'out' => 'https://foo.bar.com/foo/index.html'), array('base' => 'http://foo.bar.com/foo/', 'in' => 'index.html?go=1&bar=2', 'out' => 'http://foo.bar.com/foo/index.html?go=1&bar=2')); foreach ($tests as $test) { $this->assertEquals($test['out'], SweteTools::absoluteUrl($test['in'], $test['base']), 'Converting ' . $test['in'] . ', ' . $test['base']); } }
function getPermissions($record) { if (SweteTools::isAdmin()) { $perms = Dataface_PermissionsTool::ALL(); $perms['edit'] = 0; $perms['new'] = 0; $perms['copy'] = 0; $perms['update'] = 0; $perms['update_set'] = 0; return $perms; } }
function getPermissions(&$record) { $user =& SweteTools::getUser(); if (!isset($user)) { return null; } if (SweteTools::isAdmin()) { return null; } if (isset($record)) { require_once 'inc/SweteJob.class.php'; require_once 'inc/SweteDb.class.php'; if ($record->val('posted_by') === $user->val('username')) { //error_log($record->val("job_note_id")." note posted by ".$record->val('posted_by') ." user ".$user->val('username')); return Dataface_PermissionsTool::getRolePermissions('OWNER'); } } return Dataface_PermissionsTool::getRolePermissions('READ ONLY'); }
function handle($params) { @session_write_close(); header('Connection:close'); import('inc/SweteWebpage.class.php'); $app = Dataface_Application::getInstance(); $query = $app->getQuery(); $records = df_get_selected_records($query); $errors = array(); $failed = 0; $success = 0; if ($records) { foreach ($records as $rec) { if ($rec->table()->tablename != 'webpages') { continue; } if (!$rec->checkPermission('swete:approve page')) { $errors[] = $rec->getTitle() . ' could not be approved because you don\'t have permission.'; $failed++; continue; } $site = $this->getSite($rec->val('website_id')); $username = ''; $user = SweteTools::getUser(); if ($user) { $username = $user->val('username'); } $wp = new SweteWebpage($rec); $twp = $wp->getTranslation($site->getDestinationLanguage()); try { $res = $twp->setStatus(SweteWebpage::STATUS_APPROVED, $username, @$_POST['--comments']); $success++; } catch (Exception $ex) { error_log($ex->getMessage()); $errors[] = 'Failed to approve page ' . $rec->getTitle() . '. See error log for details.'; $failed++; } } } header('Content-type: text/json; charset="' . $app->_conf['oe'] . '"'); echo json_encode(array('code' => 200, 'failed' => $failed, 'success' => $success, 'errors' => $errors)); }
function block__after_left_column() { //add info here when the user has an uncompiled job (or jobs). $app = Dataface_Application::getInstance(); if ($app->_conf['enable_jobs']) { $jobs = SweteTools::uncompiledJobs(); if (isset($jobs) && !empty($jobs)) { $url = df_absolute_url(DATAFACE_SITE_HREF . '?-table=jobs&-action=list&compiled=0&posted_by==' . SweteTools::getUser()->val('username')); echo '<div class="uncompiled-jobs">There are <a href="' . $url . '">' . count($jobs) . ' jobs ' . '</a>' . 'waiting to be compiled.</div>'; } } }
function beforeInsert($record) { if (!$record->val('posted_by') and SweteTools::getUser()) { $record->setValue('posted_by', SweteTools::getUser()->val('username')); } }
function afterInsert($record) { require_once 'inc/SweteTools.php'; SweteTools::updateDb(); // Add default text filters. df_q("insert into site_text_filters (website_id, filter_id, filter_type, filter_order)\n\t\t\tselect " . intval($record->val('website_id')) . ", filter_id, 'Prefilter', default_order\n\t\t\tfrom text_filters where is_default_prefilter=1 and \n\t\t\t(`language` IS NULL or `language`='" . addslashes($record->val('source_language')) . "')"); df_q("insert into site_text_filters (website_id, filter_id, filter_type, filter_order)\n\t\t\tselect " . intval($record->val('website_id')) . ", filter_id, 'Postfilter', default_order\n\t\t\tfrom text_filters where is_default_postfilter=1 and \n\t\t\t(`language` IS NULL or `language`='" . addslashes($record->val('source_language')) . "')"); }
/** * @private * * @brief Callback used by preg_replace when converting CSS. */ public function _cssCallback($match) { $absUrl = SweteTools::absoluteUrl($match[2], $this->_currBase); try { $hash = $this->processResource($absUrl, true); return 'url(' . $match[1] . $hash . $match[3] . ')'; } catch (Exception $ex) { return $match[0]; } }
function handle($params) { if (!SweteTools::getUser()) { return Dataface_Error::permissionDenied("You must log into access the dashboard"); } $app = Dataface_Application::getInstance(); // Get sites summary import('Dataface/ResultReader.php'); /* $reader = new Dataface_ResultReader("select ws.source_language, ws.target_language, ws.log_translation_misses, ws.website_id, ws.website_name, ws.website_url, ws.translation_memory_id, concat('http://',ws.host,ws.base_path) as proxy_url, ws.source_language, ws.target_language, ( select count(*) from webpages w where w.website_id=ws.website_id ) as numpages, ( select count(*) from swete_strings tml where tml.website_id=ws.website_id ) as numphrases, ifnull(( select sum(tml.num_words) from xf_tm_strings xts inner join swete_strings tml on tml.string_id=xts.string_id where tml.website_id=ws.website_id ), 0) as numwords, ( select count(*) from swete_strings tml inner join websites ws2 on ws2.website_id=tml.website_id inner join xf_tm_translation_memory_strings xttms on xttms.translation_memory_id=ws2.translation_memory_id and xttms.string_id=tml.string_id where xttms.current_translation_id is not null and tml.website_id=ws.website_id ) as translated_phrases, ifnull(( select sum(tml.num_words) from swete_strings tml inner join websites ws2 on ws2.website_id=tml.website_id inner join xf_tm_translation_memory_strings xttms on xttms.translation_memory_id=ws2.translation_memory_id and xttms.string_id=tml.string_id inner join xf_tm_strings xts on xttms.string_id=xts.string_id where xttms.current_translation_id is not null and tml.website_id=ws.website_id ), 0) as translated_words from websites ws ", df_db()); */ $reader = new Dataface_ResultReader("select\n\t\t\tws.source_language,\n\t\t\tws.target_language,\n\t\t\tws.log_translation_misses,\n\t\t\tws.website_id, \n\t\t\tws.website_name,\n\t\t\tws.website_url,\n\t\t\tws.translation_memory_id,\n\t\t\tconcat('http://',ws.host,ws.base_path) as proxy_url,\n\t\t\tws.source_language,\n\t\t\tws.target_language\n\t\t\t\n\t\t\t\n\t\t\t\t\n\t\t\tfrom websites ws\n\t\t\t", df_db()); $results = array(); $languages = Dataface_Table::loadTable('websites')->getValuelist('languages'); foreach ($reader as $row) { $results[] = $row; $row->untranslated_words = $row->numwords - $row->translated_words; $row->untranslated_phrases = $row->numphrases - $row->translated_phrases; $row->source_label = @$languages[$row->source_language] ? $languages[$row->source_language] : $row->source_language; $row->target_label = @$languages[$row->target_language] ? $languages[$row->target_language] : $row->target_language; } Dataface_JavascriptTool::getInstance()->import('swete/actions/dashboard.js'); $res = df_q("select count(*) from webpages"); list($numPages) = mysql_fetch_row($res); @mysql_free_result($res); $res = df_q("select count(*) num_phrases, sum(xts.num_words) as num_words from swete_strings tml \n\t\t\tleft join xf_tm_strings xts on tml.string_id=xts.string_id"); list($numPhrases, $numWords) = mysql_fetch_row($res); @mysql_free_result($res); $res = df_q("select count(*) from websites"); list($numSites) = mysql_fetch_row($res); @mysql_free_result($res); $res = df_q("select count(*) as numphrases, ifnull(sum(xts.num_words),0) as num_words from swete_strings tml \n\t\t\tinner join websites w on w.website_id=tml.website_id\n\t\t\tinner join xf_tm_translation_memory_strings xttms on w.translation_memory_id=xttms.translation_memory_id and xttms.string_id=tml.string_id\n\t\t\tinner join xf_tm_strings xts on xts.string_id=tml.string_id\n\t\t\twhere xttms.current_translation_id is not null"); list($translatedPhrases, $translatedWords) = mysql_fetch_row($res); @mysql_free_result($res); df_display(array('results' => $results, 'systemStats' => array('numWords' => $numWords, 'numPhrases' => $numPhrases, 'numSites' => $numSites, 'numPages' => $numPages, 'translatedPhrases' => $translatedPhrases, 'translatedWords' => $translatedWords, 'untranslatedWords' => $numWords - $translatedWords, 'untranslatedPhrases' => $numPhrases - $translatedPhrases), 'swete_version' => file_get_contents('version.txt')), 'swete/actions/dashboard.html'); }
public function start() { $username = ''; $user = SweteTools::getUser(); if ($user) { $username = $user->val('username'); } $res = SweteDb::q("select running from background_processes where running=1 and username='******' limit 1"); if (mysql_num_rows($res) > 0) { // There is already a process running... we'll bow out. return false; } @mysql_free_result($res); $res = SweteDb::q("update background_processes set running=1, time_started=NOW() where process_id='" . addslashes($this->processId) . "'"); if (mysql_affected_rows(df_db()) == 0) { throw new Exception("Cannot run process " . $this->processId . " because it could not find itself in the background_processes table."); } register_shutdown_function(array($this, 'cleanup')); $this->clean = false; $this->run(); SweteDb::q("update background_processes set complete=1, running=0, time_finished=NOW() where process_id='" . addslashes($this->processId) . "'"); $this->cleanup(); return true; }
public function preprocess($html) { $obj = $this->delegate(); if (isset($obj) and method_exists($obj, 'fixHtml')) { $html = $obj->fixHtml($html); } require_once 'inc/SweteTools.php'; try { $doc = SweteTools::loadHtml($html); } catch (Exception $ex) { return $html; } if (isset($obj) and method_exists($obj, 'preprocess')) { $obj->preprocess($doc); } $xpath = new DOMXPath($doc); $txt = $xpath->query('//text()'); if ($txt->length > 0) { foreach ($txt as $txtEl) { if (in_array(strtolower($txtEl->parentNode->tagName), array('style', 'title', 'script'))) { continue; } if ($txtEl->parentNode->getAttribute('data-swete-text-filter') === 'disabled') { continue; } if (!trim($txtEl->nodeValue)) { continue; } $count = 0; $nodeValue = $this->_processText(htmlspecialchars($txtEl->nodeValue), 0, $count); if ($count > 0) { $f = $doc->createDocumentFragment(); $fres = $f->appendXML($nodeValue); $txtEl->parentNode->replaceChild($f, $txtEl); } } } return $doc->saveHtml(); }
/** * @brief Processes the HTML found at a specified URL. It goes through all of the * links and adds them to the queue to be processed in later rounds. * @param string $html The HTML to be processed. * @param string $url The URL of the webpage that stored the html in $html * @returns void * */ public function processHtml($html, $url) { require_once 'lib/simple_html_dom.php'; if (strpos($url, $this->site->getSiteUrl()) !== 0) { throw new Exception("HTML could not be processed because the url {$url} lies outside the target site: " . $this->site->getSiteUrl()); } $base = $url; if ($base[strlen($base) - 1] != '/') { $base = substr($base, 0, strrpos($base, '/')); } $dom = str_get_html($html); $baseTags = $dom->find('base[href]'); foreach ($baseTags as $baseTag) { $base = $baseTag->href; if ($base[strlen($base) - 1] != '/') { $base .= '/'; } } // Now that we have our base tag, we can begin to fire away. $node = $this->addNodeAtAbsoluteUrl($url, array('type' => 'page', 'url' => $url)); if ($this->loadContent) { $this->addNodeAtAbsoluteUrl($url, array('content' => $html)); } if (isset($this->delegate) and method_exists($this->delegate, 'loadContent')) { $this->delegate->loadContent($node, $html); } // Now let's harvest the links $links = $dom->find('a[href]'); foreach ($links as $link) { $href = SweteTools::absoluteUrl($link->href, $base); if (strpos($href, $this->site->getSiteUrl()) !== 0) { // this link doesn't belong in our tree continue; } else { $this->addNodeAtAbsoluteUrl($href); } } }
/** * @brief Gets the role ID of the currently logged in user. * @return int */ static function getRole() { if (!isset(self::$role)) { $user = self::getUser(); if ($user) { self::$role = $user->val('role_id'); } } return self::$role; }
/** * @brief Creates a new webpage with the given source path. * * @see newWebpageWithProxifiedUrl() for details on other arguments. */ public function newWebpageWithSrcPath($vals, $checkExists = true, $secure = false) { if (!@$vals['webpage_url']) { throw new Exception("No path provided"); } if ($checkExists) { $page = $this->loadWebpageBySrcPath($vals['webpage_url']); if ($page) { throw new Exception("Webpage already exists"); } } $vals['webpage_url'] = SweteTools::normalizeUrl($vals['webpage_url']); if (!isset($vals['webpage_content'])) { $vals['webpage_content'] = ''; } $rec = new Dataface_Record('webpages', array()); $rec->setValues($vals); $res = $rec->save($this->getSourceLanguage(), $secure); if (PEAR::isError($res)) { throw new Exception($res->getMessage(), $res->getCode()); } $page = $this->loadWebpageBySrcPath($vals['webpage_url']); if (!$page) { throw new Exception("We have inserted a page, but cannot seem to load it."); } return $page; }
public function htmlToJson(array &$json, $html) { $doc = SweteTools::loadHtml($html); $keys = $this->_getJsonKeys($json); $this->_htmlToJson($doc, $json, $keys['text'], $keys['html']); $this->_scriptStack = array(); return json_encode($json); }