diff --git a/.gitignore b/.gitignore index 5fe71a7a8..358114a44 100644 --- a/.gitignore +++ b/.gitignore @@ -1,26 +1,27 @@ -favicon.* -.htconfig.php -\#* -include/jquery-1.4.2.min.js -*.log -*.out -*.version* -favicon.* -home.html -addon -*~ - -#ignore documentation, it should be newly built -doc/api - -#ignore reports, should be generted with every build -report/ - -#ignore config files from eclipse, we don't want IDE files in our repository -.project -.buildpath -.externalToolBuilders -.settings -#ignore OSX .DS_Store files -.DS_Store - +favicon.* +.htconfig.php +\#* +include/jquery-1.4.2.min.js +*.log +*.out +*.version* +favicon.* +home.html +addon +*~ + +#ignore documentation, it should be newly built +doc/api + +#ignore reports, should be generted with every build +report/ + +#ignore config files from eclipse, we don't want IDE files in our repository +.project +.buildpath +.externalToolBuilders +.settings +#ignore OSX .DS_Store files +.DS_Store + +/nbproject/private/ \ No newline at end of file diff --git a/boot.php b/boot.php index 2628c0f3f..addc0c107 100644 --- a/boot.php +++ b/boot.php @@ -11,7 +11,7 @@ require_once('include/cache.php'); require_once('library/Mobile_Detect/Mobile_Detect.php'); define ( 'FRIENDICA_PLATFORM', 'Friendica'); -define ( 'FRIENDICA_VERSION', '3.0.1517' ); +define ( 'FRIENDICA_VERSION', '3.0.1518' ); define ( 'DFRN_PROTOCOL_VERSION', '2.23' ); define ( 'DB_UPDATE_VERSION', 1156 ); @@ -361,17 +361,26 @@ if(! class_exists('App')) { // Allow themes to control internal parameters // by changing App values in theme.php - // - // Possibly should make these part of the plugin - // system, but it seems like overkill to invoke - // all the plugin machinery just to change a couple - // of values + public $sourcename = ''; public $videowidth = 425; public $videoheight = 350; public $force_max_items = 0; public $theme_thread_allow = true; + // An array for all theme-controllable parameters + // Mostly unimplemented yet. Only options 'stylesheet' and + // beyond are used. + + public $theme = array( + 'sourcename' => '', + 'videowidth' => 425, + 'videoheight' => 350, + 'force_max_items' => 0, + 'thread_allow' => true, + 'stylesheet' => '' + ); + private $scheme; private $hostname; private $baseurl; @@ -385,7 +394,7 @@ if(! class_exists('App')) { function __construct() { - global $default_timezone; + global $default_timezone, $argv, $argc; $this->timezone = ((x($default_timezone)) ? $default_timezone : 'UTC'); @@ -428,6 +437,9 @@ if(! class_exists('App')) { if(isset($path) && strlen($path) && ($path != $this->path)) $this->path = $path; } + if (is_array($argv) && $argc>1 && !x($_SERVER,'SERVER_NAME') && substr(end($argv), 0, 4)=="http" ) { + $this->set_baseurl(array_pop($argv) ); + } set_include_path( "include/$this->hostname" . PATH_SEPARATOR @@ -436,6 +448,7 @@ if(! class_exists('App')) { . 'library/phpsec' . PATH_SEPARATOR . 'library/langdet' . PATH_SEPARATOR . '.' ); + if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'],0,2) === "q=") { $this->query_string = substr($_SERVER['QUERY_STRING'],2); @@ -576,6 +589,13 @@ if(! class_exists('App')) { $interval = 40000; $this->page['title'] = $this->config['sitename']; + + /* put the head template at the beginning of page['htmlhead'] + * since the code added by the modules frequently depends on it + * being first + */ + if(!isset($this->page['htmlhead'])) + $this->page['htmlhead'] = ''; $tpl = get_markup_template('head.tpl'); $this->page['htmlhead'] = replace_macros($tpl,array( '$baseurl' => $this->get_baseurl(), // FIXME for z_path!!!! @@ -586,14 +606,16 @@ if(! class_exists('App')) { '$showmore' => t('show more'), '$showfewer' => t('show fewer'), '$update_interval' => $interval - )); + )) . $this->page['htmlhead']; } function init_page_end() { + if(!isset($this->page['end'])) + $this->page['end'] = ''; $tpl = get_markup_template('end.tpl'); $this->page['end'] = replace_macros($tpl,array( '$baseurl' => $this->get_baseurl() // FIXME for z_path!!!! - )); + )) . $this->page['end']; } function set_curl_code($code) { @@ -913,6 +935,7 @@ if(! function_exists('login')) { $tpl = get_markup_template("login.tpl"); $_SESSION['return_url'] = $a->query_string; + $a->module = 'login'; } @@ -1501,9 +1524,15 @@ if(! function_exists('proc_run')) { if(count($args) && $args[0] === 'php') $args[0] = ((x($a->config,'php_path')) && (strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php'); - for($x = 0; $x < count($args); $x ++) + + // add baseurl to args. cli scripts can't construct it + $args[] = $a->get_baseurl(); + + for($x = 0; $x < count($args); $x ++) $args[$x] = escapeshellarg($args[$x]); + + $cmdline = implode($args," "); if(get_config('system','proc_windows')) proc_close(proc_open('cmd /c start /b ' . $cmdline,array(),$foo,dirname(__FILE__))); diff --git a/include/conversation.php b/include/conversation.php index 43d20a401..1de77feb1 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -865,11 +865,15 @@ function format_like($cnt,$arr,$type,$id) { $total = count($arr); if($total >= MAX_LIKERS) $arr = array_slice($arr, 0, MAX_LIKERS - 1); - if($total < MAX_LIKERS) - $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1]; - $str = implode(', ', $arr); - if($total >= MAX_LIKERS) + if($total < MAX_LIKERS) { + $last = t('and') . ' ' . $arr[count($arr)-1]; + $arr2 = array_slice($arr, 0, -1); + $str = implode(', ', $arr2) . ' ' . $last; + } + if($total >= MAX_LIKERS) { + $str = implode(', ', $arr); $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS ); + } $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str)); $o .= "\t" . '
'; } diff --git a/include/cronhooks.php b/include/cronhooks.php index 37541f013..27cf642b2 100644 --- a/include/cronhooks.php +++ b/include/cronhooks.php @@ -3,7 +3,7 @@ require_once("boot.php"); -function cronhooks_run($argv, $argc){ +function cronhooks_run(&$argv, &$argc){ global $a, $db; if(is_null($a)) { diff --git a/include/dba.php b/include/dba.php index 8d224b570..9f2dab519 100644 --- a/include/dba.php +++ b/include/dba.php @@ -232,8 +232,9 @@ function q($sql) { if($db && $db->connected) { $stmt = vsprintf($sql,$args); + //logger("dba: q: $stmt", LOGGER_ALL); if($stmt === false) - logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true)); + logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG); return $db->q($stmt); } diff --git a/include/delivery.php b/include/delivery.php index 613453bc7..428035973 100644 --- a/include/delivery.php +++ b/include/delivery.php @@ -3,7 +3,7 @@ require_once("boot.php"); require_once('include/queue_fn.php'); require_once('include/html2plain.php'); -function delivery_run($argv, $argc){ +function delivery_run(&$argv, &$argc){ global $a, $db; if(is_null($a)){ diff --git a/include/directory.php b/include/directory.php index 45386183c..356118bb0 100644 --- a/include/directory.php +++ b/include/directory.php @@ -1,7 +1,7 @@ get_feed_tags( NAMESPACE_DFRN, 'relocate' ); if(isset($reloc[0]['child'][NAMESPACE_DFRN])) { @@ -2321,23 +2322,79 @@ function local_delivery($importer,$data) { $newloc['cid'] = $importer['id']; $newloc['name'] = notags(unxmlify($base['name'][0]['data'])); $newloc['photo'] = notags(unxmlify($base['photo'][0]['data'])); + $newloc['thumb'] = notags(unxmlify($base['thumb'][0]['data'])); + $newloc['micro'] = notags(unxmlify($base['micro'][0]['data'])); $newloc['url'] = notags(unxmlify($base['url'][0]['data'])); $newloc['request'] = notags(unxmlify($base['request'][0]['data'])); $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data'])); $newloc['notify'] = notags(unxmlify($base['notify'][0]['data'])); $newloc['poll'] = notags(unxmlify($base['poll'][0]['data'])); - $newloc['site-pubkey'] = notags(unxmlify($base['site-pubkey'][0]['data'])); - $newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data'])); - $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data'])); + $newloc['sitepubkey'] = notags(unxmlify($base['sitepubkey'][0]['data'])); + /** relocated user must have original key pair */ + /*$newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data'])); + $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));*/ + + logger("items:relocate contact ".print_r($newloc, true).print_r($importer, true), LOGGER_DEBUG); + + // update contact + $r = q("SELECT photo, url FROM contact WHERE id=%d AND uid=%d;", + intval($importer['id']), + intval($importer['importer_uid'])); + if ($r === false) + return 1; + $old = $r[0]; + + $x = q("UPDATE contact SET + name = '%s', + photo = '%s', + thumb = '%s', + micro = '%s', + url = '%s', + request = '%s', + confirm = '%s', + notify = '%s', + poll = '%s', + `site-pubkey` = '%s' + WHERE id=%d AND uid=%d;", + dbesc($newloc['name']), + dbesc($newloc['photo']), + dbesc($newloc['thumb']), + dbesc($newloc['micro']), + dbesc($newloc['url']), + dbesc($newloc['request']), + dbesc($newloc['confirm']), + dbesc($newloc['notify']), + dbesc($newloc['poll']), + dbesc($newloc['sitepubkey']), + intval($importer['id']), + intval($importer['importer_uid'])); + + if ($x === false) + return 1; + // update items + $fields = array( + 'owner-link' => array($old['url'], $newloc['url']), + 'author-link' => array($old['url'], $newloc['url']), + 'owner-avatar' => array($old['photo'], $newloc['photo']), + 'author-avatar' => array($old['photo'], $newloc['photo']), + ); + foreach ($fields as $n=>$f){ + $x = q("UPDATE item SET `%s`='%s' WHERE `%s`='%s' AND uid=%d", + $n, dbesc($f[1]), + $n, dbesc($f[0]), + intval($importer['importer_uid'])); + if ($x === false) + return 1; + } // TODO // merge with current record, current contents have priority // update record, set url-updated // update profile photos // schedule a scan? - + return 0; } -*/ + // handle friend suggestion notification @@ -3238,7 +3295,7 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) { if(is_array($contact)) { if(($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING) || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) { - $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1", + $r = q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1", intval(CONTACT_IS_FRIEND), intval($contact['id']), intval($importer['uid']) diff --git a/include/notifier.php b/include/notifier.php index a999c3297..f05bac196 100644 --- a/include/notifier.php +++ b/include/notifier.php @@ -1,5 +1,4 @@ get_baseurl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']]; + } + unset($rp, $ext); + + $atom .= replace_macros($sugg_template, array( + '$name' => xmlify($owner['name']), + '$photo' => xmlify($photos[4]), + '$thumb' => xmlify($photos[5]), + '$micro' => xmlify($photos[6]), + '$url' => xmlify($owner['url']), + '$request' => xmlify($owner['request']), + '$confirm' => xmlify($owner['confirm']), + '$notify' => xmlify($owner['notify']), + '$poll' => xmlify($owner['poll']), + '$sitepubkey' => xmlify(get_config('system','site_pubkey')), + //'$pubkey' => xmlify($owner['pubkey']), + //'$prvkey' => xmlify($owner['prvkey']), + )); + $recipients_relocate = q("SELECT * FROM contact WHERE uid = %d AND self = 0 AND network = '%s'" , intval($uid), NETWORK_DFRN); + unset($photos); + } else { if($followup) { foreach($items as $item) { // there is only one item @@ -493,9 +540,12 @@ function notifier_run($argv, $argc){ else $recip_str = implode(', ', $recipients); - $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ", - dbesc($recip_str) - ); + if ($relocate) + $r = $recipients_relocate; + else + $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ", + dbesc($recip_str) + ); require_once('include/salmon.php'); @@ -507,7 +557,7 @@ function notifier_run($argv, $argc){ if(count($r)) { foreach($r as $contact) { - if((! $mail) && (! $fsuggest) && (! $followup) && (! $contact['self'])) { + if((! $mail) && (! $fsuggest) && (! $followup) && (!$relocate) && (! $contact['self'])) { if(($contact['network'] === NETWORK_DIASPORA) && ($public_message)) continue; q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )", @@ -544,7 +594,7 @@ function notifier_run($argv, $argc){ // potentially more than one recipient. Start a new process and space them out a bit. // we will deliver single recipient types of message and email recipients here. - if((! $mail) && (! $fsuggest) && (! $followup)) { + if((! $mail) && (! $fsuggest) && (!$relocate) && (! $followup)) { $this_batch[] = $contact['id']; @@ -559,7 +609,7 @@ function notifier_run($argv, $argc){ $deliver_status = 0; - logger("main delivery by notifier: followup=$followup mail=$mail fsuggest=$fsuggest"); + logger("main delivery by notifier: followup=$followup mail=$mail fsuggest=$fsuggest relocate=$relocate"); switch($contact['network']) { case NETWORK_DFRN: @@ -916,6 +966,7 @@ function notifier_run($argv, $argc){ return; } + if (array_search(__file__,get_included_files())===0){ notifier_run($argv,$argc); killme(); diff --git a/include/onepoll.php b/include/onepoll.php index 9fbef168c..a50d234e2 100644 --- a/include/onepoll.php +++ b/include/onepoll.php @@ -2,7 +2,7 @@ require_once("boot.php"); -function onepoll_run($argv, $argc){ +function onepoll_run(&$argv, &$argc){ global $a, $db; if(is_null($a)) { @@ -294,34 +294,147 @@ function onepoll_run($argv, $argc){ $metas = email_msg_meta($mbox,implode(',',$msgs)); if(count($metas) != count($msgs)) { logger("onepoll: for " . $mailconf[0]['user'] . " there are ". count($msgs) . " messages but received " . count($metas) . " metas", LOGGER_DEBUG); - break; } - $msgs = array_combine($msgs, $metas); + else { + $msgs = array_combine($msgs, $metas); - foreach($msgs as $msg_uid => $meta) { - logger("Mail: Parsing mail ".$msg_uid, LOGGER_DATA); + foreach($msgs as $msg_uid => $meta) { + logger("Mail: Parsing mail ".$msg_uid, LOGGER_DATA); - $datarray = array(); -// $meta = email_msg_meta($mbox,$msg_uid); -// $headers = email_msg_headers($mbox,$msg_uid); + $datarray = array(); + // $meta = email_msg_meta($mbox,$msg_uid); + // $headers = email_msg_headers($mbox,$msg_uid); - $datarray['uri'] = msgid2iri(trim($meta->message_id,'<>')); + $datarray['uri'] = msgid2iri(trim($meta->message_id,'<>')); - // Have we seen it before? - $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", - intval($importer_uid), - dbesc($datarray['uri']) - ); + // Have we seen it before? + $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", + intval($importer_uid), + dbesc($datarray['uri']) + ); - if(count($r)) { - logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user'],LOGGER_DEBUG); - if($meta->deleted && ! $r[0]['deleted']) { - q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1", - dbesc(datetime_convert()), - intval($r[0]['id']) - ); + if(count($r)) { + logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user'],LOGGER_DEBUG); + if($meta->deleted && ! $r[0]['deleted']) { + q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1", + dbesc(datetime_convert()), + intval($r[0]['id']) + ); + } + /*switch ($mailconf[0]['action']) { + case 0: + logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user'].". Doing nothing.", LOGGER_DEBUG); + break; + case 1: + logger("Mail: Deleting ".$msg_uid." for ".$mailconf[0]['user']); + imap_delete($mbox, $msg_uid, FT_UID); + break; + case 2: + logger("Mail: Mark as seen ".$msg_uid." for ".$mailconf[0]['user']); + imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID); + break; + case 3: + logger("Mail: Moving ".$msg_uid." to ".$mailconf[0]['movetofolder']." for ".$mailconf[0]['user']); + imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID); + if ($mailconf[0]['movetofolder'] != "") + imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID); + break; + }*/ + continue; } - /*switch ($mailconf[0]['action']) { + + + // look for a 'references' or an 'in-reply-to' header and try to match with a parent item we have locally. + + // $raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : ''); + $raw_refs = ((property_exists($meta,'references')) ? str_replace("\t",'',$meta->references) : ''); + if(! trim($raw_refs)) + $raw_refs = ((property_exists($meta,'in_reply_to')) ? str_replace("\t",'',$meta->in_reply_to) : ''); + $raw_refs = trim($raw_refs); // Don't allow a blank reference in $refs_arr + + if($raw_refs) { + $refs_arr = explode(' ', $raw_refs); + if(count($refs_arr)) { + for($x = 0; $x < count($refs_arr); $x ++) + $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x]))) . "'"; + } + $qstr = implode(',',$refs_arr); + $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( $qstr ) AND `uid` = %d LIMIT 1", + intval($importer_uid) + ); + if(count($r)) + $datarray['parent-uri'] = $r[0]['parent-uri']; // Set the parent as the top-level item + // $datarray['parent-uri'] = $r[0]['uri']; + } + + + if(! x($datarray,'parent-uri')) + $datarray['parent-uri'] = $datarray['uri']; + + // Decoding the header + $subject = imap_mime_header_decode($meta->subject); + $datarray['title'] = ""; + foreach($subject as $subpart) + if ($subpart->charset != "default") + $datarray['title'] .= iconv($subpart->charset, 'UTF-8//IGNORE', $subpart->text); + else + $datarray['title'] .= $subpart->text; + + $datarray['title'] = notags(trim($datarray['title'])); + + //$datarray['title'] = notags(trim($meta->subject)); + $datarray['created'] = datetime_convert('UTC','UTC',$meta->date); + + // Is it reply? + $reply = ((substr(strtolower($datarray['title']), 0, 3) == "re:") or + (substr(strtolower($datarray['title']), 0, 3) == "re-") or + (raw_refs != "")); + + $r = email_get_msg($mbox,$msg_uid, $reply); + if(! $r) { + logger("Mail: can't fetch msg ".$msg_uid." for ".$mailconf[0]['user']); + continue; + } + $datarray['body'] = escape_tags($r['body']); + + logger("Mail: Importing ".$msg_uid." for ".$mailconf[0]['user']); + + // some mailing lists have the original author as 'from' - add this sender info to msg body. + // todo: adding a gravatar for the original author would be cool + + if(! stristr($meta->from,$contact['addr'])) { + $from = imap_mime_header_decode($meta->from); + $fromdecoded = ""; + foreach($from as $frompart) + if ($frompart->charset != "default") + $fromdecoded .= iconv($frompart->charset, 'UTF-8//IGNORE', $frompart->text); + else + $fromdecoded .= $frompart->text; + + $datarray['body'] = "[b]".t('From: ') . escape_tags($fromdecoded) . "[/b]\n\n" . $datarray['body']; + } + + $datarray['uid'] = $importer_uid; + $datarray['contact-id'] = $contact['id']; + if($datarray['parent-uri'] === $datarray['uri']) + $datarray['private'] = 1; + if(($contact['network'] === NETWORK_MAIL) && (! get_pconfig($importer_uid,'system','allow_public_email_replies'))) { + $datarray['private'] = 1; + $datarray['allow_cid'] = '<' . $contact['id'] . '>'; + } + $datarray['author-name'] = $contact['name']; + $datarray['author-link'] = 'mailbox'; + $datarray['author-avatar'] = $contact['photo']; + + $stored_item = item_store($datarray); + q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d", + dbesc($datarray['parent-uri']), + intval($importer_uid) + ); + q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1", + intval($stored_item) + ); + switch ($mailconf[0]['action']) { case 0: logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user'].". Doing nothing.", LOGGER_DEBUG); break; @@ -339,119 +452,7 @@ function onepoll_run($argv, $argc){ if ($mailconf[0]['movetofolder'] != "") imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID); break; - }*/ - continue; - } - - - // look for a 'references' or an 'in-reply-to' header and try to match with a parent item we have locally. - -// $raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : ''); - $raw_refs = ((property_exists($meta,'references')) ? str_replace("\t",'',$meta->references) : ''); - if(! trim($raw_refs)) - $raw_refs = ((property_exists($meta,'in_reply_to')) ? str_replace("\t",'',$meta->in_reply_to) : ''); - $raw_refs = trim($raw_refs); // Don't allow a blank reference in $refs_arr - - if($raw_refs) { - $refs_arr = explode(' ', $raw_refs); - if(count($refs_arr)) { - for($x = 0; $x < count($refs_arr); $x ++) - $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x]))) . "'"; } - $qstr = implode(',',$refs_arr); - $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( $qstr ) AND `uid` = %d LIMIT 1", - intval($importer_uid) - ); - if(count($r)) - $datarray['parent-uri'] = $r[0]['parent-uri']; // Set the parent as the top-level item -// $datarray['parent-uri'] = $r[0]['uri']; - } - - - if(! x($datarray,'parent-uri')) - $datarray['parent-uri'] = $datarray['uri']; - - // Decoding the header - $subject = imap_mime_header_decode($meta->subject); - $datarray['title'] = ""; - foreach($subject as $subpart) - if ($subpart->charset != "default") - $datarray['title'] .= iconv($subpart->charset, 'UTF-8//IGNORE', $subpart->text); - else - $datarray['title'] .= $subpart->text; - - $datarray['title'] = notags(trim($datarray['title'])); - - //$datarray['title'] = notags(trim($meta->subject)); - $datarray['created'] = datetime_convert('UTC','UTC',$meta->date); - - // Is it reply? - $reply = ((substr(strtolower($datarray['title']), 0, 3) == "re:") or - (substr(strtolower($datarray['title']), 0, 3) == "re-") or - (raw_refs != "")); - - $r = email_get_msg($mbox,$msg_uid, $reply); - if(! $r) { - logger("Mail: can't fetch msg ".$msg_uid." for ".$mailconf[0]['user']); - continue; - } - $datarray['body'] = escape_tags($r['body']); - - logger("Mail: Importing ".$msg_uid." for ".$mailconf[0]['user']); - - // some mailing lists have the original author as 'from' - add this sender info to msg body. - // todo: adding a gravatar for the original author would be cool - - if(! stristr($meta->from,$contact['addr'])) { - $from = imap_mime_header_decode($meta->from); - $fromdecoded = ""; - foreach($from as $frompart) - if ($frompart->charset != "default") - $fromdecoded .= iconv($frompart->charset, 'UTF-8//IGNORE', $frompart->text); - else - $fromdecoded .= $frompart->text; - - $datarray['body'] = "[b]".t('From: ') . escape_tags($fromdecoded) . "[/b]\n\n" . $datarray['body']; - } - - $datarray['uid'] = $importer_uid; - $datarray['contact-id'] = $contact['id']; - if($datarray['parent-uri'] === $datarray['uri']) - $datarray['private'] = 1; - if(($contact['network'] === NETWORK_MAIL) && (! get_pconfig($importer_uid,'system','allow_public_email_replies'))) { - $datarray['private'] = 1; - $datarray['allow_cid'] = '<' . $contact['id'] . '>'; - } - $datarray['author-name'] = $contact['name']; - $datarray['author-link'] = 'mailbox'; - $datarray['author-avatar'] = $contact['photo']; - - $stored_item = item_store($datarray); - q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d", - dbesc($datarray['parent-uri']), - intval($importer_uid) - ); - q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1", - intval($stored_item) - ); - switch ($mailconf[0]['action']) { - case 0: - logger("Mail: Seen before ".$msg_uid." for ".$mailconf[0]['user'].". Doing nothing.", LOGGER_DEBUG); - break; - case 1: - logger("Mail: Deleting ".$msg_uid." for ".$mailconf[0]['user']); - imap_delete($mbox, $msg_uid, FT_UID); - break; - case 2: - logger("Mail: Mark as seen ".$msg_uid." for ".$mailconf[0]['user']); - imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID); - break; - case 3: - logger("Mail: Moving ".$msg_uid." to ".$mailconf[0]['movetofolder']." for ".$mailconf[0]['user']); - imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID); - if ($mailconf[0]['movetofolder'] != "") - imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID); - break; } } } diff --git a/include/poller.php b/include/poller.php index 1af23eb13..d5efa36a8 100644 --- a/include/poller.php +++ b/include/poller.php @@ -3,7 +3,7 @@ require_once("boot.php"); -function poller_run($argv, $argc){ +function poller_run(&$argv, &$argc){ global $a, $db; if(is_null($a)) { diff --git a/include/queue.php b/include/queue.php index ba3babe70..b703f930b 100644 --- a/include/queue.php +++ b/include/queue.php @@ -2,7 +2,7 @@ require_once("boot.php"); require_once('include/queue_fn.php'); -function queue_run($argv, $argc){ +function queue_run(&$argv, &$argc){ global $a, $db; if(is_null($a)){ diff --git a/include/uimport.php b/include/uimport.php new file mode 100644 index 000000000..942793168 --- /dev/null +++ b/include/uimport.php @@ -0,0 +1,237 @@ +mysqli){ + $thedb = $db->getdb(); + return $thedb->insert_id; + } else { + return mysql_insert_id(); + } + } + + function last_error(){ + global $db; + return $db->error; + } + + function db_import_assoc($table, $arr){ + if (IMPORT_DEBUG) return true; + if (isset($arr['id'])) unset($arr['id']); + $cols = implode("`,`", array_map('dbesc', array_keys($arr))); + $vals = implode("','", array_map('dbesc', array_values($arr))); + $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')"; + logger("uimport: $query",LOGGER_TRACE); + return q($query); + } + +function import_cleanup($newuid) { + q("DELETE FROM `user` WHERE uid = %d", $newuid); + q("DELETE FROM `contact` WHERE uid = %d", $newuid); + q("DELETE FROM `profile` WHERE uid = %d", $newuid); + q("DELETE FROM `photo` WHERE uid = %d", $newuid); + q("DELETE FROM `group` WHERE uid = %d", $newuid); + q("DELETE FROM `group_member` WHERE uid = %d", $newuid); + q("DELETE FROM `pconfig` WHERE uid = %d", $newuid); + +} + +function import_account(&$a, $file) { + logger("Start user import from ".$file['tmp_name']); + /* + STEPS + 1. checks + 2. replace old baseurl with new baseurl + 3. import data (look at user id and contacts id) + 4. archive non-dfrn contacts + 5. send message to dfrn contacts + */ + + $account = json_decode(file_get_contents($file['tmp_name']), true); + if ($account===null) { + notice(t("Error decoding account file")); + return; + } + + + if (!x($account, 'version')) { + notice(t("Error! No version data in file! This is not a Friendica account file?")); + return; + } + + if ($account['schema'] != DB_UPDATE_VERSION) { + notice(t("Error! I can't import this file: DB schema version is not compatible.")); + return; + } + + + $oldbaseurl = $account['baseurl']; + $newbaseurl = $a->get_baseurl(); + $olduid = $account['user']['uid']; + + unset($account['user']['uid']); + foreach($account['user'] as $k => &$v) { + $v = str_replace($oldbaseurl, $newbaseurl, $v); + } + + + // import user + $r = db_import_assoc('user', $account['user']); + if ($r===false) { + //echo ""; var_dump($r, $query, mysql_error()); killme(); + logger("uimport:insert user : ERROR : ".last_error(), LOGGER_NORMAL); + notice(t("User creation error")); + return; + } + $newuid = last_insert_id(); + //~ $newuid = 1; + + + + foreach($account['profile'] as &$profile) { + foreach($profile as $k=>&$v) { + $v = str_replace($oldbaseurl, $newbaseurl, $v); + foreach(array("profile","avatar") as $k) + $v = str_replace($newbaseurl."/photo/".$k."/".$olduid.".jpg", $newbaseurl."/photo/".$k."/".$newuid.".jpg", $v); + } + $profile['uid'] = $newuid; + $r = db_import_assoc('profile', $profile); + if ($r===false) { + logger("uimport:insert profile ".$profile['profile-name']." : ERROR : ".last_error(), LOGGER_NORMAL); + info(t("User profile creation error")); + import_cleanup($newuid); + return; + } + } + + $errorcount=0; + foreach($account['contact'] as &$contact) { + if ($contact['uid'] == $olduid && $contact['self'] == '1'){ + foreach($contact as $k=>&$v) { + $v = str_replace($oldbaseurl, $newbaseurl, $v); + foreach(array("profile","avatar","micro") as $k) + $v = str_replace($newbaseurl."/photo/".$k."/".$olduid.".jpg", $newbaseurl."/photo/".$k."/".$newuid.".jpg", $v); + } + } + if ($contact['uid'] == $olduid && $contact['self'] == '0') { + switch ($contact['network']){ + case NETWORK_DFRN: + // send relocate message (below) + break; + case NETWORK_ZOT: + // TODO handle zot network + break; + case NETWORK_MAIL2: + // TODO ? + break; + case NETWORK_FEED: + case NETWORK_MAIL: + // Nothing to do + break; + default: + // archive other contacts + $contact['archive'] = "1"; + } + } + $contact['uid'] = $newuid; + $r = db_import_assoc('contact', $contact); + if ($r===false) { + logger("uimport:insert contact ".$contact['nick'].",".$contact['network']." : ERROR : ".last_error(), LOGGER_NORMAL); + $errorcount++; + } else { + $contact['newid'] = last_insert_id(); + } + } + if ($errorcount>0) { + notice( sprintf(tt("%d contact not imported", "%d contacts not imported", $errorcount), $errorcount) ); + } + + foreach($account['group'] as &$group) { + $group['uid'] = $newuid; + $r = db_import_assoc('group', $group); + if ($r===false) { + logger("uimport:insert group ".$group['name']." : ERROR : ".last_error(), LOGGER_NORMAL); + } else { + $group['newid'] = last_insert_id(); + } + } + + foreach($account['group_member'] as &$group_member) { + $group_member['uid'] = $newuid; + + $import = 0; + foreach($account['group'] as $group) { + if ($group['id'] == $group_member['gid'] && isset($group['newid'])) { + $group_member['gid'] = $group['newid']; + $import++; + break; + } + } + foreach($account['contact'] as $contact) { + if ($contact['id'] == $group_member['contact-id'] && isset($contact['newid'])) { + $group_member['contact-id'] = $contact['newid']; + $import++; + break; + } + } + if ($import==2) { + $r = db_import_assoc('group_member', $group_member); + if ($r===false) { + logger("uimport:insert group member ".$group_member['id']." : ERROR : ".last_error(), LOGGER_NORMAL); + } + } + } + + + + + + foreach($account['photo'] as &$photo) { + $photo['uid'] = $newuid; + $photo['data'] = hex2bin($photo['data']); + + $p = new Photo($photo['data'], $photo['type']); + $r = $p->store( + $photo['uid'], + $photo['contact-id'], //0 + $photo['resource-id'], + $photo['filename'], + $photo['album'], + $photo['scale'], + $photo['profile'], //1 + $photo['allow_cid'], + $photo['allow_gid'], + $photo['deny_cid'], + $photo['deny_gid'] + ); + + if ($r===false) { + logger("uimport:insert photo ".$photo['resource-id'].",". $photo['scale']. " : ERROR : ".last_error(), LOGGER_NORMAL); + } + } + + foreach($account['pconfig'] as &$pconfig) { + $pconfig['uid'] = $newuid; + $r = db_import_assoc('pconfig', $pconfig); + if ($r===false) { + logger("uimport:insert pconfig ".$pconfig['id']. " : ERROR : ".last_error(), LOGGER_NORMAL); + } + } + + // send relocate messages + proc_run('php', 'include/notifier.php', 'relocate' , $newuid); + + info(t("Done. You can now login with your username and password")); + goaway( $a->get_baseurl() ."/login"); + + +} diff --git a/index.php b/index.php index 7d7674530..bd5b3e396 100644 --- a/index.php +++ b/index.php @@ -115,19 +115,9 @@ if(! x($_SESSION,'authenticated')) header('X-Account-Management-Status: none'); -/* - * Create the page head after setting the language - * and getting any auth credentials - */ - -$a->init_pagehead(); - -/** - * Build the page ending -- this is stuff that goes right before - * the closing