seblucas /
cops
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * COPS (Calibre OPDS PHP Server) class file |
||
| 4 | * |
||
| 5 | * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) |
||
| 6 | * @author Sébastien Lucas <[email protected]> |
||
| 7 | */ |
||
| 8 | |||
| 9 | abstract class Base |
||
| 10 | { |
||
| 11 | const PAGE_INDEX = "index"; |
||
| 12 | const PAGE_ALL_AUTHORS = "1"; |
||
| 13 | const PAGE_AUTHORS_FIRST_LETTER = "2"; |
||
| 14 | const PAGE_AUTHOR_DETAIL = "3"; |
||
| 15 | const PAGE_ALL_BOOKS = "4"; |
||
| 16 | const PAGE_ALL_BOOKS_LETTER = "5"; |
||
| 17 | const PAGE_ALL_SERIES = "6"; |
||
| 18 | const PAGE_SERIE_DETAIL = "7"; |
||
| 19 | const PAGE_OPENSEARCH = "8"; |
||
| 20 | const PAGE_OPENSEARCH_QUERY = "9"; |
||
| 21 | const PAGE_ALL_RECENT_BOOKS = "10"; |
||
| 22 | const PAGE_ALL_TAGS = "11"; |
||
| 23 | const PAGE_TAG_DETAIL = "12"; |
||
| 24 | const PAGE_BOOK_DETAIL = "13"; |
||
| 25 | const PAGE_ALL_CUSTOMS = "14"; |
||
| 26 | const PAGE_CUSTOM_DETAIL = "15"; |
||
| 27 | const PAGE_ABOUT = "16"; |
||
| 28 | const PAGE_ALL_LANGUAGES = "17"; |
||
| 29 | const PAGE_LANGUAGE_DETAIL = "18"; |
||
| 30 | const PAGE_CUSTOMIZE = "19"; |
||
| 31 | const PAGE_ALL_PUBLISHERS = "20"; |
||
| 32 | const PAGE_PUBLISHER_DETAIL = "21"; |
||
| 33 | const PAGE_ALL_RATINGS = "22"; |
||
| 34 | const PAGE_RATING_DETAIL = "23"; |
||
| 35 | |||
| 36 | const COMPATIBILITY_XML_ALDIKO = "aldiko"; |
||
| 37 | |||
| 38 | private static $db = NULL; |
||
| 39 | |||
| 40 | 144 | public static function isMultipleDatabaseEnabled () { |
|
| 41 | 144 | global $config; |
|
| 42 | 144 | return is_array ($config['calibre_directory']); |
|
| 43 | } |
||
| 44 | |||
| 45 | 49 | public static function useAbsolutePath () { |
|
| 46 | 49 | global $config; |
|
| 47 | 49 | $path = self::getDbDirectory(); |
|
| 48 | 49 | return preg_match ('/^\//', $path) || // Linux / |
|
| 49 | 49 | preg_match ('/^\w\:/', $path); // Windows X: |
|
| 50 | } |
||
| 51 | |||
| 52 | 56 | public static function noDatabaseSelected () { |
|
| 53 | 56 | return self::isMultipleDatabaseEnabled () && is_null (GetUrlParam (DB)); |
|
| 54 | } |
||
| 55 | |||
| 56 | 4 | View Code Duplication | public static function getDbList () { |
| 57 | 4 | global $config; |
|
| 58 | 4 | if (self::isMultipleDatabaseEnabled ()) { |
|
| 59 | 4 | return $config['calibre_directory']; |
|
| 60 | } else { |
||
| 61 | 1 | return array ("" => $config['calibre_directory']); |
|
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | 5 | View Code Duplication | public static function getDbNameList () { |
| 66 | 5 | global $config; |
|
| 67 | 5 | if (self::isMultipleDatabaseEnabled ()) { |
|
| 68 | 5 | return array_keys ($config['calibre_directory']); |
|
| 69 | } else { |
||
| 70 | return array (""); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | 1 | View Code Duplication | public static function getDbName ($database = NULL) { |
| 75 | 1 | global $config; |
|
| 76 | 1 | if (self::isMultipleDatabaseEnabled ()) { |
|
| 77 | 1 | if (is_null ($database)) $database = GetUrlParam (DB, 0); |
|
| 78 | 1 | if (!is_null($database) && !preg_match('/^\d+$/', $database)) { |
|
| 79 | self::error ($database); |
||
| 80 | } |
||
| 81 | 1 | $array = array_keys ($config['calibre_directory']); |
|
| 82 | 1 | return $array[$database]; |
|
| 83 | } |
||
| 84 | return ""; |
||
| 85 | } |
||
| 86 | |||
| 87 | 126 | View Code Duplication | public static function getDbDirectory ($database = NULL) { |
| 88 | 126 | global $config; |
|
| 89 | 126 | if (self::isMultipleDatabaseEnabled ()) { |
|
| 90 | 9 | if (is_null ($database)) $database = GetUrlParam (DB, 0); |
|
| 91 | 9 | if (!is_null($database) && !preg_match('/^\d+$/', $database)) { |
|
| 92 | self::error ($database); |
||
| 93 | } |
||
| 94 | 9 | $array = array_values ($config['calibre_directory']); |
|
| 95 | 9 | return $array[$database]; |
|
| 96 | } |
||
| 97 | 117 | return $config['calibre_directory']; |
|
| 98 | } |
||
| 99 | |||
| 100 | // -DC- Add image directory |
||
| 101 | 98 | public static function getImgDirectory ($database = NULL) { |
|
| 102 | 98 | global $config; |
|
|
0 ignored issues
–
show
|
|||
| 103 | if (self::isMultipleDatabaseEnabled ()) { |
||
| 104 | if (is_null ($database)) $database = GetUrlParam (DB, 0); |
||
| 105 | 2 | $array = array_values ($config['image_directory']); |
|
| 106 | 2 | return $array[$database]; |
|
| 107 | } |
||
| 108 | return $config['image_directory']; |
||
| 109 | 2 | } |
|
| 110 | |||
| 111 | public static function getDbFileName ($database = NULL) { |
||
| 112 | 159 | return self::getDbDirectory ($database) .'metadata.db'; |
|
| 113 | 159 | } |
|
| 114 | |||
| 115 | 98 | private static function error ($database) { |
|
| 116 | 97 | if (php_sapi_name() != "cli") { |
|
| 117 | 97 | header("location: checkconfig.php?err=1"); |
|
| 118 | 97 | } |
|
| 119 | throw new Exception("Database <{$database}> not found."); |
||
| 120 | } |
||
| 121 | 98 | ||
| 122 | public static function getDb ($database = NULL) { |
||
| 123 | 2 | if (is_null (self::$db)) { |
|
| 124 | 2 | try { |
|
| 125 | if (is_readable (self::getDbFileName ($database))) { |
||
| 126 | self::$db = new PDO('sqlite:'. self::getDbFileName ($database)); |
||
| 127 | 158 | if (useNormAndUp ()) { |
|
| 128 | self::$db->sqliteCreateFunction ('normAndUp', 'normAndUp', 1); |
||
| 129 | } |
||
| 130 | 4 | } else { |
|
| 131 | 4 | self::error ($database); |
|
| 132 | 3 | } |
|
| 133 | 3 | } catch (Exception $e) { |
|
| 134 | 2 | self::error ($database); |
|
| 135 | } |
||
| 136 | } |
||
| 137 | 1 | return self::$db; |
|
| 138 | } |
||
| 139 | 2 | ||
| 140 | public static function checkDatabaseAvailability () { |
||
| 141 | if (self::noDatabaseSelected ()) { |
||
| 142 | 103 | for ($i = 0; $i < count (self::getDbList ()); $i++) { |
|
| 143 | 103 | self::getDb ($i); |
|
| 144 | 103 | self::clearDb (); |
|
| 145 | } |
||
| 146 | 24 | } else { |
|
| 147 | 24 | self::getDb (); |
|
| 148 | } |
||
| 149 | return true; |
||
| 150 | 19 | } |
|
| 151 | 19 | ||
| 152 | 18 | public static function clearDb () { |
|
| 153 | self::$db = NULL; |
||
| 154 | 19 | } |
|
| 155 | 19 | ||
| 156 | 19 | public static function executeQuerySingle ($query, $database = NULL) { |
|
| 157 | 19 | return self::getDb ($database)->query($query)->fetchColumn(); |
|
| 158 | 19 | } |
|
| 159 | 19 | ||
| 160 | public static function getCountGeneric($table, $id, $pageId, $numberOfString = NULL) { |
||
| 161 | if (!$numberOfString) { |
||
| 162 | 35 | $numberOfString = $table . ".alphabetical"; |
|
| 163 | } |
||
| 164 | $count = self::executeQuerySingle ('select count(*) from ' . $table); |
||
| 165 | 35 | if ($count == 0) return NULL; |
|
| 166 | 35 | $entry = new Entry (localize($table . ".title"), $id, |
|
| 167 | 35 | str_format (localize($numberOfString, $count), $count), "text", |
|
| 168 | array ( new LinkNavigation ("?page=".$pageId)), "", $count); |
||
| 169 | return $entry; |
||
| 170 | } |
||
| 171 | 25 | ||
| 172 | 25 | public static function getEntryArrayWithBookNumber ($query, $columns, $params, $category) { |
|
| 173 | 17 | /* @var $result PDOStatement */ |
|
| 174 | |||
| 175 | 8 | list (, $result) = self::executeQuery ($query, $columns, "", $params, -1); |
|
| 176 | $entryArray = array(); |
||
| 177 | 25 | while ($post = $result->fetchObject ()) |
|
| 178 | 25 | { |
|
| 179 | 25 | /* @var $instance Author|Tag|Serie|Publisher */ |
|
| 180 | |||
| 181 | 35 | $instance = new $category ($post); |
|
| 182 | if (property_exists($post, "sort")) { |
||
| 183 | $title = $post->sort; |
||
| 184 | 75 | } else { |
|
| 185 | 75 | $title = $post->name; |
|
| 186 | } |
||
| 187 | 75 | array_push ($entryArray, new Entry ($title, $instance->getEntryId (), |
|
| 188 | 7 | str_format (localize("bookword", $post->count), $post->count), "text", |
|
| 189 | 7 | array ( new LinkNavigation ($instance->getUri ())), "", $post->count)); |
|
| 190 | } |
||
| 191 | return $entryArray; |
||
| 192 | 75 | } |
|
| 193 | 73 | ||
| 194 | public static function executeQuery($query, $columns, $filter, $params, $n, $database = NULL, $numberPerPage = NULL) { |
||
| 195 | $totalResult = -1; |
||
| 196 | 75 | ||
| 197 | if (useNormAndUp ()) { |
||
| 198 | $query = preg_replace("/upper/", "normAndUp", $query); |
||
| 199 | 28 | $columns = preg_replace("/upper/", "normAndUp", $columns); |
|
| 200 | 28 | } |
|
| 201 | 28 | ||
| 202 | if (is_null ($numberPerPage)) { |
||
| 203 | $numberPerPage = getCurrentOption ("max_item_per_page"); |
||
| 204 | 28 | } |
|
| 205 | 28 | ||
| 206 | if ($numberPerPage != -1 && $n != -1) |
||
| 207 | { |
||
| 208 | 75 | // First check total number of results |
|
| 209 | 75 | $result = self::getDb ($database)->prepare (str_format ($query, "count(*)", $filter)); |
|
| 210 | 75 | $result->execute ($params); |
|
| 211 | $totalResult = $result->fetchColumn (); |
||
| 212 | |||
| 213 | // Next modify the query and params |
||
| 214 | $query .= " limit ?, ?"; |
||
| 215 | array_push ($params, ($n - 1) * $numberPerPage, $numberPerPage); |
||
| 216 | } |
||
| 217 | |||
| 218 | $result = self::getDb ($database)->prepare(str_format ($query, $columns, $filter)); |
||
| 219 | $result->execute ($params); |
||
| 220 | return array ($totalResult, $result); |
||
| 221 | } |
||
| 222 | |||
| 223 | } |
||
| 224 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state