Example #1
0
	/**
	 * Function to create a new page from a $filedata array. It deals with
	 * processing the image data, checks if it's an image, puts values like
	 * height in the variables, sends to database function and file function.
	 * 
	 * This function fails silently for the user. For now.
	 *
	 * @author	Woxxy
	 * @param	array|$filedata It's an array of data produced by CodeIgniter 
	 * 			upload function
	 * @param	int|$chapter_id The ID of the chapter
	 * @param	int|$hidden NOT USED
	 * @param	string|$description NOT USED
	 * @return	boolean true on success, false on failure.
	 */
	public function add_page($filedata, $chapter_id, $hidden = 0, $description = "") {

		// Check if that file is actually an image
		if (!$imagedata = @getimagesize($filedata["server_path"])) {
			log_message('error', 'add_page: uploaded file doesn\'t seem to be an image');
			return false;
		}

		// Let's set some variables
		$this->chapter_id = $chapter_id;

		// Load the chapter and comic as soon as possible.
		if (!$this->get_chapter()) {
			log_message('error', 'add_page: couldn\'t find related chapter');
			return false;
		}

		if ($hidden == 1)
			$this->hidden = 1; else
			$this->hidden = 0;
		$this->description = $description;

		// Decide if it should overwrite. Normally files get overwritten.
		$overwrite = $filedata["overwrite"];

		// Throw the image to the folder with this function
		// While we aren't looking, this also creates the thumbnails
		if (!$this->add_page_file($filedata)) {
			log_message('error', 'add_page: failed creating file');
			return false;
		}

		// We need the dir to the chapter
		$dir = "content/comics/" . $this->chapter->comic->directory() . "/" . $this->chapter->directory() . "/";


		// $imagedata = @getimagesize($filedata["server_path"]);
		// We had set $imagedata before
		// We already have the thumbnail! Get data for that too.
		$thumbdata = @getimagesize($dir . "thumb_" . $filedata["name"]);

		// If a page in this chapter with the same filename exists, pick its ID and start updating it
		// This makes so everything gets overwritten with no error
		$page = new Page();
		$page->where('chapter_id', $this->chapter_id)->where('filename', $filedata["name"])->get();
		if ($page->result_count() > 0) {
			$this->id = $page->id;
		}

		// Prepare the variables
		$this->filename = $filedata["name"];
		$this->thumbnail = "thumb_";
		$this->width = $imagedata["0"];
		$this->height = $imagedata["1"];
		$this->size = filesize($dir . $filedata["name"]);
		$this->mime = image_type_to_mime_type($imagedata["2"]);
		$this->thumbwidth = $thumbdata["0"];
		$this->thumbheight = $thumbdata["1"];
		$this->thumbsize = filesize($dir . "thumb_" . $filedata["name"]);

		// Check from the thumbnail if the image is in colors or not
		$is_bw = $this->is_bw();
		if ($is_bw == "bw")
			$this->grayscale = 1;
		else if ($is_bw == "rgb")
			$this->grayscale = 0;
		else {
			log_message('error', 'add_page: error while determining if black and white or RGB');
			return false;
		}

		// Finally, save everything to database, and, in case of failure, remove the image files
		if (!$this->update_page_db()) {
			log_message('error', 'add_page: failed writing to database');
			$this->remove_page_file();
			return false;
		}

		// All good
		return true;
	}
Example #2
0
 function check($repair = FALSE)
 {
     // make sure we got the comic
     if ($this->get_comic() === FALSE) {
         $errors[] = 'chapter_comic_entry_not_found';
         set_notice('warning', _('Found a chapter entry without a comic entry, Chapter ID: ' . $this->id));
         log_message('debug', 'check: chapter entry without comic entry');
         if ($repair) {
             $this->remove_chapter_db();
         }
         return FALSE;
     }
     $errors = array();
     // check if the directory exists at all
     $path = 'content/comics/' . $this->comic->directory() . '/' . $this->directory() . '/';
     if (!is_dir($path)) {
         $errors[] = 'chapter_directory_not_found';
         set_notice('warning', _('No directory found for:') . ' ' . $this->comic->name . ' > ' . $this->title());
         log_message('debug', 'check: chapter directory missing at ' . $path);
         // the folder doesn't exist, so get rid of the entry from database
         if ($repair) {
             $this->remove_chapter_db();
         }
         // there's no recovery from this, return the error codes
         return $errors;
     }
     // check if there are extraneous files in the folder
     $files = get_dir_file_info($path);
     foreach ($files as $key => $file) {
         // check that the file is writable
         if (!is_writable($file['relative_path'])) {
             // non writable files are horrendous, send a notice and stop the machines
             $errors[] = 'chapter_non_writable_file';
             set_notice('warning', _('Found non writable files in the comics folder. Check your files permissions.'));
             log_message('debug', 'check: non writable file: ' . $file['relative_path']);
             return $errors;
         }
         // get the extension
         $ext = strtolower(substr($file['name'], -4));
         if (in_array($ext, array('.zip'))) {
             // maybe it's just the zip created by the archive system
             $archives = new Archive();
             $archives->where('comic_id', $this->comic_id)->where('chapter_id', $this->id)->where('volume_id', 0)->get();
             if ($archives->result_count()) {
                 foreach ($archives as $archive) {
                     // we actually have an archive, but is it the same file?
                     if ($file['name'] == $archive->filename) {
                         // same file, unset to confirm
                         unset($files[$key]);
                         continue;
                     }
                 }
             }
         }
         if (in_array($ext, array('.png', '.jpg', 'jpeg', '.gif'))) {
             $page = new Page();
             $page->where('chapter_id', $this->id)->where('filename', $file['name'])->get();
             if ($page->result_count() == 1) {
                 // it's a simple page, unset to confirm
                 unset($files[$key]);
                 continue;
             }
         }
     }
     // now we have an array with files that don't belong here
     foreach ($files as $file) {
         $errors[] = 'chapter_unidentified_file';
         set_notice('warning', _('Unidentified file found in:') . ' ' . $this->comic->name . ' > ' . $this->title() . ': ' . $file['name']);
         log_message('debug', 'check: unidentified file ' . $file['relative_path'] . $file['name']);
         // repairing this means getting rid of extraneous files
         if ($repair) {
             // it's possible the file is not removeable
             if (is_writable($file['relative_path'] . $file['name'])) {
                 // the files SHOULD be writable, we checked it earlier
                 if (is_dir($file['relative_path'] . $file['name'])) {
                     delete_files($file['relative_path'] . $file['name']);
                     rmdir($file['relative_path'] . $file['name']);
                 } else {
                     unlink($file['relative_path'] . $file['name']);
                 }
             }
         }
     }
     // everything's been checked. The errors are in the set_notice system
     return $errors;
 }
Example #3
0
 /**
  * Function to create a new page from a $filedata array. It deals with
  * processing the image data, checks if it's an image, puts values like
  * height in the variables, sends to database function and file function.
  *
  * This function fails silently for the user. For now.
  *
  * @author	Woxxy
  * @param	array|$filedata It's an array of data produced by CodeIgniter
  * 			upload function
  * @param	int|$chapter_id The ID of the chapter
  * @param	int|$hidden NOT USED
  * @param	string|$description NOT USED
  * @return	boolean true on success, false on failure.
  */
 public function add_page($path, $filename, $chapter)
 {
     // Check if that file is actually an image
     if (!($imagedata = @getimagesize($path))) {
         log_message('error', 'add_page: uploaded file doesn\'t seem to be an image');
         return false;
     }
     // Let's set some variables
     $this->chapter_id = $chapter->id;
     // Load the chapter and comic as soon as possible.
     if (!$this->get_chapter()) {
         log_message('error', 'add_page: couldn\'t find related chapter');
         return false;
     }
     // Throw the image to the folder with this function
     // While we aren't looking, this also creates the thumbnails
     if (!$this->add_page_file($path, $filename)) {
         log_message('error', 'add_page: failed creating file');
         return false;
     }
     // We need the dir to the chapter
     $dir = "content/comics/" . $this->chapter->comic->directory() . "/" . $this->chapter->directory() . "/";
     // If a page in this chapter with the same filename exists, pick its ID and start updating it
     // This makes so everything gets overwritten with no error
     $page = new Page();
     $page->where('chapter_id', $this->chapter_id)->where('filename', $filename)->get();
     if ($page->result_count() > 0) {
         $this->id = $page->id;
     }
     // Prepare the variables
     $this->filename = $filename;
     $this->width = $imagedata["0"];
     $this->height = $imagedata["1"];
     $this->size = filesize($dir . $filename);
     $this->mime = image_type_to_mime_type($imagedata["2"]);
     // Finally, save everything to database, and, in case of failure, remove the image files
     if (!$this->update_page_db()) {
         log_message('error', 'add_page: failed writing to database');
         $this->remove_page_file();
         return false;
     }
     $this->on_change($chapter);
     // All good
     return true;
 }
Example #4
0
 function tools_optimize_thumbnails($howmany = NULL)
 {
     if (!isAjax()) {
         show_404();
     }
     if (!find_imagick()) {
         show_404();
     }
     $pages = new Page();
     if (is_null($howmany)) {
         $count = $pages->where('description', '')->count();
         $this->output->set_output(json_encode(array('count' => $count)));
         return TRUE;
     }
     if (is_numeric($howmany) && $howmany > 0) {
         $pages->where('description', '')->limit(10)->get();
         if ($pages->result_count() < 1) {
             $this->output->set_output(json_encode(array('status' => 'done')));
             return TRUE;
         }
         $warnings = array();
         foreach ($pages->all as $page) {
             if (!$page->rebuild_thumbnail()) {
                 $last_notice = end($this->notices);
                 if ($last_notice['type'] == 'warning') {
                     $warnings[] = $last_notice['message'];
                 }
                 $this->output->set_output(json_encode(array('error' => $this->notices)));
                 return FALSE;
             }
         }
         $this->output->set_output(json_encode(array('status' => 'success', 'warnings' => $warnings)));
         return TRUE;
     }
 }