function get_url_id() { $strip = basename($_SERVER['REQUEST_URI']); $strip = explode("?", $strip); if (count($strip) > 0) { return $strip[0]; } return NULL; } switch ($request_method) { case 'get': $result = $query->getConsumo(new stdClass()); break; case 'put': $put = file_get_contents("php://input", 'r'); $params = json_decode($put); $result = $query->updateConsumo($params); break; case 'post': $put = file_get_contents("php://input", 'r'); $params = json_decode($put); $result = $query->createConsumo($params); break; case 'delete': $id = get_url_id(); $params = new stdClass(); $params->id = $id; $result = $query->destroyConsumo($params); break; } echo json_encode($result);
function insert_urls_from_pool($dbh, $network_id, $channel_id, $nick_id, $message, $urls) { if (!$network_id) { return FALSE; } if (!$channel_id) { return FALSE; } if (!$nick_id) { return FALSE; } if (!$message) { return FALSE; } if (!$urls) { return FALSE; } if (!is_numeric($network_id)) { return FALSE; } if (!is_numeric($channel_id)) { return FALSE; } if (!is_numeric($nick_id)) { return FALSE; } $complete_urls = array(); foreach ($urls as $url) { $url_id = get_url_id($dbh, $url); if ($url_id === FALSE) { continue; } // get information about the new url if (!$url_id) { echo "url={$url}\n"; // information gathering... $http_meta = get_empty_http_meta(); if (substr($url, 0, 5) == 'http:') { $http_meta = get_http_meta($url, 0); // todo: we could choose to skip the url and the message if the state is not 1 here // or we record it (as we do now) and handle it someway later } // handle redirects // we store the original url but with the details of the destination // we will also keep a copy of the destination as a seperate record // we could probably handle this cleaner (e.g. redirecting url doesn't have text of destination), but maybe not much need $redirects_to_id = 0; if (array_key_exists('redirect', $http_meta) and array_key_exists('location', $http_meta)) { $redirects_to_url = $http_meta['location']; $redirects_to_id = get_url_id($dbh, $redirects_to_url); if ($redirects_to_id === FALSE) { continue; } if (!$redirects_to_id) { echo "Saving redirects_to record "; $redirects_to_id = insert_url($dbh, $redirects_to_url, $http_meta['state'], $http_meta['content_length'], $http_meta['content_type'], 0, $http_meta['html_title']); if ($redirects_to_id === FALSE) { continue; } // todo: do we need to do this as well for redirects? //$rv = insert_url_to_message($dbh, $dst_url_id, $message_id); //if (!$rv) { continue; } } } // store the new url print_r($http_meta); $url_id = insert_url($dbh, $url, $http_meta['state'], $http_meta['content_length'], $http_meta['content_type'], $redirects_to_id, $http_meta['html_title']); if ($url_id === FALSE) { continue; } } $complete_urls[$url] = $url_id; } if (count($complete_urls) != count($urls)) { // one or more of the urls failed to insert // abandon this message echo "\n\n<strong>complete_urls != urls - this message will be ignored but some urls may already have been inserted</strong>\n\n"; return 0; } $message = merge_url_ids_to_message($message, $complete_urls); // finally record the message $message_id = insert_message($dbh, $channel_id, $nick_id, $message); if (!$message_id) { return 0; } foreach ($complete_urls as $url_id) { #print "urlid=$url_id\n"; $rv = insert_url_to_message($dbh, $url_id, $message_id); if (!$rv) { continue; } } if (count($complete_urls)) { return $message_id; } else { return 0; } }