'GET', 'callback' => array($this, 'manage_serial'), /* 'permission_callback' => function () { return current_user_can('edit_others_posts'); }, */ 'permission_callback' => '__return_true' ]); register_rest_route('aeroscroll/v1', '/exportimagegallery', [ 'methods' => 'POST', 'callback' => array($this, 'export_imagegallery'), 'args' => array(), 'permission_callback' => function () { return current_user_can('edit_others_posts'); }, //'permission_callback' => '__return_true', 'args' => array() ]); register_rest_route('aeroscroll/v1', '/importimagegallery', [ 'methods' => 'POST', 'callback' => array($this, 'import_imagegallery'), 'args' => array(), /* 'permission_callback' => function () { return current_user_can('edit_others_posts'); }, */ 'permission_callback' => '__return_true', 'args' => array() ]); // Image Optimization register_rest_route('aeroscroll/v1', '/optimizeimages', [ 'methods' => 'POST', 'callback' => array($this, 'optimize_images'), 'args' => array(), 'permission_callback' => function () { return current_user_can('edit_others_posts'); }, 'args' => array() ]); } public function manage_serial($request) { $error = ""; $licence_active = false; $licence_code = ""; $wp_serials_request = "get"; $wp_serials_email = ""; $wp_serials_serial_key = ""; $wp_serials_storedserial_key = ""; $wp_serials_product_id = ""; $wp_serials_instance = ""; $result_update = ""; $encrypted_serial = ""; $debug = ""; global $wpdb; $table_settings = $wpdb->prefix . 'aeroscroll_gallery_settings'; try { $urlparts = wp_parse_url(home_url()); $domain = $urlparts['host']; $wp_serials_instance = $domain; if ($request->get_param('request')) $wp_serials_request = $request->get_param('request'); if ($request->get_param('email')) $wp_serials_email = $request->get_param('email'); if ($request->get_param('serial_key')) $wp_serials_serial_key = $request->get_param('serial_key'); if ($request->get_param('product_id')) $wp_serials_product_id = $request->get_param('product_id'); // FOR CHECK -> Get Details from Database if ($wp_serials_request == "get") { $query_select = "SELECT * FROM " . $table_settings . ";"; $table_cols = $wpdb->get_results($query_select); if (count($table_cols) > 0) { $wp_serials_email = $table_cols[0]->email; $wp_serials_serial_key = $table_cols[0]->serial_key; $wp_serials_storedserial_key = $table_cols[0]->serial_key; $wp_serials_product_id = $table_cols[0]->product_id; } else { $licence_active = false; $wp_serials_email = ""; $wp_serials_serial_key = ""; $wp_serials_product_id = ""; // Now INSERT ROW $query_insert = $wpdb->prepare("INSERT INTO " . $table_settings . " (email, serial_key, product_id) VALUES ('', '', '')"); $wpdb->query($query_insert); } } else if ($wp_serials_request == "check") { $query_select = "SELECT * FROM " . $table_settings . ";"; $table_cols = $wpdb->get_results($query_select); if (count($table_cols) > 0) { $wp_serials_email = $table_cols[0]->email; $wp_serials_serial_key = $table_cols[0]->serial_key; $wp_serials_product_id = $table_cols[0]->product_id; } else { $licence_active = false; $wp_serials_email = ""; $wp_serials_serial_key = ""; $wp_serials_product_id = ""; // Now INSERT ROW $query_insert = $wpdb->prepare("INSERT INTO " . $table_settings . " (email, serial_key, product_id) VALUES ('', '', '')"); $wpdb->query($query_insert); } } else if ($wp_serials_request == "activate") { if ($wp_serials_serial_key != "") { $query_select = "SELECT * FROM " . $table_settings . ";"; $table_cols = $wpdb->get_results($query_select); if (count($table_cols) == 0) { // Now INSERT ROW $query_insert = $wpdb->prepare("INSERT INTO " . $table_settings . " (email, serial_key, product_id) VALUES ('', '', '')"); $wpdb->query($query_insert); } //$api_url .= "&instance=" . $wp_serials_instance; } } else if ($wp_serials_request == "deactivate") { $query_select = "SELECT * FROM " . $table_settings . ";"; $table_cols = $wpdb->get_results($query_select); if (count($table_cols) > 0) { $wp_serials_email = $table_cols[0]->email; $wp_serials_serial_key = $table_cols[0]->serial_key; $wp_serials_product_id = $table_cols[0]->product_id; $wp_serials_instance = $table_cols[0]->instance; } } if ($wp_serials_request == "get") { $wp_serials_request = "check"; } // FROM UPDATE of Serials plugin if ($wp_serials_request == "check") { $wp_serials_request = "validate"; } $api_url = 'http://www.aeroscroll.com/?wc-api=serial-numbers-api&request=' . $wp_serials_request . '&email=' . $wp_serials_email . '&serial_key=' . $wp_serials_serial_key . '&product_id=' . $wp_serials_product_id; if ($wp_serials_request == "deactivate") $api_url .= "&instance=" . $wp_serials_instance; if ($wp_serials_request == "activate") $api_url .= "&instance=" . $domain; $remote = wp_remote_get($api_url); if (is_wp_error($remote) || 200 !== wp_remote_retrieve_response_code($remote) || empty(wp_remote_retrieve_body($remote))) { $licence_active = false; $licence_code = "server_error"; $debug = $remote['body']; } $remote = json_decode(wp_remote_retrieve_body($remote)); if ($remote->success == true) { if ($remote->data->code == "") { $licence_active = true; } $wp_serials_instance = $remote->data->instance; $licence_code = $remote->data->code; } if ($licence_code == "key_activated" || $licence_code == "instance_already_activated") { $licence_active = true; // Update Databse // Now UPDATE ROW $wpdb->show_errors = TRUE; $wpdb->suppress_errors = FALSE; $encrypted_serial = $wp_serials_serial_key; $result_update = $wpdb->update( $table_settings, array( 'serial_key' => $wp_serials_serial_key, 'product_id' => $wp_serials_product_id, 'instance' => $wp_serials_instance, 'updated_at' => current_time('mysql') ), array( "identifier" => "1" ) ); /* if ($wpdb->error) { $error = $wpdb->error; } */ } else if ($licence_code == "server_error") { $licence_active = true; if($wp_serials_storedserial_key != "" && $wp_serials_storedserial_key != null) { } } else if ($licence_code == "key_valid") { $licence_active = true; } else if ($licence_code == "key_deactivated") { $licence_active = false; $result_update = $wpdb->update( $table_settings, array( 'serial_key' => '', 'product_id' => '', 'instance' => '', 'updated_at' => current_time('mysql') ), array( "identifier" => "1" ) ); } } catch (Exception $ex) { $error = $ex; } $obj = new stdClass(); $obj->active = $licence_active; $obj->code = $licence_code; $obj->email = $wp_serials_email; $obj->serialkey = $wp_serials_serial_key; $obj->storedserialkey = $wp_serials_storedserial_key; $obj->productid = $wp_serials_product_id; $obj->instance = $wp_serials_instance; $obj->domain = $domain; $obj->debug = $debug; return $obj; } public function export_imagegallery($request) { global $wpdb; $imagegallery_id = -1; $success = false; $debug = ""; $exportfile = ""; if ($request->get_param('imagegallery_id')) $imagegallery_id = $request->get_param('imagegallery_id'); if ($imagegallery_id != -1 && $imagegallery_id != null) { $table = $wpdb->prefix . 'aeroscroll_gallery_imagegallery_images'; $query = "SELECT * FROM $table WHERE imagegallery_id = " . $imagegallery_id; $results = $wpdb->get_results($query); $galleryimages = $results; try { // Create the ZIP Archive $zip = new ZipArchive; $zip_path = getcwd() . '/wp-content/uploads/aeroscroll-gallery/exports/'; if (!file_exists($zip_path)) { mkdir($zip_path, 0755, true); } //echo "ZIP PATH: " . $zip_path; if ($zip->open($zip_path . 'export_gallery_id-' . $imagegallery_id . '.zip', ZipArchive::CREATE) === TRUE) { foreach ($galleryimages as $img) { // For each image Get URL to add to zip all images to one folder // Structure: // > Images // json file $image_name = $img->image_name; $image_relative_dir = $img->relativedir; $image_fullpath = getcwd() . $image_relative_dir . "/" . $image_name; // If File Exists add to ZIP if (file_exists($image_fullpath)) { $zip->addFile($image_fullpath, "images/".$image_name); } // Check if optimized exists $optimizedpath = getcwd() . $image_relative_dir . '/__optimized/opt__' . $image_name; if (file_exists($optimizedpath)) { $zip->addFile($optimizedpath, "images/__optimized/opt__" . $image_name); } $img->relativedir = ""; } $json_string = json_encode($galleryimages); $zip->addFromString('gallery.json', $json_string); // All files are added, so close the zip file. $zip->close(); $success = true; $exportfile = get_site_url() . '/wp-content/uploads/aeroscroll-gallery/exports/' . 'export_gallery_id-' . $imagegallery_id . '.zip'; } } catch (Exception $ex) { $debug = $ex; var_dump($ex); $success = false; } } $obj = new stdClass(); $obj->success = $success; $obj->exportfile = $exportfile; $obj->debug = $debug; return $obj; } public function import_imagegallery($request) { global $wpdb; $imagegallery_id = -1; $success = false; $debug = ""; $zip_path = getcwd() . '/wp-content/uploads/aeroscroll-gallery/imports/'; if (!file_exists($zip_path)) { mkdir($zip_path, 0755, true); } /* if ($request->get_header('imagegallery_id')) $imagegallery_id = $request->get_header('imagegallery_id'); */ if ( isset($_GET['imagegallery_id']) ) { // If so echo the value try { $imagegallery_id = (int)$_GET['imagegallery_id']; } catch(Exception $ex) { $debug = $ex; } } //$debug = $imagegallery_id; if (isset($_FILES)) { $import_file = null; foreach ($_FILES as $var => $value) { $import_file = $value; } $filename = $import_file['name']; $type = $import_file['type']; $file_tmp = $import_file['tmp_name']; //$debug .= $filename; $finalpath = $zip_path . $filename; move_uploaded_file($file_tmp, $finalpath); $zip = new ZipArchive; $res = $zip->open($finalpath); $json_data = null; if ($res === TRUE) { $final_zip_path = $zip_path . str_replace(".zip","",$filename) . "/"; $zip->extractTo($final_zip_path); //$debug = $final_zip_path; $zip->close(); $gallery_json_file = $final_zip_path . "gallery.json"; if (file_exists($gallery_json_file)) { // Read JSON data $json = file_get_contents($gallery_json_file); $json_data = json_decode($json,true); // Copy images to destination folder $sourceDirectory = $final_zip_path.'/images'; if (file_exists($sourceDirectory)) { $destinationDirectory = getcwd() . '/wp-content/uploads/aeroscroll-gallery/gallery-'.$imagegallery_id; if (!file_exists($destinationDirectory)) { mkdir($destinationDirectory, 0755, true); } $this->copyDirectory($sourceDirectory, $destinationDirectory); //$this->recurse_copy($sourceDirectory, $destinationDirectory); } // Process JSON Data try { $table = $wpdb->prefix . 'aeroscroll_gallery_imagegallery_images'; $query = "SELECT * FROM $table WHERE imagegallery_id = " . $imagegallery_id; $results = $wpdb->get_results($query); $galleryimages = $results; //$debug = $imagegallery_id; if(count($galleryimages) >= 0) { foreach ($json_data as $img) { $relativedir = '/wp-content/uploads/aeroscroll-gallery/gallery-'.$imagegallery_id; $query_insert = $wpdb->prepare("INSERT INTO " . $table . " (imagegallery_id, title, description, relativedir, image_order, image_name) VALUES ('".$imagegallery_id."', '".$img["title"]."', '".$img["description"]."', '".$relativedir."', '".$img["image_order"]."', '".$img["image_name"]."')"); $inres = $wpdb->query($query_insert); $debug = $inres; } $success = true; } } catch(Exception $ex) { $debug = $ex; } } } else { $success = false; $debug = "unzip failed"; } } $obj = new stdClass(); $obj->success = $success; $obj->debug = $debug; return $obj; } public function copyDirectory($source, $destination) { try { if (!is_dir($destination)) { mkdir($destination, 0755, true); } $files = scandir($source); foreach ($files as $file) { if ($file !== '.' && $file !== '..') { $sourceFile = $source . '/' . $file; $destinationFile = $destination . '/' . $file; if (is_dir($sourceFile)) { $this->copyDirectory($sourceFile, $destinationFile); } else { copy($sourceFile, $destinationFile); } } } } catch(Exception $ex) { echo $ex; } } public function recurse_copy($src,$dst) { $dir = opendir($src); @mkdir($dst); while(false !== ( $file = readdir($dir)) ) { if (( $file != '.' ) && ( $file != '..' )) { if ( is_dir($src . '/' . $file) ) { recurse_copy($src . '/' . $file,$dst . '/' . $file); } else { copy($src . '/' . $file,$dst . '/' . $file); } } } closedir($dir); } public function optimize_images($params) { $error = ""; try { $images = $params['images']; $paramrelativedir = $params['relativedir']; $dir = getcwd() . $paramrelativedir; $result = 0; $filename = ""; if ($images != null) { if (is_array($images)) { $total_files = count($images); $progress_step = (int)(100 / $total_files); for ($x = 0; $x < count($images); $x++) { //$filename = $images[$x]->name; $filename = $images[$x]["name"]; $fullpath = getcwd() . $paramrelativedir . "/" . $filename; $targetpath = getcwd() . $paramrelativedir . "/__optimized/" . "opt__" . $filename; if (!file_exists(getcwd() . $paramrelativedir . "/__optimized/")) { mkdir(getcwd() . $paramrelativedir . "/__optimized/", 0777, true); } if (file_exists($fullpath)) { $this->compressImage($fullpath, $targetpath, 50); $result = 1; } } } //$error = $images; } } catch (Exception $ex) { $error = $ex; } return '{ "result": ' . $result . ', "error": "' . $error . '" }'; } public function compressImage($source, $destination, $quality) { $result = 0; $info = getimagesize($source); //echo "mime: " . $info['mime']; if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source); imagejpeg($image, $destination, $quality); return $result; } public function add_watermark($image, $gallery_properties) { $output = ""; $watermark_type = $gallery_properties->watermark_type; $watermark_text = $gallery_properties->watermark_text; $watermark_fontsize = $gallery_properties->watermark_fontsize; $watermark_color = $gallery_properties->watermark_color; $watermark_opacity = $gallery_properties->watermark_opacity; $watermark_position = $gallery_properties->watermark_position; $watermark_image_url = $gallery_properties->watermark_image_url; $watermark_image_size = $gallery_properties->watermark_image_size; $image_name = $image->image_name; $image_relativedir = $image->relativedir; $thumbnail_image = $image->thumbnail_image; $im = imagecreatefromstring(file_get_contents($thumbnail_image)); $stamp = null; $text = ""; $textcolor = null; $font_file = plugin_dir_path(dirname(__FILE__)) . 'public/fonts/' . 'roboto.ttf'; $im_w = imagesx($im); $im_h = imagesy($im); $MARGE = 30; $margin_right = $MARGE; $margin_top = $MARGE; if ($watermark_type == "2") { $stamp = imagecreatefromstring(file_get_contents($watermark_image_url)); $stamp_w = imagesx($stamp); $stamp_h = imagesy($stamp); $percent_size = $gallery_properties->watermark_image_size; $percent_stamp_w = (int)($im_w * $percent_size / 100); $percent_stamp_h = (int)($im_h * $percent_size / 100); switch ($watermark_position) { case 1: $margin_right = $MARGE; $margin_top = $MARGE; break; case 2: $margin_right = (int)($im_w / 2 - $percent_stamp_w / 2); $margin_top = $MARGE; break; case 3: $margin_right = $im_w - $MARGE - $percent_stamp_w; $margin_top = $MARGE; break; case 4: $margin_right = $MARGE; $margin_top = (int)($im_h / 2 - $percent_stamp_h / 2); break; case 5: $margin_right = (int)($im_w / 2 - $percent_stamp_w / 2); $margin_top = (int)($im_h / 2 - $percent_stamp_h / 2); break; case 6: $margin_right = $im_w - $MARGE - $percent_stamp_w; $margin_top = (int)($im_h / 2 - $percent_stamp_h / 2); break; case 7: $margin_right = $MARGE; $margin_top = $im_h - $MARGE - $percent_stamp_h; break; case 8: $margin_right = (int)($im_w / 2 - $percent_stamp_w / 2); $margin_top = $im_h - $MARGE - $percent_stamp_h; break; case 9: $margin_right = $im_w - $MARGE - $percent_stamp_w; $margin_top = $im_h - $MARGE - $percent_stamp_h; break; default: $margin_right = $MARGE; $margin_top = $MARGE; } } if ($watermark_type == "1") { $text_canvas = imagecreatetruecolor($MARGE, $MARGE); $textcolor = imagecolorallocate($text_canvas, 255, 255, 255); $text_with = 0; $final_watermark_fontsize = (int)($watermark_fontsize * $im_h / 100); switch ($watermark_position) { case 1: $margin_right = $MARGE; $margin_top = $MARGE; break; case 2: $margin_right = (int)($im_w / 2 - $text_with / 2); $margin_top = $MARGE; break; case 3: $margin_right = $im_w - $MARGE - $text_with; $margin_top = $MARGE; break; case 4: $margin_right = $MARGE; $margin_top = (int)($im_h / 2 - $final_watermark_fontsize / 2); break; case 5: $margin_right = (int)($im_w / 2 - $text_with / 2); $margin_top = (int)($im_h / 2 - $final_watermark_fontsize / 2); break; case 6: $margin_right = $im_w - $MARGE - $text_with; $margin_top = (int)($im_h / 2 - $final_watermark_fontsize / 2); break; case 7: $margin_right = $MARGE; $margin_top = $im_h - $MARGE - $final_watermark_fontsize; break; case 8: $margin_right = (int)($im_w / 2 - $text_with / 2); $margin_top = $im_h - $MARGE - $final_watermark_fontsize; break; case 9: $margin_right = $im_w - $MARGE - $text_with; $margin_top = $im_h - $MARGE - $final_watermark_fontsize; break; default: $margin_right = $MARGE; $margin_top = $MARGE; } $margin_top += $final_watermark_fontsize; } $output_folder = getcwd() . $image_relativedir . '/__watermarked'; if ($image->folderpath != null && $image->folderpath != "") { $output_folder = $image->folderpath . '/__watermarked'; } if (!file_exists($output_folder)) { mkdir($output_folder, 0755, true); } $output = $output_folder . '/watermark_' . $image_name; $output_data = new stdClass(); $output_data->output = $output; $output_data->output_url = str_replace($image_name, "", $image->featured_image) . '__watermarked' . '/watermark_' . $image_name; if (!file_exists($output_folder)) { mkdir($output_folder, 0777, true); } $opacity = (float)($gallery_properties->watermark_opacity / 100); $final_opacity = 127 - (int)(127 * $opacity); if ($watermark_type == "1") { $im_text = imagecreatetruecolor($im_w, $im_h); list($r, $g, $b) = sscanf($watermark_color, "#%02x%02x%02x"); $textcolor = imagecolorallocatealpha($im_text, $r, $g, $b, $final_opacity); imagettftext($im, $final_watermark_fontsize, 0, $margin_right, $margin_top, $textcolor, $font_file, $watermark_text); } if ($watermark_type == "2") { $stamp_imgResized = imagescale($stamp, $percent_stamp_w, $percent_stamp_h); imagealphablending($stamp_imgResized, false); imagesavealpha($stamp_imgResized, true); imagefilter($stamp_imgResized, IMG_FILTER_COLORIZE, 0, 0, 0, $final_opacity); // the fourth parameter is alpha imagecopy($im, $stamp_imgResized, $margin_right, $margin_top, 0, 0, $percent_stamp_w, $percent_stamp_h); } if (str_contains($thumbnail_image, '.png')) { imagepng($im, $output); } else if (str_contains($thumbnail_image, '.jpg')) { imagejpeg($im, $output); } imagedestroy($im); return $output_data; } } ?>