Exemple #1
0
	function chapter_get() {
		if (!$this->get('id') || !is_numeric($this->get('id'))) {
			$this->response(NULL, 400);
		}

		$chapter = new Chapter();
		$chapter->where('id', $this->get('id'))->limit(1)->get();

		if ($chapter->result_count() == 1) {
			$chapter->get_comic();
			$chapter->get_teams();

			$result = array();
			$result['comic'] = $chapter->comic->to_array();
			$result['chapter'] = $chapter->to_array();
			$result['teams'] = array();
			foreach ($chapter->teams as $team) {
				$result['teams'][] = $team->to_array();
			}
			$result['pages'] = $chapter->get_pages();


			$this->response($result, 200); // 200 being the HTTP response code
		} else {
			$this->response(array('error' => _('Chapter could not be found')), 404);
		}
	}
Exemple #2
0
 /**
  * Creates a compressed cache file for the chapter
  *
  * @author Woxxy
  * @return url to compressed file
  */
 function compress($comic, $language = 'en', $volume = null, $chapter = null, $subchapter = 0)
 {
     require_once FCPATH . 'assets/pclzip/pclzip.lib.php';
     $files = array();
     if (get_setting('fs_dl_volume_enabled') && $volume !== null && $chapter === null) {
         if ($volume == 0) {
             show_404();
         }
         $chapters = new Chapter();
         $chapters->where('comic_id', $comic->id)->where('volume', $volume)->order_by('volume', 'asc')->order_by('chapter', 'asc')->order_by('subchapter', 'asc')->get();
         if ($chapters->result_count() == 0) {
             show_404();
         }
         $volume_id = $volume;
         $chapter_id = $chapters->id;
         $filepath = $comic->directory();
         $filename = $this->filename_chapters_compressed($chapters);
         foreach ($chapters as $chaptere) {
             $pages = new Page();
             $pages->where('chapter_id', $chaptere->id)->get();
             foreach ($pages as $page) {
                 $files[] = array(PCLZIP_ATT_FILE_NAME => 'content/comics/' . $comic->directory() . '/' . $chaptere->directory() . '/' . $page->filename, PCLZIP_ATT_FILE_NEW_FULL_NAME => $this->filename_chapter_compressed($chaptere) . '/' . $page->filename);
             }
         }
     } else {
         $chaptere = new Chapter();
         $chaptere->where('comic_id', $comic->id)->where('language', $language)->where('volume', $volume)->where('chapter', $chapter)->where('subchapter', $subchapter);
         $chaptere->get();
         if ($chaptere->result_count() == 0) {
             show_404();
         }
         $volume_id = 0;
         $chapter_id = $chaptere->id;
         $filepath = $comic->directory() . '/' . $chaptere->directory();
         $filename = $this->filename_chapter_compressed($chaptere);
         $pages = new Page();
         $pages->where('chapter_id', $chaptere->id)->get();
         foreach ($pages as $page) {
             $files[] = 'content/comics/' . $comic->directory() . '/' . $chaptere->directory() . '/' . $page->filename;
         }
     }
     $this->where('comic_id', $comic->id)->where('volume_id', $volume_id)->where('chapter_id', $chapter_id)->get();
     if ($this->result_count() == 0 || !file_exists('content/comics/' . $filepath . '/' . $this->filename)) {
         $this->remove_old();
         $archive = new PclZip('content/comics/' . $filepath . '/' . $filename . '.zip');
         $archive->create($files, PCLZIP_OPT_REMOVE_ALL_PATH, PCLZIP_OPT_NO_COMPRESSION);
         $this->comic_id = $comic->id;
         $this->volume_id = $volume_id;
         $this->chapter_id = $chapter_id;
         $this->filename = $filename . '.zip';
         $this->size = filesize('content/comics/' . $filepath . '/' . $filename . '.zip');
         $this->lastdownload = date('Y-m-d H:i:s', time());
         $this->save();
     } else {
         $this->lastdownload = date('Y-m-d H:i:s', time());
         $this->save();
     }
     return array("url" => site_url() . 'content/comics/' . $filepath . '/' . urlencode($this->filename), "server_path" => FCPATH . 'content/comics/' . $filepath . '/' . $this->filename);
 }
Exemple #3
0
	/**
	 * Handles both creating of new pages in the database and editing old ones.
	 * It determines if it should update or not by checking if $this->id has
	 * been set. It can get the values from both the $data array and direct 
	 * variable assignation. Be aware that array > variables. The latter ones
	 * will be overwritten. Particularly, the variables that the user isn't
	 * allowed to set personally are unset and reset with the automated values.
	 * It's quite safe to throw stuff at it.
	 * 
	 * If you're overwriting an image, at this point the ID would be alreasy set.
	 *
	 * @author	Woxxy
	 * @param	array $data contains the minimal data
	 * @return	boolean true on success, false on failure
	 */
	public function update_page_db($data = array()) {
		// Check if we're updating or creating a new entry by looking at $data["id"].
		// False is returned if the ID was not found.
		if (isset($data["id"])) {
			$this->where("id", $data["id"])->get();
			if ($chapter->result_count() == 0) {
				set_notice('error', _('There isn\'t a page in the database related to this ID.'));
				log_message('error', 'update_page_db: failed to find requested id');
				return false;
			}
		}
		else {
			// let's set the creator name if it's a new entry
			if (!isset($this->chapter_id)) {
				set_notice('error', _('There was no selected chapter.'));
				log_message('error', 'update_page_db: chapter_id was not set');
				return false;
			}

			// let's also check that the related comic is defined, and exists
			$chapter = new Chapter($this->chapter_id);
			if ($chapter->result_count() == 0) {
				set_notice('error', _('The selected chapter doesn\'t exist.'));
				log_message('error', 'update_page_db: chapter_id does not exist in comic database');
				return false;
			}

			$this->creator = $this->logged_id();
		}

		// always set the editor name
		$this->editor = $this->logged_id();

		// Unset sensible variables
		// Not even admins should touch these, for database stability.
		// Yeah, basically everything, as this function is fully automated
		unset($data["creator"]);
		unset($data["editor"]);
		unset($data["filename"]);
		unset($data["thumnail"]);
		unset($data["mime"]);
		unset($data["size"]);
		unset($data["height"]);
		unset($data["width"]);
		unset($data["thumbheight"]);
		unset($data["thumbwidth"]);
		unset($data["thumbsize"]);


		// Loop over the array and assign values to the variables.
		foreach ($data as $key => $value) {
			$this->$key = $value;
		}

		// let's save and give some error check. Push false if fail, true if good.
		$success = $this->save();
		if (!$success) {
			if (!$this->valid) {
				set_notice('error', _('Check that you have inputted all the required fields.'));
				log_message('error', 'update_page_db: failed validation');
			}
			else {
				set_notice('error', _('Failed to write to database for unknown reasons.'));
				log_message('error', 'update_page_db: failed to save');
			}
			return false;
		}
		else {
			// Good job!
			return true;
		}
	}
Exemple #4
0
	public function download($comic, $language = 'en', $volume = 0, $chapter = "", $subchapter = 0, $team = 0, $joint = 0, $pagetext = 'page', $page = 1) {
		if(!get_setting('fs_dl_enabled'))
			show_404();
		$comice = new Comic();
		$comice->where('stub', $comic)->get();
		if ($comice->result_count() == 0) {
			set_notice('warn', 'This comic doesn\'t exist.');
		}

		if ($chapter == "") {
			redirect('/reader/comic/' . $comic);
		}

		$chaptere = new Chapter();
		$chaptere->where('comic_id', $comice->id)->where('language', $language)->where('volume', $volume)->where('chapter', $chapter)->order_by('subchapter', 'ASC');

		if (!is_int($subchapter) && $subchapter == 'page') {
			$current_page = $team;
		}
		else {
			$chaptere->where('subchapter', $subchapter);

			if ($team == 'page')
				$current_page = $joint;
			else {
				if ($team != 0) {
					$teame = new Team();
					$teame->where('stub', $team)->get();
					$chaptere->where('team_id', $teame->id);
				}

				if ($joint == 'page')
					$current_page = $pagetext;

				if ($joint != 0) {
					$chaptere->where('joint_id', $joint);
				}
			}
		}

		if (!isset($current_page)) {
			if ($page != 1)
				$current_page = $page;
			else
				$current_page = 1;
		}

		$chaptere->get();
		if ($chaptere->result_count() == 0) {
			show_404();
		}
		
		$archive = new Archive();
		$url = $archive->compress($chaptere);
		redirect($url);
	}
Exemple #5
0
	/**
	 * Returns the URL of the reader for the next chapter
	 *
	 * @author	Woxxy
	 * @return	string the href to the next chapter
	 */
	public function next() {
		// If we've already used this function, it's ready for use, no need to calc it again
		if (isset($this->next))
			return $this->next;

		// Needs the comic
		$this->get_comic();
		$chapter = new Chapter();

		// Check if there are subchapters for this chapter.
		$chapter->where('comic_id', $this->comic->id)->where('volume', $this->volume)->where('chapter', $this->chapter)->where('language', $this->language)->having('subchapter >', $this->subchapter)->order_by('subchapter', 'asc')->limit(1)->get();
		if ($chapter->result_count() == 0) {
			// There aren't subchapters for this chapter. Then let's look for the next chapter
			$chapter = new Chapter();
			$chapter->where('comic_id', $this->comic->id)->where('volume', $this->volume)->having('chapter > ', $this->chapter)->where('language', $this->language)->order_by('chapter', 'asc')->order_by('subchapter', 'asc')->limit(1)->get();
			if ($chapter->result_count() == 0) {
				// Check if there's a chapter in the next volume.
				// This works even if chapter goes vol2 33 -> vol3 34 or vol2 33 -> vol3 1
				$chapter = new Chapter();
				$chapter->where('comic_id', $this->comic->id)->having('volume > ', $this->volume)->where('language', $this->language)->order_by('chapter', 'asc')->order_by('subchapter', 'asc')->limit(1)->get();
				if ($chapter->result_count() == 0) {
					// There's no next chapter. Redirect to the comic page.
					return site_url('/reader/read/' . $this->comic->stub);
				}
			}
		}

		// We do have a chapter or more. Get them.
		$chaptere = new Chapter();
		$chaptere->where('comic_id', $this->comic->id)->where('volume', $chapter->volume)->where('chapter', $chapter->chapter)->where('language', $this->language)->where('subchapter', $chapter->subchapter)->get();

		$done = false;
		// Do we have more than a next chapter? Make so it has the same teams on it.
		if ($chaptere->result_count() > 1) {
			foreach ($chaptere->all as $chap) {
				if ($chap->team_id == $this->team_id && $chap->joint_id == $this->joint_id) {
					$chapter = $chap;
					$done = true;
					break;
				}
			}
			// What if the teams changed, and the old teams stopped working on it? Get a different team.
			// There must be multiple teams on the next chapter for this to happen. Rare but happens.
			if (!$done) {
				/**
				 * @todo This is a pretty random way to select the next chapter version, needs refinement.
				 */
				$chapter = $chaptere->all['0'];
			}
		}
		// There's only one chapter, simply use it.
		else {
			$chapter = $chaptere;
		}

		// This is a heavy function. Let's play it smart and cache the value.
		// Send to the href function that returns a nice URL.
		$this->next = $chapter->href();

		// finally, return the URL.
		return $this->next;
	}
Exemple #6
0
 /**
  * Returns the chapter
  * 
  * Available filters: id (required)
  *
  * @author Woxxy
  */
 function chapter_get()
 {
     if ($this->get('comic_stub') || is_numeric($this->get('comic_id')) || is_numeric($this->get('volume')) || is_numeric($this->get('chapter')) || is_numeric($this->get('subchapter')) || is_numeric($this->get('team_id')) || is_numeric($this->get('joint_id'))) {
         $chapter = new Chapter();
         if ($this->get('comic_stub')) {
             $chapter->where_related('comic', 'stub', $this->get('comic_stub'));
         }
         // this mess is a complete search system through integers!
         if (is_numeric($this->get('comic_id'))) {
             $chapter->where('comic_id', $this->get('comic_id'));
         }
         if (is_numeric($this->get('volume'))) {
             $chapter->where('volume', $this->get('volume'));
         }
         if (is_numeric($this->get('chapter'))) {
             $chapter->where('chapter', $this->get('chapter'));
         }
         if (is_numeric($this->get('subchapter'))) {
             $chapter->where('subchapter', $this->get('subchapter'));
         }
         if (is_numeric($this->get('team_id'))) {
             $chapter->where('team_id', $this->get('team_id'));
         }
         if (is_numeric($this->get('joint_id'))) {
             $chapter->where('joint_id', $this->get('joint_id'));
         }
         // and we'll still give only one result
         $chapter->limit(1)->get();
     } else {
         // check that the id is at least a valid number
         $this->_check_id();
         $chapter = new Chapter();
         // get the single chapter by id
         $chapter->where('id', $this->get('id'))->limit(1)->get();
     }
     if ($chapter->result_count() == 1) {
         $chapter->get_comic();
         $chapter->get_teams();
         // the pretty array gets pages too: [comic][chapter][teams][pages]
         $result = array();
         $result['comic'] = $chapter->comic->to_array();
         $result['chapter'] = $chapter->to_array();
         $result['teams'] = array();
         foreach ($chapter->teams as $team) {
             $result['teams'][] = $team->to_array();
         }
         // this time we get the pages
         $result['pages'] = $chapter->get_pages();
         // all good
         $this->response($result, 200);
         // 200 being the HTTP response code
     } else {
         // the chapter with that id doesn't exist
         $this->response(array('error' => _('Chapter could not be found')), 404);
     }
 }
Exemple #7
0
 public function check($repair = FALSE, $recursive = FALSE)
 {
     $dir = "content/comics/" . $this->directory() . "/";
     $errors = array();
     if (!is_dir($dir)) {
         $errors[] = 'comic_directory_not_found';
         set_notice('warning', _('No directory found for:') . ' ' . $this->name . ' (' . $this->directory() . ')');
         log_message('debug', 'check: comic directory missing at ' . $dir);
         if ($repair) {
             // the best we can do is removing the database entry
             $this->remove_comic_db();
         }
     } else {
         // check that there are no unidentified files in the comic folder
         $map = directory_map($dir, 1);
         foreach ($map as $key => $item) {
             $item_path = $dir . $item;
             if (is_dir($item_path)) {
                 // gotta split the directory to get stub and uniqid
                 $item_arr = explode('_', $item);
                 $uniqid = end($item_arr);
                 $stub = str_replace('_' . $uniqid, '', $item);
                 $chapter = new Chapter();
                 $chapter->where('stub', $stub)->where('uniqid', $uniqid)->get();
                 if ($chapter->result_count() == 0) {
                     $errors[] = 'comic_unidentified_directory_found';
                     set_notice('warning', _('Unidentified directory found at:') . ' ' . $item_path);
                     log_message('debug', 'check: unidentified directory found at ' . $item_path);
                     if ($repair) {
                         // you have to remove all the files in the folder first
                         delete_files($item_path, TRUE);
                         rmdir($item_path);
                     }
                 }
             } else {
                 if ($item != $this->thumbnail && $item != 'thumb_' . $this->thumbnail) {
                     $ext = strtolower(substr($item, -4));
                     if (in_array($ext, array('.zip'))) {
                         $archive = new Archive();
                         $archive->where('comic_id', $this->id)->where('filename', $item)->get();
                         if ($archive->result_count()) {
                             continue;
                         }
                     }
                     // if it's not the thumbnail image, it's an unidentified file
                     $errors[] = 'comic_unidentified_file_found';
                     set_notice('warning', _('Unidentified file found at:') . ' ' . $item_path);
                     log_message('debug', 'check: unidentified file found at ' . $item_path);
                     if ($repair) {
                         unlink($item_path);
                     }
                 }
             }
         }
     }
     return $errors;
 }
Exemple #8
0
 function tools_check_library()
 {
     $type = $this->input->post('type');
     if ($type != 'page' && $type != 'chapter') {
         show_404();
     }
     $page = $this->input->post('page');
     if (!is_numeric($page)) {
         show_404();
     }
     $repair = FALSE;
     if ($this->input->post('repair') == 'repair') {
         $repair = TRUE;
     }
     if ($type == 'page') {
         $count = 300;
         if ($repair) {
             $count = 50;
         }
         $items = new Page();
     }
     if ($type == 'chapter') {
         $count = 15;
         if ($repair) {
             $count = 2;
         }
         $items = new Chapter();
     }
     $offset = $page * $count - $count;
     $items->limit($count, $offset)->get_iterated();
     if ($items->result_count() == 0) {
         if ($type == 'chapter') {
             $pages = new Page();
             $pages_count = $pages->count();
             $this->output->set_output(json_encode(array('status' => 'done', 'pages_count' => $pages_count)));
         } else {
             $this->output->set_output(json_encode(array('status' => 'done')));
         }
         return TRUE;
     }
     foreach ($items as $item) {
         $item->check($repair);
     }
     $warnings = array();
     foreach ($this->notices as $notice) {
         if ($notice['type'] == 'error') {
             if (!$this->input->is_cli_request()) {
                 $this->output->set_output(json_encode(array('status' => 'error', 'message' => $notice['message'])));
             }
             return FALSE;
         }
         if ($notice['type'] == 'warning') {
             $warnings[] = $notice['message'];
         }
     }
     $this->output->set_output(json_encode(array('status' => count($warnings) > 0 ? 'warning' : 'success', 'messages' => $warnings, 'processed' => $items->result_count())));
 }
Exemple #9
0
 public function read($comic, $language = 'en', $volume = 0, $chapter = "", $subchapter = 0, $team = 0, $joint = 0, $pagetext = 'page', $page = 1)
 {
     $comice = new Comic();
     $comice->where('stub', $comic)->get();
     if ($comice->result_count() == 0) {
         set_notice('warn', 'This comic doesn\'t exist.');
     }
     if ($chapter == "") {
         redirect('series/' . $comic);
     }
     if (!$this->_check_adult($comice)) {
         // or this function won't stop
         return FALSE;
     }
     $chaptere = new Chapter();
     $chaptere->where('comic_id', $comice->id)->where('language', $language)->where('volume', $volume)->where('chapter', $chapter)->order_by('subchapter', 'ASC');
     if (!is_int($subchapter) && $subchapter == 'page') {
         $current_page = $team;
     } else {
         $chaptere->where('subchapter', $subchapter);
         if ($team == 'page') {
             $current_page = $joint;
         } else {
             if ($team != 0) {
                 $teame = new Team();
                 $teame->where('stub', $team)->get();
                 $chaptere->where('team_id', $teame->id);
             }
             if ($joint == 'page') {
                 $current_page = $pagetext;
             }
             if ($joint != 0) {
                 $chaptere->where('joint_id', $joint);
             }
         }
     }
     if (!isset($current_page)) {
         if ($page != 1) {
             $current_page = $page;
         } else {
             $current_page = 1;
         }
     }
     $chaptere->get();
     if ($chaptere->result_count() == 0) {
         show_404();
     }
     $pages = $chaptere->get_pages();
     foreach ($pages as $page) {
         unset($page["object"]);
     }
     $next_chapter = $chaptere->next();
     if ($current_page > count($pages)) {
         redirect($next_chapter);
     }
     if ($current_page < 1) {
         $current_page = 1;
     }
     $chapters = new Chapter();
     $chapters->where('comic_id', $comice->id)->order_by('volume', 'desc')->order_by('chapter', 'desc')->order_by('subchapter', 'desc')->get_bulk();
     $comics = new Comic();
     $comics->order_by('name', 'ASC')->limit(100)->get();
     $this->template->set('is_reader', TRUE);
     $this->template->set('comic', $comice);
     $this->template->set('chapter', $chaptere);
     $this->template->set('chapters', $chapters);
     $this->template->set('comics', $comics);
     $this->template->set('current_page', $current_page);
     $this->template->set('pages', $pages);
     $this->template->set('next_chapter', $next_chapter);
     $this->template->title($comice->name, _('Chapter') . ' ' . $chaptere->chapter, get_setting('fs_gen_site_title'));
     switch ($comice->format) {
         case 1:
             $format = 'readtoon';
             break;
         default:
             $format = 'read';
     }
     $this->template->build($format);
 }