Ejemplo n.º 1
0
 /**
  * insert
  * This inserts a new record for the specified object
  * with the specified information, amazing!
  */
 public static function insert($type, $oid, $user, $agent = '')
 {
     $type = self::validate_type($type);
     $sql = "INSERT INTO `object_count` (`object_type`,`object_id`,`date`,`user`,`agent`) " . " VALUES (?, ?, ?, ?, ?)";
     $db_results = Dba::write($sql, array($type, $oid, time(), $user, $agent));
     if (!$db_results) {
         debug_event('statistics', 'Unabled to insert statistics:' . $sql, '3');
     }
 }
Ejemplo n.º 2
0
 /**
  * insert
  * This inserts a new record for the specified object
  * with the specified information, amazing!
  */
 public static function insert($type, $oid, $user, $agent = '', $location, $count_type = 'stream')
 {
     if (!self::is_already_inserted($type, $oid, $user)) {
         $type = self::validate_type($type);
         $latitude = null;
         $longitude = null;
         $geoname = null;
         if (isset($location['latitude'])) {
             $latitude = $location['latitude'];
         }
         if (isset($location['longitude'])) {
             $longitude = $location['longitude'];
         }
         if (isset($location['name'])) {
             $geoname = $location['name'];
         }
         $sql = "INSERT INTO `object_count` (`object_type`,`object_id`,`count_type`,`date`,`user`,`agent`, `geo_latitude`, `geo_longitude`, `geo_name`) " . " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
         $db_results = Dba::write($sql, array($type, $oid, $count_type, time(), $user, $agent, $latitude, $longitude, $geoname));
         if (Core::is_media($type)) {
             Useractivity::post_activity($user, 'play', $type, $oid);
         }
         if (!$db_results) {
             debug_event('statistics', 'Unabled to insert statistics:' . $sql, '3');
         }
     } else {
         debug_event('statistics', 'Statistics insertion ignored due to graceful delay.', '3');
     }
 }
Ejemplo n.º 3
0
 /**
  * update
  *
  * This function updates the saved version with the current settings.
  */
 public function update(array $data = null)
 {
     if ($data && is_array($data)) {
         $this->name = $data['name'];
         $this->type = $data['pl_type'];
         $this->random = $data['random'];
         $this->limit = $data['limit'];
     }
     if (!$this->id) {
         return false;
     }
     $sql = "UPDATE `search` SET `name` = ?, `type` = ?, `rules` = ?, `logic_operator` = ?, `random` = ?, `limit` = ? WHERE `id` = ?";
     Dba::write($sql, array($this->name, $this->type, serialize($this->rules), $this->logic_operator, $this->random, $this->limit, $this->id));
     return $this->id;
 }
Ejemplo n.º 4
0
 /**
  * update_380005
  *
  * Add manual update flag on artist
  */
 public static function update_380005()
 {
     $retval = true;
     $sql = "ALTER TABLE `artist` ADD COLUMN `manual_update` SMALLINT( 1 ) DEFAULT '0'";
     $retval &= Dba::write($sql);
     return $retval;
 }
Ejemplo n.º 5
0
 /**
  * Remove the video from disk.
  */
 public function remove_from_disk()
 {
     $deleted = parent::remove_from_disk();
     if ($deleted) {
         $sql = "DELETE FROM `tvshow_episode` WHERE `id` = ?";
         $deleted = Dba::write($sql, array($this->id));
     }
     return $deleted;
 }
Ejemplo n.º 6
0
 public function remove_from_disk()
 {
     $deleted = true;
     $season_ids = $this->get_seasons();
     foreach ($season_ids as $id) {
         $season = new TVShow_Season($id);
         $deleted = $season->remove_from_disk();
         if (!$deleted) {
             debug_event('tvshow', 'Error when deleting the season `' . $id . '`.', 1);
             break;
         }
     }
     if ($deleted) {
         $sql = "DELETE FROM `tvshow` WHERE `id` = ?";
         $deleted = Dba::write($sql, array($this->id));
         if ($deleted) {
             Art::gc('tvshow', $this->id);
             Userflag::gc('tvshow', $this->id);
             Rating::gc('tvshow', $this->id);
             Shoutbox::gc('tvshow', $this->id);
             Useractivity::gc('tvshow', $this->id);
         }
     }
     return $deleted;
 }
Ejemplo n.º 7
0
 /**
  * set_rating
  * This function sets the rating for the current object.
  * If no userid is passed in, we use the currently logged in user.
  */
 public function set_rating($rating, $user_id = null)
 {
     if (is_null($user_id)) {
         $user_id = $GLOBALS['user']->id;
     }
     $user_id = intval($user_id);
     debug_event('Rating', "Setting rating for {$this->type} {$this->id} to {$rating}", 5);
     // If score is -1, then remove rating
     if ($rating == '-1') {
         $sql = "DELETE FROM `rating` WHERE " . "`object_id` = ? AND " . "`object_type` = ? AND " . "`user` = ?";
         $params = array($this->id, $this->type, $user_id);
     } else {
         $sql = "REPLACE INTO `rating` " . "(`object_id`, `object_type`, `rating`, `user`) " . "VALUES (?, ?, ?, ?)";
         $params = array($this->id, $this->type, $rating, $user_id);
     }
     Dba::write($sql, $params);
     parent::add_to_cache('rating_' . $this->type . '_user' . $user_id, $this->id, $rating);
     foreach (Plugin::get_plugins('save_rating') as $plugin_name) {
         $plugin = new Plugin($plugin_name);
         if ($plugin->load($GLOBALS['user'])) {
             $plugin->_plugin->save_rating($this, $rating);
         }
     }
     return true;
 }
Ejemplo n.º 8
0
 /**
  * delete_track
  * This deletes a track from the tmpplaylist
  */
 public function delete_track($id)
 {
     /* delete the track its self */
     $sql = "DELETE FROM `tmp_playlist_data` WHERE `id` = ?";
     Dba::write($sql, array($id));
     return true;
 }
Ejemplo n.º 9
0
 /**
  * rebuild_all_preferences
  * This rebuilds the user preferences for all installed users, called by the plugin functions
  */
 public static function rebuild_all_preferences()
 {
     // Clean out any preferences garbage left over
     $sql = "DELETE `user_preference`.* FROM `user_preference` " . "LEFT JOIN `user` ON `user_preference`.`user` = `user`.`id` " . "WHERE `user_preference`.`user` != -1 AND `user`.`id` IS NULL";
     Dba::write($sql);
     // Get only users who has less preferences than excepted
     // otherwise it would have significant performance issue with large user database
     $sql = "SELECT `user` FROM `user_preference` " . "GROUP BY `user` HAVING COUNT(*) < (" . "SELECT COUNT(`id`) FROM `preference` WHERE `catagory` != 'system')";
     $db_results = Dba::read($sql);
     while ($row = Dba::fetch_assoc($db_results)) {
         User::fix_preferences($row['user']);
     }
     return true;
 }
Ejemplo n.º 10
0
 /**
  * Add a new wanted release.
  * @param string $mbid
  * @param int $artist
  * @param string $artist_mbid
  * @param string $name
  * @param int $year
  */
 public static function add_wanted($mbid, $artist, $artist_mbid, $name, $year)
 {
     $sql = "INSERT INTO `wanted` (`user`, `artist`, `artist_mbid`, `mbid`, `name`, `year`, `date`, `accepted`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
     $accept = $GLOBALS['user']->has_access('75') ? true : AmpConfig::get('wanted_auto_accept');
     $params = array($GLOBALS['user']->id, $artist, $artist_mbid, $mbid, $name, $year, time(), '0');
     Dba::write($sql, $params);
     if ($accept) {
         $wantedid = Dba::insert_id();
         $wanted = new Wanted($wantedid);
         $wanted->accept();
         database_object::remove_from_cache('wanted', $wantedid);
     }
 }
Ejemplo n.º 11
0
 public function save_access()
 {
     $sql = "UPDATE `share` SET `counter` = (`counter` + 1), lastvisit_date = ? WHERE `id` = ?";
     return Dba::write($sql, array(time(), $this->id));
 }
Ejemplo n.º 12
0
 /**
  * delete
  * Deletes the catalog and everything associated with it
  * it takes the catalog id
  */
 public static function delete($catalog_id)
 {
     // Large catalog deletion can take time
     set_time_limit(0);
     // First remove the songs in this catalog
     $sql = "DELETE FROM `song` WHERE `catalog` = ?";
     $db_results = Dba::write($sql, array($catalog_id));
     // Only if the previous one works do we go on
     if (!$db_results) {
         return false;
     }
     $sql = "DELETE FROM `video` WHERE `catalog` = ?";
     $db_results = Dba::write($sql, array($catalog_id));
     if (!$db_results) {
         return false;
     }
     $catalog = self::create_from_id($catalog_id);
     $sql = 'DELETE FROM `catalog_' . $catalog->get_type() . '` WHERE catalog_id = ?';
     $db_results = Dba::write($sql, array($catalog_id));
     if (!$db_results) {
         return false;
     }
     // Next Remove the Catalog Entry it's self
     $sql = "DELETE FROM `catalog` WHERE `id` = ?";
     Dba::write($sql, array($catalog_id));
     // Run the cleaners...
     self::gc();
 }
Ejemplo n.º 13
0
 public static function create(array $data)
 {
     $subject = trim(strip_tags($data['subject']));
     $message = trim(strip_tags($data['message']));
     if (empty($subject)) {
         AmpError::add('subject', T_('Error: Subject Required'));
     }
     $to_user = User::get_from_username($data['to_user']);
     if (!$to_user->id) {
         AmpError::add('to_user', T_('Error: Unknown user'));
     }
     if (!AmpError::occurred()) {
         $from_user = $data['from_user'] ?: $GLOBALS['user']->id;
         $creation_date = $data['creation_date'] ?: time();
         $is_read = $data['is_read'] ?: 0;
         $sql = "INSERT INTO `user_pvmsg` (`subject`, `message`, `from_user`, `to_user`, `creation_date`, `is_read`) " . "VALUES (?, ?, ?, ?, ?, ?)";
         if (Dba::write($sql, array($subject, $message, $from_user, $to_user->id, $creation_date, $is_read))) {
             $insert_id = Dba::insert_id();
             // Never send email in case of user impersonation
             if (!isset($data['from_user']) && $insert_id) {
                 if (Preference::get_by_user($to_user->id, 'notify_email')) {
                     if (!empty($to_user->email)) {
                         $mailer = new Mailer();
                         $mailer->set_default_sender();
                         $mailer->recipient = $to_user->email;
                         $mailer->recipient_name = $to_user->fullname;
                         $mailer->subject = "[" . T_('Private Message') . "] " . $subject;
                         $mailer->message = sprintf(T_("You just received a new private message from %s.\n\n\n        ----------------------\n        %s\n        ----------------------\n\n        %s\n        "), $GLOBALS['user']->fullname, $message, AmpConfig::get('web_path') . "/pvmsg.php?action=show&pvmsg_id=" . $insert_id);
                         $mailer->send();
                     }
                 }
             }
             return $insert_id;
         }
     }
     return false;
 }
Ejemplo n.º 14
0
 /**
  * gc
  * This cleans up art that no longer has a corresponding object
  */
 public static function gc()
 {
     // iterate over our types and delete the images
     foreach (array('album', 'artist') as $type) {
         $sql = "DELETE FROM `image` USING `image` LEFT JOIN `" . $type . "` ON `" . $type . "`.`id`=" . "`image`.`object_id` WHERE `object_type`='" . $type . "' AND `" . $type . "`.`id` IS NULL";
         Dba::write($sql);
     }
     // foreach
 }
Ejemplo n.º 15
0
 /**
  * delete
  * this function deletes a specific shoutbox entry
  */
 public function delete($shout_id)
 {
     // Delete the shoutbox post
     $shout_id = Dba::escape($shout_id);
     $sql = "DELETE FROM `user_shout` WHERE `id`='{$shout_id}'";
     Dba::write($sql);
 }
Ejemplo n.º 16
0
 public static function gc()
 {
     $sql = 'DELETE FROM `song_preview` USING `song_preview` ' . 'LEFT JOIN `session` ON `session`.`id`=`song_preview`.`session` ' . 'WHERE `session`.`id` IS NULL';
     return Dba::write($sql);
 }
Ejemplo n.º 17
0
 protected function deleteRecord($id)
 {
     $sql = 'DELETE FROM ' . $this->getTableName() . ' WHERE id = ?';
     \Dba::write($sql, array($id));
 }
Ejemplo n.º 18
0
 /**
  * Sort the tracks and save the new position
  */
 public function sort_tracks()
 {
     /* First get all of the songs in order of their tracks */
     $sql = "SELECT A.`id`\n                FROM `playlist_data` AS A\n           LEFT JOIN `song` AS B ON A.object_id = B.id\n           LEFT JOIN `artist` AS C ON B.artist = C.id\n           LEFT JOIN `album` AS D ON B.album = D.id\n               WHERE A.`playlist` = ?\n            ORDER BY C.`name` ASC,\n                     B.`title` ASC,\n                     D.`year` ASC,\n                     D.`name` ASC,\n                     B.`track` ASC";
     $db_results = Dba::query($sql, array($this->id));
     $i = 1;
     $results = array();
     while ($r = Dba::fetch_assoc($db_results)) {
         $new_data = array();
         $new_data['id'] = $r['id'];
         $new_data['track'] = $i;
         $results[] = $new_data;
         $i++;
     }
     // end while results
     foreach ($results as $data) {
         $sql = "UPDATE `playlist_data` SET `track` = ? WHERE `id` = ?";
         Dba::write($sql, array($data['track'], $data['id']));
     }
     // foreach re-ordered results
     return true;
 }
Ejemplo n.º 19
0
 /**
  * Update a broadcast from data array.
  * @param array $data
  * @return int
  */
 public function update(array $data)
 {
     if (isset($data['edit_tags'])) {
         Tag::update_tag_list($data['edit_tags'], 'broadcast', $this->id, true);
     }
     $sql = "UPDATE `broadcast` SET `name` = ?, `description` = ?, `is_private` = ? " . "WHERE `id` = ?";
     $params = array($data['name'], $data['description'], !empty($data['private']), $this->id);
     Dba::write($sql, $params);
     return $this->id;
 }
Ejemplo n.º 20
0
 /**
  * clean catalog procedure
  *
  * Removes local songs that no longer exist.
  */
 public function clean_catalog_proc()
 {
     if (!Core::is_readable($this->path)) {
         // First sanity check; no point in proceeding with an unreadable
         // catalog root.
         debug_event('catalog', 'Catalog path:' . $this->path . ' unreadable, clean failed', 1);
         AmpError::add('general', T_('Catalog Root unreadable, stopping clean'));
         AmpError::display('general');
         return 0;
     }
     $dead_total = 0;
     $stats = self::get_stats($this->id);
     $this->count = 0;
     foreach (array('video', 'song') as $media_type) {
         $total = $stats[$media_type . 's'];
         // UGLY
         if ($total == 0) {
             continue;
         }
         $chunks = floor($total / 10000);
         $dead = array();
         foreach (range(0, $chunks) as $chunk) {
             $dead = array_merge($dead, $this->_clean_chunk($media_type, $chunk, 10000));
         }
         $dead_count = count($dead);
         // The AlmightyOatmeal sanity check
         // Never remove everything; it might be a dead mount
         if ($dead_count >= $total) {
             debug_event('catalog', 'All files would be removed. Doing nothing.', 1);
             AmpError::add('general', T_('All files would be removed. Doing nothing'));
             continue;
         }
         if ($dead_count) {
             $dead_total += $dead_count;
             $sql = "DELETE FROM `{$media_type}` WHERE `id` IN " . '(' . implode(',', $dead) . ')';
             $db_results = Dba::write($sql);
         }
     }
     \Lib\Metadata\Repository\Metadata::gc();
     \Lib\Metadata\Repository\MetadataField::gc();
     return $dead_total;
 }
Ejemplo n.º 21
0
 /**
  * update_instance
  * This takes an ID and an array of data and updates the instance specified
  */
 public function update_instance($uid, $data)
 {
     $uid = Dba::escape($uid);
     $host = $data['host'] ? Dba::escape($data['host']) : '127.0.0.1';
     $port = $data['port'] ? Dba::escape($data['port']) : '6600';
     $name = Dba::escape($data['name']);
     $pass = Dba::escape($data['password']);
     $sql = "UPDATE `localplay_mpd` SET `host`='{$host}', `port`='{$port}', `name`='{$name}', `password`='{$pass}' WHERE `id`='{$uid}'";
     $db_results = Dba::write($sql);
     return true;
 }
Ejemplo n.º 22
0
 /**
  * delete
  * This deletes the current object from the database
  */
 public function delete()
 {
     $sql = "DELETE FROM `live_stream` WHERE `id` = ?";
     Dba::write($sql, array($this->id));
     return true;
 }
Ejemplo n.º 23
0
 /**
  * remove_plugin_version
  * This removes the version row from the db done on uninstall
  */
 public function remove_plugin_version()
 {
     $name = Dba::escape('Plugin_' . $this->_plugin->name);
     $sql = "DELETE FROM `update_info` WHERE `key`='{$name}'";
     Dba::write($sql);
     return true;
 }
Ejemplo n.º 24
0
 /**
  * save_objects
  * This takes the full array of object ids, often passed into show and
  * if necessary it saves them
  */
 public function save_objects($object_ids)
 {
     // Saving these objects has two operations, one holds it in
     // a local variable and then second holds it in a row in the
     // tmp_browse table
     // Only do this if it's not a simple browse
     if (!$this->is_simple()) {
         $this->_cache = $object_ids;
         $this->set_total(count($object_ids));
         $id = $this->id;
         if ($id != 'nocache') {
             $data = self::_serialize($this->_cache);
             $sql = 'UPDATE `tmp_browse` SET `object_data` = ? ' . 'WHERE `sid` = ? AND `id` = ?';
             Dba::write($sql, array($data, session_id(), $id));
         }
     }
     return true;
 }
Ejemplo n.º 25
0
 /**
  * optimize_tables
  *
  * This runs an optimize on the tables and updates the stats to improve
  * join speed.
  * This can be slow, but is a good idea to do from time to time. We do
  * it in case the dba isn't doing it... which we're going to assume they
  * aren't.
  */
 public static function optimize_tables()
 {
     $sql = "SHOW TABLES";
     $db_results = Dba::read($sql);
     while ($row = Dba::fetch_row($db_results)) {
         $sql = "OPTIMIZE TABLE `" . $row[0] . "`";
         Dba::write($sql);
         $sql = "ANALYZE TABLE `" . $row[0] . "`";
         Dba::write($sql);
     }
 }
Ejemplo n.º 26
0
 /**
  * delete
  *
  * deletes the specified access_list entry
  */
 public static function delete($id)
 {
     Dba::write('DELETE FROM `access_list` WHERE `id` = ?', array($id));
 }
Ejemplo n.º 27
0
 /**
  * Duplicate an object associate images to a new object
  * @param string $object_type
  * @param int $old_object_id
  * @param int $new_object_id
  * @return boolean
  */
 public static function duplicate($object_type, $old_object_id, $new_object_id)
 {
     if (AmpConfig::get('album_art_store_disk')) {
         $sql = "SELECT `size`, `kind` FROM `image` WHERE `object_type` = ? AND `object_id` = ?";
         $db_results = Dba::read($sql, array($object_type, $old_object_id));
         while ($row = Dba::fetch_assoc($db_results)) {
             $image = self::read_from_dir($row['size'], $object_type, $old_object_id, $row['kind']);
             if ($image != null) {
                 self::write_to_dir($image, $row['size'], $object_type, $new_object_id, $row['kind']);
             }
         }
     }
     $sql = "INSERT INTO `image` (`image`, `mime`, `size`, `object_type`, `object_id`, `kind`) SELECT `image`, `mime`, `size`, `object_type`, ? as `object_id`, `kind` FROM `image` WHERE `object_type` = ? AND `object_id` = ?";
     return Dba::write($sql, array($new_object_id, $object_type, $old_object_id));
 }
Ejemplo n.º 28
0
 /**
  * clear_now_playing
  *
  * There really isn't anywhere else for this function, shouldn't have
  * deleted it in the first place.
  */
 public static function clear_now_playing()
 {
     $sql = 'TRUNCATE `now_playing`';
     Dba::write($sql);
     return true;
 }
Ejemplo n.º 29
0
 /**
  * remove_map
  * This will only remove tag maps for the current user
  */
 public function remove_map($type, $object_id)
 {
     if (!Core::is_library_item($type)) {
         return false;
     }
     // TODO: Review the tag edition per user.
     $sql = "DELETE FROM `tag_map` WHERE `tag_id` = ? AND `object_type` = ? AND `object_id` = ? ";
     //AND `user` = ?";
     Dba::write($sql, array($this->id, $type, $object_id));
     //, $GLOBALS['user']->id));
     return true;
 }
Ejemplo n.º 30
0
 /**
  * clean_catalog_proc
  *
  * Removes songs that no longer exist.
  */
 public function clean_catalog_proc()
 {
     $dead = 0;
     try {
         $api = $this->createClient();
         if ($api != null) {
             $sql = 'SELECT `id`, `file` FROM `song` WHERE `catalog` = ?';
             $db_results = Dba::read($sql, array($this->id));
             while ($row = Dba::fetch_assoc($db_results)) {
                 debug_event('soundcloud-clean', 'Starting work on ' . $row['file'] . '(' . $row['id'] . ')', 5, 'ampache-catalog');
                 $remove = false;
                 try {
                     $track = $this->url_to_track($row['file']);
                     $song = json_decode($api->get('tracks/' . $track));
                     if ($song->user_favorite != true) {
                         $remove = true;
                     }
                 } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                     if ($e->getHttpCode() == '404') {
                         $remove = true;
                     } else {
                         debug_event('soundcloud-clean', 'Clean error: ' . $e->getMessage(), 5, 'ampache-catalog');
                     }
                 } catch (Exception $e) {
                     debug_event('soundcloud-clean', 'Clean error: ' . $e->getMessage(), 5, 'ampache-catalog');
                 }
                 if (!$remove) {
                     debug_event('soundcloud-clean', 'keeping song', 5, 'ampache-catalog');
                 } else {
                     debug_event('soundcloud-clean', 'removing song', 5, 'ampache-catalog');
                     $dead++;
                     Dba::write('DELETE FROM `song` WHERE `id` = ?', array($row['id']));
                 }
             }
         } else {
             echo "<p>" . T_('API Error: cannot connect to SoundCloud.') . "</p><hr />\n";
             flush();
         }
     } catch (Exception $ex) {
         echo "<p>" . T_('SoundCloud exception: ') . $ex->getMessage() . "</p><hr />\n";
     }
     return $dead;
 }