Example #1
0
 /**
  * proccess_request
  * Process the request for the public area
  */
 private function proccess_request()
 {
     global $lang;
     $this->template = 404;
     // Default template
     $this->pages = $this->get_pages();
     // get theme pages
     // Prepare te request array to use the legacy request (?v=file.ext)
     if (check_value($_GET['v']) && preg_match("/^\\w*\\.jpg|png|gif\$/", $_GET['v'])) {
         $this->base_request = '?' . $this->request_array[1];
         unset($this->request_array[1]);
     }
     @session_start();
     if (count($_SESSION['ImagesUp']) > 0) {
         $_SESSION['ImagesUp'] = array_values($_SESSION['ImagesUp']);
         self::$uploaded = true;
     }
     if (chevereto_config('maintenance')) {
         $this->base_request = 'maintenance';
     }
     // Switch according the request
     switch ($this->base_request) {
         case '':
         case 'index.php':
             @session_start();
             $_SESSION['last_upload_request'] = time();
             $this->template = 'index';
             break;
         case 'json':
             json_prepare();
             // Do a special trick for the json action=login
             if ($_REQUEST['action'] == 'login') {
                 // Check for user match...
                 $login_user = login_user($_REQUEST['password'], $_REQUEST['keep']);
                 if ($login_user !== false) {
                     $json_array = array('status_code' => 200, 'status_txt' => 'logged in');
                 } else {
                     $json_array = array('status_code' => 403, 'status_txt' => 'invalid login');
                 }
             } elseif ($_REQUEST['action'] == 'logout') {
                 do_logout();
                 $json_array = array('status_code' => 200, 'status_txt' => 'logged out');
             }
             $json_array = check_value($json_array) ? $json_array : array('status' => 403, 'status_txt' => 'unauthorized');
             session_write_close();
             die(json_output($json_array));
             break;
         case __CHV_VIRTUALFOLDER_IMAGE__:
             // View request
             $id_public = $this->request_array[1];
             $this->template = !is_upload_result() ? 'view' : 'uploaded';
             self::$is_viewer = true;
             break;
         case __CHV_VIRTUALFOLDER_UPLOADED__:
             @session_start();
             if (count($_SESSION['ImagesUp']) > 0) {
                 $this->template = 'uploaded';
                 self::$doctitle = $lang['doctitle_upload_complete'];
             } else {
                 $this->redirect(__CHV_BASE_URL__, 400);
             }
             break;
         case 'error-javascript':
             chevereto_die(array(get_lang_txt('critical_js_step_1'), get_lang_txt('critical_js_step_2')), 'JavaScript', array(get_lang_txt('critical_js')));
             break;
         case '?chevereto':
             $this->template = 'bool';
             break;
             // Legacy viewer
         // Legacy viewer
         case '?v=' . $_GET['v']:
             // View request
             $id_public = $_GET['v'];
             $this->legacy_redirect = true;
             break;
         case 'delete':
         case 'delete-confirm':
             //$delete_what = $this->request_array[1];
             $id_public = $this->request_array[2];
             $deleteHash = $this->request_array[3];
             $this->template = $this->base_request;
             self::$is_viewer = true;
             break;
         case 'maintenance':
             $this->template = 'maintenance';
             self::$doctitle = chevereto_config('doctitle');
             break;
         default:
             // Pages request
             require_once $this->path_theme . 'pages/pages_config.php';
             // We load the special pages config
             if (in_array($this->base_request . '.php', $this->pages) and $this->request_array[1] == '' and $pages_config[$this->base_request]['live']) {
                 $this->template = 'pages/' . $this->base_request;
                 self::$doctitle = $pages_config[$this->base_request]['title'];
             } else {
                 $this->template = 'shorturl';
                 $id_public = $this->base_request;
                 self::$is_viewer = true;
             }
             break;
     }
     // Ask for the login on index and pages
     if ($this->template == 'index' || $this->template == 'pages/' . $this->base_request) {
         if (conditional_config('private_mode')) {
             if (!is_logged_user()) {
                 $doctitle = get_lang_txt('txt_enter_password') . ' - ' . chevereto_config('doctitle');
                 include __CHV_PATH_SYSTEM__ . 'login.php';
                 die;
             }
         }
     }
     if ($this->template == 'uploaded') {
         self::$doctitle = get_lang_txt('doctitle_upload_complete');
         self::$image_info = $_SESSION['ImagesUp'][0];
         self::$uploaded_images = $_SESSION['ImagesUp'];
         $_SESSION['ImagesUp'] = NULL;
         unset($_SESSION['ImagesUp']);
     }
     if (preg_match('/view|shorturl|delete/', $this->template) || $this->legacy_redirect) {
         // Test connection
         if ($this->dB->dead) {
             self::$doctitle = 'dB connection error';
             $this->template = 404;
         } else {
             // get image info
             $imageID = $this->legacy_redirect ? $id_public : decodeID($id_public);
             self::$image_info = $this->dB->image_info($imageID);
             self::$id_public = $id_public;
             if (!is_array(self::$image_info)) {
                 // Record?
                 if ($this->template == 'delete-confirm') {
                     json_output(array('status_code' => 403, 'status_txt' => 'target image doesn\'t exists'));
                 } else {
                     $this->template = 404;
                 }
             } else {
                 if ($this->legacy_redirect) {
                     $this->redirect(__CHV_BASE_URL__ . __CHV_VIRTUALFOLDER_IMAGE__ . '/' . encodeID(self::$image_info['image_id']), 301);
                 }
                 $target = get_image_target(self::$image_info);
                 self::$image_target = $target['image_path'];
                 self::$image_thumb_target = $target['image_thumb_path'];
                 self::$image_url = absolute_to_url($target['image_path']);
                 self::$image_thumb_url = absolute_to_url($target['image_thumb_path']);
                 self::$image_filename = self::$image_info['image_filename'];
                 self::$image_viewer = __CHV_BASE_URL__ . __CHV_VIRTUALFOLDER_IMAGE__ . '/' . $id_public;
                 self::$delete_image_url = __CHV_BASE_URL__ . 'delete/image/' . self::$id_public . '/' . self::$image_info['image_delete_hash'];
                 $image_delete_proceed = !empty(self::$image_info['image_delete_hash']) && $deleteHash === self::$image_info['image_delete_hash'] ? true : false;
                 switch ($this->template) {
                     case 'delete':
                         if (!$image_delete_proceed) {
                             $this->redirect(__CHV_BASE_URL__ . __CHV_VIRTUALFOLDER_IMAGE__ . '/' . self::$id_public, 301);
                         }
                         self::$delete_image_confirm_url = __CHV_BASE_URL__ . 'delete-confirm/image/' . self::$id_public . '/' . self::$image_info['image_delete_hash'];
                         self::$doctitle = get_lang_txt('doctitle_delete_confirm') . ' ' . self::$image_info['image_filename'];
                         break;
                     case 'delete-confirm':
                         if (!$image_delete_proceed) {
                             json_output(array('status_code' => 403, 'status_txt' => 'invalid delete hash'));
                         } else {
                             require_once __CHV_PATH_ADMIN_CLASSES__ . 'class.manage.php';
                             $manage = new Manage(array('id' => self::$image_info['image_id'], 'action' => 'delete'));
                             if ($manage->dead) {
                                 $json_array = array('status_code' => 403, 'status_txt' => $manage->error);
                             } else {
                                 $json_array = $manage->process();
                             }
                         }
                         // Make the status_txt more readable...
                         switch ($json_array['status_code']) {
                             case 200:
                                 $json_array['status_txt'] = get_lang_txt('txt_image_deleted');
                                 break;
                             default:
                             case 403:
                                 $json_array['status_txt'] = get_lang_txt('txt_error_deleting_image');
                                 break;
                         }
                         json_output($json_array);
                         break;
                     default:
                         self::$doctitle = get_lang_txt('doctitle_viewing_image') . ' ' . self::$image_info['image_filename'];
                         break;
                 }
             }
         }
     }
     if ($this->template == 404) {
         status_header(404);
         self::$doctitle = check_value(self::$doctitle) ? self::$doctitle : get_lang_txt('txt_404_title');
     } else {
         status_header(200);
     }
     // We load the template
     if ($this->template == 'bool') {
         exit(json_encode(true));
     } else {
         $this->load_template();
     }
 }
Example #2
0
function array_merge_minified($array, $minified, $create_missing_files = true)
{
    if (conditional_config('minify')) {
        $minified = array_map('minify_name', $minified);
    }
    return array_merge($array, $minified);
}
Example #3
0
 /**
  * process
  * Does the thing
  *
  * @param	string
  * @return	mixed
  */
 public function process()
 {
     if ($this->valid_data()) {
         $this->extension = $this->get_true_extension($this->mime);
         if ($this->extension == 'bmp') {
             require_once 'class.imageconvert.php';
             $this->ImageConvert = new ImageConvert($this->working, $this->extension, $this->img_upload_path . 'temp_' . generateRandomString(256));
             unset($this->working);
             unset($this->extension);
             $this->working = $this->ImageConvert->out;
             $this->extension = 'png';
         }
         switch ($this->storage) {
             case 'direct':
                 $this->img_upload_path = __CHV_PATH_IMAGES__;
                 break;
             case 'datefolder':
             case 'datefolders':
             default:
                 // Sets the date folder YYYY/MM/DD
                 $datefolder = $this->img_upload_path . date('Y/m/d/');
                 $old_umask = umask(0);
                 if (!file_exists($datefolder) && !@mkdir($datefolder, 0755, true)) {
                     $this->error = "Unable to create upload folder";
                     return false;
                 }
                 umask($old_umask);
                 $this->img_upload_path = $datefolder;
                 break;
         }
         $image_filename = $this->nameFile($this->img_upload_path, $this->extension, chevereto_config('file_naming'), $this->original_file_name);
         // Prepare and formats the temp image
         $formated_temp = $this->working . '.' . $this->extension;
         rename($this->working, $formated_temp);
         unset($this->working);
         $this->working = $formated_temp;
         // Call the resize class
         require_once 'class.imageresize.php';
         // Thumb
         $thumb_filename = str_replace($this->extension, 'th.' . $this->extension, $image_filename);
         $this->ThumbResize = new ImageResize($this->working, $thumb_filename, $this->thumb_width, $this->thumb_height, true);
         // Fixed width but fluid height? Replace the line above with this:
         // $this->ThumbResize = new ImageResize($this->working, $thumb_filename, $this->thumb_width);
         if (check_value($this->ThumbResize->error)) {
             $this->error = $this->ThumbResize->error . " (thumb)";
             return false;
         }
         // Resize?
         if (check_value($this->resize_width)) {
             $this->ImageResize = new ImageResize($this->working, $this->working, $this->resize_width);
             if (check_value($this->ImageResize->error)) {
                 $this->error = $this->ImageResize->error;
                 return false;
             }
         }
         if (!check_value($this->error)) {
             // Apply the watermark ?
             if (!is_animated_image($this->working) && conditional_config('watermark_enable') and chevereto_config('watermark_opacity') > 0) {
                 switch ($this->extension) {
                     case 'gif':
                         $src = imagecreatefromgif($this->working);
                         break;
                     case 'png':
                         $src = imagecreatefrompng($this->working);
                         break;
                     case 'jpg':
                         $src = imagecreatefromjpeg($this->working);
                         break;
                 }
                 $src_width = imagesx($src);
                 $src_height = imagesy($src);
                 $watermark_src = imagecreatefrompng(__CHV_WATERMARK_FILE__);
                 $watermark_width = imagesx($watermark_src);
                 $watermark_height = imagesy($watermark_src);
                 // Calculate the position
                 switch (chevereto_config('watermark_x_position')) {
                     case 'left':
                         $watermark_x = chevereto_config('watermark_margin');
                         break;
                     case 'center':
                         $watermark_x = $src_width / 2 - $watermark_width / 2;
                         break;
                     case 'right':
                         $watermark_x = $src_width - $watermark_width - chevereto_config('watermark_margin');
                         break;
                 }
                 switch (chevereto_config('watermark_y_position')) {
                     case 'top':
                         $watermark_y = chevereto_config('watermark_margin');
                         break;
                     case 'center':
                         $watermark_y = $src_height / 2 - $watermark_height / 2;
                         break;
                     case 'bottom':
                         $watermark_y = $src_height - $watermark_height - chevereto_config('watermark_margin');
                         break;
                 }
                 // Watermark has the same or greater size of the image ?
                 // --> Center the watermark
                 if ($watermark_width == $src_width && $watermark_height == $src_height) {
                     $watermark_x = $src_width / 2 - $watermark_width / 2;
                     $watermark_y = $src_height / 2 - $watermark_height / 2;
                 }
                 // Watermark is too big ?
                 // --> Fit the watermark on the image
                 if ($watermark_width > $src_width || $watermark_height > $src_height) {
                     // Watermark is wider than the image
                     if ($watermark_width > $src_width) {
                         $watermark_new_width = $src_width;
                         $watermark_new_height = $src_width * $watermark_height / $watermark_width;
                         if ($watermark_new_height > $src_height) {
                             $watermark_new_width = $src_height * $watermark_width / $watermark_height;
                             $watermark_new_height = $src_height;
                         }
                     } else {
                         $watermark_new_width = $src_height * $watermark_width / $watermark_height;
                         $watermark_new_height = $src_height;
                     }
                     $watermark_temp = $this->img_upload_path . 'temp_watermark_' . generateRandomString(64) . '.png';
                     $WatermarkResize = new ImageResize(__CHV_WATERMARK_FILE__, $watermark_temp, $watermark_new_width);
                     if (!check_value($WatermarkResize->error)) {
                         $watermark_width = $watermark_new_width;
                         $watermark_height = $watermark_new_height;
                         $watermark_src = imagecreatefrompng($watermark_temp);
                         $watermark_x = $src_width / 2 - $watermark_width / 2;
                         $watermark_y = $src_height / 2 - $watermark_height / 2;
                     }
                 }
                 // Apply and save the watermark
                 imagecopymerge_alpha($src, $watermark_src, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height, chevereto_config('watermark_opacity'), $this->extension);
                 switch ($this->extension) {
                     case 'gif':
                         imagegif($src, $this->working);
                         break;
                     case 'png':
                         imagepng($src, $this->working);
                         break;
                     case 'jpg':
                         imagejpeg($src, $this->working, 96);
                         break;
                 }
                 imagedestroy($src);
                 @unlink($watermark_temp);
             }
             // Move the temp to the final path...
             $uploaded = rename($this->working, $image_filename);
             // Change the CHMOD of the file (for some php enviroments)
             @chmod($image_filename, 0644);
             @chmod($thumb_filename, 0644);
             if ($uploaded) {
                 $info = get_info($image_filename);
                 $file_path = absolute_to_relative($image_filename);
                 $thumb_path = absolute_to_relative($thumb_filename);
                 $image_url = absolute_to_url($image_filename);
                 $name = str_replace('.' . $this->extension, '', str_replace($this->img_upload_path, '', $image_filename));
                 $this->image_info = array('image_name' => $name, 'image_filename' => $name . "." . $this->extension, 'image_type' => $this->extension, 'image_path' => $file_path, 'image_url' => $image_url, 'image_width' => $info['width'], 'image_height' => $info['height'], 'image_attr' => 'width="' . $info['width'] . '" height="' . $info['height'] . '"', 'image_bytes' => $info['bytes'], 'image_size' => $info['size'], 'image_thumb_url' => absolute_to_url($thumb_filename), 'image_thumb_path' => $thumb_path, 'image_thumb_width' => $this->thumb_width, 'image_thumb_height' => $this->thumb_height);
                 switch ($this->storage) {
                     case 'direct':
                         $this->image_info['storage_id'] = 2;
                         break;
                     case 'datefolder':
                     case 'datefolders':
                         $this->image_info['storage_id'] = NULL;
                         break;
                 }
                 // Shorthand the dB object
                 $dB = $this->dB;
                 if ($dB->dead) {
                     $this->error = $dB->error;
                     return false;
                 }
                 if ($dB->insert_file($this->image_info)) {
                     $image_delete_hash = $dB->image_delete_hash;
                     $this->image_info['image_id'] = $dB->last_insert_id();
                     $this->image_info['image_id_public'] = encodeID($this->image_info['image_id']);
                     $this->image_info['image_viewer'] = __CHV_BASE_URL__ . __CHV_VIRTUALFOLDER_IMAGE__ . '/' . $this->image_info['image_id_public'];
                     $this->image_info['image_shorturl'] = __CHV_BASE_URL__ . $this->image_info['image_id_public'];
                     $this->image_info['image_delete_hash'] = $image_delete_hash;
                     $this->image_info['image_delete_url'] = __CHV_BASE_URL__ . 'delete/image/' . $this->image_info['image_id_public'] . '/' . $image_delete_hash;
                     $this->image_info['image_delete_confirm_url'] = __CHV_BASE_URL__ . 'delete-confirm/image/' . $this->image_info['image_id_public'] . '/' . $image_delete_hash;
                     $this->image_info['image_date'] = date('Y-m-d H:i:s', time());
                     return true;
                 } else {
                     unlink($image_filename);
                     unlink($thumb_filename);
                     $this->error = $dB->error;
                     return false;
                 }
             } else {
                 unlink($this->working);
                 $this->error = 'error uploading';
                 return false;
             }
         } else {
             unlink($this->working);
             return false;
         }
     } else {
         // Invalid data
         return false;
     }
 }