cvs: pearweb /include pear-database.php
| From: | Alan Knowles | Date: | Sat, 11 Sep 2004 09:47:59 +0000 |
| Subject: | cvs: pearweb /include pear-database.php | ||
| Groups: | php.pear.cvs php.pear.core | ||
| Request: | Send a blank email to pear-cvs+get-23825@lists.php.net to get a copy of this message | ||
alan_k Sat Sep 11 05:47:59 2004 EDT
Modified files:
/pearweb/include pear-database.php
Log:
fix pierre's commit
http://cvs.php.net/diff.php/pearweb/include/pear-database.php?r1=1.240&r2=1.241&ty=u Index: pearweb/include/pear-database.php diff -u pearweb/include/pear-database.php:1.240 pearweb/include/pear-database.php:1.241 --- pearweb/include/pear-database.php:1.240 Sat Sep 11 05:21:14 2004 +++ pearweb/include/pear-database.php Sat Sep 11 05:47:56 2004 @@ -17,7 +17,7 @@ | Tomas V.V.Cox <cox@php.net> | | Martin Jansen <mj@php.net> | +----------------------------------------------------------------------+ - $Id: pear-database.php,v 1.240 2004/09/11 09:21:14 pajoye Exp $ + $Id: pear-database.php,v 1.241 2004/09/11 09:47:56 alan_k Exp $ */ require_once 'DB/storage.php'; @@ -1631,7 +1631,690 @@ release::logDownload($package_id, $log_release, $log_file); - header('ZendWhoIsWho() API 1.0 + header('Last-modified: '.HTTP::date(filemtime($path))); + header('Content-type: application/octet-stream'); + if ($uncompress) { + $tarname = preg_replace('/\.tgz$/', '.tar', $basename); + header('Content-disposition: attachment; filename="'.$tarname.'"'); + readgzfile($path); + } else { + header('Content-disposition: attachment; filename="'.$basename.'"'); + header('Content-length: '.filesize($path)); + readfile($path); + } + + return true; + } + header('HTTP/1.0 404 Not Found'); + print 'File not found'; + } + + // }}} + // {{{ proto bool release::isValidState(string) API 1.0 + + /** + * Determine if release state is valid + * + * @static + * @param string State + * @return boolean + */ + function isValidState($state) + { + static $states = array('devel', 'snapshot', 'alpha', 'beta', 'stable'); + return in_array($state, $states); + } + + // }}} + // {{{ proto array release::betterStates(string) API 1.0 + + /** + * ??? + * + * @param string Release state + * @param boolean include the state in the array returned + * @return boolean + */ + function betterStates($state, $include = false) + { + static $states = array('snapshot', 'devel', 'alpha', 'beta', 'stable'); + $i = array_search($state, $states); + if ($include) { + $i--; + } + if ($i === false) { + return false; + } + return array_slice($states, $i + 1); + } + + // }}} + // {{{ NOEXPORT release::logDownload(integer, string, string) + + /** + * Log release download + * + * @param integer ID of the package + * @param integer ID of the release + * @param string Filename + */ + function logDownload($package, $release_id, $file = null) + { + global $dbh; + + $id = $dbh->nextId("downloads"); + + $query = "INSERT INTO downloads (id, file, package, release, dl_when, dl_who, dl_host) VALUES (?, ?, ?, ?, ?, ?, ?)"; + + $err = $dbh->query($query, array($id, (int)$file, $package, + $release_id, date("Y-m-d H:i:s"), + $_SERVER['REMOTE_ADDR'], + gethostbyaddr($_SERVER['REMOTE_ADDR']) + )); + + // {{{ Update package_stats table + + // Check if an entry for the release already exists + $query = "SELECT COUNT(pid) FROM package_stats WHERE pid = ? AND rid = ?"; + $exists = $dbh->getOne($query, array($package, $release_id)); + + if ($exists == 0) { + $pkg_info = package::info($package, null, true); + + $query = 'SELECT version FROM releases' + . ' WHERE package = ? AND id = ?'; + $version = $dbh->getOne($query, array($package, $release_id)); + + if (!$version) { + return PEAR::raiseError('release:: the package you requested' + . ' has no release by that number'); + } + + $query = 'INSERT INTO package_stats' + . ' (dl_number, package, release, pid, rid, cid, last_dl)' + . ' VALUES (1, ?, ?, ?, ?, ?, ?)'; + + $dbh->query($query, array($pkg_info['name'], + $version, + $package, + $release_id, + $pkg_info['categoryid'], + date('Y-m-d H:i:s') + ) + ); + } else { + $query = 'UPDATE package_stats ' + . ' SET dl_number = dl_number + 1,' + . " last_dl = '" . date('Y-m-d H:i:s') . "'" + . ' WHERE pid = ? AND rid = ?'; + $dbh->query($query, array($package, $release_id)); + } + + // }}} + } + + // }}} + // {{{ +proto string release::promote(array, string) API 1.0 + + /** + * Promote new release + * + * @param array Coming from PEAR_common::infoFromDescFile('package.xml') + * @param string Filename of the new uploaded release + * @return void + */ + function promote($pkginfo, $upload) + { + if ($_SERVER['SERVER_NAME'] != 'pear.php.net') { + return; + } + $pacid = package::info($pkginfo['package'], 'packageid'); + $authors = package::info($pkginfo['package'], 'authors'); + $txt_authors = ''; + foreach ($authors as $a) { + $txt_authors .= $a['name']; + if ($a['showemail']) { + $txt_authors .= " <{$a['email']}>"; + } + $txt_authors .= " ({$a['role']})\n"; + } + $upload = basename($upload); + $release = "{$pkginfo['package']}-{$pkginfo['version']} ({$pkginfo['release_state']})"; + $txtanounce =<<<END +The new PEAR package $release has been released at http://pear.php.net/. + +Release notes +------------- +{$pkginfo['release_notes']} + +Package Info +------------- +{$pkginfo['description']} + +Related Links +------------- +Package home: http://pear.php.net/package/$pkginfo[package] + Changelog: http://pear.php.net/package/$pkginfo[package]/download/$pkginfo[version] + Download: http://pear.php.net/get/$upload + +Authors +------------- +$txt_authors +END; + + $to = '"PEAR general list" <pear-general@lists.php.net>'; + $from = '"PEAR Announce" <pear-dev@lists.php.net>'; + $subject = "[ANNOUNCEMENT] $release Released."; + mail($to, $subject, $txtanounce, "From: $from", "-f pear-sys@php.net"); + } + + // }}} + // {{{ NOEXPORT release::remove(int, int) + + /** + * Remove release + * + * @param integer ID of the package + * @param integer ID of the release + * @return boolean + */ + function remove($package, $release) + { + global $dbh, $auth_user; + if (!$auth_user->isAdmin() && !$auth_user->isQA() && + !user::maintains($auth_user->handle, $package, 'lead')) { + return PEAR::raiseError('release::remove: insufficient privileges'); + } + + $success = true; + + // get files that have to be removed + $query = sprintf("SELECT fullpath FROM files WHERE package = '%s' AND release = '%s'", + $package, + $release); + + $sth = $dbh->query($query); + + while ($row = $sth->fetchRow(DB_FETCHMODE_ASSOC)) { + if (!@unlink($row['fullpath'])) { + $success = false; + } + } + + $query = sprintf("DELETE FROM files WHERE package = '%s' AND release = '%s'", + $package, + $release + ); + $sth = $dbh->query($query); + + $query = sprintf("DELETE FROM releases WHERE package = '%s' AND id = '%s'", + $package, + $release + ); + $sth = $dbh->query($query); + + if (PEAR::isError($sth)) { + return false; + } else { + return true; + } + } + + // }}} + // {{{ getFAQ() + + /** + * Get FAQ items for given package version + * + * @param string Name of the package + * @param string Version string of the package + * @return mixed PEAR_Error or Array + */ + function getFAQ($package, $version) + { + global $dbh; + + $query = "SELECT f.* FROM packages_faq f, packages p, releases r " + . "WHERE p.name = ? AND p.id = r.package AND r.version = ? AND r.id = f.release"; + + return $dbh->getAll($query, array($package, $version), DB_FETCHMODE_ASSOC); + } + // }}} +} + + +/** + * Class to handle notes + * + * @class note + * @package pearweb + * @author Stig S. Bakken <ssb@fast.no> + * @author Tomas V.V. Cox <cox@php.net> + * @author Martin Jansen <mj@php.net> + */ +class note +{ + // {{{ +proto bool note::add(string, int, string, string) API 1.0 + + function add($key, $value, $note, $author = "") + { + global $dbh; + if (empty($author)) { + $author = $_COOKIE['PEAR_USER']; + } + $nid = $dbh->nextId("notes"); + $stmt = $dbh->prepare("INSERT INTO notes (id,$key,nby,ntime,note) ". + "VALUES(?,?,?,?,?)"); + $res = $dbh->execute($stmt, array($nid, $value, $author, + gmdate('Y-m-d H:i'), $note)); + if (DB::isError($res)) { + return $res; + } + return true; + } + + // }}} + // {{{ +proto bool note::remove(int) API 1.0 + + function remove($id) + { + global $dbh; + $id = (int)$id; + $res = $dbh->query("DELETE FROM notes WHERE id = $id"); + if (DB::isError($res)) { + return $res; + } + return true; + } + + // }}} + // {{{ +proto bool note::removeAll(string, int) API 1.0 + + function removeAll($key, $value) + { + global $dbh; + $res = $dbh->query("DELETE FROM notes WHERE $key = ". $dbh->quote($value)); + if (DB::isError($res)) { + return $res; + } + return true; + } + + // }}} +} + +class user +{ + // {{{ *proto bool user::remove(string) API 1.0 + + function remove($uid) + { + global $dbh; + note::removeAll("uid", $uid); + $dbh->query('DELETE FROM users WHERE handle = '. $dbh->quote($uid)); + return ($dbh->affectedRows() > 0); + } + + // }}} + // {{{ *proto bool user::rejectRequest(string, string) API 1.0 + + function rejectRequest($uid, $reason) + { + global $dbh; + list($email) = $dbh->getRow('SELECT email FROM users WHERE handle = ?', + array($uid)); + note::add("uid", $uid, "Account rejected: $reason"); + $msg = "Your PEAR account request was rejected by " . $_COOKIE['PEAR_USER'] . ":\n\n". + "$reason\n"; + $xhdr = "From: " . $_COOKIE['PEAR_USER'] . "@php.net"; + mail($email, "Your PEAR Account Request", $msg, $xhdr, "-f pear-sys@php.net"); + return true; + } + + // }}} + // {{{ *proto bool user::activate(string) API 1.0 + + function activate($uid, $karmalevel = 'pear.dev') + { + require_once "Damblan/Karma.php"; + + global $dbh; + + $user =& new PEAR_User($dbh, $uid); + $karma = new Damblan_Karma($dbh); + + if (@$user->registered) { + return false; + } + @$arr = unserialize($user->userinfo); + note::removeAll("uid", $uid); + $user->set('registered', 1); + /* $user->set('ppp_only', 0); */ + if (is_array($arr)) { + $user->set('userinfo', $arr[1]); + } + $user->set('created', gmdate('Y-m-d H:i')); + $user->set('createdby', $_COOKIE['PEAR_USER']); + $user->store(); + $karma->grant($user->handle, $karmalevel); + note::add("uid", $uid, "Account opened"); + $msg = "Your PEAR account request has been opened.\n". + "To log in, go to http://pear.php.net/ and click on \"login\" in\n". + "the top-right menu.\n"; + $xhdr = "From: " . $_COOKIE['PEAR_USER'] . "@php.net"; + mail($user->email, "Your PEAR Account Request", $msg, $xhdr, "-f pear-sys@php.net"); + return true; + } + + // }}} + // {{{ +proto bool user::isAdmin(string) API 1.0 + + function isAdmin($handle) + { + require_once "Damblan/Karma.php"; + + global $dbh; + $karma = new Damblan_Karma($dbh); + + return $karma->has($handle, "pear.admin"); + } + + // }}} + // {{{ +proto bool user::isQA(string) API post 1.0 + + function isQA($handle) + { + require_once 'Damblan/Karma.php'; + + global $dbh; + $karma = new Damblan_Karma($dbh); + + return $karma->has($handle, 'pear.qa'); + } + + // }}} + // {{{ proto bool user::listAdmins() API 1.0 + + function listAdmins() + { + require_once "Damblan/Karma.php"; + + global $dbh; + $karma = new Damblan_Karma($dbh); + + return $karma->getUser("pear.admin"); + } + + // }}} + // {{{ +proto bool user::exists(string) API 1.0 + + function exists($handle) + { + global $dbh; + $sql = "SELECT handle FROM users WHERE handle=?"; + $res = $dbh->query($sql, array($handle)); + return ($res->numRows() > 0); + } + + // }}} + // {{{ +proto string user::maintains(string|int, [string]) API 1.0 + + function maintains($user, $pkgid, $role = 'any') + { + global $dbh; + $package_id = package::info($pkgid, 'id'); + if ($role == 'any') { + return $dbh->getOne('SELECT role FROM maintains WHERE handle = ? '. + 'AND package = ?', array($user, $package_id)); + } + if (is_array($role)) { + return $dbh->getOne('SELECT role FROM maintains WHERE handle = ? AND package = ? '. + 'AND role IN ("?")', array($user, $package_id, implode('","', $role))); + } + return $dbh->getOne('SELECT role FROM maintains WHERE handle = ? AND package = ? '. + 'AND role = ?', array($user, $package_id, $role)); + } + + // }}} + // {{{ proto string user::info(string, [string]) API 1.0 + + function info($user, $field = null) + { + global $dbh; + if ($field === null) { + $row = $dbh->getRow('SELECT * FROM users WHERE registered = 1 AND handle = ?', + array($user), DB_FETCHMODE_ASSOC); + unset($row['password']); + return $row; + } + if ($field == 'password' || preg_match('/[^a-z]/', $user)) { + return null; + } + return $dbh->getRow('SELECT ! FROM users WHERE handle = ?', + array($field, $user), DB_FETCHMODE_ASSOC); + + } + + // }}} + // {{{ listAll() + + function listAll($registered_only = true) + { + global $dbh; + $query = "SELECT * FROM users"; + if ($registered_only === true) { + $query .= " WHERE registered = 1"; + } + $query .= " ORDER BY handle"; + return $dbh->getAll($query, null, DB_FETCHMODE_ASSOC); + } + + // }}} + // {{{ add() + + /** + * Add a new user account + * + * During most of this method's operation, PEAR's error handling + * is set to PEAR_ERROR_RETURN. + * + * But, during the DB_storage::set() phase error handling is set to + * PEAR_ERROR_CALLBACK the report_warning() function. So, if an + * error happens a warning message is printed AND the incomplete + * user information is removed. + * + * @param array $data Information about the user + * + * @return mixed true if there are no problems, false if sending the + * email failed, 'set error' if DB_storage::set() failed + * or an array of error messages for other problems + * + * @access public + */ + function add(&$data) + { + global $dbh; + + PEAR::pushErrorHandling(PEAR_ERROR_RETURN); + $errors = array(); + + $required = array( + 'handle' => 'Username', + 'firstname' => 'First Name', + 'lastname' => 'Last Name', + 'email' => 'Email address', + 'purpose' => 'Intended purpose', + ); + + $name = $data['firstname'] . " " . $data['lastname']; + + foreach ($required as $field => $desc) { + if (empty($data[$field])) { + $data['jumpto'] = $field; + $errors[] = 'Please enter ' . $desc; + } + } + + if (!preg_match(PEAR_COMMON_USER_NAME_REGEX, $data['handle'])) { + $errors[] = 'Username must start with a letter and contain' + . ' only letters and digits'; + } + + if ($data['password'] != $data['password2']) { + $data['password'] = $data['password2'] = ""; + $data['jumpto'] = "password"; + $errors[] = 'Passwords did not match'; + } + + if (!$data['password']) { + $data['jumpto'] = "password"; + $errors[] = 'Empty passwords not allowed'; + } + + $handle = strtolower($data['handle']); + $obj =& new PEAR_User($dbh, $handle); + + if (isset($obj->created)) { + $data['jumpto'] = "handle"; + $errors[] = 'Sorry, that username is already taken'; + } + + if ($errors) { + $data['display_form'] = true; + return $errors; + } + + $err = $obj->insert($handle); + + if (DB::isError($err)) { + if ($err->getCode() == DB_ERROR_CONSTRAINT) { + $data['display_form'] = true; + $data['jumpto'] = 'handle'; + $errors[] = 'Sorry, that username is already taken'; + } else { + $data['display_form'] = false; + $errors[] = $err; + } + return $errors; + } + + $data['display_form'] = false; + $md5pw = md5($data['password']); + $showemail = @(bool)$data['showemail']; + // hack to temporarily embed the "purpose" in + // the user's "userinfo" column + $userinfo = serialize(array($data['purpose'], $data['moreinfo'])); + $set_vars = array('name' => $name, + 'email' => $data['email'], + 'homepage' => $data['homepage'], + 'showemail' => $showemail, + 'password' => $md5pw, + 'registered' => 0, + 'userinfo' => $userinfo); + + PEAR::pushErrorHandling(PEAR_ERROR_CALLBACK, 'report_warning'); + foreach ($set_vars as $var => $value) { + $err = $obj->set($var, $value); + if (PEAR::isError($err)) { + user::remove($data['handle']); + return 'set error'; + } + } + PEAR::popErrorHandling(); + + $msg = "Requested from: {$_SERVER['REMOTE_ADDR']}\n". + "Username: {$handle}\n". + "Real Name: {$name}\n". + (isset($data['showemail']) ? "Email: {$data['email']}\n" : "") . + "Purpose:\n". + "{$data['purpose']}\n\n". + "To handle: http://{$_SERVER['SERVER_NAME']}/admin/?acreq={$handle}\n"; + + if ($data['moreinfo']) { + $msg .= "\nMore info:\n{$data['moreinfo']}\n"; + } + + $xhdr = "From: $name <{$data['email']}>\nMessage-Id: <account-request-{$handle}@pear.php.net>\n"; + // $xhdr .= "\nBCC: pear-group@php.net"; + $subject = "PEAR Account Request: {$handle}"; + + if (DEVBOX == false) { + $ok = @mail('pear-group@php.net', $subject, $msg, $xhdr, + '-f pear-sys@php.net'); + } else { + $ok = true; + } + + PEAR::popErrorHandling(); + + return $ok; + } + + // }}} + // {{{ update + + /** + * Update user information + * + * @access public + * @param array User information + * @return object Instance of PEAR_User + */ + function update($data) { + global $dbh; + + $fields = array("name", "email", "homepage", "showemail", "userinfo", "pgpkeyid", "wishlist"); + + $user =& new PEAR_User($dbh, $data['handle']); + foreach ($data as $key => $value) { + if (!in_array($key, $fields)) { + continue; + } + $user->set($key, $value); + } + $user->store(); + + return $user; + } + + // }}} + // {{{ getRecentReleases(string, [int]) + + /** + * Get recent releases for the given user + * + * @access public + * @param string Handle of the user + * @param int Number of releases (default is 10) + * @return array + */ + function getRecentReleases($handle, $n = 10) + { + global $dbh; + $recent = array(); + + $query = "SELECT p.id AS id, " . + "p.name AS name, " . + "p.summary AS summary, " . + "r.version AS version, " . + "r.releasedate AS releasedate, " . + "r.releasenotes AS releasenotes, " . + "r.doneby AS doneby, " . + "r.state AS state " . + "FROM packages p, releases r, maintains m " . + "WHERE p.package_type = 'pear' AND p.id = r.package " . + "AND p.id = m.package AND m.handle = '" . $handle . "' " . + "ORDER BY r.releasedate DESC"; + + $sth = $dbh->limitQuery($query, 0, $n); + while ($sth->fetchInto($row, DB_FETCHMODE_ASSOC)) { + $recent[] = $row; + } + return $recent; + } + + // }}} + // {{{ * proto array user::getZendWhoIsWho() API 1.0 /** * Get list of current developers and the packages they maintain. @@ -1645,7 +2328,7 @@ * @return array */ function getZendWhoIsWho() { - return false; + return false; // until we can flag agreement I guess. global $dbh; // IP whitelist
http://cvs.php.net/diff.php/pearweb/include/pear-database.php?r1=1.240&r2=1.241&ty=u Index: pearweb/include/pear-database.php diff -u pearweb/include/pear-database.php:1.240 pearweb/include/pear-database.php:1.241 --- pearweb/include/pear-database.php:1.240 Sat Sep 11 05:21:14 2004 +++ pearweb/include/pear-database.php Sat Sep 11 05:47:56 2004 @@ -17,7 +17,7 @@ | Tomas V.V.Cox <cox@php.net> | | Martin Jansen <mj@php.net> | +----------------------------------------------------------------------+ - $Id: pear-database.php,v 1.240 2004/09/11 09:21:14 pajoye Exp $ + $Id: pear-database.php,v 1.241 2004/09/11 09:47:56 alan_k Exp $ */ require_once 'DB/storage.php'; @@ -1631,7 +1631,690 @@ release::logDownload($package_id, $log_release, $log_file); - header('ZendWhoIsWho() API 1.0 + header('Last-modified: '.HTTP::date(filemtime($path))); + header('Content-type: application/octet-stream'); + if ($uncompress) { + $tarname = preg_replace('/\.tgz$/', '.tar', $basename); + header('Content-disposition: attachment; filename="'.$tarname.'"'); + readgzfile($path); + } else { + header('Content-disposition: attachment; filename="'.$basename.'"'); + header('Content-length: '.filesize($path)); + readfile($path); + } + + return true; + } + header('HTTP/1.0 404 Not Found'); + print 'File not found'; + } + + // }}} + // {{{ proto bool release::isValidState(string) API 1.0 + + /** + * Determine if release state is valid + * + * @static + * @param string State + * @return boolean + */ + function isValidState($state) + { + static $states = array('devel', 'snapshot', 'alpha', 'beta', 'stable'); + return in_array($state, $states); + } + + // }}} + // {{{ proto array release::betterStates(string) API 1.0 + + /** + * ??? + * + * @param string Release state + * @param boolean include the state in the array returned + * @return boolean + */ + function betterStates($state, $include = false) + { + static $states = array('snapshot', 'devel', 'alpha', 'beta', 'stable'); + $i = array_search($state, $states); + if ($include) { + $i--; + } + if ($i === false) { + return false; + } + return array_slice($states, $i + 1); + } + + // }}} + // {{{ NOEXPORT release::logDownload(integer, string, string) + + /** + * Log release download + * + * @param integer ID of the package + * @param integer ID of the release + * @param string Filename + */ + function logDownload($package, $release_id, $file = null) + { + global $dbh; + + $id = $dbh->nextId("downloads"); + + $query = "INSERT INTO downloads (id, file, package, release, dl_when, dl_who, dl_host) VALUES (?, ?, ?, ?, ?, ?, ?)"; + + $err = $dbh->query($query, array($id, (int)$file, $package, + $release_id, date("Y-m-d H:i:s"), + $_SERVER['REMOTE_ADDR'], + gethostbyaddr($_SERVER['REMOTE_ADDR']) + )); + + // {{{ Update package_stats table + + // Check if an entry for the release already exists + $query = "SELECT COUNT(pid) FROM package_stats WHERE pid = ? AND rid = ?"; + $exists = $dbh->getOne($query, array($package, $release_id)); + + if ($exists == 0) { + $pkg_info = package::info($package, null, true); + + $query = 'SELECT version FROM releases' + . ' WHERE package = ? AND id = ?'; + $version = $dbh->getOne($query, array($package, $release_id)); + + if (!$version) { + return PEAR::raiseError('release:: the package you requested' + . ' has no release by that number'); + } + + $query = 'INSERT INTO package_stats' + . ' (dl_number, package, release, pid, rid, cid, last_dl)' + . ' VALUES (1, ?, ?, ?, ?, ?, ?)'; + + $dbh->query($query, array($pkg_info['name'], + $version, + $package, + $release_id, + $pkg_info['categoryid'], + date('Y-m-d H:i:s') + ) + ); + } else { + $query = 'UPDATE package_stats ' + . ' SET dl_number = dl_number + 1,' + . " last_dl = '" . date('Y-m-d H:i:s') . "'" + . ' WHERE pid = ? AND rid = ?'; + $dbh->query($query, array($package, $release_id)); + } + + // }}} + } + + // }}} + // {{{ +proto string release::promote(array, string) API 1.0 + + /** + * Promote new release + * + * @param array Coming from PEAR_common::infoFromDescFile('package.xml') + * @param string Filename of the new uploaded release + * @return void + */ + function promote($pkginfo, $upload) + { + if ($_SERVER['SERVER_NAME'] != 'pear.php.net') { + return; + } + $pacid = package::info($pkginfo['package'], 'packageid'); + $authors = package::info($pkginfo['package'], 'authors'); + $txt_authors = ''; + foreach ($authors as $a) { + $txt_authors .= $a['name']; + if ($a['showemail']) { + $txt_authors .= " <{$a['email']}>"; + } + $txt_authors .= " ({$a['role']})\n"; + } + $upload = basename($upload); + $release = "{$pkginfo['package']}-{$pkginfo['version']} ({$pkginfo['release_state']})"; + $txtanounce =<<<END +The new PEAR package $release has been released at http://pear.php.net/. + +Release notes +------------- +{$pkginfo['release_notes']} + +Package Info +------------- +{$pkginfo['description']} + +Related Links +------------- +Package home: http://pear.php.net/package/$pkginfo[package] + Changelog: http://pear.php.net/package/$pkginfo[package]/download/$pkginfo[version] + Download: http://pear.php.net/get/$upload + +Authors +------------- +$txt_authors +END; + + $to = '"PEAR general list" <pear-general@lists.php.net>'; + $from = '"PEAR Announce" <pear-dev@lists.php.net>'; + $subject = "[ANNOUNCEMENT] $release Released."; + mail($to, $subject, $txtanounce, "From: $from", "-f pear-sys@php.net"); + } + + // }}} + // {{{ NOEXPORT release::remove(int, int) + + /** + * Remove release + * + * @param integer ID of the package + * @param integer ID of the release + * @return boolean + */ + function remove($package, $release) + { + global $dbh, $auth_user; + if (!$auth_user->isAdmin() && !$auth_user->isQA() && + !user::maintains($auth_user->handle, $package, 'lead')) { + return PEAR::raiseError('release::remove: insufficient privileges'); + } + + $success = true; + + // get files that have to be removed + $query = sprintf("SELECT fullpath FROM files WHERE package = '%s' AND release = '%s'", + $package, + $release); + + $sth = $dbh->query($query); + + while ($row = $sth->fetchRow(DB_FETCHMODE_ASSOC)) { + if (!@unlink($row['fullpath'])) { + $success = false; + } + } + + $query = sprintf("DELETE FROM files WHERE package = '%s' AND release = '%s'", + $package, + $release + ); + $sth = $dbh->query($query); + + $query = sprintf("DELETE FROM releases WHERE package = '%s' AND id = '%s'", + $package, + $release + ); + $sth = $dbh->query($query); + + if (PEAR::isError($sth)) { + return false; + } else { + return true; + } + } + + // }}} + // {{{ getFAQ() + + /** + * Get FAQ items for given package version + * + * @param string Name of the package + * @param string Version string of the package + * @return mixed PEAR_Error or Array + */ + function getFAQ($package, $version) + { + global $dbh; + + $query = "SELECT f.* FROM packages_faq f, packages p, releases r " + . "WHERE p.name = ? AND p.id = r.package AND r.version = ? AND r.id = f.release"; + + return $dbh->getAll($query, array($package, $version), DB_FETCHMODE_ASSOC); + } + // }}} +} + + +/** + * Class to handle notes + * + * @class note + * @package pearweb + * @author Stig S. Bakken <ssb@fast.no> + * @author Tomas V.V. Cox <cox@php.net> + * @author Martin Jansen <mj@php.net> + */ +class note +{ + // {{{ +proto bool note::add(string, int, string, string) API 1.0 + + function add($key, $value, $note, $author = "") + { + global $dbh; + if (empty($author)) { + $author = $_COOKIE['PEAR_USER']; + } + $nid = $dbh->nextId("notes"); + $stmt = $dbh->prepare("INSERT INTO notes (id,$key,nby,ntime,note) ". + "VALUES(?,?,?,?,?)"); + $res = $dbh->execute($stmt, array($nid, $value, $author, + gmdate('Y-m-d H:i'), $note)); + if (DB::isError($res)) { + return $res; + } + return true; + } + + // }}} + // {{{ +proto bool note::remove(int) API 1.0 + + function remove($id) + { + global $dbh; + $id = (int)$id; + $res = $dbh->query("DELETE FROM notes WHERE id = $id"); + if (DB::isError($res)) { + return $res; + } + return true; + } + + // }}} + // {{{ +proto bool note::removeAll(string, int) API 1.0 + + function removeAll($key, $value) + { + global $dbh; + $res = $dbh->query("DELETE FROM notes WHERE $key = ". $dbh->quote($value)); + if (DB::isError($res)) { + return $res; + } + return true; + } + + // }}} +} + +class user +{ + // {{{ *proto bool user::remove(string) API 1.0 + + function remove($uid) + { + global $dbh; + note::removeAll("uid", $uid); + $dbh->query('DELETE FROM users WHERE handle = '. $dbh->quote($uid)); + return ($dbh->affectedRows() > 0); + } + + // }}} + // {{{ *proto bool user::rejectRequest(string, string) API 1.0 + + function rejectRequest($uid, $reason) + { + global $dbh; + list($email) = $dbh->getRow('SELECT email FROM users WHERE handle = ?', + array($uid)); + note::add("uid", $uid, "Account rejected: $reason"); + $msg = "Your PEAR account request was rejected by " . $_COOKIE['PEAR_USER'] . ":\n\n". + "$reason\n"; + $xhdr = "From: " . $_COOKIE['PEAR_USER'] . "@php.net"; + mail($email, "Your PEAR Account Request", $msg, $xhdr, "-f pear-sys@php.net"); + return true; + } + + // }}} + // {{{ *proto bool user::activate(string) API 1.0 + + function activate($uid, $karmalevel = 'pear.dev') + { + require_once "Damblan/Karma.php"; + + global $dbh; + + $user =& new PEAR_User($dbh, $uid); + $karma = new Damblan_Karma($dbh); + + if (@$user->registered) { + return false; + } + @$arr = unserialize($user->userinfo); + note::removeAll("uid", $uid); + $user->set('registered', 1); + /* $user->set('ppp_only', 0); */ + if (is_array($arr)) { + $user->set('userinfo', $arr[1]); + } + $user->set('created', gmdate('Y-m-d H:i')); + $user->set('createdby', $_COOKIE['PEAR_USER']); + $user->store(); + $karma->grant($user->handle, $karmalevel); + note::add("uid", $uid, "Account opened"); + $msg = "Your PEAR account request has been opened.\n". + "To log in, go to http://pear.php.net/ and click on \"login\" in\n". + "the top-right menu.\n"; + $xhdr = "From: " . $_COOKIE['PEAR_USER'] . "@php.net"; + mail($user->email, "Your PEAR Account Request", $msg, $xhdr, "-f pear-sys@php.net"); + return true; + } + + // }}} + // {{{ +proto bool user::isAdmin(string) API 1.0 + + function isAdmin($handle) + { + require_once "Damblan/Karma.php"; + + global $dbh; + $karma = new Damblan_Karma($dbh); + + return $karma->has($handle, "pear.admin"); + } + + // }}} + // {{{ +proto bool user::isQA(string) API post 1.0 + + function isQA($handle) + { + require_once 'Damblan/Karma.php'; + + global $dbh; + $karma = new Damblan_Karma($dbh); + + return $karma->has($handle, 'pear.qa'); + } + + // }}} + // {{{ proto bool user::listAdmins() API 1.0 + + function listAdmins() + { + require_once "Damblan/Karma.php"; + + global $dbh; + $karma = new Damblan_Karma($dbh); + + return $karma->getUser("pear.admin"); + } + + // }}} + // {{{ +proto bool user::exists(string) API 1.0 + + function exists($handle) + { + global $dbh; + $sql = "SELECT handle FROM users WHERE handle=?"; + $res = $dbh->query($sql, array($handle)); + return ($res->numRows() > 0); + } + + // }}} + // {{{ +proto string user::maintains(string|int, [string]) API 1.0 + + function maintains($user, $pkgid, $role = 'any') + { + global $dbh; + $package_id = package::info($pkgid, 'id'); + if ($role == 'any') { + return $dbh->getOne('SELECT role FROM maintains WHERE handle = ? '. + 'AND package = ?', array($user, $package_id)); + } + if (is_array($role)) { + return $dbh->getOne('SELECT role FROM maintains WHERE handle = ? AND package = ? '. + 'AND role IN ("?")', array($user, $package_id, implode('","', $role))); + } + return $dbh->getOne('SELECT role FROM maintains WHERE handle = ? AND package = ? '. + 'AND role = ?', array($user, $package_id, $role)); + } + + // }}} + // {{{ proto string user::info(string, [string]) API 1.0 + + function info($user, $field = null) + { + global $dbh; + if ($field === null) { + $row = $dbh->getRow('SELECT * FROM users WHERE registered = 1 AND handle = ?', + array($user), DB_FETCHMODE_ASSOC); + unset($row['password']); + return $row; + } + if ($field == 'password' || preg_match('/[^a-z]/', $user)) { + return null; + } + return $dbh->getRow('SELECT ! FROM users WHERE handle = ?', + array($field, $user), DB_FETCHMODE_ASSOC); + + } + + // }}} + // {{{ listAll() + + function listAll($registered_only = true) + { + global $dbh; + $query = "SELECT * FROM users"; + if ($registered_only === true) { + $query .= " WHERE registered = 1"; + } + $query .= " ORDER BY handle"; + return $dbh->getAll($query, null, DB_FETCHMODE_ASSOC); + } + + // }}} + // {{{ add() + + /** + * Add a new user account + * + * During most of this method's operation, PEAR's error handling + * is set to PEAR_ERROR_RETURN. + * + * But, during the DB_storage::set() phase error handling is set to + * PEAR_ERROR_CALLBACK the report_warning() function. So, if an + * error happens a warning message is printed AND the incomplete + * user information is removed. + * + * @param array $data Information about the user + * + * @return mixed true if there are no problems, false if sending the + * email failed, 'set error' if DB_storage::set() failed + * or an array of error messages for other problems + * + * @access public + */ + function add(&$data) + { + global $dbh; + + PEAR::pushErrorHandling(PEAR_ERROR_RETURN); + $errors = array(); + + $required = array( + 'handle' => 'Username', + 'firstname' => 'First Name', + 'lastname' => 'Last Name', + 'email' => 'Email address', + 'purpose' => 'Intended purpose', + ); + + $name = $data['firstname'] . " " . $data['lastname']; + + foreach ($required as $field => $desc) { + if (empty($data[$field])) { + $data['jumpto'] = $field; + $errors[] = 'Please enter ' . $desc; + } + } + + if (!preg_match(PEAR_COMMON_USER_NAME_REGEX, $data['handle'])) { + $errors[] = 'Username must start with a letter and contain' + . ' only letters and digits'; + } + + if ($data['password'] != $data['password2']) { + $data['password'] = $data['password2'] = ""; + $data['jumpto'] = "password"; + $errors[] = 'Passwords did not match'; + } + + if (!$data['password']) { + $data['jumpto'] = "password"; + $errors[] = 'Empty passwords not allowed'; + } + + $handle = strtolower($data['handle']); + $obj =& new PEAR_User($dbh, $handle); + + if (isset($obj->created)) { + $data['jumpto'] = "handle"; + $errors[] = 'Sorry, that username is already taken'; + } + + if ($errors) { + $data['display_form'] = true; + return $errors; + } + + $err = $obj->insert($handle); + + if (DB::isError($err)) { + if ($err->getCode() == DB_ERROR_CONSTRAINT) { + $data['display_form'] = true; + $data['jumpto'] = 'handle'; + $errors[] = 'Sorry, that username is already taken'; + } else { + $data['display_form'] = false; + $errors[] = $err; + } + return $errors; + } + + $data['display_form'] = false; + $md5pw = md5($data['password']); + $showemail = @(bool)$data['showemail']; + // hack to temporarily embed the "purpose" in + // the user's "userinfo" column + $userinfo = serialize(array($data['purpose'], $data['moreinfo'])); + $set_vars = array('name' => $name, + 'email' => $data['email'], + 'homepage' => $data['homepage'], + 'showemail' => $showemail, + 'password' => $md5pw, + 'registered' => 0, + 'userinfo' => $userinfo); + + PEAR::pushErrorHandling(PEAR_ERROR_CALLBACK, 'report_warning'); + foreach ($set_vars as $var => $value) { + $err = $obj->set($var, $value); + if (PEAR::isError($err)) { + user::remove($data['handle']); + return 'set error'; + } + } + PEAR::popErrorHandling(); + + $msg = "Requested from: {$_SERVER['REMOTE_ADDR']}\n". + "Username: {$handle}\n". + "Real Name: {$name}\n". + (isset($data['showemail']) ? "Email: {$data['email']}\n" : "") . + "Purpose:\n". + "{$data['purpose']}\n\n". + "To handle: http://{$_SERVER['SERVER_NAME']}/admin/?acreq={$handle}\n"; + + if ($data['moreinfo']) { + $msg .= "\nMore info:\n{$data['moreinfo']}\n"; + } + + $xhdr = "From: $name <{$data['email']}>\nMessage-Id: <account-request-{$handle}@pear.php.net>\n"; + // $xhdr .= "\nBCC: pear-group@php.net"; + $subject = "PEAR Account Request: {$handle}"; + + if (DEVBOX == false) { + $ok = @mail('pear-group@php.net', $subject, $msg, $xhdr, + '-f pear-sys@php.net'); + } else { + $ok = true; + } + + PEAR::popErrorHandling(); + + return $ok; + } + + // }}} + // {{{ update + + /** + * Update user information + * + * @access public + * @param array User information + * @return object Instance of PEAR_User + */ + function update($data) { + global $dbh; + + $fields = array("name", "email", "homepage", "showemail", "userinfo", "pgpkeyid", "wishlist"); + + $user =& new PEAR_User($dbh, $data['handle']); + foreach ($data as $key => $value) { + if (!in_array($key, $fields)) { + continue; + } + $user->set($key, $value); + } + $user->store(); + + return $user; + } + + // }}} + // {{{ getRecentReleases(string, [int]) + + /** + * Get recent releases for the given user + * + * @access public + * @param string Handle of the user + * @param int Number of releases (default is 10) + * @return array + */ + function getRecentReleases($handle, $n = 10) + { + global $dbh; + $recent = array(); + + $query = "SELECT p.id AS id, " . + "p.name AS name, " . + "p.summary AS summary, " . + "r.version AS version, " . + "r.releasedate AS releasedate, " . + "r.releasenotes AS releasenotes, " . + "r.doneby AS doneby, " . + "r.state AS state " . + "FROM packages p, releases r, maintains m " . + "WHERE p.package_type = 'pear' AND p.id = r.package " . + "AND p.id = m.package AND m.handle = '" . $handle . "' " . + "ORDER BY r.releasedate DESC"; + + $sth = $dbh->limitQuery($query, 0, $n); + while ($sth->fetchInto($row, DB_FETCHMODE_ASSOC)) { + $recent[] = $row; + } + return $recent; + } + + // }}} + // {{{ * proto array user::getZendWhoIsWho() API 1.0 /** * Get list of current developers and the packages they maintain. @@ -1645,7 +2328,7 @@ * @return array */ function getZendWhoIsWho() { - return false; + return false; // until we can flag agreement I guess. global $dbh; // IP whitelist