Jump to content

gdfhghjdfghgfhf

Members
  • Posts

    218
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

gdfhghjdfghgfhf's Achievements

Regular Member

Regular Member (3/5)

0

Reputation

  1. I paid a programmer to make a shop basket script to work with Spreadshirt API. Everything is working perfectly, except that the basket keeps empting itself. I think the session is lost at some point so the script creates another BasketId. I tried to find if there was a specific reason it was happenning, without any success... I can't reproduce the bug. It just happens randomly without any reason. Closing the browser, resetting apache or even the whole webserver won't provoke session lost. I've got two different scripts working with cookies on the same domain and they don't have any problem (one is a cookie for the admin login session and the other cookie is to save the user's last viewed articles on the shop) I tried all solutions found on google without any success : editing php.ini , forcing ini settings through php, tried the htaccess way, ... Here's the "sessions" part of my phpinfo: http://gyazo.com/168e2144ddd9ee368a05754dfd463021 shop-ajax.php (session handling @ line 18) ini_set('session.cookie_domain', '.mywebsite.com' ); header("Pragma: no-cache"); header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate"); $language = addslashes($_GET['l']); $shopid = addslashes($_GET['shop']); // if($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') { // die("no direct access allowed"); // } if(!session_id()) { $lifetime=60 * 60 * 24 * 365; $domain = ".mywebsite.com"; session_set_cookie_params($lifetime,"/",$domain); @session_start(); } // Configuration $config['ShopSource'] = "com"; $config['ShopId'] = $shopid; $config['ShopKey'] = "*****"; $config['ShopSecret'] = "*****"; /* * add an article to the basket */ if (isset($_POST['size']) && isset($_POST['appearance']) && isset($_POST['quantity'])) { /* * create an new basket if not exist */ if (!isset($_SESSION['basketUrl'])) { /* * get shop xml */ $stringApiUrl = 'http://api.spreadshirt.'.$config['ShopSource'].'/api/v1/shops/' . $config['ShopId']; $stringXmlShop = oldHttpRequest($stringApiUrl, null, 'GET'); if ($stringXmlShop[0]!='<') die($stringXmlShop); $objShop = new SimpleXmlElement($stringXmlShop); if (!is_object($objShop)) die('Basket not loaded'); /* * create the basket */ $namespaces = $objShop->getNamespaces(true); $basketUrl = createBasket('net', $objShop, $namespaces); $_SESSION['basketUrl'] = $basketUrl; $_SESSION['namespaces'] = $namespaces; /* * get the checkout url */ $checkoutUrl = checkout($_SESSION['basketUrl'], $_SESSION['namespaces']); // basket language workaround if ($language=="fr") { if (!strstr($checkoutUrl,'/fr')) { $checkoutUrl = str_replace("spreadshirt.com","spreadshirt.com/fr",$checkoutUrl); } } $_SESSION['checkoutUrl'] = $checkoutUrl; } /* Workaround for not having the appearance id */ if ($_POST['appearance']==0) { $stringApiArticleUrl = 'http://api.spreadshirt.'.$config['ShopSource'].'/api/v1/shops/' . $config['ShopId'].'/articles/'.intval($_POST['article']).'?fullData=true'; $stringXmlArticle = oldHttpRequest($stringApiArticleUrl, null, 'GET'); if ($stringXmlArticle[0]!='<') die($stringXmlArticle); $objArticleShop = new SimpleXmlElement($stringXmlArticle); if (!is_object($objArticleShop)) die('Article not loaded'); $_POST['appearance'] = intval($objArticleShop->product->appearance['id']); } /* * article data to be sent to the basket resource */ $data = array( 'articleId' => intval($_POST['article']), 'size' => intval($_POST['size']), 'appearance' => intval($_POST['appearance']), 'quantity' => intval($_POST['quantity']), 'shopId' => $config['ShopId'] ); /* * add to basket */ addBasketItem($_SESSION['basketUrl'] , $_SESSION['namespaces'] , $data); $basketData = prepareBasket(); echo json_encode(array("c" => array("u" => $_SESSION['checkoutUrl'],"q" => $basketData[0],"l" => $basketData[1]))); } // no call, just read basket if not empty if (isset($_GET['basket'])) { if (array_key_exists('basketUrl',$_SESSION) && !empty($_SESSION['basketUrl'])) { $basketData = prepareBasket(); echo json_encode(array("c" => array("u" => $_SESSION['checkoutUrl'],"q" => $basketData[0],"l" => $basketData[1]))); } else { echo json_encode(array("c" => array("u" => "","q" => 0,"l" => ""))); } } function prepareBasket() { $intInBasket=0; if (isset($_SESSION['basketUrl'])) { $basketItems=getBasket($_SESSION['basketUrl']); if(!empty($basketItems)) { foreach($basketItems->basketItems->basketItem as $item) { $intInBasket += $item->quantity; } } } $l = ""; $pQ = parse_url($_SESSION['checkoutUrl']); if (preg_match("#^basketId\=([0-9a-f\-])*$#i", $pQ['query'])) { $l = $pQ['query']; } return array($intInBasket,$l); } // Additional functions function addBasketItem($basketUrl, $namespaces, $data) { global $config; $basketItemsUrl = $basketUrl . "/items"; $basketItem = new SimpleXmlElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <basketItem xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net"> <quantity>' . $data['quantity'] . '</quantity> <element id="' . $data['articleId'] . '" type="sprd:article" xlink:href="http://api.spreadshirt.'.$config['ShopSource'].'/api/v1/shops/' . $data['shopId'] . '/articles/' . $data['articleId'] . '"> <properties> <property key="appearance">' . $data['appearance'] . '</property> <property key="size">' . $data['size'] . '</property> </properties> </element> <links> <link type="edit" xlink:href="http://' . $data['shopId'] .'.spreadshirt.' .$config['ShopSource'].'/-A' . $data['articleId'] . '"/> <link type="continueShopping" xlink:href="http://' . $data['shopId'].'.spreadshirt.'.$config['ShopSource'].'"/> </links> </basketItem>'); $header = array(); $header[] = createAuthHeader("POST", $basketItemsUrl); $header[] = "Content-Type: application/xml"; $result = oldHttpRequest($basketItemsUrl, $header, 'POST', $basketItem->asXML()); } function createBasket($platform, $shop, $namespaces) { $basket = new SimpleXmlElement('<basket xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net"> <shop id="' . $shop['id'] . '"/> </basket>'); $attributes = $shop->baskets->attributes($namespaces['xlink']); $basketsUrl = $attributes->href; $header = array(); $header[] = createAuthHeader("POST", $basketsUrl); $header[] = "Content-Type: application/xml"; $result = oldHttpRequest($basketsUrl, $header, 'POST', $basket->asXML()); $basketUrl = parseHttpHeaders($result, "Location"); return $basketUrl; } function checkout($basketUrl, $namespaces) { $basketCheckoutUrl = $basketUrl . "/checkout"; $header = array(); $header[] = createAuthHeader("GET", $basketCheckoutUrl); $header[] = "Content-Type: application/xml"; $result = oldHttpRequest($basketCheckoutUrl, $header, 'GET'); $checkoutRef = new SimpleXMLElement($result); $refAttributes = $checkoutRef->attributes($namespaces['xlink']); $checkoutUrl = (string)$refAttributes->href; return $checkoutUrl; } /* * functions to build headers */ function createAuthHeader($method, $url) { global $config; $time = time() *1000; $data = "$method $url $time"; $sig = sha1("$data ".$config['ShopSecret']); return "Authorization: SprdAuth apiKey=\"".$config['ShopKey']."\", data=\"$data\", sig=\"$sig\""; } function parseHttpHeaders($header, $headername) { $retVal = array(); $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header)); foreach($fields as $field) { if (preg_match('/(' . $headername . '): (.+)/m', $field, $match)) { return $match[2]; } } return $retVal; } function getBasket($basketUrl) { $header = array(); $basket = ""; if (!empty($basketUrl)) { $header[] = createAuthHeader("GET", $basketUrl); $header[] = "Content-Type: application/xml"; $result = oldHttpRequest($basketUrl, $header, 'GET'); $basket = new SimpleXMLElement($result); } return $basket; } function oldHttpRequest($url, $header = null, $method = 'GET', $data = null, $len = null) { switch ($method) { case 'GET': $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); if (!is_null($header)) curl_setopt($ch, CURLOPT_HTTPHEADER, $header); break; case 'POST': $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, true); //not createBasket but addBasketItem curl_setopt($ch, CURLOPT_POSTFIELDS, $data); break; } $result = curl_exec($ch); curl_close($ch); return $result; } ?> There's also 2 other parts of the script : a form to add a sample tshirt to the basket (example.php) and a script to call the ajax (shop-controller.js). Can post it if needed but there's no session handling stuff.
  2. I have a script that will search for a file inside all directories of my server, then return the full path to the file. The search query comes encoded in base64. The script works perfectly when searching a file without accents. But as soon as i search for something with accents, it won't work. Here is my file structure: When searching for the first file (WWEgQmFzdGEhIC0gMjAwMiAtIEx1Y2hhIFkgRmllc3RhLnppcA==) it will return the correct path. But when searching for the second file with accents (TGEgU291cmlzIETpZ2xpbmd16WUgLSAxOTg4IC0gUXVhcnRpZXIgTGlicmUuemlw) it will not return any result even if the file exists Here's the script: header("Content-type: text/html; charset=utf-8"); // $encodedquery = "WWEgQmFzdGEhIC0gMjAwMiAtIEx1Y2hhIFkgRmllc3RhLnppcA=="; // query without accent = WORKS ! $encodedquery = "TGEgU291cmlzIETpZ2xpbmd16WUgLSAxOTg4IC0gUXVhcnRpZXIgTGlicmUuemlw" // query with accents = DOESNT WORKS $decode = utf8_encode(base64_decode($encodedquery); $search = preg_quote($decode); echo "searching for \"$search\" ...."; function rsearch($folder, $pattern) { $dir = new RecursiveDirectoryIterator($folder); $ite = new RecursiveIteratorIterator($dir); $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH); $fileList = array(); foreach($files as $file) { $fileList = array_merge($fileList, $file); } return $fileList; } $root = $_SERVER['DOCUMENT_ROOT']; $resultatss = rsearch($root, '/.*\/'.$search.'/'); foreach ($resultatss as $resultat) { echo "<br>FOUND FILE = $resultat"; }
  3. Thanks for the reply. 2 problems now : - The image is saved as PNG in the cache folder, but isn't resized. - The first time i will load the image, i will get an error (image cannot be displayed because it contains error) but the image will still be saved as PNG in the cache folder. Second time i load the image, it will be displayed correctly (using the cached version) but it isn't resized. Here's the full code of my page. The first part is used to cache the image, the second part is used to display the non-cached image (it reads an image from a ZIP file and output the content without extracting anything) pochette.php : if (empty($_GET['display'])) { header('Content-Type: image/png'); $imgpochette = $_GET['i']; $ENABLE_CACHE = true; $CACHE_TIME_HOURS = 744; $CACHE_FILE_PATH = "pochette_album/$imgpochette.png"; if($ENABLE_CACHE && file_exists($CACHE_FILE_PATH) && (time() - filemtime($CACHE_FILE_PATH) < ($CACHE_TIME_HOURS * 60 * 60))) { echo @file_get_contents($CACHE_FILE_PATH); } else { // Load the requested image $imgdisplay = "http://www.pirate-punk.com/pochette.php?i=$imgpochette&display=1"; $image = imagecreatefromstring(file_get_contents($imgdisplay)); $width = "30"; $height = "30"; list($originalWidth, $originalHeight) = getimagesize($CACHE_FILE_PATH); $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight); // Send the image imagejpeg($image, $CACHE_FILE_PATH); exit(); @file_put_contents($CACHE_FILE_PATH, $output); echo $output; } } if (!empty($_GET['display'])) { function showimage($zip_file, $file_name) { $z = new ZipArchive(); if ($z->open($zip_file) !== true) { echo "File not found."; return false; } $stat = $z->statName($file_name); $fp = $z->getStream($file_name); // search for a path/to/file matching file, returning the index of it $index = $z->locateName($file_name, ZipArchive::FL_NOCASE|ZipArchive::FL_NODIR); // get the name of the file based on the index $full_file_name = $z->getNameIndex($index); // now get the stream $fp = $z->getStream($full_file_name); if(!$fp) { echo "Could not load image."; return false; } header('Content-Type: image/jpeg'); header('Content-Length: ' . $stat['size']); fpassthru($fp); return true; } $imgsrcencoded = $_GET['i']; $imagesrc = base64_decode($imgsrcencoded); $explodez = explode("#",$imagesrc); $imgg = utf8_encode($explodez[1]); $dirnfile = $explodez[0]; $zipp = end((explode('/', $dirnfile))); $dirr = str_replace($zipp,"",$dirnfile); $dirr = rtrim($dirr,"/"); $imgg = rtrim($imgg); chdir($dirr); if (empty($_GET['debug'])) { echo showimage($zipp,$imgg); } }
  4. This script will load an image (jpg, gif or png) and then save a PNG local copy for caching. I'm trying to find a way to resize the image to 300x300 before saving it as a PNG. I tried to use the function imagecopyresampled() (as you can see in the commented lines) but i get an error saying the image could not be displayed because it contains errors. header('Content-Type: image/png'); $imgpochette = $_GET['i']; $ENABLE_CACHE = true; $CACHE_TIME_HOURS = 744; $CACHE_FILE_PATH = "pochette_album/$imgpochette.png"; if($ENABLE_CACHE && file_exists($CACHE_FILE_PATH) && (time() - filemtime($CACHE_FILE_PATH) < ($CACHE_TIME_HOURS * 60 * 60))) { echo @file_get_contents($CACHE_FILE_PATH); } else { // Load the requested image $imgdisplay = "http://www.pirate-punk.com/pochette.php?i=$imgpochette&display=1"; $image = imagecreatefromstring(file_get_contents($imgdisplay)); // $width = "30"; // $height = "30"; // $new_image = imagecreatetruecolor($width, $height); // imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $image.getWidth(), $image.getHeight()); // Send the image imagejpeg($image, $CACHE_FILE_PATH); exit(); @file_put_contents($CACHE_FILE_PATH, $output); echo $output; }
  5. Awesome !!!!!! it seems to be working perfectly now !!!! thanks a lot for the time you spent on this, you were very helpful and explained things very well. I'm going to consider making a donation next month
  6. Hello, Sorry for late reply, i was away from home. Here's the result: The filename inside the archive and the passed filename seems to be exactly the same thing... I'm really confused.
  7. This script will find a zip archive in a specific folder, then read an image file inside the zip and output it as jpg. It works perfectly when using a simple image filename, but as soon as the image file has special characters it returns an error "could not load image". Here's the code: <?php function showimage($zip_file, $file_name) { $z = new ZipArchive(); if ($z->open($zip_file) !== true) { echo "File not found."; return false; } $stat = $z->statName($file_name); $fp = $z->getStream($file_name); if(!$fp) { echo "Could not load image."; return false; } header('Content-Type: image/jpeg'); header('Content-Length: ' . $stat['size']); fpassthru($fp); return true; } $imgsrcencoded = $_GET['i']; $imagesrc = base64_decode($imgsrcencoded); $explodez = explode("#",$imagesrc); $imgg = utf8_encode($explodez[1]); $dirnfile = $explodez[0]; $zipp = end((explode('/', $dirnfile))); $dirr = str_replace($zipp,"",$dirnfile); $dirr = rtrim($dirr,"/"); $imgg = urlencode(rtrim($imgg)); // $zipp = urlencode($zipp); chdir($dirr); if (empty($_GET['debug'])) { echo showimage($zipp,$imgg); // echo showimage("jpg.zip","test.jpg"); } // debug if (!empty($_GET['debug'])) { echo " base64 query = $imgsrcencoded<br> decoded query = $imgsrc<br> zipp = $zipp<br> imgg = $imgg<br> dirr = $dirr<br> current directory = "; echo getcwd(); } ?> And here's what my debug returns: So as you can see the script correctly switch to the corresponding directory and it is able to open the zip archive but it fails when come the time to read the JPG file if the filename has special characters I tried with or without utf8_encode() on the image's filename, i tried with or without urlencode() or rawurlencode() but i always get the same result... any suggestion ? i'm desperate.
  8. @AbraCadaver: not working @Barand: the problem is that the script returns incomplete filenames. If i have a file called "test123456789.zip" on my server and i use the script to search for "test12345" it will return 1 match but the script will print "test12345" instead of printing "test123456789.zip" Not sure how i could use realpath() to get the full filename of "test12345"
  9. DAMN !!! I just realised i have the same problem with $_SERVER['QUERY_STRING'] or $_SERVER['REQUEST_URI'] but this time it is with characters like "#" or "|" any solution ?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.