vcard.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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_cards (
  28. * id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  29. * addressbookid INT(11) UNSIGNED NOT NULL,
  30. * fullname VARCHAR(255),
  31. * carddata TEXT,
  32. * uri VARCHAR(100),
  33. * lastmodified INT(11) UNSIGNED
  34. * );
  35. */
  36. /**
  37. * This class manages our vCards
  38. */
  39. class OC_Contacts_VCard{
  40. /**
  41. * @brief Returns all cards of an address book
  42. * @param integer $id
  43. * @return array
  44. *
  45. * The cards are associative arrays. You'll find the original vCard in
  46. * ['carddata']
  47. */
  48. public static function all($id){
  49. if(is_array($id)) {
  50. $id_sql = join(',', array_fill(0, count($id), '?'));
  51. $prep = 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid IN ('.$id_sql.') ORDER BY fullname';
  52. try {
  53. $stmt = OC_DB::prepare( $prep );
  54. $result = $stmt->execute($id);
  55. } catch(Exception $e) {
  56. OC_Log::write('contacts','OC_Contacts_VCard:all:, exception: '.$e->getMessage(),OC_Log::DEBUG);
  57. OC_Log::write('contacts','OC_Contacts_VCard:all, ids: '.join(',', $id),OC_Log::DEBUG);
  58. OC_Log::write('contacts','SQL:'.$prep,OC_Log::DEBUG);
  59. }
  60. } else {
  61. $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? ORDER BY fullname' );
  62. $result = $stmt->execute(array($id));
  63. }
  64. $cards = array();
  65. while( $row = $result->fetchRow()){
  66. $cards[] = $row;
  67. }
  68. return $cards;
  69. }
  70. /**
  71. * @brief Returns a card
  72. * @param integer $id
  73. * @return associative array
  74. */
  75. public static function find($id){
  76. $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE id = ?' );
  77. $result = $stmt->execute(array($id));
  78. return $result->fetchRow();
  79. }
  80. /**
  81. * @brief finds a card by its DAV Data
  82. * @param integer $aid Addressbook id
  83. * @param string $uri the uri ('filename')
  84. * @return associative array
  85. */
  86. public static function findWhereDAVDataIs($aid,$uri){
  87. $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? AND uri = ?' );
  88. $result = $stmt->execute(array($aid,$uri));
  89. return $result->fetchRow();
  90. }
  91. /**
  92. * @brief Adds a card
  93. * @param integer $id Addressbook id
  94. * @param string $data vCard file
  95. * @return insertid on success or null if card is not parseable.
  96. */
  97. public static function add($id,$data){
  98. $fn = null;
  99. $card = OC_VObject::parse($data);
  100. if(!is_null($card)){
  101. $fn = $card->getAsString('FN');
  102. if(!$fn){ // Fix missing 'FN' field.
  103. $n = $card->getAsString('N');
  104. if(!is_null($n)){
  105. $fn = join(' ', array_reverse(array_slice(explode(';', $n), 0, 2)));
  106. $card->setString('FN', $fn);
  107. OC_Log::write('contacts','OC_Contacts_VCard::add. Added missing \'FN\' field: '.$fn,OC_Log::DEBUG);
  108. } else {
  109. $fn = 'Unknown Name';
  110. }
  111. }
  112. $n = $card->getAsString('N');
  113. if(!$n){ // Fix missing 'N' field.
  114. $n = implode(';', array_reverse(array_slice(explode(' ', $fn), 0, 2))).';;;';
  115. $card->setString('N', $n);
  116. OC_Log::write('contacts','OC_Contacts_VCard::add. Added missing \'N\' field: '.$n,OC_Log::DEBUG);
  117. }
  118. $uid = $card->getAsString('UID');
  119. if(is_null($uid)){
  120. $card->setUID();
  121. $uid = $card->getAsString('UID');
  122. //$data = $card->serialize();
  123. };
  124. $uri = $uid.'.vcf';
  125. // Add product ID.
  126. $prodid = trim($card->getAsString('PRODID'));
  127. if(!$prodid) {
  128. $appinfo = OC_App::getAppInfo('contacts');
  129. $prodid = '//ownCloud//NONSGML '.$appinfo['name'].' '.$appinfo['version'].'//EN';
  130. $card->setString('PRODID', $prodid);
  131. }
  132. // VCARD must have a version
  133. $version = $card->getAsString('VERSION');
  134. // Add version if needed
  135. if(!$version){
  136. $card->add(new Sabre_VObject_Property('VERSION','3.0'));
  137. //$data = $card->serialize();
  138. }/* else {
  139. OC_Log::write('contacts','OC_Contacts_VCard::add. Version already set as: '.$version,OC_Log::DEBUG);
  140. }*/
  141. $now = new DateTime;
  142. $card->setString('REV', $now->format(DateTime::W3C));
  143. $data = $card->serialize();
  144. }
  145. else{
  146. // that's hard. Creating a UID and not saving it
  147. OC_Log::write('contacts','OC_Contacts_VCard::add. Error parsing VCard: '.$data,OC_Log::ERROR);
  148. return null; // Ditch cards that can't be parsed by Sabre.
  149. //$uid = self::createUID();
  150. //$uri = $uid.'.vcf';
  151. };
  152. $stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)' );
  153. $result = $stmt->execute(array($id,$fn,$data,$uri,time()));
  154. $newid = OC_DB::insertid('*PREFIX*contacts_cards');
  155. OC_Contacts_Addressbook::touch($id);
  156. return $newid;
  157. }
  158. /**
  159. * @brief Adds a card with the data provided by sabredav
  160. * @param integer $id Addressbook id
  161. * @param string $uri the uri the card will have
  162. * @param string $data vCard file
  163. * @return insertid
  164. */
  165. public static function addFromDAVData($id,$uri,$data){
  166. $fn = $n = null;
  167. $email = null;
  168. $card = OC_VObject::parse($data);
  169. if(!is_null($card)){
  170. foreach($card->children as $property){
  171. if($property->name == 'FN'){
  172. $fn = $property->value;
  173. }
  174. if($property->name == 'N'){
  175. $n = $property->value;
  176. }
  177. if($property->name == 'EMAIL' && is_null($email)){
  178. $email = $property->value;
  179. }
  180. }
  181. }
  182. if(!$fn) {
  183. if($n){
  184. $fn = join(' ', array_reverse(array_slice(explode(';', $n), 0, 2)));
  185. } elseif($email) {
  186. $fn = $email;
  187. } else {
  188. $fn = 'Unknown Name';
  189. }
  190. $card->addProperty('FN', $fn);
  191. $data = $card->serialize();
  192. OC_Log::write('contacts','OC_Contacts_VCard::add. Added missing \'FN\' field: '.$n,OC_Log::DEBUG);
  193. }
  194. if(!$n){ // Fix missing 'N' field.
  195. $n = implode(';', array_reverse(array_slice(explode(' ', $fn), 0, 2))).';;;';
  196. $card->setString('N', $n);
  197. $data = $card->serialize();
  198. OC_Log::write('contacts','OC_Contacts_VCard::add. Added missing \'N\' field: '.$n,OC_Log::DEBUG);
  199. }
  200. $stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)' );
  201. $result = $stmt->execute(array($id,$fn,$data,$uri,time()));
  202. OC_Contacts_Addressbook::touch($id);
  203. return OC_DB::insertid('*PREFIX*contacts_cards');
  204. }
  205. /**
  206. * @brief edits a card
  207. * @param integer $id id of card
  208. * @param string $data vCard file
  209. * @return boolean
  210. */
  211. public static function edit($id, $data){
  212. $oldcard = self::find($id);
  213. $fn = null;
  214. $card = OC_VObject::parse($data);
  215. if(!is_null($card)){
  216. foreach($card->children as $property){
  217. if($property->name == 'FN'){
  218. $fn = $property->value;
  219. break;
  220. }
  221. }
  222. } else {
  223. return false;
  224. }
  225. $now = new DateTime;
  226. $card->setString('REV', $now->format(DateTime::W3C));
  227. $data = $card->serialize();
  228. $stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_cards SET fullname = ?,carddata = ?, lastmodified = ? WHERE id = ?' );
  229. $result = $stmt->execute(array($fn,$data,time(),$id));
  230. OC_Contacts_Addressbook::touch($oldcard['addressbookid']);
  231. return true;
  232. }
  233. /**
  234. * @brief edits a card with the data provided by sabredav
  235. * @param integer $id Addressbook id
  236. * @param string $uri the uri of the card
  237. * @param string $data vCard file
  238. * @return boolean
  239. */
  240. public static function editFromDAVData($aid,$uri,$data){
  241. $oldcard = self::findWhereDAVDataIs($aid,$uri);
  242. $fn = null;
  243. $card = OC_VObject::parse($data);
  244. if(!is_null($card)){
  245. foreach($card->children as $property){
  246. if($property->name == 'FN'){
  247. $fn = $property->value;
  248. break;
  249. }
  250. }
  251. }
  252. $now = new DateTime;
  253. $card->setString('REV', $now->format(DateTime::W3C));
  254. $data = $card->serialize();
  255. $stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_cards SET fullname = ?,carddata = ?, lastmodified = ? WHERE id = ?' );
  256. $result = $stmt->execute(array($fn,$data,time(),$oldcard['id']));
  257. OC_Contacts_Addressbook::touch($oldcard['addressbookid']);
  258. return true;
  259. }
  260. /**
  261. * @brief deletes a card
  262. * @param integer $id id of card
  263. * @return boolean
  264. */
  265. public static function delete($id){
  266. // FIXME: Add error checking.
  267. $stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE id = ?' );
  268. $stmt->execute(array($id));
  269. return true;
  270. }
  271. /**
  272. * @brief Creates a UID
  273. * @return string
  274. */
  275. public static function createUID(){
  276. return substr(md5(rand().time()),0,10);
  277. }
  278. /**
  279. * @brief deletes a card with the data provided by sabredav
  280. * @param integer $aid Addressbook id
  281. * @param string $uri the uri of the card
  282. * @return boolean
  283. */
  284. public static function deleteFromDAVData($aid,$uri){
  285. // FIXME: Add error checking. Deleting a card gives an Kontact/Akonadi error.
  286. $stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE addressbookid = ? AND uri=?' );
  287. $stmt->execute(array($aid,$uri));
  288. return true;
  289. }
  290. /**
  291. * @brief Data structure of vCard
  292. * @param object $property
  293. * @return associative array
  294. *
  295. * look at code ...
  296. */
  297. public static function structureContact($object){
  298. $details = array();
  299. foreach($object->children as $property){
  300. $temp = self::structureProperty($property);
  301. if(array_key_exists($property->name,$details)){
  302. $details[$property->name][] = $temp;
  303. }
  304. else{
  305. $details[$property->name] = array($temp);
  306. }
  307. }
  308. return $details;
  309. }
  310. /**
  311. * @brief Data structure of properties
  312. * @param object $property
  313. * @return associative array
  314. *
  315. * returns an associative array with
  316. * ['name'] name of property
  317. * ['value'] htmlspecialchars escaped value of property
  318. * ['parameters'] associative array name=>value
  319. * ['checksum'] checksum of whole property
  320. * NOTE: $value is not escaped anymore. It shouldn't make any difference
  321. * but we should look out for any problems.
  322. */
  323. public static function structureProperty($property){
  324. $value = $property->value;
  325. //$value = htmlspecialchars($value);
  326. if($property->name == 'ADR' || $property->name == 'N'){
  327. $value = OC_VObject::unescapeSemicolons($value);
  328. }
  329. $temp = array(
  330. 'name' => $property->name,
  331. 'value' => $value,
  332. 'parameters' => array(),
  333. 'checksum' => md5($property->serialize()));
  334. foreach($property->parameters as $parameter){
  335. // Faulty entries by kaddressbook
  336. if($parameter->name == 'TYPE' && $parameter->value == 'PREF'){
  337. $parameter->name = 'PREF';
  338. $parameter->value = '1';
  339. }
  340. if ($property->name == 'TEL' && $parameter->name == 'TYPE'){
  341. if (isset($temp['parameters'][$parameter->name])){
  342. $temp['parameters'][$parameter->name][] = $parameter->value;
  343. }
  344. else{
  345. $temp['parameters'][$parameter->name] = array($parameter->value);
  346. }
  347. }
  348. else{
  349. $temp['parameters'][$parameter->name] = $parameter->value;
  350. }
  351. }
  352. return $temp;
  353. }
  354. }