Changeset 624

Show
Ignore:
Timestamp:
07/16/06 15:38:43 (2 years ago)
Author:
coolengineer
Message:

BlogAPI/blogger.php:

Move _get_post function to index.php

BlogAPI/metaweblog.php:

Change build post instance via _make_post

BlogAPI/index.php:

Include components after checking existance.
Added _make_post for building post instance.
Change selecting randome name of attached files.

Location:
sandbox/plugins/BlogAPI
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • sandbox/plugins/BlogAPI/blogger.php

    r604 r624  
    139139} 
    140140 
    141 function _get_post( $post, $type = "bl" ) 
    142 {  
    143     DEBUG( "Enter: " . __FUNCTION__ . "\n" ); 
    144     $params = func_get_args(); 
    145     global $service, $hostURL, $blogURL; 
    146     return array(  
    147                 "userid" => "", 
    148                 "dateCreated" => _dateiso8601( $post->created ), 
    149                 "datePosted" => _dateiso8601( $post->published ), 
    150                 "dateModified" => _dateiso8601( $post->modified ), 
    151                 "title" =>  _escape_content($post->title), 
    152                 "postid" => $post->id, 
    153                 "categories" => array( _getCategoryNameById($post->category) ), 
    154                 "link" => $hostURL . $blogURL . "/" . $post->id , 
    155                 "permaLink" => $hostURL . $blogURL . "/" . $post->id , 
    156                 "description" => ($type == "mt" ? $post->content : "" ), 
    157                 "content" => $post->content 
    158                 ); 
    159 } 
    160  
    161141function blogger_getRecentPosts() 
    162142{ 
  • sandbox/plugins/BlogAPI/index.php

    r604 r624  
    4141    if( TATTERTOOLS_VERSION < "1.0.6" && file_exists( $blogapi_dir . "/$name.php" ) ) 
    4242    { 
    43         include_once ( $blogapi_dir . "/$name.php"); 
     43        DEBUG ( $blogapi_dir . "/$name.php\n"); 
     44        include_once( $blogapi_dir . "/$name.php"); 
    4445    } 
    4546    else 
    4647    { 
    47         include_once ( $blogapi_dir . "/../../components/$name.php"); 
     48        $componet_file = $blogapi_dir . "/../../components/$name.php"; 
     49        if( file_exists( $componet_file ) ) 
     50        { 
     51            include_once( $componet_file ); 
     52        } 
     53        else 
     54        { 
     55            print( "File($componet_file) doesn't exist\n" ); 
     56        } 
    4857    } 
    4958} 
     
    243252    return $name; 
    244253 
     254} 
     255 
     256function _make_post( $param, $ispublic, $postid = -1 ) 
     257{ 
     258    $post = new Post(); 
     259    if( $postid != -1 ) 
     260    { 
     261        if( !$post->open( $postid ) ) 
     262        { 
     263            return false; 
     264        } 
     265    } 
     266 
     267    $post->content = $param['description']; 
     268    $post->title = $param['title']; 
     269    $post->tags = array_merge( split(",", $param['mt_excerpt']) , $param['tagwords'] ); 
     270 
     271    $post->created = _timestamp( $param['dateCreated'] ); 
     272    $post->modified = _timestamp( $param['dateCreated'] ); 
     273 
     274    $post->category = _getCategoryIdByName( $param['categories'] ); 
     275    $post->acceptComment = $param['mt_allow_comments'] !== 0 ? true : false; 
     276    $post->acceptTrackback = $param['mt_allow_pings'] !== 0 ? true : false; 
     277 
     278    if( $ispublic ) 
     279    { 
     280        $post->visibility = "public"; 
     281        $post->published = _timestamp( $param['dateCreated'] ); 
     282    } 
     283    else 
     284    { 
     285        $post->visibility = "private"; 
     286    } 
     287 
     288    return $post; 
     289} 
     290 
     291function _get_post( $post, $type = "bl" ) 
     292{  
     293    DEBUG( "Enter: " . __FUNCTION__ . "\n" ); 
     294    $post->loadTags(); 
     295    DEBUG( "Post " ); 
     296    DEBUG( $post, true ); 
     297    $params = func_get_args(); 
     298    global $service, $hostURL, $blogURL; 
     299    return array(  
     300                "userid" => "", 
     301                "dateCreated" => _dateiso8601( $post->created ), 
     302                "datePosted" => _dateiso8601( $post->published ), 
     303                "dateModified" => _dateiso8601( $post->modified ), 
     304                "title" =>  _escape_content($post->title), 
     305                "postid" => $post->id, 
     306                "categories" => array( _getCategoryNameById($post->category) ), 
     307                "link" => $hostURL . $blogURL . "/" . $post->id , 
     308                "permaLink" => $hostURL . $blogURL . "/" . $post->id , 
     309                "description" => ($type == "mt" ? $post->content : "" ), 
     310                "content" => $post->content, 
     311                "mt_allow_comments" => $post->acceptComment ? 1 : 0, 
     312                "mt_allow_pings" => $post->acceptTrackback ? 1 : 0, 
     313                "mt_excerpt" => join( ",", $post->tags ) 
     314                ); 
    245315} 
    246316 
     
    317387} 
    318388 
     389 
     390function _file_hash( $content ) 
     391{ 
     392    $md5sum = md5( $content ); 
     393    return sprintf( "ta%stt%ser%s", substr( $md5sum, 0, 7 ), substr( $md5sum, 7, 7 ), substr( $md5sum, 14, 7 ) ); 
     394} 
     395 
     396 
    319397function addAttachment($owner,$parent,$file){ 
    320398    global $database; 
     
    342420            break; 
    343421    } 
     422 
     423    /* Create directory for owner */ 
    344424    $path="../../attach/$owner"; 
    345425    if(!is_dir($path)){ 
     
    349429        @chmod($path,0777); 
    350430    } 
    351     do{ 
    352         $attachment['name']=rand(1000000000,9999999999).".$extension"; 
    353         $attachment['path']="$path/{$attachment['name']}"; 
    354     }while(file_exists($attachment['path'])); 
    355  
    356     /* Fixed by coolengineer */ 
     431 
     432    /* Select unique file name from md5sum of content */ 
     433    $attachment['name'] = _file_hash( $file['content'] )  . ".$extension"; 
     434    $attachment['path'] = "$path/{$attachment['name']}"; 
     435 
     436DEBUG("\nCHECK " . __LINE__ ); 
     437    deleteAttachment($owner,-1,$attachment['name']); 
     438DEBUG("\nCHECK " . __LINE__ ); 
     439 
    357440    if( $file['content'] ) 
    358441    { 
     
    362445            return false; 
    363446        } 
    364         DEBUG( "Length 1: " . $attachment['size'] . "\n"); 
    365447        $attachment['size'] = fwrite( $f, $file['content'] ); 
    366         DEBUG( "Length 2: " . $attachment['size'] . "\n"); 
    367448        fclose( $f ); 
    368449        $file['tmp_name'] = $attachment['path']; 
     
    382463        return false; 
    383464*/ 
     465DEBUG("\nCHECK " . __LINE__ ); 
    384466    @chmod($attachment['path'],0666); 
     467    DEBUG( "\ninsert into {$database['prefix']}Attachments values ($owner, {$attachment['parent']}, '{$attachment['name']}', '$label', '{$attachment['mime']}', {$attachment['size']}, {$attachment['width']}, {$attachment['height']}, UNIX_TIMESTAMP(), 0,0)"); 
    385468    $result=mysql_query("insert into {$database['prefix']}Attachments values ($owner, {$attachment['parent']}, '{$attachment['name']}', '$label', '{$attachment['mime']}', {$attachment['size']}, {$attachment['width']}, {$attachment['height']}, UNIX_TIMESTAMP(), 0,0)"); 
    386469    if(!$result){ 
     470DEBUG("\nCHECK " . __LINE__ ); 
    387471        @unlink($attachment['path']); 
    388472        return false; 
    389473    } 
     474DEBUG("\nCHECK " . __LINE__ ); 
    390475    return $attachment; 
    391476} 
     477 
    392478 
    393479/* Up to here, copied from blog/owner/entry/attach/index.php */ 
     
    406492    global $database, $blogapi_dir; 
    407493    @unlink("$blogapi_dir/../../attach/$owner/$name"); 
    408     if($parent===false){ 
     494    $name=mysql_escape_string($name); 
     495    $parent_clause = ""; 
     496    if( $parent >= 0 ) 
     497    { 
     498        $parent_clause = "parent = $parent and "; 
     499    } 
     500    DEBUG("\ndelete from {$database['prefix']}Attachments where owner = $owner and $parent_clause name = '$name'"); 
     501    if(mysql_query("delete from {$database['prefix']}Attachments where owner = $owner and $parent_clause name = '$name'")&&(mysql_affected_rows()==1)){ 
    409502        return true; 
    410503    } 
    411     $name=mysql_escape_string($name); 
    412     if(mysql_query("delete from {$database['prefix']}Attachments where owner = $owner and parent = $parent and name = '$name'")&&(mysql_affected_rows()==1)){ 
    413         return true; 
    414     } 
     504    DEBUG("\nDelete failure: " . mysql_error() ); 
    415505    return false; 
    416506} 
     
    429519/* Work around end */ 
    430520 
    431 function preview_encode( $owner, $name ) 
    432 { 
    433     return "?__preview__{" . $owner . "," . $name . "}"; 
    434 } 
    435  
    436 function preview_decode_core( $content ) 
    437 { 
    438     if( preg_match( "/\?__preview__{([^,]+),([^}]+)}/", $content, $matches ) ) 
    439     { 
    440         return array( $matches[1], $matches[2] ); 
    441     } 
    442     return false; 
    443 } 
    444  
    445 function preview_decode( $content, $parent ) 
     521function _get_attaches( $content, $parent ) 
    446522{ 
    447523    global $owner; 
    448     $attaches = array(); 
    449     while( list($o,$n) = preview_decode_core($content) ) 
    450     { 
    451 DEBUG("Owner: $o, Name $n\n" ); 
    452         array_push( $attaches, array( $o, $n ) ); 
    453         $content = preg_replace( "/\?__preview__[^}]+}/", "", $content, 1 ); 
    454     } 
    455     DEBUG( $attaches, true ); 
    456     return array( $content, $attaches ); 
    457 } 
    458  
    459 function fixAttachments( $attaches, $parent ) 
     524    preg_match_all( "/attach\/$owner\/(ta.{7}tt.{7}er.{7}\.[a-z]{2,5})/", $content, $matches ); 
     525    DEBUG( $matches[1], true ); 
     526    return $matches[1]; 
     527} 
     528 
     529function _update_attaches( $attaches, $parent ) 
    460530{ 
    461531    global $database, $owner; 
    462     deleteAttachments( $owner, $parent ); 
    463532    foreach( $attaches as $att ) 
    464533    { 
    465         if( $att[0] == $owner ) 
    466         { 
    467             DEBUG( "update {$database['prefix']}Attachments set parent=$parent where owner=$owner and parent=0 and name='" . $att[1] . "'\n"); 
    468             mysql_query( "update {$database['prefix']}Attachments set parent=$parent where owner=$owner and parent=0 and name='" . $att[1] . "'"); 
    469         } 
     534        DEBUG( "update {$database['prefix']}Attachments set parent=$parent where owner=$owner and parent=0 and name='" . $att . "'\n"); 
     535        mysql_query( "update {$database['prefix']}Attachments set parent=$parent where owner=$owner and parent=0 and name='" . $att . "'"); 
    470536    } 
    471537} 
  • sandbox/plugins/BlogAPI/metaweblog.php

    r604 r624  
    1919 
    2020    $cat = array(); 
    21     while(1) 
     21    while($category->id) 
    2222    { 
    2323        DEBUG( " Category: " . $category->name . "\n" ); 
     
    139139    } 
    140140 
    141     $post = new Post(); 
    142     $post->content = $params[3]['description']; 
    143     $post->title = $params[3]['title']; 
    144     $post->tags = join( ',', $params[3]['mt_keywords'] ); 
    145     $post->created = _timestamp( $params[3]['dateCreated'] ); 
    146     $post->modified = _timestamp( $params[3]['dateCreated'] ); 
    147     $post->published = _timestamp( $params[3]['dateCreated'] ); 
    148     $post->category = _getCategoryIdByName( $params[3]['categories'] ); 
    149  
    150     if( $params[4] ) 
    151     { 
    152         $post->visibility = "public"; 
    153     } 
    154     else 
    155     { 
    156         $post->visibility = "private"; 
    157     } 
    158  
    159     list( $post->content, $attaches ) = preview_decode( $post->content ); 
     141    $post = _make_post( $params[3], $params[4] ); 
     142 
     143    $attaches = _get_attaches( $post->content ); 
    160144 
    161145    if( !$post->add() ) 
     
    163147        $post->close(); 
    164148        DEBUG( "Adding failure." ); 
    165         return XMLRPCFault( 1, "Posting error" ); 
    166     } 
    167  
    168     fixAttachments( $attaches, $post->id ); 
     149        return XMLRPCFault( 1, "Tattertools posting error" ); 
     150    } 
     151 
     152    _update_attaches( $attaches, $post->id ); 
    169153    RSS::refresh(); 
    170154 
     
    249233    } 
    250234 
    251     $post = new Post(); 
    252     if( !$post->open( $params[0] ) ) 
    253     { 
    254         return XMLRPCFault( 1, "Posting error" ); 
    255     } 
    256  
    257     $post->content = $params[3]['description']; 
    258     $post->title = $params[3]['title']; 
    259     $post->tags = join( ',', $params[3]['categories'] ); 
    260     $post->modified = _timestamp( $params[3]['dateCreated'] ); 
    261  
    262     list( $post->content, $attaches ) = preview_decode( $post->content ); 
    263  
    264     if( $params[4] ) 
    265     { 
    266         $post->visibility = "public"; 
    267         $post->published = _timestamp( $params[3]['dateCreated'] ); 
    268     } 
    269     else 
    270     { 
    271         $post->visibility = "private"; 
    272     } 
     235    $post = _make_post( $params[3], $params[4], $params[0] ); 
     236    if( !$post ) 
     237    { 
     238        return XMLRPCFault( 1, "Tattertools editing error" ); 
     239    } 
     240 
     241    $attaches = _get_attaches( $post->content ); 
    273242 
    274243    $ret = $post->update(); 
    275244 
    276     fixAttachments( $attaches, $post->id ); 
     245    _update_attaches( $attaches, $post->id ); 
    277246    RSS::refresh(); 
    278247 
     
    319288        return new XMLRPCFault( 1, "Can't create file" ); 
    320289    } 
    321     return array ( 'url' => getBlogURL() . "/attach/$owner/" . $attachment['name'] . preview_encode( $owner, $attachment['name'] ) ); 
     290    $attachurl = array ( 'url' => getBlogURL() . "/attach/$owner/" . $attachment['name'] ); 
     291    DEBUG( "\nAttached url : " ); 
     292    DEBUG( $attachurl, true ); 
     293    return $attachurl; 
    322294} 
    323295?>