| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | error_reporting(E_ALL); |
|---|
| 11 | require_once(dirname(__FILE__)."/plog-load-config.php"); |
|---|
| 12 | include(PLOGGER_DIR."plog-admin/plog-admin-functions.php"); |
|---|
| 13 | |
|---|
| 14 | define("DEBUG",0); |
|---|
| 15 | $debug_msgs = ""; |
|---|
| 16 | define("GR_SERVER_VERSION","2.14"); |
|---|
| 17 | |
|---|
| 18 | define("GR_STAT_SUCCESS",0); |
|---|
| 19 | define("GR_STAT_PROTO_MAJ_VER_INVAL",101); |
|---|
| 20 | define("GR_STAT_PROTO_MIN_VER_INVAL",102); |
|---|
| 21 | define("GR_STAT_PROTO_VER_FMT_INVAL",103); |
|---|
| 22 | define("GR_STAT_PROTO_VER_MISSING",104); |
|---|
| 23 | define("GR_STAT_PASSWORD_WRONG",201); |
|---|
| 24 | define("GR_STAT_LOGIN_MISSING",202); |
|---|
| 25 | define("GR_STAT_UNKNOWN_CMD",301); |
|---|
| 26 | define("GR_STAT_NO_ADD_PERMISSION",401); |
|---|
| 27 | define("GR_STAT_NO_FILENAME",402); |
|---|
| 28 | define("GR_STAT_UPLOAD_PHOTO_FAIL",403); |
|---|
| 29 | define("GR_STAT_NO_WRITE_PERMISSION",404); |
|---|
| 30 | define("GR_STAT_NO_CREATE_ALBUM_PERMISSION",501); |
|---|
| 31 | define("GR_STAT_CREATE_ALBUM_FAILED",502); |
|---|
| 32 | |
|---|
| 33 | class response { |
|---|
| 34 | function response() { |
|---|
| 35 | $this->keys = array(); |
|---|
| 36 | $this->keys["server_version"] = GR_SERVER_VERSION; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | function set_key($key,$value) { |
|---|
| 40 | $this->keys[$key] = $value; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | function write() { |
|---|
| 44 | print "#__GR2PROTO__\n"; |
|---|
| 45 | foreach($this->keys as $key => $val) print "${key}=${val}\n"; |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | function get_album_by_name($name) { |
|---|
| 50 | $sqlAlbum = "SELECT * FROM `".TABLE_PREFIX."albums` WHERE name = '".mysql_real_escape_string($name)."'"; |
|---|
| 51 | $resultAlbum = run_query($sqlAlbum); |
|---|
| 52 | return mysql_fetch_assoc($resultAlbum); |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | function login($user,$password) { |
|---|
| 56 | global $response; |
|---|
| 57 | global $config; |
|---|
| 58 | if (($user == $config["admin_username"]) && (md5($password) == $config["admin_password"])) { |
|---|
| 59 | $response->set_key("status",GR_STAT_SUCCESS); |
|---|
| 60 | $response->set_key("status_text","Login successful"); |
|---|
| 61 | } else { |
|---|
| 62 | $response->set_key("status",GR_STAT_PASSWORD_WRONG); |
|---|
| 63 | $response->set_key("status_text","Login failed"); |
|---|
| 64 | }; |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | function list_albums() { |
|---|
| 68 | global $config; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | $sqlCollections = "SELECT * FROM `".TABLE_PREFIX."collections` ORDER BY `name` ASC"; |
|---|
| 72 | $resultCollections = run_query($sqlCollections); |
|---|
| 73 | $albums = $parents = array(); |
|---|
| 74 | $albums[1] = array( |
|---|
| 75 | "name" => "Plogger", |
|---|
| 76 | "title" => $config["gallery_name"], |
|---|
| 77 | "summary" => "1", |
|---|
| 78 | "parent" => 0, |
|---|
| 79 | |
|---|
| 80 | "perms.add" => "false", |
|---|
| 81 | "perms.write" => "false", |
|---|
| 82 | "perms.del_item" => "false", |
|---|
| 83 | "perms.del_alb" => "false", |
|---|
| 84 | |
|---|
| 85 | "perms.create_sub" => "true", |
|---|
| 86 | ); |
|---|
| 87 | $i = 2; |
|---|
| 88 | |
|---|
| 89 | while($rowCollection = mysql_fetch_assoc($resultCollections)){ |
|---|
| 90 | $id = $rowCollection["id"]; |
|---|
| 91 | $description = $rowCollection["description"]; |
|---|
| 92 | $name = $rowCollection["name"]; |
|---|
| 93 | if (empty($description)) |
|---|
| 94 | { |
|---|
| 95 | $description = " "; |
|---|
| 96 | }; |
|---|
| 97 | if (empty($name)) |
|---|
| 98 | { |
|---|
| 99 | $name = "no name"; |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | $albums[$i] = array( |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | "name" => $name, |
|---|
| 106 | "title" => $name, |
|---|
| 107 | "id" => $id, |
|---|
| 108 | |
|---|
| 109 | "summary" => "", |
|---|
| 110 | |
|---|
| 111 | "parent" => 1, |
|---|
| 112 | |
|---|
| 113 | "perms.add" => "false", |
|---|
| 114 | "perms.write" => "false", |
|---|
| 115 | "perms.del_item" => "false", |
|---|
| 116 | "perms.del_alb" => "false", |
|---|
| 117 | |
|---|
| 118 | "perms.create_sub" => "true", |
|---|
| 119 | ); |
|---|
| 120 | $parents[$id] = $i; |
|---|
| 121 | $i++; |
|---|
| 122 | }; |
|---|
| 123 | $sqlAlbum = "SELECT * FROM `".TABLE_PREFIX."albums` ORDER BY `name` ASC"; |
|---|
| 124 | $resultAlbum = run_query($sqlAlbum); |
|---|
| 125 | while ($rowAlbum = mysql_fetch_assoc($resultAlbum)){ |
|---|
| 126 | $id = $rowAlbum["id"]; |
|---|
| 127 | $parent_id = $parents[$rowAlbum["parent_id"]]; |
|---|
| 128 | $albums[$i] = array( |
|---|
| 129 | "name" => $rowAlbum["name"], |
|---|
| 130 | "title" => $rowAlbum["name"], |
|---|
| 131 | "summary" => $rowAlbum["description"], |
|---|
| 132 | |
|---|
| 133 | "parent" => $parent_id, |
|---|
| 134 | "resize_size" => 480, |
|---|
| 135 | "thumb_size" => 240, |
|---|
| 136 | |
|---|
| 137 | "perms.add" => "true", |
|---|
| 138 | "perms.write" => "true", |
|---|
| 139 | "perms.del_item" => "true", |
|---|
| 140 | |
|---|
| 141 | "perms.create_sub" => "false", |
|---|
| 142 | ); |
|---|
| 143 | $i++; |
|---|
| 144 | }; |
|---|
| 145 | $i = 1; |
|---|
| 146 | global $response; |
|---|
| 147 | $response->set_key("status",GR_STAT_SUCCESS); |
|---|
| 148 | |
|---|
| 149 | $response->set_key("status_text","Fetch albums successful."); |
|---|
| 150 | |
|---|
| 151 | foreach($albums as $id => $data){ |
|---|
| 152 | unset($data["id"]); |
|---|
| 153 | foreach($data as $key => $val) { |
|---|
| 154 | $response->set_key("album.${key}.${i}",$val); |
|---|
| 155 | }; |
|---|
| 156 | $i++; |
|---|
| 157 | }; |
|---|
| 158 | $response->set_key("album_count",$i); |
|---|
| 159 | $response->set_key("can_create_root","no"); |
|---|
| 160 | }; |
|---|
| 161 | |
|---|
| 162 | function list_images($albumname) { |
|---|
| 163 | global $response; |
|---|
| 164 | $response->set_key("status",GR_STAT_SUCCESS); |
|---|
| 165 | $response->set_key("status_text","List of images"); |
|---|
| 166 | |
|---|
| 167 | if (empty($albumname)) |
|---|
| 168 | { |
|---|
| 169 | $albumname = "Plogger"; |
|---|
| 170 | }; |
|---|
| 171 | |
|---|
| 172 | $albuminfo = get_album_by_name($albumname); |
|---|
| 173 | $i = 0; |
|---|
| 174 | |
|---|
| 175 | if ($albuminfo) { |
|---|
| 176 | $sqlPictures = "SELECT * FROM `".TABLE_PREFIX."pictures` WHERE parent_album = " . intval($albuminfo['id']); |
|---|
| 177 | $resultAlbum = run_query($sqlPictures); |
|---|
| 178 | while ($rowAlbum = mysql_fetch_assoc($resultAlbum)){ |
|---|
| 179 | $response->set_key("image.name.${i}",$rowAlbum["path"]); |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | $thumbname = "plog-content/thumbs/" . $rowAlbum['id'] . '-' . basename($rowAlbum["path"]); |
|---|
| 184 | $response->set_key("image.thumbName.${i}",$thumbname); |
|---|
| 185 | $i++; |
|---|
| 186 | }; |
|---|
| 187 | }; |
|---|
| 188 | $response->set_key("image_count",$i); |
|---|
| 189 | $server = "http://" . $_SERVER["SERVER_NAME"] . dirname($_SERVER["REQUEST_URI"]) . "/"; |
|---|
| 190 | $response->set_key("baseurl",$server); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | function gr_add_album($parent,$name,$description) { |
|---|
| 194 | |
|---|
| 195 | $query = "SELECT * FROM `".TABLE_PREFIX."collections` WHERE name = '" . mysql_real_escape_string($parent) . "'"; |
|---|
| 196 | $result = run_query($query); |
|---|
| 197 | |
|---|
| 198 | $row = mysql_fetch_assoc($result); |
|---|
| 199 | |
|---|
| 200 | if (empty($name)) |
|---|
| 201 | { |
|---|
| 202 | $name = "no name"; |
|---|
| 203 | }; |
|---|
| 204 | |
|---|
| 205 | if (empty($description)) |
|---|
| 206 | { |
|---|
| 207 | $description = "no description"; |
|---|
| 208 | }; |
|---|
| 209 | |
|---|
| 210 | $parent_id = $row["id"]; |
|---|
| 211 | |
|---|
| 212 | $result = add_album($name,$description,$parent_id); |
|---|
| 213 | global $response; |
|---|
| 214 | if (0 == $result["id"]) { |
|---|
| 215 | $response->set_key("status",GR_STAT_CREATE_ALBUM_FAILED); |
|---|
| 216 | $response->set_key("status_text","Could not create album"); |
|---|
| 217 | |
|---|
| 218 | } else { |
|---|
| 219 | $response->set_key("status",GR_STAT_SUCCESS); |
|---|
| 220 | $response->set_key("status_text","Album created"); |
|---|
| 221 | }; |
|---|
| 222 | |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | function add_image($album,$filename,$caption) { |
|---|
| 226 | $filedat = $_FILES["userfile"]; |
|---|
| 227 | $albuminfo = get_album_by_name($album); |
|---|
| 228 | $src = $filedat["tmp_name"]; |
|---|
| 229 | $result = add_picture($albuminfo["id"],$_FILES["userfile"]["tmp_name"],$_FILES["userfile"]["name"],$caption); |
|---|
| 230 | |
|---|
| 231 | global $debug_msgs; |
|---|
| 232 | $debug_msgs .= print_r($result,true); |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | global $response; |
|---|
| 237 | if ($result["picture_id"] === false) { |
|---|
| 238 | $response->set_key("status",GR_STAT_UPLOAD_PHOTO_FAIL); |
|---|
| 239 | $response->set_key("status_text","Add photo failed."); |
|---|
| 240 | } else { |
|---|
| 241 | $response->set_key("status",GR_STAT_SUCCESS); |
|---|
| 242 | |
|---|
| 243 | $response->set_key("status_text","Add photo successful."); |
|---|
| 244 | }; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | header("Content-type: text/plain"); |
|---|
| 248 | $cmd = isset($_POST["cmd"]) ? $_POST["cmd"] : ""; |
|---|
| 249 | |
|---|
| 250 | if (DEBUG) { |
|---|
| 251 | $fd = fopen("debug.txt","a"); |
|---|
| 252 | fwrite($fd,print_r($_POST,true)); |
|---|
| 253 | fwrite($fd,print_r($_FILES,true)); |
|---|
| 254 | fwrite($fd,print_r($debug_msgs,true)); |
|---|
| 255 | fclose($fd); |
|---|
| 256 | }; |
|---|
| 257 | |
|---|
| 258 | $response = new response(); |
|---|
| 259 | |
|---|
| 260 | switch($cmd) |
|---|
| 261 | { |
|---|
| 262 | case "login": |
|---|
| 263 | login($_POST["uname"],$_POST["password"]); |
|---|
| 264 | break; |
|---|
| 265 | |
|---|
| 266 | case "fetch-albums": |
|---|
| 267 | list_albums(); |
|---|
| 268 | break; |
|---|
| 269 | |
|---|
| 270 | case "fetch-album-images"; |
|---|
| 271 | list_images($_POST["set_albumName"]); |
|---|
| 272 | break; |
|---|
| 273 | |
|---|
| 274 | case "add-item": |
|---|
| 275 | add_image($_POST["set_albumName"],$_FILES["userfile"]["name"],$_POST["caption"]); |
|---|
| 276 | break; |
|---|
| 277 | |
|---|
| 278 | case "new-album": |
|---|
| 279 | |
|---|
| 280 | gr_add_album($_POST["set_albumName"],$_POST["newAlbumTitle"],$_POST["newAlbumDesc"]); |
|---|
| 281 | break; |
|---|
| 282 | |
|---|
| 283 | default: |
|---|
| 284 | $response->set_key("status",GR_STAT_UNKNOWN_CMD); |
|---|
| 285 | $response->set_key("status_text","Unknown command."); |
|---|
| 286 | }; |
|---|
| 287 | |
|---|
| 288 | $response->write(); |
|---|
| 289 | ?> |
|---|