function api_dispatch($method) { if (!$GLOBALS['cfg']['enable_feature_api']) { api_output_error(999, 'API disabled'); } $method = filter_strict($method); $enc_method = htmlspecialchars($method); $methods = $GLOBALS['cfg']['api']['methods']; if (!$method || !isset($methods[$method])) { api_output_error(404, "Method '{$enc_method}' not found"); } $method_row = $methods[$method]; if (!$method_row['enabled']) { api_output_error(404, "Method '{$enc_method}' not found"); } $method_row['name'] = $method; # TO DO: check API keys here # TO DO: actually check auth here (whatever that means...) if ($method_row['requires_auth']) { api_auth_ensure_auth($method_row); } if ($method_row['requires_crumb']) { api_auth_ensure_crumb($method_row); } loadlib($method_row['library']); $parts = explode(".", $method); $method = array_pop($parts); $func = "{$method_row['library']}_{$method}"; call_user_func($func); exit; }
function api_dispatch() { # # Output formats # $format = request_str('format'); if ($format = request_str('format')) { if (in_array($format, $GLOBALS['cfg']['api']['formats']['valid'])) { $GLOBALS['cfg']['api']['formats']['current'] = $format; } else { $format = null; } } if (!$format) { $GLOBALS['cfg']['api']['formats']['current'] = $GLOBALS['cfg']['api']['formats']['default']; } # # Can I get a witness? # if (!$GLOBALS['cfg']['enable_feature_api']) { api_output_error(999, 'The API is currently disabled'); } # # Is this a valid method? # $method = request_str('method'); if (!$method) { api_output_error(404, 'Method not found'); } if (!isset($GLOBALS['cfg']['api']['methods'][$method])) { api_output_error(404, 'Method not found'); } $method_row = $GLOBALS['cfg']['api']['methods'][$method]; if (!$method_row['enabled']) { api_output_error(404, 'Method not found'); } $lib = $method_row['library']; loadlib($lib); $method = explode(".", $method); $function = $lib . "_" . array_pop($method); if (!function_exists($function)) { api_output_error(404, 'Method not found'); } # # Auth-y bits # if ($method_row['required_login']) { # Please, to write me... } # # Go! # call_user_func($function); exit; }
function storage_s3_file_store($object_id, $data, $more = array()) { if ($more['type']) { $type = $more['type']; } else { loadlib('mime_type'); $type = mime_type_identify($object_id); } $put = s3_put(storage_s3_bucket(), array('id' => $object_id, 'acl' => 'public-read', 'content_type' => $type, 'data' => $data, 'meta' => array('date-synced' => time()))); return $put; }
function flickr_push_subscriptions_is_push_backup(&$subscription) { loadlib("flickr_backups"); $owner = users_get_by_id($subscription['user_id']); if (!flickr_backups_is_registered_user($owner)) { return 0; } if (!flickr_backups_is_registered_subscription($subscription)) { return 0; } return 1; }
function api_privatesquare_venues_checkin() { $venue_id = post_str("venue_id"); $status_id = post_int32("status_id"); if (!$venue_id) { api_output_error(999, "Missing venue ID"); } if (!isset($status_id)) { api_output_error(999, "Missing status ID"); } $fsq_user = foursquare_users_get_by_user_id($GLOBALS['cfg']['user']['id']); $checkin = array('user_id' => $GLOBALS['cfg']['user']['id'], 'venue_id' => $venue_id, 'status_id' => $status_id); # where am I? $venue = foursquare_venues_get_by_venue_id($venue_id); if (!$venue) { $rsp = foursquare_venues_archive_venue($venue_id); if ($rsp['ok']) { $venue = $rsp['venue']; } } if ($venue) { $checkin['locality'] = $venue['locality']; $checkin['latitude'] = $venue['latitude']; $checkin['longitude'] = $venue['longitude']; } # check to see if we're checking in to 4sq too if ($broadcast = post_str("broadcast")) { $method = 'checkins/add'; $args = array('oauth_token' => $fsq_user['oauth_token'], 'venueId' => $venue_id, 'broadcast' => $broadcast); $more = array('method' => 'POST'); $rsp = foursquare_api_call($method, $args, $more); if ($rsp['ok']) { $checkin['checkin_id'] = $rsp['rsp']['checkin']['id']; } # on error, then what? } if ($GLOBALS['cfg']['enable_feature_weather_tracking']) { loadlib("weather_google"); $rsp = weather_google_conditions($checkin['latitude'], $checkin['longitude']); if ($rsp['ok']) { $conditions = $rsp['conditions']; $conditions['source'] = $rsp['source']; $checkin['weather'] = json_encode($conditions); } } $rsp = privatesquare_checkins_create($checkin); if (!$rsp['ok']) { api_output_error(999, "Check in failed"); } $out = array('checkin' => $rsp['checkin']); api_output_ok($out); }
function _cache_do_remote($method, $key, $data = null) { $engine = trim($GLOBALS['cfg']['cache_remote_engine']); if (!$engine) { return array('ok' => 0, 'error' => 'Remote caching is not enabled'); } $remote_lib = "cache_{$engine}"; $remote_func = "cache_{$engine}_{$method}"; $args = $data ? array($key, $data) : array($key); loadlib($remote_lib); $rsp = call_user_func_array($remote_func, $args); $rsp['cache_key'] = $key; $rsp['cache'] = $engine; return $rsp; }
function flickr_photos_update_photo(&$photo, $update) { $cache_key = "photo_{$photo['id']}"; # $lookup = flickr_photos_lookup_photo($photo['id']); if (!$lookup) { return; } $user = users_get_by_id($lookup['user_id']); $cluster_id = $user['cluster_id']; $enc_id = AddSlashes($photo['id']); $where = "id={$enc_id}"; # see also: git:parallel-flickr/solr/conf/schema.xml $solr_fields = array('perms', 'geoperms', 'geocontext', 'media', 'latitude', 'longitude', 'accuracy', 'woeid', 'datetaken', 'dateupload', 'title', 'description'); $solr_update = 0; $hash = array(); foreach ($update as $k => $v) { $hash[$k] = AddSlashes($v); if (in_array($k, $solr_fields)) { $solr_update++; } } $rsp = db_update_users($cluster_id, 'FlickrPhotos', $hash, $where); if (!$rsp['ok']) { return $rsp; } cache_unset($cache_key); if ($GLOBALS['cfg']['enable_feature_solr'] && $solr_update) { $photo = flickr_photos_get_by_id($photo['id']); # This is a quick hack that may become permanent. Basically # we need to refetch the data in flickr.photos.getInfo in # order to update the solr db. Normally the _index_photo pulls # this information from disk; the files having been written # by the bin/backup_photos.php script. As I write this the www # server does not have write permissions on the static photos # directory. If it did, this whole problem would go away and in # the end that may be the simplest possible solution. Until then # we'll fetch the (meta) data directly from the API and force # feed it to the search indexer. If you're wondering: Yes, it means # that the local solr db and the actual JSON dump of photos.getInfo # will be out of sync but that will sort itself out the next # time bin/backup_photos.php is run (20111231/straup) loadlib("flickr_photos_metadata"); $meta = flickr_photos_metadata_fetch($photo, 'inflate'); flickr_photos_search_index_photo($photo, $meta); } return $rsp; }
function enplacify_uri($uri) { foreach ($GLOBALS['cfg']['enplacify'] as $service => $data) { foreach ($data['uris'] as $pattern) { if (!preg_match($pattern, $uri)) { continue; } $service_lib = "enplacify_{$service}"; $service_func = "enplacify_{$service}_uri"; loadlib($service_lib); $rsp = call_user_func_array($service_func, array($uri)); return $rsp; } } return array('ok' => 0, 'error' => 'failed to locate any valid services for URL'); }
function flickr_photos_metadata_fetch(&$photo, $inflate = 0) { loadlib("flickr_api"); loadlib("flickr_users"); $flickr_user = flickr_users_get_by_user_id($photo['user_id']); $method = 'flickr.photos.getInfo'; $args = array('photo_id' => $photo['id'], 'auth_token' => $flickr_user['auth_token']); $more = array(); if (!$inflate) { $more['raw'] = 1; } $rsp = flickr_api_call($method, $args, $more); if ($rsp['ok']) { $data = $inflate ? $rsp['rsp'] : $rsp['body']; $rsp = okay(array('data' => $data)); } return $rsp; }
function api_auth_ensure_auth(&$method, $key_row = null) { $type = $GLOBALS['cfg']['api_auth_type']; $auth_lib = "api_auth_{$type}"; $auth_func = "api_auth_{$type}_has_auth"; try { loadlib($auth_lib); } catch (Exception $e) { return 0; } if (!function_exists($auth_func)) { return 0; } $rsp = call_user_func_array($auth_func, array($method, $key_row)); if (!$rsp['ok']) { api_output_error($rsp['error_code'], $rsp['error']); } return $rsp; }
function flickr_photos_permissions_can_view_photo(&$photo, $viewer_id = 0, $more = array()) { if ($viewer_id && $photo['user_id'] == $viewer_id) { return 1; } $perms_map = flickr_photos_permissions_map(); $perms = $perms_map[$photo['perms']]; if (!$viewer_id && $perms == 'public') { return 1; } if ($perms == 'public') { return 1; } if ($contact = flickr_contacts_get_contact($photo['user_id'], $viewer_id)) { $rel_map = flickr_contacts_relationship_map(); $str_rel = $rel_map[$contact['rel']]; if ($perms == 'friends' || $perms == 'family') { return $str_rel == $perms ? 1 : 0; } if ($perms == 'friends and family') { return in_array($str_rel, array('friends', 'family')) ? 1 : 0; } } # Note: this is predicated on the assumption that the user # actually has permissions to view the photo otherwise the # backup/import code would not have downloaded the photo; the # problem is not a flickr permissions issue but due to the # fact that the photo owner is not a registered parallel-flickr # user and hence their contact list is not present. # (20120607/straup) if ($viewer_id && isset($more['allow_if_is_faved'])) { loadlib("flickr_faves"); $viewer = users_get_by_id($viewer_id); if (flickr_faves_is_faved_by_user($viewer, $photo['id'])) { return 1; } } return 0; }
function foursquare_venues_archive_venue($venue_id) { loadlib("foursquare_users"); loadlib("foursquare_api"); loadlib("reverse_geoplanet"); $fsq_user = foursquare_users_random_user(); $method = "venues/{$venue_id}"; $args = array('oauth_token' => $fsq_user['oauth_token']); $rsp = foursquare_api_call($method, $args); if (!$rsp['ok']) { return $rsp; } $data = $rsp['rsp']['venue']; $lat = $data['location']['lat']; $lon = $data['location']['lng']; $venue = array('venue_id' => $data['id'], 'name' => $data['name'], 'latitude' => $lat, 'longitude' => $lon, 'data' => json_encode($data)); # might be better/easier to geocode string place names (20120121/straup) $geo_rsp = reverse_geoplanet($lat, $lon, $GLOBALS['cfg']['reverse_geoplanet_remote_endpoint']); if ($geo_rsp['ok']) { $venue['locality'] = $geo_rsp['data']['locality']; } return foursquare_venues_add_venue($venue); }
<?php # # $Id$ # loadlib("maps"); ################################################################# function png_export_dots(&$dots, $more = array()) { $defaults = array('width' => 1024, 'height' => 768); $more = array_merge($defaults, $more); list($map, $img) = maps_image_for_dots($dots, $more); if (!$img) { return null; } imagepng($img, $more['path']); imagedestroy($img); return $more['path']; } #################################################################
<?php include "include/init.php"; loadlib("flickr_places"); loadlib("flickr_photos_places"); loadlib("flickr_photos_geo"); if (!$GLOBALS['cfg']['enable_feature_solr'] || !$GLOBALS['cfg']['enable_feature_places']) { error_disabled(); } $flickr_user = flickr_users_get_by_url(); $owner = users_get_by_id($flickr_user['user_id']); $is_own = $owner['id'] == $GLOBALS['cfg']['user']['id'] ? 1 : 0; $GLOBALS['smarty']->assign_by_ref("owner", $owner); $GLOBALS['smarty']->assign("is_own", $is_own); # $woeid = get_int32("woeid"); if (!$woeid) { error_404(); } $place = flickr_places_get_by_woeid($woeid); if (!$place) { error_404(); } $placetypes = flickr_places_valid_placetypes(); $hier = array(); # put this in _get_by_woeid? probably... foreach ($placetypes as $type) { if (isset($place[$type])) { $woeid = $place[$type]['woeid']; $parts = explode(",", $place[$type]['_content']); $name = trim($parts[0]);
function api_keys_delete(&$key, $reason = '') { loadlib("api_oauth2_access_tokens"); $rsp = api_oauth2_access_tokens_delete_for_key($key); if (!$rsp['ok']) { return $rsp; } $update = array('deleted' => time()); return api_keys_update($key, $update); }
<?php include "init_local.php"; loadlib("random"); $length = 32; echo random_string($length) . "\n"; exit;
<?php loadlib('user'); if (user_id()) { redirect('/home'); } else { redirect('/start'); }
<?php # # $Id$ # loadlib("geo_utils"); loadlib("geo_geohash"); ################################################################# function search_dots(&$args, $viewer_id = 0, $more = array()) { return _search_by($args, 'dots', $viewer_id, $more); } function search_sheets(&$args, $viewer_id = 0, $more = array()) { return _search_by($args, 'sheets', $viewer_id, $more); } ################################################################# function _search_by(&$args, $search_by, $viewer_id = 0, $more = array()) { $where_parts = _search_generate_where_parts($args); $where = array(); # # Note that order of these keys is important for database # indexes. # foreach (array('user', 'geo', 'time', 'extras') as $what) { if (isset($where_parts[$what])) { $where = array_merge($where, $where_parts[$what]); } } if (!count($where)) {
<?php $root = dirname(dirname(__FILE__)); ini_set("include_path", "{$root}/www:{$root}/www/include"); include "include/init.php"; loadlib("cli"); loadlib("api_config"); loadlib("api_spec"); $spec = array("output" => array("flag" => "o", "required" => 0, "help" => "..., default is STDOUT"), "all" => array("flag" => "a", "required" => 0, "boolean" => 1, "help" => "..."), "exclude" => array("flag" => "e", "required" => 0, "help" => "...")); $opts = cli_getopts($spec); # api_config_init(); ksort($GLOBALS['cfg']['api']['methods']); # this is a dirty hack... (20130406/straup) $tmpdir = realpath(dirname(__FILE__)) . "/api_c"; if (!is_dir($tmpdir)) { mkdir($tmpdir); } $GLOBALS['smarty']->compile_dir = $tmpdir; # $exclude = $opts['exclude'] ? explode(",", $opts['exclude']) : array(); # if ($opts['output']) { $fh = fopen($opts['output'], 'w'); } else { $fh = fopen("php://output", "w"); } $methods = array(); foreach ($GLOBALS['cfg']['api']['methods'] as $method_name => $method_details) { $include = 1; if (!$method_details['enabled']) {
<?php $root = dirname(dirname(__FILE__)); ini_set("include_path", "{$root}/www:{$root}/www/include"); set_time_limit(0); # include "include/init.php"; loadlib("flickr_backups"); $map = flickr_backups_type_map("string keys"); foreach (flickr_backups_users() as $user) { $backups = flickr_backups_for_user($user); foreach ($map as $label => $type_id) { if (isset($backups[$label])) { continue; } echo "backup", "register '{$user['username']}' for {$label} backups\n"; $rsp = flickr_backups_create($user, $type_id); } }
<?php $root = dirname(dirname(__FILE__)); ini_set("include_path", "{$root}/www:{$root}/www/include"); set_time_limit(0); # include "include/init.php"; loadlib("backfill"); loadlib("flickr_photos"); loadlib("flickr_photos_search"); if (!$GLOBALS['cfg']['enable_feature_solr']) { echo "search indexing is disabled, exiting"; exit; } function index_photo($row, $more = array()) { $photo = flickr_photos_get_by_id($row['id']); $rsp = flickr_photos_search_index_photo($photo); } $sql = "SELECT * FROM FlickrPhotos"; backfill_db_users($sql, 'index_photo'); exit;
<?php include "include/init.php"; loadlib("flickr_api"); $redir = get_str('redir') ? get_str('redir') : '/'; # Some basic sanity checking like are you already logged in? if ($GLOBALS['cfg']['user']['id']) { header("location: {$redir}"); exit; } if (!$GLOBALS['cfg']['enable_feature_signin']) { $GLOBALS['smarty']->display("page_signin_disabled.txt"); exit; } # Build a URL with the perms for the auth token we're requesting # and send the user there. Rocket science, I know... $extra = array(); if ($redir = get_str('redir')) { $extra['redir'] = $redir; } $perms = $GLOBALS['cfg']['flickr_api_perms']; $url = flickr_api_auth_url($perms, $extra); header("location: {$url}"); exit;
<?php loadlib('blog_comment'); # Save the new comment and update the template if ('POST' == $_SERVER['REQUEST_METHOD'] && 'qwerty' == $_POST['test']) { blog_comment_save($_POST['name'], $_POST['website'], $_POST['text']); unset($_POST['test']); unset($_POST['name']); unset($_POST['website']); unset($_POST['text']); } # Check the hash if this is supposed to be a post preview if (4 != sizeof($URL_PARTS)) { $hash = array_pop($URL_PARTS); $post = implode('/', $URL_PARTS); if ($hash != sha1_file(dirname(__FILE__) . "/../posts/{$post}")) { display('404'); } $post .= ".preview.{$FORMAT}"; assign('preview', true); } else { $post = implode('/', $URL_PARTS) . ".{$FORMAT}"; } if (file_exists("{$smarty->template_dir}/.posts/{$post}")) { assign('page', true); display(".posts/{$post}"); } else { display('404'); }
<?php # # $Id$ # ################################################################# loadlib("geo_geocode"); loadlib("geo_geohash"); loadlib("geo_utils"); ################################################################# function dots_derive_derived_from_map($string_keys = 0) { if (!$string_keys) { return $GLOBALS['cfg']['dots_derived_from']; } return array_flip($GLOBALS['cfg']['dots_derived_from']); } ################################################################# # # Note we are not passing by ref # function dots_derive_location_data($data) { $derived = array('ok' => 1); $rsp = array('ok' => 0); # if (is_numeric($data['latitude']) && is_numeric($data['longitude'])) { # pass } else { if (isset($data['geohash'])) { $rsp = dots_derive_location_from_geohash($data);
<?php include "include/init.php"; loadlib("privatesquare_checkins"); loadlib("privatesquare_checkins_utils"); loadlib("privatesquare_export"); loadlib("foursquare_users"); $fsq_id = get_int32("foursquare_id"); if (!$fsq_id) { error_404(); } $history_url = "user/{$fsq_id}/history/"; login_ensure_loggedin($history_url); $fsq_user = foursquare_users_get_by_foursquare_id($fsq_id); if (!$fsq_user) { error_404(); } $owner = users_get_by_id($fsq_user['user_id']); $is_own = $owner['id'] == $GLOBALS['cfg']['user']['id'] ? 1 : 0; # for now... if (!$is_own) { error_403(); } $more = array(); if ($page = get_int32("page")) { $more['page'] = $page; } if ($when = get_str("when")) { $more['when'] = $when; $history_url .= urlencode($when) . "/"; # TO DO: find some better heuristic for this number
loadlib('error'); loadlib('sanitize'); loadlib('filter'); loadlib('db'); loadlib('dbtickets'); #loadlib('cache'); loadlib('crypto'); loadlib('crumb'); loadlib('login'); loadlib('email'); loadlib('utf8'); #loadlib('args'); #loadlib('calendar'); loadlib('users'); #loadlib('versions'); loadlib('http'); if ($GLOBALS['cfg']['site_disabled'] && !$this_is_shell) { header("HTTP/1.1 503 Service Temporarily Unavailable"); header("Status: 503 Service Temporarily Unavailable"); if ($retry = intval($GLOBALS['cfg']['site_disabled_retry_after'])) { header("Retry-After: {$retry}"); } $smarty->display("page_site_disabled.txt"); exit; } # # general utility functions # function dumper($foo) { echo "<pre style=\"text-align: left;\">";
<?php ################################################################# loadlib("solr"); loadlib("solr_utils"); loadlib("solr_dates"); loadlib("solr_machinetags"); loadlib("flickr_photos_permissions"); loadlib("flickr_geo_permissions"); loadlib("flickr_photos_metadata"); loadlib("flickr_places"); loadlib("flickr_photos_exif"); loadlib("exif_tools"); ################################################################# function flickr_photos_search(&$query, $more = array()) { if (!$GLOBALS['cfg']['enable_feature_solr']) { return not_okay('search indexing is disabled'); } # OMGWTF: When sorting by date_taken|posted the results # are basically anything but sorted. It's unclear to me # whether this is a known Lucene thing or ... what? I # suppose it might make sense to store dates as INTs but # then we lose the ability to do date facteing, for calendar # pages sometime in the future. So for now we'll just sort # by photo ID since it accomplishes the same thing... # (20111121/straup) # # see also: http://phatness.com/2009/11/sorting-by-date-with-solr/ $defaults = array('viewer_id' => 0, 'sort' => 'id desc'); $more = array_merge($defaults, $more);
<?php # # $Id$ # include "include/init.php"; loadlib("formats"); loadlib("import"); loadlib("import_flickr"); loadlib("flickr"); loadlib("google"); ################################################################# login_ensure_loggedin("{$GLOBALS['cfg']['abs_root_url']}upload"); # temporary bits until everything gets merged in to one # magic upload box... $GLOBALS['smarty']->assign("include_url_upload", 1); if (!$GLOBALS['cfg']['enable_feature_import']) { $GLOBALS['error']['uploads_disabled'] = 1; $smarty->display("page_upload_disabled.txt"); exit; } ################################################################# $crumb_key = 'upload'; $crumb_ok = crumb_check($crumb_key); $GLOBALS['smarty']->assign("crumb_key", $crumb_key); # $label = filter_strict(post_str('label')); $private = post_str('private') ? 1 : 0; $dots_index_on = filter_strict(post_str('dots_index_on')); $mime_type = filter_strict(post_str('mime_type')); $GLOBALS['smarty']->assign("label", $label);
<?php $root = dirname(dirname(__FILE__)); ini_set("include_path", "{$root}/www:{$root}/www/include"); set_time_limit(0); # include "include/init.php"; loadlib("cli"); loadlib("flickr_backups"); loadlib("flickr_push"); loadlib("flickr_push_subscriptions"); $features = array("backups", "flickr_push", "flickr_push_backups"); if (!features_is_enabled($features)) { echo "backups are currently disabled\n"; exit; } $spec = array("url" => array("flag" => "u", "required" => 1, "help" => "the *root* URL of your copy of parallel-ogram (the need to specify this here is not a feature...)")); $opts = cli_getopts($spec); $topic = $opts['topic']; # This sucks to have to do but I am uncertain what the # better alternative is right now... (20120601/straup) $root = rtrim($opts['url'], '/') . "/"; $GLOBALS['cfg']['abs_root_url'] = $root; log_info("set 'abs_root_url' to '{$GLOBALS['cfg']['abs_root_url']}'"); $topic_map = flickr_push_topic_map("string keys"); $topics = array("my_photos", "my_faves"); foreach (flickr_backups_users() as $user) { foreach ($topics as $topic) { $sub = array('user_id' => $user['id'], 'topic_id' => $topic_map[$topic]); $rsp = flickr_push_subscriptions_register_subscription($sub); log_info("[{$user['username']}] {$topic}: {$rsp['ok']}");
<?php # Firefox and most phones will cache aggressively without this header("Cache-Control: no-cache, must-revalidate\r\n"); # If no one is logged in, just show the login/signup form loadlib('user'); if (user_id()) { Sd('logged_in', true); } else { redirect('/start'); } # Update the address if requested loadlib('location'); if ('POST' == $_SERVER['REQUEST_METHOD'] && !location_set_address(@$_POST['address'])) { Sd('error', 'Error updating location.'); } $location = location_get(); if (is_array($location)) { Sd('address', $location['address']); if ($location['sweep_ts']) { Sd('sweep', date($DATEFORMAT, $location['sweep_ts'])); } Sd('impossible', (bool) $location['impossible']); Sd('inaccurate', (bool) $location['inaccurate']); } # See if we still need the SMS confirmation form $c = db_query("SELECT confirm_sms FROM users WHERE id = '" . user_id() . "' LIMIT 1;"); Sd('need_confirm_sms', !is_array($c) || !sizeof($c) || !$c[0]['confirm_sms']);