/** * Receive the File/Image * * @param \Zbase\Entity\Laravel\Entity $parentObject The Parent */ public function receiveFile(\Zbase\Entity\Laravel\Entity $parentObject) { try { $index = 'file'; $entityName = $this->entityName; $defaultImageFormat = zbase_config_get('node.files.image.format', 'png'); $folder = zbase_storage_path() . '/' . zbase_tag() . '/' . $this->actionUrlRouteName() . '/' . $parentObject->id() . '/'; zbase_directory_check($folder, true); $nodeFileObject = zbase_entity($entityName, [], true); $nodeFiles = $parentObject->childrenFiles(); if (preg_match('/http\\:/', $index) || preg_match('/https\\:/', $index)) { // File given is a URL if ($nodeFileObject->isUrlToFile()) { $filename = zbase_file_name_from_file(basename($index), time(), true); $uploadedFile = zbase_file_download_from_url($index, $folder . $filename); } else { $this->is_primary = empty($nodeFiles) ? 1 : 0; $this->status = 2; $this->mimetype = null; $this->size = null; $this->filename = null; $this->url = $index; $this->{$this->parentObjectIndexId} = $parentObject->id(); $this->user_id = zbase_auth_has() ? zbase_auth_user()->id() : null; $this->save(); return true; } } if (zbase_file_exists($index)) { $uploadedFile = $index; $filename = basename($index); } if (!empty($_FILES[$index]['name'])) { $filename = zbase_file_name_from_file($_FILES[$index]['name'], time(), true); $uploadedFile = zbase_file_upload_image($index, $folder, $filename, $defaultImageFormat); } if (!empty($uploadedFile) && zbase_file_exists($uploadedFile)) { $this->is_primary = empty($nodeFiles) ? 1 : 0; $this->status = 2; $this->user_id = zbase_auth_has() ? zbase_auth_user()->id() : null; $this->mimetype = zbase_file_mime_type($uploadedFile); $this->size = zbase_file_size($uploadedFile); $this->filename = basename($uploadedFile); $this->{$this->parentObjectIndexId} = $parentObject->id(); if (empty($nodeFiles)) { $parentObject->image = $this->filename; $parentObject->save(); } $this->save(); return true; } } catch (\Zbase\Exceptions\RuntimeException $e) { if (zbase_is_dev()) { dd($e); } zbase_abort(500); } return false; }
/** * Log to File * @param string $msg the mssg to write * @param string $type Type of Log * @param string $logFile the file to write the log * @param string|Entity The entity to save the log * @return null */ function zbase_log($msg, $type = null, $logFile = null, $entity = null) { if (!empty($msg) && is_array($msg)) { $msg = implode(PHP_EOL, $msg); } if (!empty($entity)) { if (!$entity instanceof \Zbase\Interfaces\EntityInterface) { $entity = zbase_entity($entity); } if ($entity instanceof \Zbase\Interfaces\EntityLogInterface) { $options = []; zbase_entity($entity)->log($msg, $type, $options); return; } } $folder = zbase_storage_path() . '/logs/' . date('Y/m/d/'); zbase_directory_check($folder, true); $file = !empty($logFile) ? $logFile : 'log.txt'; $file = str_replace(array('/', '\\', ':'), '_', $file); if (preg_match('/.txt/', $file) == 0) { $file .= '.txt'; } $msg = date('Y-m-d H:i:s') . ' : ' . zbase_ip() . PHP_EOL . $msg . PHP_EOL . "--------------------" . PHP_EOL; file_put_contents($folder . $file, $msg . PHP_EOL, FILE_APPEND); }
/** * Upload a file for this node * @param string $index The Upload file name/index or the URL to file to download and save * @return void */ public function uploadNodeFile($index = 'file') { try { $folder = zbase_storage_path() . '/' . zbase_tag() . '/' . static::$nodeNamePrefix . '_category' . '/' . $this->id() . '/'; zbase_directory_check($folder, true); if (!empty($_FILES[$index]['name'])) { $filename = $this->alphaId(); //zbase_file_name_from_file($_FILES[$index]['name'], time(), true); $uploadedFile = zbase_file_upload_image($index, $folder, $filename, zbase_config_get('node.files.image.format', 'png')); } if (!empty($uploadedFile) && zbase_file_exists($uploadedFile)) { $filename = basename($uploadedFile); $this->setDataOption('avatar', $filename); $this->save(); } } catch (\Zbase\Exceptions\RuntimeException $e) { if (zbase_is_dev()) { dd($e); } zbase_abort(500); } }
/** * Upload a file for this node * @param string $index The Upload file name/index or the URL to file to download and save * @return void */ public function uploadNodeFile($index = 'file') { try { $defaultImageFormat = zbase_config_get('node.files.image.format', 'png'); $folder = zbase_storage_path() . '/' . zbase_tag() . '/' . static::$nodeNamePrefix . '/' . $this->id() . '/'; zbase_directory_check($folder, true); $nodeFileObject = zbase_entity(static::$nodeNamePrefix . '_files', [], true); $nodeFiles = $this->files()->get(); if (preg_match('/http\\:/', $index) || preg_match('/https\\:/', $index)) { // File given is a URL if ($nodeFileObject->isUrlToFile()) { $filename = zbase_file_name_from_file(basename($index), time(), true); $uploadedFile = zbase_file_download_from_url($index, $folder . $filename); } else { $nodeFiles = $this->files()->get(); $nodeFileObject->is_primary = empty($nodeFiles) ? 1 : 0; $nodeFileObject->status = 2; $nodeFileObject->mimetype = null; $nodeFileObject->size = null; $nodeFileObject->filename = null; $nodeFileObject->url = $index; $nodeFileObject->node_id = $this->id(); $nodeFileObject->save(); $this->files()->save($nodeFileObject); return $nodeFileObject; } } if (zbase_file_exists($index)) { $uploadedFile = $index; $filename = basename($index); } if (!empty($_FILES[$index]['name'])) { $filename = zbase_file_name_from_file($_FILES[$index]['name'], time(), true); $uploadedFile = zbase_file_upload_image($index, $folder, $filename, $defaultImageFormat); } if (!empty($uploadedFile) && zbase_file_exists($uploadedFile)) { $nodeFileObject->is_primary = empty($nodeFiles) ? 1 : 0; $nodeFileObject->status = 2; $nodeFileObject->mimetype = zbase_file_mime_type($uploadedFile); $nodeFileObject->size = zbase_file_size($uploadedFile); $nodeFileObject->filename = basename($uploadedFile); $nodeFileObject->node_id = $this->id(); $nodeFileObject->save(); $this->files()->save($nodeFileObject); return $nodeFileObject; } } catch (\Zbase\Exceptions\RuntimeException $e) { if (zbase_is_dev()) { dd($e); } zbase_abort(500); } }
/** * Send Order pHoto to Shane */ public function sendOrderToShane() { $token = zbase_config_get('zivsluck.telegram.bot.token'); $shane = zbase_config_get('zivsluck.telegram.shane'); $enable = env('ZIVSLUCK_TELEGRAM', zbase_config_get('zivsluck.telegram.enable', false)); if ($enable) { $folder = zbase_storage_path() . '/zivsluck/var/'; zbase_directory_check($folder, true); $orderUrl = $this->imageSrc(); $image = $folder . $this->id() . '.png'; file_put_contents($image, file_get_contents($orderUrl)); $url = 'https://api.telegram.org/bot' . $token . '/sendPhoto?chat_id=' . $shane; $post_fields = array('chat_id' => $shane, 'photo' => new \CURLFile(realpath($image)), 'caption' => 'New Order from ' . $this->name . ' with Order ID: ' . $this->maskedId() . ' Date Ordered: ' . $this->created_at->format('F d, Y, h:i:s A')); $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data")); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); curl_exec($ch); /** * Send the dealer Image */ zbase_directory_check($folder, true); $orderUrl = $this->imageSrc('dealer'); $image = $folder . $this->id() . '.png'; file_put_contents($image, file_get_contents($orderUrl)); $url = 'https://api.telegram.org/bot' . $token . '/sendPhoto?chat_id=' . $shane; $post_fields = array('chat_id' => $shane, 'photo' => new \CURLFile(realpath($image)), 'caption' => 'Dealer\'s copy for Order from ' . $this->name . ' with Order ID: ' . $this->maskedId() . ' Date Ordered: ' . $this->created_at->format('F d, Y, h:i:s A')); $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data")); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); curl_exec($ch); } }
/** * */ public function loadWidgetFrom($path) { if (zbase_directory_check($path)) { $files = zbase_directory_files($path); if (!empty($files)) { foreach ($files as $file) { $widget = (require $file); if (empty($widget['id'])) { $name = str_replace('.php', '', basename($file)); $widget['id'] = $name; } if (!empty($widget['type']) && !is_array($widget['type']) && strtolower($widget['type']) == 'placeholder') { $this->widget($name, $widget); } else { $this->widgets[$name] = $widget; } } } } }
/** * Post Upload a new File * @param type $fileIndex */ public function postUploadFile($fileIndex = 'file', $caption = null) { try { if (!$this->postFileEnabled()) { throw new \Zbase\Exceptions\ConfigNotFoundException('Uploading is disabled for ' . $this->postTableName()); } $defaultImageFormat = zbase_config_get('post.files.image.format', 'png'); $folder = $this->postFileUploadFolder(); zbase_directory_check($folder, true); /** * Check if we have a URL given * @TODO save image that are from remote URL */ if (preg_match('/http\\:/', $fileIndex) || preg_match('/https\\:/', $fileIndex)) { return; } if (!empty($_FILES[$fileIndex]['name'])) { $filename = zbase_file_name_from_file($_FILES[$fileIndex]['name'], time(), true); $uploadedFile = zbase_file_upload_image($fileIndex, $folder, $filename, $defaultImageFormat); } if (!empty($uploadedFile) && zbase_file_exists($uploadedFile)) { /** * No Table, let's use the option column * to save the image link */ if (!$this->postFileHasTable()) { $file = new \stdClass(); } if (!empty($file)) { $file->is_primary = 0; $file->status = 2; $file->mimetype = zbase_file_mime_type($uploadedFile); $file->size = zbase_file_size($uploadedFile); $file->filename = basename($uploadedFile); $file->post_id = $this->postId(); $file->caption = $caption; return $this->postFileAdd($file); } } } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_exception_throw($e); return false; } }
/** * Upload a file for this node * @param string $index The Upload file name/index or the URL to file to download and save * @return void */ public function uploadProfileImage($index = 'file') { try { $folder = $this->profileFolder(); zbase_directory_check($folder, true); $filename = md5($this->alphaId() . time()); $uploadedFile = zbase_file_upload_image($index, $folder, $filename, zbase_config_get('node.files.image.format', 'png')); if (!empty($uploadedFile) && zbase_file_exists($uploadedFile)) { if (file_exists($folder . $this->avatar)) { unlink($folder . $this->avatar); } return basename($uploadedFile); } } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_exception_throw($e); } }
/** * Serve file/image publicly * Will create a file in the public folder and serve the public file * @param string $path Path to file original * @param int $width * @param int $height * @param int $quality * @param boolean $download * * @return array [src => '',size => '', mime => ''] */ function zbase_file_serve_image($path, $width, $height, $quality = 80, $download = false) { if (file_exists($path)) { $info = $path !== null ? getimagesize($path) : getimagesizefromstring($path); zbase_directory_check(zbase_storage_path('tmp/images'), true); $newFile = zbase_storage_path('tmp/images') . '/' . md5(basename($path) . '_' . $width . 'x' . $height); switch ($info[2]) { case IMAGETYPE_JPEG: $newFile = $newFile . '.jpg'; break; case IMAGETYPE_GIF: $newFile = $newFile . '.gif'; break; case IMAGETYPE_PNG: $newFile = $newFile . '.png'; break; default: return false; } if (!file_exists($newFile)) { copy($path, $newFile); zbase_file_image_resize($newFile, null, $width, $height, true, 'file', true, false, $quality); } $mime = image_type_to_mime_type($info[2]); $size = filesize($newFile); return ['src' => $newFile, 'size' => $size, 'mime' => $mime]; } }