Esempio n. 1
0
 public static function run($argc, $argv)
 {
     logger('onepoll: start');
     if ($argc > 1 && intval($argv[1])) {
         $contact_id = intval($argv[1]);
     }
     if (!$contact_id) {
         logger('onepoll: no contact');
         return;
     }
     $d = datetime_convert();
     $contacts = q("SELECT abook.*, xchan.*, account.*\n\t\t\tFROM abook LEFT JOIN account on abook_account = account_id left join xchan on xchan_hash = abook_xchan \n\t\t\twhere abook_id = %d\n\t\t\tand abook_pending = 0 and abook_archived = 0 and abook_blocked = 0 and abook_ignored = 0\n\t\t\tAND (( account_flags = %d ) OR ( account_flags = %d )) limit 1", intval($contact_id), intval(ACCOUNT_OK), intval(ACCOUNT_UNVERIFIED));
     if (!$contacts) {
         logger('onepoll: abook_id not found: ' . $contact_id);
         return;
     }
     $contact = $contacts[0];
     $t = $contact['abook_updated'];
     $importer_uid = $contact['abook_channel'];
     $r = q("SELECT * from channel left join xchan on channel_hash = xchan_hash where channel_id = %d limit 1", intval($importer_uid));
     if (!$r) {
         return;
     }
     $importer = $r[0];
     logger("onepoll: poll: ({$contact['id']}) IMPORTER: {$importer['xchan_name']}, CONTACT: {$contact['xchan_name']}");
     $last_update = $contact['abook_updated'] === $contact['abook_created'] || $contact['abook_updated'] <= NULL_DATE ? datetime_convert('UTC', 'UTC', 'now - 7 days') : datetime_convert('UTC', 'UTC', $contact['abook_updated'] . ' - 2 days');
     if ($contact['xchan_network'] === 'rss') {
         logger('onepoll: processing feed ' . $contact['xchan_name'], LOGGER_DEBUG);
         handle_feed($importer['channel_id'], $contact_id, $contact['xchan_hash']);
         q("update abook set abook_connected = '%s' where abook_id = %d", dbesc(datetime_convert()), intval($contact['abook_id']));
         return;
     }
     if ($contact['xchan_network'] !== 'zot') {
         return;
     }
     // update permissions
     $x = zot_refresh($contact, $importer);
     $responded = false;
     $updated = datetime_convert();
     $connected = datetime_convert();
     if (!$x) {
         // mark for death by not updating abook_connected, this is caught in include/poller.php
         q("update abook set abook_updated = '%s' where abook_id = %d", dbesc($updated), intval($contact['abook_id']));
     } else {
         q("update abook set abook_updated = '%s', abook_connected = '%s' where abook_id = %d", dbesc($updated), dbesc($connected), intval($contact['abook_id']));
         $responded = true;
     }
     if (!$responded) {
         return;
     }
     if ($contact['xchan_connurl']) {
         $fetch_feed = true;
         $x = null;
         // They haven't given us permission to see their stream
         $can_view_stream = intval(get_abconfig($importer_uid, $contact['abook_xchan'], 'their_perms', 'view_stream'));
         if (!$can_view_stream) {
             $fetch_feed = false;
         }
         // we haven't given them permission to send us their stream
         $can_send_stream = intval(get_abconfig($importer_uid, $contact['abook_xchan'], 'my_perms', 'send_stream'));
         if (!$can_send_stream) {
             $fetch_feed = false;
         }
         if ($fetch_feed) {
             $feedurl = str_replace('/poco/', '/zotfeed/', $contact['xchan_connurl']);
             $feedurl .= '?f=&mindate=' . urlencode($last_update);
             $x = z_fetch_url($feedurl);
             logger('feed_update: ' . print_r($x, true), LOGGER_DATA);
         }
         if ($x && $x['success']) {
             $total = 0;
             logger('onepoll: feed update ' . $contact['xchan_name'] . ' ' . $feedurl);
             $j = json_decode($x['body'], true);
             if ($j['success'] && $j['messages']) {
                 foreach ($j['messages'] as $message) {
                     $results = process_delivery(array('hash' => $contact['xchan_hash']), get_item_elements($message), array(array('hash' => $importer['xchan_hash'])), false);
                     logger('onepoll: feed_update: process_delivery: ' . print_r($results, true), LOGGER_DATA);
                     $total++;
                 }
                 logger("onepoll: {$total} messages processed");
             }
         }
     }
     // update the poco details for this connection
     if ($contact['xchan_connurl']) {
         $r = q("SELECT xlink_id from xlink \n\t\t\t\twhere xlink_xchan = '%s' and xlink_updated > %s - INTERVAL %s and xlink_static = 0 limit 1", intval($contact['xchan_hash']), db_utcnow(), db_quoteinterval('1 DAY'));
         if (!$r) {
             poco_load($contact['xchan_hash'], $contact['xchan_connurl']);
         }
     }
     return;
 }
Esempio n. 2
0
File: zot.php Progetto: 23n/hubzilla
/**
 * @brief Process incoming array of messages.
 *
 * Process an incoming array of messages which were obtained via pickup, and
 * import, update, delete as directed.
 *
 * The message types handled here are 'activity' (e.g. posts), 'mail' ,
 * 'profile', 'location' and 'channel_sync'.
 *
 * @param array $arr
 *  'pickup' structure returned from remote site
 * @param string $sender_url
 *  the url specified by the sender in the initial communication.
 *  We will verify the sender and url in each returned message structure and
 *  also verify that all the messages returned match the site url that we are
 *  currently processing.
 *
 * @returns array
 * suitable for logging remotely, enumerating the processing results of each message/recipient combination
 *  * [0] => \e string $channel_hash
 *  * [1] => \e string $delivery_status
 *  * [2] => \e string $address
 */
function zot_import($arr, $sender_url)
{
    $data = json_decode($arr['body'], true);
    if (!$data) {
        logger('zot_import: empty body');
        return array();
    }
    if (array_key_exists('iv', $data)) {
        $data = json_decode(crypto_unencapsulate($data, get_config('system', 'prvkey')), true);
    }
    if (!$data['success']) {
        if ($data['message']) {
            logger('remote pickup failed: ' . $data['message']);
        }
        return false;
    }
    $incoming = $data['pickup'];
    $return = array();
    if (is_array($incoming)) {
        foreach ($incoming as $i) {
            if (!is_array($i)) {
                logger('incoming is not an array');
                continue;
            }
            $result = null;
            if (array_key_exists('iv', $i['notify'])) {
                $i['notify'] = json_decode(crypto_unencapsulate($i['notify'], get_config('system', 'prvkey')), true);
            }
            logger('zot_import: notify: ' . print_r($i['notify'], true), LOGGER_DATA);
            $hub = zot_gethub($i['notify']['sender']);
            if (!$hub || $hub['hubloc_url'] != $sender_url) {
                logger('zot_import: potential forgery: wrong site for sender: ' . $sender_url . ' != ' . print_r($i['notify'], true));
                continue;
            }
            $message_request = array_key_exists('message_id', $i['notify']) ? true : false;
            if ($message_request) {
                logger('processing message request');
            }
            $i['notify']['sender']['hash'] = make_xchan_hash($i['notify']['sender']['guid'], $i['notify']['sender']['guid_sig']);
            $deliveries = null;
            if (array_key_exists('message', $i) && array_key_exists('type', $i['message']) && $i['message']['type'] === 'rating') {
                // rating messages are processed only by directory servers
                logger('Rating received: ' . print_r($arr, true), LOGGER_DATA);
                $result = process_rating_delivery($i['notify']['sender'], $i['message']);
                continue;
            }
            if (array_key_exists('recipients', $i['notify']) && count($i['notify']['recipients'])) {
                logger('specific recipients');
                $recip_arr = array();
                foreach ($i['notify']['recipients'] as $recip) {
                    if (is_array($recip)) {
                        $recip_arr[] = make_xchan_hash($recip['guid'], $recip['guid_sig']);
                    }
                }
                $r = false;
                if ($recip_arr) {
                    stringify_array_elms($recip_arr);
                    $recips = implode(',', $recip_arr);
                    $r = q("select channel_hash as hash from channel where channel_hash in ( " . $recips . " ) \n\t\t\t\t\t\tand channel_removed = 0 ");
                }
                if (!$r) {
                    logger('recips: no recipients on this site');
                    continue;
                }
                // It's a specifically targetted post. If we were sent a public_scope hint (likely),
                // get rid of it so that it doesn't get stored and cause trouble.
                if ($i && is_array($i) && array_key_exists('message', $i) && is_array($i['message']) && $i['message']['type'] === 'activity' && array_key_exists('public_scope', $i['message'])) {
                    unset($i['message']['public_scope']);
                }
                $deliveries = $r;
                // We found somebody on this site that's in the recipient list.
            } else {
                if ($i['message'] && array_key_exists('flags', $i['message']) && in_array('private', $i['message']['flags']) && $i['message']['type'] === 'activity') {
                    if (array_key_exists('public_scope', $i['message']) && $i['message']['public_scope'] === 'public') {
                        // This should not happen but until we can stop it...
                        logger('private message was delivered with no recipients.');
                        continue;
                    }
                }
                logger('public post');
                // Public post. look for any site members who are or may be accepting posts from this sender
                // and who are allowed to see them based on the sender's permissions
                $deliveries = allowed_public_recips($i);
                if ($i['message'] && array_key_exists('type', $i['message']) && $i['message']['type'] === 'location') {
                    $sys = get_sys_channel();
                    $deliveries = array(array('hash' => $sys['xchan_hash']));
                }
                // if the scope is anything but 'public' we're going to store it as private regardless
                // of the private flag on the post.
                if ($i['message'] && array_key_exists('public_scope', $i['message']) && $i['message']['public_scope'] !== 'public') {
                    if (!array_key_exists('flags', $i['message'])) {
                        $i['message']['flags'] = array();
                    }
                    if (!in_array('private', $i['message']['flags'])) {
                        $i['message']['flags'][] = 'private';
                    }
                }
            }
            // Go through the hash array and remove duplicates. array_unique() won't do this because the array is more than one level.
            $no_dups = array();
            if ($deliveries) {
                foreach ($deliveries as $d) {
                    if (!in_array($d['hash'], $no_dups)) {
                        $no_dups[] = $d['hash'];
                    }
                }
                if ($no_dups) {
                    $deliveries = array();
                    foreach ($no_dups as $n) {
                        $deliveries[] = array('hash' => $n);
                    }
                }
            }
            if (!$deliveries) {
                logger('zot_import: no deliveries on this site');
                continue;
            }
            if ($i['message']) {
                if ($i['message']['type'] === 'activity') {
                    $arr = get_item_elements($i['message']);
                    $v = validate_item_elements($i['message'], $arr);
                    if (!$v['success']) {
                        logger('Activity rejected: ' . $v['message'] . ' ' . print_r($i['message'], true));
                        continue;
                    }
                    logger('Activity received: ' . print_r($arr, true), LOGGER_DATA);
                    logger('Activity recipients: ' . print_r($deliveries, true), LOGGER_DATA);
                    $relay = array_key_exists('flags', $i['message']) && in_array('relay', $i['message']['flags']) ? true : false;
                    $result = process_delivery($i['notify']['sender'], $arr, $deliveries, $relay, false, $message_request);
                } elseif ($i['message']['type'] === 'mail') {
                    $arr = get_mail_elements($i['message']);
                    logger('Mail received: ' . print_r($arr, true), LOGGER_DATA);
                    logger('Mail recipients: ' . print_r($deliveries, true), LOGGER_DATA);
                    $result = process_mail_delivery($i['notify']['sender'], $arr, $deliveries);
                } elseif ($i['message']['type'] === 'profile') {
                    $arr = get_profile_elements($i['message']);
                    logger('Profile received: ' . print_r($arr, true), LOGGER_DATA);
                    logger('Profile recipients: ' . print_r($deliveries, true), LOGGER_DATA);
                    $result = process_profile_delivery($i['notify']['sender'], $arr, $deliveries);
                } elseif ($i['message']['type'] === 'channel_sync') {
                    // $arr = get_channelsync_elements($i['message']);
                    $arr = $i['message'];
                    logger('Channel sync received: ' . print_r($arr, true), LOGGER_DATA);
                    logger('Channel sync recipients: ' . print_r($deliveries, true), LOGGER_DATA);
                    $result = process_channel_sync_delivery($i['notify']['sender'], $arr, $deliveries);
                } elseif ($i['message']['type'] === 'location') {
                    $arr = $i['message'];
                    logger('Location message received: ' . print_r($arr, true), LOGGER_DATA);
                    logger('Location message recipients: ' . print_r($deliveries, true), LOGGER_DATA);
                    $result = process_location_delivery($i['notify']['sender'], $arr, $deliveries);
                }
            }
            if ($result) {
                $return = array_merge($return, $result);
            }
        }
    }
    return $return;
}
Esempio n. 3
0
function import_items($channel, $items, $sync = false, $relocate = null)
{
    if ($channel && $items) {
        $allow_code = false;
        $r = q("select account_id, account_roles, channel_pageflags from account left join channel on channel_account_id = account_id \n\t\t\twhere channel_id = %d limit 1", intval($channel['channel_id']));
        if ($r) {
            if ($r[0]['account_roles'] & ACCOUNT_ROLE_ALLOWCODE || $r[0]['channel_pageflags'] & PAGE_ALLOWCODE) {
                $allow_code = true;
            }
        }
        $deliver = false;
        // Don't deliver any messages or notifications when importing
        foreach ($items as $i) {
            $item_result = false;
            $item = get_item_elements($i, $allow_code);
            if (!$item) {
                continue;
            }
            if ($relocate && $item['mid'] === $item['parent_mid']) {
                item_url_replace($channel, $item, $relocate['url'], z_root(), $relocate['channel_address']);
            }
            $r = q("select id, edited from item where mid = '%s' and uid = %d limit 1", dbesc($item['mid']), intval($channel['channel_id']));
            if ($r) {
                // flags may have changed and we are probably relocating the post,
                // so force an update even if we have the same timestamp
                if ($item['edited'] >= $r[0]['edited']) {
                    $item['id'] = $r[0]['id'];
                    $item['uid'] = $channel['channel_id'];
                    $item_result = item_store_update($item, $allow_code, $deliver);
                }
            } else {
                $item['aid'] = $channel['channel_account_id'];
                $item['uid'] = $channel['channel_id'];
                $item_result = item_store($item, $allow_code, $deliver);
            }
            if ($sync && $item['item_wall']) {
                // deliver singletons if we have any
                if ($item_result && $item_result['success']) {
                    Zotlabs\Daemon\Master::Summon(['Notifier', 'single_activity', $item_result['item_id']]);
                }
            }
        }
    }
}
Esempio n. 4
0
function import_items($channel, $items)
{
    if ($channel && $items) {
        $allow_code = false;
        $r = q("select account_id, account_roles, channel_pageflags from account left join channel on channel_account_id = account_id \n\t\t\twhere channel_id = %d limit 1", intval($channel['channel_id']));
        if ($r) {
            if ($r[0]['account_roles'] & ACCOUNT_ROLE_ALLOWCODE || $r[0]['channel_pageflags'] & PAGE_ALLOWCODE) {
                $allow_code = true;
            }
        }
        foreach ($items as $i) {
            $item = get_item_elements($i, $allow_code);
            if (!$item) {
                continue;
            }
            $r = q("select id, edited from item where mid = '%s' and uid = %d limit 1", dbesc($item['mid']), intval($channel['channel_id']));
            if ($r) {
                if ($item['edited'] > $r[0]['edited']) {
                    $item['id'] = $r[0]['id'];
                    $item['uid'] = $channel['channel_id'];
                    item_store_update($item);
                    continue;
                }
            } else {
                $item['aid'] = $channel['channel_account_id'];
                $item['uid'] = $channel['channel_id'];
                $item_result = item_store($item);
            }
        }
    }
}
Esempio n. 5
0
function externals_run($argv, $argc)
{
    cli_startup();
    $a = get_app();
    $total = 0;
    $attempts = 0;
    logger('externals: startup', LOGGER_DEBUG);
    // pull in some public posts
    while ($total == 0 && $attempts < 3) {
        $arr = array('url' => '');
        call_hooks('externals_url_select', $arr);
        if ($arr['url']) {
            $url = $arr['url'];
        } else {
            $randfunc = db_getfunc('RAND');
            // fixme this query does not deal with directory realms.
            $r = q("select site_url, site_pull from site where site_url != '%s' and site_flags != %d and site_type = %d and site_dead = 0 order by {$randfunc} limit 1", dbesc(z_root()), intval(DIRECTORY_MODE_STANDALONE), intval(SITE_TYPE_ZOT));
            if ($r) {
                $url = $r[0]['site_url'];
            }
        }
        $blacklisted = false;
        if (!check_siteallowed($url)) {
            logger('blacklisted site: ' . $url);
            $blacklisted = true;
        }
        $attempts++;
        // make sure we can eventually break out if somebody blacklists all known sites
        if ($blacklisted) {
            if ($attempts > 20) {
                break;
            }
            $attempts--;
            continue;
        }
        if ($url) {
            if ($r[0]['site_pull'] !== NULL_DATE) {
                $mindate = urlencode(datetime_convert('', '', $r[0]['site_pull'] . ' - 1 day'));
            } else {
                $days = get_config('externals', 'since_days');
                if ($days === false) {
                    $days = 15;
                }
                $mindate = urlencode(datetime_convert('', '', 'now - ' . intval($days) . ' days'));
            }
            $feedurl = $url . '/zotfeed?f=&mindate=' . $mindate;
            logger('externals: pulling public content from ' . $feedurl, LOGGER_DEBUG);
            $x = z_fetch_url($feedurl);
            if ($x && $x['success']) {
                q("update site set site_pull = '%s' where site_url = '%s'", dbesc(datetime_convert()), dbesc($url));
                $j = json_decode($x['body'], true);
                if ($j['success'] && $j['messages']) {
                    $sys = get_sys_channel();
                    foreach ($j['messages'] as $message) {
                        // on these posts, clear any route info.
                        $message['route'] = '';
                        $results = process_delivery(array('hash' => 'undefined'), get_item_elements($message), array(array('hash' => $sys['xchan_hash'])), false, true);
                        $total++;
                    }
                    logger('externals: import_public_posts: ' . $total . ' messages imported', LOGGER_DEBUG);
                }
            }
        }
    }
}
Esempio n. 6
0
function externals_run($argv, $argc)
{
    cli_startup();
    $a = get_app();
    $total = 0;
    $attempts = 0;
    logger('externals: startup', LOGGER_DEBUG);
    // pull in some public posts
    while ($total == 0 && $attempts < 3) {
        $arr = array('url' => '');
        call_hooks('externals_url_select', $arr);
        if ($arr['url']) {
            $url = $arr['url'];
        } else {
            $randfunc = db_getfunc('RAND');
            $r = q("select site_url, site_pull from site where site_url != '%s' and site_flags != %d order by {$randfunc} limit 1", dbesc(z_root()), intval(DIRECTORY_MODE_STANDALONE));
            if ($r) {
                $url = $r[0]['site_url'];
            }
        }
        // Note: blacklisted sites must be stored in the config as an array.
        // No simple way to turn this into a personal config because we have no identity here.
        // For that we probably need a variant of superblock.
        $blacklisted = false;
        $bl1 = get_config('system', 'blacklisted_sites');
        if (is_array($bl1) && $bl1) {
            foreach ($bl1 as $bl) {
                if ($bl && strpos($url, $bl) !== false) {
                    $blacklisted = true;
                    break;
                }
            }
        }
        $attempts++;
        // make sure we can eventually break out if somebody blacklists all known sites
        if ($blacklisted) {
            if ($attempts > 20) {
                break;
            }
            $attempts--;
            continue;
        }
        if ($url) {
            if ($r[0]['site_pull'] !== NULL_DATE) {
                $mindate = urlencode(datetime_convert('', '', $r[0]['site_pull'] . ' - 1 day'));
            } else {
                $days = get_config('externals', 'since_days');
                if ($days === false) {
                    $days = 15;
                }
                $mindate = urlencode(datetime_convert('', '', 'now - ' . intval($days) . ' days'));
            }
            $feedurl = $url . '/zotfeed?f=&mindate=' . $mindate;
            logger('externals: pulling public content from ' . $feedurl, LOGGER_DEBUG);
            $x = z_fetch_url($feedurl);
            if ($x && $x['success']) {
                q("update site set site_pull = '%s' where site_url = '%s'", dbesc(datetime_convert()), dbesc($url));
                $j = json_decode($x['body'], true);
                if ($j['success'] && $j['messages']) {
                    $sys = get_sys_channel();
                    foreach ($j['messages'] as $message) {
                        // on these posts, clear any route info.
                        $message['route'] = '';
                        $results = process_delivery(array('hash' => 'undefined'), get_item_elements($message), array(array('hash' => $sys['xchan_hash'])), false, true);
                        $total++;
                        //						$z = q("select id from item where mid = '%s' and uid = %d limit 1",
                        //							dbesc($message['message_id']),
                        //							intval($sys['channel_id'])
                        //						);
                        $z = null;
                        if ($z) {
                            $flag_bits = ITEM_WALL | ITEM_ORIGIN | ITEM_UPLINK;
                            // preserve the source
                            $r = q("update item set source_xchan = owner_xchan where id = %d", intval($z[0]['id']));
                            $r = q("update item set item_flags = ( item_flags | %d ), owner_xchan = '%s' \n\t\t\t\t\t\t\t\twhere id = %d", intval($flag_bits), dbesc($sys['xchan_hash']), intval($z[0]['id']));
                        }
                    }
                    logger('externals: import_public_posts: ' . $total . ' messages imported', LOGGER_DEBUG);
                }
            }
        }
    }
}
Esempio n. 7
0
function import_post(&$a)
{
    $account_id = get_account_id();
    if (!$account_id) {
        return;
    }
    $max_identities = account_service_class_fetch($account_id, 'total_identities');
    $max_friends = account_service_class_fetch($account_id, 'total_channels');
    $max_feeds = account_service_class_fetch($account_id, 'total_feeds');
    if ($max_identities !== false) {
        $r = q("select channel_id from channel where channel_account_id = %d", intval($account_id));
        if ($r && count($r) > $max_identities) {
            notice(sprintf(t('Your service plan only allows %d channels.'), $max_identities) . EOL);
            return;
        }
    }
    $data = null;
    $seize = x($_REQUEST, 'make_primary') ? intval($_REQUEST['make_primary']) : 0;
    $import_posts = x($_REQUEST, 'import_posts') ? intval($_REQUEST['import_posts']) : 0;
    $src = $_FILES['filename']['tmp_name'];
    $filename = basename($_FILES['filename']['name']);
    $filesize = intval($_FILES['filename']['size']);
    $filetype = $_FILES['filename']['type'];
    if ($src) {
        if ($filesize) {
            $data = @file_get_contents($src);
        }
        unlink($src);
    }
    if (!$src) {
        $old_address = x($_REQUEST, 'old_address') ? $_REQUEST['old_address'] : '';
        if (!$old_address) {
            logger('mod_import: nothing to import.');
            notice(t('Nothing to import.') . EOL);
            return;
        }
        $email = x($_REQUEST, 'email') ? $_REQUEST['email'] : '';
        $password = x($_REQUEST, 'password') ? $_REQUEST['password'] : '';
        $channelname = substr($old_address, 0, strpos($old_address, '@'));
        $servername = substr($old_address, strpos($old_address, '@') + 1);
        $scheme = 'https://';
        $api_path = '/api/red/channel/export/basic?f=&channel=' . $channelname;
        if ($import_posts) {
            $api_path .= '&posts=1';
        }
        $binary = false;
        $redirects = 0;
        $opts = array('http_auth' => $email . ':' . $password);
        $url = $scheme . $servername . $api_path;
        $ret = z_fetch_url($url, $binary, $redirects, $opts);
        if (!$ret['success']) {
            $ret = z_fetch_url('http://' . $servername . $api_path, $binary, $redirects, $opts);
        }
        if ($ret['success']) {
            $data = $ret['body'];
        } else {
            notice(t('Unable to download data from old server') . EOL);
        }
    }
    if (!$data) {
        logger('mod_import: empty file.');
        notice(t('Imported file is empty.') . EOL);
        return;
    }
    $data = json_decode($data, true);
    //	logger('import: data: ' . print_r($data,true));
    //	print_r($data);
    // import channel
    $channel = $data['channel'];
    $r = q("select * from channel where (channel_guid = '%s' or channel_hash = '%s' or channel_address = '%s' ) limit 1", dbesc($channel['channel_guid']), dbesc($channel['channel_hash']), dbesc($channel['channel_address']));
    // We should probably also verify the hash
    if ($r) {
        if ($r[0]['channel_guid'] === $channel['channel_guid'] || $r[0]['channel_hash'] === $channel['channel_hash']) {
            logger('mod_import: duplicate channel. ', print_r($channel, true));
            notice(t('Cannot create a duplicate channel identifier on this system. Import failed.') . EOL);
            return;
        } else {
            // try at most ten times to generate a unique address.
            $x = 0;
            $found_unique = false;
            do {
                $tmp = $channel['channel_address'] . mt_rand(1000, 9999);
                $r = q("select * from channel where channel_address = '%s' limit 1", dbesc($tmp));
                if (!$r) {
                    $channel['channel_address'] = $tmp;
                    $found_unique = true;
                    break;
                }
                $x++;
            } while ($x < 10);
            if (!$found_unique) {
                logger('mod_import: duplicate channel. randomisation failed.', print_r($channel, true));
                notice(t('Unable to create a unique channel address. Import failed.') . EOL);
                return;
            }
        }
    }
    unset($channel['channel_id']);
    $channel['channel_account_id'] = get_account_id();
    $channel['channel_primary'] = $seize ? 1 : 0;
    dbesc_array($channel);
    $r = dbq("INSERT INTO channel (`" . implode("`, `", array_keys($channel)) . "`) VALUES ('" . implode("', '", array_values($channel)) . "')");
    if (!$r) {
        logger('mod_import: channel clone failed. ', print_r($channel, true));
        notice(t('Channel clone failed. Import failed.') . EOL);
        return;
    }
    $r = q("select * from channel where channel_account_id = %d and channel_guid = '%s' limit 1", intval(get_account_id()), $channel['channel_guid']);
    if (!$r) {
        logger('mod_import: channel not found. ', print_r($channel, true));
        notice(t('Cloned channel not found. Import failed.') . EOL);
        return;
    }
    // reset
    $channel = $r[0];
    set_default_login_identity(get_account_id(), $channel['channel_id'], false);
    if ($data['photo']) {
        require_once 'include/photo/photo_driver.php';
        import_channel_photo(base64url_decode($data['photo']['data']), $data['photo']['type'], get_account_id(), $channel['channel_id']);
    }
    $profiles = $data['profile'];
    if ($profiles) {
        foreach ($profiles as $profile) {
            unset($profile['id']);
            $profile['aid'] = get_account_id();
            $profile['uid'] = $channel['channel_id'];
            // we are going to reset all profile photos to the original
            // somebody will have to fix this later and put all the applicable photos into the export
            $profile['photo'] = z_root() . '/photo/profile/l/' . $channel['channel_id'];
            $profile['thumb'] = z_root() . '/photo/profile/m/' . $channel['channel_id'];
            dbesc_array($profile);
            $r = dbq("INSERT INTO profile (`" . implode("`, `", array_keys($profile)) . "`) VALUES ('" . implode("', '", array_values($profile)) . "')");
        }
    }
    $hublocs = $data['hubloc'];
    if ($hublocs) {
        foreach ($hublocs as $hubloc) {
            $arr = array('guid' => $hubloc['hubloc_guid'], 'guid_sig' => $hubloc['guid_sig'], 'url' => $hubloc['hubloc_url'], 'url_sig' => $hubloc['hubloc_url_sig']);
            if ($hubloc['hubloc_hash'] === $channel['channel_hash'] && $hubloc['hubloc_flags'] & HUBLOC_FLAGS_PRIMARY && $seize) {
                $hubloc['hubloc_flags'] = $hubloc['hubloc_flags'] ^ HUBLOC_FLAGS_PRIMARY;
            }
            if (!zot_gethub($arr)) {
                unset($hubloc['hubloc_id']);
                dbesc_array($hubloc);
                $r = dbq("INSERT INTO hubloc (`" . implode("`, `", array_keys($hubloc)) . "`) VALUES ('" . implode("', '", array_values($hubloc)) . "')");
            }
        }
    }
    // create new hubloc for the new channel at this site
    $r = q("insert into hubloc ( hubloc_guid, hubloc_guid_sig, hubloc_hash, hubloc_addr, hubloc_network, hubloc_flags, \n\t\thubloc_url, hubloc_url_sig, hubloc_host, hubloc_callback, hubloc_sitekey )\n\t\tvalues ( '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s' )", dbesc($channel['channel_guid']), dbesc($channel['channel_guid_sig']), dbesc($channel['channel_hash']), dbesc($channel['channel_address'] . '@' . get_app()->get_hostname()), dbesc('zot'), intval($seize ? HUBLOC_FLAGS_PRIMARY : 0), dbesc(z_root()), dbesc(base64url_encode(rsa_sign(z_root(), $channel['channel_prvkey']))), dbesc(get_app()->get_hostname()), dbesc(z_root() . '/post'), dbesc(get_config('system', 'pubkey')));
    // reset the original primary hubloc if it is being seized
    if ($seize) {
        $r = q("update hubloc set hubloc_flags = (hubloc_flags & ~%d) where (hubloc_flags & %d)>0 and hubloc_hash = '%s' and hubloc_url != '%s' ", intval(HUBLOC_FLAGS_PRIMARY), intval(HUBLOC_FLAGS_PRIMARY), dbesc($channel['channel_hash']), dbesc(z_root()));
    }
    // import xchans and contact photos
    if ($seize) {
        // replace any existing xchan we may have on this site if we're seizing control
        $r = q("delete from xchan where xchan_hash = '%s'", dbesc($channel['channel_hash']));
        $r = q("insert into xchan ( xchan_hash, xchan_guid, xchan_guid_sig, xchan_pubkey, xchan_photo_l, xchan_photo_m, xchan_photo_s, xchan_addr, xchan_url, xchan_follow, xchan_connurl, xchan_name, xchan_network, xchan_photo_date, xchan_name_date ) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", dbesc($channel['channel_hash']), dbesc($channel['channel_guid']), dbesc($channel['channel_guid_sig']), dbesc($channel['channel_pubkey']), dbesc($a->get_baseurl() . "/photo/profile/l/" . $channel['channel_id']), dbesc($a->get_baseurl() . "/photo/profile/m/" . $channel['channel_id']), dbesc($a->get_baseurl() . "/photo/profile/s/" . $channel['channel_id']), dbesc($channel['channel_address'] . '@' . get_app()->get_hostname()), dbesc(z_root() . '/channel/' . $channel['channel_address']), dbesc(z_root() . '/follow?f=&url=%s'), dbesc(z_root() . '/poco/' . $channel['channel_address']), dbesc($channel['channel_name']), dbesc('zot'), dbesc(datetime_convert()), dbesc(datetime_convert()));
    }
    $xchans = $data['xchan'];
    if ($xchans) {
        foreach ($xchans as $xchan) {
            $r = q("select xchan_hash from xchan where xchan_hash = '%s' limit 1", dbesc($xchan['xchan_hash']));
            if ($r) {
                continue;
            }
            dbesc_array($xchan);
            $r = dbq("INSERT INTO xchan (`" . implode("`, `", array_keys($xchan)) . "`) VALUES ('" . implode("', '", array_values($xchan)) . "')");
            require_once 'include/photo/photo_driver.php';
            $photos = import_profile_photo($xchan['xchan_photo_l'], $xchan['xchan_hash']);
            if ($photos[4]) {
                $photodate = NULL_DATE;
            } else {
                $photodate = $xchan['xchan_photo_date'];
            }
            $r = q("update xchan set xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s', xchan_photo_mimetype = '%s', xchan_photo_date = '%s'\n\t\t\t\twhere xchan_hash = '%s'", dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), dbesc($photos[3]), dbesc($photodate), dbesc($xchan_hash));
        }
    }
    // FIXME - ensure we have an xchan if somebody is trying to pull a fast one
    $friends = 0;
    $feeds = 0;
    // import contacts
    $abooks = $data['abook'];
    if ($abooks) {
        foreach ($abooks as $abook) {
            if ($max_friends !== false && $friends > $max_friends) {
                continue;
            }
            if ($max_feeds !== false && $abook['abook_flags'] & ABOOK_FLAG_FEED && $feeds > $max_feeds) {
                continue;
            }
            unset($abook['abook_id']);
            $abook['abook_account'] = get_account_id();
            $abook['abook_channel'] = $channel['channel_id'];
            dbesc_array($abook);
            $r = dbq("INSERT INTO abook (`" . implode("`, `", array_keys($abook)) . "`) VALUES ('" . implode("', '", array_values($abook)) . "')");
            $friends++;
            if ($abook['abook_flags'] & ABOOK_FLAG_FEED) {
                $feeds++;
            }
        }
    }
    $configs = $data['config'];
    if ($configs) {
        foreach ($configs as $config) {
            unset($config['id']);
            $config['uid'] = $channel['channel_id'];
            dbesc_array($config);
            $r = dbq("INSERT INTO pconfig (`" . implode("`, `", array_keys($config)) . "`) VALUES ('" . implode("', '", array_values($config)) . "')");
        }
    }
    $groups = $data['group'];
    if ($groups) {
        $saved = array();
        foreach ($groups as $group) {
            $saved[$group['hash']] = array('old' => $group['id']);
            unset($group['id']);
            $group['uid'] = $channel['channel_id'];
            dbesc_array($group);
            $r = dbq("INSERT INTO groups (`" . implode("`, `", array_keys($group)) . "`) VALUES ('" . implode("', '", array_values($group)) . "')");
        }
        $r = q("select * from `groups` where uid = %d", intval($channel['channel_id']));
        if ($r) {
            foreach ($r as $rr) {
                $saved[$rr['hash']]['new'] = $rr['id'];
            }
        }
    }
    $group_members = $data['group_member'];
    if ($groups_members) {
        foreach ($group_members as $group_member) {
            unset($group_member['id']);
            $group_member['uid'] = $channel['channel_id'];
            foreach ($saved as $x) {
                if ($x['old'] == $group_member['gid']) {
                    $group_member['gid'] = $x['new'];
                }
            }
            dbesc_array($group_member);
            $r = dbq("INSERT INTO group_member (`" . implode("`, `", array_keys($group_member)) . "`) VALUES ('" . implode("', '", array_values($group_member)) . "')");
        }
    }
    $saved_notification_flags = notifications_off($channel['channel_id']);
    if ($import_posts && array_key_exists('item', $data) && $data['item']) {
        foreach ($data['item'] as $i) {
            $item = get_item_elements($i);
            $r = q("select id, edited from item where mid = '%s' and uid = %d limit 1", dbesc($item['mid']), intval($channel['channel_id']));
            if ($r) {
                if ($item['edited'] > $r[0]['edited']) {
                    $item['id'] = $r[0]['id'];
                    $item['uid'] = $channel['channel_id'];
                    item_store_update($item);
                    continue;
                }
            } else {
                $item['aid'] = $channel['channel_account_id'];
                $item['uid'] = $channel['channel_id'];
                $item_result = item_store($item);
            }
        }
    }
    notifications_on($channel['channel_id'], $saved_notification_flags);
    if (array_key_exists('item_id', $data) && $data['item_id']) {
        foreach ($data['item_id'] as $i) {
            $r = q("select id from item where mid = '%s' and uid = %d limit 1", dbesc($i['mid']), intval($channel['channel_id']));
            if (!$r) {
                continue;
            }
            $z = q("select * from item_id where service = '%s' and sid = '%s' and iid = %d and uid = %d limit 1", dbesc($i['service']), dbesc($i['sid']), intval($r[0]['id']), intval($channel['channel_id']));
            if (!$z) {
                q("insert into item_id (iid,uid,sid,service) values(%d,%d,'%s','%s')", intval($r[0]['id']), intval($channel['channel_id']), dbesc($i['sid']), dbesc($i['service']));
            }
        }
    }
    // FIXME - ensure we have a self entry if somebody is trying to pull a fast one
    // send out refresh requests
    // notify old server that it may no longer be primary.
    proc_run('php', 'include/notifier.php', 'location', $channel['channel_id']);
    // This will indirectly perform a refresh_all *and* update the directory
    proc_run('php', 'include/directory.php', $channel['channel_id']);
    notice(t('Import completed.') . EOL);
    change_channel($channel['channel_id']);
    goaway(z_root() . '/network');
}
Esempio n. 8
0
/**
 * @brief
 *
 * @param array $channel
 * @param array $observer
 * @param array $args
 * @return array
 */
function photo_upload($channel, $observer, $args)
{
    $ret = array('success' => false);
    $channel_id = $channel['channel_id'];
    $account_id = $channel['channel_account_id'];
    if (!perm_is_allowed($channel_id, $observer['xchan_hash'], 'write_storage')) {
        $ret['message'] = t('Permission denied.');
        return $ret;
    }
    //	call_hooks('photo_upload_begin', $args);
    /*
     * Determine the album to use
     */
    $album = $args['album'];
    if (intval($args['visible']) || $args['visible'] === 'true') {
        $visible = 1;
    } else {
        $visible = 0;
    }
    $deliver = true;
    if (array_key_exists('deliver', $args)) {
        $deliver = intval($args['deliver']);
    }
    // Set to default channel permissions. If the parent directory (album) has permissions set,
    // use those instead. If we have specific permissions supplied, they take precedence over
    // all other settings. 'allow_cid' being passed from an external source takes priority over channel settings.
    // ...messy... needs re-factoring once the photos/files integration stabilises
    $acl = new Zotlabs\Access\AccessList($channel);
    if (array_key_exists('directory', $args) && $args['directory']) {
        $acl->set($args['directory']);
    }
    if (array_key_exists('allow_cid', $args)) {
        $acl->set($args);
    }
    if (array_key_exists('group_allow', $args) || array_key_exists('contact_allow', $args) || array_key_exists('group_deny', $args) || array_key_exists('contact_deny', $args)) {
        $acl->set_from_array($args);
    }
    $ac = $acl->get();
    $os_storage = 0;
    if ($args['os_path'] && $args['getimagesize']) {
        $imagedata = @file_get_contents($args['os_path']);
        $filename = $args['filename'];
        $filesize = strlen($imagedata);
        // this is going to be deleted if it exists
        $src = '/tmp/deletemenow';
        $type = $args['getimagesize']['mime'];
        $os_storage = 1;
    } elseif ($args['data'] || $args['content']) {
        // allow an import from a binary string representing the image.
        // This bypasses the upload step and max size limit checking
        $imagedata = $args['content'] ? $args['content'] : $args['data'];
        $filename = $args['filename'];
        $filesize = strlen($imagedata);
        // this is going to be deleted if it exists
        $src = '/tmp/deletemenow';
        $type = $args['mimetype'] ? $args['mimetype'] : $args['type'];
    } else {
        $f = array('src' => '', 'filename' => '', 'filesize' => 0, 'type' => '');
        //		call_hooks('photo_upload_file',$f);
        if (x($f, 'src') && x($f, 'filesize')) {
            $src = $f['src'];
            $filename = $f['filename'];
            $filesize = $f['filesize'];
            $type = $f['type'];
        } else {
            $src = $_FILES['userfile']['tmp_name'];
            $filename = basename($_FILES['userfile']['name']);
            $filesize = intval($_FILES['userfile']['size']);
            $type = $_FILES['userfile']['type'];
        }
        if (!$type) {
            $type = guess_image_type($filename);
        }
        logger('photo_upload: received file: ' . $filename . ' as ' . $src . ' (' . $type . ') ' . $filesize . ' bytes', LOGGER_DEBUG);
        $maximagesize = get_config('system', 'maximagesize');
        if ($maximagesize && $filesize > $maximagesize) {
            $ret['message'] = sprintf(t('Image exceeds website size limit of %lu bytes'), $maximagesize);
            @unlink($src);
            call_hooks('photo_upload_end', $ret);
            return $ret;
        }
        if (!$filesize) {
            $ret['message'] = t('Image file is empty.');
            @unlink($src);
            call_hooks('photo_post_end', $ret);
            return $ret;
        }
        logger('photo_upload: loading the contents of ' . $src, LOGGER_DEBUG);
        $imagedata = @file_get_contents($src);
    }
    $r = q("select sum(filesize) as total from photo where aid = %d and imgscale = 0 ", intval($account_id));
    $limit = engr_units_to_bytes(service_class_fetch($channel_id, 'photo_upload_limit'));
    if ($r && $limit !== false && $r[0]['total'] + strlen($imagedata) > $limit) {
        $ret['message'] = upgrade_message();
        @unlink($src);
        call_hooks('photo_post_end', $ret);
        return $ret;
    }
    $ph = photo_factory($imagedata, $type);
    if (!$ph->is_valid()) {
        $ret['message'] = t('Unable to process image');
        logger('photo_upload: unable to process image');
        @unlink($src);
        call_hooks('photo_upload_end', $ret);
        return $ret;
    }
    $exif = $ph->orient($args['os_path'] ? $args['os_path'] : $src);
    @unlink($src);
    $max_length = get_config('system', 'max_image_length');
    if (!$max_length) {
        $max_length = MAX_IMAGE_LENGTH;
    }
    if ($max_length > 0) {
        $ph->scaleImage($max_length);
    }
    $width = $ph->getWidth();
    $height = $ph->getHeight();
    $smallest = 0;
    $photo_hash = $args['resource_id'] ? $args['resource_id'] : photo_new_resource();
    $visitor = '';
    if ($channel['channel_hash'] !== $observer['xchan_hash']) {
        $visitor = $observer['xchan_hash'];
    }
    $errors = false;
    $p = array('aid' => $account_id, 'uid' => $channel_id, 'xchan' => $visitor, 'resource_id' => $photo_hash, 'filename' => $filename, 'album' => $album, 'imgscale' => 0, 'photo_usage' => PHOTO_NORMAL, 'allow_cid' => $ac['allow_cid'], 'allow_gid' => $ac['allow_gid'], 'deny_cid' => $ac['deny_cid'], 'deny_gid' => $ac['deny_gid'], 'os_storage' => $os_storage, 'os_path' => $args['os_path']);
    if ($args['created']) {
        $p['created'] = $args['created'];
    }
    if ($args['edited']) {
        $p['edited'] = $args['edited'];
    }
    if ($args['title']) {
        $p['title'] = $args['title'];
    }
    if ($args['description']) {
        $p['description'] = $args['description'];
    }
    $link = array();
    $r0 = $ph->save($p);
    $link[0] = array('rel' => 'alternate', 'type' => 'text/html', 'href' => z_root() . '/photo/' . $photo_hash . '-0.' . $ph->getExt(), 'width' => $ph->getWidth(), 'height' => $ph->getHeight());
    if (!$r0) {
        $errors = true;
    }
    unset($p['os_storage']);
    unset($p['os_path']);
    if (($width > 1024 || $height > 1024) && !$errors) {
        $ph->scaleImage(1024);
    }
    $p['imgscale'] = 1;
    $r1 = $ph->save($p);
    $link[1] = array('rel' => 'alternate', 'type' => 'text/html', 'href' => z_root() . '/photo/' . $photo_hash . '-1.' . $ph->getExt(), 'width' => $ph->getWidth(), 'height' => $ph->getHeight());
    if (!$r1) {
        $errors = true;
    }
    if (($width > 640 || $height > 640) && !$errors) {
        $ph->scaleImage(640);
    }
    $p['imgscale'] = 2;
    $r2 = $ph->save($p);
    $link[2] = array('rel' => 'alternate', 'type' => 'text/html', 'href' => z_root() . '/photo/' . $photo_hash . '-2.' . $ph->getExt(), 'width' => $ph->getWidth(), 'height' => $ph->getHeight());
    if (!$r2) {
        $errors = true;
    }
    if (($width > 320 || $height > 320) && !$errors) {
        $ph->scaleImage(320);
    }
    $p['imgscale'] = 3;
    $r3 = $ph->save($p);
    $link[3] = array('rel' => 'alternate', 'type' => 'text/html', 'href' => z_root() . '/photo/' . $photo_hash . '-3.' . $ph->getExt(), 'width' => $ph->getWidth(), 'height' => $ph->getHeight());
    if (!$r3) {
        $errors = true;
    }
    if ($errors) {
        q("delete from photo where resource_id = '%s' and uid = %d", dbesc($photo_hash), intval($channel_id));
        $ret['message'] = t('Photo storage failed.');
        logger('photo_upload: photo store failed.');
        call_hooks('photo_upload_end', $ret);
        return $ret;
    }
    $item_hidden = $visible ? 0 : 1;
    $lat = $lon = null;
    if ($exif && $exif['GPS']) {
        if (feature_enabled($channel_id, 'photo_location')) {
            $lat = getGps($exif['GPS']['GPSLatitude'], $exif['GPS']['GPSLatitudeRef']);
            $lon = getGps($exif['GPS']['GPSLongitude'], $exif['GPS']['GPSLongitudeRef']);
        }
    }
    $title = $args['description'] ? $args['description'] : $args['filename'];
    $large_photos = feature_enabled($channel['channel_id'], 'large_photos');
    linkify_tags($a, $args['body'], $channel_id);
    if ($large_photos) {
        $scale = 1;
        $width = $link[1]['width'];
        $height = $link[1]['height'];
        $tag = $r1 ? '[zmg=' . $width . 'x' . $height . ']' : '[zmg]';
    } else {
        $scale = 2;
        $width = $link[2]['width'];
        $height = $link[2]['height'];
        $tag = $r2 ? '[zmg=' . $width . 'x' . $height . ']' : '[zmg]';
    }
    $author_link = '[zrl=' . z_root() . '/channel/' . $channel['channel_address'] . ']' . $channel['channel_name'] . '[/zrl]';
    $photo_link = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash . ']' . t('a new photo') . '[/zrl]';
    $album_link = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/album/' . bin2hex($album) . ']' . (strlen($album) ? $album : '/') . '[/zrl]';
    $activity_format = sprintf(t('%1$s posted %2$s to %3$s', 'photo_upload'), $author_link, $photo_link, $album_link);
    $summary = ($args['body'] ? $args['body'] : '') . '[footer]' . $activity_format . '[/footer]';
    $obj_body = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash . ']' . $tag . z_root() . "/photo/{$photo_hash}-{$scale}." . $ph->getExt() . '[/zmg]' . '[/zrl]';
    // Create item object
    $object = array('type' => ACTIVITY_OBJ_PHOTO, 'title' => $title, 'created' => $p['created'], 'edited' => $p['edited'], 'id' => z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash, 'link' => $link, 'body' => $obj_body);
    $target = array('type' => ACTIVITY_OBJ_ALBUM, 'title' => $album ? $album : '/', 'id' => z_root() . '/photos/' . $channel['channel_address'] . '/album/' . bin2hex($album));
    // Create item container
    if ($args['item']) {
        foreach ($args['item'] as $i) {
            $item = get_item_elements($i);
            $force = false;
            if ($item['mid'] === $item['parent_mid']) {
                $item['body'] = $summary;
                $item['obj_type'] = ACTIVITY_OBJ_PHOTO;
                $item['obj'] = json_encode($object);
                $item['tgt_type'] = ACTIVITY_OBJ_ALBUM;
                $item['target'] = json_encode($target);
                if ($item['author_xchan'] === $channel['channel_hash']) {
                    $item['sig'] = base64url_encode(rsa_sign($item['body'], $channel['channel_prvkey']));
                    $item['item_verified'] = 1;
                } else {
                    $item['sig'] = '';
                }
                $force = true;
            }
            $r = q("select id, edited from item where mid = '%s' and uid = %d limit 1", dbesc($item['mid']), intval($channel['channel_id']));
            if ($r) {
                if ($item['edited'] > $r[0]['edited'] || $force) {
                    $item['id'] = $r[0]['id'];
                    $item['uid'] = $channel['channel_id'];
                    item_store_update($item, false, $deliver);
                    continue;
                }
            } else {
                $item['aid'] = $channel['channel_account_id'];
                $item['uid'] = $channel['channel_id'];
                $item_result = item_store($item, false, $deliver);
            }
        }
    } else {
        $mid = item_message_id();
        $arr = array();
        if ($lat && $lon) {
            $arr['coord'] = $lat . ' ' . $lon;
        }
        $arr['aid'] = $account_id;
        $arr['uid'] = $channel_id;
        $arr['mid'] = $mid;
        $arr['parent_mid'] = $mid;
        $arr['item_hidden'] = $item_hidden;
        $arr['resource_type'] = 'photo';
        $arr['resource_id'] = $photo_hash;
        $arr['owner_xchan'] = $channel['channel_hash'];
        $arr['author_xchan'] = $observer['xchan_hash'];
        $arr['title'] = $title;
        $arr['allow_cid'] = $ac['allow_cid'];
        $arr['allow_gid'] = $ac['allow_gid'];
        $arr['deny_cid'] = $ac['deny_cid'];
        $arr['deny_gid'] = $ac['deny_gid'];
        $arr['verb'] = ACTIVITY_POST;
        $arr['obj_type'] = ACTIVITY_OBJ_PHOTO;
        $arr['obj'] = json_encode($object);
        $arr['tgt_type'] = ACTIVITY_OBJ_ALBUM;
        $arr['target'] = json_encode($target);
        $arr['item_wall'] = 1;
        $arr['item_origin'] = 1;
        $arr['item_thread_top'] = 1;
        $arr['item_private'] = intval($acl->is_private());
        $arr['plink'] = z_root() . '/channel/' . $channel['channel_address'] . '/?f=&mid=' . $arr['mid'];
        $arr['body'] = $summary;
        // this one is tricky because the item and the photo have the same permissions, those of the photo.
        // Use the channel read_stream permissions to get the correct public_policy for the item and recalculate the
        // private flag accordingly. This may cause subtle bugs due to custom permissions roles. We want to use
        // public policy when federating items to other sites, but should probably ignore them when accessing the item
        // in the photos pages - using the photos permissions instead. We need the public policy to keep the photo
        // linked item from leaking into the feed when somebody has a channel with read_stream restrictions.
        $arr['public_policy'] = map_scope($channel['channel_r_stream'], true);
        if ($arr['public_policy']) {
            $arr['item_private'] = 1;
        }
        $result = item_store($arr, false, $deliver);
        $item_id = $result['item_id'];
        if ($visible && $deliver) {
            Zotlabs\Daemon\Master::Summon(array('Notifier', 'wall-new', $item_id));
        }
    }
    $ret['success'] = true;
    $ret['item'] = $arr;
    $ret['body'] = $obj_body;
    $ret['resource_id'] = $photo_hash;
    $ret['photoitem_id'] = $item_id;
    call_hooks('photo_upload_end', $ret);
    return $ret;
}
Esempio n. 9
0
function import_items($channel, $items, $sync = false)
{
    if ($channel && $items) {
        $allow_code = false;
        $r = q("select account_id, account_roles, channel_pageflags from account left join channel on channel_account_id = account_id \n\t\t\twhere channel_id = %d limit 1", intval($channel['channel_id']));
        if ($r) {
            if ($r[0]['account_roles'] & ACCOUNT_ROLE_ALLOWCODE || $r[0]['channel_pageflags'] & PAGE_ALLOWCODE) {
                $allow_code = true;
            }
        }
        $deliver = false;
        // Don't deliver any messages or notifications when importing
        foreach ($items as $i) {
            $item_result = false;
            $item = get_item_elements($i, $allow_code);
            if (!$item) {
                continue;
            }
            $r = q("select id, edited from item where mid = '%s' and uid = %d limit 1", dbesc($item['mid']), intval($channel['channel_id']));
            if ($r) {
                if ($item['edited'] > $r[0]['edited']) {
                    $item['id'] = $r[0]['id'];
                    $item['uid'] = $channel['channel_id'];
                    $item_result = item_store_update($item, $allow_code, $deliver);
                    if ($sync && $item['item_wall']) {
                        // deliver singletons if we have any
                        if ($item_result && $item_result['success']) {
                            Zotlabs\Daemon\Master::Summon(array('Notifier', 'single_activity', $item_result['item_id']));
                        }
                    }
                    continue;
                }
            } else {
                $item['aid'] = $channel['channel_account_id'];
                $item['uid'] = $channel['channel_id'];
                $item_result = item_store($item, $allow_code, $deliver);
            }
            if ($sync && $item['item_wall']) {
                // deliver singletons if we have any
                if ($item_result && $item_result['success']) {
                    Zotlabs\Daemon\Master::Summon(array('Notifier', 'single_activity', $item_result['item_id']));
                }
            }
        }
    }
}
Esempio n. 10
0
/**
 * @brief
 *
 * @param array $channel
 * @param array $observer
 * @param array $args
 * @return array
 */
function photo_upload($channel, $observer, $args)
{
    $ret = array('success' => false);
    $channel_id = $channel['channel_id'];
    $account_id = $channel['channel_account_id'];
    if (!perm_is_allowed($channel_id, $observer['xchan_hash'], 'write_storage')) {
        $ret['message'] = t('Permission denied.');
        return $ret;
    }
    //	call_hooks('photo_upload_begin', $args);
    /*
     * Determine the album to use
     */
    $album = $args['album'];
    if (intval($args['visible']) || $args['visible'] === 'true') {
        $visible = 1;
    } else {
        $visible = 0;
    }
    // Set to default channel permissions. If the parent directory (album) has permissions set,
    // use those instead. If we have specific permissions supplied, they take precedence over
    // all other settings. 'allow_cid' being passed from an external source takes priority over channel settings.
    // ...messy... needs re-factoring once the photos/files integration stabilises
    $acl = new AccessList($channel);
    if (array_key_exists('directory', $args) && $args['directory']) {
        $acl->set($args['directory']);
    }
    if (array_key_exists('allow_cid', $args)) {
        $acl->set($args);
    }
    if (array_key_exists('group_allow', $args) || array_key_exists('contact_allow', $args) || array_key_exists('group_deny', $args) || array_key_exists('contact_deny', $args)) {
        $acl->set_from_array($args);
    }
    $ac = $acl->get();
    $os_storage = 0;
    if ($args['os_path'] && $args['getimagesize']) {
        $imagedata = @file_get_contents($args['os_path']);
        $filename = $args['filename'];
        $filesize = strlen($imagedata);
        // this is going to be deleted if it exists
        $src = '/tmp/deletemenow';
        $type = $args['getimagesize']['mime'];
        $os_storage = 1;
    } elseif ($args['data']) {
        // allow an import from a binary string representing the image.
        // This bypasses the upload step and max size limit checking
        $imagedata = $args['data'];
        $filename = $args['filename'];
        $filesize = strlen($imagedata);
        // this is going to be deleted if it exists
        $src = '/tmp/deletemenow';
        $type = $args['type'];
    } else {
        $f = array('src' => '', 'filename' => '', 'filesize' => 0, 'type' => '');
        //		call_hooks('photo_upload_file',$f);
        if (x($f, 'src') && x($f, 'filesize')) {
            $src = $f['src'];
            $filename = $f['filename'];
            $filesize = $f['filesize'];
            $type = $f['type'];
        } else {
            $src = $_FILES['userfile']['tmp_name'];
            $filename = basename($_FILES['userfile']['name']);
            $filesize = intval($_FILES['userfile']['size']);
            $type = $_FILES['userfile']['type'];
        }
        if (!$type) {
            $type = guess_image_type($filename);
        }
        logger('photo_upload: received file: ' . $filename . ' as ' . $src . ' (' . $type . ') ' . $filesize . ' bytes', LOGGER_DEBUG);
        $maximagesize = get_config('system', 'maximagesize');
        if ($maximagesize && $filesize > $maximagesize) {
            $ret['message'] = sprintf(t('Image exceeds website size limit of %lu bytes'), $maximagesize);
            @unlink($src);
            call_hooks('photo_upload_end', $ret);
            return $ret;
        }
        if (!$filesize) {
            $ret['message'] = t('Image file is empty.');
            @unlink($src);
            call_hooks('photo_post_end', $ret);
            return $ret;
        }
        logger('photo_upload: loading the contents of ' . $src, LOGGER_DEBUG);
        $imagedata = @file_get_contents($src);
    }
    $r = q("select sum(size) as total from photo where aid = %d and scale = 0 ", intval($account_id));
    $limit = service_class_fetch($channel_id, 'photo_upload_limit');
    if ($r && $limit !== false && $r[0]['total'] + strlen($imagedata) > $limit) {
        $ret['message'] = upgrade_message();
        @unlink($src);
        call_hooks('photo_post_end', $ret);
        return $ret;
    }
    $ph = photo_factory($imagedata, $type);
    if (!$ph->is_valid()) {
        $ret['message'] = t('Unable to process image');
        logger('photo_upload: unable to process image');
        @unlink($src);
        call_hooks('photo_upload_end', $ret);
        return $ret;
    }
    $exif = $ph->orient($args['os_path'] ? $args['os_path'] : $src);
    @unlink($src);
    $max_length = get_config('system', 'max_image_length');
    if (!$max_length) {
        $max_length = MAX_IMAGE_LENGTH;
    }
    if ($max_length > 0) {
        $ph->scaleImage($max_length);
    }
    $width = $ph->getWidth();
    $height = $ph->getHeight();
    $smallest = 0;
    $photo_hash = $args['resource_id'] ? $args['resource_id'] : photo_new_resource();
    $visitor = '';
    if ($channel['channel_hash'] !== $observer['xchan_hash']) {
        $visitor = $observer['xchan_hash'];
    }
    $errors = false;
    $p = array('aid' => $account_id, 'uid' => $channel_id, 'xchan' => $visitor, 'resource_id' => $photo_hash, 'filename' => $filename, 'album' => $album, 'scale' => 0, 'photo_usage' => PHOTO_NORMAL, 'allow_cid' => $ac['allow_cid'], 'allow_gid' => $ac['allow_gid'], 'deny_cid' => $ac['deny_cid'], 'deny_gid' => $ac['deny_gid'], 'os_storage' => $os_storage, 'os_path' => $args['os_path']);
    if ($args['created']) {
        $p['created'] = $args['created'];
    }
    if ($args['edited']) {
        $p['edited'] = $args['edited'];
    }
    if ($args['title']) {
        $p['title'] = $args['title'];
    }
    if ($args['description']) {
        $p['description'] = $args['description'];
    }
    $r1 = $ph->save($p);
    if (!$r1) {
        $errors = true;
    }
    unset($p['os_storage']);
    unset($p['os_path']);
    if (($width > 640 || $height > 640) && !$errors) {
        $ph->scaleImage(640);
        $p['scale'] = 1;
        $r2 = $ph->save($p);
        $smallest = 1;
        if (!$r2) {
            $errors = true;
        }
    }
    if (($width > 320 || $height > 320) && !$errors) {
        $ph->scaleImage(320);
        $p['scale'] = 2;
        $r3 = $ph->save($p);
        $smallest = 2;
        if (!$r3) {
            $errors = true;
        }
    }
    if ($errors) {
        q("delete from photo where resource_id = '%s' and uid = %d", dbesc($photo_hash), intval($channel_id));
        $ret['message'] = t('Photo storage failed.');
        logger('photo_upload: photo store failed.');
        call_hooks('photo_upload_end', $ret);
        return $ret;
    }
    // This will be the width and height of the smallest representation
    $width_x_height = $ph->getWidth() . 'x' . $ph->getHeight();
    // Create item container
    $item_hidden = $visible ? 0 : 1;
    $lat = $lon = null;
    if ($exif && $exif['GPS']) {
        if (feature_enabled($channel_id, 'photo_location')) {
            $lat = getGps($exif['GPS']['GPSLatitude'], $exif['GPS']['GPSLatitudeRef']);
            $lon = getGps($exif['GPS']['GPSLongitude'], $exif['GPS']['GPSLongitudeRef']);
        }
    }
    if ($args['item']) {
        foreach ($args['item'] as $i) {
            $item = get_item_elements($i);
            $force = false;
            if ($item['mid'] === $item['parent_mid']) {
                $item['body'] = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash . ']' . $tag . z_root() . "/photo/{$photo_hash}-{$smallest}." . $ph->getExt() . '[/zmg]' . '[/zrl]';
                if ($item['author_xchan'] === $channel['channel_hash']) {
                    $item['sig'] = base64url_encode(rsa_sign($item['body'], $channel['channel_prvkey']));
                    $item['item_verified'] = 1;
                } else {
                    $item['sig'] = '';
                }
                $force = true;
            }
            $r = q("select id, edited from item where mid = '%s' and uid = %d limit 1", dbesc($item['mid']), intval($channel['channel_id']));
            if ($r) {
                if ($item['edited'] > $r[0]['edited'] || $force) {
                    $item['id'] = $r[0]['id'];
                    $item['uid'] = $channel['channel_id'];
                    item_store_update($item);
                    continue;
                }
            } else {
                $item['aid'] = $channel['channel_account_id'];
                $item['uid'] = $channel['channel_id'];
                $item_result = item_store($item);
            }
        }
    } else {
        $title = '';
        $mid = item_message_id();
        $arr = array();
        if ($lat && $lon) {
            $arr['coord'] = $lat . ' ' . $lon;
        }
        $arr['aid'] = $account_id;
        $arr['uid'] = $channel_id;
        $arr['mid'] = $mid;
        $arr['parent_mid'] = $mid;
        $arr['item_hidden'] = $item_hidden;
        $arr['resource_type'] = 'photo';
        $arr['resource_id'] = $photo_hash;
        $arr['owner_xchan'] = $channel['channel_hash'];
        $arr['author_xchan'] = $observer['xchan_hash'];
        $arr['title'] = $title;
        $arr['allow_cid'] = $ac['allow_cid'];
        $arr['allow_gid'] = $ac['allow_gid'];
        $arr['deny_cid'] = $ac['deny_cid'];
        $arr['deny_gid'] = $ac['deny_gid'];
        $arr['verb'] = ACTIVITY_POST;
        $arr['item_wall'] = 1;
        $arr['item_origin'] = 1;
        $arr['item_thread_top'] = 1;
        $arr['item_private'] = intval($acl->is_private());
        $arr['plink'] = z_root() . '/channel/' . $channel['channel_address'] . '/?f=&mid=' . $arr['mid'];
        // We should also put a width_x_height on large photos. Left as an exercise for
        // devs looking for simple stuff to fix.
        $larger = feature_enabled($channel['channel_id'], 'large_photos');
        if ($larger) {
            $tag = '[zmg]';
            if ($r2) {
                $smallest = 1;
            } else {
                $smallest = 0;
            }
        } else {
            if ($width_x_height) {
                $tag = '[zmg=' . $width_x_height . ']';
            } else {
                $tag = '[zmg]';
            }
        }
        $arr['body'] = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash . ']' . $tag . z_root() . "/photo/{$photo_hash}-{$smallest}." . $ph->getExt() . '[/zmg]' . '[/zrl]';
        $result = item_store($arr);
        $item_id = $result['item_id'];
        if ($visible) {
            proc_run('php', "include/notifier.php", 'wall-new', $item_id);
        }
    }
    $ret['success'] = true;
    $ret['item'] = $arr;
    $ret['body'] = $arr['body'];
    $ret['resource_id'] = $photo_hash;
    $ret['photoitem_id'] = $item_id;
    call_hooks('photo_upload_end', $ret);
    return $ret;
}
Esempio n. 11
0
function onepoll_run($argv, $argc)
{
    cli_startup();
    $a = get_app();
    logger('onepoll: start');
    $manual_id = 0;
    $generation = 0;
    $force = false;
    $restart = false;
    if ($argc > 1 && intval($argv[1])) {
        $contact_id = intval($argv[1]);
    }
    if (!$contact_id) {
        logger('onepoll: no contact');
        return;
    }
    $d = datetime_convert();
    $contacts = q("SELECT abook.*, xchan.*, account.*\n\t\tFROM abook LEFT JOIN account on abook_account = account_id left join xchan on xchan_hash = abook_xchan \n\t\twhere abook_id = %d\n\t\tAND (( abook_flags & %d ) OR ( abook_flags = %d ))\n\t\tAND NOT ( abook_flags & %d )\n\t\tAND (( account_flags = %d ) OR ( account_flags = %d )) limit 1", intval($contact_id), intval(ABOOK_FLAG_HIDDEN | ABOOK_FLAG_PENDING | ABOOK_FLAG_UNCONNECTED | ABOOK_FLAG_FEED), intval(0), intval(ABOOK_FLAG_ARCHIVED | ABOOK_FLAG_BLOCKED | ABOOK_FLAG_IGNORED), intval(ACCOUNT_OK), intval(ACCOUNT_UNVERIFIED));
    if (!$contacts) {
        logger('onepoll: abook_id not found: ' . $contact_id);
        return;
    }
    $contact = $contacts[0];
    $t = $contact['abook_updated'];
    $importer_uid = $contact['abook_channel'];
    $r = q("SELECT * from channel left join xchan on channel_hash = xchan_hash where channel_id = %d limit 1", intval($importer_uid));
    if (!$r) {
        return;
    }
    $importer = $r[0];
    logger("onepoll: poll: ({$contact['id']}) IMPORTER: {$importer['xchan_name']}, CONTACT: {$contact['xchan_name']}");
    $last_update = $contact['abook_updated'] === $contact['abook_created'] || $contact['abook_updated'] === NULL_DATE ? datetime_convert('UTC', 'UTC', 'now - 7 days') : datetime_convert('UTC', 'UTC', $contact['abook_updated'] . ' - 2 days');
    if ($contact['xchan_network'] === 'rss') {
        logger('onepoll: processing feed ' . $contact['xchan_name'], LOGGER_DEBUG);
        handle_feed($importer['channel_id'], $contact_id, $contact['xchan_hash']);
        q("update abook set abook_connected = '%s' where abook_id = %d limit 1", dbesc(datetime_convert()), intval($contact['abook_id']));
        return;
    }
    if ($contact['xchan_network'] !== 'zot') {
        return;
    }
    // update permissions
    $x = zot_refresh($contact, $importer);
    $responded = false;
    $updated = datetime_convert();
    if (!$x) {
        // mark for death by not updating abook_connected, this is caught in include/poller.php
        q("update abook set abook_updated = '%s' where abook_id = %d limit 1", dbesc($updated), intval($contact['abook_id']));
    } else {
        q("update abook set abook_updated = '%s', abook_connected = '%s' where abook_id = %d limit 1", dbesc($updated), dbesc($updated), intval($contact['abook_id']));
        $responded = true;
    }
    if (!$responded) {
        return;
    }
    if ($contact['xchan_connurl']) {
        $fetch_feed = true;
        $x = null;
        if (!($contact['abook_their_perms'] & PERMS_R_STREAM)) {
            $fetch_feed = false;
        }
        if ($fetch_feed) {
            $feedurl = str_replace('/poco/', '/zotfeed/', $contact['xchan_connurl']);
            $x = z_fetch_url($feedurl . '?f=&mindate=' . urlencode($last_update));
            logger('feed_update: ' . print_r($x, true), LOGGER_DATA);
        }
        if ($x && $x['success']) {
            $total = 0;
            logger('onepoll: feed update ' . $contact['xchan_name']);
            $j = json_decode($x['body'], true);
            if ($j['success'] && $j['messages']) {
                foreach ($j['messages'] as $message) {
                    $results = process_delivery(array('hash' => $contact['xchan_hash']), get_item_elements($message), array(array('hash' => $importer['xchan_hash'])), false);
                    logger('onepoll: feed_update: process_delivery: ' . print_r($results, true));
                    $total++;
                }
                logger("onepoll: {$total} messages processed");
            }
        }
    }
    // fetch some items
    // set last updated timestamp
    if ($contact['xchan_connurl']) {
        $r = q("SELECT xlink_id from xlink \n\t\t\twhere xlink_xchan = '%s' and xlink_updated > UTC_TIMESTAMP() - INTERVAL 1 DAY limit 1", intval($contact['xchan_hash']));
        if (!$r) {
            poco_load($contact['xchan_hash'], $contact['xchan_connurl']);
        }
    }
    return;
}