/** * Downloads image from picnik (or can be used for any url, really) and stores it at a file named $destination -- primarily a wrapper for PostToRemoteFileAndGetResponse with built-in file writing and error handling * * @param string $url * @param string $destination (optional) * @return string|bool Returns the filename the image was saved to, or false if anything went wrong */ public function downloadToFile($url, $destination = false, &$errorType = null) { $result = PostToRemoteFileAndGetResponse($url); if (!$destination) { // generate a random name for our downloaded file and store it in cache dir while (true) { // we can name it .tmp because the extension will be corrected after the image type is detected $destination = ISC_CACHE_DIRECTORY . 'picnikimage_' . Interspire_String::generateRandomString(16) . '.tmp'; if (!file_exists($destination)) { break; } } } $fh = fopen($destination, 'wb'); if ($fh) { if (!fwrite($fh, $result)) { fclose($fh); $this->log->LogSystemError('general', 'Failed to write downloaded Picnik image to local file'); $errorType = 1; return false; } fclose($fh); isc_chmod($destination, ISC_WRITEABLE_FILE_PERM); // set the chmod just incase this was a new file } else { $this->log->LogSystemError('general', 'Failed to open local file for saving downloaded Picnik image'); $errorType = 2; return false; } return $destination; }
/** * Generate a random string of characters to a specific length based on the specified selection of characters * * @param int $length * @param string $selection */ public static function randomString($length, $selection = '0123456789abcdefghijklmnopqrstuvwxyz') { return Interspire_String::generateRandomString($length, $selection); }