error_reporting(0); // $url_from_name = strip_tags($_GET['name']); $tierToApplePrice = Array( 0 => 'FREE', 1 => 0.99, 2 => 1.99, 3 => 2.99, 4 => 3.99, 5 => 4.99, 550 => 0.99, 560 => 1.99, 570 => 2.99, 580 => 3.99, 590 => 4.99, 6 => 5.99, 7 => 6.99, 8 => 7.99, 9 => 8.99, 10 => 9.99, 11 => 10.99, 12 => 11.99, 13 => 12.99, 14 => 13.99, 15 => 14.99, 16 => 15.99, 17 => 16.99, 18 => 17.99, 19 => 18.99, 20 => 19.99, 21 => 20.99, 22 => 21.99, 23 => 22.99, 24 => 23.99, 25 => 24.99, 26 => 25.99, 27 => 26.99, 28 => 27.99, 29 => 28.99, 30 => 29.99, 31 => 30.99, 32 => 31.99, 33 => 32.99, 34 => 33.99, 35 => 34.99, 36 => 35.99, 37 => 36.99, 38 => 37.99, 39 => 38.99, 40 => 39.99, 41 => 40.99, 42 => 41.99, 43 => 42.99, 44 => 43.99, 45 => 44.99, 46 => 45.99, 47 => 46.99, 48 => 47.99, 49 => 48.99, 50 => 49.99, 51 => 54.99, 52 => 59.99, 53 => 64.99, 54 => 69.99, 55 => 74.99, 56 => 79.99, 57 => 84.99, 58 => 89.99, 59 => 94.99, 60 => 99.99, 61 => 109.99, 62 => 119.99, 63 => 124.99, 64 => 129.99, 65 => 139.99, 66 => 149.99, 67 => 159.99, 68 => 169.99, 69 => 174.99, 70 => 179.99, 71 => 189.99, 72 => 199.99, 73 => 209.99, 74 => 219.99, 75 => 229.99, 76 => 239.99, 77 => 249.99, 78 => 299.99, 79 => 349.99, 80 => 399.99, 81 => 449.99, 82 => 499.99, 83 => 599.99, 84 => 699.99, 85 => 799.99, 86 => 899.99, 87 => 999.99, ); $filename = 'keys.txt'; $signed_request = strip_tags($_POST['signed_request']); // /* tmp */ $signed_request = 'PtuV87WC2F8bFwpl3z6dMFQP2UcK9JRI9RT1i0ckSXk.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImlzc3VlZF9hdCI6MTM4NzM2NTkwMCwicGFnZSI6eyJpZCI6IjIxMjcyMDk3NTU3NzU1NCIsImxpa2VkIjp0cnVlLCJhZG1pbiI6dHJ1ZX0sInVzZXIiOnsiY291bnRyeSI6InBsIiwibG9jYWxlIjoiZW5fVVMiLCJhZ2UiOnsibWluIjoyMX19fQ'; $secret = '7745a7ba3361a2f0fbf26021223b91d6'; // print_r(parse_signed_request($signed_request)); $decoded_request = parse_signed_request($signed_request); $tab_id = $decoded_request['page']['id']; $do_we_have_it = 0; $numer_of_magazines = 0; $store_hash = getHashFromName($url_from_name); $mag_details = renderContentForStoreHash($store_hash); $past_issues = listAllIssuesForStoreHash($store_hash, $url_from_name); /* get hash from name */ function getHashFromName ($name) { global $do_we_have_it, $numer_of_magazines; $data = json_decode(file_get_contents('https://api.presspadapp.com/public/1.0/stores.json', true), true); foreach ($data['stores'] as $key => $value) { // $name_trimmed = trim(preg_replace('/[\-]{2,}/', '-', preg_replace('/[^a-z]/', '-', strtolower($value['name']))), '-'); $name_trimmed = $value['slug']; $numer_of_magazines++; if ($name_trimmed === $name) { $do_we_have_it = 1; return $value['hash']; break; } } } /* render frame content for given store hash */ function renderContentForStoreHash ($store_hash) { global $do_we_have_it; if ($do_we_have_it) { $data = json_decode(file_get_contents('http://api.presspadapp.com/public/1.0/stores/'.$store_hash.'.json', true), true); $return = Array(); foreach ($data as $key => $value) { array_push($return, $value); } return $return[0]; } } /* render past issues for given store hash */ function listAllIssuesForStoreHash ($store_hash, $name) { global $do_we_have_it, $numer_of_magazines; // if no magazine, show 8 examples if ($do_we_have_it) { $data = json_decode(file_get_contents('http://api.presspadapp.com/public/1.0/stores/'.$store_hash.'/issues.json', true), true); $i = 0; foreach ($data as $key => $value) { return $value; } } else { $data = json_decode(file_get_contents('http://api.presspadapp.com/public/1.0/stores.json', true), true); $i = 0; $alphabet = range('a', 'z'); $start = round(((array_search($name[0], $alphabet) + 1) / 25) * $numer_of_magazines); if ($start >= $numer_of_magazines) { $start = $numer_of_magazines - 6; } $stop = $start + 6; foreach ($data['stores'] as $key => $value) { if ($key >= $start && $key <= $stop) { $result[] = $value; } } return $result; } } /* read data */ function readStoreHashFromTabId ($tab_id) { global $filename; // read file $file_contents = fread(fopen($filename, "r"), filesize($filename)); // deserialize $data = unserialize($file_contents); if ($data[$tab_id]) return $data[$tab_id]; else return false; } /* decode signed_request from facebook tab */ function parse_signed_request($signed_request) { global $secret; list($encoded_sig, $payload) = explode('.', $signed_request, 2); // decode the data $sig = base64_url_decode($encoded_sig); $data = json_decode(base64_url_decode($payload), true); // confirm the signature $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true); if ($sig !== $expected_sig) { error_log('Bad Signed JSON signature!'); return null; } return $data; } function base64_url_decode($input) { return base64_decode(strtr($input, '-_', '+/')); } function titleCase($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc"), $exceptions = array("and", "to", "of", "das", "dos", "I", "II", "III", "IV", "V", "VI", "the", "a", "an")) { /* * Exceptions in lower case are words you don't want converted * Exceptions all in upper case are any words you don't want converted to title case * but should be converted to upper case, e.g.: * king henry viii or king henry Viii should be King Henry VIII */ $string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8"); foreach ($delimiters as $dlnr => $delimiter) { $words = explode($delimiter, $string); $newwords = array(); foreach ($words as $wordnr => $word) { if (in_array(mb_strtoupper($word, "UTF-8"), $exceptions)) { // check exceptions list for any words that should be in upper case $word = mb_strtoupper($word, "UTF-8"); } elseif (in_array(mb_strtolower($word, "UTF-8"), $exceptions)) { // check exceptions list for any words that should be in upper case $word = mb_strtolower($word, "UTF-8"); } elseif (!in_array($word, $exceptions)) { // convert to uppercase (non-utf8 only) $word = ucfirst($word); } array_push($newwords, $word); } $string = join($delimiter, $newwords); }//foreach return $string; } ?>