示例#1
0
 /**
  * Make the script sleep during a given delay
  *
  * @param TryAgain\Handler $handler
  */
 public function process(Handler $handler)
 {
     $delay = $this->getFibonacciNumber();
     if ($delay > 0) {
         time_sleep_until(microtime(true) + $delay);
     }
 }
示例#2
0
 /**
  * 运行守护进程,归档日志文件。
  * 
  * @see ZtChart_Model_Monitor_Abstract::run()
  */
 public function run()
 {
     sleep($this->_console->getDelayTimestamp());
     while (true) {
         $this->_timestamp = $timestamp = time();
         $deadlineTimestamp = $timestamp - self::CYCLE_TIMESTAMP;
         $this->import($this->_console->getLogPaths(), $deadlineTimestamp);
         $storage = new ZtChart_Model_Storage('monitor', 'archive');
         $storage->write($deadlineTimestamp);
         time_sleep_until($timestamp + self::CYCLE_TIMESTAMP);
     }
 }
示例#3
0
 private function tickProcessor()
 {
     while (!$this->server->isShutdown()) {
         $start = microtime(true);
         $this->tick();
         $time = microtime(true);
         if ($time - $start < 0.01) {
             @time_sleep_until($time + 0.01 - ($time - $start));
         }
     }
     $this->tick();
     $this->socket->close();
 }
示例#4
0
 private function tickProcessor()
 {
     $this->lastMeasure = \microtime(\true);
     while (!$this->shutdown) {
         $start = \microtime(\true);
         $max = 5000;
         while (--$max and $this->receivePacket()) {
         }
         while ($this->receiveStream()) {
         }
         $time = \microtime(\true) - $start;
         if ($time < 0.05) {
             \time_sleep_until(\microtime(\true) + 0.05 - $time);
         }
         $this->tick();
     }
 }
示例#5
0
 public function onCommand(ConsoleCommandEvent $event)
 {
     $output = $event->getOutput();
     $input = new ArgvInput();
     try {
         $input->bind($this->getDefinition());
     } catch (\RuntimeException $e) {
     }
     $delay = filter_var($input->getOption('delay'), FILTER_VALIDATE_INT);
     if ($delay > 0) {
         $wakeupAt = time() + mt_rand(1, $delay);
         $output->writeln('<comment>Waiting until ' . date('Y-m-d H:i:s', $wakeupAt) . ' eRepublik time.</comment>');
         time_sleep_until($wakeupAt);
     }
     $this->configPath = $input->getOption('config');
     $this->loadConfig();
 }
示例#6
0
 /**
  * @param int $shootTime
  * @param int $stopTime
  *
  * @return \Generator
  */
 public function shootThenGo($shootTime, $stopTime)
 {
     $entry = time();
     $timestamp = $shootTime;
     while (true) {
         if ($timestamp > $stopTime) {
             break;
         }
         if ($timestamp > time()) {
             time_sleep_until($timestamp);
         }
         (yield $timestamp);
         if ($timestamp < $entry) {
             $timestamp = $entry;
             continue;
         }
         ++$timestamp;
     }
 }
示例#7
0
文件: Scheduler.php 项目: chh/kue
 /**
  * Schedules jobs, sleeps until a job has to be scheduled. Returns 
  * when jobs were scheduled.
  *
  * @return int Number of scheduled jobs.
  */
 function run()
 {
     $now = new DateTime('now');
     $sleep = min(array_map(function ($entry) use($now) {
         list($expression, $job) = $entry;
         return $expression->getNextRunDate($now)->getTimestamp();
     }, $this->entries));
     time_sleep_until($sleep);
     $scheduled = 0;
     foreach ($this->entries as $entry) {
         list($expression, $job) = $entry;
         if ($expression->isDue($now)) {
             $this->queue->push($job);
             $scheduled += 1;
         }
     }
     $this->queue->flush();
     return $scheduled;
 }
示例#8
0
function remove_obsolete_hublocs()
{
    logger('remove_obsolete_hublocs', LOGGER_DEBUG);
    // Get rid of any hublocs which are ours but aren't valid anymore -
    // e.g. they point to a different and perhaps transient URL that we aren't using.
    // I need to stress that this shouldn't happen. fix_system_urls() fixes hublocs
    // when it discovers the URL has changed. So it's unclear how we could end up
    // with URLs pointing to the old site name. But it happens. This may be an artifact
    // of an old bug or maybe a regression in some newer code. In any event, they
    // mess up communications and we have to take action if we find any.
    // First make sure we have any hublocs (at all) with this URL and sitekey.
    // We don't want to perform this operation while somebody is in the process
    // of renaming their hub or installing certs.
    $r = q("select hubloc_id from hubloc where hubloc_url = '%s' and hubloc_sitekey = '%s'", dbesc(z_root()), dbesc(get_config('system', 'pubkey')));
    if (!$r || !count($r)) {
        return;
    }
    $channels = array();
    // Good. We have at least one *valid* hubloc.
    // Do we have any invalid ones?
    $r = q("select hubloc_id from hubloc where hubloc_sitekey = '%s' and hubloc_url != '%s'", dbesc(get_config('system', 'pubkey')), dbesc(z_root()));
    $p = q("select hubloc_id from hubloc where hubloc_sitekey != '%s' and hubloc_url = '%s'", dbesc(get_config('system', 'pubkey')), dbesc(z_root()));
    if (is_array($r) && is_array($p)) {
        $r = array_merge($r, $p);
    }
    if (!$r) {
        return;
    }
    // We've got invalid hublocs. Get rid of them.
    logger('remove_obsolete_hublocs: removing ' . count($r) . ' hublocs.');
    $interval = get_config('system', 'delivery_interval') !== false ? intval(get_config('system', 'delivery_interval')) : 2;
    foreach ($r as $rr) {
        q("update hubloc set hubloc_deleted = 1 where hubloc_id = %d", intval($rr['hubloc_id']));
        $x = q("select channel_id from channel where channel_hash = '%s' limit 1", dbesc($rr['hubloc_hash']));
        if ($x) {
            Zotlabs\Daemon\Master::Summon(array('Notifier', 'location', $x[0]['channel_id']));
            if ($interval) {
                @time_sleep_until(microtime(true) + (double) $interval);
            }
        }
    }
}
示例#9
0
 /**
  * Keep the app running as a daemon.
  *
  * The app sends a report every X seconds as defined in the daemon.interval config setting.
  *
  * @throws Exceptions\EnsureException if config settings are wrong
  */
 protected function runAsDaemon()
 {
     // interval (in seconds) between sending reports.
     $interval = $this->cfg('daemon.interval');
     Ensure::that($interval > $this->cfg('http.timeout'), 'daemon.interval must be larger than http.timeout in order to run as daemon');
     // start of the reporting
     $start = microtime(true);
     // A practically endless loop
     // 64 bit signed integers gives us 292,471,208,678 years of runtime with 1-second intervals
     // 32 bit signed integers gives us 69 years of runtime with 1-second intervals
     for ($i = 0; true; ++$i) {
         // The ideal starting time of this iteration
         $this_iteration_start = $start + $interval * $i;
         // The ideal starting time of the next iteration
         $next_iteration_start = $this_iteration_start + $interval;
         // Send the report. This may take a few seconds.
         try {
             $this->sendReport();
         } catch (Exception $e) {
             $this->warning('Could not send report to remote server: {message}', ['message' => $e->getMessage()]);
         }
         // Sleep until next iteration should start
         time_sleep_until($next_iteration_start);
     }
 }
示例#10
0
 /**
  * Enforce the minimum execution time. Call this at the end of your long
  * processing to make sure that it doesn't take less time than the
  * minimum execution time. This is used to avoid being blocked by
  * overzealous server protection solutions.
  *
  * @return  void
  */
 public function enforce_min_exec_time()
 {
     // Try to get a sane value for PHP's maximum_execution_time INI parameter
     if (@function_exists('ini_get')) {
         $php_max_exec = @ini_get("maximum_execution_time");
     } else {
         $php_max_exec = 10;
     }
     if ($php_max_exec == "" || $php_max_exec == 0) {
         $php_max_exec = 10;
     }
     // Decrease $php_max_exec time by 500 msec we need (approx.) to tear down
     // the application, as well as another 500msec added for rounding
     // error purposes. Also make sure this is never going to be less than 0.
     $php_max_exec = max($php_max_exec * 1000 - 1000, 0);
     // Get the "minimum execution time per step" Akeeba Backup configuration variable
     $minexectime = $this->min_exec_time;
     if (!is_numeric($minexectime)) {
         $minexectime = 0;
     }
     // Make sure we are not over PHP's time limit!
     if ($minexectime > $php_max_exec) {
         $minexectime = $php_max_exec;
     }
     // Get current running time
     $elapsed_time = $this->getRunningTime() * 1000;
     // Only run a sleep delay if we haven't reached the minexectime execution time
     if ($minexectime > $elapsed_time && $elapsed_time > 0) {
         $sleep_msec = $minexectime - $elapsed_time;
         if (function_exists('usleep')) {
             usleep(1000 * $sleep_msec);
         } elseif (function_exists('time_nanosleep')) {
             $sleep_sec = floor($sleep_msec / 1000);
             $sleep_nsec = 1000000 * ($sleep_msec - $sleep_sec * 1000);
             time_nanosleep($sleep_sec, $sleep_nsec);
         } elseif (function_exists('time_sleep_until')) {
             $until_timestamp = time() + $sleep_msec / 1000;
             time_sleep_until($until_timestamp);
         } elseif (function_exists('sleep')) {
             $sleep_sec = ceil($sleep_msec / 1000);
             sleep($sleep_sec);
         }
     } elseif ($elapsed_time > 0) {
         // No sleep required, even if user configured us to be able to do so.
     }
 }
示例#11
0
function ratenotif_run($argv, $argc)
{
    cli_startup();
    $a = get_app();
    require_once "session.php";
    require_once "datetime.php";
    require_once 'include/items.php';
    require_once 'include/Contact.php';
    if ($argc < 3) {
        return;
    }
    logger('ratenotif: invoked: ' . print_r($argv, true), LOGGER_DEBUG);
    $cmd = $argv[1];
    $item_id = $argv[2];
    if ($cmd === 'rating') {
        $r = q("select * from xlink where xlink_id = %d and xlink_static = 1 limit 1", intval($item_id));
        if (!$r) {
            logger('rating not found');
            return;
        }
        $encoded_item = array('type' => 'rating', 'encoding' => 'zot', 'target' => $r[0]['xlink_link'], 'rating' => intval($r[0]['xlink_rating']), 'rating_text' => $r[0]['xlink_rating_text'], 'signature' => $r[0]['xlink_sig'], 'edited' => $r[0]['xlink_updated']);
    }
    $channel = channelx_by_hash($r[0]['xlink_xchan']);
    if (!$channel) {
        logger('no channel');
        return;
    }
    $primary = get_directory_primary();
    if (!$primary) {
        return;
    }
    $interval = get_config('system', 'delivery_interval') !== false ? intval(get_config('system', 'delivery_interval')) : 2;
    $deliveries_per_process = intval(get_config('system', 'delivery_batch_count'));
    if ($deliveries_per_process <= 0) {
        $deliveries_per_process = 1;
    }
    $deliver = array();
    $x = z_fetch_url($primary . '/regdir');
    if ($x['success']) {
        $j = json_decode($x['body'], true);
        if ($j && $j['success'] && is_array($j['directories'])) {
            foreach ($j['directories'] as $h) {
                if ($h == z_root()) {
                    continue;
                }
                $hash = random_string();
                $n = zot_build_packet($channel, 'notify', null, null, $hash);
                q("insert into outq ( outq_hash, outq_account, outq_channel, outq_driver, outq_posturl, outq_async, outq_created, outq_updated, outq_notify, outq_msg ) values ( '%s', %d, %d, '%s', '%s', %d, '%s', '%s', '%s', '%s' )", dbesc($hash), intval($channel['channel_account_id']), intval($channel['channel_id']), dbesc('zot'), dbesc($h . '/post'), intval(1), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc($n), dbesc(json_encode($encoded_item)));
                $deliver[] = $hash;
                if (count($deliver) >= $deliveries_per_process) {
                    proc_run('php', 'include/deliver.php', $deliver);
                    $deliver = array();
                    if ($interval) {
                        @time_sleep_until(microtime(true) + (double) $interval);
                    }
                }
            }
            // catch any stragglers
            if (count($deliver)) {
                proc_run('php', 'include/deliver.php', $deliver);
            }
        }
    }
    logger('ratenotif: complete.');
    return;
}
示例#12
0
文件: bot.php 项目: rbraband/LouCes
 public function run()
 {
     $this->add_category('default', array('humanice' => true), PUBLICY);
     $this->cron = Cron::factory();
     $this->cron->attach($this);
     $this->lou = LoU::factory($this->server, $this->email, $this->password, $this->debug);
     if ($this->debug) {
         $this->log("Entered debugmode!");
     }
     $this->lou->attach($this);
     $this->lou->get_self();
     $this->forum = Forum::factory($this->lou);
     $this->igm = Igm::factory($this->lou);
     $this->load_hooks();
     $this->globalchat = defined('GLOBALCHAT') ? GLOBALCHAT : false;
     $this->lou->get_self_alliance();
     $this->lou->setAllianceAllowOnline(true);
     while ($this->lou->isConnected(true)) {
         $slepp_until = time() + POLLTRIP;
         $event = $this->cron->check();
         $chat = $this->lou->check();
         if (time() < $slepp_until) {
             time_sleep_until($slepp_until);
         }
     }
 }
示例#13
0
文件: zot.php 项目: 23n/hubzilla
/**
 * Send a zot packet to all hubs where this channel is duplicated, refreshing
 * such things as personal settings, channel permissions, address book updates, etc.
 *
 * @param int $uid
 * @param array $packet (optional) default null
 * @param boolean $groups_changed (optional) default false
 */
function build_sync_packet($uid = 0, $packet = null, $groups_changed = false)
{
    $a = get_app();
    logger('build_sync_packet');
    if ($packet) {
        logger('packet: ' . print_r($packet, true), LOGGER_DATA);
    }
    if (!$uid) {
        $uid = local_channel();
    }
    if (!$uid) {
        return;
    }
    $r = q("select * from channel where channel_id = %d limit 1", intval($uid));
    if (!$r) {
        return;
    }
    $channel = $r[0];
    if (intval($channel['channel_removed'])) {
        return;
    }
    $h = q("select * from hubloc where hubloc_hash = '%s' and hubloc_deleted = 0", dbesc($channel['channel_hash']));
    if (!$h) {
        return;
    }
    $synchubs = array();
    foreach ($h as $x) {
        if ($x['hubloc_host'] == $a->get_hostname()) {
            continue;
        }
        $synchubs[] = $x;
    }
    if (!$synchubs) {
        return;
    }
    $r = q("select xchan_guid, xchan_guid_sig from xchan where xchan_hash  = '%s' limit 1", dbesc($channel['channel_hash']));
    if (!$r) {
        return;
    }
    $env_recips = array();
    $env_recips[] = array('guid' => $r[0]['xchan_guid'], 'guid_sig' => $r[0]['xchan_guid_sig']);
    $info = $packet ? $packet : array();
    $info['type'] = 'channel_sync';
    $info['encoding'] = 'red';
    // note: not zot, this packet is very red specific
    if (array_key_exists($uid, $a->config) && array_key_exists('transient', $a->config[$uid])) {
        $settings = $a->config[$uid]['transient'];
        if ($settings) {
            $info['config'] = $settings;
        }
    }
    if ($channel) {
        $info['channel'] = array();
        foreach ($channel as $k => $v) {
            // filter out any joined tables like xchan
            if (strpos($k, 'channel_') !== 0) {
                continue;
            }
            // don't pass these elements, they should not be synchronised
            $disallowed = array('channel_id', 'channel_account_id', 'channel_primary', 'channel_prvkey', 'channel_address', 'channel_deleted', 'channel_removed', 'channel_system');
            if (in_array($k, $disallowed)) {
                continue;
            }
            $info['channel'][$k] = $v;
        }
    }
    if ($groups_changed) {
        $r = q("select hash as collection, visible, deleted, name from groups where uid = %d", intval($uid));
        if ($r) {
            $info['collections'] = $r;
        }
        $r = q("select groups.hash as collection, group_member.xchan as member from groups left join group_member on groups.id = group_member.gid where group_member.uid = %d", intval($uid));
        if ($r) {
            $info['collection_members'] = $r;
        }
    }
    $interval = get_config('system', 'delivery_interval') !== false ? intval(get_config('system', 'delivery_interval')) : 2;
    logger('build_sync_packet: packet: ' . print_r($info, true), LOGGER_DATA);
    foreach ($synchubs as $hub) {
        $hash = random_string();
        $n = zot_build_packet($channel, 'notify', $env_recips, $hub['hubloc_sitekey'], $hash);
        q("insert into outq ( outq_hash, outq_account, outq_channel, outq_driver, outq_posturl, outq_async, outq_created, outq_updated, outq_notify, outq_msg ) values ( '%s', %d, %d, '%s', '%s', %d, '%s', '%s', '%s', '%s' )", dbesc($hash), intval($channel['channel_account']), intval($channel['channel_id']), dbesc('zot'), dbesc($hub['hubloc_callback']), intval(1), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc($n), dbesc(json_encode($info)));
        proc_run('php', 'include/deliver.php', $hash);
        if ($interval) {
            @time_sleep_until(microtime(true) + (double) $interval);
        }
    }
}
示例#14
0
function do_delivery($deliveries)
{
    if (!(is_array($deliveries) && count($deliveries))) {
        return;
    }
    $interval = get_config('system', 'delivery_interval') !== false ? intval(get_config('system', 'delivery_interval')) : 2;
    $deliveries_per_process = intval(get_config('system', 'delivery_batch_count'));
    if ($deliveries_per_process <= 0) {
        $deliveries_per_process = 1;
    }
    $deliver = array();
    foreach ($deliveries as $d) {
        $deliver[] = $d;
        if (count($deliver) >= $deliveries_per_process) {
            proc_run('php', 'include/deliver.php', $deliver);
            $deliver = array();
            if ($interval) {
                @time_sleep_until(microtime(true) + (double) $interval);
            }
        }
    }
    // catch any stragglers
    if ($deliver) {
        proc_run('php', 'include/deliver.php', $deliver);
    }
}
示例#15
0
function notifier_run($argv, $argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "dba.php";
        $db = new dba($db_host, $db_user, $db_pass, $db_data);
        unset($db_host, $db_user, $db_pass, $db_data);
    }
    require_once "session.php";
    require_once "datetime.php";
    require_once 'include/items.php';
    require_once 'include/bbcode.php';
    load_config('config');
    load_config('system');
    load_hooks();
    if ($argc < 3) {
        return;
    }
    $a->set_baseurl(get_config('system', 'url'));
    logger('notifier: invoked: ' . print_r($argv, true));
    $cmd = $argv[1];
    switch ($cmd) {
        case 'mail':
        default:
            $item_id = intval($argv[2]);
            if (!$item_id) {
                return;
            }
            break;
    }
    $expire = false;
    $mail = false;
    $fsuggest = false;
    $top_level = false;
    $recipients = array();
    $url_recipients = array();
    $normal_mode = true;
    if ($cmd === 'mail') {
        $normal_mode = false;
        $mail = true;
        $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1", intval($item_id));
        if (!count($message)) {
            return;
        }
        $uid = $message[0]['uid'];
        $recipients[] = $message[0]['contact-id'];
        $item = $message[0];
    } elseif ($cmd === 'expire') {
        $normal_mode = false;
        $expire = true;
        $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1 \n\t\t\tAND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE", intval($item_id));
        $uid = $item_id;
        $item_id = 0;
        if (!count($items)) {
            return;
        }
    } elseif ($cmd === 'suggest') {
        $normal_mode = false;
        $fsuggest = true;
        $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item_id));
        if (!count($suggest)) {
            return;
        }
        $uid = $suggest[0]['uid'];
        $recipients[] = $suggest[0]['cid'];
        $item = $suggest[0];
    } else {
        // find ancestors
        $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1", intval($item_id));
        if (!count($r) || !intval($r[0]['parent'])) {
            return;
        }
        $target_item = $r[0];
        $parent_id = intval($r[0]['parent']);
        $uid = $r[0]['uid'];
        $updated = $r[0]['edited'];
        if (!$parent_id) {
            return;
        }
        $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer` \n\t\t\tFROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d ORDER BY `id` ASC", intval($parent_id));
        if (!count($items)) {
            return;
        }
        // avoid race condition with deleting entries
        if ($items[0]['deleted']) {
            foreach ($items as $item) {
                $item['deleted'] = 1;
            }
        }
        if (count($items) == 1 && $items[0]['id'] === $target_item['id'] && $items[0]['uri'] === $items[0]['parent-uri']) {
            logger('notifier: top level post');
            $top_level = true;
        }
    }
    $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`, \n\t\t`user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, \n\t\t`user`.`page-flags`, `user`.`prvnets`\n\t\tFROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` \n\t\tWHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1", intval($uid));
    if (!count($r)) {
        return;
    }
    $owner = $r[0];
    $walltowall = $top_level && $owner['id'] != $items[0]['contact-id'] ? true : false;
    $hub = get_config('system', 'huburl');
    // If this is a public conversation, notify the feed hub
    $public_message = true;
    // fill this in with a single salmon slap if applicable
    $slap = '';
    if (!($mail || $fsuggest)) {
        require_once 'include/group.php';
        $parent = $items[0];
        // This is IMPORTANT!!!!
        // We will only send a "notify owner to relay" or followup message if the referenced post
        // originated on our system by virtue of having our hostname somewhere
        // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
        // if $parent['wall'] == 1 we will already have the parent message in our array
        // and we will relay the whole lot.
        // expire sends an entire group of expire messages and cannot be forwarded.
        // However the conversation owner will be a part of the conversation and will
        // be notified during this run.
        // Other DFRN conversation members will be alerted during polled updates.
        // Diaspora members currently are not notified of expirations, and other networks have
        // either limited or no ability to process deletions. We should at least fix Diaspora
        // by stringing togther an array of retractions and sending them onward.
        $localhost = $a->get_hostname();
        if (strpos($localhost, ':')) {
            $localhost = substr($localhost, 0, strpos($localhost, ':'));
        }
        /**
         *
         * Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes 
         * have been known to cause runaway conditions which affected several servers, along with 
         * permissions issues. 
         *
         */
        $relay_to_owner = false;
        if (!$top_level && $parent['wall'] == 0 && !$expire && stristr($target_item['uri'], $localhost)) {
            $relay_to_owner = true;
        }
        if ($cmd === 'uplink' && intval($parent['forum_mode']) && !$top_level) {
            $relay_to_owner = true;
        }
        // until the 'origin' flag has been in use for several months
        // we will just use it as a fallback test
        // later we will be able to use it as the primary test of whether or not to relay.
        if (!$target_item['origin']) {
            $relay_to_owner = false;
        }
        if ($parent['origin']) {
            $relay_to_owner = false;
        }
        if ($relay_to_owner) {
            logger('notifier: followup', LOGGER_DEBUG);
            // local followup to remote post
            $followup = true;
            $public_message = false;
            // not public
            $conversant_str = dbesc($parent['contact-id']);
        } else {
            $followup = false;
            // don't send deletions onward for other people's stuff
            if ($target_item['deleted'] && !intval($target_item['wall'])) {
                logger('notifier: ignoring delete notification for non-wall item');
                return;
            }
            if (strlen($parent['allow_cid']) || strlen($parent['allow_gid']) || strlen($parent['deny_cid']) || strlen($parent['deny_gid'])) {
                $public_message = false;
                // private recipients, not public
            }
            $allow_people = expand_acl($parent['allow_cid']);
            $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
            $deny_people = expand_acl($parent['deny_cid']);
            $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
            // if our parent is a forum, uplink to the origonal author causing
            // a delivery fork
            if (intval($parent['forum_mode']) && !$top_level && $cmd !== 'uplink') {
                proc_run('php', 'include/notifier', 'uplink', $item_id);
            }
            $conversants = array();
            foreach ($items as $item) {
                $recipients[] = $item['contact-id'];
                $conversants[] = $item['contact-id'];
                // pull out additional tagged people to notify (if public message)
                if ($public_message && strlen($item['inform'])) {
                    $people = explode(',', $item['inform']);
                    foreach ($people as $person) {
                        if (substr($person, 0, 4) === 'cid:') {
                            $recipients[] = intval(substr($person, 4));
                            $conversants[] = intval(substr($person, 4));
                        } else {
                            $url_recipients[] = substr($person, 4);
                        }
                    }
                }
            }
            logger('notifier: url_recipients' . print_r($url_recipients, true));
            $conversants = array_unique($conversants);
            $recipients = array_unique(array_merge($recipients, $allow_people, $allow_groups));
            $deny = array_unique(array_merge($deny_people, $deny_groups));
            $recipients = array_diff($recipients, $deny);
            $conversant_str = dbesc(implode(', ', $conversants));
        }
        $r = q("SELECT * FROM `contact` WHERE `id` IN ( {$conversant_str} ) AND `blocked` = 0 AND `pending` = 0");
        if (count($r)) {
            $contacts = $r;
        }
    }
    $feed_template = get_markup_template('atom_feed.tpl');
    $mail_template = get_markup_template('atom_mail.tpl');
    $atom = '';
    $slaps = array();
    $hubxml = feed_hublinks();
    $birthday = feed_birthday($owner['uid'], $owner['timezone']);
    if (strlen($birthday)) {
        $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
    }
    $atom .= replace_macros($feed_template, array('$version' => xmlify(FRIENDICA_VERSION), '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname']), '$feed_title' => xmlify($owner['name']), '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00', ATOM_TIME)), '$hub' => $hubxml, '$salmon' => '', '$name' => xmlify($owner['name']), '$profile_page' => xmlify($owner['url']), '$photo' => xmlify($owner['photo']), '$thumb' => xmlify($owner['thumb']), '$picdate' => xmlify(datetime_convert('UTC', 'UTC', $owner['avatar-date'] . '+00:00', ATOM_TIME)), '$uridate' => xmlify(datetime_convert('UTC', 'UTC', $owner['uri-date'] . '+00:00', ATOM_TIME)), '$namdate' => xmlify(datetime_convert('UTC', 'UTC', $owner['name-date'] . '+00:00', ATOM_TIME)), '$birthday' => $birthday));
    if ($mail) {
        $public_message = false;
        // mail is  not public
        $body = fix_private_photos($item['body'], $owner['uid']);
        $atom .= replace_macros($mail_template, array('$name' => xmlify($owner['name']), '$profile_page' => xmlify($owner['url']), '$thumb' => xmlify($owner['thumb']), '$item_id' => xmlify($item['uri']), '$subject' => xmlify($item['title']), '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00', ATOM_TIME)), '$content' => xmlify($body), '$parent_id' => xmlify($item['parent-uri'])));
    } elseif ($fsuggest) {
        $public_message = false;
        // suggestions are not public
        $sugg_template = get_markup_template('atom_suggest.tpl');
        $atom .= replace_macros($sugg_template, array('$name' => xmlify($item['name']), '$url' => xmlify($item['url']), '$photo' => xmlify($item['photo']), '$request' => xmlify($item['request']), '$note' => xmlify($item['note'])));
        // We don't need this any more
        q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id']));
    } else {
        if ($followup) {
            foreach ($items as $item) {
                // there is only one item
                if (!$item['parent']) {
                    continue;
                }
                if ($item['id'] == $item_id) {
                    logger('notifier: followup: item: ' . print_r($item, true), LOGGER_DATA);
                    $slap = atom_entry($item, 'html', $owner, $owner, false);
                    $atom .= atom_entry($item, 'text', $owner, $owner, false);
                }
            }
        } else {
            foreach ($items as $item) {
                if (!$item['parent']) {
                    continue;
                }
                // private emails may be in included in public conversations. Filter them.
                if ($public_message && $item['private']) {
                    continue;
                }
                $contact = get_item_contact($item, $contacts);
                if (!$contact) {
                    continue;
                }
                if ($normal_mode) {
                    // we only need the current item, but include the parent because without it
                    // older sites without a corresponding dfrn_notify change may do the wrong thing.
                    if ($item_id == $item['id'] || $item['id'] == $item['parent']) {
                        $atom .= atom_entry($item, 'text', $contact, $owner, true);
                    }
                } else {
                    $atom .= atom_entry($item, 'text', $contact, $owner, true);
                }
                if ($top_level && $public_message && $item['author-link'] === $item['owner-link'] && !$expire) {
                    $slaps[] = atom_entry($item, 'html', $contact, $owner, true);
                }
            }
        }
    }
    $atom .= '</feed>' . "\r\n";
    logger('notifier: ' . $atom, LOGGER_DATA);
    logger('notifier: slaps: ' . print_r($slaps, true), LOGGER_DATA);
    // If this is a public message and pubmail is set on the parent, include all your email contacts
    $mail_disabled = function_exists('imap_open') && !get_config('system', 'imap_disabled') ? 0 : 1;
    if (!$mail_disabled) {
        if (!strlen($target_item['allow_cid']) && !strlen($target_item['allow_gid']) && !strlen($target_item['deny_cid']) && !strlen($target_item['deny_gid']) && intval($target_item['pubmail'])) {
            $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'", intval($uid), dbesc(NETWORK_MAIL));
            if (count($r)) {
                foreach ($r as $rr) {
                    $recipients[] = $rr['id'];
                }
            }
        }
    }
    if ($followup) {
        $recip_str = $parent['contact-id'];
    } else {
        $recip_str = implode(', ', $recipients);
    }
    $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ", dbesc($recip_str));
    require_once 'include/salmon.php';
    $interval = get_config('system', 'delivery_interval') === false ? 2 : intval(get_config('system', 'delivery_interval'));
    // delivery loop
    if (count($r)) {
        foreach ($r as $contact) {
            if (!$mail && !$fsuggest && !$followup && !$contact['self']) {
                if ($contact['network'] === NETWORK_DIASPORA && $public_message) {
                    continue;
                }
                q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )", dbesc($cmd), intval($item_id), intval($contact['id']));
            }
        }
        foreach ($r as $contact) {
            if ($contact['self']) {
                continue;
            }
            // potentially more than one recipient. Start a new process and space them out a bit.
            // we will deliver single recipient types of message and email receipients here.
            if (!$mail && !$fsuggest && !$followup) {
                proc_run('php', 'include/delivery.php', $cmd, $item_id, $contact['id']);
                if ($interval) {
                    @time_sleep_until(microtime(true) + (double) $interval);
                }
                continue;
            }
            $deliver_status = 0;
            logger("main delivery by notifier: followup={$followup} mail={$mail} fsuggest={$fsuggest}");
            switch ($contact['network']) {
                case NETWORK_DFRN:
                    // perform local delivery if we are on the same site
                    $basepath = implode('/', array_slice(explode('/', $contact['url']), 0, 3));
                    if (link_compare($basepath, $a->get_baseurl())) {
                        $nickname = basename($contact['url']);
                        if ($contact['issued-id']) {
                            $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
                        } else {
                            $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
                        }
                        $x = q("SELECT\t`contact`.*, `contact`.`uid` AS `importer_uid`, \n\t\t\t\t\t\t\t`contact`.`pubkey` AS `cpubkey`, \n\t\t\t\t\t\t\t`contact`.`prvkey` AS `cprvkey`, \n\t\t\t\t\t\t\t`contact`.`thumb` AS `thumb`, \n\t\t\t\t\t\t\t`contact`.`url` as `url`,\n\t\t\t\t\t\t\t`contact`.`name` as `senderName`,\n\t\t\t\t\t\t\t`user`.* \n\t\t\t\t\t\t\tFROM `contact` \n\t\t\t\t\t\t\tLEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` \n\t\t\t\t\t\t\tWHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0\n\t\t\t\t\t\t\tAND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'\n\t\t\t\t\t\t\t{$sql_extra}\n\t\t\t\t\t\t\tAND `user`.`account_expired` = 0 LIMIT 1", dbesc(NETWORK_DFRN), dbesc($nickname));
                        if (count($x)) {
                            require_once 'library/simplepie/simplepie.inc';
                            logger('mod-delivery: local delivery');
                            local_delivery($x[0], $atom);
                            break;
                        }
                    }
                    logger('notifier: dfrndelivery: ' . $contact['name']);
                    $deliver_status = dfrn_deliver($owner, $contact, $atom);
                    logger('notifier: dfrn_delivery returns ' . $deliver_status);
                    if ($deliver_status == -1) {
                        logger('notifier: delivery failed: queuing message');
                        // queue message for redelivery
                        add_to_queue($contact['id'], NETWORK_DFRN, $atom);
                    }
                    break;
                case NETWORK_OSTATUS:
                    // Do not send to otatus if we are not configured to send to public networks
                    if ($owner['prvnets']) {
                        break;
                    }
                    if (get_config('system', 'ostatus_disabled') || get_config('system', 'dfrn_only')) {
                        break;
                    }
                    if ($followup && $contact['notify']) {
                        logger('notifier: slapdelivery: ' . $contact['name']);
                        $deliver_status = slapper($owner, $contact['notify'], $slap);
                        if ($deliver_status == -1) {
                            // queue message for redelivery
                            add_to_queue($contact['id'], NETWORK_OSTATUS, $slap);
                        }
                    } else {
                        // only send salmon if public - e.g. if it's ok to notify
                        // a public hub, it's ok to send a salmon
                        if (count($slaps) && $public_message && !$expire) {
                            logger('notifier: slapdelivery: ' . $contact['name']);
                            foreach ($slaps as $slappy) {
                                if ($contact['notify']) {
                                    $deliver_status = slapper($owner, $contact['notify'], $slappy);
                                    if ($deliver_status == -1) {
                                        // queue message for redelivery
                                        add_to_queue($contact['id'], NETWORK_OSTATUS, $slappy);
                                    }
                                }
                            }
                        }
                    }
                    break;
                case NETWORK_MAIL:
                    if (get_config('system', 'dfrn_only')) {
                        break;
                    }
                    // WARNING: does not currently convert to RFC2047 header encodings, etc.
                    $addr = $contact['addr'];
                    if (!strlen($addr)) {
                        break;
                    }
                    if ($cmd === 'wall-new' || $cmd === 'comment-new') {
                        $it = null;
                        if ($cmd === 'wall-new') {
                            $it = $items[0];
                        } else {
                            $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($argv[2]), intval($uid));
                            if (count($r)) {
                                $it = $r[0];
                            }
                        }
                        if (!$it) {
                            break;
                        }
                        $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
                        if (!count($local_user)) {
                            break;
                        }
                        $reply_to = '';
                        $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1", intval($uid));
                        if ($r1 && $r1[0]['reply_to']) {
                            $reply_to = $r1[0]['reply_to'];
                        }
                        $subject = $it['title'] ? $it['title'] : t("(no subject)");
                        $headers = 'From: ' . $local_user[0]['username'] . ' <' . $local_user[0]['email'] . '>' . "\n";
                        if ($reply_to) {
                            $headers .= 'Reply-to: ' . $reply_to . "\n";
                        }
                        $headers .= 'Message-id: <' . $it['uri'] . '>' . "\n";
                        if ($it['uri'] !== $it['parent-uri']) {
                            $header .= 'References: <' . $it['parent-uri'] . '>' . "\n";
                            if (!strlen($it['title'])) {
                                $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' LIMIT 1", dbesc($it['parent-uri']));
                                if (count($r)) {
                                    $subtitle = $r[0]['title'];
                                    if ($subtitle) {
                                        if (strncasecmp($subtitle, 'RE:', 3)) {
                                            $subject = $subtitle;
                                        } else {
                                            $subject = 'Re: ' . $subtitle;
                                        }
                                    }
                                }
                            }
                        }
                        $headers .= 'MIME-Version: 1.0' . "\n";
                        $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
                        $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
                        $html = prepare_body($it);
                        $message = '<html><body>' . $html . '</body></html>';
                        logger('notifier: email delivery to ' . $addr);
                        mail($addr, $subject, $message, $headers);
                    }
                    break;
                case NETWORK_DIASPORA:
                    require_once 'include/diaspora.php';
                    if (get_config('system', 'dfrn_only') || !get_config('system', 'diaspora_enabled')) {
                        break;
                    }
                    if ($mail) {
                        diaspora_send_mail($item, $owner, $contact);
                        break;
                    }
                    if (!$normal_mode) {
                        break;
                    }
                    // special handling for followup to public post
                    // all other public posts processed as public batches further below
                    if ($public_message) {
                        if ($followup) {
                            diaspora_send_followup($target_item, $owner, $contact, true);
                        }
                        break;
                    }
                    if (!$contact['pubkey']) {
                        break;
                    }
                    if ($target_item['verb'] === ACTIVITY_DISLIKE) {
                        // unsupported
                        break;
                    } elseif ($target_item['deleted'] && $target_item['verb'] !== ACTIVITY_LIKE) {
                        // diaspora delete,
                        diaspora_send_retraction($target_item, $owner, $contact);
                        break;
                    } elseif ($followup) {
                        // send comments, likes and retractions of likes to owner to relay
                        diaspora_send_followup($target_item, $owner, $contact);
                        break;
                    } elseif ($target_item['parent'] != $target_item['id']) {
                        // we are the relay - send comments, likes and unlikes to our conversants
                        diaspora_send_relay($target_item, $owner, $contact);
                        break;
                    } elseif ($top_level && !$walltowall) {
                        // currently no workable solution for sending walltowall
                        diaspora_send_status($target_item, $owner, $contact);
                        break;
                    }
                    break;
                case NETWORK_FEED:
                case NETWORK_FACEBOOK:
                    if (get_config('system', 'dfrn_only')) {
                        break;
                    }
                default:
                    break;
            }
        }
    }
    // send additional slaps to mentioned remote tags (@foo@example.com)
    if ($slap && count($url_recipients) && ($followup || $top_level) && $public_message && !$expire) {
        if (!get_config('system', 'dfrn_only')) {
            foreach ($url_recipients as $url) {
                if ($url) {
                    logger('notifier: urldelivery: ' . $url);
                    $deliver_status = slapper($owner, $url, $slap);
                    // TODO: redeliver/queue these items on failure, though there is no contact record
                }
            }
        }
    }
    if ($public_message) {
        $r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s' \n\t\t\tAND `uid` = %d AND `rel` != %d group by `batch` ORDER BY rand() ", dbesc(NETWORK_DIASPORA), intval($owner['uid']), intval(CONTACT_IS_SHARING));
        $r2 = q("SELECT `id`, `name`,`network` FROM `contact` \n\t\t\tWHERE `network` = '%s' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0\n\t\t\tAND `rel` != %d order by rand() ", dbesc(NETWORK_DFRN), intval($owner['uid']), intval(CONTACT_IS_SHARING));
        $r = array_merge($r2, $r1);
        if (count($r)) {
            logger('pubdeliver: ' . print_r($r, true), LOGGER_DEBUG);
            // throw everything into the queue in case we get killed
            foreach ($r as $rr) {
                if (!$mail && !$fsuggest && !$followup) {
                    q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )", dbesc($cmd), intval($item_id), intval($rr['id']));
                }
            }
            foreach ($r as $rr) {
                // except for Diaspora batch jobs
                // Don't deliver to folks who have already been delivered to
                if ($rr['network'] !== NETWORK_DIASPORA && in_array($rr['id'], $conversants)) {
                    logger('notifier: already delivered id=' . $rr['id']);
                    continue;
                }
                if (!$mail && !$fsuggest && !$followup) {
                    logger('notifier: delivery agent: ' . $rr['name'] . ' ' . $rr['id']);
                    proc_run('php', 'include/delivery.php', $cmd, $item_id, $rr['id']);
                    if ($interval) {
                        @time_sleep_until(microtime(true) + (double) $interval);
                    }
                }
            }
        }
        if (strlen($hub)) {
            $hubs = explode(',', $hub);
            if (count($hubs)) {
                foreach ($hubs as $h) {
                    $h = trim($h);
                    if (!strlen($h)) {
                        continue;
                    }
                    $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname']);
                    post_url($h, $params);
                    logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
                    if (count($hubs) > 1) {
                        sleep(7);
                    }
                    // try and avoid multiple hubs responding at precisely the same time
                }
            }
        }
    }
    if ($normal_mode) {
        call_hooks('notifier_normal', $target_item);
    }
    call_hooks('notifier_end', $target_item);
    return;
}
 /**
  * API呼び出し制限の10秒当り300回を守る
  *
  * 実のところ、Trello側の制限アルゴリズムが不明なため
  * あまり意味はない。制限に引っかかるとリターンコード
  * 429が返ってくるので、それで処理したほうがベター。
  */
 private function gentlyAPICall()
 {
     if (++$this->apiCall > 300) {
         // もし301回目の呼び出しが11秒を超えている場合はWarningが発生
         // 通常は表示されないが、嫌なら今の時間と比較し回避する
         time_sleep_until($this->benchTime->addSecond(11)->timestamp);
         $this->apiCall = 1;
         $this->benchTime = Carbon::now();
     }
 }
 /**
  * Block until the given file is older than $lockoutS
  * Using this function with a large value of $lockoutS can cause a script
  * timeout.
  *
  * @param string $filename the filename
  * @param integer $lockoutS the minimum number of seconds to wait
  */
 private static function blockUntilFileOlderThan($filename, $lockoutS) {
     if (time() > filemtime($filename) + $lockoutS)
         return;
     @time_sleep_until(filemtime($filename) + $lockoutS);
 }
示例#18
0
function do_delivery($deliveries)
{
    if (!(is_array($deliveries) && count($deliveries))) {
        return;
    }
    $interval = get_config('system', 'delivery_interval') !== false ? intval(get_config('system', 'delivery_interval')) : 2;
    $deliveries_per_process = intval(get_config('system', 'delivery_batch_count'));
    if ($deliveries_per_process <= 0) {
        $deliveries_per_process = 1;
    }
    $deliver = array();
    foreach ($deliveries as $d) {
        if (!$d) {
            continue;
        }
        $deliver[] = $d;
        if (count($deliver) >= $deliveries_per_process) {
            Zotlabs\Daemon\Master::Summon(array('Deliver', $deliver));
            $deliver = array();
            if ($interval) {
                @time_sleep_until(microtime(true) + (double) $interval);
            }
        }
    }
    // catch any stragglers
    if ($deliver) {
        Zotlabs\Daemon\Master::Summon(array('Deliver', $deliver));
    }
}
示例#19
0
文件: queue.php 项目: vinzv/friendica
function queue_run(&$argv, &$argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "include/dba.php";
        $db = new dba($db_host, $db_user, $db_pass, $db_data);
        unset($db_host, $db_user, $db_pass, $db_data);
    }
    require_once "include/session.php";
    require_once "include/datetime.php";
    require_once 'include/items.php';
    require_once 'include/bbcode.php';
    require_once 'include/pidfile.php';
    require_once 'include/socgraph.php';
    load_config('config');
    load_config('system');
    $lockpath = get_lockpath();
    if ($lockpath != '') {
        $pidfile = new pidfile($lockpath, 'queue');
        if ($pidfile->is_already_running()) {
            logger("queue: Already running");
            if ($pidfile->running_time() > 9 * 60) {
                $pidfile->kill();
                logger("queue: killed stale process");
                // Calling a new instance
                proc_run('php', "include/queue.php");
            }
            return;
        }
    }
    $a->set_baseurl(get_config('system', 'url'));
    load_hooks();
    if ($argc > 1) {
        $queue_id = intval($argv[1]);
    } else {
        $queue_id = 0;
    }
    $deadguys = array();
    logger('queue: start');
    // Handling the pubsubhubbub requests
    proc_run('php', 'include/pubsubpublish.php');
    $interval = get_config('system', 'delivery_interval') === false ? 2 : intval(get_config('system', 'delivery_interval'));
    // If we are using the worker we don't need a delivery interval
    if (get_config("system", "worker")) {
        $interval = false;
    }
    $r = q("select * from deliverq where 1");
    if ($r) {
        foreach ($r as $rr) {
            logger('queue: deliverq');
            proc_run('php', 'include/delivery.php', $rr['cmd'], $rr['item'], $rr['contact']);
            if ($interval) {
                @time_sleep_until(microtime(true) + (double) $interval);
            }
        }
    }
    $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`\n\t\tINNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`\n\t\tWHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
    if ($r) {
        foreach ($r as $rr) {
            logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
            logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
        }
        q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
    }
    if ($queue_id) {
        $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1", intval($queue_id));
    } else {
        // For the first 12 hours we'll try to deliver every 15 minutes
        // After that, we'll only attempt delivery once per hour.
        $r = q("SELECT `id` FROM `queue` WHERE (( `created` > UTC_TIMESTAMP() - INTERVAL 12 HOUR && `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ) OR ( `last` < UTC_TIMESTAMP() - INTERVAL 1 HOUR ))");
    }
    if (!$r) {
        return;
    }
    if (!$queue_id) {
        call_hooks('queue_predeliver', $a, $r);
    }
    // delivery loop
    require_once 'include/salmon.php';
    require_once 'include/diaspora.php';
    foreach ($r as $q_item) {
        // queue_predeliver hooks may have changed the queue db details,
        // so check again if this entry still needs processing
        if ($queue_id) {
            $qi = q("select * from queue where `id` = %d limit 1", intval($queue_id));
        } else {
            $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ", intval($q_item['id']));
        }
        if (!count($qi)) {
            continue;
        }
        $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1", intval($qi[0]['cid']));
        if (!count($c)) {
            remove_queue_item($q_item['id']);
            continue;
        }
        if (in_array($c[0]['notify'], $deadguys)) {
            logger('queue: skipping known dead url: ' . $c[0]['notify']);
            update_queue_time($q_item['id']);
            continue;
        }
        if (!poco_reachable($c[0]['url'])) {
            logger('queue: skipping probably dead url: ' . $c[0]['url']);
            update_queue_time($q_item['id']);
            continue;
        }
        $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`\n\t\t\tFROM `user` WHERE `uid` = %d LIMIT 1", intval($c[0]['uid']));
        if (!count($u)) {
            remove_queue_item($q_item['id']);
            continue;
        }
        $data = $qi[0]['content'];
        $public = $qi[0]['batch'];
        $contact = $c[0];
        $owner = $u[0];
        $deliver_status = 0;
        switch ($contact['network']) {
            case NETWORK_DFRN:
                logger('queue: dfrndelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
                $deliver_status = dfrn_deliver($owner, $contact, $data);
                if ($deliver_status == -1) {
                    update_queue_time($q_item['id']);
                    $deadguys[] = $contact['notify'];
                } else {
                    remove_queue_item($q_item['id']);
                }
                break;
            case NETWORK_OSTATUS:
                if ($contact['notify']) {
                    logger('queue: slapdelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
                    $deliver_status = slapper($owner, $contact['notify'], $data);
                    if ($deliver_status == -1) {
                        update_queue_time($q_item['id']);
                    } else {
                        remove_queue_item($q_item['id']);
                    }
                }
                break;
            case NETWORK_DIASPORA:
                if ($contact['notify']) {
                    logger('queue: diaspora_delivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
                    $deliver_status = diaspora_transmit($owner, $contact, $data, $public, true);
                    if ($deliver_status == -1) {
                        update_queue_time($q_item['id']);
                    } else {
                        remove_queue_item($q_item['id']);
                    }
                }
                break;
            default:
                $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
                call_hooks('queue_deliver', $a, $params);
                if ($params['result']) {
                    remove_queue_item($q_item['id']);
                } else {
                    update_queue_time($q_item['id']);
                }
                break;
        }
    }
    return;
}
示例#20
0
 /**
  * Used to increment the failed attempt counter
  */
 public function attemptFailed($sleep = 0.3)
 {
     if ($this->cd_trigger > 0) {
         $count = $this->session->get($this->key . '.info.fail_count', 0) + 1;
         $total = $this->session->get($this->key . '.info.fail_total', 0) + 1;
         $this->session->set($this->key . '.info.fail_count', $count);
         $this->session->set($this->key . '.info.fail_total', $total);
         $this->session->set($this->key . '.info.last_attempt', time());
     }
     if (is_numeric($sleep)) {
         // optional delay to help slow down brute-force attacks
         time_sleep_until(microtime(true) + abs(floatval($sleep)));
     }
 }
示例#21
0
/**
 * Send a zot packet to all hubs where this channel is duplicated, refreshing
 * such things as personal settings, channel permissions, address book updates, etc.
 *
 * @param int $uid
 * @param array $packet (optional) default null
 * @param boolean $groups_changed (optional) default false
 */
function build_sync_packet($uid = 0, $packet = null, $groups_changed = false)
{
    if (UNO) {
        return;
    }
    logger('build_sync_packet');
    if ($packet) {
        logger('packet: ' . print_r($packet, true), LOGGER_DATA, LOG_DEBUG);
    }
    if (!$uid) {
        $uid = local_channel();
    }
    if (!$uid) {
        return;
    }
    $r = q("select * from channel where channel_id = %d limit 1", intval($uid));
    if (!$r) {
        return;
    }
    $channel = $r[0];
    if (intval($channel['channel_removed'])) {
        return;
    }
    $h = q("select * from hubloc where hubloc_hash = '%s' and hubloc_deleted = 0", dbesc($channel['channel_hash']));
    if (!$h) {
        return;
    }
    $synchubs = array();
    foreach ($h as $x) {
        if ($x['hubloc_host'] == App::get_hostname()) {
            continue;
        }
        $y = q("select site_dead from site where site_url = '%s' limit 1", dbesc($x['hubloc_url']));
        if (!$y || $y[0]['site_dead'] == 0) {
            $synchubs[] = $x;
        }
    }
    if (!$synchubs) {
        return;
    }
    $r = q("select xchan_guid, xchan_guid_sig from xchan where xchan_hash  = '%s' limit 1", dbesc($channel['channel_hash']));
    if (!$r) {
        return;
    }
    $env_recips = array();
    $env_recips[] = array('guid' => $r[0]['xchan_guid'], 'guid_sig' => $r[0]['xchan_guid_sig']);
    $info = $packet ? $packet : array();
    $info['type'] = 'channel_sync';
    $info['encoding'] = 'red';
    // note: not zot, this packet is very platform specific
    $info['relocate'] = ['channel_address' => $channel['channel_address'], 'url' => z_root()];
    if (array_key_exists($uid, App::$config) && array_key_exists('transient', App::$config[$uid])) {
        $settings = App::$config[$uid]['transient'];
        if ($settings) {
            $info['config'] = $settings;
        }
    }
    if ($channel) {
        $info['channel'] = array();
        foreach ($channel as $k => $v) {
            // filter out any joined tables like xchan
            if (strpos($k, 'channel_') !== 0) {
                continue;
            }
            // don't pass these elements, they should not be synchronised
            $disallowed = array('channel_id', 'channel_account_id', 'channel_primary', 'channel_prvkey', 'channel_address', 'channel_deleted', 'channel_removed', 'channel_system');
            if (in_array($k, $disallowed)) {
                continue;
            }
            $info['channel'][$k] = $v;
        }
    }
    if ($groups_changed) {
        $r = q("select hash as collection, visible, deleted, gname as name from groups where uid = %d", intval($uid));
        if ($r) {
            $info['collections'] = $r;
        }
        $r = q("select groups.hash as collection, group_member.xchan as member from groups left join group_member on groups.id = group_member.gid where group_member.uid = %d", intval($uid));
        if ($r) {
            $info['collection_members'] = $r;
        }
    }
    $interval = get_config('system', 'delivery_interval') !== false ? intval(get_config('system', 'delivery_interval')) : 2;
    logger('build_sync_packet: packet: ' . print_r($info, true), LOGGER_DATA, LOG_DEBUG);
    $total = count($synchubs);
    foreach ($synchubs as $hub) {
        $hash = random_string();
        $n = zot_build_packet($channel, 'notify', $env_recips, $hub['hubloc_sitekey'], $hash);
        queue_insert(array('hash' => $hash, 'account_id' => $channel['channel_account_id'], 'channel_id' => $channel['channel_id'], 'posturl' => $hub['hubloc_callback'], 'notify' => $n, 'msg' => json_encode($info)));
        Zotlabs\Daemon\Master::Summon(array('Deliver', $hash));
        $total = $total - 1;
        if ($interval && $total) {
            @time_sleep_until(microtime(true) + (double) $interval);
        }
    }
}
示例#22
0
<?php

#########################################################################################################################
#                                                                                                                       #
#   Copyright (c) 2012 by Oscar Buijten; http://myichimoku.eu  and  http://oscar.buijten.fr                             #
#                                                                                                                       #
#   This work is made available under the terms of the Creative Commons Attribution-NonCommercial 3.0 Unported,         #
#                                                                                                                       #
#   http://creativecommons.org/licenses/by-nc/3.0/legalcode                                                             #
#                                                                                                                       #
#   This work is WITHOUT ANY WARRANTY; without even the implied warranty of FITNESS FOR A PARTICULAR PURPOSE.           #
#                                                                                                                       #
#########################################################################################################################
echo "Create and email daily report: " . date('H:i:s', time()) . "\n";
exec("/usr/local/bin/php.ORIG.5 /homez.31/domainese/myichimoku/cron/dailyreport.php > /dev/null &");
$sleepuntil = strtotime("+2 minutes");
time_sleep_until($sleepuntil);
echo "Email reminders: " . date('H:i:s', time()) . "\n";
exec("/usr/local/bin/php.ORIG.5 /homez.31/domainese/myichimoku/cron/reminders.php > /dev/null &");
$sleepuntil = strtotime("+2 minutes");
time_sleep_until($sleepuntil);
echo "Email trade signals: " . date('H:i:s', time()) . "\n";
exec("/usr/local/bin/php.ORIG.5 /homez.31/domainese/myichimoku/cron/tradesignals_email.php > /dev/null &");
$sleepuntil = strtotime("+2 minutes");
time_sleep_until($sleepuntil);
echo "Back-up MyIchimoku application: " . date('H:i:s', time()) . "\n";
exec("/usr/local/bin/php.ORIG.5 /homez.31/domainese/myichimoku/cron/applicationbackup.php > /dev/null &");
$sleepuntil = strtotime("+5 minutes");
time_sleep_until($sleepuntil);
echo "Back-up MyIchimoku database: " . date('H:i:s', time()) . "\n";
exec("/usr/local/bin/perl /homez.31/domainese/www/backup/msd_cron/crondump.pl > /dev/null &");
示例#23
0
 public static function run($argc, $argv)
 {
     $maxsysload = intval(get_config('system', 'maxloadavg'));
     if ($maxsysload < 1) {
         $maxsysload = 50;
     }
     if (function_exists('sys_getloadavg')) {
         $load = sys_getloadavg();
         if (intval($load[0]) > $maxsysload) {
             logger('system: load ' . $load . ' too high. Cron deferred to next scheduled run.');
             return;
         }
     }
     // Check for a lockfile.  If it exists, but is over an hour old, it's stale.  Ignore it.
     $lockfile = 'store/[data]/cron';
     if (file_exists($lockfile) && filemtime($lockfile) > time() - 3600 && !get_config('system', 'override_cron_lockfile')) {
         logger("cron: Already running");
         return;
     }
     // Create a lockfile.  Needs two vars, but $x doesn't need to contain anything.
     file_put_contents($lockfile, $x);
     logger('cron: start');
     // run queue delivery process in the background
     Master::Summon(array('Queue'));
     Master::Summon(array('Poller'));
     // maintenance for mod sharedwithme - check for updated items and remove them
     require_once 'include/sharedwithme.php';
     apply_updates();
     // expire any expired mail
     q("delete from mail where expires > '%s' and expires < %s ", dbesc(NULL_DATE), db_utcnow());
     // expire any expired items
     $r = q("select id from item where expires > '2001-01-01 00:00:00' and expires < %s \n\t\t\tand item_deleted = 0 ", db_utcnow());
     if ($r) {
         require_once 'include/items.php';
         foreach ($r as $rr) {
             drop_item($rr['id'], false);
         }
     }
     // delete expired access tokens
     $r = q("select atoken_id from atoken where atoken_expires > '%s' and atoken_expires < %s", dbesc(NULL_DATE), db_utcnow());
     if ($r) {
         require_once 'include/security.php';
         foreach ($r as $rr) {
             atoken_delete($rr['atoken_id']);
         }
     }
     // Ensure that every channel pings a directory server once a month. This way we can discover
     // channels and sites that quietly vanished and prevent the directory from accumulating stale
     // or dead entries.
     $r = q("select channel_id from channel where channel_dirdate < %s - INTERVAL %s", db_utcnow(), db_quoteinterval('30 DAY'));
     if ($r) {
         foreach ($r as $rr) {
             Master::Summon(array('Directory', $rr['channel_id'], 'force'));
             if ($interval) {
                 @time_sleep_until(microtime(true) + (double) $interval);
             }
         }
     }
     // publish any applicable items that were set to be published in the future
     // (time travel posts). Restrict to items that have come of age in the last
     // couple of days to limit the query to something reasonable.
     $r = q("select id from item where item_delayed = 1 and created <= %s  and created > '%s' ", db_utcnow(), dbesc(datetime_convert('UTC', 'UTC', 'now - 2 days')));
     if ($r) {
         foreach ($r as $rr) {
             $x = q("update item set item_delayed = 0 where id = %d", intval($rr['id']));
             if ($x) {
                 $z = q("select * from item where id = %d", intval($message_id));
                 if ($z) {
                     xchan_query($z);
                     $sync_item = fetch_post_tags($z);
                     build_sync_packet($sync_item[0]['uid'], ['item' => [encode_item($sync_item[0], true)]]);
                 }
                 Master::Summon(array('Notifier', 'wall-new', $rr['id']));
             }
         }
     }
     $abandon_days = intval(get_config('system', 'account_abandon_days'));
     if ($abandon_days < 1) {
         $abandon_days = 0;
     }
     // once daily run birthday_updates and then expire in background
     // FIXME: add birthday updates, both locally and for xprof for use
     // by directory servers
     $d1 = intval(get_config('system', 'last_expire_day'));
     $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
     // Allow somebody to staggger daily activities if they have more than one site on their server,
     // or if it happens at an inconvenient (busy) hour.
     $h1 = intval(get_config('system', 'cron_hour'));
     $h2 = intval(datetime_convert('UTC', 'UTC', 'now', 'G'));
     if ($d2 != $d1 && $h1 == $h2) {
         Master::Summon(array('Cron_daily'));
     }
     // update any photos which didn't get imported properly
     // This should be rare
     $r = q("select xchan_photo_l, xchan_hash from xchan where xchan_photo_l != '' and xchan_photo_m = '' \n\t\t\tand xchan_photo_date < %s - INTERVAL %s", db_utcnow(), db_quoteinterval('1 DAY'));
     if ($r) {
         require_once 'include/photo/photo_driver.php';
         foreach ($r as $rr) {
             $photos = import_xchan_photo($rr['xchan_photo_l'], $rr['xchan_hash']);
             $x = q("update xchan set xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s', xchan_photo_mimetype = '%s'\n\t\t\t\t\twhere xchan_hash = '%s'", dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), dbesc($photos[3]), dbesc($rr['xchan_hash']));
         }
     }
     // pull in some public posts
     if (!get_config('system', 'disable_discover_tab')) {
         Master::Summon(array('Externals'));
     }
     $generation = 0;
     $restart = false;
     if ($argc > 1 && $argv[1] == 'restart') {
         $restart = true;
         $generation = intval($argv[2]);
         if (!$generation) {
             killme();
         }
     }
     reload_plugins();
     $d = datetime_convert();
     // TODO check to see if there are any cronhooks before wasting a process
     if (!$restart) {
         Master::Summon(array('Cronhooks'));
     }
     set_config('system', 'lastcron', datetime_convert());
     //All done - clear the lockfile
     @unlink($lockfile);
     return;
 }
示例#24
0
<?php

ini_set('display_errors', 0);
time_sleep_until(microtime(true) + 0.4);
header('Content-Type: application/json');
echo json_encode(array('status' => 'ok'));
示例#25
0
文件: diaspora.php 项目: Mauru/red
function diaspora_transmit($owner, $contact, $slap, $public_batch, $queue_run = false)
{
    $enabled = intval(get_config('system', 'diaspora_enabled'));
    if (!$enabled) {
        return 200;
    }
    if ($public_batch) {
        $dest_url = $contact['hubloc_callback'] . '/public';
    } else {
        $dest_url = $contact['hubloc_callback'] . '/users/' . $contact['hubloc_guid'];
    }
    logger('diaspora_transmit: URL: ' . $dest_url, LOGGER_DEBUG);
    if (intval(get_config('system', 'diaspora_test'))) {
        return 200;
    }
    $a = get_app();
    $logid = random_string(4);
    logger('diaspora_transmit: ' . $logid . ' ' . $dest_url, LOGGER_DEBUG);
    $hash = random_string();
    $interval = get_config('system', 'delivery_interval') !== false ? intval(get_config('system', 'delivery_interval')) : 2;
    q("insert into outq ( outq_hash, outq_account, outq_channel, outq_driver, outq_posturl, outq_async, outq_created, outq_updated, outq_notify, outq_msg ) values ( '%s', %d, %d, '%s', '%s', %d, '%s', '%s', '%s', '%s' )", dbesc($hash), intval($owner['account_id']), intval($owner['channel_id']), dbesc('post'), dbesc($dest_url), intval(1), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc(''), dbesc($slap));
    proc_run('php', 'include/deliver.php', $hash);
    if ($interval) {
        @time_sleep_until(microtime(true) + (double) $interval);
    }
}
示例#26
0
function poller_run(&$argv, &$argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "include/dba.php";
        $db = new dba($db_host, $db_user, $db_pass, $db_data);
        unset($db_host, $db_user, $db_pass, $db_data);
    }
    require_once 'include/session.php';
    require_once 'include/datetime.php';
    require_once 'library/simplepie/simplepie.inc';
    require_once 'include/items.php';
    require_once 'include/Contact.php';
    require_once 'include/email.php';
    require_once 'include/socgraph.php';
    require_once 'include/pidfile.php';
    load_config('config');
    load_config('system');
    $maxsysload = intval(get_config('system', 'maxloadavg'));
    if ($maxsysload < 1) {
        $maxsysload = 50;
    }
    if (function_exists('sys_getloadavg')) {
        $load = sys_getloadavg();
        if (intval($load[0]) > $maxsysload) {
            logger('system: load ' . $load . ' too high. Poller deferred to next scheduled run.');
            return;
        }
    }
    $lockpath = get_lockpath();
    if ($lockpath != '') {
        $pidfile = new pidfile($lockpath, 'poller');
        if ($pidfile->is_already_running()) {
            logger("poller: Already running");
            if ($pidfile->running_time() > 9 * 60) {
                $pidfile->kill();
                logger("poller: killed stale process");
                // Calling a new instance
                proc_run('php', 'include/poller.php');
            }
            exit;
        }
    }
    $a->set_baseurl(get_config('system', 'url'));
    load_hooks();
    logger('poller: start');
    // run queue delivery process in the background
    proc_run('php', "include/queue.php");
    // run diaspora photo queue process in the background
    proc_run('php', "include/dsprphotoq.php");
    // expire any expired accounts
    q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0 \n\t\tAND `account_expires_on` != '0000-00-00 00:00:00' \n\t\tAND `account_expires_on` < UTC_TIMESTAMP() ");
    // delete user and contact records for recently removed accounts
    $r = q("SELECT * FROM `user` WHERE `account_removed` = 1 AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
    if ($r) {
        foreach ($r as $user) {
            q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
            q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
        }
    }
    $abandon_days = intval(get_config('system', 'account_abandon_days'));
    if ($abandon_days < 1) {
        $abandon_days = 0;
    }
    // Check OStatus conversations
    check_conversations();
    // To-Do: Regenerate usage statistics
    // q("ANALYZE TABLE `item`");
    // once daily run birthday_updates and then expire in background
    $d1 = get_config('system', 'last_expire_day');
    $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
    if ($d2 != intval($d1)) {
        update_contact_birthdays();
        update_suggestions();
        set_config('system', 'last_expire_day', $d2);
        proc_run('php', 'include/expire.php');
    }
    $last = get_config('system', 'cache_last_cleared');
    if ($last) {
        $next = $last + 3600;
        // Once per hour
        $clear_cache = $next <= time();
    } else {
        $clear_cache = true;
    }
    if ($clear_cache) {
        // clear old cache
        Cache::clear();
        // clear old item cache files
        clear_cache();
        // clear cache for photos
        clear_cache($a->get_basepath(), $a->get_basepath() . "/photo");
        // clear smarty cache
        clear_cache($a->get_basepath() . "/view/smarty3/compiled", $a->get_basepath() . "/view/smarty3/compiled");
        // clear cache for image proxy
        if (!get_config("system", "proxy_disabled")) {
            clear_cache($a->get_basepath(), $a->get_basepath() . "/proxy");
            $cachetime = get_config('system', 'proxy_cache_time');
            if (!$cachetime) {
                $cachetime = PROXY_DEFAULT_TIME;
            }
            q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
        }
        set_config('system', 'cache_last_cleared', time());
    }
    $manual_id = 0;
    $generation = 0;
    $hub_update = false;
    $force = false;
    $restart = false;
    if ($argc > 1 && $argv[1] == 'force') {
        $force = true;
    }
    if ($argc > 1 && $argv[1] == 'restart') {
        $restart = true;
        $generation = intval($argv[2]);
        if (!$generation) {
            killme();
        }
    }
    if ($argc > 1 && intval($argv[1])) {
        $manual_id = intval($argv[1]);
        $force = true;
    }
    $interval = intval(get_config('system', 'poll_interval'));
    if (!$interval) {
        $interval = get_config('system', 'delivery_interval') === false ? 3 : intval(get_config('system', 'delivery_interval'));
    }
    $sql_extra = $manual_id ? " AND `id` = {$manual_id} " : "";
    reload_plugins();
    $d = datetime_convert();
    if (!$restart) {
        proc_run('php', 'include/cronhooks.php');
    }
    // Only poll from those with suitable relationships,
    // and which have a polling address and ignore Diaspora since
    // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
    $abandon_sql = $abandon_days ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days)) : '';
    $contacts = q("SELECT `contact`.`id` FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid` \n\t\tWHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''\n\t\tAND NOT `network` IN ( '%s', '%s', '%s' )\n\t\t{$sql_extra} \n\t\tAND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0 \n\t\tAND `contact`.`archive` = 0 \n\t\tAND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 {$abandon_sql} ORDER BY RAND()", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_FACEBOOK), dbesc(NETWORK_PUMPIO));
    if (!count($contacts)) {
        return;
    }
    foreach ($contacts as $c) {
        $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1", intval($c['id']));
        if (!$res || !count($res)) {
            continue;
        }
        foreach ($res as $contact) {
            $xml = false;
            if ($manual_id) {
                $contact['last-update'] = '0000-00-00 00:00:00';
            }
            if ($contact['network'] === NETWORK_DFRN) {
                $contact['priority'] = 2;
            }
            if (!get_config('system', 'ostatus_use_priority') and $contact['network'] === NETWORK_OSTATUS) {
                $contact['priority'] = 2;
            }
            if ($contact['priority'] || $contact['subhub']) {
                $hub_update = true;
                $update = false;
                $t = $contact['last-update'];
                // We should be getting everything via a hub. But just to be sure, let's check once a day.
                // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
                // This also lets us update our subscription to the hub, and add or replace hubs in case it
                // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
                if ($contact['subhub']) {
                    $poll_interval = get_config('system', 'pushpoll_frequency');
                    $contact['priority'] = $poll_interval !== false ? intval($poll_interval) : 3;
                    $hub_update = false;
                    if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day") || $force) {
                        $hub_update = true;
                    }
                } else {
                    $hub_update = false;
                }
                /**
                 * Based on $contact['priority'], should we poll this site now? Or later?
                 */
                switch ($contact['priority']) {
                    case 5:
                        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
                            $update = true;
                        }
                        break;
                    case 4:
                        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
                            $update = true;
                        }
                        break;
                    case 3:
                        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
                            $update = true;
                        }
                        break;
                    case 2:
                        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
                            $update = true;
                        }
                        break;
                    case 1:
                    default:
                        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
                            $update = true;
                        }
                        break;
                }
                if (!$update && !$force) {
                    continue;
                }
            }
            proc_run('php', 'include/onepoll.php', $contact['id']);
            if ($interval) {
                @time_sleep_until(microtime(true) + (double) $interval);
            }
        }
    }
    return;
}
示例#27
0
 private function tickProcessor()
 {
     $this->nextTick = microtime(true);
     while ($this->isRunning) {
         $this->tick();
         $next = $this->nextTick - 0.0001;
         if ($next > microtime(true)) {
             try {
                 time_sleep_until($next);
             } catch (\Exception $e) {
                 //Sometimes $next is less than the current time. High load?
             }
         }
     }
 }
示例#28
0
 /**
  * @throws \Exception
  */
 public function execute()
 {
     Shell::clear();
     ob_start();
     Shell::clearToEol();
     $eolClear = ob_get_clean();
     $eol = $eolClear . "\n";
     $colSeparator = '  | ';
     $lastNumProcessed = 0;
     $firstNumProcessed = 0;
     $lastRangeRateTime = $startTime = time();
     $avgRangeRate = 0;
     $curRangeRate = 0;
     while (true) {
         $loopStartTime = microtime(true);
         $hostMaxLen = 0;
         $hosts = $this->_rangeStats->getProcessingHosts();
         foreach ($hosts as $hostInfo) {
             $len = strlen(self::hostDisplayName($hostInfo['hostname'], $hostInfo['instance']));
             if ($len > $hostMaxLen) {
                 $hostMaxLen = $len;
             }
         }
         $colWidth = $hostMaxLen + 14 + strlen($colSeparator);
         $winWidth = Shell::columns();
         $numCols = floor($winWidth / $colWidth);
         if ($numCols < 1) {
             $numCols = 1;
         }
         $curCol = 1;
         $allReports = StatsCollector::getReports($hosts);
         Shell::home();
         echo $eol;
         echo " Stats for " . $_REQUEST['__path__'] . $eol . $eol;
         $totalItems = 0;
         $processed = 0;
         $errors = 0;
         $skipped = 0;
         $totalRate = 0;
         $numNodes = 0;
         foreach ($hosts as $hostInfo) {
             $hostname = $hostInfo['hostname'];
             $instance = $hostInfo['instance'];
             $reportId = $hostname . '|' . $instance;
             $report = isset($allReports[$reportId]) ? $allReports[$reportId] : false;
             echo " " . str_pad(self::hostDisplayName($hostname, $instance), $hostMaxLen, ' ', STR_PAD_RIGHT);
             if ($report) {
                 $totalItems += $report->totalItems;
                 $processed += $report->totalProcessed;
                 $errors += $report->totalErrors;
                 $skipped += $report->totalSkipped;
                 $totalRate += $report->averageRate;
                 $numNodes++;
                 $rateNum = $report->averageRate;
                 printf(" %4s items/s", $rateNum);
             } else {
                 echo "         DOWN";
             }
             if ($curCol >= $numCols) {
                 echo $eol;
                 $curCol = 1;
             } else {
                 echo $colSeparator;
                 $curCol++;
             }
         }
         if ($curCol != 1) {
             echo $eol;
         }
         echo $eol;
         $avgRate = $numNodes > 0 ? round($totalRate / $numNodes) : 0;
         echo " Active Nodes : " . $numNodes . $eol;
         /*echo " Total Items  : " . $totalItems . $eol;
           echo " Processed    : " . $processed . $eol;
           echo " Skipped      : " . $skipped . $eol;
           echo " Errors       : " . $errors . $eol;*/
         echo " Total Rate   : " . number_format($totalRate) . " items/second" . $eol;
         echo " Average rate : " . number_format($avgRate) . " items/second" . $eol;
         echo $eol . $eol . " Range Statistics:" . $eol;
         $stats = $this->_rangeStats->getStats();
         $processedStats = isset($stats['processed']) ? $stats['processed'] : array();
         $numProcessed = empty($processedStats['Ranges']) ? 0 : $processedStats['Ranges'];
         $numUnprocessed = empty($stats['unprocessed']['Ranges']) ? 0 : $stats['unprocessed']['Ranges'];
         $numProcessing = empty($stats['processing']['Ranges']) ? 0 : $stats['processing']['Ranges'];
         $numFailed = empty($stats['failed']['Ranges']) ? 0 : $stats['failed']['Ranges'];
         $totalRanges = $numProcessed + $numUnprocessed + $numProcessing + $numFailed;
         if ($totalRanges > 0) {
             $processedPct = round($numProcessed * 100 / $totalRanges, 2);
             $unprocessedPct = round($numUnprocessed * 100 / $totalRanges, 2);
             $processingPct = round($numProcessing * 100 / $totalRanges, 2);
             $failedPct = round($numFailed * 100 / $totalRanges, 2);
         } else {
             $processedPct = 0;
             $unprocessedPct = 0;
             $processingPct = 0;
             $failedPct = 0;
         }
         $len = strlen(number_format($totalRanges));
         $fmt = "%-" . $len . "s (%0.2f%%)" . $eol;
         echo "  Processed   : " . sprintf($fmt, number_format($numProcessed), $processedPct);
         echo "  Unprocessed : " . sprintf($fmt, number_format($numUnprocessed), $unprocessedPct);
         echo "  Processing  : " . sprintf($fmt, number_format($numProcessing), $processingPct);
         $failedStr = "  Failed      : " . sprintf($fmt, number_format($numFailed), $failedPct);
         if ($numFailed > 0) {
             $failedStr = "" . $failedStr . "";
         }
         echo $failedStr;
         echo "  Requeued    : " . $this->_rangeStats->getRequeuedRangesDelta() . $eol;
         echo $eol;
         $fields = array('Total Items' => 'num', 'Processed Items' => 'num', '' => 'blank', 'Avg Range Time' => 'time', 'Max Range Time' => 'time', 'Avg Proc Items' => 'num', 'Max Proc Items' => 'num', 'Avg Total Items' => 'num', 'Max Total Items' => 'num');
         $labelWidth = 0;
         foreach ($fields as $field => $type) {
             if ($type != 'blank') {
                 $len = strlen($field);
                 if ($len > $labelWidth) {
                     $labelWidth = $len;
                 }
             }
         }
         foreach ($fields as $field => $type) {
             if ($type == 'blank') {
                 echo $eol;
             } else {
                 $value = empty($processedStats[$field]) ? 0 : $processedStats[$field];
                 echo '  ' . str_pad($field, $labelWidth, ' ', STR_PAD_RIGHT) . ' : ';
                 switch ($type) {
                     case 'num':
                         echo number_format($value);
                         break;
                     case 'rate':
                         echo number_format($value) . ' items/second';
                         break;
                     case 'time':
                         echo number_format($value) . ' seconds';
                         break;
                     default:
                         echo $processedStats[$field];
                         break;
                 }
                 echo $eol;
             }
         }
         // Calculate time remaining
         $avgRangeTime = $this->_rangeStats->getAvgRangeTime(30);
         $remainingMachineSeconds = $avgRangeTime * ($numUnprocessed + $numProcessing);
         $remainingSeconds = $numNodes > 0 ? round($remainingMachineSeconds / $numNodes) : 0;
         echo $eol;
         echo " Remaining time: " . self::formatTime($remainingSeconds) . $eol;
         // Calculate time remaining (method 2)
         $statsTotalItems = empty($processedStats['Total Items']) ? 0 : $processedStats['Total Items'];
         $estimatedTotalItems = $numProcessed == 0 || $totalRanges == 0 ? 0 : $statsTotalItems / ($numProcessed / $totalRanges);
         $estimatedRemainingItems = $estimatedTotalItems - $statsTotalItems;
         $remainingSeconds2 = $totalRate == 0 ? 0 : round($estimatedRemainingItems / $totalRate);
         echo $eol;
         echo " Estimated total items: " . number_format($estimatedTotalItems) . $eol;
         echo " Estimated remaining items: " . number_format($estimatedRemainingItems) . $eol;
         echo " Remaining time: " . self::formatTime($remainingSeconds2) . $eol;
         // Estimate number of items still to process
         $processedRatio = $statsTotalItems == 0 ? 0 : $processedStats['Processed Items'] / $statsTotalItems;
         $estimatedProcessedItems = $estimatedRemainingItems * $processedRatio;
         echo $eol;
         echo " Estimated remaining items to process: " . number_format($estimatedProcessedItems) . $eol;
         Shell::clearToEndOfScreen();
         // make sure it takes at least 1 second to go round the loop
         if (microtime(true) < $loopStartTime + 1) {
             time_sleep_until($loopStartTime + 1);
         }
     }
 }
示例#29
0
文件: timer.php 项目: 01J/topm
 /**
  * Enforce the minimum execution time
  *
  * @param    bool $log Should I log what I'm doing? Default is true.
  */
 public function enforce_min_exec_time($log = true)
 {
     // Try to get a sane value for PHP's maximum_execution_time INI parameter
     if (@function_exists('ini_get')) {
         $php_max_exec = @ini_get("maximum_execution_time");
     } else {
         $php_max_exec = 10;
     }
     if ($php_max_exec == "" || $php_max_exec == 0) {
         $php_max_exec = 10;
     }
     // Decrease $php_max_exec time by 500 msec we need (approx.) to tear down
     // the application, as well as another 500msec added for rounding
     // error purposes. Also make sure this is never gonna be less than 0.
     $php_max_exec = max($php_max_exec * 1000 - 1000, 0);
     // Get the "minimum execution time per step" Akeeba Backup configuration variable
     $configuration = AEFactory::getConfiguration();
     $minexectime = $configuration->get('akeeba.tuning.min_exec_time', 0);
     if (!is_numeric($minexectime)) {
         $minexectime = 0;
     }
     // Make sure we are not over PHP's time limit!
     if ($minexectime > $php_max_exec) {
         $minexectime = $php_max_exec;
     }
     // Get current running time
     $elapsed_time = $this->getRunningTime() * 1000;
     // Only run a sleep delay if we haven't reached the minexectime execution time
     if ($minexectime > $elapsed_time && $elapsed_time > 0) {
         $sleep_msec = $minexectime - $elapsed_time;
         if (function_exists('usleep')) {
             if ($log) {
                 AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Sleeping for {$sleep_msec} msec, using usleep()");
             }
             usleep(1000 * $sleep_msec);
         } elseif (function_exists('time_nanosleep')) {
             if ($log) {
                 AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Sleeping for {$sleep_msec} msec, using time_nanosleep()");
             }
             $sleep_sec = floor($sleep_msec / 1000);
             $sleep_nsec = 1000000 * ($sleep_msec - $sleep_sec * 1000);
             time_nanosleep($sleep_sec, $sleep_nsec);
         } elseif (function_exists('time_sleep_until')) {
             if ($log) {
                 AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Sleeping for {$sleep_msec} msec, using time_sleep_until()");
             }
             $until_timestamp = time() + $sleep_msec / 1000;
             time_sleep_until($until_timestamp);
         } elseif (function_exists('sleep')) {
             $sleep_sec = ceil($sleep_msec / 1000);
             if ($log) {
                 AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Sleeping for {$sleep_sec} seconds, using sleep()");
             }
             sleep($sleep_sec);
         }
     } elseif ($elapsed_time > 0) {
         // No sleep required, even if user configured us to be able to do so.
         if ($log) {
             AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "No need to sleep; execution time: {$elapsed_time} msec; min. exec. time: {$minexectime} msec");
         }
     }
 }
 /**
  * Asserts that $actual is a valid HTML fragment
  *
  * @todo Put this whole stuff in a \PHPUnit_Framework_Constraint and just call assertThat
  *
  * @param        $actual
  * @param string $message
  */
 public function assertValidHTML($actual, $message = 'HTML text is not valid. ')
 {
     if (!USE_EXTERNAL_HTML_VALIDATOR) {
         $doc = $this->loadXML($actual, true);
         $this->assertNotFalse($doc, $message);
         return;
     }
     $actual = $this->wrapHtmlFragment($actual);
     $curlVersion = curl_version();
     // cURL
     $curl = curl_init();
     curl_setopt_array($curl, array(CURLOPT_CONNECTTIMEOUT => 1, CURLOPT_URL => 'http://validator.w3.org/check', CURLOPT_USERAGENT => 'cURL ' . $curlVersion['version'], CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => array('output' => 'json', 'fragment' => $actual)));
     @time_sleep_until(self::$lastValidatorCallTime + 1);
     self::$lastValidatorCallTime = time();
     $response = curl_exec($curl);
     $curlInfo = curl_getinfo($curl);
     curl_close($curl);
     if ($response === false) {
         $this->markTestIncomplete('Could not connect to validation service.');
     }
     if ($curlInfo['http_code'] != '200') {
         $this->markTestIncomplete('Error connecting to validation service. HTTP ' . $curlInfo['http_code']);
     }
     $response = json_decode($response, true);
     if ($response === null) {
         $this->markTestIncomplete('Validation service returned an invalid response (invalid JSON).');
     }
     // fail if errors or warnings
     if (array_key_exists('messages', $response)) {
         foreach ($response['messages'] as $responseMessage) {
             if ($responseMessage['type'] === 'error' || $responseMessage['type'] === 'warning') {
                 $this->fail($message . ucfirst($response['messages'][0]['type']) . ': ' . $response['messages'][0]['message']);
             }
         }
     }
     // valid
     $this->successColor = 'bg-green,fg-black';
     $this->assertTrue(true);
 }