function HandleApprove($pagename, $auth='edit') { global $ApproveUrlPattern,$WhiteUrlPatterns,$ApprovedUrlPagesFmt,$action; Lock(2); $page = ReadPage($pagename); $text = preg_replace('/[()]/','',$page['text']); preg_match_all("/$ApproveUrlPattern/",$text,$match); ReadApprovedUrls($pagename); $addpat = array(); foreach($match[0] as $a) { if ($action=='approvesites') $a=preg_replace("!^([^:]+://[^/]+).*$!",'$1',$a); $addpat[] = $a; } if (count($addpat)>0) { $aname = FmtPageName($ApprovedUrlPagesFmt[0],$pagename); $apage = RetrieveAuthPage($aname, $auth); if (!$apage) Abort("?cannot edit $aname"); $new = $apage; if (substr($new['text'],-1,1)!="\n") $new['text'].="\n"; foreach($addpat as $a) { foreach((array)$WhiteUrlPatterns as $pat) if (preg_match("!^$pat(/|$)!i",$a)) continue 2; $urlp = preg_quote($a,'!'); $WhiteUrlPatterns[] = $urlp; $new['text'].=" $a\n"; } $_POST['post'] = 'y'; PostPage($aname,$apage,$new); } Redirect($pagename); }
function HandleApprove($pagename) { global $ApproveUrlPattern, $WhiteUrlPatterns, $ApprovedUrlPagesFmt, $action; Lock(2); $page = ReadPage($pagename); $text = preg_replace('/[()]/', '', $page['text']); preg_match_all("/{$ApproveUrlPattern}/", $text, $match); ReadApprovedUrls($pagename); $addpat = array(); foreach ($match[0] as $a) { foreach ((array) $WhiteUrlPatterns as $pat) { if (preg_match("!^{$pat}(/|\$)!", $a)) { continue 2; } } if ($action == 'approvesites') { $a = preg_replace("!^([^:]+://[^/]+).*\$!", '$1', $a); } $addpat[] = $a; } if (count($addpat) > 0) { $aname = FmtPageName($ApprovedUrlPagesFmt[0], $pagename); $apage = ReadPage($aname, ''); $new = $apage; if (substr($new['text'], -1, 1) != "\n") { $new['text'] .= "\n"; } foreach ($addpat as $pat) { $new['text'] .= " {$pat}\n"; } $_REQUEST['post'] = 'y'; PostPage($aname, $apage, $new); } Redirect($pagename); }
/** * Get a cached test result */ function PSS_GetCacheEntry($url) { $id = null; $cache_lock = Lock("PSS Cache"); if (isset($cache_lock)) { if (is_file('./tmp/pss.cache')) { $cache = json_decode(file_get_contents('./tmp/pss.cache'), true); // delete stale cache entries $now = time(); $dirty = false; foreach ($cache as $cache_key => &$cache_entry) { if ($cache_entry['expires'] < $now) { $dirty = true; unset($cache[$cache_key]); } } if ($dirty) { file_put_contents('./tmp/pss.cache', json_encode($cache)); } $key = md5($url); if (array_key_exists($key, $cache) && array_key_exists('id', $cache[$key])) { $id = $cache[$key]['id']; } } Unlock($cache_lock); } return $id; }
function TSViewCreate($server, $tsview_name, &$metrics) { $needs_update = false; if (!is_dir('./dat')) { mkdir('./dat', 0777, true); } $def = './dat/tsview-' . sha1($tsview_name) . '.json'; $lock = Lock("TSView {$tsview_name}"); if (isset($lock)) { if (is_file($def)) { $current = json_decode(file_get_contents($def), true); } if (!isset($current) || !is_array($current)) { $current = array(); } foreach ($metrics as $metric => $x) { if (!array_key_exists($metric, $current)) { $needs_update = true; $current[$metric] = 1; } } if ($needs_update) { $data = array('names' => array()); foreach ($current as $metric => $x) { $data['names'][] = $metric; } $body = json_encode($data); if (http_put_raw("{$server}{$tsview_name}", $body)) { file_put_contents($def, json_encode($current)); } } Unlock($lock); } }
/** * For each IP/Installer pair, keep track of the last 4 checks and if they * were within the last hour fail the request. * * @param mixed $installer */ function CheckIp($installer) { $ok = true; $ip = $_SERVER["REMOTE_ADDR"]; if (isset($ip) && strlen($ip)) { $lock = Lock("Installers", true, 5); if ($lock) { $now = time(); $file = "./tmp/installers.dat"; if (gz_is_file($file)) { $history = json_decode(gz_file_get_contents($file), true); } if (!isset($history) || !is_array($history)) { $history = array(); } if (isset($history[$ip])) { if (isset($history[$ip][$installer])) { $history[$ip][$installer][] = $now; if (count($history[$ip][$installer]) > 10) { array_shift($history[$ip][$installer]); } if (isset($history[$ip]["last-{$installer}"]) && $now - $history[$ip]["last-{$installer}"] < 3600) { $count = 0; foreach ($history[$ip][$installer] as $time) { if ($now - $time < 3600) { $count++; } } if ($count > 4) { $ok = false; } } } else { $history[$ip][$installer] = array($now); } } else { $history[$ip] = array($installer => array($now)); } $history[$ip]['last'] = $now; if ($ok) { $history[$ip]["last-{$installer}"] = $now; } // prune any agents that haven't connected in 7 days foreach ($history as $agent => $info) { if ($now - $info['last'] > 604800) { unset($history[$agent]); } } gz_file_put_contents($file, json_encode($history)); Unlock($lock); } } return $ok; }
function HandlePostRename($pagename) { global $RedirectToRenameFmt, $GroupPattern, $WikiWordPattern; $newpagename = MakePageName($pagename, stripmagic($_POST['group'] . '.' . $_POST['renametext'])); if (PageExists($newpagename)) { Abort("'{$newpagename}' already exists"); } else { Lock(2); $page = RetrieveAuthPage($pagename, "edit"); if ($page) { $ntext = $page['text']; } else { Abort("cannot get '{$pagename}'"); } $ogroup = FmtPageName('$Group', $pagename); $ngroup = FmtPageName('$Group', $newpagename); # Abort('stop for testing'); if ($_POST['addgroup']) { if ($_POST['addgroup'] == 'new') { $h = $ogroup; $ogroup = $ngroup; $ngroup = $h; } if ($ogroup == $ngroup) { $ngroup = $ngroup . '1'; } $ntext = preg_replace("/(\\[[=@].*?[=@]\\])|(\\[\\[[^#].*?\\]\\])|([`:\\/])?\\b(({$GroupPattern}([\\/.]))?{$WikiWordPattern})/e", 'QualifyUnqualifiedLinks($ngroup,$ogroup,"$0")', $ntext); # Abort(str_replace("\n",'<br/>',str_replace("\n\n",'</p><p>',$ntext))); # $page = RetrieveAuthPage($newpagename,"edit"); } $page['text'] = $ntext; WritePage($newpagename, $page); $page['text'] = str_replace('$RenameText', $newpagename, $RedirectToRenameFmt); WritePage($pagename, $page); } Redirect($pagename); }
function HandlePostAttr($pagename, $auth = 'attr') { global $PageAttributes, $EnablePostAttrClearSession; Lock(2); $page = RetrieveAuthPage($pagename, $auth, true); if (!$page) { Abort("?unable to read $pagename"); } foreach($PageAttributes as $attr=>$p) { $v = stripmagic(@$_POST[$attr]); if ($v == '') continue; if ($v=='clear') unset($page[$attr]); else if (strncmp($attr, 'passwd', 6) != 0) $page[$attr] = $v; else { $a = array(); preg_match_all('/"[^"]*"|\'[^\']*\'|\\S+/', $v, $match); foreach($match[0] as $pw) $a[] = preg_match('/^(@|\\w+:)/', $pw) ? $pw : crypt(preg_replace('/^([\'"])(.*)\\1$/', '$2', $pw)); if ($a) $page[$attr] = implode(' ',$a); } } WritePage($pagename,$page); Lock(0); if (IsEnabled($EnablePostAttrClearSession, 1)) { @session_start(); unset($_SESSION['authid']); unset($_SESSION['authlist']); $_SESSION['authpw'] = array(); } Redirect($pagename); exit; }
/** * Cache the test ID in the case of multiple submits */ function PSS_TestSubmitted(&$test) { if (array_key_exists('id', $test) && array_key_exists('url', $test)) { $now = time(); $cache_time = 10080; // 7 days (in minutes) if (array_key_exists('cache', $_REQUEST) && $_REQUEST['cache'] > 0) { $cache_time = (int) $_REQUEST['cache']; } $expires = $now + $cache_time * 60; $entry = array('id' => $test['id'], 'expires' => $expires); $key = md5($test['url']); // update the cache $cache_lock = Lock("PSS Cache"); if (isset($cache_lock)) { if (is_file('./tmp/pss.cache')) { $cache = json_decode(file_get_contents('./tmp/pss.cache'), true); } else { $cache = array(); } // delete stale cache entries foreach ($cache as $cache_key => &$cache_entry) { if ($cache_entry['expires'] < $now) { unset($cache[$cache_key]); } } $cache[$key] = $entry; file_put_contents('./tmp/pss.cache', json_encode($cache)); Unlock($cache_lock); } } }
function HandleRss($pagename) { global $RssMaxItems, $RssSourceSize, $RssDescSize, $RssChannelFmt, $RssChannelDesc, $RssTimeFmt, $RssChannelBuildDate, $RssItemsRDFList, $RssItemsRDFListFmt, $RssItems, $RssItemFmt, $HandleRssFmt, $FmtV; $t = ReadTrail($pagename, $pagename); $page = RetrieveAuthPage($pagename, 'read', false); if (!$page) { Abort("?cannot read {$pagename}"); } $cbgmt = $page['time']; $r = array(); for ($i = 0; $i < count($t) && count($r) < $RssMaxItems; $i++) { if (!PageExists($t[$i]['pagename'])) { continue; } $page = RetrieveAuthPage($t[$i]['pagename'], 'read', false); Lock(0); if (!$page) { continue; } $text = MarkupToHTML($t[$i]['pagename'], substr($page['text'], 0, $RssSourceSize)); $text = entityencode(preg_replace("/<.*?>/s", "", $text)); preg_match("/^(.{0,{$RssDescSize}}\\s)/s", $text, $match); $r[] = array('name' => $t[$i]['pagename'], 'time' => $page['time'], 'desc' => $match[1] . " ...", 'author' => $page['author']); if ($page['time'] > $cbgmt) { $cbgmt = $page['time']; } } SDV($RssChannelBuildDate, entityencode(gmdate('D, d M Y H:i:s \\G\\M\\T', $cbgmt))); SDV($RssChannelDesc, entityencode(FmtPageName('$Group.$Title', $pagename))); foreach ($r as $page) { $FmtV['$RssItemPubDate'] = gmstrftime($RssTimeFmt, $page['time']); $FmtV['$RssItemDesc'] = $page['desc']; $FmtV['$RssItemAuthor'] = $page['author']; $RssItemsRDFList[] = entityencode(FmtPageName($RssItemsRDFListFmt, $page['name'])); $RssItems[] = entityencode(FmtPageName($RssItemFmt, $page['name'])); } header("Content-type: text/xml"); PrintFmt($pagename, $HandleRssFmt); exit; }
array_shift($tests['times']); } // update the number of high-priority "page loads" that we think are in the queue if (array_key_exists('tests', $tests) && $testInfo['priority'] == 0) { $tests['tests'] = max(0, $tests['tests'] - $testCount); } file_put_contents("./tmp/{$location}.tests", json_encode($tests)); } UnlockLocation($lock); } // see if it is an industry benchmark test if (array_key_exists('industry', $ini) && array_key_exists('industry_page', $ini) && strlen($ini['industry']) && strlen($ini['industry_page'])) { if (!is_dir('./video/dat')) { mkdir('./video/dat', 0777, true); } $indLock = Lock("Industry Video"); if (isset($indLock)) { // update the page in the industry list $ind; $data = file_get_contents('./video/dat/industry.dat'); if ($data) { $ind = json_decode($data, true); } $update = array(); $update['id'] = $id; $update['last_updated'] = $now; $ind[$ini['industry']][$ini['industry_page']] = $update; $data = json_encode($ind); file_put_contents('./video/dat/industry.dat', $data); Unlock($indLock); }
function ImportBenchmarkRun($benchmark, $epoch, &$test_data) { global $error; $lock = Lock("Benchmark {$benchmark} Cron", true, 86400); if (isset($lock)) { if (!is_dir("./results/benchmarks/{$benchmark}")) { mkdir("./results/benchmarks/{$benchmark}", 0777, true); } if (is_file("./results/benchmarks/{$benchmark}/state.json")) { $state = json_decode(file_get_contents("./results/benchmarks/{$benchmark}/state.json"), true); } else { $state = array('running' => false, 'needs_aggregation' => false, 'runs' => array()); } $state['running'] = true; $state['last_run'] = $epoch; $state['tests'] = array(); foreach ($test_data as $test) { $test['submitted'] = $epoch; $state['tests'][] = $test; } file_put_contents("./results/benchmarks/{$benchmark}/state.json", json_encode($state)); Unlock($lock); // kick off the collection and aggregation of the results $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' || isset($_SERVER['HTTP_SSL']) && $_SERVER['HTTP_SSL'] == 'On' ? 'https' : 'http'; $host = $_SERVER['HTTP_HOST']; $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); $cron = "{$protocol}://{$host}{$uri}/benchmarks/cron.php?benchmark=" . urlencode($benchmark); file_get_contents($cron); } }
/** * Send a quick http request locally if we need to process cron events (to each of the cron entry points) * * This only runs events on 15-minute intervals and tries to keep it close to the clock increments (00, 15, 30, 45) * */ function CheckCron() { // open and lock the cron job file - abandon quickly if we can't get a lock $should_run = false; $minutes15 = false; $minutes60 = false; $cron_lock = Lock("Cron Check", false, 1200); if (isset($cron_lock)) { $last_run = 0; if (is_file('./tmp/wpt_cron.dat')) { $last_run = file_get_contents('./tmp/wpt_cron.dat'); } $now = time(); $elapsed = $now - $last_run; if (!$last_run) { $should_run = true; $minutes15 = true; $minutes60 = true; } elseif ($elapsed > 120) { if ($elapsed > 1200) { // if it has been over 20 minutes, run regardless of the wall-clock time $should_run = true; } else { $minute = gmdate('i', $now) % 5; if ($minute < 2) { $should_run = true; $minute = gmdate('i', $now) % 15; if ($minute < 2) { $minutes15 = true; } $minute = gmdate('i', $now) % 60; if ($minute < 2) { $minutes60 = true; } } } } if ($should_run) { file_put_contents('./tmp/wpt_cron.dat', $now); } Unlock($cron_lock); } // send the cron requests if ($should_run) { if (is_file('./settings/benchmarks/benchmarks.txt') && is_file('./benchmarks/cron.php')) { SendAsyncRequest('/benchmarks/cron.php'); } SendAsyncRequest('/cron/5min.php'); if (is_file('./jpeginfo/cleanup.php')) { SendAsyncRequest('/jpeginfo/cleanup.php'); } if ($minutes15) { SendAsyncRequest('/cron/15min.php'); } if ($minutes60) { SendAsyncRequest('/cron/hourly.php'); } } }
/** * @param TestPaths $localPaths Paths for step or run to save the cached data * @param array $requests The requests to save * @param array $pageData The page data to save * @param int $ver Cache version */ function SaveCachedDevToolsRequests($localPaths, &$requests, &$pageData, $ver) { $cacheFile = $localPaths->devtoolsRequestsCacheFile($ver); $lock = Lock($cacheFile); if (isset($lock)) { if (gz_is_file($cacheFile)) { $cache = json_decode(gz_file_get_contents($cacheFile), true); } if (!isset($cache) || !is_array($cache)) { $cache = array(); } $cache['requests'] = $requests; $cache['pageData'] = $pageData; gz_file_put_contents($cacheFile, json_encode($cache)); Unlock($lock); } }
function NotifyUpdate($pagename, $dir = '') { global $NotifyList, $NotifyListPageFmt, $NotifyFile, $IsPagePosted, $FmtV, $NotifyTimeFmt, $NotifyItemFmt, $SearchPatterns, $NotifySquelch, $NotifyDelay, $Now, $NotifySubjectFmt, $NotifyBodyFmt, $NotifyHeaders, $NotifyParameters; $abort = ignore_user_abort(true); if ($dir) { flush(); chdir($dir); } $GLOBALS['EnableRedirect'] = 0; ## Read in the current notify configuration $pn = FmtPageName($NotifyListPageFmt, $pagename); $npage = ReadPage($pn, READPAGE_CURRENT); preg_match_all('/^[\\s*:#->]*(notify[:=].*)/m', $npage['text'], $nlist); $nlist = array_merge((array) @$NotifyList, (array) @$nlist[1]); if (!$nlist) { return; } ## make sure other processes are locked out Lock(2); ## let's load the current .notifylist table $nfile = FmtPageName($NotifyFile, $pagename); $nfp = @fopen($nfile, 'r'); if ($nfp) { ## get our current squelch and delay timestamps clearstatcache(); $sz = filesize($nfile); list($nextevent, $firstpost) = explode(' ', rtrim(fgets($nfp, $sz))); ## restore our notify array $notify = unserialize(fgets($nfp, $sz)); fclose($nfp); } if (!is_array($notify)) { $notify = array(); } ## if this is for a newly posted page, get its information if ($IsPagePosted) { $page = ReadPage($pagename, READPAGE_CURRENT); $FmtV['$PostTime'] = strftime($NotifyTimeFmt, $Now); $item = urlencode(FmtPageName($NotifyItemFmt, $pagename)); if ($firstpost < 1) { $firstpost = $Now; } } foreach ($nlist as $n) { $opt = ParseArgs($n); $mailto = preg_split('/[\\s,]+/', $opt['notify']); if (!$mailto) { continue; } if ($opt['squelch']) { foreach ($mailto as $m) { $squelch[$m] = $opt['squelch']; } } if (!$IsPagePosted) { continue; } if ($opt['link']) { $link = MakePageName($pagename, $opt['link']); if (!preg_match("/(^|,){$link}(,|\$)/i", $page['targets'])) { continue; } } $pats = @(array) $SearchPatterns[$opt['list']]; if ($opt['group']) { $pats[] = FixGlob($opt['group'], '$1$2.*'); } if ($opt['name']) { $pats[] = FixGlob($opt['name'], '$1*.$2'); } if ($pats && !MatchPageNames($pagename, $pats)) { continue; } if ($opt['trail']) { $trail = ReadTrail($pagename, $opt['trail']); for ($i = 0; $i < count($trail); $i++) { if ($trail[$i]['pagename'] == $pagename) { break; } } if ($i >= count($trail)) { continue; } } foreach ($mailto as $m) { $notify[$m][] = $item; } } $nnow = time(); if ($nnow < $firstpost + $NotifyDelay) { $nextevent = $firstpost + $NotifyDelay; } else { $firstpost = 0; $nextevent = $nnow + 86400; $mailto = array_keys($notify); $subject = FmtPageName($NotifySubjectFmt, $pagename); $body = FmtPageName($NotifyBodyFmt, $pagename); foreach ($mailto as $m) { $msquelch = @$notify[$m]['lastmail'] + (@$squelch[$m] ? $squelch[$m] : $NotifySquelch); if ($nnow < $msquelch) { if ($msquelch < $nextevent && count($notify[$m]) > 1) { $nextevent = $msquelch; } continue; } unset($notify[$m]['lastmail']); if (!$notify[$m]) { unset($notify[$m]); continue; } $mbody = str_replace('$NotifyItems', urldecode(implode("\n", $notify[$m])), $body); if ($NotifyParameters) { mail($m, $subject, $mbody, $NotifyHeaders, $NotifyParameters); } else { mail($m, $subject, $mbody, $NotifyHeaders); } $notify[$m] = array('lastmail' => $nnow); } } ## save the updated notify status $nfp = @fopen($nfile, "w"); if ($nfp) { fputs($nfp, "{$nextevent} {$firstpost}\n"); fputs($nfp, serialize($notify) . "\n"); fclose($nfp); } Lock(0); return true; }
<?php chdir('..'); $MIN_DAYS = 7; include 'common.inc'; require_once 'archive.inc'; ignore_user_abort(true); set_time_limit(86400); // only allow it to run for 1 day // bail if we are already running $lock = Lock("Archive 2", false, 86400); if (!isset($lock)) { echo "Archive2 process is already running\n"; exit(0); } $archive_dir = null; if (array_key_exists('archive_dir', $settings)) { $archive_dir = $settings['archive_dir']; } $archive2_dir = null; if (array_key_exists('archive2_dir', $settings)) { $archive2_dir = $settings['archive2_dir']; } if (array_key_exists('archive2_days', $settings)) { $MIN_DAYS = $settings['archive2_days']; } $MIN_DAYS = max($MIN_DAYS, 1); $UTC = new DateTimeZone('UTC'); $now = time(); // Archive each day of archives to long-term storage if (isset($archive_dir) && strlen($archive_dir) && isset($archive2_dir) && strlen($archive2_dir)) {
function HandlePostAttr($pagename) { global $PageAttributes, $EnablePostAttrClearSession; Lock(2); $page = RetrieveAuthPage($pagename, 'attr', true, READPAGE_CURRENT); if (!$page) { Abort("?unable to read {$pagename}"); } foreach ($PageAttributes as $attr => $p) { $v = @$_POST[$attr]; if ($v == '') { continue; } if ($v == 'clear') { unset($page[$attr]); } else { if (strncmp($attr, 'passwd', 6) != 0) { $page[$attr] = $v; } else { $a = array(); foreach (preg_split('/\\s+/', $v, -1, PREG_SPLIT_NO_EMPTY) as $pw) { $a[] = preg_match('/^\\w+:/', $pw) ? $pw : crypt($pw); } if ($a) { $page[$attr] = implode(' ', $a); } } } } WritePage($pagename, $page); Lock(0); if (IsEnabled($EnablePostAttrClearSession, 1)) { @session_start(); unset($_SESSION['authid']); $_SESSION['authpw'] = array(); } Redirect($pagename); exit; }
function isFreeLock($lock) { if (Lock($lock, 0)) { Unlock($lock); return true; } return false; }
function HandleSource($pagename) { global $HTTPHeaders; Lock(1); $page = RetrieveAuthPage($pagename, 'read'); if (!$page) { Abort("?cannot source {$pagename}"); } foreach ($HTTPHeaders as $h) { $h = preg_replace('!^Content-type:\\s+text/html!i', 'Content-type: text/plain', $h); header($h); } echo @$page['text']; }
function HandleUpgrade($pagename, $auth = 'ALWAYS') { global $SiteGroup, $SiteAdminGroup, $StatusPageName, $ScriptUrl, $AuthUserPageFmt, $VersionNum, $Version; StopWatch('HandleUpgrade: begin'); $message = ''; $done = ''; ## check for Site.* --> SiteAdmin.* foreach(array('AuthUser', 'NotifyList', 'Blocklist', 'ApprovedUrls') as $n) { $n0 = "$SiteGroup.$n"; $n1 = "$SiteAdminGroup.$n"; StopWatch("HandleUpgrade: checking $n0 -> $n1"); ## checking AuthUser is special, because Site.AuthUser comes with the ## distribution. if ($n == 'AuthUser') { ## if we already have a user-modified SiteAdmin.AuthUser, we can skip SDV($AuthUserPageFmt, '$SiteAdminGroup.AuthUser'); $n1 = FmtPageName($AuthUserPageFmt, $pagename); $page = ReadPage($n1, READPAGE_CURRENT); if (@$page['time'] > 1000000000) continue; ## if there's not a user-modified Site.AuthUser, we can skip $page = ReadPage($n0, READPAGE_CURRENT); if (@$page['time'] == 1000000000) continue; } else if (!PageExists($n0) || PageExists($n1)) continue; if (@$_REQUEST['migrate'] == 'yes') { ## if the admin wants PmWiki to migrate, do it. $page = RetrieveAuthPage($n0, 'admin', true); StopWatch("HandleUpgrade: copying $n0 -> $n1"); if ($page) { WritePage($n1, $page); $done .= "<li>Copied $n0 to $n1</li>"; continue; } } $message .= "<li>$n0 -> $n1</li>"; } if ($message) { $migrateurl = "$ScriptUrl?action=upgrade&migrate=yes"; $infourl = 'http://www.pmwiki.org/wiki/PmWiki/UpgradeToSiteAdmin'; $message = "<h2>Upgrade notice -- SiteAdmin group</h2> <p>This version of PmWiki expects several administrative pages from the <em>Site</em> group to be found in a new <em>SiteAdmin</em> group. On this site, the following pages appear to need to be relocated:</p> <ul>$message</ul> <p>For more information about this change, including the various options for proceeding, see</p> <blockquote><a target='_blank' href='$infourl'>$infourl</a></blockquote> <form action='$ScriptUrl' method='post'> <p>If you would like PmWiki to attempt to automatically copy these pages into their new <br /> locations for you, try <input type='hidden' name='action' value='upgrade' /> <input type='hidden' name='migrate' value='yes' /> <input type='submit' value='Relocate pages listed above' /> (admin password required) </p> </form> <p>If you want to configure PmWiki so that it continues to look for the above pages in <em>$SiteGroup</em>, add the following line near the top of <em>local/config.php</em>:</p> <blockquote><pre>\$SiteAdminGroup = \$SiteGroup;</pre></blockquote> $Version "; print $message; exit; } StopWatch("UpgradeCheck: writing $StatusPageName"); Lock(2); SDV($StatusPageName, "$SiteAdminGroup.Status"); $page = ReadPage($StatusPageName); $page['updatedto'] = $VersionNum; WritePage($StatusPageName, $page); if ($done) { $done .= "<li>Updated $StatusPageName</li>"; echo "<h2>Upgrade to $Version ... ok</h2><ul>$done</ul>"; $GLOBALS['EnableRedirect'] = 0; } Redirect($pagename); }
/** * Update the feeds * */ function UpdateFeeds() { if (!is_dir('./tmp')) { mkdir('./tmp', 0777); } $feedData = array(); $lock = Lock("Update Feeds", false); if (isset($lock)) { // load the list of feeds require_once './settings/feeds.inc'; require_once './lib/simplepie.inc'; // loop through and update each one foreach ($feeds as $category => &$feedList) { $feedData[$category] = array(); foreach ($feedList as $feedSource => $feedUrl) { $feedUrl = trim($feedUrl); if (strlen($feedUrl)) { $feed = new SimplePie(); if ($feed) { $rawFeed = trim(http_fetch($feedUrl)); $feed->set_raw_data($rawFeed); $feed->enable_cache(false); $feed->init(); // try sanitizing the data if we have a problem parsing the feed if (strlen($feed->error())) { FixFeed($rawFeed); $feed->set_raw_data($rawFeed); $feed->enable_cache(false); $feed->init(); } foreach ($feed->get_items() as $item) { $dateStr = $item->get_date(DATE_RSS); if ($dateStr && strlen($dateStr)) { $date = strtotime($dateStr); if ($date) { // only include articles from the last 30 days $now = time(); $elapsed = 0; if ($now > $date) { $elapsed = $now - $date; } $days = (int) ($elapsed / 86400); if ($days <= 30) { // make sure we don't have another article from the exact same time while (isset($feedData[$category][$date])) { $date++; } $feedData[$category][$date] = array('source' => $feedSource, 'title' => $item->get_title(), 'link' => urldecode($item->get_permalink()), 'date' => $dateStr); } } } $item->__destruct(); } $feed->__destruct(); unset($feed); } } } if (count($feedData[$category])) { krsort($feedData[$category]); } } // save out the feed data file_put_contents('./tmp/feeds.dat', json_encode($feedData)); Unlock($lock); } }
if (isset($_REQUEST['id'])) { $videoId = trim($_REQUEST['id']); $videoPath = './' . GetVideoPath($_REQUEST['id']); if (!is_file("{$videoPath}/video.ini")) { $optionsFile = "{$videoPath}/testinfo.json"; if (gz_is_file($optionsFile)) { $tests = json_decode(gz_file_get_contents($optionsFile), true); if (isset($tests) && !is_array($tests)) { unset($tests); } } } } // Render the video if (isset($tests) && count($tests)) { $lock = Lock("video-{$videoId}", false, 600); if ($lock) { RenderVideo($tests); if (is_file("{$videoPath}/render.mp4")) { rename("{$videoPath}/render.mp4", "{$videoPath}/video.mp4"); } $ini = 'completed=' . gmdate('c') . "\r\n"; file_put_contents("{$videoPath}/video.ini", $ini); Unlock($lock); } } $elapsed = microtime(true) - $start; //echo number_format($elapsed, 3) . " seconds"; function RenderVideo(&$tests) { global $width, $height, $maxAspectRatio, $videoExtendTime, $biggestThumbnail, $fps, $labelHeight, $timeHeight, $rowPadding, $speed, $fractionTime;
if (@$Newline) { $p = str_replace($Newline, "\n", $p); } else { $p = str_replace("²", "\n", $p); } array_push($mailpost, urldecode($p) . "\n"); if ($t < $oldestpost) { $oldestpost = $t; } } fclose($fp); if ($oldestpost > $Now - $MailPostsDelay) { Lock(0); return; } $MailPostsFunction($MailPostsTo, $MailPostsSubject, str_replace('$MailPostsList', join('', $mailpost), $MailPostsMessage), $MailPostsHeaders); $fp = @fopen($MailPostsFile, "w"); if ($fp) { fputs($fp, "{$Now} #lastmailed\n"); fclose($fp); } Lock(0); function MailPostsSendmail($to, $subject, $msg, $headers) { if (preg_match('/From: .*?([-.\\w]+@[-.\\w]+)/', $headers, $match)) { $from = "-f" . $match[1]; } $mailer = popen("/usr/lib/sendmail -t -i {$from}", "w"); fwrite($mailer, "To: {$to}\nSubject: {$subject}\n{$headers}\n\n{$msg}"); pclose($mailer); }
/** * See if we are requiring key validation and if so, enforce the restrictions * * @param mixed $test * @param mixed $error */ function ValidateKey(&$test, &$error, $key = null) { global $admin; // load the secret key (if there is one) $secret = ''; $keys = parse_ini_file('./settings/keys.ini', true); if ($keys && isset($keys['server']) && isset($keys['server']['secret'])) { $secret = trim($keys['server']['secret']); } if (strlen($secret)) { // ok, we require key validation, see if they have an hmac (user with form) // or API key if (!isset($key) && isset($test['vh']) && strlen($test['vh'])) { // validate the hash $hashStr = $secret; $hashStr .= $_SERVER['HTTP_USER_AGENT']; $hashStr .= $test['owner']; $hashStr .= $test['vd']; $hmac = sha1($hashStr); // check the elapsed time since the hmac was issued $now = time(); $origTime = strtotime($test['vd']); $elapsed = abs($now - $origTime); if ($hmac != $test['vh'] || $elapsed > 86400) { $error = 'Your test request could not be validated (this can happen if you leave the browser window open for over a day before submitting a test). Please try submitting it again.'; } } elseif (isset($key) || isset($test['key']) && strlen($test['key'])) { if (isset($test['key']) && strlen($test['key']) && !isset($key)) { $key = $test['key']; } // see if it was an auto-provisioned key if (preg_match('/^(?P<prefix>[0-9A-Za-z]+)\\.(?P<key>[0-9A-Za-z]+)$/', $key, $matches)) { $prefix = $matches['prefix']; $db = new SQLite3(__DIR__ . "/dat/{$prefix}_api_keys.db"); $k = $db->escapeString($matches['key']); $info = $db->querySingle("SELECT key_limit FROM keys WHERE key='{$k}'", true); $db->close(); if (isset($info) && is_array($info) && isset($info['key_limit'])) { $keys[$key] = array('limit' => $info['key_limit']); } } // validate their API key and enforce any rate limits if (array_key_exists($key, $keys)) { if (array_key_exists('default location', $keys[$key]) && strlen($keys[$key]['default location']) && !strlen($test['location'])) { $test['location'] = $keys[$key]['default location']; } if (isset($keys[$key]['priority'])) { $test['priority'] = $keys[$key]['priority']; } if (isset($keys[$key]['limit'])) { $limit = (int) $keys[$key]['limit']; // update the number of tests they have submitted today if (!is_dir('./dat')) { mkdir('./dat', 0777, true); } $lock = Lock("API Keys"); if (isset($lock)) { $keyfile = './dat/keys_' . gmdate('Ymd') . '.dat'; $usage = null; if (is_file($keyfile)) { $usage = json_decode(file_get_contents($keyfile), true); } if (!isset($usage)) { $usage = array(); } if (isset($usage[$key])) { $used = (int) $usage[$key]; } else { $used = 0; } $runcount = max(1, $test['runs']); if (!$test['fvonly']) { $runcount *= 2; } if ($limit > 0) { if ($used + $runcount <= $limit) { $used += $runcount; $usage[$key] = $used; } else { $error = 'The test request will exceed the daily test limit for the given API key'; } } else { $used += $runcount; $usage[$key] = $used; } if (!strlen($error)) { file_put_contents($keyfile, json_encode($usage)); } Unlock($lock); } } // check to see if we need to limit queue lengths from this API key if ($keys[$key]['queue_limit']) { $test['queue_limit'] = $keys[$key]['queue_limit']; } } else { $error = 'Invalid API Key'; } if (!strlen($error) && $key != $keys['server']['key']) { global $usingAPI; $usingAPI = true; } // Make sure API keys don't exceed the max configured priority $maxApiPriority = GetSetting('maxApiPriority'); if ($maxApiPriority) { $test['priority'] = max($test['priority'], $maxApiPriority); } } elseif (!isset($admin) || !$admin) { $error = 'An error occurred processing your request (missing API key).'; if (GetSetting('allow_getkeys')) { $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' || isset($_SERVER['HTTP_SSL']) && $_SERVER['HTTP_SSL'] == 'On' ? 'https' : 'http'; $url = "{$protocol}://{$_SERVER['HTTP_HOST']}/getkey.php"; $error .= " If you do not have an API key assigned you can request one at {$url}"; } } } }
<?php // Jobs that need to run every hour chdir('..'); include 'common.inc'; ignore_user_abort(true); set_time_limit(3600); error_reporting(E_ALL); $lock = Lock("cron-60", false, 3600); if (!isset($lock)) { exit(0); } header("Content-type: text/plain"); header("Cache-Control: no-cache, must-revalidate"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); echo "Running hourly cron...\n"; require_once './ec2/ec2.inc.php'; if (GetSetting('ec2_key')) { EC2_DeleteOrphanedVolumes(); } GitUpdate(); AgentUpdate(); echo "Done\n"; /** * Automatically update from the git master (if configured) * */ function GitUpdate() { if (GetSetting('gitUpdate')) { echo "Updating from GitHub...\n";
<?php // Jobs that need to run every 15 minutes chdir('..'); include 'common.inc'; ignore_user_abort(true); set_time_limit(1200); error_reporting(E_ALL); $lock = Lock("cron-15", false, 1200); if (!isset($lock)) { exit(0); } // Update the safe browsing black list SBL_Update(); // update the feeds require_once 'updateFeeds.php'; UpdateFeeds();
function FmtPageList($fmt, $pagename, $opt) { global $GroupPattern, $SearchPatterns, $FmtV, $FPLFunctions; $opt = array_merge(@$_REQUEST, $opt); if (!$opt['q']) { $opt['q'] = stripmagic(@$_REQUEST['q']); } if (!$opt['q']) { return; } $terms = preg_split('/((?<!\\S)[-+]?[\'"].*?[\'"](?!\\S)|\\S+)/', $opt['q'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); if (preg_match("!^({$GroupPattern}(\\|{$GroupPattern})*)?/!i", @$terms[0], $match)) { $opt['group'] = @$match[1]; $terms[0] = str_replace(@$match[1] . '/', '', $terms[0]); } $excl = array(); $incl = array(); foreach ($terms as $t) { if (trim($t) == '') { continue; } if (preg_match('/^([^\'":=]*)[:=]([\'"]?)(.*?)\\2$/', $t, $match)) { $opt[$match[1]] = $match[3]; continue; } preg_match('/^([-+]?)([\'"]?)(.+?)\\2$/', $t, $match); if ($match[1] == '-') { $excl[] = $match[3]; } else { $incl[] = $match[3]; } } $show = isset($opt['list']) ? $opt['list'] : 'default'; $pats = (array) @$SearchPatterns[$show]; if (@$opt['group']) { array_unshift($pats, "/^({$opt['group']})\\./i"); } if (@$opt['trail']) { $t = ReadTrail($pagename, $opt['trail']); foreach ($t as $pagefile) { $pagelist[] = $pagefile['pagename']; } } else { $pagelist = ListPages($pats); } $matches = array(); $searchterms = count($excl) + count($incl); foreach ($pagelist as $pagefile) { $page = ReadPage($pagefile); Lock(0); if (!$page) { continue; } if ($searchterms) { $text = $pagefile . "\n" . @$page['text'] . "\n" . @$page['targets']; foreach ($excl as $t) { if (stristr($text, $t)) { continue 2; } } foreach ($incl as $t) { if (!stristr($text, $t)) { continue 2; } } } $matches[] = array('pagename' => $pagefile, 'size' => strlen(@$page['text']), 'author' => @$page['author'], 'time' => $page['time']); } sort($matches); $FmtV['$MatchCount'] = count($matches); $FmtV['$MatchSearched'] = count($pagelist); $FmtV['$Needle'] = $opt['q']; $fmtfn = @$FPLFunctions[$opt['fmt']]; if (!function_exists($fmtfn)) { $fmtfn = 'FPLByGroup'; } $FmtV['$MatchList'] = $fmtfn($pagename, $matches, $opt); return FmtPageName($fmt, $pagename); }
function HandleSource($pagename) { Lock(1); $page = RetrieveAuthPage($pagename, 'read'); if (!$page) { Abort("?cannot source {$pagename}"); } header("Content-type: text/plain"); echo $page['text']; }
function PageIndexUpdate($pagelist = NULL, $dir = '') { global $EnableReadOnly, $PageIndexUpdateList, $PageIndexFile, $PageIndexTime, $Now; if (IsEnabled($EnableReadOnly, 0)) return; $abort = ignore_user_abort(true); if ($dir) { flush(); chdir($dir); } if (is_null($pagelist)) { $pagelist = (array)$PageIndexUpdateList; $PageIndexUpdateList = array(); } if (!$pagelist || !$PageIndexFile) return; SDV($PageIndexTime, 10); $c = count($pagelist); $updatecount = 0; StopWatch("PageIndexUpdate begin ($c pages to update)"); $pagelist = (array)$pagelist; $timeout = time() + $PageIndexTime; $cmpfn = create_function('$a,$b', 'return strlen($b)-strlen($a);'); Lock(2); $ofp = fopen("$PageIndexFile,new", 'w'); foreach($pagelist as $pn) { if (@$updated[$pn]) continue; @$updated[$pn]++; if (time() > $timeout) continue; $page = ReadPage($pn, READPAGE_CURRENT); if ($page) { $targets = str_replace(',', ' ', @$page['targets']); $terms = PageIndexTerms(array(@$page['text'], $targets, $pn)); usort($terms, $cmpfn); $x = ''; foreach($terms as $t) { if (strpos($x, $t) === false) $x .= " $t"; } fputs($ofp, "$pn:$Now: $targets :$x\n"); } $updatecount++; } $ifp = @fopen($PageIndexFile, 'r'); if ($ifp) { while (!feof($ifp)) { $line = fgets($ifp, 4096); while (substr($line, -1, 1) != "\n" && !feof($ifp)) $line .= fgets($ifp, 4096); $i = strpos($line, ':'); if ($i === false) continue; $n = substr($line, 0, $i); if (@$updated[$n]) continue; fputs($ofp, $line); } fclose($ifp); } fclose($ofp); if (file_exists($PageIndexFile)) unlink($PageIndexFile); rename("$PageIndexFile,new", $PageIndexFile); fixperms($PageIndexFile); StopWatch("PageIndexUpdate end ($updatecount updated)"); ignore_user_abort($abort); }
function LinkIndexUpdate($pagelist) { global $LinkIndexFile, $PCache, $LinkIndexTime; SDV($LinkIndexTime, 10); if (!$pagelist || !$LinkIndexFile) { return; } StopWatch('LinkIndexUpdate begin'); $pagelist = (array) $pagelist; Lock(2); $ofp = fopen("{$LinkIndexFile},new", 'w'); $timeout = time() + $LinkIndexTime; foreach ($pagelist as $n) { if (time() > $timeout) { break; } if (isset($PCache[$n]['targets'])) { $targets = $PCache[$n]['targets']; } else { $page = ReadPage($n, READPAGE_CURRENT); if (!$page) { continue; } $targets = @$page['targets']; } fputs($ofp, "{$n}={$targets}\n"); } $ifp = @fopen($LinkIndexFile, 'r'); if ($ifp) { while (!feof($ifp)) { $line = fgets($ifp, 4096); while (substr($line, -1, 1) != "\n" && !feof($ifp)) { $line .= fgets($ifp, 4096); } $i = strpos($line, '='); if ($i === false) { continue; } $n = substr($line, 0, $i); if (in_array($n, $pagelist)) { continue; } fputs($ofp, $line); } fclose($ifp); } fclose($ofp); if (file_exists($LinkIndexFile)) { unlink($LinkIndexFile); } rename("{$LinkIndexFile},new", $LinkIndexFile); fixperms($LinkIndexFile); StopWatch('LinkIndexUpdate end'); }
/** * Do all of the processing for a given benchmark * * @param mixed $benchmark */ function ProcessBenchmark($benchmark) { global $logFile; $lock = Lock("Benchmark {$benchmark} Cron", false, 86400); if (isset($lock)) { logMsg("Processing benchmark '{$benchmark}'", "./log/{$logFile}", true); $options = array(); if (include "./settings/benchmarks/{$benchmark}.php") { if (!is_dir("./results/benchmarks/{$benchmark}")) { mkdir("./results/benchmarks/{$benchmark}", 0777, true); } if (is_file("./results/benchmarks/{$benchmark}/state.json")) { $state = json_decode(file_get_contents("./results/benchmarks/{$benchmark}/state.json"), true); } else { $state = array('running' => false, 'needs_aggregation' => true, 'runs' => array()); // build up a list of runs if we have data if (is_dir("./results/benchmarks/{$benchmark}/data")) { $files = scandir("./results/benchmarks/{$benchmark}/data"); $last_run = 0; foreach ($files as $file) { if (preg_match('/([0-9]+_[0-9]+)\\..*/', $file, $matches)) { $UTC = new DateTimeZone('UTC'); $date = DateTime::createFromFormat('Ymd_Hi', $matches[1], $UTC); $time = $date->getTimestamp(); $state['runs'][] = $time; if ($time > $last_run) { $last_run = $time; } } } if ($last_run) { $state['last_run'] = $last_run; } } file_put_contents("./results/benchmarks/{$benchmark}/state.json", json_encode($state)); } if (!is_array($state)) { $state = array('running' => false); } if (!array_key_exists('running', $state)) { $state['running'] = false; } if (array_key_exists('running', $state)) { CheckBenchmarkStatus($benchmark, $state); // update the state between steps file_put_contents("./results/benchmarks/{$benchmark}/state.json", json_encode($state)); } else { $state['running'] = false; } if (!$state['running'] && (array_key_exists('runs', $state) && count($state['runs'])) && (!array_key_exists('needs_aggregation', $state) || $state['needs_aggregation'])) { AggregateResults($benchmark, $state, $options); file_put_contents("./results/benchmarks/{$benchmark}/state.json", json_encode($state)); } // see if we need to kick off a new benchmark run if (!$state['running'] && (!array_key_exists('tests', $state) || !is_array($state['tests']) || !count($state['tests']))) { if (!array_key_exists('last_run', $state)) { $state['last_run'] = 0; } $now = time(); if (call_user_func("{$benchmark}ShouldExecute", $state['last_run'], $now)) { if (is_file("./log/{$logFile}")) { unlink("./log/{$logFile}"); } logMsg("Running benchmark '{$benchmark}'", "./log/{$logFile}", true); if (SubmitBenchmark($configurations, $state, $benchmark)) { $state['last_run'] = $now; $state['running'] = true; } } else { logMsg("Benchmark '{$benchmark}' does not need to be run", "./log/{$logFile}", true); } } file_put_contents("./results/benchmarks/{$benchmark}/state.json", json_encode($state)); } logMsg("Done Processing benchmark '{$benchmark}'", "./log/{$logFile}", true); Unlock($lock); } }