function setConfigItem($configPath, $key, $value) { $key = sanitiseStringToAlphaNumeric($key); $value = sanitiseToIniValue($value); $currentValue = getConfigItem($configPath, $key); if (!is_writable($configPath)) { webServiceError('&error-config-file-not-writable;', 500, array('path' => $configPath)); } $newConfigItemLine = $key . '="' . $value . '"'; if ($currentValue === null) { $iniFilePointer = fopen($configPath, 'a'); fwrite($iniFilePointer, "\n" . $newConfigItemLine . "\n"); fclose($iniFilePointer); chmod($configPath, 0600); //security! return; } $iniLines = file($configPath); $newIniLines = array(); $replaceExistingLine = False; foreach ($iniLines as $iniLine) { if (stringStartsWith($iniLine, $key) && ($currentValue == null || containsString($iniLine, $currentValue))) { $newIniLines[] = $newConfigItemLine . "\n"; } elseif (trim($iniLine) != '') { $newIniLines[] = $iniLine; } } file_put_contents($configPath, implode('', $newIniLines)); chmod($configPath, 0600); //security! }
function onMessage($from, $channel, $msg) { //Only trigger on !botlog if (stringStartsWith($msg, "{$this->config['trigger']}botlog")) { //Get hold of number of rows to show and possible password $tmp = explode(" ", $msg); $pass = ''; if (count($tmp) == 3) { $pass = $tmp[1]; $limit = $tmp[2]; } else { if (count($tmp) == 2) { $limit = $tmp[1]; } } if (!is_numeric($limit)) { $limit = 10; } if (strlen($this->config['adminPass']) > 0 && $pass != $this->config['adminPass']) { sendMessage($this->socket, $channel, "{$from}: Wrong password"); } else { //Password auth ok, display log data sendMessage($this->socket, $channel, "{$from}: Last {$limit} entries from bot log:"); $logdata = file('logs/vikingbot.log'); $rows = count($logdata); for ($i = $rows - $limit; $i < $rows; $i++) { sendMessage($this->socket, $channel, "{$logdata[$i]}"); //Avoid Excess flood kick from server usleep(500000); } sendMessage($this->socket, $channel, "------------"); } } }
function subtractURLsStart($a, $b) { //Removes $b from the beginning of $a, if possible. Else errors. if (stringStartsWith($a, $b)) { return substr_b($a, strlen($b)); } else { trigger_error("URL processing error: subtractURLsStart('" . htmlentities($a) . "','" . htmlentities($b) . "')", E_USER_ERROR); } }
function shortenURL($url) { $offset = 0; if (stringStartsWith($url, "https")) { $offset = 9; } else { if (stringStartsWith($url, "http")) { $offset = 8; } } $main = substr($url, 0, strpos($url, "/", $offset)); $file = strrchr($url, "/"); return $main . "/..." . $file; }
function onMessage($from, $channel, $msg) { if (stringStartsWith($msg, "{$this->config['trigger']}op")) { $bits = explode(" ", $msg); $pass = @$bits[3]; $who = @$bits[1]; $where = @$bits[2]; if (strlen($this->config['adminPass']) > 0 && $pass != $this->config['adminPass']) { sendMessage($this->socket, $channel, "{$from}: Wrong password"); } else { if (strlen($who) == 0 || strlen($where) == 0) { sendMessage($this->socket, $channel, "{$from}: Syntax: {$this->config['trigger']}op nick #channel [adminPassword]"); } else { sendData($this->socket, "MODE {$where} +o {$who}"); sendMessage($this->socket, $channel, "{$from}: OP has been attempted given, note that i must be OP myself to do it."); } } } }
public function onMessage($from, $channel, $msg) { if (stringStartsWith($msg, "{$this->config['trigger']}upgrade")) { $bits = explode(" ", $msg); $pass = $bits[1]; if (strlen($this->config['adminPass']) > 0 && $pass != $this->config['adminPass']) { sendMessage($this->socket, $channel, "{$from}: Wrong password"); } else { $restartRequired = false; sendMessage($this->socket, $channel, "{$from}: Starting upgrade of bot and its plugins..."); $response = trim(shell_exec("git pull")); if (empty($response)) { sendMessage($this->socket, $channel, "{$from}: Error upgrading core. Check permissions!"); } elseif ($response != 'Already up-to-date.') { $restartRequired = true; sendMessage($this->socket, $channel, "{$from}: Upgrading core: {$response}"); } $coreDir = getcwd(); $pluginsRecDirIterator = new RecursiveDirectoryIterator('./'); foreach (new RecursiveIteratorIterator($pluginsRecDirIterator) as $gitDir) { if (stringEndsWith($gitDir, ".git/..")) { chdir($gitDir); $response = trim(shell_exec("git pull")); if (empty($response)) { sendMessage($this->socket, $channel, "{$from}: Error upgrading sub git. Check permissions!"); } elseif ($response != 'Already up-to-date.') { $restartRequired = true; sendMessage($this->socket, $channel, "{$from}: Upgrading sub git: {$response}"); } chdir($coreDir); } } if ($restartRequired) { sendMessage($this->socket, $channel, "{$from}: Restarting..."); sendData($this->socket, 'QUIT :Restarting due to upgrade'); die(exec('sh start.sh > /dev/null &')); } else { sendMessage($this->socket, $channel, "{$from}: Everything up to date, not restarting."); } } } }
function onMessage($from, $channel, $msg) { if (stringStartsWith($msg, "{$this->config['trigger']}upgrade")) { $bits = explode(" ", $msg); $pass = $bits[1]; if (strlen($this->config['adminPass']) > 0 && $pass != $this->config['adminPass']) { sendMessage($this->socket, $channel, "{$from}: Wrong password"); } else { sendMessage($this->socket, $channel, "{$from}: Starting upgrade..."); $response = trim(shell_exec("git pull")); if ($response == 'Already up-to-date.') { sendMessage($this->socket, $channel, "{$from}: The bot is already up to date, not restarting."); } else { sendMessage($this->socket, $channel, "{$from}: {$response}"); sendMessage($this->socket, $channel, "{$from}: Restarting..."); sendData($this->socket, 'QUIT :Restarting due to upgrade'); die(exec('sh start.sh > /dev/null &')); } } } }
public function defaultUpdate(Request $request, $item) { // Uppfæra öll "translatables"... foreach (config('formable.locales') as $locale) { foreach ($request->all() as $k => $v) { if (stringEndsWith($k, '_' . $locale)) { $keyName = substr($k, 0, -3); // Uppfæra líka default attribute... if ($locale === config('app.fallback_locale')) { $item->{$keyName} = $v; } if (in_array($keyName, $item->translatable)) { $item->translations($locale)->add($keyName, $v); } } } } //dd($request->all()); // Uppfæra öll "extras"... foreach (config('formable.locales') as $locale) { foreach ($request->all() as $k => $v) { if (stringStartsWith($k, 'extra_' . $locale)) { $keyName = substr($k, 9); if ($item->hasExtra($keyName)) { $item->extras($locale)->add($keyName, $v); } } } if (property_exists($this, 'fillableExtras')) { foreach ($item->fillableExtras as $fe_key => $fe_val) { $_k = 'extra_' . $locale . '_' . $fe_key; if (!$request->has($_k)) { $item->extras($locale)->add($fe_key, ''); } } } } }
$zipsInPreviewDirectory = glob($allDocumentsPreviewDirectory . '*.zip'); if (count($zipsInPreviewDirectory)) { foreach ($zipsInPreviewDirectory as $zipInPreviewDirectory) { silentlyUnlink($zipInPreviewDirectory); if (file_exists($zipInPreviewDirectory)) { webServiceError('Docvert internal error: unable to remove ZIP file at "' . $zipInPreviewDirectory . '"'); } } $zipFilePath = $zipsInPreviewDirectory[0]; } else { $zipFileName = chooseNameOfZipFile($allDocumentsPreviewDirectory); $zipFilePath = $allDocumentsPreviewDirectory . $zipFileName; } $filesInPreviewDirectory = glob($previewDirectory . '*'); foreach ($filesInPreviewDirectory as $fileInPreviewDirectory) { if (!stringStartsWith(basename($fileInPreviewDirectory), "docvert") && !stringEndsWith(basename($fileInPreviewDirectory), "wmf") && !stringEndsWith(basename($fileInPreviewDirectory), "gif") && !stringEndsWith(basename($fileInPreviewDirectory), "png") && !stringEndsWith(basename($fileInPreviewDirectory), "jpeg") && !stringEndsWith(basename($fileInPreviewDirectory), "jpg") && !stringEndsWith(basename($fileInPreviewDirectory), "svg")) { //print 'Delete: '.$fileInPreviewDirectory.'<br />'; silentlyUnlink($fileInPreviewDirectory); } else { //print 'Retain: '.$fileInPreviewDirectory.'<br />'; } } $docbookPath = $previewDirectory . 'docvert--all-docbook.xml'; $docbook = file_get_contents($docbookPath); $docbook = str_replace('{{body}}', $docbookBody, $docbook); $docbook = str_replace('{{title}}', $docbookTitle, $docbook); $contentPath = $previewDirectory . 'content.xml'; file_put_contents($contentPath, $docbook); $pipelineToUse = $pipeline; $autoPipeline = $autopipeline; $skipAheadToDocbook = true;
public function defaultUpdate(Request $request, $item) { // Uppfæra öll "translatables"... foreach (config('formable.locales') as $locale) { foreach ($request->all() as $k => $v) { if (stringEndsWith($k, '_' . $locale)) { $keyName = substr($k, 0, -3); // Uppfæra líka default attribute... if ($locale === config('app.fallback_locale')) { $item->{$keyName} = $v; } if (in_array($keyName, $item->translatable)) { $item->translations($locale)->add($keyName, $v); } } } } // Uppfæra öll "extras"... foreach (config('formable.locales') as $locale) { foreach ($request->all() as $k => $v) { if (stringStartsWith($k, 'extra_' . $locale)) { $keyName = substr($k, 9); if (array_key_exists($keyName, $item->extras[$locale])) { $item->extras($locale)->add($keyName, $v); } } } } }
function kalMenuArray($args = array(), $pages = false, $lvl = 1, &$menu = "", &$paths = array()) { if (isset($args['hidden']) && $args['hidden']) { if (!$pages) { $pages = App\Page::where('accordion', 0)->where('parent_id', 0)->orderBy('order')->get(); } } else { if (!$pages) { $pages = App\Page::where('accordion', 0)->where('parent_id', 0)->where('status', '>', 0)->orderBy('order')->get(); } } /*$menu .= "<div>";*/ if (isset($args['maxlvl']) && $lvl >= $args['maxlvl']) { return $menu; } foreach ($pages as $page) { if ($page->slug[0] == '_') { continue; } if ($lvl == 1 && isset($args['topmenu']) && !$page->topmenu) { continue; } $className = "App\\" . ucfirst($page->hlutur); $m = new $className(); $_id = $page->id; if (strtolower(trim($page->modelName())) != strtolower(trim($page->hlutur))) { $_id = 0; } if (isset($args['hidden']) && $args['hidden']) { $subs = $m->where('accordion', 0)->where($m->parent_key, $_id)->orderBy('order')->get(); } else { $subs = $m->where('accordion', 0)->where($m->parent_key, $_id)->where('status', '>', 0)->orderBy('order')->get(); } $paths[] = $page->slug; $path = "/"; $active = \Request::segment($lvl) == $page->slug ? 'active' : ''; //echo $lvl.' '.$page->path.' '.$active.'<br>'; $being_viewed = \Request::path() == rtrim(ltrim($page->path, '/'), '/') ? 'being_viewed' : ''; $has_subs = !$subs->isEmpty() ? 'has_subs' : ''; foreach ($paths as $k => $p) { $path .= "{$p}/"; } // Commenta út ef þú vilt ekki sýna síður með "óþýdda" titla... if (!$page->translations(lang())->has('title')) { continue; } $title = $page->translation('title') ?: $page->title; if ($page->translations(lang())->has('slug')) { if ($page->translation('slug') != '') { $page->path = str_replace($page->slug, $page->translation('slug'), $page->path); } } $_link = stringStartsWith($page->url, '#') ? \Request::root() . "/" . $page->url : $page->path; $smooth = ''; if (\Request::is('/')) { $_link = stringStartsWith($page->url, '#') ? $page->url : $page->path; $smooth = stringStartsWith($page->url, '#') ? 'data-uk-smooth-scroll="{offset:30}"' : ''; } $menu .= "<div class='lvl-{$lvl} {$active} {$being_viewed} {$has_subs}'><a href='{$_link}' {$smooth}>{$title}</a>"; if ($active || isset($args['expand_all']) && $args['expand_all']) { if (!$subs->isEmpty()) { $lvl++; kalMenuArray($args, $subs, $lvl, $menu, $paths); $lvl--; } } array_pop($paths); $menu .= "</div>"; } $menu .= "<div class='mobile-button'><a><i class='fa fa-bars'></i></a></div>"; /*$menu .= "</div>";*/ return $menu; }
function showGenerationStep() { $docvertDir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR; $disallowDocumentGeneration = getGlobalConfigItem('doNotAllowDocumentGeneration'); if ($disallowDocumentGeneration == 'true') { return $this->getThemeFragment('generation-disabled.htmlf'); } if (isset($_REQUEST['step'])) { switch ($_REQUEST['step']) { case '4': if (!isset($_REQUEST['pages'])) { webServiceError('&error-webpage-generation-no-pages;'); } $template = $this->getThemeFragment('generation-step4.htmlf'); $hiddenFormChosenPages = array(); $listItems = array(); foreach ($_REQUEST['pages'] as $page) { $listItems[] = "\n\t\t\t\t" . '<li>' . $page . '</li>'; $hiddenFormChosenPages[] = "\n\t\t\t\t" . '<input type="hidden" name="pages[]" value="' . $page . '"/>'; } $template = str_replace('{{page-order}}', implode($listItems), $template); $template = str_replace('{{hidden-form-chosen-pages}}', implode($hiddenFormChosenPages), $template); $generatorPipelines = glob($this->docvertRootDirectory . 'generator-pipeline' . DIRECTORY_SEPARATOR . '*'); $generatorPipelinesArray = array(); foreach ($generatorPipelines as $generatorPipeline) { $generatorName = basename($generatorPipeline); $generatorPipelinesArray[] = '<option value="' . $generatorName . '">' . $generatorName . '</option>'; } return str_replace('{{generator-pipelines}}', implode('', $generatorPipelinesArray), $template); case '3': $template = $this->getThemeFragment('generation-step3.htmlf'); $listItems = array(); foreach ($_REQUEST['pages'] as $page) { $listItems[] = "\n\t\t\t\t" . '<option value="' . $page . '">' . $page . '</option>'; } return str_replace('{{chosen-scrape-urls}}', implode($listItems), $template); case '2': if (!isset($_REQUEST['url'])) { webServiceError('&error-webpage-generation-url;'); } $originalUrl = $_REQUEST['url']; if (trim($originalUrl) == '') { webServiceError('&error-webpage-generation-no-url-given;'); } if (!stringStartsWith($originalUrl, 'http')) { $originalUrl = 'http://' . $originalUrl; } $originalUrl = str_replace(array("\n", "\r", "\t", " "), '', $originalUrl); include_once dirname(__FILE__) . '/http.php'; if (trim(getUrlLocalPart($originalUrl)) == '') { $originalUrl = followUrlRedirects($originalUrl . '/'); } else { $originalUrl = followUrlRedirects($originalUrl); } if ($originalUrl === false) { webServiceError('&error-webpage-cannot-get-url;', 500, array('url' => $originalUrl)); } $page = file_get_contents($originalUrl); $baseTagPattern = "/<base[^>]*?href=([^>]*?)>/is"; preg_match($baseTagPattern, $page, $matches); if (count($matches) > 0) { $originalUrl = trim($matches[1]); $originalUrl = substr($originalUrl, 1, strlen($originalUrl) - 2); } $url = $originalUrl; $connectionPart = getUrlConnectionPart($url); $getUrlLocalPart = getUrlLocalPart($url); $localPartDirectory = getUrlLocalPartDirectory($url); $links = array(); $matches = null; preg_match_all('/href="(.*?)"/', $page, $matches); $matches = $matches[1]; $urls = array(); $urls[$originalUrl] = 'value that does not matter'; foreach ($matches as $match) { $link = $match; if (stringStartsWith($link, '/')) { $link = $connectionPart . $link; } elseif (stringStartsWith($link, "http://") || stringStartsWith($link, "https://")) { } elseif (stringStartsWith($link, "mailto:")) { } else { $link = $connectionPart . resolveRelativeUrl($localPartDirectory . $link); } if (containsString($link, '#')) { $link = substringBefore($link, '#'); } if (stringEndsWith($link, '?')) { $link = substringBefore($link, '?'); } if (stringStartsWith($link, 'http')) { $fileExtension = substr($link, strrpos($link, '.') + 1); switch ($fileExtension) { case 'avi': case 'mov': case 'mpg': case 'css': case 'jpeg': case 'jpg': case 'gif': case 'png': case 'bmp': case 'apng': case 'tiff': case 'ico': case 'js': case 'gz': case 'tar': case 'zip': case 'bin': case 'sit': case 'mp3': case 'mp4': case 'wav': case 'swf': case 'fla': case 'rss': case 'atom': case 'pdf': case 'xls': case 'doc': case 'txt': case 'pps': break; default: $urls[$link] = 'value that does not matter'; } } } $urls = array_keys($urls); $mostLikelyUrls = array(); $possibleUrls = array(); $unlikelyUrls = array(); $numberOfSlashesInOriginalUrl = strlen($originalUrl) - strlen(str_replace('/', '', $originalUrl)); foreach ($urls as $url) { $url = followUrlRedirects($url); if (trim($url) != '') { $numberOfSlashesInUrl = strlen($url) - strlen(str_replace('/', '', $url)); if (stringStartsWith($url, $connectionPart . $localPartDirectory) && $numberOfSlashesInUrl == $numberOfSlashesInOriginalUrl) { $mostLikelyUrls[] = $url; } elseif (stringStartsWith($url, $connectionPart)) { $possibleUrls[] = $url; } else { $unlikelyUrls[] = $url; } } } asort($unlikelyUrls); $itemId = 0; foreach ($mostLikelyUrls as $url) { $links[] = '<li class="orderingItem"><label for="urlId' . $itemId . '"><input type="checkbox" name="pages[]" value="' . $url . '" id="urlId' . $itemId . '" checked="checked"/><span class="title">' . $url . '</label></span></li>' . "\n"; $itemId++; } foreach ($possibleUrls as $url) { $links[] = '<li class="orderingItem"><label for="urlId' . $itemId . '"><input type="checkbox" name="pages[]" value="' . $url . '" id="urlId' . $itemId . '"/><span class="title">' . $url . '</label></span></li>' . "\n"; $itemId++; } foreach ($unlikelyUrls as $url) { $links[] = '<li class="orderingItem"><label for="urlId' . $itemId . '"><input type="checkbox" name="pages[]" value="' . $url . '" id="urlId' . $itemId . '"/><span class="title">' . $url . '</label></span></li>' . "\n"; $itemId++; } $step2Template = $this->getThemeFragment('generation-step2.htmlf'); $step2Template = str_replace('{{scrape-results}}', implode('', $links), $step2Template); $step2Template = str_replace('{{scrape-url}}', $url, $step2Template); return $step2Template; default: return $this->getThemeFragment('generation-step1.htmlf'); } } else { return $this->getThemeFragment('generation-step1.htmlf'); } }
function generateDocument($pages, $generatorPipeline) { if (preg_match('/.\\//s', $generatorPipeline)) { webServiceError('&error-disallowed-characters;'); } $userAgent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:bignumber) Docvert'; $httpContextOptions = array('http' => array('header' => 'User-Agent: ' . $userAgent)); $httpContext = stream_context_create($httpContextOptions); $docvertDir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR; $docvertWritableDir = getWritableDirectory(); $disallowDocumentGeneration = getGlobalConfigItem('doNotAllowDocumentGeneration'); if ($disallowDocumentGeneration == 'true') { webServiceError('&document-generation-disabled;'); } $pageXml = '<c:document xmlns="http://www.w3.org/1999/xhtml" xmlns:c="container">' . "\n"; $pageTemplate = "\n\t" . '<c:page url="{{url}}" {{baseUrl}}>{{page}}</c:page>' . "\n"; $config = array('indent' => true, 'output-xhtml' => true, 'wrap' => 200); if (!class_exists('tidy')) { webServiceError('&tidy-is-not-installed;'); } $tidy = new tidy(); $baseTagPattern = "/<base[^>]*?href=([^>]*?)>/is"; foreach ($pages as $page) { if (trim($page) != '' && (stringStartsWith($page, 'http://') || stringStartsWith($page, 'https://'))) { $pageHtml = file_get_contents($page, null, $httpContext); $tidy->parseString($pageHtml, $config, 'utf8'); $tidy->cleanRepair(); $thisPage = str_replace('{{url}}', $page, $pageTemplate); $baseUrl = ''; //supporting that ugly old hack of <base> preg_match($baseTagPattern, $pageHtml, $matches); if (count($matches) > 0) { $baseUrl = 'baseUrl="' . substr($matches[1], 1, -2) . '"'; } $thisPage = str_replace('{{baseUrl}}', $baseUrl, $thisPage); $tidiedPageContents = characterEntityToNCR(removeDoctype(removeXmlComments($tidy))); $styleTagPattern = "/<style.*?<\\/style>/is"; $tidiedPageContents = preg_replace($styleTagPattern, '', $tidiedPageContents); $scriptTagPattern = "/<script.*?<\\/script>/is"; $tidiedPageContents = preg_replace($scriptTagPattern, '', $tidiedPageContents); $questionMarkPattern = "/<\\?.*?\\?>/is"; //as strangely used on news.yahoo.com $tidiedPageContents = preg_replace($questionMarkPattern, '', $tidiedPageContents); $thisPage = str_replace('{{page}}', $tidiedPageContents, $thisPage); $pageXml .= $thisPage; } } $pageXml .= '</c:document>'; $temporaryDirectory = getTemporaryDirectory(); $pipelineDirectory = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'generator-pipeline' . DIRECTORY_SEPARATOR . $generatorPipeline . DIRECTORY_SEPARATOR; $pipelinePath = $pipelineDirectory . 'pipeline.xml'; if (!file_exists($pipelinePath)) { webServiceError('&generation-pipeline-not-found; ' . revealXml($pipelinePath)); } $pipelineString = file_get_contents($pipelinePath); $pipelineString = substr($pipelineString, strpos($pipelineString, '<pipeline>') + 10); $pipelineString = substr($pipelineString, 0, strpos($pipelineString, '</pipeline>')); $pipelineStages = xmlStringToArray($pipelineString); $pipelineSettings = array("pipeline" => $generatorPipeline, "autopipeline" => $generatorPipeline); processAPipelineLevel($pipelineStages, $pageXml, $pipelineDirectory, $temporaryDirectory, $temporaryDirectory, $pipelineSettings); $openDocumentPath = $temporaryDirectory . 'output.odt'; zipFiles($temporaryDirectory, $openDocumentPath); header('Content-disposition: attachment; filename=' . basename($openDocumentPath)); header('Content-type: application/vnd.oasis.opendocument.text'); readfile($openDocumentPath); }
function process($currentXml) { $extractImagesPath = $this->docvertTransformDirectory . 'extract-pages-html-images-and-links.xsl'; $htmlUrls = trim(xsltTransform($currentXml, $extractImagesPath)); $htmlUrlLines = explode("\n", $htmlUrls); $imageUrls = array(); foreach ($htmlUrlLines as $htmlUrlLine) { if (trim($htmlUrlLine) == '') { continue; } $urlLineParts = explode("\t", $htmlUrlLine); $urlType = $urlLineParts[0]; $baseUrl = $urlLineParts[1]; $possiblyRelativeUrl = $urlLineParts[2]; $fullUrl = ''; if (stringStartsWith($possiblyRelativeUrl, "http://") || stringStartsWith($possiblyRelativeUrl, "https://") || stringStartsWith($possiblyRelativeUrl, "mailto:")) { $fullUrl = $possiblyRelativeUrl; } else { $connectionPart = getUrlConnectionPart($baseUrl); $getUrlLocalPart = getUrlLocalPart($baseUrl); $localPartDirectory = getUrlLocalPartDirectory($baseUrl); if (stringStartsWith($possiblyRelativeUrl, '/')) { $fullUrl = $connectionPart . $possiblyRelativeUrl; } else { $relativePath = resolveRelativeUrl($localPartDirectory . $possiblyRelativeUrl); if (!stringStartsWith($relativePath, '/')) { $relativePath = '/' . $relativePath; } $fullUrl = $connectionPart . $relativePath; } } $missingImagePlaceholderImagePath = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . '404image.gif'; if (!file_exists($missingImagePlaceholderImagePath)) { webServiceError('&dynamic-error-process-downloadimagesandsetlinks-missing-placeholder;', 500, array('fourOhFourImagePath' => $fourOhFourImagePath)); } $fullUrl = html_entity_decode($fullUrl); switch ($urlType) { case 'image': $imageData = file_get_contents($fullUrl); if ($imageData == null) { $imageData = file_get_contents($missingImagePlaceholderImagePath); } $picturesDirectory = $this->contentDirectory . DIRECTORY_SEPARATOR . 'Pictures'; if (!file_exists($picturesDirectory)) { mkdir($picturesDirectory); } if (!function_exists('imagecreatefromstring')) { webServiceError('&error-process-downloadimagesandsetlinks-missing-gd;'); } $imageResource = imagecreatefromstring($imageData); if (!$imageResource) { $imageResource = imagecreatefromstring(file_get_contents($missingImagePlaceholderImagePath)); } $imageWidth = imagesx($imageResource); $imageHeight = imagesy($imageResource); $fileExtension = substr($fullUrl, strrpos($fullUrl, '.') + 1); switch ($fileExtension) { case 'jpg': case 'jpeg': case 'gif': case 'png': break; default: $fileExtension = 'jpg'; } $openDocumentPath = 'Pictures/' . md5($fullUrl) . '.' . $fileExtension; file_put_contents($this->contentDirectory . DIRECTORY_SEPARATOR . $openDocumentPath, $imageData); $imageUrlReplacement = $openDocumentPath . '" c:width="' . $imageWidth . '" c:height="' . $imageHeight; //FIXME: assumes image @src has double-quote and not single $currentXml = str_replace('"' . $possiblyRelativeUrl . '"', '"' . $imageUrlReplacement . '"', $currentXml); break; case 'link': $linkUrl = $urlLineParts[2]; //print '"'.$possiblyRelativeUrl.' vs '.$fullUrl.'<hr />'; $currentXml = str_replace('"' . $possiblyRelativeUrl . '"', '"' . htmlentities($fullUrl) . '"', $currentXml); break; } } return $currentXml; }
function followUrlRedirects($url, $maximumNumberOfOfRedirects = false) { if ($maximumNumberOfOfRedirects === false) { $maximumNumberOfOfRedirects = 10; } $finalRedirectionUrl = false; $redirectPath = array(); $numberOfRedirectsRemaining = $maximumNumberOfOfRedirects; $timeOutInSeconds = 5; while ($finalRedirectionUrl == false) { $previousUrl = $url; $wasRedirectedThisTime = false; $domainAndPort = getUrlDomainAndPortPart($url); $redirectPath[] = $url; $localPart = getUrlLocalPart($url); $result = pullpage('HEAD', $domainAndPort[0], $domainAndPort[1], $localPart, false, false, false, false, null, null, false, $timeOutInSeconds); if ($result === false) { return null; } $result = explode("\n", $result); foreach ($result as $line) { $headParts = explode(':', $line); if (count($headParts) > 1) { $subject = array_shift($headParts); $value = implode(':', $headParts); if (strtolower(trim($subject)) == "location") { $url = trim($value); if (!stringStartsWith($url, 'http://') && !stringStartsWith($url, 'https://')) { $url = 'http://' . $domainAndPort[0] . $url; } //print "Was redirected to ".$url."<br />"; if ($previousUrl != $url) { $wasRedirectedThisTime = true; } } } } if ($wasRedirectedThisTime == false) { $finalRedirectionUrl = true; } if ($numberOfRedirectsRemaining <= 0) { webServiceError("&error-maximum-number-of-redirects-followed;", 500, array('redirectPaths' => implode("</li><li>", $redirectPath))); } $numberOfRedirectsRemaining--; } return $url; }
} elseif (stringStartsWith($argument, '--pipeline=')) { //die(substringAfter($argument, '--pipeline=')); $pipelineName = rawurldecode(substringAfter($argument, '--pipeline=')); if (!file_exists(DOCVERT_DIR . 'pipeline' . DIRECTORY_SEPARATOR . $pipelineName)) { $extraParameters .= '&error-commandline-no-pipeline;'; $errorPlaceholders['pipeline'] = $pipelineName; } else { $pipeline = 'regularpipeline:' . $pipelineName; } } elseif (stringStartsWith($argument, '--autopipeline=')) { $autoPipeline = rawurldecode(substringAfter($argument, '--autopipeline=')); } elseif (stringStartsWith($argument, '--converter=')) { $converter = substringAfter($argument, '--converter='); } else { $extraParameters .= 'Unknown argument: ' . $argument; if (!stringStartsWith($argument, '--')) { $extraParameters .= '&error-commandline-forget-double-dash;'; } $extraParameters .= "\n\n"; } } if (!$files || !$converter || !$pipeline || !$outputZip || $extraParameters) { $commandLineHelp = "\n" . $extraParameters; $commandLineHelp .= '&error-command-line-help;'; $errorPlaceholders['commandLineFiles'] = ''; $errorPlaceholders['commandLineConverter'] = ''; $errorPlaceholders['commandLinePipeline'] = ''; $errorPlaceholders['commandLineOutputZip'] = ''; if (!$files) { $errorPlaceholders['commandLineFiles'] = ' --input-files '; }