addressbook.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * ownCloud - Addressbook
  4. *
  5. * @author Jakob Sack
  6. * @copyright 2011 Jakob Sack mail@jakobsack.de
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /*
  23. *
  24. * The following SQL statement is just a help for developers and will not be
  25. * executed!
  26. *
  27. * CREATE TABLE contacts_addressbooks (
  28. * id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  29. * userid VARCHAR(255) NOT NULL,
  30. * displayname VARCHAR(255),
  31. * uri VARCHAR(100),
  32. * description TEXT,
  33. * ctag INT(11) UNSIGNED NOT NULL DEFAULT '1'
  34. * );
  35. *
  36. */
  37. /**
  38. * This class manages our addressbooks.
  39. */
  40. class OC_Contacts_Addressbook {
  41. /**
  42. * @brief Returns the list of addressbooks for a specific user.
  43. * @param string $uid
  44. * @param boolean $active Only return addressbooks with this $active state, default(=false) is don't care
  45. * @return array or false.
  46. */
  47. public static function all($uid, $active=false) {
  48. $values = array($uid);
  49. $active_where = '';
  50. if ($active) {
  51. $active_where = ' AND active = ?';
  52. $values[] = 1;
  53. }
  54. try {
  55. $stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ? ' . $active_where . ' ORDER BY displayname' );
  56. $result = $stmt->execute($values);
  57. } catch(Exception $e) {
  58. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.' exception: '.$e->getMessage(), OCP\Util::ERROR);
  59. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.' uid: '.$uid, OCP\Util::DEBUG);
  60. return false;
  61. }
  62. $addressbooks = array();
  63. while( $row = $result->fetchRow()) {
  64. $addressbooks[] = $row;
  65. }
  66. if(!$active && !count($addressbooks)) {
  67. self::addDefault($uid);
  68. }
  69. return $addressbooks;
  70. }
  71. /**
  72. * @brief Get active addressbook IDs for a user.
  73. * @param integer $uid User id. If null current user will be used.
  74. * @return array
  75. */
  76. public static function activeIds($uid = null) {
  77. if(is_null($uid)) {
  78. $uid = OCP\USER::getUser();
  79. }
  80. $activeaddressbooks = self::all($uid, true);
  81. $ids = array();
  82. foreach($activeaddressbooks as $addressbook) {
  83. $ids[] = $addressbook['id'];
  84. }
  85. return $ids;
  86. }
  87. /**
  88. * @brief Returns the list of active addressbooks for a specific user.
  89. * @param string $uid
  90. * @return array
  91. */
  92. public static function active($uid) {
  93. return self::all($uid, true);
  94. }
  95. /**
  96. * @brief Returns the list of addressbooks for a principal (DAV term of user)
  97. * @param string $principaluri
  98. * @return array
  99. */
  100. public static function allWherePrincipalURIIs($principaluri){
  101. $uid = self::extractUserID($principaluri);
  102. return self::all($uid);
  103. }
  104. /**
  105. * @brief Gets the data of one address book
  106. * @param integer $id
  107. * @return associative array or false.
  108. */
  109. public static function find($id) {
  110. try {
  111. $stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id = ?' );
  112. $result = $stmt->execute(array($id));
  113. } catch(Exception $e) {
  114. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
  115. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', id: '.$id, OCP\Util::DEBUG);
  116. return false;
  117. }
  118. return $result->fetchRow();
  119. }
  120. /**
  121. * @brief Adds default address book
  122. * @return $id ID of the newly created addressbook or false on error.
  123. */
  124. public static function addDefault($uid = null) {
  125. if(is_null($uid)) {
  126. $uid = OCP\USER::getUser();
  127. }
  128. $id = self::add($uid, 'Contacts', 'Default Address Book');
  129. if($id !== false) {
  130. self::setActive($id, true);
  131. }
  132. return $id;
  133. }
  134. /**
  135. * @brief Creates a new address book
  136. * @param string $userid
  137. * @param string $name
  138. * @param string $description
  139. * @return insertid
  140. */
  141. public static function add($uid,$name,$description='') {
  142. try {
  143. $stmt = OCP\DB::prepare( 'SELECT uri FROM *PREFIX*contacts_addressbooks WHERE userid = ? ' );
  144. $result = $stmt->execute(array($uid));
  145. } catch(Exception $e) {
  146. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.' exception: '.$e->getMessage(), OCP\Util::ERROR);
  147. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.' uid: '.$uid, OCP\Util::DEBUG);
  148. return false;
  149. }
  150. $uris = array();
  151. while($row = $result->fetchRow()){
  152. $uris[] = $row['uri'];
  153. }
  154. $uri = self::createURI($name, $uris );
  155. try {
  156. $stmt = OCP\DB::prepare( 'INSERT INTO *PREFIX*contacts_addressbooks (userid,displayname,uri,description,ctag) VALUES(?,?,?,?,?)' );
  157. $result = $stmt->execute(array($uid,$name,$uri,$description,1));
  158. } catch(Exception $e) {
  159. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
  160. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', uid: '.$uid, OCP\Util::DEBUG);
  161. return false;
  162. }
  163. return OCP\DB::insertid('*PREFIX*contacts_addressbooks');
  164. }
  165. /**
  166. * @brief Creates a new address book from the data sabredav provides
  167. * @param string $principaluri
  168. * @param string $uri
  169. * @param string $name
  170. * @param string $description
  171. * @return insertid or false
  172. */
  173. public static function addFromDAVData($principaluri,$uri,$name,$description) {
  174. $uid = self::extractUserID($principaluri);
  175. try {
  176. $stmt = OCP\DB::prepare('INSERT INTO *PREFIX*contacts_addressbooks (userid,displayname,uri,description,ctag) VALUES(?,?,?,?,?)');
  177. $result = $stmt->execute(array($uid,$name,$uri,$description,1));
  178. } catch(Exception $e) {
  179. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
  180. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', uid: '.$uid, OCP\Util::DEBUG);
  181. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', uri: '.$uri, OCP\Util::DEBUG);
  182. return false;
  183. }
  184. return OCP\DB::insertid('*PREFIX*contacts_addressbooks');
  185. }
  186. /**
  187. * @brief Edits an addressbook
  188. * @param integer $id
  189. * @param string $name
  190. * @param string $description
  191. * @return boolean
  192. */
  193. public static function edit($id,$name,$description) {
  194. // Need these ones for checking uri
  195. $addressbook = self::find($id);
  196. if(is_null($name)) {
  197. $name = $addressbook['name'];
  198. }
  199. if(is_null($description)) {
  200. $description = $addressbook['description'];
  201. }
  202. try {
  203. $stmt = OCP\DB::prepare('UPDATE *PREFIX*contacts_addressbooks SET displayname=?,description=?, ctag=ctag+1 WHERE id=?');
  204. $result = $stmt->execute(array($name,$description,$id));
  205. } catch(Exception $e) {
  206. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
  207. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', id: '.$id, OCP\Util::DEBUG);
  208. return false;
  209. }
  210. return true;
  211. }
  212. /**
  213. * @brief Activates an addressbook
  214. * @param integer $id
  215. * @param boolean $active
  216. * @return boolean
  217. */
  218. public static function setActive($id,$active) {
  219. $sql = 'UPDATE *PREFIX*contacts_addressbooks SET active = ? WHERE id = ?';
  220. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', id: '.$id.', active: '.intval($active), OCP\Util::ERROR);
  221. try {
  222. $stmt = OCP\DB::prepare($sql);
  223. $stmt->execute(array(intval($active), $id));
  224. return true;
  225. } catch(Exception $e) {
  226. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception for '.$id.': '.$e->getMessage(), OCP\Util::ERROR);
  227. return false;
  228. }
  229. }
  230. /**
  231. * @brief Checks if an addressbook is active.
  232. * @param integer $id ID of the address book.
  233. * @return boolean
  234. */
  235. public static function isActive($id) {
  236. $sql = 'SELECT active FROM *PREFIX*contacts_addressbooks WHERE id = ?';
  237. try {
  238. $stmt = OCP\DB::prepare( $sql );
  239. $result = $stmt->execute(array($id));
  240. $row = $result->fetchRow();
  241. return (bool)$row['active'];
  242. } catch(Exception $e) {
  243. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
  244. }
  245. }
  246. /**
  247. * @brief removes an address book
  248. * @param integer $id
  249. * @return boolean
  250. */
  251. public static function delete($id) {
  252. self::setActive($id, false);
  253. try {
  254. $stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*contacts_addressbooks WHERE id = ?' );
  255. $stmt->execute(array($id));
  256. } catch(Exception $e) {
  257. OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception for '.$id.': '.$e->getMessage(), OCP\Util::ERROR);
  258. return false;
  259. }
  260. $cards = OC_Contacts_VCard::all($id);
  261. foreach($cards as $card){
  262. OC_Contacts_VCard::delete($card['id']);
  263. }
  264. return true;
  265. }
  266. /**
  267. * @brief Updates ctag for addressbook
  268. * @param integer $id
  269. * @return boolean
  270. */
  271. public static function touch($id) {
  272. $stmt = OCP\DB::prepare( 'UPDATE *PREFIX*contacts_addressbooks SET ctag = ctag + 1 WHERE id = ?' );
  273. $stmt->execute(array($id));
  274. return true;
  275. }
  276. /**
  277. * @brief Creates a URI for Addressbook
  278. * @param string $name name of the addressbook
  279. * @param array $existing existing addressbook URIs
  280. * @return string new name
  281. */
  282. public static function createURI($name,$existing) {
  283. $name = str_replace(' ', '_', strtolower($name));
  284. $newname = $name;
  285. $i = 1;
  286. while(in_array($newname, $existing)) {
  287. $newname = $name.$i;
  288. $i = $i + 1;
  289. }
  290. return $newname;
  291. }
  292. /**
  293. * @brief gets the userid from a principal path
  294. * @return string
  295. */
  296. public static function extractUserID($principaluri) {
  297. list($prefix, $userid) = Sabre_DAV_URLUtil::splitPath($principaluri);
  298. return $userid;
  299. }
  300. }